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:
@@ -25,6 +25,7 @@
|
|||||||
style="@style/EntityHeader">
|
style="@style/EntityHeader">
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
|
android:id="@+id/battery_info_layout"
|
||||||
android:layout_width="170dp"
|
android:layout_width="170dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginStart="72dp"
|
android:layout_marginStart="72dp"
|
||||||
|
@@ -5778,6 +5778,11 @@
|
|||||||
<!-- Title to display the battery percentage. [CHAR LIMIT=24] -->
|
<!-- 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>
|
<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] -->
|
<!-- Title for force stop dialog [CHAR LIMIT=30] -->
|
||||||
<string name="dialog_stop_title">Stop app?</string>
|
<string name="dialog_stop_title">Stop app?</string>
|
||||||
<!-- Message body for force stop dialog [CHAR LIMIT=NONE] -->
|
<!-- Message body for force stop dialog [CHAR LIMIT=NONE] -->
|
||||||
@@ -7290,6 +7295,8 @@
|
|||||||
<string name="help_uri_sim_combination_warning" translatable="false"></string>
|
<string name="help_uri_sim_combination_warning" translatable="false"></string>
|
||||||
<!-- url for the NFC and payment settings page -->
|
<!-- url for the NFC and payment settings page -->
|
||||||
<string name="help_uri_nfc_and_payment_settings" translatable="false"></string>
|
<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] -->
|
<!-- User account title [CHAR LIMIT=30] -->
|
||||||
<string name="user_account_title">Account for content</string>
|
<string name="user_account_title">Account for content</string>
|
||||||
|
@@ -267,6 +267,15 @@ public final class Utils extends com.android.settingslib.Utils {
|
|||||||
return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
|
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) {
|
public static String getBatteryPercentage(Intent batteryChangedIntent) {
|
||||||
return formatPercentage(getBatteryLevel(batteryChangedIntent));
|
return formatPercentage(getBatteryLevel(batteryChangedIntent));
|
||||||
}
|
}
|
||||||
|
@@ -21,9 +21,11 @@ import android.content.Context;
|
|||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
import androidx.preference.SwitchPreference;
|
import androidx.preference.SwitchPreference;
|
||||||
|
|
||||||
import com.android.internal.R;
|
import com.android.internal.R;
|
||||||
|
import com.android.settings.Utils;
|
||||||
import com.android.settings.core.BasePreferenceController;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
|
|
||||||
@@ -34,12 +36,27 @@ import com.android.settings.core.PreferenceControllerMixin;
|
|||||||
public class BatteryPercentagePreferenceController extends BasePreferenceController implements
|
public class BatteryPercentagePreferenceController extends BasePreferenceController implements
|
||||||
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
|
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
|
||||||
|
|
||||||
|
private Preference mPreference;
|
||||||
|
|
||||||
public BatteryPercentagePreferenceController(Context context, String preferenceKey) {
|
public BatteryPercentagePreferenceController(Context context, String preferenceKey) {
|
||||||
super(context, 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
|
@Override
|
||||||
public int getAvailabilityStatus() {
|
public int getAvailabilityStatus() {
|
||||||
|
if (!Utils.isBatteryPresent(mContext)) {
|
||||||
|
return CONDITIONALLY_UNAVAILABLE;
|
||||||
|
}
|
||||||
return mContext.getResources().getBoolean(
|
return mContext.getResources().getBoolean(
|
||||||
R.bool.config_battery_percentage_setting_available) ? AVAILABLE
|
R.bool.config_battery_percentage_setting_available) ? AVAILABLE
|
||||||
: UNSUPPORTED_ON_DEVICE;
|
: UNSUPPORTED_ON_DEVICE;
|
||||||
|
@@ -21,6 +21,7 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.IntDef;
|
import androidx.annotation.IntDef;
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
@@ -41,6 +42,7 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
*/
|
*/
|
||||||
public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
|
private static final String TAG = "BatteryBroadcastRcvr";
|
||||||
/**
|
/**
|
||||||
* Callback when the following has been changed:
|
* Callback when the following has been changed:
|
||||||
*
|
*
|
||||||
@@ -56,12 +58,14 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
|||||||
@IntDef({BatteryUpdateType.MANUAL,
|
@IntDef({BatteryUpdateType.MANUAL,
|
||||||
BatteryUpdateType.BATTERY_LEVEL,
|
BatteryUpdateType.BATTERY_LEVEL,
|
||||||
BatteryUpdateType.BATTERY_SAVER,
|
BatteryUpdateType.BATTERY_SAVER,
|
||||||
BatteryUpdateType.BATTERY_STATUS})
|
BatteryUpdateType.BATTERY_STATUS,
|
||||||
|
BatteryUpdateType.BATTERY_NOT_PRESENT})
|
||||||
public @interface BatteryUpdateType {
|
public @interface BatteryUpdateType {
|
||||||
int MANUAL = 0;
|
int MANUAL = 0;
|
||||||
int BATTERY_LEVEL = 1;
|
int BATTERY_LEVEL = 1;
|
||||||
int BATTERY_SAVER = 2;
|
int BATTERY_SAVER = 2;
|
||||||
int BATTERY_STATUS = 3;
|
int BATTERY_STATUS = 3;
|
||||||
|
int BATTERY_NOT_PRESENT = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -101,9 +105,11 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
|||||||
if (intent != null && mBatteryListener != null) {
|
if (intent != null && mBatteryListener != null) {
|
||||||
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
|
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
|
||||||
final String batteryLevel = Utils.getBatteryPercentage(intent);
|
final String batteryLevel = Utils.getBatteryPercentage(intent);
|
||||||
final String batteryStatus = Utils.getBatteryStatus(
|
final String batteryStatus = Utils.getBatteryStatus(mContext, intent);
|
||||||
mContext, intent);
|
if (!Utils.isBatteryPresent(intent)) {
|
||||||
if (forceUpdate) {
|
Log.w(TAG, "Problem reading the battery meter.");
|
||||||
|
mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_NOT_PRESENT);
|
||||||
|
} else if (forceUpdate) {
|
||||||
mBatteryListener.onBatteryChanged(BatteryUpdateType.MANUAL);
|
mBatteryListener.onBatteryChanged(BatteryUpdateType.MANUAL);
|
||||||
} else if(!batteryLevel.equals(mBatteryLevel)) {
|
} else if(!batteryLevel.equals(mBatteryLevel)) {
|
||||||
mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_LEVEL);
|
mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_LEVEL);
|
||||||
|
@@ -25,6 +25,10 @@ import android.icu.text.NumberFormat;
|
|||||||
import android.os.BatteryManager;
|
import android.os.BatteryManager;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.text.TextUtils;
|
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 android.widget.TextView;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
@@ -35,7 +39,9 @@ import com.android.settings.R;
|
|||||||
import com.android.settings.core.BasePreferenceController;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
|
import com.android.settings.utils.AnnotationSpan;
|
||||||
import com.android.settings.widget.EntityHeaderController;
|
import com.android.settings.widget.EntityHeaderController;
|
||||||
|
import com.android.settingslib.HelpUtils;
|
||||||
import com.android.settingslib.Utils;
|
import com.android.settingslib.Utils;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||||
@@ -49,6 +55,7 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
|
|||||||
implements PreferenceControllerMixin, LifecycleObserver, OnStart {
|
implements PreferenceControllerMixin, LifecycleObserver, OnStart {
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final String KEY_BATTERY_HEADER = "battery_header";
|
static final String KEY_BATTERY_HEADER = "battery_header";
|
||||||
|
private static final String ANNOTATION_URL = "url";
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
|
BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
|
||||||
@@ -94,7 +101,11 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
|
|||||||
mBatteryPercentText = mBatteryLayoutPref.findViewById(R.id.battery_percent);
|
mBatteryPercentText = mBatteryLayoutPref.findViewById(R.id.battery_percent);
|
||||||
mSummary1 = mBatteryLayoutPref.findViewById(R.id.summary1);
|
mSummary1 = mBatteryLayoutPref.findViewById(R.id.summary1);
|
||||||
|
|
||||||
quickUpdateHeaderPreference();
|
if (com.android.settings.Utils.isBatteryPresent(mContext)) {
|
||||||
|
quickUpdateHeaderPreference();
|
||||||
|
} else {
|
||||||
|
showHelpMessage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -146,6 +157,32 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
|
|||||||
mBatteryPercentText.setText(formatBatteryPercentageText(batteryLevel));
|
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) {
|
private CharSequence formatBatteryPercentageText(int batteryLevel) {
|
||||||
return TextUtils.expandTemplate(mContext.getText(R.string.battery_header_title_alternate),
|
return TextUtils.expandTemplate(mContext.getText(R.string.battery_header_title_alternate),
|
||||||
NumberFormat.getIntegerInstance().format(batteryLevel));
|
NumberFormat.getIntegerInstance().format(batteryLevel));
|
||||||
|
@@ -44,6 +44,7 @@ public abstract class PowerUsageBase extends DashboardFragment {
|
|||||||
protected BatteryStatsHelper mStatsHelper;
|
protected BatteryStatsHelper mStatsHelper;
|
||||||
protected UserManager mUm;
|
protected UserManager mUm;
|
||||||
private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
|
private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
|
||||||
|
protected boolean mIsBatteryPresent = true;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttach(Activity activity) {
|
public void onAttach(Activity activity) {
|
||||||
@@ -60,6 +61,9 @@ public abstract class PowerUsageBase extends DashboardFragment {
|
|||||||
|
|
||||||
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext());
|
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext());
|
||||||
mBatteryBroadcastReceiver.setBatteryChangedListener(type -> {
|
mBatteryBroadcastReceiver.setBatteryChangedListener(type -> {
|
||||||
|
if (type == BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT) {
|
||||||
|
mIsBatteryPresent = false;
|
||||||
|
}
|
||||||
restartBatteryStatsLoader(type);
|
restartBatteryStatsLoader(type);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -56,8 +56,8 @@ import com.android.settingslib.widget.LayoutPreference;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Displays a list of apps and subsystems that consume power, ordered by how much power was
|
* Displays a list of apps and subsystems that consume power, ordered by how much power was consumed
|
||||||
* consumed since the last time it was unplugged.
|
* since the last time it was unplugged.
|
||||||
*/
|
*/
|
||||||
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
|
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
|
||||||
public class PowerUsageSummary extends PowerUsageBase implements OnLongClickListener,
|
public class PowerUsageSummary extends PowerUsageBase implements OnLongClickListener,
|
||||||
@@ -220,7 +220,9 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
|
|||||||
KEY_TIME_SINCE_LAST_FULL_CHARGE);
|
KEY_TIME_SINCE_LAST_FULL_CHARGE);
|
||||||
mBatteryUtils = BatteryUtils.getInstance(getContext());
|
mBatteryUtils = BatteryUtils.getInstance(getContext());
|
||||||
|
|
||||||
restartBatteryInfoLoader();
|
if (Utils.isBatteryPresent(getContext())) {
|
||||||
|
restartBatteryInfoLoader();
|
||||||
|
}
|
||||||
mBatteryTipPreferenceController.restoreInstanceState(icicle);
|
mBatteryTipPreferenceController.restoreInstanceState(icicle);
|
||||||
updateBatteryTipFlag(icicle);
|
updateBatteryTipFlag(icicle);
|
||||||
}
|
}
|
||||||
@@ -287,6 +289,10 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
|
|||||||
if (context == null) {
|
if (context == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Skip refreshing UI if battery is not present.
|
||||||
|
if (!mIsBatteryPresent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Skip BatteryTipLoader if device is rotated or only battery level change
|
// Skip BatteryTipLoader if device is rotated or only battery level change
|
||||||
if (mNeedUpdateBatteryTip
|
if (mNeedUpdateBatteryTip
|
||||||
@@ -295,7 +301,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
|
|||||||
} else {
|
} else {
|
||||||
mNeedUpdateBatteryTip = true;
|
mNeedUpdateBatteryTip = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// reload BatteryInfo and updateUI
|
// reload BatteryInfo and updateUI
|
||||||
restartBatteryInfoLoader();
|
restartBatteryInfoLoader();
|
||||||
updateLastFullChargePreference();
|
updateLastFullChargePreference();
|
||||||
@@ -354,6 +359,10 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
|
|||||||
if (getContext() == null) {
|
if (getContext() == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Skip restartBatteryInfoLoader if battery is not present.
|
||||||
|
if (!mIsBatteryPresent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
getLoaderManager().restartLoader(BATTERY_INFO_LOADER, Bundle.EMPTY,
|
getLoaderManager().restartLoader(BATTERY_INFO_LOADER, Bundle.EMPTY,
|
||||||
mBatteryInfoLoaderCallbacks);
|
mBatteryInfoLoaderCallbacks);
|
||||||
if (mPowerFeatureProvider.isEstimateDebugEnabled()) {
|
if (mPowerFeatureProvider.isEstimateDebugEnabled()) {
|
||||||
@@ -378,7 +387,10 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
|
|||||||
@Override
|
@Override
|
||||||
protected void restartBatteryStatsLoader(@BatteryUpdateType int refreshType) {
|
protected void restartBatteryStatsLoader(@BatteryUpdateType int refreshType) {
|
||||||
super.restartBatteryStatsLoader(refreshType);
|
super.restartBatteryStatsLoader(refreshType);
|
||||||
mBatteryHeaderPreferenceController.quickUpdateHeaderPreference();
|
// Update battery header if battery is present.
|
||||||
|
if (mIsBatteryPresent) {
|
||||||
|
mBatteryHeaderPreferenceController.quickUpdateHeaderPreference();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -392,7 +404,6 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
|
|||||||
restartBatteryTipLoader();
|
restartBatteryTipLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||||
new BaseSearchIndexProvider(R.xml.power_usage_summary);
|
new BaseSearchIndexProvider(R.xml.power_usage_summary);
|
||||||
}
|
}
|
||||||
|
@@ -19,6 +19,7 @@ package com.android.settings.fuelgauge;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.FeatureFlagUtils;
|
import android.util.FeatureFlagUtils;
|
||||||
|
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
@@ -32,6 +33,8 @@ import com.android.settingslib.core.lifecycle.events.OnStop;
|
|||||||
public class TopLevelBatteryPreferenceController extends BasePreferenceController implements
|
public class TopLevelBatteryPreferenceController extends BasePreferenceController implements
|
||||||
LifecycleObserver, OnStart, OnStop {
|
LifecycleObserver, OnStart, OnStop {
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
boolean mIsBatteryPresent = true;
|
||||||
private final BatteryBroadcastReceiver mBatteryBroadcastReceiver;
|
private final BatteryBroadcastReceiver mBatteryBroadcastReceiver;
|
||||||
private Preference mPreference;
|
private Preference mPreference;
|
||||||
private BatteryInfo mBatteryInfo;
|
private BatteryInfo mBatteryInfo;
|
||||||
@@ -40,6 +43,9 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle
|
|||||||
super(context, preferenceKey);
|
super(context, preferenceKey);
|
||||||
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
|
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
|
||||||
mBatteryBroadcastReceiver.setBatteryChangedListener(type -> {
|
mBatteryBroadcastReceiver.setBatteryChangedListener(type -> {
|
||||||
|
if (type == BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT) {
|
||||||
|
mIsBatteryPresent = false;
|
||||||
|
}
|
||||||
BatteryInfo.getBatteryInfo(mContext, info -> {
|
BatteryInfo.getBatteryInfo(mContext, info -> {
|
||||||
mBatteryInfo = info;
|
mBatteryInfo = info;
|
||||||
updateState(mPreference);
|
updateState(mPreference);
|
||||||
@@ -75,7 +81,10 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle
|
|||||||
if (FeatureFlagUtils.isEnabled(mContext, FeatureFlags.SILKY_HOME)) {
|
if (FeatureFlagUtils.isEnabled(mContext, FeatureFlags.SILKY_HOME)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
// Display help message if battery is not present.
|
||||||
|
if (!mIsBatteryPresent) {
|
||||||
|
return mContext.getText(R.string.battery_missing_message);
|
||||||
|
}
|
||||||
return getDashboardLabel(mContext, mBatteryInfo);
|
return getDashboardLabel(mContext, mBatteryInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,18 +18,25 @@ package com.android.settings.display;
|
|||||||
|
|
||||||
import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
|
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 static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
|
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(shadows = ShadowUtils.class)
|
||||||
public class BatteryPercentagePreferenceControllerTest {
|
public class BatteryPercentagePreferenceControllerTest {
|
||||||
|
|
||||||
private static final String PREF_KEY = "battery_percentage";
|
private static final String PREF_KEY = "battery_percentage";
|
||||||
@@ -43,6 +50,11 @@ public class BatteryPercentagePreferenceControllerTest {
|
|||||||
mController = new BatteryPercentagePreferenceController(mContext, PREF_KEY);
|
mController = new BatteryPercentagePreferenceController(mContext, PREF_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
ShadowUtils.reset();
|
||||||
|
}
|
||||||
|
|
||||||
private int getPercentageSetting() {
|
private int getPercentageSetting() {
|
||||||
return Settings.System.getInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT, 0);
|
return Settings.System.getInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT, 0);
|
||||||
}
|
}
|
||||||
@@ -60,4 +72,11 @@ public class BatteryPercentagePreferenceControllerTest {
|
|||||||
final int isOn = getPercentageSetting();
|
final int isOn = getPercentageSetting();
|
||||||
assertThat(isOn).isEqualTo(0);
|
assertThat(isOn).isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAvailabilityStatus_batteryNotPresent_shouldReturnConditionallyUnavailable() {
|
||||||
|
ShadowUtils.setIsBatteryPresent(false);
|
||||||
|
|
||||||
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -90,6 +90,19 @@ public class BatteryBroadcastReceiverTest {
|
|||||||
verify(mBatteryListener).onBatteryChanged(BatteryUpdateType.BATTERY_LEVEL);
|
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
|
@Test
|
||||||
@Config(shadows = {
|
@Config(shadows = {
|
||||||
BatteryFixSliceTest.ShadowBatteryStatsHelperLoader.class,
|
BatteryFixSliceTest.ShadowBatteryStatsHelperLoader.class,
|
||||||
|
@@ -44,6 +44,7 @@ import androidx.recyclerview.widget.RecyclerView;
|
|||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.core.BasePreferenceController;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
|
import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||||
import com.android.settings.widget.EntityHeaderController;
|
import com.android.settings.widget.EntityHeaderController;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
import com.android.settingslib.widget.LayoutPreference;
|
import com.android.settingslib.widget.LayoutPreference;
|
||||||
@@ -61,7 +62,7 @@ import org.robolectric.annotation.Config;
|
|||||||
import org.robolectric.shadows.ShadowPowerManager;
|
import org.robolectric.shadows.ShadowPowerManager;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(shadows = ShadowEntityHeaderController.class)
|
@Config(shadows = {ShadowEntityHeaderController.class, ShadowUtils.class})
|
||||||
public class BatteryHeaderPreferenceControllerTest {
|
public class BatteryHeaderPreferenceControllerTest {
|
||||||
|
|
||||||
private static final String PREF_KEY = "battery_header";
|
private static final String PREF_KEY = "battery_header";
|
||||||
@@ -116,7 +117,7 @@ public class BatteryHeaderPreferenceControllerTest {
|
|||||||
|
|
||||||
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
|
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
|
||||||
|
|
||||||
mController = new BatteryHeaderPreferenceController(mContext, PREF_KEY);
|
mController = spy(new BatteryHeaderPreferenceController(mContext, PREF_KEY));
|
||||||
mLifecycle.addObserver(mController);
|
mLifecycle.addObserver(mController);
|
||||||
mController.setActivity(mActivity);
|
mController.setActivity(mActivity);
|
||||||
mController.setFragment(mPreferenceFragment);
|
mController.setFragment(mPreferenceFragment);
|
||||||
@@ -129,6 +130,7 @@ public class BatteryHeaderPreferenceControllerTest {
|
|||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
ShadowEntityHeaderController.reset();
|
ShadowEntityHeaderController.reset();
|
||||||
|
ShadowUtils.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -214,4 +216,13 @@ public class BatteryHeaderPreferenceControllerTest {
|
|||||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(
|
||||||
BasePreferenceController.AVAILABLE_UNSEARCHABLE);
|
BasePreferenceController.AVAILABLE_UNSEARCHABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void displayPreference_batteryNotPresent_shouldShowHelpMessage() {
|
||||||
|
ShadowUtils.setIsBatteryPresent(false);
|
||||||
|
|
||||||
|
mController.displayPreference(mPreferenceScreen);
|
||||||
|
|
||||||
|
verify(mController).showHelpMessage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -45,7 +45,6 @@ import android.view.MenuItem;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
|
||||||
import androidx.loader.app.LoaderManager;
|
import androidx.loader.app.LoaderManager;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
@@ -56,6 +55,7 @@ import com.android.settings.SettingsActivity;
|
|||||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
|
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
|
||||||
import com.android.settings.testutils.FakeFeatureFactory;
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
import com.android.settings.testutils.XmlTestUtils;
|
import com.android.settings.testutils.XmlTestUtils;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||||
import com.android.settingslib.core.instrumentation.VisibilityLoggerMixin;
|
import com.android.settingslib.core.instrumentation.VisibilityLoggerMixin;
|
||||||
import com.android.settingslib.widget.LayoutPreference;
|
import com.android.settingslib.widget.LayoutPreference;
|
||||||
|
|
||||||
@@ -72,6 +72,7 @@ import org.mockito.stubbing.Answer;
|
|||||||
import org.robolectric.Robolectric;
|
import org.robolectric.Robolectric;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -224,6 +225,7 @@ public class PowerUsageSummaryTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Config(shadows = ShadowUtils.class)
|
||||||
public void nonIndexableKeys_MatchPreferenceKeys() {
|
public void nonIndexableKeys_MatchPreferenceKeys() {
|
||||||
final Context context = RuntimeEnvironment.application;
|
final Context context = RuntimeEnvironment.application;
|
||||||
final List<String> niks =
|
final List<String> niks =
|
||||||
|
@@ -25,6 +25,7 @@ import static com.google.common.truth.Truth.assertThat;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.FeatureFlagUtils;
|
import android.util.FeatureFlagUtils;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
import com.android.settings.core.FeatureFlags;
|
import com.android.settings.core.FeatureFlags;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@@ -84,4 +85,13 @@ public class TopLevelBatteryPreferenceControllerTest {
|
|||||||
|
|
||||||
assertThat(mController.getSummary()).isNull();
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -46,6 +46,7 @@ public class ShadowUtils {
|
|||||||
private static boolean sIsSystemAlertWindowEnabled;
|
private static boolean sIsSystemAlertWindowEnabled;
|
||||||
private static boolean sIsVoiceCapable;
|
private static boolean sIsVoiceCapable;
|
||||||
private static ArraySet<String> sResultLinks = new ArraySet<>();
|
private static ArraySet<String> sResultLinks = new ArraySet<>();
|
||||||
|
private static boolean sIsBatteryPresent;
|
||||||
|
|
||||||
@Implementation
|
@Implementation
|
||||||
protected static int enforceSameOwner(Context context, int userId) {
|
protected static int enforceSameOwner(Context context, int userId) {
|
||||||
@@ -67,6 +68,7 @@ public class ShadowUtils {
|
|||||||
sIsDemoUser = false;
|
sIsDemoUser = false;
|
||||||
sIsVoiceCapable = false;
|
sIsVoiceCapable = false;
|
||||||
sResultLinks = new ArraySet<>();
|
sResultLinks = new ArraySet<>();
|
||||||
|
sIsBatteryPresent = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setIsDemoUser(boolean isDemoUser) {
|
public static void setIsDemoUser(boolean isDemoUser) {
|
||||||
@@ -155,4 +157,13 @@ public class ShadowUtils {
|
|||||||
public static void setHandledDomains(ArraySet<String> links) {
|
public static void setHandledDomains(ArraySet<String> links) {
|
||||||
sResultLinks = links;
|
sResultLinks = links;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
protected static boolean isBatteryPresent(Context context) {
|
||||||
|
return sIsBatteryPresent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setIsBatteryPresent(boolean isBatteryPresent) {
|
||||||
|
sIsBatteryPresent = isBatteryPresent;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user