• 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 android.app.Activity;
19 import android.app.LoaderManager;
20 import android.content.Context;
21 import android.content.Loader;
22 import android.os.Bundle;
23 import android.os.UserManager;
24 import android.support.annotation.VisibleForTesting;
25 import android.view.Menu;
26 
27 import com.android.internal.os.BatteryStatsHelper;
28 import com.android.settings.dashboard.DashboardFragment;
29 import com.android.settings.utils.AsyncLoader;
30 
31 /**
32  * Common base class for things that need to show the battery usage graph.
33  */
34 public abstract class PowerUsageBase extends DashboardFragment
35         implements LoaderManager.LoaderCallbacks<BatteryStatsHelper> {
36 
37     // +1 to allow ordering for PowerUsageSummary.
38     @VisibleForTesting
39     static final int MENU_STATS_REFRESH = Menu.FIRST + 1;
40 
41     protected BatteryStatsHelper mStatsHelper;
42     protected UserManager mUm;
43     private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
44 
45     @Override
onAttach(Activity activity)46     public void onAttach(Activity activity) {
47         super.onAttach(activity);
48         mUm = (UserManager) activity.getSystemService(Context.USER_SERVICE);
49         mStatsHelper = new BatteryStatsHelper(activity, true);
50     }
51 
52     @Override
onCreate(Bundle icicle)53     public void onCreate(Bundle icicle) {
54         super.onCreate(icicle);
55         mStatsHelper.create(icicle);
56         setHasOptionsMenu(true);
57 
58         mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext());
59         mBatteryBroadcastReceiver.setBatteryChangedListener(() -> {
60             restartBatteryStatsLoader();
61         });
62 
63         getLoaderManager().initLoader(0, icicle, this);
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 
85     @Override
onStop()86     public void onStop() {
87         super.onStop();
88     }
89 
90     @Override
onDestroy()91     public void onDestroy() {
92         super.onDestroy();
93         if (getActivity().isChangingConfigurations()) {
94             mStatsHelper.storeState();
95         }
96     }
97 
restartBatteryStatsLoader()98     protected void restartBatteryStatsLoader() {
99         getLoaderManager().restartLoader(0, Bundle.EMPTY, this);
100     }
101 
refreshUi()102     protected abstract void refreshUi();
103 
updatePreference(BatteryHistoryPreference historyPref)104     protected void updatePreference(BatteryHistoryPreference historyPref) {
105         historyPref.setStats(mStatsHelper);
106     }
107 
108     @Override
onCreateLoader(int id, Bundle args)109     public Loader<BatteryStatsHelper> onCreateLoader(int id,
110             Bundle args) {
111         return new BatteryStatsHelperLoader(getContext(), args);
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();
119     }
120 
121     @Override
onLoaderReset(Loader<BatteryStatsHelper> loader)122     public void onLoaderReset(Loader<BatteryStatsHelper> loader) {
123 
124     }
125 }
126