Skip to content

Commit b9690f7

Browse files
jruesgaimjyotiraditya
authored andcommitted
Settings: Reset battery stats [2/2]
imjyotiraditya: Improvments and code cleanup Change-Id: I859c694d13085b48e363b0537c6c1d384acb8989
1 parent 6eddb5f commit b9690f7

3 files changed

Lines changed: 61 additions & 18 deletions

File tree

AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"/>
5151
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
5252
<uses-permission android:name="android.permission.BATTERY_STATS"/>
53+
<uses-permission android:name="android.permission.RESET_BATTERY_STATS"/>
5354
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
5455
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
5556
<uses-permission android:name="android.permission.MOVE_PACKAGE" />

res/values/wave_strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,8 @@
239239
<string name="fingerprint_power_button_press_off_summary">Touch power button to unlock with fingerprint when screen is off</string>
240240
<string name="fingerprint_power_button_press_on_summary">Press power button firmly to unlock with fingerprint when screen is off</string>
241241

242+
<!-- Battery stats reset -->
243+
<string name="battery_stats_reset">Reset stats</string>
244+
<string name="battery_stats_message">Battery history stats are going to be reset</string>
245+
242246
</resources>

src/com/android/settings/fuelgauge/PowerUsageSummary.java

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,30 @@
1818

1919
import static com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpdateType;
2020

21+
import android.app.AlertDialog;
2122
import android.app.settings.SettingsEnums;
2223
import android.content.Context;
2324
import android.database.ContentObserver;
2425
import android.net.Uri;
2526
import android.os.Bundle;
2627
import android.os.Handler;
28+
import android.os.Looper;
2729
import android.provider.Settings.Global;
2830
import android.text.format.Formatter;
31+
import android.view.Menu;
32+
import android.view.MenuInflater;
33+
import android.view.MenuItem;
2934
import android.view.View;
3035
import android.view.View.OnLongClickListener;
3136
import android.widget.Button;
3237
import android.widget.TextView;
33-
import androidx.preference.Preference;
3438

39+
import androidx.annotation.NonNull;
3540
import androidx.annotation.VisibleForTesting;
3641
import androidx.loader.app.LoaderManager;
3742
import androidx.loader.app.LoaderManager.LoaderCallbacks;
3843
import androidx.loader.content.Loader;
44+
import androidx.preference.Preference;
3945

4046
import com.android.settings.R;
4147
import com.android.settings.SettingsActivity;
@@ -73,6 +79,8 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
7379
static final int BATTERY_INFO_LOADER = 1;
7480
@VisibleForTesting
7581
static final int BATTERY_TIP_LOADER = 2;
82+
@VisibleForTesting
83+
static final int MENU_STATS_RESET = Menu.FIRST + 1;
7684
public static final int DEBUG_INFO_LOADER = 3;
7785

7886
@VisibleForTesting
@@ -96,7 +104,7 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
96104
BatteryTipPreferenceController mBatteryTipPreferenceController;
97105

