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.dialer.app.calllog;
18 
19 import android.app.job.JobInfo;
20 import android.app.job.JobParameters;
21 import android.app.job.JobScheduler;
22 import android.app.job.JobService;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.provider.VoicemailContract;
26 import com.android.dialer.common.LogUtil;
27 import com.android.dialer.constants.ScheduledJobIds;
28 
29 /** Monitors voicemail provider changes to update active notifications. */
30 public class VoicemailNotificationJobService extends JobService {
31 
32   private static JobInfo jobInfo;
33 
34   /**
35    * Start monitoring the provider. The provider should be monitored whenever a visual voicemail
36    * notification is visible.
37    */
scheduleJob(Context context)38   public static void scheduleJob(Context context) {
39     context.getSystemService(JobScheduler.class).schedule(getJobInfo(context));
40     LogUtil.i("VoicemailNotificationJobService.scheduleJob", "job scheduled");
41   }
42 
43   /**
44    * Stop monitoring the provider. The provider should not be monitored when visual voicemail
45    * notification is cleared.
46    */
cancelJob(Context context)47   public static void cancelJob(Context context) {
48     context.getSystemService(JobScheduler.class).cancel(ScheduledJobIds.VVM_NOTIFICATION_JOB);
49     LogUtil.i("VoicemailNotificationJobService.scheduleJob", "job canceled");
50   }
51 
52   @Override
onStartJob(JobParameters params)53   public boolean onStartJob(JobParameters params) {
54     LogUtil.i("VoicemailNotificationJobService.onStartJob", "updating notification");
55     VisualVoicemailUpdateTask.scheduleTask(
56         this,
57         () -> {
58           jobFinished(params, false);
59         });
60     return true; // Running in background
61   }
62 
63   @Override
onStopJob(JobParameters params)64   public boolean onStopJob(JobParameters params) {
65     return false;
66   }
67 
getJobInfo(Context context)68   private static JobInfo getJobInfo(Context context) {
69     if (jobInfo == null) {
70       jobInfo =
71           new JobInfo.Builder(
72                   ScheduledJobIds.VVM_NOTIFICATION_JOB,
73                   new ComponentName(context, VoicemailNotificationJobService.class))
74               .addTriggerContentUri(
75                   new JobInfo.TriggerContentUri(
76                       VoicemailContract.Voicemails.CONTENT_URI,
77                       JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS))
78               .setTriggerContentMaxDelay(0)
79               .build();
80     }
81 
82     return jobInfo;
83   }
84 }
85