enqueueBufferedTasks method

Future<void> enqueueBufferedTasks()

Enqueues the tasks buffered with buffer, earliest first.

Only as many tasks are enqueued as there are free notification slots (NotificationManager.pendingNotificationLimit minus pending ones). The rest are discarded and buffered again on a later resume. Each enqueued task updates TaskControl.hasBeenScheduledUntil.

Called by the SmartphoneDeploymentExecutor when it resumes.

Implementation

Future<void> enqueueBufferedTasks() async {
  _userTaskBuffer.sort((a, b) => a.triggerTime.compareTo(b.triggerTime));
  var remainingNotifications =
      NotificationManager.pendingNotificationLimit -
      (await SmartPhoneClientManager().notificationManager.pendingNotificationRequestsCount);

  var numberOfTasksToEnqueue = min(remainingNotifications, _userTaskBuffer.length);

  // Being mindful of the OS limitations, only schedule however many
  // tasks as remaining notification slots
  List<UserTaskBufferItem> toEnqueue = _userTaskBuffer.sublist(0, numberOfTasksToEnqueue);

  debug('$runtimeType - Enqueuing ${toEnqueue.length} tasks.');

  for (var item in toEnqueue) {
    item.taskControl.hasBeenScheduledUntil = item.triggerTime;
    await enqueue(item.taskExecutor, triggerTime: item.triggerTime, sendNotification: item.sendNotification);
  }

  // Discard the tasks that we couldn't queue, they will be re-queued later.
  _userTaskBuffer.clear();
}