• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.overlay.FeatureFactory;
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_WITH_DELAY =
37             Duration.ofMinutes(40).toMillis();
38     private static final long RESCHEDULE_FOR_BOOT_ACTION_WITHOUT_DELAY =
39             Duration.ofSeconds(6).toMillis();
40 
41     private final Handler mHandler = new Handler(Looper.getMainLooper());
42 
43     public static final String ACTION_PERIODIC_JOB_RECHECK =
44             "com.android.settings.battery.action.PERIODIC_JOB_RECHECK";
45     public static final String ACTION_SETUP_WIZARD_FINISHED =
46             "com.google.android.setupwizard.SETUP_WIZARD_FINISHED";
47 
48     /** Invokes periodic job rechecking process. */
invokeJobRecheck(Context context)49     public static void invokeJobRecheck(Context context) {
50         context = context.getApplicationContext();
51         final Intent intent = new Intent(ACTION_PERIODIC_JOB_RECHECK);
52         intent.setClass(context, BootBroadcastReceiver.class);
53         context.sendBroadcast(intent);
54     }
55 
56     @Override
onReceive(Context context, Intent intent)57     public void onReceive(Context context, Intent intent) {
58         final String action = intent == null ? "" : intent.getAction();
59         if (DatabaseUtils.isWorkProfile(context)) {
60             Log.w(TAG, "do not start job for work profile action=" + action);
61             return;
62         }
63 
64         switch (action) {
65             case Intent.ACTION_BOOT_COMPLETED:
66             case ACTION_SETUP_WIZARD_FINISHED:
67             case ACTION_PERIODIC_JOB_RECHECK:
68                 Log.d(TAG, "refresh periodic job from action=" + action);
69                 refreshJobs(context);
70                 break;
71             case Intent.ACTION_TIME_CHANGED:
72                 Log.d(TAG, "refresh job and clear all data from action=" + action);
73                 DatabaseUtils.clearAll(context);
74                 PeriodicJobManager.getInstance(context).refreshJob(/*fromBoot=*/ false);
75                 break;
76             default:
77                 Log.w(TAG, "receive unsupported action=" + action);
78         }
79 
80         // Waits a while to recheck the scheduler to avoid AlarmManager is not ready.
81         if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
82             final Intent recheckIntent = new Intent(ACTION_PERIODIC_JOB_RECHECK);
83             recheckIntent.setClass(context, BootBroadcastReceiver.class);
84             final long delayedTime = getRescheduleTimeForBootAction(context);
85             mHandler.postDelayed(() -> context.sendBroadcast(recheckIntent), delayedTime);
86 
87             // Refreshes the usage source from UsageStatsManager when booting.
88             DatabaseUtils.removeUsageSource(context);
89 
90             BatteryUsageLogUtils.writeLog(context, Action.RECHECK_JOB, "delay:" + delayedTime);
91         } else if (ACTION_SETUP_WIZARD_FINISHED.equals(action)) {
92             ElapsedTimeUtils.storeSuwFinishedTimestamp(context, System.currentTimeMillis());
93         }
94     }
95 
getRescheduleTimeForBootAction(Context context)96     private long getRescheduleTimeForBootAction(Context context) {
97         final boolean delayHourlyJobWhenBooting =
98                 FeatureFactory.getFactory(context)
99                         .getPowerUsageFeatureProvider(context)
100                         .delayHourlyJobWhenBooting();
101         return delayHourlyJobWhenBooting
102                 ? RESCHEDULE_FOR_BOOT_ACTION_WITH_DELAY
103                 : RESCHEDULE_FOR_BOOT_ACTION_WITHOUT_DELAY;
104     }
105 
refreshJobs(Context context)106     private static void refreshJobs(Context context) {
107         PeriodicJobManager.getInstance(context).refreshJob(/*fromBoot=*/ true);
108     }
109 }
110