1 /* 2 * Copyright (C) 2020 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.server.people.data; 18 19 import android.annotation.UserIdInt; 20 import android.app.job.JobInfo; 21 import android.app.job.JobParameters; 22 import android.app.job.JobScheduler; 23 import android.app.job.JobService; 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.os.CancellationSignal; 27 28 import com.android.server.LocalServices; 29 import com.android.server.people.PeopleServiceInternal; 30 31 import java.util.concurrent.TimeUnit; 32 33 /** 34 * This service runs periodically to ensure the data consistency and the retention policy for a 35 * specific user's data. 36 */ 37 public class DataMaintenanceService extends JobService { 38 39 private static final long JOB_RUN_INTERVAL = TimeUnit.HOURS.toMillis(24); 40 41 /** This job ID must be unique within the system server. */ 42 private static final int BASE_JOB_ID = 0xC315BD7; // 204561367 43 scheduleJob(Context context, @UserIdInt int userId)44 static void scheduleJob(Context context, @UserIdInt int userId) { 45 int jobId = getJobId(userId); 46 JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); 47 if (jobScheduler.getPendingJob(jobId) == null) { 48 ComponentName component = new ComponentName(context, DataMaintenanceService.class); 49 JobInfo newJob = new JobInfo.Builder(jobId, component) 50 .setRequiresDeviceIdle(true) 51 .setPeriodic(JOB_RUN_INTERVAL) 52 .build(); 53 jobScheduler.schedule(newJob); 54 } 55 } 56 cancelJob(Context context, @UserIdInt int userId)57 static void cancelJob(Context context, @UserIdInt int userId) { 58 JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); 59 jobScheduler.cancel(getJobId(userId)); 60 } 61 62 private CancellationSignal mSignal; 63 64 @Override onStartJob(JobParameters params)65 public boolean onStartJob(JobParameters params) { 66 int userId = getUserId(params.getJobId()); 67 mSignal = new CancellationSignal(); 68 new Thread(() -> { 69 PeopleServiceInternal peopleServiceInternal = 70 LocalServices.getService(PeopleServiceInternal.class); 71 peopleServiceInternal.pruneDataForUser(userId, mSignal); 72 jobFinished(params, mSignal.isCanceled()); 73 }).start(); 74 return true; 75 } 76 77 @Override onStopJob(JobParameters params)78 public boolean onStopJob(JobParameters params) { 79 if (mSignal != null) { 80 mSignal.cancel(); 81 } 82 return false; 83 } 84 getJobId(@serIdInt int userId)85 private static int getJobId(@UserIdInt int userId) { 86 return BASE_JOB_ID + userId; 87 } 88 getUserId(int jobId)89 private static @UserIdInt int getUserId(int jobId) { 90 return jobId - BASE_JOB_ID; 91 } 92 } 93