Implement the battery tips cards.(1/2)

Bug: 291689623
Test: Manual
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:3ff665bd3292986030fdd8a3689b35bbf6f5a615)
Change-Id: I4443cdb21b3ba30900fc2f6fcc21c4c56dc1293f
This commit is contained in:
mxyyiyi
2023-07-19 13:28:52 +08:00
committed by Xinyi Mao
parent db17697b67
commit c4ac74872b
20 changed files with 657 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
<?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:radius="@dimen/battery_tips_card_corner_radius_normal" />
</shape>

View File

@@ -0,0 +1,27 @@
<?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>

View File

@@ -0,0 +1,27 @@
<?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>

View File

@@ -0,0 +1,25 @@
<!--
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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?android:attr/textColorSecondary"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12 19,6.41z"/>
</vector>

View File

@@ -0,0 +1,32 @@
<!--
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.
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp">
<item>
<shape android:shape="oval">
<size
android:width="24dp"
android:height="24dp" />
<solid android:color="?android:attr/colorBackground" />
</shape>
</item>
<item android:drawable="@drawable/ic_battery_tips_close"
android:gravity="center"
android:width="16dp"
android:height="16dp"/>
</layer-list>

View File

@@ -0,0 +1,25 @@
<!--
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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="32dp"
android:height="32dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?android:attr/colorAccent"
android:pathData="M7,20h4c0,1.1 -0.9,2 -2,2S7,21.1 7,20zM5,19h8v-2H5V19zM16.5,9.5c0,3.82 -2.66,5.86 -3.77,6.5H5.27C4.16,15.36 1.5,13.32 1.5,9.5C1.5,5.36 4.86,2 9,2S16.5,5.36 16.5,9.5zM14.5,9.5C14.5,6.47 12.03,4 9,4S3.5,6.47 3.5,9.5c0,2.47 1.49,3.89 2.35,4.5h6.3C13.01,13.39 14.5,11.97 14.5,9.5zM21.37,7.37L20,8l1.37,0.63L22,10l0.63,-1.37L24,8l-1.37,-0.63L22,6L21.37,7.37zM19,6l0.94,-2.06L22,3l-2.06,-0.94L19,0l-0.94,2.06L16,3l2.06,0.94L19,6z"/>
</vector>

View File

@@ -0,0 +1,25 @@
<!--
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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="?android:attr/colorAccent"
android:pathData="M242,120L686,120L686,632L408,920L369,889Q363,884 360,875Q357,866 357,853L357,843L402,632L103,632Q79,632 61,614Q43,596 43,572L43,490.16Q43,483 41.5,475.5Q40,468 43,461L169,171Q177.88,149.75 198.6,134.88Q219.31,120 242,120ZM626,180L229,180Q229,180 229,180Q229,180 229,180L103,479L103,572Q103,572 103,572Q103,572 103,572L476,572L423,821L626,607L626,180ZM626,607L626,607L626,572L626,572Q626,572 626,572Q626,572 626,572L626,479L626,180Q626,180 626,180Q626,180 626,180L626,180L626,607ZM686,632L686,572L819,572L819,180L686,180L686,120L879,120L879,632L686,632Z" />
</vector>

View File

@@ -0,0 +1,25 @@
<!--
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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="?android:attr/colorAccent"
android:pathData="M716,840L272,840L272,328L550,40L589,71Q595,76 598,85Q601,94 601,107L601,117L556,328L855,328Q879,328 897,346Q915,364 915,388L915,469.84Q915,477 916.5,484.5Q918,492 915,499L789,789Q780.12,810.25 759.41,825.13Q738.69,840 716,840ZM332,780L729,780Q729,780 729,780Q729,780 729,780L855,481L855,388Q855,388 855,388Q855,388 855,388L482,388L535,139L332,353L332,780ZM332,353L332,353L332,388L332,388Q332,388 332,388Q332,388 332,388L332,481L332,780Q332,780 332,780Q332,780 332,780L332,780L332,353ZM272,328L272,388L139,388L139,780L272,780L272,840L79,840L79,328L272,328Z" />
</vector>

View File

