1 /* 2 * Copyright (C) 2021 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.launcher3.model; 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.util.Log; 26 27 import com.android.launcher3.R; 28 29 /** 30 * A job to request AppShareabilityManager to update its shareability data 31 * The shareability status of an app is not expected to change often, so this job is only 32 * run periodically. 33 */ 34 public final class AppShareabilityJobService extends JobService { 35 36 private static final String TAG = "AppShareabilityJobService"; 37 // Run this job once a week 38 private static final int RECURRENCE_INTERVAL_MILLIS = 604800000; 39 40 @Override onStartJob(final JobParameters params)41 public boolean onStartJob(final JobParameters params) { 42 Context context = getApplicationContext(); 43 AppShareabilityManager.INSTANCE.get(context).requestFullUpdate(); 44 return false; // Job is finished 45 } 46 47 @Override onStopJob(final JobParameters params)48 public boolean onStopJob(final JobParameters params) { 49 Log.d(TAG, "App shareability data update job stopped; id=" + params.getJobId() 50 + ", reason=" 51 + JobParameters.getInternalReasonCodeDescription(params.getStopReason())); 52 return true; // Reschedule the job 53 } 54 55 /** 56 * Creates and schedules the job. 57 * Does not schedule a duplicate job if one is already pending. 58 * @param context The application context 59 */ schedule(Context context)60 public static void schedule(Context context) { 61 final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); 62 63 final JobInfo pendingJob = jobScheduler.getPendingJob(R.integer.app_shareability_job_id); 64 if (pendingJob != null) { 65 // Don't schedule duplicate jobs 66 return; 67 } 68 69 final JobInfo newJob = new JobInfo.Builder(R.integer.app_shareability_job_id, 70 new ComponentName(context, AppShareabilityJobService.class)) 71 .setPeriodic(RECURRENCE_INTERVAL_MILLIS) 72 .setPersisted(true) 73 .setRequiresBatteryNotLow(true) 74 .build(); 75 jobScheduler.schedule(newJob); 76 } 77 } 78