From 6ba8b1efaeb37eb308fdb861c95b2e1f5147bb4b Mon Sep 17 00:00:00 2001 From: Wesley Wang Date: Thu, 7 Apr 2022 20:29:29 +0800 Subject: [PATCH] Add metrics for low battery warning - Log the action when clicking low battery warning notification or confirm dialog's each buttons - Log the dismiss event for notification and dialog - Log popup event Eldar: https://eldar.corp.google.com/assessments/51308713/drafts/480035865?jsmode=o#sections/11001001000 Bug: 218405244 Test: atest SystemUITests Change-Id: I01d9ffb34797b386650a1f8877acf86190968a82 --- .../systemui/power/BatteryWarningEvents.java | 64 +++++++++++++++++++ .../power/PowerNotificationWarnings.java | 32 ++++++++-- .../power/PowerNotificationWarningsTest.java | 5 +- 3 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 packages/SystemUI/src/com/android/systemui/power/BatteryWarningEvents.java diff --git a/packages/SystemUI/src/com/android/systemui/power/BatteryWarningEvents.java b/packages/SystemUI/src/com/android/systemui/power/BatteryWarningEvents.java new file mode 100644 index 0000000000000..1f8a303309d46 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/power/BatteryWarningEvents.java @@ -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; + } + } +} diff --git a/packages/SystemUI/src/com/android/systemui/power/PowerNotificationWarnings.java b/packages/SystemUI/src/com/android/systemui/power/PowerNotificationWarnings.java index 3e00a5f74d8fc..429f2df6b50c0 100644 --- a/packages/SystemUI/src/com/android/systemui/power/PowerNotificationWarnings.java +++ b/packages/SystemUI/src/com/android/systemui/power/PowerNotificationWarnings.java @@ -55,6 +55,7 @@ import android.view.WindowManager; import androidx.annotation.VisibleForTesting; +import com.android.internal.logging.UiEventLogger; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; import com.android.settingslib.Utils; import com.android.settingslib.fuelgauge.BatterySaverUtils; @@ -169,6 +170,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI { private BatteryStateSnapshot mCurrentBatterySnapshot; private ActivityStarter mActivityStarter; private final BroadcastSender mBroadcastSender; + private final UiEventLogger mUiEventLogger; private final Lazy mBatteryControllerLazy; private final DialogLaunchAnimator mDialogLaunchAnimator; @@ -178,7 +180,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI { @Inject public PowerNotificationWarnings(Context context, ActivityStarter activityStarter, BroadcastSender broadcastSender, Lazy batteryControllerLazy, - DialogLaunchAnimator dialogLaunchAnimator) { + DialogLaunchAnimator dialogLaunchAnimator, UiEventLogger uiEventLogger) { mContext = context; mNoMan = mContext.getSystemService(NotificationManager.class); mPowerMan = (PowerManager) context.getSystemService(Context.POWER_SERVICE); @@ -189,6 +191,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI { mBatteryControllerLazy = batteryControllerLazy; mDialogLaunchAnimator = dialogLaunchAnimator; mUseSevereDialog = mContext.getResources().getBoolean(R.bool.config_severe_battery_dialog); + mUiEventLogger = uiEventLogger; } @Override @@ -333,6 +336,7 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI { final Notification n = nb.build(); mNoMan.cancelAsUser(TAG_BATTERY, SystemMessage.NOTE_BAD_CHARGER, UserHandle.ALL); mNoMan.notifyAsUser(TAG_BATTERY, SystemMessage.NOTE_POWER_LOW, n, UserHandle.ALL); + logEvent(BatteryWarningEvents.LowBatteryWarningEvent.LOW_BATTERY_NOTIFICATION); } private boolean showSevereLowBatteryDialog() { @@ -692,17 +696,25 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI { } else { d.setTitle(R.string.battery_saver_confirmation_title); d.setPositiveButton(R.string.battery_saver_confirmation_ok, - (dialog, which) -> setSaverMode(true, false)); - d.setNegativeButton(android.R.string.cancel, null); + (dialog, which) -> { + 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.setOnDismissListener((dialog) -> mSaverConfirmation = null); + d.setOnDismissListener((dialog) -> { + mSaverConfirmation = null; + logEvent(BatteryWarningEvents.LowBatteryWarningEvent.SAVER_CONFIRM_DISMISS); + }); WeakReference ref = mBatteryControllerLazy.get().getLastPowerSaverStartView(); if (ref != null && ref.get() != null && ref.get().isAggregatedVisible()) { mDialogLaunchAnimator.showFromView(d, ref.get()); } else { d.show(); } + logEvent(BatteryWarningEvents.LowBatteryWarningEvent.SAVER_CONFIRM_DIALOG); mSaverConfirmation = d; mBatteryControllerLazy.get().clearLastPowerSaverStartView(); } @@ -794,6 +806,12 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI { mActivityStarter.startActivity(intent, true /* dismissShade */); } + private void logEvent(BatteryWarningEvents.LowBatteryWarningEvent event) { + if (mUiEventLogger != null) { + mUiEventLogger.log(event); + } + } + private final class Receiver extends BroadcastReceiver { public void init() { @@ -819,15 +837,21 @@ public class PowerNotificationWarnings implements PowerUI.WarningsUI { final String action = intent.getAction(); Slog.i(TAG, "Received " + action); if (action.equals(ACTION_SHOW_BATTERY_SAVER_SETTINGS)) { + logEvent(BatteryWarningEvents + .LowBatteryWarningEvent.LOW_BATTERY_NOTIFICATION_SETTINGS); dismissLowBatteryNotification(); mContext.startActivityAsUser(mOpenBatterySaverSettings, UserHandle.CURRENT); } else if (action.equals(ACTION_START_SAVER)) { + logEvent(BatteryWarningEvents + .LowBatteryWarningEvent.LOW_BATTERY_NOTIFICATION_TURN_ON); setSaverMode(true, true); dismissLowBatteryNotification(); } else if (action.equals(ACTION_SHOW_START_SAVER_CONFIRMATION)) { dismissLowBatteryNotification(); showStartSaverConfirmation(intent.getExtras()); } else if (action.equals(ACTION_DISMISSED_WARNING)) { + logEvent(BatteryWarningEvents + .LowBatteryWarningEvent.LOW_BATTERY_NOTIFICATION_CANCEL); dismissLowBatteryWarning(); } else if (ACTION_CLICKED_TEMP_WARNING.equals(action)) { dismissHighTemperatureWarningInternal(); diff --git a/packages/SystemUI/tests/src/com/android/systemui/power/PowerNotificationWarningsTest.java b/packages/SystemUI/tests/src/com/android/systemui/power/PowerNotificationWarningsTest.java index 1ffa9dd57aa9c..26e4d9dfb0037 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/power/PowerNotificationWarningsTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/power/PowerNotificationWarningsTest.java @@ -46,6 +46,7 @@ import android.testing.AndroidTestingRunner; import android.testing.TestableLooper; import android.view.View; +import com.android.internal.logging.UiEventLogger; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; import com.android.settingslib.fuelgauge.BatterySaverUtils; import com.android.systemui.SysuiTestCase; @@ -79,6 +80,8 @@ public class PowerNotificationWarningsTest extends SysuiTestCase { @Mock private DialogLaunchAnimator mDialogLaunchAnimator; @Mock + private UiEventLogger mUiEventLogger; + @Mock private View mView; private BroadcastReceiver mReceiver; @@ -101,7 +104,7 @@ public class PowerNotificationWarningsTest extends SysuiTestCase { ActivityStarter starter = mDependency.injectMockDependency(ActivityStarter.class); BroadcastSender broadcastSender = mDependency.injectMockDependency(BroadcastSender.class); mPowerNotificationWarnings = new PowerNotificationWarnings(wrapper, starter, - broadcastSender, () -> mBatteryController, mDialogLaunchAnimator); + broadcastSender, () -> mBatteryController, mDialogLaunchAnimator, mUiEventLogger); BatteryStateSnapshot snapshot = new BatteryStateSnapshot(100, false, false, 1, BatteryManager.BATTERY_HEALTH_GOOD, 5, 15); mPowerNotificationWarnings.updateSnapshot(snapshot);