@@ -0,0 +1,125 @@
<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:orientation="vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
<LinearLayout
android:id="@+id/tips_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/battery_tips_all_rounded_bg"
android:orientation="vertical"
android:padding="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:src="@drawable/ic_battery_tips_lightbulb" />
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageButton
android:id="@+id/dismiss_button"
style="@style/Banner.Dismiss.SettingsLib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="0dp"
android:src="@drawable/ic_battery_tips_close_icon" />
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:maxLines="2"
android:textAlignment="viewStart"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="?android:attr/textColorPrimary" />
<TextView
android:id="@+id/summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="start"
android:maxLines="10"
android:textAlignment="viewStart"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary" />
<com.google.android.material.button.MaterialButton
android:id="@+id/action_button"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="8dp"
android:text="@string/battery_tips_card_action_button"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorPrimary"
android:textStyle="bold"
app:strokeColor="?android:attr/colorAccent"
app:strokeWidth="1dp" />
</LinearLayout>
<Space
android:layout_width="0dp"
android:layout_height="1dp"/>
<LinearLayout
android:id="@+id/feedback_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/battery_tips_half_rounded_bottom_bg"
android:gravity="center_vertical|start"
android:orientation="horizontal"
android:paddingHorizontal="24dp"
android:paddingVertical="16dp"
android:visibility="gone">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="20dp"
android:layout_weight="1"
android:text="@string/battery_tips_card_feedback_info"
android:textAlignment="viewStart"
android:textColor="?android:attr/textColorPrimary"
android:textStyle="bold"/>
<ImageButton
android:id="@+id/thumb_up"
style="@style/Banner.Dismiss.SettingsLib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="20dp"
android:src="@drawable/ic_battery_tips_thumb_up" />
<ImageButton
android:id="@+id/thumb_down"
style="@style/Banner.Dismiss.SettingsLib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:src="@drawable/ic_battery_tips_thumb_down" />
</LinearLayout>
</LinearLayout>

View File

@@ -373,6 +373,10 @@
<dimen name="chartview_trapezoid_margin_start">1dp</dimen>
<dimen name="chartview_trapezoid_margin_bottom">2dp</dimen>
<!-- Battery tips card view component -->
<dimen name="battery_tips_card_corner_radius_small">4dp</dimen>
<dimen name="battery_tips_card_corner_radius_normal">24dp</dimen>
<!-- Dimensions for Dream settings cards -->
<dimen name="dream_item_min_column_width">174dp</dimen>
<dimen name="dream_item_corner_radius">28dp</dimen>

View File

@@ -9686,6 +9686,24 @@
<!-- Preference summary for battery usage list page[CHAR_LIMIT=50]-->
<string name="app_battery_usage_summary">Set battery usage for apps</string>
<!-- Label of action button in battery tips card [CHAR LIMIT=NONE] -->
<string name="battery_tips_card_action_button" translatable="false">Optimize</string>
<!-- Feedback card message in battery tips card [CHAR LIMIT=NONE] -->
<string name="battery_tips_card_feedback_info" translatable="false">Is this message helpful?</string>
<!-- Title of battery tips: adaptive brightness [CHAR LIMIT=NONE] -->
<string name="battery_tips_adaptive_brightness_title" translatable="false">Turn on adaptive brightness to extend battery life</string>
<!-- Summary of battery tips: adaptive brightness [CHAR LIMIT=NONE] -->
<string name="battery_tips_adaptive_brightness_summary" translatable="false">It will help reduce your daily battery drain by 10%</string>
<!-- Title of battery tips: reduce screen timeout [CHAR LIMIT=NONE] -->
<string name="battery_tips_screen_timeout_title" translatable="false">Reduce screen timeout to extend battery life</string>
<!-- Summary of battery tips: reduce screen timeout [CHAR LIMIT=NONE] -->
<string name="battery_tips_screen_timeout_summary" translatable="false">It will help reduce your daily battery drain by 10%</string>
<!-- Filter title for battery unrestricted[CHAR_LIMIT=50]-->
<string name="filter_battery_unrestricted_title">Unrestricted</string>

View File

@@ -21,6 +21,18 @@
android:title="@string/advanced_battery_title"
settings:keywords="@string/keywords_battery_usage">
<PreferenceCategory
android:key="battery_tips_category"
settings:controller=
"com.android.settings.fuelgauge.batteryusage.BatteryTipsController"
settings:isPreferenceVisible="false">
<com.android.settings.fuelgauge.batteryusage.BatteryTipsCardPreference
android:key="battery_tips_card"
settings:isPreferenceVisible="false" />
</PreferenceCategory>
<com.android.settings.fuelgauge.batteryusage.BatteryHistoryPreference
android:key="battery_chart"
settings:controller=

