Merge "Add metrics for low battery warning" into tm-dev

This commit is contained in:
TreeHugger Robot
2022-04-14 15:48:55 +00:00
committed by Android (Google) Code Review
3 changed files with 96 additions and 5 deletions

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.power;
import com.android.internal.logging.UiEvent;
import com.android.internal.logging.UiEventLogger;
/**
* Events related to the battery warning.
*/
public class BatteryWarningEvents {
/** Enums for logging low battery warning notification and dialog */
public enum LowBatteryWarningEvent implements UiEventLogger.UiEventEnum {
@UiEvent(doc = "Low battery warning notification displayed")
LOW_BATTERY_NOTIFICATION(1048),
@UiEvent(doc = "Low battery warning notification positive button clicked")
LOW_BATTERY_NOTIFICATION_TURN_ON(1049),
@UiEvent(doc = "Low battery warning notification negative button clicked")
LOW_BATTERY_NOTIFICATION_CANCEL(1050),
@UiEvent(doc = "Low battery warning notification content clicked")
LOW_BATTERY_NOTIFICATION_SETTINGS(1051),
@UiEvent(doc = "Battery saver confirm dialog displayed")
SAVER_CONFIRM_DIALOG(1052),
@UiEvent(doc = "Battery saver confirm dialog positive button clicked")
SAVER_CONFIRM_OK(1053),
@UiEvent(doc = "Battery saver confirm dialog negative button clicked")
SAVER_CONFIRM_CANCEL(1054),
@UiEvent(doc = "Battery saver confirm dialog dismissed")
SAVER_CONFIRM_DISMISS(1055);
private final int mId;
LowBatteryWarningEvent(int id) {
mId = id;
}
@Override
public int getId() {
return mId;
}
}
}

View File

