diff options
author | Eric Erfanian <erfanian@google.com> | 2017-05-05 15:29:54 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-05-05 15:29:54 +0000 |
commit | 43b3152d075138b11e1fe297682b3bbc775e5641 (patch) | |
tree | 3a325b0effac02fbd228b8ddf2f96589e5df72cd /java/com/android/voicemail/impl/scheduling/TaskReceiver.java | |
parent | 2034759538273aed8a0b412d8fb90994eba1f7ea (diff) | |
parent | 300a8683b9c28424fb0f0c571a0abe128c461f69 (diff) |
Update Dialer to V10 RC16 am: 10b34a5ebf
am: 300a8683b9
Change-Id: If5695bad59f8a4dab6e6c90b54943b900bd2652a
Diffstat (limited to 'java/com/android/voicemail/impl/scheduling/TaskReceiver.java')
-rw-r--r-- | java/com/android/voicemail/impl/scheduling/TaskReceiver.java | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/java/com/android/voicemail/impl/scheduling/TaskReceiver.java b/java/com/android/voicemail/impl/scheduling/TaskReceiver.java new file mode 100644 index 000000000..00d36d00f --- /dev/null +++ b/java/com/android/voicemail/impl/scheduling/TaskReceiver.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.voicemail.impl.scheduling; + +import android.annotation.TargetApi; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Build.VERSION_CODES; +import android.os.Bundle; +import com.android.voicemail.impl.VvmLog; +import java.util.ArrayList; +import java.util.List; + +/** + * BroadcastReceiver to queue and run {@link Task} with the {@link android.app.job.JobScheduler}. A + * task is queued using a explicit broadcast to this receiver. The intent should contain enough + * information in {@link Intent#getExtras()} to construct the task (see {@link + * Tasks#createIntent(Context, Class)}). The task will be queued directly in {@link TaskExecutor} if + * it is already running, or in {@link TaskSchedulerJobService} if not. + */ +@TargetApi(VERSION_CODES.O) +public class TaskReceiver extends BroadcastReceiver { + + private static final String TAG = "VvmTaskReceiver"; + + private static final List<Intent> deferredBroadcasts = new ArrayList<>(); + + /** + * When {@link TaskExecutor#isTerminating()} is {@code true}, newly added tasks will be deferred + * to allow the TaskExecutor to terminate properly. After termination is completed this should be + * called to add the tasks again. + */ + public static void resendDeferredBroadcasts(Context context) { + for (Intent intent : deferredBroadcasts) { + context.sendBroadcast(intent); + } + } + + @Override + public void onReceive(Context context, Intent intent) { + if (intent == null) { + VvmLog.w(TAG, "null intent received"); + return; + } + VvmLog.i(TAG, "task received"); + TaskExecutor taskExecutor = TaskExecutor.getRunningInstance(); + if (taskExecutor != null) { + VvmLog.i(TAG, "TaskExecutor already running"); + if (taskExecutor.isTerminating()) { + // The current taskExecutor and cannot do anything and a new job cannot be scheduled. Defer + // the task until a new job can be scheduled. + VvmLog.w(TAG, "TaskExecutor is terminating, bouncing task"); + deferredBroadcasts.add(intent); + return; + } + Task task = Tasks.createTask(context, 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); + } + } +} |