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.app.PendingIntent;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.util.Log;
24 
25 import com.android.settings.fuelgauge.BatteryUsageHistoricalLogEntry.Action;
26 import com.android.settings.fuelgauge.batteryusage.bugreport.BatteryUsageLogUtils;
27 import com.android.settingslib.fuelgauge.BatteryUtils;
28 
29 /** Receives the periodic alarm {@link PendingIntent} callback. */
30 public final class PeriodicJobReceiver extends BroadcastReceiver {
31     private static final String TAG = "PeriodicJobReceiver";
32     public static final String ACTION_PERIODIC_JOB_UPDATE =
33             "com.android.settings.battery.action.PERIODIC_JOB_UPDATE";
34 
35     @Override
onReceive(Context context, Intent intent)36     public void onReceive(Context context, Intent intent) {
37         try {
38             loadDataAndRefreshJob(context, intent);
39         } catch (Exception e) {
40             BatteryUsageLogUtils.writeLog(
41                     context,
42                     Action.SCHEDULE_JOB,
43                     String.format("loadDataAndRefreshJob() failed: %s", e));
44         }
45     }
46 
loadDataAndRefreshJob(Context context, Intent intent)47     private static void loadDataAndRefreshJob(Context context, Intent intent) {
48         final String action = intent == null ? "" : intent.getAction();
49         if (!ACTION_PERIODIC_JOB_UPDATE.equals(action)) {
50             Log.w(TAG, "receive unexpected action=" + action);
51             return;
52         }
53         if (BatteryUtils.isWorkProfile(context)) {
54             BatteryUsageLogUtils.writeLog(
55                     context, Action.SCHEDULE_JOB, "do not refresh job for work profile");
56             Log.w(TAG, "do not refresh job for work profile action=" + action);
57             return;
58         }
59         BatteryUsageLogUtils.writeLog(context, Action.EXECUTE_JOB, "");
60         BatteryUsageDataLoader.enqueueWork(context, /* isFullChargeStart= */ false);
61         Log.d(TAG, "refresh periodic job from action=" + action);
62         PeriodicJobManager.getInstance(context).refreshJob(/* fromBoot= */ false);
63         DatabaseUtils.clearExpiredDataIfNeeded(context);
64     }
65 }
66