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