1 /* 2 * Copyright (C) 2022 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.batteryusage; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.Handler; 23 import android.os.Looper; 24 import android.util.Log; 25 26 import com.android.settings.core.instrumentation.ElapsedTimeUtils; 27 import com.android.settings.fuelgauge.BatteryUsageHistoricalLogEntry.Action; 28 import com.android.settings.fuelgauge.batteryusage.bugreport.BatteryUsageLogUtils; 29 import com.android.settingslib.fuelgauge.BatteryUtils; 30 31 import java.time.Duration; 32 33 /** Receives broadcasts to start or stop the periodic fetching job. */ 34 public final class BootBroadcastReceiver extends BroadcastReceiver { 35 private static final String TAG = "BootBroadcastReceiver"; 36 private static final long RESCHEDULE_FOR_BOOT_ACTION_DELAY_MILLIS = 37 Duration.ofSeconds(6).toMillis(); 38 39 private final Handler mHandler = new Handler(Looper.getMainLooper()); 40 41 public static final String ACTION_PERIODIC_JOB_RECHECK = 42 "com.android.settings.battery.action.PERIODIC_JOB_RECHECK"; 43 public static final String ACTION_SETUP_WIZARD_FINISHED = 44 "com.google.android.setupwizard.SETUP_WIZARD_FINISHED"; 45 46 /** Invokes periodic job rechecking process. */ invokeJobRecheck(Context context)47 public static void invokeJobRecheck(Context context) { 48 context = context.getApplicationContext(); 49 final Intent intent = new Intent(ACTION_PERIODIC_JOB_RECHECK); 50 intent.setClass(context, BootBroadcastReceiver.class); 51 context.sendBroadcast(intent); 52 } 53 54 @Override onReceive(Context context, Intent intent)55 public void onReceive(Context context, Intent intent) { 56 final String action = intent == null ? "" : intent.getAction(); 57 if (BatteryUtils.isWorkProfile(context)) { 58 Log.w(TAG, "do not start job for work profile action=" + action); 59 return; 60 } 61 62 switch (action) { 63 case Intent.ACTION_BOOT_COMPLETED: 64 case ACTION_SETUP_WIZARD_FINISHED: 65 case ACTION_PERIODIC_JOB_RECHECK: 66 Log.d(TAG, "refresh periodic job from action=" + action); 67 refreshJobs(context); 68 break; 69 case Intent.ACTION_TIME_CHANGED: 70 Log.d(TAG, "refresh job and clear data from action=" + action); 71 DatabaseUtils.clearDataAfterTimeChangedIfNeeded(context, intent); 72 break; 73 case Intent.ACTION_TIMEZONE_CHANGED: 74 Log.d(TAG, "refresh job and clear all data from action=" + action); 75 DatabaseUtils.clearDataAfterTimeZoneChangedIfNeeded(context); 76 break; 77 default: 78 Log.w(TAG, "receive unsupported action=" + action); 79 } 80 81 // Waits a while to recheck the scheduler to avoid AlarmManager is not ready. 82 if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { 83 final Intent recheckIntent = new Intent(ACTION_PERIODIC_JOB_RECHECK); 84 recheckIntent.setClass(context, BootBroadcastReceiver.class); 85 final long delayedTime = RESCHEDULE_FOR_BOOT_ACTION_DELAY_MILLIS; 86 mHandler.postDelayed(() -> context.sendBroadcast(recheckIntent), delayedTime); 87 88 // Refreshes the usage source from UsageStatsManager when booting. 89 DatabaseUtils.removeUsageSource(context); 90 91 BatteryUsageLogUtils.writeLog(context, Action.RECHECK_JOB, "delay:" + delayedTime); 92 } else if (ACTION_SETUP_WIZARD_FINISHED.equals(action)) { 93 ElapsedTimeUtils.storeSuwFinishedTimestamp(context, System.currentTimeMillis()); 94 } 95 } 96 refreshJobs(Context context)97 private static void refreshJobs(Context context) { 98 PeriodicJobManager.getInstance(context).refreshJob(/* fromBoot= */ true); 99 } 100 } 101