Update related UI if battery is not present

This change is to update the related UI in the battery page if the
battery is not present. This includes the following updates:
1. Update the summary of battery tile in the Settings homepage
2. Replace the battery level with "Unknown"
3. Replace the summary with help message in the battery page
4. Remove the battery meter icon

Bug: 171368508
Test: verify on an issue device
Change-Id: I892e0d137143160a0bce0c11ce9265120ebb8fd4
This commit is contained in:
Mill Chen
2020-10-21 16:32:56 +08:00
parent d682b2cc57
commit ad99e2ef38
15 changed files with 182 additions and 15 deletions

View File

@@ -25,6 +25,7 @@
style="@style/EntityHeader">
<LinearLayout
android:id="@+id/battery_info_layout"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"

View File

@@ -5778,6 +5778,11 @@
<!-- Title to display the battery percentage. [CHAR LIMIT=24] -->
<string name="battery_header_title_alternate"><xliff:g id="number" example="88">^1</xliff:g><small> <font size="20"><xliff:g id="unit" example="%">%</xliff:g></font></small></string>
<!-- Summary for top level battery tile if battery is not present. [CHAR LIMIT=NONE] -->
<string name="battery_missing_message">Problem reading your battery meter</string>
<!-- Summary to battery page if battery is not present. [CHAR LIMIT=NONE] -->
<string name="battery_missing_help_message">Problem reading your battery meter. Tap to <annotation id="url">learn more</annotation></string>
<!-- Title for force stop dialog [CHAR LIMIT=30] -->
<string name="dialog_stop_title">Stop app?</string>
<!-- Message body for force stop dialog [CHAR LIMIT=NONE] -->
@@ -7290,6 +7295,8 @@
<string name="help_uri_sim_combination_warning" translatable="false"></string>
<!-- url for the NFC and payment settings page -->
<string name="help_uri_nfc_and_payment_settings" translatable="false"></string>
<!-- url for battery page if battery is not present -->
<string name="help_url_battery_missing" translatable="false"></string>
<!-- User account title [CHAR LIMIT=30] -->
<string name="user_account_title">Account for content</string>

View File

