Home | History | Annotate | Download | only in impl
      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.telecom.PhoneAccountHandle;
     33 import com.android.dialer.constants.ScheduledJobIds;
     34 
     35 /**
     36  * JobService triggered when the setup wizard is completed, and rerun all {@link ActivationTask}
     37  * scheduled during the setup.
     38  */
     39 @TargetApi(VERSION_CODES.O)
     40 public class DeviceProvisionedJobService extends JobService {
     41 
     42   private static final String EXTRA_PHONE_ACCOUNT_HANDLE = "EXTRA_PHONE_ACCOUNT_HANDLE";
     43 
     44   /** Queue the phone account to be reactivated after the setup wizard has completed. */
     45   public static void activateAfterProvisioned(
     46       Context context, PhoneAccountHandle phoneAccountHandle) {
     47     JobInfo jobInfo =
     48         new JobInfo.Builder(
     49                 ScheduledJobIds.VVM_DEVICE_PROVISIONED_JOB,
     50                 new ComponentName(context, DeviceProvisionedJobService.class))
     51             .addTriggerContentUri(
     52                 new TriggerContentUri(Global.getUriFor(Global.DEVICE_PROVISIONED), 0))
     53             // VVM activation must be run as soon as possible to avoid voicemail loss
     54             .setTriggerContentMaxDelay(0)
     55             .build();
     56 
     57     Intent intent = new Intent();
     58     intent.putExtra(EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
     59     context.getSystemService(JobScheduler.class).enqueue(jobInfo, new JobWorkItem(intent));
     60   }
     61 
     62   @Override
     63   public boolean onStartJob(JobParameters params) {
     64     Assert.isTrue(isDeviceProvisioned());
     65     VvmLog.i("DeviceProvisionedJobService.onStartJob", "device provisioned");
     66     for (JobWorkItem item = params.dequeueWork(); item != null; item = params.dequeueWork()) {
     67       PhoneAccountHandle phoneAccountHandle =
     68           item.getIntent().getParcelableExtra(EXTRA_PHONE_ACCOUNT_HANDLE);
     69       VvmLog.i(
     70           "DeviceProvisionedJobService.onStartJob",
     71           "restarting activation for " + phoneAccountHandle);
     72       ActivationTask.start(this, phoneAccountHandle, null);
     73     }
     74     return false; // job not running in background
     75   }
     76 
     77   @Override
     78   public boolean onStopJob(JobParameters params) {
     79     return true; // reschedule job
     80   }
     81 
     82   private boolean isDeviceProvisioned() {
     83     return Settings.Global.getInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 0) == 1;
     84   }
     85 }
     86