Merge "Migrate Tips Card in battery uasge to Settings Card." into main
This commit is contained in:
committed by
Android (Google) Code Review
commit
93b093033a
@@ -1,22 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2023 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.
|
||||
-->
|
||||
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="@color/settingslib_materialColorSurfaceBright" />
|
||||
<corners android:radius="@dimen/battery_tips_card_corner_radius_normal" />
|
||||
</shape>
|
@@ -1,21 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2023 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.
|
||||
-->
|
||||
|
||||
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:color="?android:attr/colorControlHighlight">
|
||||
<item android:drawable="@drawable/battery_tips_all_rounded_bg"/>
|
||||
</ripple>
|
@@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2023 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.
|
||||
-->
|
||||
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="@color/settingslib_dialog_background"/>
|
||||
<corners
|
||||
android:topLeftRadius="@dimen/battery_tips_card_corner_radius_small"
|
||||
android:topRightRadius="@dimen/battery_tips_card_corner_radius_small"
|
||||
android:bottomLeftRadius="@dimen/battery_tips_card_corner_radius_normal"
|
||||
android:bottomRightRadius="@dimen/battery_tips_card_corner_radius_normal"
|
||||
/>
|
||||
</shape>
|
@@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2023 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.
|
||||
-->
|
||||
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="@color/settingslib_dialog_background"/>
|
||||
<corners
|
||||
android:topLeftRadius="@dimen/battery_tips_card_corner_radius_normal"
|
||||
android:topRightRadius="@dimen/battery_tips_card_corner_radius_normal"
|
||||
android:bottomLeftRadius="@dimen/battery_tips_card_corner_radius_small"
|
||||
android:bottomRightRadius="@dimen/battery_tips_card_corner_radius_small"
|
||||
/>
|
||||
</shape>
|
@@ -1,61 +0,0 @@
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/battery_tips_card"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="?android:attr/listPreferredItemPaddingStart"
|
||||
android:layout_marginEnd="?android:attr/listPreferredItemPaddingEnd"
|
||||
android:background="@drawable/battery_tips_all_rounded_bg_ripple"
|
||||
android:orientation="vertical"
|
||||
android:padding="20dp">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/icon"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical|start"
|
||||
android:contentDescription="@string/battery_usage_anomaly_content_description"
|
||||
android:src="@drawable/ic_battery_tips_lightbulb" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:textAlignment="viewStart"
|
||||
android:textAppearance="@style/TextAppearance.Material3.TitleMedium"
|
||||
android:textColor="@color/settingslib_materialColorOnSurface" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:gravity="end"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/dismiss_button"
|
||||
style="@style/Widget.Material3.Button.TextButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="end|center_vertical"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:paddingHorizontal="16dp"
|
||||
android:text="@string/battery_tips_card_dismiss_button"
|
||||
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle2"
|
||||
android:textColor="@color/color_accent_selector" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/main_button"
|
||||
style="@style/Widget.Material3.Button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="end|center_vertical"
|
||||
android:paddingHorizontal="16dp"
|
||||
android:text="@string/battery_tips_card_action_button"
|
||||
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle2"
|
||||
android:textColor="@color/settingslib_materialColorOnPrimary"
|
||||
app:backgroundTint="@color/color_accent_selector" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
@@ -27,7 +27,7 @@
|
||||
"com.android.settings.fuelgauge.batteryusage.BatteryTipsController"
|
||||
settings:isPreferenceVisible="false">
|
||||
|
||||
<com.android.settings.fuelgauge.batteryusage.BatteryTipsCardPreference
|
||||
<com.android.settings.widget.TipCardPreference
|
||||
android:key="battery_tips_card"
|
||||
settings:isPreferenceVisible="false" />
|
||||
|
||||
|
@@ -28,10 +28,11 @@ import androidx.annotation.Nullable;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.widget.TipCardPreference;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
final class AnomalyEventWrapper {
|
||||
class AnomalyEventWrapper {
|
||||
private static final String TAG = "AnomalyEventWrapper";
|
||||
|
||||
private final Context mContext;
|
||||
@@ -235,16 +236,16 @@ final class AnomalyEventWrapper {
|
||||
return mHighlightSlotPair;
|
||||
}
|
||||
|
||||
boolean updateTipsCardPreference(BatteryTipsCardPreference preference) {
|
||||
boolean updateTipsCardPreference(TipCardPreference preference) {
|
||||
final String titleString = getTitleString();
|
||||
if (TextUtils.isEmpty(titleString)) {
|
||||
return false;
|
||||
}
|
||||
preference.setTitle(titleString);
|
||||
preference.setIconResourceId(getIconResId());
|
||||
preference.setButtonColorResourceId(getColorResId());
|
||||
preference.setMainButtonLabel(getMainBtnString());
|
||||
preference.setDismissButtonLabel(getDismissBtnString());
|
||||
preference.setIconResId(getIconResId());
|
||||
preference.setTintColorResId(getColorResId());
|
||||
preference.setPrimaryButtonText(getDismissBtnString());
|
||||
preference.setSecondaryButtonText(getMainBtnString());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -1,150 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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.settings.fuelgauge.batteryusage;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import com.google.android.material.button.MaterialButton;
|
||||
|
||||
/** A preference for displaying the battery tips card view. */
|
||||
public class BatteryTipsCardPreference extends Preference implements View.OnClickListener {
|
||||
|
||||
private static final String TAG = "BatteryTipsCardPreference";
|
||||
|
||||
interface OnConfirmListener {
|
||||
void onConfirm();
|
||||
}
|
||||
|
||||
interface OnRejectListener {
|
||||
void onReject();
|
||||
}
|
||||
|
||||
private OnConfirmListener mOnConfirmListener;
|
||||
private OnRejectListener mOnRejectListener;
|
||||
private int mIconResourceId = 0;
|
||||
private int mButtonColorResourceId = 0;
|
||||
|
||||
@VisibleForTesting CharSequence mMainButtonLabel;
|
||||
@VisibleForTesting CharSequence mDismissButtonLabel;
|
||||
|
||||
public BatteryTipsCardPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setLayoutResource(R.layout.battery_tips_card);
|
||||
setViewId(R.id.battery_tips_card);
|
||||
setSelectable(false);
|
||||
}
|
||||
|
||||
public void setOnConfirmListener(OnConfirmListener listener) {
|
||||
mOnConfirmListener = listener;
|
||||
}
|
||||
|
||||
public void setOnRejectListener(OnRejectListener listener) {
|
||||
mOnRejectListener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the icon in tips card.
|
||||
*/
|
||||
public void setIconResourceId(int resourceId) {
|
||||
if (mIconResourceId != resourceId) {
|
||||
mIconResourceId = resourceId;
|
||||
notifyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the background color for main button and the text color for dismiss button.
|
||||
*/
|
||||
public void setButtonColorResourceId(int resourceId) {
|
||||
if (mButtonColorResourceId != resourceId) {
|
||||
mButtonColorResourceId = resourceId;
|
||||
notifyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the label of main button in tips card.
|
||||
*/
|
||||
public void setMainButtonLabel(CharSequence label) {
|
||||
if (!TextUtils.equals(mMainButtonLabel, label)) {
|
||||
mMainButtonLabel = label;
|
||||
notifyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the label of dismiss button in tips card.
|
||||
*/
|
||||
public void setDismissButtonLabel(CharSequence label) {
|
||||
if (!TextUtils.equals(mDismissButtonLabel, label)) {
|
||||
mDismissButtonLabel = label;
|
||||
notifyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
final int viewId = view.getId();
|
||||
if (viewId == R.id.main_button || viewId == R.id.battery_tips_card) {
|
||||
if (mOnConfirmListener != null) {
|
||||
mOnConfirmListener.onConfirm();
|
||||
}
|
||||
} else if (viewId == R.id.dismiss_button) {
|
||||
if (mOnRejectListener != null) {
|
||||
mOnRejectListener.onReject();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder view) {
|
||||
super.onBindViewHolder(view);
|
||||
|
||||
((TextView) view.findViewById(R.id.title)).setText(getTitle());
|
||||
|
||||
final LinearLayout tipsCard = (LinearLayout) view.findViewById(R.id.battery_tips_card);
|
||||
tipsCard.setOnClickListener(this);
|
||||
final MaterialButton mainButton = (MaterialButton) view.findViewById(R.id.main_button);
|
||||
mainButton.setOnClickListener(this);
|
||||
mainButton.setText(mMainButtonLabel);
|
||||
final MaterialButton dismissButton =
|
||||
(MaterialButton) view.findViewById(R.id.dismiss_button);
|
||||
dismissButton.setOnClickListener(this);
|
||||
dismissButton.setText(mDismissButtonLabel);
|
||||
if (mButtonColorResourceId != 0) {
|
||||
final int colorInt = getContext().getColor(mButtonColorResourceId);
|
||||
mainButton.setBackgroundColor(colorInt);
|
||||
dismissButton.setTextColor(colorInt);
|
||||
}
|
||||
|
||||
if (mIconResourceId != 0) {
|
||||
((ImageView) view.findViewById(R.id.icon)).setImageResource(mIconResourceId);
|
||||
}
|
||||
}
|
||||
}
|
@@ -25,6 +25,7 @@ import androidx.preference.PreferenceScreen;
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.widget.TipCardPreference;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
/** Controls the update for battery tips card */
|
||||
@@ -50,10 +51,9 @@ public class BatteryTipsController extends BasePreferenceController {
|
||||
void onAnomalyReject();
|
||||
}
|
||||
|
||||
private OnAnomalyConfirmListener mOnAnomalyConfirmListener;
|
||||
private OnAnomalyRejectListener mOnAnomalyRejectListener;
|
||||
|
||||
@VisibleForTesting BatteryTipsCardPreference mCardPreference;
|
||||
@VisibleForTesting OnAnomalyConfirmListener mOnAnomalyConfirmListener;
|
||||
@VisibleForTesting OnAnomalyRejectListener mOnAnomalyRejectListener;
|
||||
@VisibleForTesting TipCardPreference mCardPreference;
|
||||
@VisibleForTesting AnomalyEventWrapper mAnomalyEventWrapper = null;
|
||||
@VisibleForTesting Boolean mIsAcceptable = false;
|
||||
|
||||
@@ -117,42 +117,20 @@ public class BatteryTipsController extends BasePreferenceController {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set battery tips card listener
|
||||
mCardPreference.setOnConfirmListener(
|
||||
mCardPreference.setPrimaryButtonAction(
|
||||
() -> {
|
||||
mCardPreference.setVisible(false);
|
||||
if (mOnAnomalyConfirmListener != null) {
|
||||
mOnAnomalyConfirmListener.onAnomalyConfirm();
|
||||
} else if (mAnomalyEventWrapper.updateSystemSettingsIfAvailable()
|
||||
|| mAnomalyEventWrapper.launchSubSetting()) {
|
||||
mMetricsFeatureProvider.action(
|
||||
/* attribution= */ SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
/* action= */ SettingsEnums.ACTION_BATTERY_TIPS_CARD_ACCEPT,
|
||||
/* pageId= */ SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
/* key= */ ANOMALY_KEY,
|
||||
/* value= */ anomalyKeyNumber);
|
||||
}
|
||||
onBatteryTipsCardDismiss(anomalyKeyNumber);
|
||||
return null;
|
||||
});
|
||||
mCardPreference.setOnRejectListener(
|
||||
mCardPreference.setSecondaryButtonAction(
|
||||
() -> {
|
||||
mCardPreference.setVisible(false);
|
||||
if (mOnAnomalyRejectListener != null) {
|
||||
mOnAnomalyRejectListener.onAnomalyReject();
|
||||
}
|
||||
// For anomaly events with same record key, dismissed until next time full
|
||||
// charged.
|
||||
final String dismissRecordKey = mAnomalyEventWrapper.getDismissRecordKey();
|
||||
if (!TextUtils.isEmpty(dismissRecordKey)) {
|
||||
DatabaseUtils.setDismissedPowerAnomalyKeys(mContext, dismissRecordKey);
|
||||
}
|
||||
mMetricsFeatureProvider.action(
|
||||
/* attribution= */ SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
/* action= */ SettingsEnums.ACTION_BATTERY_TIPS_CARD_DISMISS,
|
||||
/* pageId= */ SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
/* key= */ ANOMALY_KEY,
|
||||
/* value= */ anomalyKeyNumber);
|
||||
onBatteryTipsCardAccept(anomalyKeyNumber);
|
||||
return null;
|
||||
});
|
||||
|
||||
mCardPreference.setPrimaryButtonVisibility(true);
|
||||
mCardPreference.setSecondaryButtonVisibility(true);
|
||||
mCardPreference.buildContent();
|
||||
mCardPreference.setVisible(true);
|
||||
mMetricsFeatureProvider.action(
|
||||
/* attribution= */ SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
@@ -161,4 +139,37 @@ public class BatteryTipsController extends BasePreferenceController {
|
||||
/* key= */ ANOMALY_KEY,
|
||||
/* value= */ anomalyKeyNumber);
|
||||
}
|
||||
|
||||
private void onBatteryTipsCardDismiss(final int anomalyKeyNumber) {
|
||||
mCardPreference.setVisible(false);
|
||||
if (mOnAnomalyRejectListener != null) {
|
||||
mOnAnomalyRejectListener.onAnomalyReject();
|
||||
}
|
||||
// For anomaly events with same record key, dismissed until next time full charged.
|
||||
final String dismissRecordKey = mAnomalyEventWrapper.getDismissRecordKey();
|
||||
if (!TextUtils.isEmpty(dismissRecordKey)) {
|
||||
DatabaseUtils.setDismissedPowerAnomalyKeys(mContext, dismissRecordKey);
|
||||
}
|
||||
mMetricsFeatureProvider.action(
|
||||
/* attribution= */ SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
/* action= */ SettingsEnums.ACTION_BATTERY_TIPS_CARD_DISMISS,
|
||||
/* pageId= */ SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
/* key= */ ANOMALY_KEY,
|
||||
/* value= */ anomalyKeyNumber);
|
||||
}
|
||||
|
||||
private void onBatteryTipsCardAccept(final int anomalyKeyNumber) {
|
||||
mCardPreference.setVisible(false);
|
||||
if (mOnAnomalyConfirmListener != null) {
|
||||
mOnAnomalyConfirmListener.onAnomalyConfirm();
|
||||
} else if (mAnomalyEventWrapper.updateSystemSettingsIfAvailable()
|
||||
|| mAnomalyEventWrapper.launchSubSetting()) {
|
||||
mMetricsFeatureProvider.action(
|
||||
/* attribution= */ SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
/* action= */ SettingsEnums.ACTION_BATTERY_TIPS_CARD_ACCEPT,
|
||||
/* pageId= */ SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
/* key= */ ANOMALY_KEY,
|
||||
/* value= */ anomalyKeyNumber);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -19,6 +19,7 @@ package com.android.settings.widget
|
||||
import android.content.Context
|
||||
import android.content.res.Resources
|
||||
import android.util.AttributeSet
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.res.vectorResource
|
||||
import com.android.settings.spa.preference.ComposePreference
|
||||
@@ -37,6 +38,9 @@ constructor(
|
||||
/** A icon resource id for displaying icon on tips card. */
|
||||
var iconResId: Int? = null
|
||||
|
||||
/** A color resource id for displaying icon and button text on tips card. */
|
||||
var tintColorResId: Int? = null
|
||||
|
||||
/** The primary button's text. */
|
||||
var primaryButtonText: String = ""
|
||||
|
||||
@@ -85,6 +89,8 @@ constructor(
|
||||
title = title?.toString() ?: "",
|
||||
text = summary?.toString() ?: "",
|
||||
buttons = listOfNotNull(configPrimaryButton(), configSecondaryButton()),
|
||||
tintColor = tintColorResId?.let { Color(context.getColor(it)) }
|
||||
?: Color.Unspecified,
|
||||
onDismiss = onDismiss,
|
||||
imageVector =
|
||||
iconResId
|
||||
|
@@ -1,293 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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.settings.fuelgauge.batteryusage;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.provider.Settings;
|
||||
import android.util.Pair;
|
||||
import android.view.View;
|
||||
|
||||
import com.android.settings.DisplaySettings;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.testutils.BatteryTestUtils;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.TimeZone;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public final class BatteryTipsCardPreferenceTest {
|
||||
|
||||
private Context mContext;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private BatteryTipsCardPreference mBatteryTipsCardPreference;
|
||||
private PowerUsageAdvanced mPowerUsageAdvanced;
|
||||
private BatteryTipsController mBatteryTipsController;
|
||||
private BatteryChartPreferenceController mBatteryChartPreferenceController;
|
||||
|
||||
@Mock private View mFakeView;
|
||||
@Mock private BatteryUsageBreakdownController mBatteryUsageBreakdownController;
|
||||
@Mock private BatteryDiffEntry mFakeEntry;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
mBatteryTipsCardPreference = new BatteryTipsCardPreference(mContext, /* attrs= */ null);
|
||||
mBatteryTipsController = new BatteryTipsController(mContext);
|
||||
mBatteryChartPreferenceController =
|
||||
spy(new BatteryChartPreferenceController(mContext, null, null));
|
||||
mBatteryChartPreferenceController.mPrefContext = mContext;
|
||||
mBatteryTipsController.mCardPreference = mBatteryTipsCardPreference;
|
||||
|
||||
mPowerUsageAdvanced = spy(new PowerUsageAdvanced());
|
||||
doReturn(mContext).when(mPowerUsageAdvanced).getContext();
|
||||
mPowerUsageAdvanced.mBatteryTipsController = mBatteryTipsController;
|
||||
mPowerUsageAdvanced.mBatteryChartPreferenceController = mBatteryChartPreferenceController;
|
||||
mPowerUsageAdvanced.mBatteryUsageBreakdownController = mBatteryUsageBreakdownController;
|
||||
mPowerUsageAdvanced.mBatteryLevelData =
|
||||
Optional.of(
|
||||
new BatteryLevelData(
|
||||
Map.of(
|
||||
1694354400000L, 1, // 2023-09-10 22:00:00
|
||||
1694361600000L, 2, // 2023-09-11 00:00:00
|
||||
1694368800000L, 3))); // 2023-09-11 02:00:00
|
||||
doReturn("TestEntriesKey").when(mFakeEntry).getKey();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructor_returnExpectedResult() {
|
||||
assertThat(mBatteryTipsCardPreference.getLayoutResource())
|
||||
.isEqualTo(R.layout.battery_tips_card);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onClick_mainBtnOfSettingsAnomalyLaunchPage_getAdaptiveBrightnessLauncher() {
|
||||
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
|
||||
PowerAnomalyEvent adaptiveBrightnessAnomaly =
|
||||
BatteryTestUtils.createAdaptiveBrightnessAnomalyEvent(/* changeSettings= */ false);
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true);
|
||||
when(mFakeView.getId()).thenReturn(R.id.main_button);
|
||||
doNothing().when(mContext).startActivity(captor.capture());
|
||||
|
||||
mPowerUsageAdvanced.onDisplayAnomalyEventUpdated(
|
||||
adaptiveBrightnessAnomaly, adaptiveBrightnessAnomaly);
|
||||
mBatteryTipsCardPreference.onClick(mFakeView);
|
||||
|
||||
assertThat(mBatteryTipsCardPreference.isVisible()).isFalse();
|
||||
verify(mContext).startActivity(any(Intent.class));
|
||||
final Intent intent = captor.getValue();
|
||||
assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
|
||||
.isEqualTo(DisplaySettings.class.getName());
|
||||
assertThat(intent.getIntExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY, -1))
|
||||
.isEqualTo(SettingsEnums.DISPLAY);
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_SHOW,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_BRIGHTNESS.getNumber());
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_ACCEPT,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_BRIGHTNESS.getNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onClick_mainBtnOfSettingsAnomalyChangeSettings_settingsChanged()
|
||||
throws Settings.SettingNotFoundException {
|
||||
Settings.System.putInt(
|
||||
mContext.getContentResolver(),
|
||||
Settings.System.SCREEN_BRIGHTNESS_MODE,
|
||||
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
|
||||
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
|
||||
PowerAnomalyEvent adaptiveBrightnessAnomaly =
|
||||
BatteryTestUtils.createAdaptiveBrightnessAnomalyEvent(/* changeSettings= */ true);
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true);
|
||||
when(mFakeView.getId()).thenReturn(R.id.main_button);
|
||||
doNothing().when(mContext).startActivity(captor.capture());
|
||||
|
||||
mPowerUsageAdvanced.onDisplayAnomalyEventUpdated(
|
||||
adaptiveBrightnessAnomaly, adaptiveBrightnessAnomaly);
|
||||
mBatteryTipsCardPreference.onClick(mFakeView);
|
||||
|
||||
assertThat(mBatteryTipsCardPreference.isVisible()).isFalse();
|
||||
assertThat(
|
||||
Settings.System.getInt(
|
||||
mContext.getContentResolver(),
|
||||
Settings.System.SCREEN_BRIGHTNESS_MODE))
|
||||
.isEqualTo(Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
|
||||
verify(mContext, never()).startActivity(any(Intent.class));
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_SHOW,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_BRIGHTNESS.getNumber());
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_ACCEPT,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_BRIGHTNESS.getNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onClick_dismissBtnOfSettingsAnomaly_cardDismissAndLogged() {
|
||||
final PowerAnomalyEvent screenTimeoutAnomaly =
|
||||
BatteryTestUtils.createScreenTimeoutAnomalyEvent();
|
||||
DatabaseUtils.removeDismissedPowerAnomalyKeys(mContext);
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true);
|
||||
when(mFakeView.getId()).thenReturn(R.id.dismiss_button);
|
||||
|
||||
mPowerUsageAdvanced.onDisplayAnomalyEventUpdated(
|
||||
screenTimeoutAnomaly, screenTimeoutAnomaly);
|
||||
mBatteryTipsCardPreference.onClick(mFakeView);
|
||||
|
||||
assertThat(mBatteryTipsCardPreference.isVisible()).isFalse();
|
||||
assertThat(DatabaseUtils.getDismissedPowerAnomalyKeys(mContext)).hasSize(1);
|
||||
assertThat(DatabaseUtils.getDismissedPowerAnomalyKeys(mContext))
|
||||
.contains(PowerAnomalyKey.KEY_SCREEN_TIMEOUT.name());
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_SHOW,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_SCREEN_TIMEOUT.getNumber());
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_DISMISS,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_SCREEN_TIMEOUT.getNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onClick_mainBtnOfAppsAnomaly_selectHighlightSlot() {
|
||||
final PowerAnomalyEvent appsAnomaly = BatteryTestUtils.createAppAnomalyEvent();
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true);
|
||||
when(mFakeView.getId()).thenReturn(R.id.main_button);
|
||||
doNothing().when(mBatteryChartPreferenceController).selectHighlightSlotIndex();
|
||||
when(mPowerUsageAdvanced.findRelatedBatteryDiffEntry(any())).thenReturn(mFakeEntry);
|
||||
|
||||
mPowerUsageAdvanced.onDisplayAnomalyEventUpdated(appsAnomaly, appsAnomaly);
|
||||
assertHighlightSlotIndexPair(1, 0);
|
||||
mBatteryTipsCardPreference.onClick(mFakeView);
|
||||
|
||||
assertThat(mBatteryTipsCardPreference.isVisible()).isFalse();
|
||||
verify(mContext, never()).startActivity(any(Intent.class));
|
||||
verify(mBatteryChartPreferenceController).onHighlightSlotIndexUpdate(eq(1), eq(0));
|
||||
verify(mBatteryChartPreferenceController).selectHighlightSlotIndex();
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_SHOW,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_APP_TOTAL_HIGHER_THAN_USUAL.getNumber());
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_ACCEPT,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_APP_TOTAL_HIGHER_THAN_USUAL.getNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onClick_dismissBtnOfAppsAnomaly_keepHighlightSlotIndex() {
|
||||
final PowerAnomalyEvent appsAnomaly = BatteryTestUtils.createAppAnomalyEvent();
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true);
|
||||
when(mFakeView.getId()).thenReturn(R.id.dismiss_button);
|
||||
when(mPowerUsageAdvanced.findRelatedBatteryDiffEntry(any())).thenReturn(mFakeEntry);
|
||||
|
||||
mPowerUsageAdvanced.onDisplayAnomalyEventUpdated(appsAnomaly, appsAnomaly);
|
||||
assertHighlightSlotIndexPair(1, 0);
|
||||
mBatteryTipsCardPreference.onClick(mFakeView);
|
||||
|
||||
assertThat(mBatteryTipsCardPreference.isVisible()).isFalse();
|
||||
verify(mContext, never()).startActivity(any(Intent.class));
|
||||
verify(mBatteryChartPreferenceController).onHighlightSlotIndexUpdate(eq(1), eq(0));
|
||||
verify(mBatteryChartPreferenceController, never()).selectHighlightSlotIndex();
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_SHOW,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_APP_TOTAL_HIGHER_THAN_USUAL.getNumber());
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_DISMISS,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_APP_TOTAL_HIGHER_THAN_USUAL.getNumber());
|
||||
}
|
||||
|
||||
private void assertHighlightSlotIndexPair(
|
||||
int dailyHighlightSlotIndex, int hourlyHighlightSlotIndex) {
|
||||
assertThat(mPowerUsageAdvanced.mBatteryLevelData.isPresent()).isTrue();
|
||||
assertThat(mPowerUsageAdvanced.mHighlightEventWrapper.isPresent()).isTrue();
|
||||
Pair<Integer, Integer> slotIndexPair =
|
||||
mPowerUsageAdvanced
|
||||
.mHighlightEventWrapper
|
||||
.get()
|
||||
.getHighlightSlotPair(mPowerUsageAdvanced.mBatteryLevelData.get());
|
||||
assertThat(slotIndexPair)
|
||||
.isEqualTo(Pair.create(dailyHighlightSlotIndex, hourlyHighlightSlotIndex));
|
||||
assertThat(mPowerUsageAdvanced.mBatteryChartPreferenceController.mDailyHighlightSlotIndex)
|
||||
.isEqualTo(dailyHighlightSlotIndex);
|
||||
assertThat(mPowerUsageAdvanced.mBatteryChartPreferenceController.mHourlyHighlightSlotIndex)
|
||||
.isEqualTo(hourlyHighlightSlotIndex);
|
||||
}
|
||||
}
|
@@ -16,8 +16,13 @@
|
||||
|
||||
package com.android.settings.fuelgauge.batteryusage;
|
||||
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -29,11 +34,11 @@ import android.os.LocaleList;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.BatteryTestUtils;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.widget.TipCardPreference;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
@@ -47,134 +52,156 @@ public final class BatteryTipsControllerTest {
|
||||
private Context mContext;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private BatteryTipsController mBatteryTipsController;
|
||||
|
||||
@Mock private BatteryTipsCardPreference mBatteryTipsCardPreference;
|
||||
private TipCardPreference mCardPreference;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
Locale.setDefault(new Locale("en_US"));
|
||||
org.robolectric.shadows.ShadowSettings.set24HourTimeFormat(false);
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
final Resources resources = spy(mContext.getResources());
|
||||
resources.getConfiguration().setLocales(new LocaleList(new Locale("en_US")));
|
||||
doReturn(resources).when(mContext).getResources();
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
mBatteryTipsController = new BatteryTipsController(mContext);
|
||||
mBatteryTipsController.mCardPreference = mBatteryTipsCardPreference;
|
||||
mBatteryTipsController = spy(new BatteryTipsController(mContext));
|
||||
mCardPreference = new TipCardPreference(mContext);
|
||||
mBatteryTipsController.mCardPreference = mCardPreference;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleBatteryTipsCardUpdated_null_hidePreference() {
|
||||
mBatteryTipsController.handleBatteryTipsCardUpdated(/* powerAnomalyEvents= */ null, false);
|
||||
|
||||
verify(mBatteryTipsCardPreference).setVisible(false);
|
||||
assertThat(mCardPreference.isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleBatteryTipsCardUpdated_adaptiveBrightnessAnomaly_showAnomaly() {
|
||||
PowerAnomalyEvent event = BatteryTestUtils.createAdaptiveBrightnessAnomalyEvent();
|
||||
AnomalyEventWrapper anomalyEventWrapper =
|
||||
spy(
|
||||
new AnomalyEventWrapper(
|
||||
mContext,
|
||||
BatteryTestUtils.createAdaptiveBrightnessAnomalyEvent(true)));
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true);
|
||||
|
||||
mBatteryTipsController.handleBatteryTipsCardUpdated(
|
||||
new AnomalyEventWrapper(mContext, event), false);
|
||||
mBatteryTipsController.handleBatteryTipsCardUpdated(anomalyEventWrapper, false);
|
||||
|
||||
// Check pre-defined string
|
||||
verify(mBatteryTipsCardPreference)
|
||||
.setTitle("Turn on adaptive brightness to extend battery life");
|
||||
verify(mBatteryTipsCardPreference).setIconResourceId(R.drawable.ic_battery_tips_lightbulb);
|
||||
verify(mBatteryTipsCardPreference).setButtonColorResourceId(R.color.color_accent_selector);
|
||||
verify(mBatteryTipsCardPreference).setMainButtonLabel("View Settings");
|
||||
verify(mBatteryTipsCardPreference).setDismissButtonLabel("Got it");
|
||||
// Check proto info
|
||||
verify(mBatteryTipsCardPreference).setVisible(true);
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_SHOW,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_BRIGHTNESS.getNumber());
|
||||
assertThat(mCardPreference.getTitle())
|
||||
.isEqualTo("Turn on adaptive brightness to extend battery life");
|
||||
assertThat(mCardPreference.getPrimaryButtonText()).isEqualTo("Got it");
|
||||
assertThat(mCardPreference.getSecondaryButtonText()).isEqualTo("View Settings");
|
||||
assertThat(mCardPreference.getIconResId()).isEqualTo(R.drawable.ic_battery_tips_lightbulb);
|
||||
assertThat(mCardPreference.getTintColorResId()).isEqualTo(R.color.color_accent_selector);
|
||||
assertThat(mCardPreference.getPrimaryButtonVisibility()).isTrue();
|
||||
assertThat(mCardPreference.getSecondaryButtonVisibility()).isTrue();
|
||||
assertCardButtonActionAndMetrics(anomalyEventWrapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleBatteryTipsCardUpdated_screenTimeoutAnomaly_showAnomaly() {
|
||||
PowerAnomalyEvent event = BatteryTestUtils.createScreenTimeoutAnomalyEvent();
|
||||
AnomalyEventWrapper anomalyEventWrapper =
|
||||
spy(
|
||||
new AnomalyEventWrapper(
|
||||
mContext, BatteryTestUtils.createScreenTimeoutAnomalyEvent(true)));
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true);
|
||||
|
||||
mBatteryTipsController.handleBatteryTipsCardUpdated(
|
||||
new AnomalyEventWrapper(mContext, event), false);
|
||||
mBatteryTipsController.handleBatteryTipsCardUpdated(anomalyEventWrapper, false);
|
||||
|
||||
verify(mBatteryTipsCardPreference).setTitle("Reduce screen timeout to extend battery life");
|
||||
verify(mBatteryTipsCardPreference).setIconResourceId(R.drawable.ic_battery_tips_lightbulb);
|
||||
verify(mBatteryTipsCardPreference).setButtonColorResourceId(R.color.color_accent_selector);
|
||||
verify(mBatteryTipsCardPreference).setMainButtonLabel("View Settings");
|
||||
verify(mBatteryTipsCardPreference).setDismissButtonLabel("Got it");
|
||||
verify(mBatteryTipsCardPreference).setVisible(true);
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_SHOW,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_SCREEN_TIMEOUT.getNumber());
|
||||
assertThat(mCardPreference.getTitle())
|
||||
.isEqualTo("Reduce screen timeout to extend battery life");
|
||||
assertThat(mCardPreference.getPrimaryButtonText()).isEqualTo("Got it");
|
||||
assertThat(mCardPreference.getSecondaryButtonText()).isEqualTo("View Settings");
|
||||
assertThat(mCardPreference.getIconResId()).isEqualTo(R.drawable.ic_battery_tips_lightbulb);
|
||||
assertThat(mCardPreference.getTintColorResId()).isEqualTo(R.color.color_accent_selector);
|
||||
assertThat(mCardPreference.getPrimaryButtonVisibility()).isTrue();
|
||||
assertThat(mCardPreference.getSecondaryButtonVisibility()).isTrue();
|
||||
assertCardButtonActionAndMetrics(anomalyEventWrapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleBatteryTipsCardUpdated_screenTimeoutAnomalyHasTitle_showAnomaly() {
|
||||
PowerAnomalyEvent event = BatteryTestUtils.createScreenTimeoutAnomalyEvent();
|
||||
PowerAnomalyEvent anomalyEvent = BatteryTestUtils.createScreenTimeoutAnomalyEvent(true);
|
||||
String testTitle = "TestTitle";
|
||||
event =
|
||||
event.toBuilder()
|
||||
anomalyEvent =
|
||||
anomalyEvent.toBuilder()
|
||||
.setWarningBannerInfo(
|
||||
event.getWarningBannerInfo().toBuilder()
|
||||
anomalyEvent.getWarningBannerInfo().toBuilder()
|
||||
.setTitleString(testTitle)
|
||||
.build())
|
||||
.build();
|
||||
AnomalyEventWrapper anomalyEventWrapper =
|
||||
spy(new AnomalyEventWrapper(mContext, anomalyEvent));
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true);
|
||||
|
||||
mBatteryTipsController.handleBatteryTipsCardUpdated(
|
||||
new AnomalyEventWrapper(mContext, event), false);
|
||||
mBatteryTipsController.handleBatteryTipsCardUpdated(anomalyEventWrapper, false);
|
||||
|
||||
verify(mBatteryTipsCardPreference).setTitle(testTitle);
|
||||
verify(mBatteryTipsCardPreference).setIconResourceId(R.drawable.ic_battery_tips_lightbulb);
|
||||
verify(mBatteryTipsCardPreference).setButtonColorResourceId(R.color.color_accent_selector);
|
||||
verify(mBatteryTipsCardPreference).setMainButtonLabel("View Settings");
|
||||
verify(mBatteryTipsCardPreference).setDismissButtonLabel("Got it");
|
||||
verify(mBatteryTipsCardPreference).setVisible(true);
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_SHOW,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_SCREEN_TIMEOUT.getNumber());
|
||||
assertThat(mCardPreference.getTitle()).isEqualTo(testTitle);
|
||||
assertThat(mCardPreference.getPrimaryButtonText()).isEqualTo("Got it");
|
||||
assertThat(mCardPreference.getSecondaryButtonText()).isEqualTo("View Settings");
|
||||
assertThat(mCardPreference.getIconResId()).isEqualTo(R.drawable.ic_battery_tips_lightbulb);
|
||||
assertThat(mCardPreference.getTintColorResId()).isEqualTo(R.color.color_accent_selector);
|
||||
assertThat(mCardPreference.getPrimaryButtonVisibility()).isTrue();
|
||||
assertThat(mCardPreference.getSecondaryButtonVisibility()).isTrue();
|
||||
assertCardButtonActionAndMetrics(anomalyEventWrapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleBatteryTipsCardUpdated_appAnomaly_showAnomaly() {
|
||||
PowerAnomalyEvent event = BatteryTestUtils.createAppAnomalyEvent();
|
||||
AnomalyEventWrapper anomalyEventWrapper =
|
||||
spy(new AnomalyEventWrapper(mContext, BatteryTestUtils.createAppAnomalyEvent()));
|
||||
when(mFeatureFactory.powerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true);
|
||||
|
||||
AnomalyEventWrapper eventWrapper = new AnomalyEventWrapper(mContext, event);
|
||||
eventWrapper.setRelatedBatteryDiffEntry(new BatteryDiffEntry(mContext, "", "Chrome", 0));
|
||||
mBatteryTipsController.handleBatteryTipsCardUpdated(eventWrapper, false);
|
||||
anomalyEventWrapper.setRelatedBatteryDiffEntry(
|
||||
new BatteryDiffEntry(mContext, "", "Chrome", 0));
|
||||
mBatteryTipsController.setOnAnomalyConfirmListener(
|
||||
() -> mBatteryTipsController.acceptTipsCard());
|
||||
mBatteryTipsController.handleBatteryTipsCardUpdated(anomalyEventWrapper, true);
|
||||
|
||||
verify(mBatteryTipsCardPreference).setTitle("Chrome used more battery than usual");
|
||||
verify(mBatteryTipsCardPreference)
|
||||
.setIconResourceId(R.drawable.ic_battery_tips_warning_icon);
|
||||
verify(mBatteryTipsCardPreference)
|
||||
.setButtonColorResourceId(R.color.color_battery_anomaly_app_warning_selector);
|
||||
verify(mBatteryTipsCardPreference).setMainButtonLabel("Check");
|
||||
verify(mBatteryTipsCardPreference).setDismissButtonLabel("Got it");
|
||||
verify(mBatteryTipsCardPreference).setVisible(true);
|
||||
assertThat(mCardPreference.getTitle()).isEqualTo("Chrome used more battery than usual");
|
||||
assertThat(mCardPreference.getPrimaryButtonText()).isEqualTo("Got it");
|
||||
assertThat(mCardPreference.getSecondaryButtonText()).isEqualTo("Check");
|
||||
assertThat(mCardPreference.getIconResId())
|
||||
.isEqualTo(R.drawable.ic_battery_tips_warning_icon);
|
||||
assertThat(mCardPreference.getTintColorResId())
|
||||
.isEqualTo(R.color.color_battery_anomaly_app_warning_selector);
|
||||
assertThat(mCardPreference.getPrimaryButtonVisibility()).isTrue();
|
||||
assertThat(mCardPreference.getSecondaryButtonVisibility()).isTrue();
|
||||
assertThat(mCardPreference.isVisible()).isTrue();
|
||||
assertCardButtonActionAndMetrics(anomalyEventWrapper);
|
||||
}
|
||||
|
||||
private void assertCardButtonActionAndMetrics(final AnomalyEventWrapper anomalyEventWrapper) {
|
||||
when(anomalyEventWrapper.updateSystemSettingsIfAvailable()).thenReturn(true);
|
||||
|
||||
final int powerAnomalyKeyNumber = anomalyEventWrapper.getAnomalyKeyNumber();
|
||||
assertCardMetrics(SettingsEnums.ACTION_BATTERY_TIPS_CARD_SHOW, powerAnomalyKeyNumber);
|
||||
assertThat(mCardPreference.isVisible()).isTrue();
|
||||
|
||||
// Check accept button action
|
||||
mCardPreference.setVisible(true);
|
||||
mCardPreference.getSecondaryButtonAction().invoke();
|
||||
assertCardMetrics(SettingsEnums.ACTION_BATTERY_TIPS_CARD_ACCEPT, powerAnomalyKeyNumber);
|
||||
assertThat(mCardPreference.isVisible()).isFalse();
|
||||
final boolean isAppAnomalyCard = powerAnomalyKeyNumber > 1;
|
||||
verify(anomalyEventWrapper, isAppAnomalyCard ? never() : times(1))
|
||||
.updateSystemSettingsIfAvailable();
|
||||
|
||||
// Check reject button action
|
||||
mCardPreference.setVisible(true);
|
||||
mCardPreference.getPrimaryButtonAction().invoke();
|
||||
assertCardMetrics(SettingsEnums.ACTION_BATTERY_TIPS_CARD_DISMISS, powerAnomalyKeyNumber);
|
||||
assertThat(mCardPreference.isVisible()).isFalse();
|
||||
}
|
||||
|
||||
private void assertCardMetrics(final int action, final int powerAnomalyKeyNumber) {
|
||||
verify(mFeatureFactory.metricsFeatureProvider)
|
||||
.action(
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
SettingsEnums.ACTION_BATTERY_TIPS_CARD_SHOW,
|
||||
action,
|
||||
SettingsEnums.FUELGAUGE_BATTERY_HISTORY_DETAIL,
|
||||
BatteryTipsController.ANOMALY_KEY,
|
||||
PowerAnomalyKey.KEY_APP_TOTAL_HIGHER_THAN_USUAL.getNumber());
|
||||
powerAnomalyKeyNumber);
|
||||
}
|
||||
}
|
||||
|
@@ -264,18 +264,28 @@ public class BatteryTestUtils {
|
||||
|
||||
/** Create a power anomaly event proto of screen timeout. */
|
||||
public static PowerAnomalyEvent createScreenTimeoutAnomalyEvent() {
|
||||
return createScreenTimeoutAnomalyEvent(false);
|
||||
}
|
||||
|
||||
/** Create a power anomaly event proto of screen timeout. */
|
||||
public static PowerAnomalyEvent createScreenTimeoutAnomalyEvent(boolean changeSettings) {
|
||||
WarningBannerInfo.Builder warningBannerInfoBuilder =
|
||||
WarningBannerInfo.newBuilder()
|
||||
.setMainButtonDestination(ScreenTimeoutSettings.class.getName())
|
||||
.setMainButtonSourceMetricsCategory(SettingsEnums.SCREEN_TIMEOUT)
|
||||
.setMainButtonSourceHighlightKey("60000");
|
||||
if (changeSettings) {
|
||||
warningBannerInfoBuilder
|
||||
.setMainButtonConfigSettingsName(Settings.System.SCREEN_OFF_TIMEOUT)
|
||||
.setMainButtonConfigSettingsValue(60000);
|
||||
}
|
||||
return PowerAnomalyEvent.newBuilder()
|
||||
.setEventId("ScreenTimeoutAnomaly")
|
||||
.setType(PowerAnomalyType.TYPE_SETTINGS_BANNER)
|
||||
.setKey(PowerAnomalyKey.KEY_SCREEN_TIMEOUT)
|
||||
.setDismissRecordKey(PowerAnomalyKey.KEY_SCREEN_TIMEOUT.name())
|
||||
.setScore(1.1f)
|
||||
.setWarningBannerInfo(
|
||||
WarningBannerInfo.newBuilder()
|
||||
.setMainButtonDestination(ScreenTimeoutSettings.class.getName())
|
||||
.setMainButtonSourceMetricsCategory(SettingsEnums.SCREEN_TIMEOUT)
|
||||
.setMainButtonSourceHighlightKey("60000")
|
||||
.build())
|
||||
.setWarningBannerInfo(warningBannerInfoBuilder.build())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user