diff options
Diffstat (limited to 'java/com/android/voicemail/impl/scheduling')
3 files changed, 45 insertions, 4 deletions
diff --git a/java/com/android/voicemail/impl/scheduling/TaskExecutor.java b/java/com/android/voicemail/impl/scheduling/TaskExecutor.java index 84dc1db4a..e3b718e50 100644 --- a/java/com/android/voicemail/impl/scheduling/TaskExecutor.java +++ b/java/com/android/voicemail/impl/scheduling/TaskExecutor.java @@ -193,13 +193,13 @@ final class TaskExecutor { /** Should attempt to run the next task when a task has finished or been added. */ private boolean taskAutoRunDisabledForTesting = false; + /** Handles execution of the background task in teh worker thread. */ @VisibleForTesting final class WorkerThreadHandler extends Handler { public WorkerThreadHandler(Looper looper) { super(looper); } - @Override @WorkerThread public void handleMessage(Message msg) { @@ -218,6 +218,7 @@ final class TaskExecutor { } } + /** Handles completion of the background task in the main thread. */ @VisibleForTesting final class MainThreadHandler extends Handler { @@ -233,6 +234,11 @@ final class TaskExecutor { getTasks().remove(task); task.onCompleted(); isWorkerThreadBusy = false; + if (!isJobRunning() || isTerminating()) { + // TaskExecutor was terminated when the task is running in background, don't need to run the + // next task or terminate again + return; + } maybeRunNextTask(); } } @@ -290,6 +296,7 @@ final class TaskExecutor { @MainThread private void maybeRunNextTask() { Assert.isMainThread(); + if (isWorkerThreadBusy) { return; } diff --git a/java/com/android/voicemail/impl/scheduling/TaskReceiver.java b/java/com/android/voicemail/impl/scheduling/TaskReceiver.java index 00d36d00f..e78dcf72c 100644 --- a/java/com/android/voicemail/impl/scheduling/TaskReceiver.java +++ b/java/com/android/voicemail/impl/scheduling/TaskReceiver.java @@ -49,6 +49,7 @@ public class TaskReceiver extends BroadcastReceiver { for (Intent intent : deferredBroadcasts) { context.sendBroadcast(intent); } + deferredBroadcasts.clear(); } @Override @@ -68,13 +69,13 @@ public class TaskReceiver extends BroadcastReceiver { deferredBroadcasts.add(intent); return; } - Task task = Tasks.createTask(context, intent.getExtras()); + Task task = Tasks.createTask(context.getApplicationContext(), intent.getExtras()); taskExecutor.addTask(task); } else { VvmLog.i(TAG, "scheduling new job"); List<Bundle> taskList = new ArrayList<>(); taskList.add(intent.getExtras()); - TaskSchedulerJobService.scheduleJob(context, taskList, 0, true); + TaskSchedulerJobService.scheduleJob(context.getApplicationContext(), taskList, 0, true); } } } diff --git a/java/com/android/voicemail/impl/scheduling/TaskSchedulerJobService.java b/java/com/android/voicemail/impl/scheduling/TaskSchedulerJobService.java index 9bfce0052..107234edc 100644 --- a/java/com/android/voicemail/impl/scheduling/TaskSchedulerJobService.java +++ b/java/com/android/voicemail/impl/scheduling/TaskSchedulerJobService.java @@ -23,9 +23,11 @@ import android.app.job.JobScheduler; import android.app.job.JobService; import android.content.ComponentName; import android.content.Context; +import android.content.SharedPreferences; import android.os.Build.VERSION_CODES; import android.os.Bundle; import android.os.Parcelable; +import android.preference.PreferenceManager; import android.support.annotation.MainThread; import com.android.dialer.constants.ScheduledJobIds; import com.android.voicemail.impl.Assert; @@ -41,11 +43,28 @@ public class TaskSchedulerJobService extends JobService implements TaskExecutor. private static final String EXTRA_TASK_EXTRAS_ARRAY = "extra_task_extras_array"; + private static final String EXTRA_JOB_ID = "extra_job_id"; + + private static final String EXPECTED_JOB_ID = + "com.android.voicemail.impl.scheduling.TaskSchedulerJobService.EXPECTED_JOB_ID"; + + private static final String NEXT_JOB_ID = + "com.android.voicemail.impl.scheduling.TaskSchedulerJobService.NEXT_JOB_ID"; + private JobParameters jobParameters; @Override @MainThread public boolean onStartJob(JobParameters params) { + int jobId = params.getTransientExtras().getInt(EXTRA_JOB_ID); + int expectedJobId = + PreferenceManager.getDefaultSharedPreferences(this).getInt(EXPECTED_JOB_ID, 0); + if (jobId != expectedJobId) { + VvmLog.e( + TAG, "Job " + jobId + " is not the last scheduled job " + expectedJobId + ", ignoring"); + return false; // nothing more to do. Job not running in background. + } + VvmLog.i(TAG, "starting " + jobId); jobParameters = params; TaskExecutor.createRunningInstance(this); TaskExecutor.getRunningInstance() @@ -97,6 +116,13 @@ public class TaskSchedulerJobService extends JobService implements TaskExecutor. jobScheduler.cancel(ScheduledJobIds.VVM_TASK_SCHEDULER_JOB); } Bundle extras = new Bundle(); + int jobId = createJobId(context); + extras.putInt(EXTRA_JOB_ID, jobId); + PreferenceManager.getDefaultSharedPreferences(context) + .edit() + .putInt(EXPECTED_JOB_ID, jobId) + .apply(); + extras.putParcelableArray( EXTRA_TASK_EXTRAS_ARRAY, pendingTasks.toArray(new Bundle[pendingTasks.size()])); JobInfo.Builder builder = @@ -112,7 +138,7 @@ public class TaskSchedulerJobService extends JobService implements TaskExecutor. VvmLog.i(TAG, "running job instantly."); } jobScheduler.schedule(builder.build()); - VvmLog.i(TAG, "job scheduled"); + VvmLog.i(TAG, "job " + jobId + " scheduled"); } /** @@ -143,4 +169,11 @@ public class TaskSchedulerJobService extends JobService implements TaskExecutor. } return result; } + + private static int createJobId(Context context) { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + int jobId = sharedPreferences.getInt(NEXT_JOB_ID, 0); + sharedPreferences.edit().putInt(NEXT_JOB_ID, jobId + 1).apply(); + return jobId; + } } |