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.Bundle; 23 import android.os.UserManager; 24 import android.view.Menu; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.loader.app.LoaderManager; 28 import androidx.loader.content.Loader; 29 30 import com.android.internal.os.BatteryStatsHelper; 31 import com.android.settings.dashboard.DashboardFragment; 32 33 /** 34 * Common base class for things that need to show the battery usage graph. 35 */ 36 public abstract class PowerUsageBase extends DashboardFragment { 37 38 // +1 to allow ordering for PowerUsageSummary. 39 @VisibleForTesting 40 static final int MENU_STATS_REFRESH = Menu.FIRST + 1; 41 private static final String TAG = "PowerUsageBase"; 42 private static final String KEY_REFRESH_TYPE = "refresh_type"; 43 44 protected BatteryStatsHelper mStatsHelper; 45 protected UserManager mUm; 46 private BatteryBroadcastReceiver mBatteryBroadcastReceiver; 47 48 @Override onAttach(Activity activity)49 public void onAttach(Activity activity) { 50 super.onAttach(activity); 51 mUm = (UserManager) activity.getSystemService(Context.USER_SERVICE); 52 mStatsHelper = new BatteryStatsHelper(activity, true); 53 } 54 55 @Override onCreate(Bundle icicle)56 public void onCreate(Bundle icicle) { 57 super.onCreate(icicle); 58 mStatsHelper.create(icicle); 59 setHasOptionsMenu(true); 60 61 mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext()); 62 mBatteryBroadcastReceiver.setBatteryChangedListener(type -> { 63 restartBatteryStatsLoader(type); 64 }); 65 } 66 67 @Override onStart()68 public void onStart() { 69 super.onStart(); 70 } 71 72 @Override onResume()73 public void onResume() { 74 super.onResume(); 75 mBatteryBroadcastReceiver.register(); 76 } 77 78 @Override onPause()79 public void onPause() { 80 super.onPause(); 81 mBatteryBroadcastReceiver.unRegister(); 82 } 83 restartBatteryStatsLoader(int refreshType)84 protected void restartBatteryStatsLoader(int refreshType) { 85 final Bundle bundle = new Bundle(); 86 bundle.putInt(KEY_REFRESH_TYPE, refreshType); 87 88 getLoaderManager().restartLoader(0, bundle, new PowerLoaderCallback()); 89 } 90 refreshUi(@atteryUpdateType int refreshType)91 protected abstract void refreshUi(@BatteryUpdateType int refreshType); 92 updatePreference(BatteryHistoryPreference historyPref)93 protected void updatePreference(BatteryHistoryPreference historyPref) { 94 final long startTime = System.currentTimeMillis(); 95 historyPref.setStats(mStatsHelper); 96 BatteryUtils.logRuntime(TAG, "updatePreference", startTime); 97 } 98 99 /** 100 * {@link android.app.LoaderManager.LoaderCallbacks} for {@link PowerUsageBase} to load 101 * the {@link BatteryStatsHelper} 102 */ 103 public class PowerLoaderCallback implements LoaderManager.LoaderCallbacks<BatteryStatsHelper> { 104 private int mRefreshType; 105 106 @Override onCreateLoader(int id, Bundle args)107 public Loader<BatteryStatsHelper> onCreateLoader(int id, 108 Bundle args) { 109 mRefreshType = args.getInt(KEY_REFRESH_TYPE); 110 return new BatteryStatsHelperLoader(getContext()); 111 } 112 113 @Override onLoadFinished(Loader<BatteryStatsHelper> loader, BatteryStatsHelper statsHelper)114 public void onLoadFinished(Loader<BatteryStatsHelper> loader, 115 BatteryStatsHelper statsHelper) { 116 mStatsHelper = statsHelper; 117 refreshUi(mRefreshType); 118 } 119 120 @Override onLoaderReset(Loader<BatteryStatsHelper> loader)121 public void onLoaderReset(Loader<BatteryStatsHelper> loader) { 122 123 } 124 } 125 } 126