View File

@@ -37,6 +37,16 @@ public interface PowerUsageFeatureProvider {
*/
boolean isBatteryUsageEnabled();
/**
* Check whether the battery tips card is enabled in the battery usage page
*/
boolean isBatteryTipsEnabled();
/**
* Check whether the feedback card is enabled in the battery tips card
*/
boolean isBatteryTipsFeedbackEnabled();
/**
* Returns a threshold (in milliseconds) for the minimal screen on time in battery usage list
*/

View File

@@ -75,6 +75,16 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider
return true;
}
@Override
public boolean isBatteryTipsEnabled() {
return false;
}
@Override
public boolean isBatteryTipsFeedbackEnabled() {
return true;
}
@Override
public double getBatteryUsageListScreenOnTimeThresholdInMs() {
return 0;

View File

@@ -98,6 +98,20 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
void onScreenOnTimeUpdated(Long screenOnTime, String slotTimestamp);
}
/**
* A callback listener for the battery tips card is updated.
* This happens when battery tips card is ready.
*/
public interface OnBatteryTipsUpdatedListener {
/**
* The callback function for the battery tips card is updated.
* @param title the title of the battery tip card
* @param summary the summary of the battery tip card
*/
void onBatteryTipsUpdated(String title, String summary);
}
@VisibleForTesting
Context mPrefContext;
@VisibleForTesting
@@ -119,6 +133,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
private List<BatteryChartViewModel> mHourlyViewModels;
private OnBatteryUsageUpdatedListener mOnBatteryUsageUpdatedListener;
private OnScreenOnTimeUpdatedListener mOnScreenOnTimeUpdatedListener;
private OnBatteryTipsUpdatedListener mOnBatteryTipsUpdatedListener;
private final SettingsActivity mActivity;
private final MetricsFeatureProvider mMetricsFeatureProvider;
@@ -209,6 +224,10 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
mOnScreenOnTimeUpdatedListener = listener;
}
void setOnBatteryTipsUpdatedListener(OnBatteryTipsUpdatedListener listener) {
mOnBatteryTipsUpdatedListener = listener;
}
void setBatteryHistoryMap(
final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap) {
Log.d(TAG, "setBatteryHistoryMap() " + (batteryHistoryMap == null ? "null"
@@ -344,6 +363,10 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
}
mOnBatteryUsageUpdatedListener.onBatteryUsageUpdated(
slotUsageData, getSlotInformation(), isBatteryUsageMapNullOrEmpty());
if (mOnBatteryTipsUpdatedListener != null) {
mOnBatteryTipsUpdatedListener.onBatteryTipsUpdated(null, null);
}
}
return true;
}

View File

