1 /* 2 * Copyright (C) 2015 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 package com.android.settings.fuelgauge; 17 18 import static com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpdateType; 19 20 import android.app.Activity; 21 import android.content.Context; 22 import android.os.BatteryUsageStats; 23 import android.os.Bundle; 24 import android.os.UserManager; 25 import android.view.Menu; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.VisibleForTesting; 29 import androidx.loader.app.LoaderManager; 30 import androidx.loader.content.Loader; 31 32 import com.android.settings.dashboard.DashboardFragment; 33 34 /** 35 * Common base class for things that need to show the battery usage graph. 36 */ 37 public abstract class PowerUsageBase extends DashboardFragment { 38 39 private static final String TAG = "PowerUsageBase"; 40 private static final String KEY_REFRESH_TYPE = "refresh_type"; 41 private static final String KEY_INCLUDE_HISTORY = "include_history"; 42 43 private static final int LOADER_BATTERY_USAGE_STATS = 1; 44 45 @VisibleForTesting 46 BatteryUsageStats mBatteryUsageStats; 47 48 protected UserManager mUm; 49 protected boolean mIsBatteryPresent = true; 50 private BatteryBroadcastReceiver mBatteryBroadcastReceiver; 51 52 @VisibleForTesting 53 final BatteryUsageStatsLoaderCallbacks mBatteryUsageStatsLoaderCallbacks = 54 new BatteryUsageStatsLoaderCallbacks(); 55 56 @Override onAttach(Activity activity)57 public void onAttach(Activity activity) { 58 super.onAttach(activity); 59 mUm = (UserManager) activity.getSystemService(Context.USER_SERVICE); 60 } 61 62 @Override onCreate(Bundle icicle)63 public void onCreate(Bundle icicle) { 64 super.onCreate(icicle); 65 66 mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext()); 67 mBatteryBroadcastReceiver.setBatteryChangedListener(type -> { 68 if (type == BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT) { 69 mIsBatteryPresent = false; 70 } 71 restartBatteryStatsLoader(type); 72 }); 73 } 74 75 @Override onStart()76 public void onStart() { 77 super.onStart(); 78 mBatteryBroadcastReceiver.register(); 79 } 80 81 @Override onStop()82 public void onStop() { 83 super.onStop(); 84 mBatteryBroadcastReceiver.unRegister(); 85 } 86 restartBatteryStatsLoader(int refreshType)87 protected void restartBatteryStatsLoader(int refreshType) { 88 final Bundle bundle = new Bundle(); 89 bundle.putInt(KEY_REFRESH_TYPE, refreshType); 90 bundle.putBoolean(KEY_INCLUDE_HISTORY, isBatteryHistoryNeeded()); 91 getLoaderManager().restartLoader(LOADER_BATTERY_USAGE_STATS, bundle, 92 mBatteryUsageStatsLoaderCallbacks); 93 } 94 onLoadFinished(@atteryUpdateType int refreshType)95 protected void onLoadFinished(@BatteryUpdateType int refreshType) { 96 refreshUi(refreshType); 97 } 98 refreshUi(@atteryUpdateType int refreshType)99 protected abstract void refreshUi(@BatteryUpdateType int refreshType); isBatteryHistoryNeeded()100 protected abstract boolean isBatteryHistoryNeeded(); 101 updatePreference(BatteryHistoryPreference historyPref)102 protected void updatePreference(BatteryHistoryPreference historyPref) { 103 final long startTime = System.currentTimeMillis(); 104 historyPref.setBatteryUsageStats(mBatteryUsageStats); 105 BatteryUtils.logRuntime(TAG, "updatePreference", startTime); 106 } 107 108 private class BatteryUsageStatsLoaderCallbacks 109 implements LoaderManager.LoaderCallbacks<BatteryUsageStats> { 110 private int mRefreshType; 111 112 @Override 113 @NonNull onCreateLoader(int id, Bundle args)114 public Loader<BatteryUsageStats> onCreateLoader(int id, Bundle args) { 115 mRefreshType = args.getInt(KEY_REFRESH_TYPE); 116 return new BatteryUsageStatsLoader(getContext(), args.getBoolean(KEY_INCLUDE_HISTORY)); 117 } 118 119 @Override onLoadFinished(Loader<BatteryUsageStats> loader, BatteryUsageStats batteryUsageStats)120 public void onLoadFinished(Loader<BatteryUsageStats> loader, 121 BatteryUsageStats batteryUsageStats) { 122 mBatteryUsageStats = batteryUsageStats; 123 PowerUsageBase.this.onLoadFinished(mRefreshType); 124 } 125 126 @Override onLoaderReset(Loader<BatteryUsageStats> loader)127 public void onLoaderReset(Loader<BatteryUsageStats> loader) { 128 } 129 } 130 } 131