@@ -267,6 +267,15 @@ public final class Utils extends com.android.settingslib.Utils {
return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
}
/**
* Return true if battery is present.
*/
public static boolean isBatteryPresent(Context context) {
Intent batteryBroadcast = context.registerReceiver(null /* receiver */,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return isBatteryPresent(batteryBroadcast);
}
public static String getBatteryPercentage(Intent batteryChangedIntent) {
return formatPercentage(getBatteryLevel(batteryChangedIntent));
}

View File

@@ -21,9 +21,11 @@ import android.content.Context;
import android.provider.Settings;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.internal.R;
import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
@@ -34,12 +36,27 @@ import com.android.settings.core.PreferenceControllerMixin;
public class BatteryPercentagePreferenceController extends BasePreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private Preference mPreference;
public BatteryPercentagePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
if (!Utils.isBatteryPresent(mContext)) {
// Disable battery percentage
onPreferenceChange(mPreference, false /* newValue */);
}
}
@Override
public int getAvailabilityStatus() {
if (!Utils.isBatteryPresent(mContext)) {
return CONDITIONALLY_UNAVAILABLE;
}
return mContext.getResources().getBoolean(
R.bool.config_battery_percentage_setting_available) ? AVAILABLE
: UNSUPPORTED_ON_DEVICE;

View File

@@ -21,6 +21,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.PowerManager;
import android.util.Log;
import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting;
@@ -41,6 +42,7 @@ import java.lang.annotation.RetentionPolicy;
*/
public class BatteryBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "BatteryBroadcastRcvr";
/**
* Callback when the following has been changed:
*
@@ -56,12 +58,14 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
@IntDef({BatteryUpdateType.MANUAL,
BatteryUpdateType.BATTERY_LEVEL,
BatteryUpdateType.BATTERY_SAVER,
BatteryUpdateType.BATTERY_STATUS})
BatteryUpdateType.BATTERY_STATUS,
BatteryUpdateType.BATTERY_NOT_PRESENT})
public @interface BatteryUpdateType {
int MANUAL = 0;
int BATTERY_LEVEL = 1;
int BATTERY_SAVER = 2;
int BATTERY_STATUS = 3;
int BATTERY_NOT_PRESENT = 4;
}
@VisibleForTesting
@@ -101,9 +105,11 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
if (intent != null && mBatteryListener != null) {
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
final String batteryLevel = Utils.getBatteryPercentage(intent);
final String batteryStatus = Utils.getBatteryStatus(
mContext, intent);
if (forceUpdate) {
final String batteryStatus = Utils.getBatteryStatus(mContext, intent);
if (!Utils.isBatteryPresent(intent)) {
Log.w(TAG, "Problem reading the battery meter.");
mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_NOT_PRESENT);
} else if (forceUpdate) {
mBatteryListener.onBatteryChanged(BatteryUpdateType.MANUAL);
} else if(!batteryLevel.equals(mBatteryLevel)) {
mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_LEVEL);

View File

@@ -25,6 +25,10 @@ import android.icu.text.NumberFormat;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.VisibleForTesting;
@@ -35,7 +39,9 @@ import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.utils.AnnotationSpan;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.HelpUtils;
import com.android.settingslib.Utils;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -49,6 +55,7 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
implements PreferenceControllerMixin, LifecycleObserver, OnStart {
@VisibleForTesting
static final String KEY_BATTERY_HEADER = "battery_header";
private static final String ANNOTATION_URL = "url";
@VisibleForTesting
BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
@@ -94,7 +101,11 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
mBatteryPercentText = mBatteryLayoutPref.findViewById(R.id.battery_percent);
mSummary1 = mBatteryLayoutPref.findViewById(R.id.summary1);
quickUpdateHeaderPreference();
if (com.android.settings.Utils.isBatteryPresent(mContext)) {
quickUpdateHeaderPreference();
} else {
showHelpMessage();
}
}
@Override
@@ -146,6 +157,32 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
mBatteryPercentText.setText(formatBatteryPercentageText(batteryLevel));
}
@VisibleForTesting
void showHelpMessage() {
final LinearLayout batteryInfoLayout =
mBatteryLayoutPref.findViewById(R.id.battery_info_layout);
// Remove battery meter icon
mBatteryMeterView.setVisibility(View.GONE);
// Update the width of battery info layout
final ViewGroup.LayoutParams params = batteryInfoLayout.getLayoutParams();
params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
batteryInfoLayout.setLayoutParams(params);
mBatteryPercentText.setText(mContext.getText(R.string.unknown));
// Add linkable text for learn more
final Intent helpIntent = HelpUtils.getHelpIntent(mContext,
mContext.getString(R.string.help_url_battery_missing),
mContext.getClass().getName());
final AnnotationSpan.LinkInfo linkInfo = new AnnotationSpan
.LinkInfo(mContext, ANNOTATION_URL, helpIntent);
if (linkInfo.isActionable()) {
mSummary1.setMovementMethod(LinkMovementMethod.getInstance());
mSummary1.setText(AnnotationSpan
.linkify(mContext.getText(R.string.battery_missing_help_message), linkInfo));
} else {
mSummary1.setText(mContext.getText(R.string.battery_missing_message));
}
}
private CharSequence formatBatteryPercentageText(int batteryLevel) {
return TextUtils.expandTemplate(mContext.getText(R.string.battery_header_title_alternate),
NumberFormat.getIntegerInstance().format(batteryLevel));

View File

@@ -44,6 +44,7 @@ public abstract class PowerUsageBase extends DashboardFragment {
protected BatteryStatsHelper mStatsHelper;
protected UserManager mUm;
private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
protected boolean mIsBatteryPresent = true;
@Override
public void onAttach(Activity activity) {
@@ -60,6 +61,9 @@ public abstract class PowerUsageBase extends DashboardFragment {
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext());
mBatteryBroadcastReceiver.setBatteryChangedListener(type -> {
if (type == BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT) {
mIsBatteryPresent = false;
}
restartBatteryStatsLoader(type);
});
}

View File

@@ -56,8 +56,8 @@ import com.android.settingslib.widget.LayoutPreference;
import java.util.List;
/**
* Displays a list of apps and subsystems that consume power, ordered by how much power was
* consumed since the last time it was unplugged.
* Displays a list of apps and subsystems that consume power, ordered by how much power was consumed
* since the last time it was unplugged.
*/
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class PowerUsageSummary extends PowerUsageBase implements OnLongClickListener,
@@ -220,7 +220,9 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
KEY_TIME_SINCE_LAST_FULL_CHARGE);
mBatteryUtils = BatteryUtils.getInstance(getContext());
restartBatteryInfoLoader();
if (Utils.isBatteryPresent(getContext())) {
restartBatteryInfoLoader();
}
mBatteryTipPreferenceController.restoreInstanceState(icicle);
updateBatteryTipFlag(icicle);
}
@@ -287,6 +289,10 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
if (context == null) {
return;
}
// Skip refreshing UI if battery is not present.
if (!mIsBatteryPresent) {
return;
}
// Skip BatteryTipLoader if device is rotated or only battery level change
if (mNeedUpdateBatteryTip
@@ -295,7 +301,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
} else {
mNeedUpdateBatteryTip = true;
}
// reload BatteryInfo and updateUI
restartBatteryInfoLoader();
updateLastFullChargePreference();
@@ -354,6 +359,10 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
if (getContext() == null) {
return;
}
// Skip restartBatteryInfoLoader if battery is not present.
if (!mIsBatteryPresent) {
return;
}
getLoaderManager().restartLoader(BATTERY_INFO_LOADER, Bundle.EMPTY,
mBatteryInfoLoaderCallbacks);
if (mPowerFeatureProvider.isEstimateDebugEnabled()) {
@@ -378,7 +387,10 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
@Override
protected void restartBatteryStatsLoader(@BatteryUpdateType int refreshType) {
super.restartBatteryStatsLoader(refreshType);
mBatteryHeaderPreferenceController.quickUpdateHeaderPreference();
// Update battery header if battery is present.
if (mIsBatteryPresent) {
mBatteryHeaderPreferenceController.quickUpdateHeaderPreference();
}
}
@Override
@@ -392,7 +404,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
restartBatteryTipLoader();
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.power_usage_summary);
}

