The article referenced in @mattm's answer is a bit outdated. IdleMaintenanceService was removed in Lollipop, things have changed. On Pie, scheduled fstrim is triggered from StorageManagerService (1) to MountService (2):
if the device is alive at midnight + idle, fstrim gets run at the next available charging + idle time
Mount service calls ...