summaryrefslogtreecommitdiff
path: root/java/com/android/voicemail/impl/StatusCheckJobService.java
blob: 870c5b47190876173395b80303255830f97cbca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * Copyright (C) 2017 The Android Open Source Project
 *
 * <p>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
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>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;

import android.annotation.TargetApi;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build.VERSION_CODES;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import com.android.dialer.constants.ScheduledJobIds;
import com.android.voicemail.impl.sync.VvmAccountManager;
import java.util.concurrent.TimeUnit;

/**
 * A job to perform {@link StatusCheckTask} once per day, performing book keeping to ensure the
 * credentials and status for a activated voicemail account is still correct. A task will be
 * scheduled for each active voicemail account. The status is expected to be always in sync, the
 * check is a failsafe to mimic the previous status check on signal return behavior.
 */
@TargetApi(VERSION_CODES.O)
public class StatusCheckJobService extends JobService {

  public static void schedule(Context context) {
    JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
    if (jobScheduler.getPendingJob(ScheduledJobIds.VVM_STATUS_CHECK_JOB) != null) {
      VvmLog.i("StatusCheckJobService.schedule", "job already scheduled");
      return;
    }

    jobScheduler.schedule(
        new JobInfo.Builder(
                ScheduledJobIds.VVM_STATUS_CHECK_JOB,
                new ComponentName(context, StatusCheckJobService.class))
            .setPeriodic(TimeUnit.DAYS.toMillis(1))
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .setRequiresCharging(true)
            .build());
  }

  @Override
  public boolean onStartJob(JobParameters params) {
    for (PhoneAccountHandle phoneAccountHandle :
        getSystemService(TelecomManager.class).getCallCapablePhoneAccounts()) {
      if (VvmAccountManager.isAccountActivated(this, phoneAccountHandle)) {
        StatusCheckTask.start(this, phoneAccountHandle);
      }
    }
    return false; // not running in background
  }

  @Override
  public boolean onStopJob(JobParameters params) {
    return false; // don't retry
  }
}