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