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