1 /* 2 * Copyright (C) 2018 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.settings.fuelgauge.batterytip; 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 androidx.annotation.VisibleForTesting; 28 29 import com.android.settings.R; 30 import com.android.settingslib.utils.ThreadUtils; 31 32 import java.util.concurrent.TimeUnit; 33 34 /** A JobService to clean up obsolete data in anomaly database */ 35 public class AnomalyCleanupJobService extends JobService { 36 private static final String TAG = "AnomalyCleanUpJobService"; 37 38 @VisibleForTesting 39 static final long CLEAN_UP_FREQUENCY_MS = TimeUnit.DAYS.toMillis(1); 40 scheduleCleanUp(Context context)41 public static void scheduleCleanUp(Context context) { 42 final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); 43 44 final ComponentName component = new ComponentName(context, AnomalyCleanupJobService.class); 45 final JobInfo.Builder jobBuilder = 46 new JobInfo.Builder(R.integer.job_anomaly_clean_up, component) 47 .setPeriodic(CLEAN_UP_FREQUENCY_MS) 48 .setRequiresDeviceIdle(true) 49 .setRequiresCharging(true) 50 .setPersisted(true); 51 final JobInfo pending = jobScheduler.getPendingJob(R.integer.job_anomaly_clean_up); 52 53 // Don't schedule it if it already exists, to make sure it runs periodically even after 54 // reboot 55 if (pending == null && jobScheduler.schedule(jobBuilder.build()) 56 != JobScheduler.RESULT_SUCCESS) { 57 Log.i(TAG, "Anomaly clean up job service schedule failed."); 58 } 59 } 60 61 @Override onStartJob(JobParameters params)62 public boolean onStartJob(JobParameters params) { 63 final BatteryDatabaseManager batteryDatabaseManager = BatteryDatabaseManager 64 .getInstance(this); 65 final BatteryTipPolicy policy = new BatteryTipPolicy(this); 66 ThreadUtils.postOnBackgroundThread(() -> { 67 batteryDatabaseManager.deleteAllAnomaliesBeforeTimeStamp( 68 System.currentTimeMillis() - TimeUnit.DAYS.toMillis( 69 policy.dataHistoryRetainDay)); 70 jobFinished(params, false /* wantsReschedule */); 71 }); 72 73 return true; 74 } 75 76 @Override onStopJob(JobParameters jobParameters)77 public boolean onStopJob(JobParameters jobParameters) { 78 return false; 79 } 80 } 81