98106
@VisibleForTesting
99-
final ContentObserver mSettingsObserver = new ContentObserver(new Handler()) {
107+
final ContentObserver mSettingsObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
100108
@Override
101109
public void onChange(boolean selfChange, Uri uri) {
102110
restartBatteryInfoLoader();
@@ -113,14 +121,14 @@ public Loader<BatteryInfo> onCreateLoader(int i, Bundle bundle) {
113121
}
114122

115123
@Override
116-
public void onLoadFinished(Loader<BatteryInfo> loader, BatteryInfo batteryInfo) {
124+
public void onLoadFinished(@NonNull Loader<BatteryInfo> loader, BatteryInfo batteryInfo) {
117125
mBatteryHeaderPreferenceController.updateHeaderPreference(batteryInfo);
118126
mBatteryInfo = batteryInfo;
119127
updateLastFullChargePreference();
120128
}
121129

122130
@Override
123-
public void onLoaderReset(Loader<BatteryInfo> loader) {
131+
public void onLoaderReset(@NonNull Loader<BatteryInfo> loader) {
124132
// do nothing
125133
}
126134
};
@@ -133,13 +141,13 @@ public Loader<List<BatteryInfo>> onCreateLoader(int i, Bundle bundle) {
133141
}
134142

135143
@Override
136-
public void onLoadFinished(Loader<List<BatteryInfo>> loader,
137-
List<BatteryInfo> batteryInfos) {
144+
public void onLoadFinished(@NonNull Loader<List<BatteryInfo>> loader,
145+
List<BatteryInfo> batteryInfos) {
138146
updateViews(batteryInfos);
139147
}
140148

141149
@Override
142-
public void onLoaderReset(Loader<List<BatteryInfo>> loader) {
150+
public void onLoaderReset(@NonNull Loader<List<BatteryInfo>> loader) {
143151
}
144152
};
145153

@@ -168,7 +176,7 @@ protected void updateViews(List<BatteryInfo> batteryInfos) {
168176
batteryView.setCharging(!oldInfo.discharging);
169177
}
170178

171-
private LoaderManager.LoaderCallbacks<List<BatteryTip>> mBatteryTipsCallbacks =
179+
private final LoaderManager.LoaderCallbacks<List<BatteryTip>> mBatteryTipsCallbacks =
172180
new LoaderManager.LoaderCallbacks<List<BatteryTip>>() {
173181

174182
@Override
@@ -177,13 +185,13 @@ public Loader<List<BatteryTip>> onCreateLoader(int id, Bundle args) {
177185
}
178186

179187
@Override
180-
public void onLoadFinished(Loader<List<BatteryTip>> loader,
181-
List<BatteryTip> data) {
188+
public void onLoadFinished(@NonNull Loader<List<BatteryTip>> loader,
189+
List<BatteryTip> data) {
182190
mBatteryTipPreferenceController.updateBatteryTips(data);
183191
}
184192

185193
@Override
186-
public void onLoaderReset(Loader<List<BatteryTip>> loader) {
194+
public void onLoaderReset(@NonNull Loader<List<BatteryTip>> loader) {
187195

188196
}
189197
};
@@ -223,13 +231,11 @@ public void onCreate(Bundle icicle) {
223231
mBatteryTipPreferenceController.restoreInstanceState(icicle);
224232
updateBatteryTipFlag(icicle);
225233
final Button showStatsBtn = mBatteryLayoutPref.findViewById(R.id.btn_show_stats);
226-
showStatsBtn.setOnClickListener(v -> {
227-
new SubSettingLauncher(getContext())
228-
.setDestination(PowerUsageAdvanced.class.getName())
229-
.setSourceMetricsCategory(getMetricsCategory())
230-
.setTitleRes(R.string.advanced_battery_title)
231-
.launch();
232-
});
234+
showStatsBtn.setOnClickListener(v -> new SubSettingLauncher(getContext())
235+
.setDestination(PowerUsageAdvanced.class.getName())
236+
.setSourceMetricsCategory(getMetricsCategory())
237+
.setTitleRes(R.string.advanced_battery_title)
238+
.launch());
233239
}
234240

235241
@Override
@@ -262,11 +268,43 @@ protected int getPreferenceScreenResId() {
262268
return R.xml.power_usage_summary;
263269
}
264270

271+
@Override
272+
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
273+
MenuItem reset = menu.add(0, MENU_STATS_RESET, 0, R.string.battery_stats_reset)
274+
.setIcon(R.drawable.ic_delete)
275+
.setAlphabeticShortcut('d');
276+
reset.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
277+
278+
super.onCreateOptionsMenu(menu, inflater);
279+
}
280+
281+
private void resetStats() {
282+
new AlertDialog.Builder(getActivity())
283+
.setTitle(R.string.battery_stats_reset)
284+
.setMessage(R.string.battery_stats_message)
285+
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
286+
mStatsHelper.resetStatistics();
287+
refreshUi(BatteryUpdateType.MANUAL);
288+
})
289+
.setNegativeButton(R.string.cancel, null)
290+
.create()
291+
.show();
292+
}
293+
265294
@Override
266295
public int getHelpResource() {
267296
return R.string.help_url_battery;
268297
}
269298

299+
@Override
300+
public boolean onOptionsItemSelected(MenuItem item) {
301+
if (item.getItemId() == MENU_STATS_RESET) {
302+
resetStats();
303+
return true;
304+
}
305+
return super.onOptionsItemSelected(item);
306+
}
307+
270308
protected void refreshUi(@BatteryUpdateType int refreshType) {
271309
final Context context = getContext();
272310
if (context == null) {

0 commit comments

Comments
 (0)