1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.voicemail.impl; 18 19 import android.annotation.TargetApi; 20 import android.app.job.JobInfo; 21 import android.app.job.JobInfo.TriggerContentUri; 22 import android.app.job.JobParameters; 23 import android.app.job.JobScheduler; 24 import android.app.job.JobService; 25 import android.app.job.JobWorkItem; 26 import android.content.ComponentName; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.os.Build.VERSION_CODES; 30 import android.provider.Settings; 31 import android.provider.Settings.Global; 32 import android.support.annotation.VisibleForTesting; 33 import android.telecom.PhoneAccountHandle; 34 import com.android.dialer.constants.ScheduledJobIds; 35 36 /** 37 * JobService triggered when the setup wizard is completed, and rerun all {@link ActivationTask} 38 * scheduled during the setup. 39 */ 40 @TargetApi(VERSION_CODES.O) 41 public class DeviceProvisionedJobService extends JobService { 42 43 @VisibleForTesting static final String EXTRA_PHONE_ACCOUNT_HANDLE = "EXTRA_PHONE_ACCOUNT_HANDLE"; 44 45 /** Queue the phone account to be reactivated after the setup wizard has completed. */ activateAfterProvisioned( Context context, PhoneAccountHandle phoneAccountHandle)46 public static void activateAfterProvisioned( 47 Context context, PhoneAccountHandle phoneAccountHandle) { 48 Intent intent = new Intent(); 49 intent.putExtra(EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle); 50 context 51 .getSystemService(JobScheduler.class) 52 .enqueue(createJobInfo(context), new JobWorkItem(intent)); 53 } 54 55 @Override onStartJob(JobParameters params)56 public boolean onStartJob(JobParameters params) { 57 if (!isDeviceProvisioned()) { 58 VvmLog.i("DeviceProvisionedJobService.onStartJob", "device not provisioned, rescheduling"); 59 getSystemService(JobScheduler.class).schedule(createJobInfo(this)); 60 return false; // job not running in background 61 } 62 VvmLog.i("DeviceProvisionedJobService.onStartJob", "device provisioned"); 63 for (JobWorkItem item = params.dequeueWork(); item != null; item = params.dequeueWork()) { 64 PhoneAccountHandle phoneAccountHandle = 65 item.getIntent().getParcelableExtra(EXTRA_PHONE_ACCOUNT_HANDLE); 66 VvmLog.i( 67 "DeviceProvisionedJobService.onStartJob", 68 "restarting activation for " + phoneAccountHandle); 69 ActivationTask.start(this, phoneAccountHandle, null); 70 } 71 return false; // job not running in background 72 } 73 74 @Override onStopJob(JobParameters params)75 public boolean onStopJob(JobParameters params) { 76 return true; // reschedule job 77 } 78 isDeviceProvisioned()79 private boolean isDeviceProvisioned() { 80 return Settings.Global.getInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 0) == 1; 81 } 82 createJobInfo(Context context)83 private static JobInfo createJobInfo(Context context) { 84 return new JobInfo.Builder( 85 ScheduledJobIds.VVM_DEVICE_PROVISIONED_JOB, 86 new ComponentName(context, DeviceProvisionedJobService.class)) 87 .addTriggerContentUri(new TriggerContentUri(Global.getUriFor(Global.DEVICE_PROVISIONED), 0)) 88 // VVM activation must be run as soon as possible to avoid voicemail loss 89 .setTriggerContentMaxDelay(0) 90 .build(); 91 } 92 } 93