1 /**
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  * <p>http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * <p>Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11  * express or implied. See the License for the specific language governing permissions and
12  * limitations under the License
13  */
14 package com.android.voicemail.impl;
15 
16 import android.annotation.TargetApi;
17 import android.app.job.JobInfo;
18 import android.app.job.JobParameters;
19 import android.app.job.JobScheduler;
20 import android.app.job.JobService;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.os.Build.VERSION_CODES;
24 import android.telecom.PhoneAccountHandle;
25 import android.telecom.TelecomManager;
26 import com.android.dialer.constants.ScheduledJobIds;
27 import com.android.voicemail.impl.sync.VvmAccountManager;
28 import java.util.concurrent.TimeUnit;
29 
30 /**
31  * A job to perform {@link StatusCheckTask} once per day, performing book keeping to ensure the
32  * credentials and status for a activated voicemail account is still correct. A task will be
33  * scheduled for each active voicemail account. The status is expected to be always in sync, the
34  * check is a failsafe to mimic the previous status check on signal return behavior.
35  */
36 @TargetApi(VERSION_CODES.O)
37 public class StatusCheckJobService extends JobService {
38 
schedule(Context context)39   public static void schedule(Context context) {
40     JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
41     if (jobScheduler.getPendingJob(ScheduledJobIds.VVM_STATUS_CHECK_JOB) != null) {
42       VvmLog.i("StatusCheckJobService.schedule", "job already scheduled");
43       return;
44     }
45 
46     jobScheduler.schedule(
47         new JobInfo.Builder(
48                 ScheduledJobIds.VVM_STATUS_CHECK_JOB,
49                 new ComponentName(context, StatusCheckJobService.class))
50             .setPeriodic(TimeUnit.DAYS.toMillis(1))
51             .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
52             .setRequiresCharging(true)
53             .build());
54   }
55 
56   @Override
onStartJob(JobParameters params)57   public boolean onStartJob(JobParameters params) {
58     for (PhoneAccountHandle phoneAccountHandle :
59         getSystemService(TelecomManager.class).getCallCapablePhoneAccounts()) {
60       if (VvmAccountManager.isAccountActivated(this, phoneAccountHandle)) {
61         StatusCheckTask.start(this, phoneAccountHandle);
62       }
63     }
64     return false; // not running in background
65   }
66 
67   @Override
onStopJob(JobParameters params)68   public boolean onStopJob(JobParameters params) {
69     return false; // don't retry
70   }
71 }
72