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 mBatteryBroadcastReceiver.register(); 71 } 72 73 @Override onStop()74 public void onStop() { 75 super.onStop(); 76 mBatteryBroadcastReceiver.unRegister(); 77 } 78 restartBatteryStatsLoader(int refreshType)79 protected void restartBatteryStatsLoader(int refreshType) { 80 final Bundle bundle = new Bundle(); 81 bundle.putInt(KEY_REFRESH_TYPE, refreshType); 82 83 getLoaderManager().restartLoader(0, bundle, new PowerLoaderCallback()); 84 } 85 refreshUi(@atteryUpdateType int refreshType)86 protected abstract void refreshUi(@BatteryUpdateType int refreshType); 87 updatePreference(BatteryHistoryPreference historyPref)88 protected void updatePreference(BatteryHistoryPreference historyPref) { 89 final long startTime = System.currentTimeMillis(); 90 historyPref.setStats(mStatsHelper); 91 BatteryUtils.logRuntime(TAG, "updatePreference", startTime); 92 } 93 94 /** 95 * {@link android.app.LoaderManager.LoaderCallbacks} for {@link PowerUsageBase} to load 96 * the {@link BatteryStatsHelper} 97 */ 98 public class PowerLoaderCallback implements LoaderManager.LoaderCallbacks<BatteryStatsHelper> { 99 private int mRefreshType; 100 101 @Override onCreateLoader(int id, Bundle args)102 public Loader<BatteryStatsHelper> onCreateLoader(int id, 103 Bundle args) { 104 mRefreshType = args.getInt(KEY_REFRESH_TYPE); 105 return new BatteryStatsHelperLoader(getContext()); 106 } 107 108 @Override onLoadFinished(Loader<BatteryStatsHelper> loader, BatteryStatsHelper statsHelper)109 public void onLoadFinished(Loader<BatteryStatsHelper> loader, 110 BatteryStatsHelper statsHelper) { 111 mStatsHelper = statsHelper; 112 refreshUi(mRefreshType); 113 } 114 115 @Override onLoaderReset(Loader<BatteryStatsHelper> loader)116 public void onLoaderReset(Loader<BatteryStatsHelper> loader) { 117 118 } 119 } 120 } 121