@@ -0,0 +1,99 @@
/*
* 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.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
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";
private final PowerUsageFeatureProvider mPowerUsageFeatureProvider;
private MaterialButton mActionButton;
private ImageButton mDismissButton;
private ImageButton mThumbUpButton;
private ImageButton mThumbDownButton;
private CharSequence mTitle;
private CharSequence mSummary;
public BatteryTipsCardPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setLayoutResource(R.layout.battery_tips_card);
setSelectable(false);
mPowerUsageFeatureProvider = FeatureFactory.getFeatureFactory()
.getPowerUsageFeatureProvider();
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
notifyChanged();
}
@Override
public void setSummary(CharSequence summary) {
mSummary = summary;
notifyChanged();
}
@Override
public void onClick(View view) {
// TODO: replace with the settings anomaly obtained from detectSettingsAnomaly();
}
@Override
public void onBindViewHolder(PreferenceViewHolder view) {
super.onBindViewHolder(view);
((TextView) view.findViewById(R.id.title)).setText(mTitle);
((TextView) view.findViewById(R.id.summary)).setText(mSummary);
mActionButton = (MaterialButton) view.findViewById(R.id.action_button);
mActionButton.setOnClickListener(this);
mDismissButton = (ImageButton) view.findViewById(R.id.dismiss_button);
mDismissButton.setOnClickListener(this);
if (!mPowerUsageFeatureProvider.isBatteryTipsFeedbackEnabled()) {
return;
}
view.findViewById(R.id.tips_card)
.setBackgroundResource(R.drawable.battery_tips_half_rounded_top_bg);
view.findViewById(R.id.feedback_card).setVisibility(View.VISIBLE);
mThumbUpButton = (ImageButton) view.findViewById(R.id.thumb_up);
mThumbUpButton.setOnClickListener(this);
mThumbDownButton = (ImageButton) view.findViewById(R.id.thumb_down);
mThumbDownButton.setOnClickListener(this);
}
}

View File

@@ -0,0 +1,77 @@
/*
* 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 androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
/** Controls the update for battery tips card */
public class BatteryTipsController extends BasePreferenceController {
private static final String TAG = "BatteryTipsController";
private static final String ROOT_PREFERENCE_KEY = "battery_tips_category";
private static final String CARD_PREFERENCE_KEY = "battery_tips_card";
private final PowerUsageFeatureProvider mPowerUsageFeatureProvider;
private Context mPrefContext;
private BatteryTipsCardPreference mCardPreference;
public BatteryTipsController(Context context) {
super(context, ROOT_PREFERENCE_KEY);
mPowerUsageFeatureProvider = FeatureFactory.getFeatureFactory()
.getPowerUsageFeatureProvider();
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPrefContext = screen.getContext();
mCardPreference = screen.findPreference(CARD_PREFERENCE_KEY);
}
/**
* Update the card visibility and contents.
* @param title a string not extend 2 lines.
* @param summary a string not extend 10 lines.
*/
// TODO: replace parameters with SettingsAnomaly Data Proto
public void handleBatteryTipsCardUpdated(String title, String summary) {
if (!mPowerUsageFeatureProvider.isBatteryTipsEnabled()) {
mCardPreference.setVisible(false);
return;
}
if (title == null || summary == null) {
mCardPreference.setVisible(false);
return;
}
mCardPreference.setTitle(title);
mCardPreference.setSummary(summary);
mCardPreference.setVisible(true);
}
}

View File

@@ -34,6 +34,8 @@ import androidx.loader.content.Loader;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.fuelgauge.BatteryBroadcastReceiver;
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexable;
@@ -143,6 +145,16 @@ public class PowerUsageAdvanced extends PowerUsageBase {
controllers.add(screenOnTimeController);
controllers.add(batteryUsageBreakdownController);
setBatteryChartPreferenceController();
final PowerUsageFeatureProvider powerUsageFeatureProvider =
FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider();
if (powerUsageFeatureProvider.isBatteryTipsEnabled()) {
BatteryTipsController batteryTipsController = new BatteryTipsController(context);
mBatteryChartPreferenceController.setOnBatteryTipsUpdatedListener(
batteryTipsController::handleBatteryTipsCardUpdated);
controllers.add(batteryTipsController);
}
return controllers;
}

View File

@@ -67,6 +67,15 @@ public class PowerUsageFeatureProviderImplTest {
assertThat(mPowerFeatureProvider.isBatteryUsageEnabled()).isTrue();
}
@Test
public void testIsBatteryTipsEnabled_returnFalse() {
assertThat(mPowerFeatureProvider.isBatteryTipsEnabled()).isFalse();
}
@Test
public void testIsBatteryTipsFeedbackEnabled_returnTrue() {
assertThat(mPowerFeatureProvider.isBatteryTipsFeedbackEnabled()).isTrue();
}
@Test
public void testGetBatteryUsageListConsumePowerThreshold_return0() {
assertThat(mPowerFeatureProvider.getBatteryUsageListConsumePowerThreshold()).isEqualTo(0.0);

View File

@@ -0,0 +1,50 @@
/*
* 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.Mockito.spy;
import android.content.Context;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public final class BatteryTipsCardPreferenceTest {
private Context mContext;
private BatteryTipsCardPreference mBatteryTipsCardPreference;
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.application);
mBatteryTipsCardPreference = new BatteryTipsCardPreference(mContext, /*attrs=*/ null);
}
@Test
public void constructor_returnExpectedResult() {
assertThat(mBatteryTipsCardPreference.getLayoutResource()).isEqualTo(
R.layout.battery_tips_card);
}
}