View File

@@ -19,6 +19,7 @@ package com.android.settings.fuelgauge;
import android.content.Context;
import android.util.FeatureFlagUtils;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@@ -32,6 +33,8 @@ import com.android.settingslib.core.lifecycle.events.OnStop;
public class TopLevelBatteryPreferenceController extends BasePreferenceController implements
LifecycleObserver, OnStart, OnStop {
@VisibleForTesting
boolean mIsBatteryPresent = true;
private final BatteryBroadcastReceiver mBatteryBroadcastReceiver;
private Preference mPreference;
private BatteryInfo mBatteryInfo;
@@ -40,6 +43,9 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle
super(context, preferenceKey);
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
mBatteryBroadcastReceiver.setBatteryChangedListener(type -> {
if (type == BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT) {
mIsBatteryPresent = false;
}
BatteryInfo.getBatteryInfo(mContext, info -> {
mBatteryInfo = info;
updateState(mPreference);
@@ -75,7 +81,10 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle
if (FeatureFlagUtils.isEnabled(mContext, FeatureFlags.SILKY_HOME)) {
return null;
}
// Display help message if battery is not present.
if (!mIsBatteryPresent) {
return mContext.getText(R.string.battery_missing_message);
}
return getDashboardLabel(mContext, mBatteryInfo);
}

View File

@@ -18,18 +18,25 @@ package com.android.settings.display;
import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.testutils.shadow.ShadowUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowUtils.class)
public class BatteryPercentagePreferenceControllerTest {
private static final String PREF_KEY = "battery_percentage";
@@ -43,6 +50,11 @@ public class BatteryPercentagePreferenceControllerTest {
mController = new BatteryPercentagePreferenceController(mContext, PREF_KEY);
}
@After
public void tearDown() {
ShadowUtils.reset();
}
private int getPercentageSetting() {
return Settings.System.getInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT, 0);
}
@@ -60,4 +72,11 @@ public class BatteryPercentagePreferenceControllerTest {
final int isOn = getPercentageSetting();
assertThat(isOn).isEqualTo(0);
}
@Test
public void getAvailabilityStatus_batteryNotPresent_shouldReturnConditionallyUnavailable() {
ShadowUtils.setIsBatteryPresent(false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
}

View File

@@ -90,6 +90,19 @@ public class BatteryBroadcastReceiverTest {
verify(mBatteryListener).onBatteryChanged(BatteryUpdateType.BATTERY_LEVEL);
}
@Test
@Config(shadows = {
BatteryFixSliceTest.ShadowBatteryStatsHelperLoader.class,
BatteryFixSliceTest.ShadowBatteryTipLoader.class
})
public void onReceive_batteryNotPresent_shouldShowHelpMessage() {
mChargingIntent.putExtra(BatteryManager.EXTRA_PRESENT, false);
mBatteryBroadcastReceiver.onReceive(mContext, mChargingIntent);
verify(mBatteryListener).onBatteryChanged(BatteryUpdateType.BATTERY_NOT_PRESENT);
}
@Test
@Config(shadows = {
BatteryFixSliceTest.ShadowBatteryStatsHelperLoader.class,

View File

@@ -44,6 +44,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.LayoutPreference;
@@ -61,7 +62,7 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowPowerManager;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowEntityHeaderController.class)
@Config(shadows = {ShadowEntityHeaderController.class, ShadowUtils.class})
public class BatteryHeaderPreferenceControllerTest {
private static final String PREF_KEY = "battery_header";
@@ -116,7 +117,7 @@ public class BatteryHeaderPreferenceControllerTest {
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
mController = new BatteryHeaderPreferenceController(mContext, PREF_KEY);
mController = spy(new BatteryHeaderPreferenceController(mContext, PREF_KEY));
mLifecycle.addObserver(mController);
mController.setActivity(mActivity);
mController.setFragment(mPreferenceFragment);
@@ -129,6 +130,7 @@ public class BatteryHeaderPreferenceControllerTest {
@After
public void tearDown() {
ShadowEntityHeaderController.reset();
ShadowUtils.reset();
}
@Test
@@ -214,4 +216,13 @@ public class BatteryHeaderPreferenceControllerTest {
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE_UNSEARCHABLE);
}
@Test
public void displayPreference_batteryNotPresent_shouldShowHelpMessage() {
ShadowUtils.setIsBatteryPresent(false);
mController.displayPreference(mPreferenceScreen);
verify(mController).showHelpMessage();
}
}

View File

@@ -45,7 +45,6 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.VisibleForTesting;
import androidx.loader.app.LoaderManager;
import androidx.preference.PreferenceScreen;
@@ -56,6 +55,7 @@ import com.android.settings.SettingsActivity;
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.XmlTestUtils;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settingslib.core.instrumentation.VisibilityLoggerMixin;
import com.android.settingslib.widget.LayoutPreference;
@@ -72,6 +72,7 @@ import org.mockito.stubbing.Answer;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
@@ -224,6 +225,7 @@ public class PowerUsageSummaryTest {
}
@Test
@Config(shadows = ShadowUtils.class)
public void nonIndexableKeys_MatchPreferenceKeys() {
final Context context = RuntimeEnvironment.application;
final List<String> niks =

View File

@@ -25,6 +25,7 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.util.FeatureFlagUtils;
import com.android.settings.R;
import com.android.settings.core.FeatureFlags;
import org.junit.After;
@@ -84,4 +85,13 @@ public class TopLevelBatteryPreferenceControllerTest {
assertThat(mController.getSummary()).isNull();
}
@Test
public void getSummary_batteryNotPresent_shouldShowWarningMessage() {
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.SILKY_HOME, false);
mController.mIsBatteryPresent = false;
assertThat(mController.getSummary())
.isEqualTo(mContext.getString(R.string.battery_missing_message));
}
}

View File

@@ -46,6 +46,7 @@ public class ShadowUtils {
private static boolean sIsSystemAlertWindowEnabled;
private static boolean sIsVoiceCapable;
private static ArraySet<String> sResultLinks = new ArraySet<>();
private static boolean sIsBatteryPresent;
@Implementation
protected static int enforceSameOwner(Context context, int userId) {
@@ -67,6 +68,7 @@ public class ShadowUtils {
sIsDemoUser = false;
sIsVoiceCapable = false;
sResultLinks = new ArraySet<>();
sIsBatteryPresent = true;
}
public static void setIsDemoUser(boolean isDemoUser) {
@@ -155,4 +157,13 @@ public class ShadowUtils {
public static void setHandledDomains(ArraySet<String> links) {
sResultLinks = links;
}
@Implementation
protected static boolean isBatteryPresent(Context context) {
return sIsBatteryPresent;
}
public static void setIsBatteryPresent(boolean isBatteryPresent) {
sIsBatteryPresent = isBatteryPresent;
}
}