@@ -55,6 +55,7 @@ import android.view.WindowManager;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import com.android.internal.logging.UiEventLogger;
import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import com.android.settingslib.Utils; import com.android.settingslib.Utils;
import com.android.settingslib.fuelgauge.BatterySaverUtils; import com.android.settingslib.fuelgauge.BatterySaverUtils;
@@ -169,6 +170,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
private BatteryStateSnapshot mCurrentBatterySnapshot; private BatteryStateSnapshot mCurrentBatterySnapshot;
private ActivityStarter mActivityStarter; private ActivityStarter mActivityStarter;
private final BroadcastSender mBroadcastSender; private final BroadcastSender mBroadcastSender;
private final UiEventLogger mUiEventLogger;
private final Lazy<BatteryController> mBatteryControllerLazy; private final Lazy<BatteryController> mBatteryControllerLazy;
private final DialogLaunchAnimator mDialogLaunchAnimator; private final DialogLaunchAnimator mDialogLaunchAnimator;
@@ -178,7 +180,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
@Inject @Inject
public PowerNotificationWarnings(Context context, ActivityStarter activityStarter, public PowerNotificationWarnings(Context context, ActivityStarter activityStarter,
BroadcastSender broadcastSender, Lazy<BatteryController> batteryControllerLazy, BroadcastSender broadcastSender, Lazy<BatteryController> batteryControllerLazy,
DialogLaunchAnimator dialogLaunchAnimator) { DialogLaunchAnimator dialogLaunchAnimator, UiEventLogger uiEventLogger) {
mContext = context; mContext = context;
mNoMan = mContext.getSystemService(NotificationManager.class); mNoMan = mContext.getSystemService(NotificationManager.class);
mPowerMan = (PowerManager) context.getSystemService(Context.POWER_SERVICE); mPowerMan = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
@@ -189,6 +191,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
mBatteryControllerLazy = batteryControllerLazy; mBatteryControllerLazy = batteryControllerLazy;
mDialogLaunchAnimator = dialogLaunchAnimator; mDialogLaunchAnimator = dialogLaunchAnimator;
mUseSevereDialog = mContext.getResources().getBoolean(R.bool.config_severe_battery_dialog); mUseSevereDialog = mContext.getResources().getBoolean(R.bool.config_severe_battery_dialog);
mUiEventLogger = uiEventLogger;
} }
@Override @Override
@@ -333,6 +336,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
final Notification n = nb.build(); final Notification n = nb.build();
mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL); mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL);
mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, n, UserHandle.ALL); mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, n, UserHandle.ALL);
logEvent(BatteryWarningEvents.LowBatteryWarningEvent.LOW_BATTERY_NOTIFICATION);
} }
private boolean showSevereLowBatteryDialog() { private boolean showSevereLowBatteryDialog() {
@@ -692,17 +696,25 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
} else { } else {
d.setTitle(R.string.battery_saver_confirmation_title); d.setTitle(R.string.battery_saver_confirmation_title);
d.setPositiveButton(R.string.battery_saver_confirmation_ok, d.setPositiveButton(R.string.battery_saver_confirmation_ok,
(dialog, which) -> setSaverMode(true, false)); (dialog, which) -> {
d.setNegativeButton(android.R.string.cancel, null); setSaverMode(true, false);
logEvent(BatteryWarningEvents.LowBatteryWarningEvent.SAVER_CONFIRM_OK);
});
d.setNegativeButton(android.R.string.cancel, (dialog, which) ->
logEvent(BatteryWarningEvents.LowBatteryWarningEvent.SAVER_CONFIRM_CANCEL));
} }
d.setShowForAllUsers(true); d.setShowForAllUsers(true);
d.setOnDismissListener((dialog) -> mSaverConfirmation = null); d.setOnDismissListener((dialog) -> {
mSaverConfirmation = null;
logEvent(BatteryWarningEvents.LowBatteryWarningEvent.SAVER_CONFIRM_DISMISS);
});
WeakReference<View> ref = mBatteryControllerLazy.get().getLastPowerSaverStartView(); WeakReference<View> ref = mBatteryControllerLazy.get().getLastPowerSaverStartView();
if (ref != null && ref.get() != null && ref.get().isAggregatedVisible()) { if (ref != null && ref.get() != null && ref.get().isAggregatedVisible()) {
mDialogLaunchAnimator.showFromView(d, ref.get()); mDialogLaunchAnimator.showFromView(d, ref.get());
} else { } else {
d.show(); d.show();
} }
logEvent(BatteryWarningEvents.LowBatteryWarningEvent.SAVER_CONFIRM_DIALOG);
mSaverConfirmation = d; mSaverConfirmation = d;
mBatteryControllerLazy.get().clearLastPowerSaverStartView(); mBatteryControllerLazy.get().clearLastPowerSaverStartView();
} }
@@ -794,6 +806,12 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
mActivityStarter.startActivity(intent, true /* dismissShade */); mActivityStarter.startActivity(intent, true /* dismissShade */);
} }
private void logEvent(BatteryWarningEvents.LowBatteryWarningEvent event) {
if (mUiEventLogger != null) {
mUiEventLogger.log(event);
}
}
private final class Receiver extends BroadcastReceiver { private final class Receiver extends BroadcastReceiver {
public void init() { public void init() {
@@ -819,15 +837,21 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI {
final String action = intent.getAction(); final String action = intent.getAction();
Slog.i(TAG, "Received " + action); Slog.i(TAG, "Received " + action);
if (action.equals(ACTION_SHOW_BATTERY_SAVER_SETTINGS)) { if (action.equals(ACTION_SHOW_BATTERY_SAVER_SETTINGS)) {
logEvent(BatteryWarningEvents
.LowBatteryWarningEvent.LOW_BATTERY_NOTIFICATION_SETTINGS);
dismissLowBatteryNotification(); dismissLowBatteryNotification();
mContext.startActivityAsUser(mOpenBatterySaverSettings, UserHandle.CURRENT); mContext.startActivityAsUser(mOpenBatterySaverSettings, UserHandle.CURRENT);
} else if (action.equals(ACTION_START_SAVER)) { } else if (action.equals(ACTION_START_SAVER)) {
logEvent(BatteryWarningEvents
.LowBatteryWarningEvent.LOW_BATTERY_NOTIFICATION_TURN_ON);
setSaverMode(true, true); setSaverMode(true, true);
dismissLowBatteryNotification(); dismissLowBatteryNotification();
} else if (action.equals(ACTION_SHOW_START_SAVER_CONFIRMATION)) { } else if (action.equals(ACTION_SHOW_START_SAVER_CONFIRMATION)) {
dismissLowBatteryNotification(); dismissLowBatteryNotification();
showStartSaverConfirmation(intent.getExtras()); showStartSaverConfirmation(intent.getExtras());
} else if (action.equals(ACTION_DISMISSED_WARNING)) { } else if (action.equals(ACTION_DISMISSED_WARNING)) {
logEvent(BatteryWarningEvents
.LowBatteryWarningEvent.LOW_BATTERY_NOTIFICATION_CANCEL);
dismissLowBatteryWarning(); dismissLowBatteryWarning();
} else if (ACTION_CLICKED_TEMP_WARNING.equals(action)) { } else if (ACTION_CLICKED_TEMP_WARNING.equals(action)) {
dismissHighTemperatureWarningInternal(); dismissHighTemperatureWarningInternal();

View File

@@ -46,6 +46,7 @@ import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper; import android.testing.TestableLooper;
import android.view.View; import android.view.View;
import com.android.internal.logging.UiEventLogger;
import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import com.android.settingslib.fuelgauge.BatterySaverUtils; import com.android.settingslib.fuelgauge.BatterySaverUtils;
import com.android.systemui.SysuiTestCase; import com.android.systemui.SysuiTestCase;
@@ -79,6 +80,8 @@ public class PowerNotificationWarningsTest extends SysuiTestCase {
@Mock @Mock
private DialogLaunchAnimator mDialogLaunchAnimator; private DialogLaunchAnimator mDialogLaunchAnimator;
@Mock @Mock
private UiEventLogger mUiEventLogger;
@Mock
private View mView; private View mView;
private BroadcastReceiver mReceiver; private BroadcastReceiver mReceiver;
@@ -101,7 +104,7 @@ public class PowerNotificationWarningsTest extends SysuiTestCase {
ActivityStarter starter = mDependency.injectMockDependency(ActivityStarter.class); ActivityStarter starter = mDependency.injectMockDependency(ActivityStarter.class);
BroadcastSender broadcastSender = mDependency.injectMockDependency(BroadcastSender.class); BroadcastSender broadcastSender = mDependency.injectMockDependency(BroadcastSender.class);
mPowerNotificationWarnings = new PowerNotificationWarnings(wrapper, starter, mPowerNotificationWarnings = new PowerNotificationWarnings(wrapper, starter,
broadcastSender, () -> mBatteryController, mDialogLaunchAnimator); broadcastSender, () -> mBatteryController, mDialogLaunchAnimator, mUiEventLogger);
BatteryStateSnapshot snapshot = new BatteryStateSnapshot(100, false, false, 1, BatteryStateSnapshot snapshot = new BatteryStateSnapshot(100, false, false, 1,
BatteryManager.BATTERY_HEALTH_GOOD, 5, 15); BatteryManager.BATTERY_HEALTH_GOOD, 5, 15);
mPowerNotificationWarnings.updateSnapshot(snapshot); mPowerNotificationWarnings.updateSnapshot(snapshot);