Snap for 12722466 from 79e1918413 to 25Q1-release

Change-Id: Icd67a9b165afd0cde59f7f7441fb09e840486948
This commit is contained in:
Android Build Coastguard Worker
2024-11-28 00:17:13 +00:00
26 changed files with 684 additions and 713 deletions

View File

@@ -134,6 +134,7 @@ android_library {
"aconfig_settings_flags",
"android.app.flags-aconfig",
"android.provider.flags-aconfig",
"android.security.flags-aconfig",
],
}

View File

@@ -1127,6 +1127,38 @@
android:value="true"/>
</activity>
<activity
android:name=".Settings$TemperatureUnitSettingsActivity"
android:label="@string/temperature_preferences_title"
android:exported="true" >
<intent-filter android:priority="1">
<action android:name="android.settings.TEMPERATURE_UNIT_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.regionalpreferences.TemperatureUnitFragment"/>
<meta-data android:name="com.android.settings.HIGHLIGHT_MENU_KEY"
android:value="@string/menu_key_system"/>
<meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
android:value="true"/>
</activity>
<activity
android:name=".Settings$FirstDayOfWeekSettingsActivity"
android:label="@string/first_day_of_week_preferences_title"
android:exported="true" >
<intent-filter android:priority="1">
<action android:name="android.settings.FIRST_DAY_OF_WEEK_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.regionalpreferences.FirstDayOfWeekItemFragment"/>
<meta-data android:name="com.android.settings.HIGHLIGHT_MENU_KEY"
android:value="@string/menu_key_system"/>
<meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
android:value="true"/>
</activity>
<activity
android:name=".Settings$KeyboardSettingsActivity"
android:label="@string/keyboard_settings"

View File

@@ -19,3 +19,10 @@ flag {
description: "Shows hdr/sdr dev opton on the development options page from aconfig"
bug: "291863102"
}
flag {
name: "page_size_app_compat_setting"
namespace: "devoptions_settings"
description: "Flag to enable page size app compat mode from Settings."
bug: "371049373"
}

View File

@@ -0,0 +1,32 @@
<!--
~ Copyright (C) 2024 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.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="?android:attr/listPreferredItemPaddingStart"
android:layout_marginEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingBottom="16dp">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAppearance="@*android:style/TextAppearance.DeviceDefault.Body1"
android:textSize="14sp"/>
</LinearLayout>

View File

@@ -12785,6 +12785,9 @@
<string name="page_agnostic_notification_channel_name">16KB Page-agnostic Mode</string>
<string name="page_agnostic_notification_action">Read more</string>
<string name= "enable_16k_app_compat_title">Run app with page size compat mode</string>
<string name= "enable_16k_app_compat_details">App will be run in page size compatibility mode on 16 KB device toggled.</string>
<!-- DSU Loader. Do not translate. -->
<string name="dsu_loader_title" translatable="false">DSU Loader</string>

View File

@@ -25,8 +25,15 @@
android:key="battery_header"
android:title="@string/summary_placeholder"
android:selectable="false"
android:paddingBottom="0px"
android:persistent="false"
settings:controller="com.android.settings.fuelgauge.BatteryHeaderPreferenceController" />
<com.android.settings.fuelgauge.BatteryHeaderTextPreference
android:key="battery_header_text"
android:selectable="false"
settings:controller="com.android.settings.fuelgauge.BatteryHeaderTextPreferenceController" />
<com.android.settingslib.widget.LayoutPreference
android:key="battery_help_message"
android:layout="@layout/preference_battery_error"

View File

@@ -98,6 +98,8 @@ public class Settings extends SettingsActivity {
public static class LanguageSettingsActivity extends SettingsActivity { /* empty */ }
/** Activity for the regional preferences settings. */
public static class RegionalPreferencesActivity extends SettingsActivity { /* empty */ }
public static class TemperatureUnitSettingsActivity extends SettingsActivity { /* empty */ }
public static class FirstDayOfWeekSettingsActivity extends SettingsActivity { /* empty */ }
public static class KeyboardSettingsActivity extends SettingsActivity { /* empty */ }
/** Activity for the navigation mode settings. */
public static class NavigationModeSettingsActivity extends SettingsActivity { /* empty */ }

View File

@@ -17,6 +17,8 @@ package com.android.settings.applications.appinfo;
import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
import static android.os.UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES;
import static android.os.UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY;
import android.app.AppOpsManager;
import android.app.settings.SettingsEnums;
@@ -34,6 +36,7 @@ import com.android.settings.Settings;
import com.android.settings.applications.AppInfoWithHeader;
import com.android.settings.applications.AppStateInstallAppsBridge;
import com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
@@ -82,15 +85,34 @@ public class ExternalSourcesDetails extends AppInfoWithHeader
public static CharSequence getPreferenceSummary(Context context, AppEntry entry) {
final UserHandle userHandle = UserHandle.getUserHandleForUid(entry.info.uid);
final UserManager um = UserManager.get(context);
final int userRestrictionSource = um.getUserRestrictionSource(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, userHandle)
| um.getUserRestrictionSource(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
userHandle);
if ((userRestrictionSource & UserManager.RESTRICTION_SOURCE_SYSTEM) != 0) {
return context.getString(com.android.settingslib.widget.restricted.R.string.disabled_by_admin);
} else if (userRestrictionSource != 0) {
return context.getString(com.android.settingslib.R.string.disabled);
if (android.security.Flags.aapmFeatureDisableInstallUnknownSources()) {
if (um.hasBaseUserRestriction(DISALLOW_INSTALL_UNKNOWN_SOURCES, userHandle)) {
return context.getString(com.android.settingslib.R.string.disabled);
} else if (um.hasUserRestrictionForUser(DISALLOW_INSTALL_UNKNOWN_SOURCES, userHandle)) {
return context.getString(
com.android.settingslib.widget.restricted.R.string.disabled_by_admin);
} else if (um.hasUserRestrictionForUser(DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,
userHandle)) {
if (RestrictedLockUtilsInternal.isPolicyEnforcedByAdvancedProtection(context,
DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, userHandle.getIdentifier())) {
return context.getString(com.android.settingslib.widget.restricted
.R.string.disabled_by_advanced_protection);
} else {
return context.getString(
com.android.settingslib.widget.restricted.R.string.disabled_by_admin);
}
}
} else {
final int userRestrictionSource = um.getUserRestrictionSource(
DISALLOW_INSTALL_UNKNOWN_SOURCES, userHandle)
| um.getUserRestrictionSource(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, userHandle);
if ((userRestrictionSource & UserManager.RESTRICTION_SOURCE_SYSTEM) != 0) {
return context.getString(
com.android.settingslib.widget.restricted.R.string.disabled_by_admin);
} else if (userRestrictionSource != 0) {
return context.getString(com.android.settingslib.R.string.disabled);
}
}
final InstallAppsState appsState = new AppStateInstallAppsBridge(context, null, null)
.createInstallAppsStateFor(entry.info.packageName, entry.info.uid);
@@ -110,14 +132,14 @@ public class ExternalSourcesDetails extends AppInfoWithHeader
if (mPackageInfo == null || mPackageInfo.applicationInfo == null) {
return false;
}
if (mUserManager.hasBaseUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
if (mUserManager.hasBaseUserRestriction(DISALLOW_INSTALL_UNKNOWN_SOURCES,
UserHandle.of(UserHandle.myUserId()))) {
mSwitchPref.setChecked(false);
mSwitchPref.setSummary(com.android.settingslib.R.string.disabled);
mSwitchPref.setEnabled(false);
return true;
}
mSwitchPref.checkRestrictionAndSetDisabled(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES);
mSwitchPref.checkRestrictionAndSetDisabled(DISALLOW_INSTALL_UNKNOWN_SOURCES);
if (!mSwitchPref.isDisabledByAdmin()) {
mSwitchPref.checkRestrictionAndSetDisabled(
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY);

View File

@@ -187,7 +187,6 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
TAG,
"Skip handleOnBroadcastReady: null assistant or "
+ "sink has active local source.");
cleanUpStatesForStartSharing();
return;
}
handleOnBroadcastReady();

View File

@@ -182,7 +182,9 @@ import com.android.settings.privacy.PrivacyDashboardFragment;
import com.android.settings.privatespace.delete.PrivateSpaceDeleteFragment;
import com.android.settings.privatespace.delete.PrivateSpaceDeletionProgressFragment;
import com.android.settings.privatespace.onelock.PrivateSpaceBiometricSettings;
import com.android.settings.regionalpreferences.FirstDayOfWeekItemFragment;
import com.android.settings.regionalpreferences.RegionalPreferencesEntriesFragment;
import com.android.settings.regionalpreferences.TemperatureUnitFragment;
import com.android.settings.safetycenter.MoreSecurityPrivacyFragment;
import com.android.settings.security.LockscreenDashboardFragment;
import com.android.settings.security.MemtagPage;
@@ -395,6 +397,8 @@ public class SettingsGateway {
ColorContrastFragment.class.getName(),
LongBackgroundTasksDetails.class.getName(),
RegionalPreferencesEntriesFragment.class.getName(),
TemperatureUnitFragment.class.getName(),
FirstDayOfWeekItemFragment.class.getName(),
BatteryInfoFragment.class.getName(),
UserAspectRatioDetails.class.getName(),
ScreenTimeoutSettings.class.getName(),

View File

@@ -16,12 +16,18 @@
package com.android.settings.enterprise;
import static android.security.advancedprotection.AdvancedProtectionManager.ADVANCED_PROTECTION_SYSTEM_ENTITY;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.app.admin.EnforcingAdmin;
import android.app.admin.UnknownAuthority;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.security.advancedprotection.AdvancedProtectionManager;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
@@ -53,37 +59,67 @@ public class ActionDisabledByAdminDialog extends Activity
@androidx.annotation.VisibleForTesting
EnforcedAdmin getAdminDetailsFromIntent(Intent intent) {
final EnforcedAdmin admin = new EnforcedAdmin(null, UserHandle.of(UserHandle.myUserId()));
final EnforcedAdmin enforcedAdmin = new EnforcedAdmin(null, UserHandle.of(
UserHandle.myUserId()));
if (intent == null) {
return admin;
return enforcedAdmin;
}
admin.component = intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN);
enforcedAdmin.component = intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
ComponentName.class);
int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
Bundle adminDetails = null;
if (admin.component == null) {
DevicePolicyManager devicePolicyManager = getSystemService(DevicePolicyManager.class);
adminDetails = devicePolicyManager.getEnforcingAdminAndUserDetails(userId,
getRestrictionFromIntent(intent));
if (adminDetails != null) {
admin.component = adminDetails.getParcelable(
DevicePolicyManager.EXTRA_DEVICE_ADMIN);
if (enforcedAdmin.component == null) {
DevicePolicyManager dpm = getSystemService(DevicePolicyManager.class);
final String restriction = getRestrictionFromIntent(intent);
if (android.security.Flags.aapmApi() && dpm != null && restriction != null) {
// TODO(b/381025131): Move advanced protection logic to DevicePolicyManager or
// elsewhere.
launchAdvancedProtectionDialogOrTryToSetAdminComponent(dpm, userId, restriction,
enforcedAdmin);
} else {
adminDetails = dpm.getEnforcingAdminAndUserDetails(userId, restriction);
if (adminDetails != null) {
enforcedAdmin.component = adminDetails.getParcelable(
DevicePolicyManager.EXTRA_DEVICE_ADMIN, ComponentName.class);
}
}
}
if (intent.hasExtra(Intent.EXTRA_USER)) {
admin.user = intent.getParcelableExtra(Intent.EXTRA_USER);
enforcedAdmin.user = intent.getParcelableExtra(Intent.EXTRA_USER, UserHandle.class);
} else {
if (adminDetails != null) {
userId = adminDetails.getInt(Intent.EXTRA_USER_ID, UserHandle.myUserId());
}
if (userId == UserHandle.USER_NULL) {
admin.user = null;
enforcedAdmin.user = null;
} else {
admin.user = UserHandle.of(userId);
enforcedAdmin.user = UserHandle.of(userId);
}
}
return admin;
return enforcedAdmin;
}
private void launchAdvancedProtectionDialogOrTryToSetAdminComponent(DevicePolicyManager dpm,
int userId, String restriction, EnforcedAdmin enforcedAdmin) {
EnforcingAdmin enforcingAdmin = dpm.getEnforcingAdmin(userId, restriction);
if (enforcingAdmin == null) {
return;
}
if (enforcingAdmin.getAuthority() instanceof UnknownAuthority authority
&& ADVANCED_PROTECTION_SYSTEM_ENTITY.equals(authority.getName())) {
AdvancedProtectionManager apm = getSystemService(AdvancedProtectionManager.class);
if (apm == null) {
return;
}
Intent apmSupportIntent = apm.createSupportIntentForPolicyIdentifierOrRestriction(
restriction, /* type */ null);
startActivityAsUser(apmSupportIntent, UserHandle.of(userId));
finish();
} else {
enforcedAdmin.component = enforcingAdmin.getComponentName();
}
}
@androidx.annotation.VisibleForTesting

View File

@@ -0,0 +1,95 @@
/*
* Copyright (C) 2024 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
import android.content.Context
import androidx.annotation.VisibleForTesting
import androidx.preference.Preference
import com.android.settings.R
import com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT
import com.android.settingslib.Utils
import com.android.settingslib.fuelgauge.BatteryUtils
import com.android.settingslib.metadata.PreferenceLifecycleContext
import com.android.settingslib.metadata.PreferenceLifecycleProvider
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.preference.PreferenceBinding
import com.android.settingslib.widget.UsageProgressBarPreference
// LINT.IfChange
class BatteryHeaderPreference :
PreferenceMetadata,
PreferenceBinding,
PreferenceLifecycleProvider {
@VisibleForTesting
var batteryBroadcastReceiver: BatteryBroadcastReceiver? = null
override val key: String
get() = KEY
override val title: Int
get() = R.string.summary_placeholder
override fun createWidget(context: Context) = UsageProgressBarPreference(context)
override fun bind(preference: Preference, metadata: PreferenceMetadata) {
super.bind(preference, metadata)
preference.isSelectable = false
if (preference is UsageProgressBarPreference) {
quickUpdateHeaderPreference(preference)
}
}
override fun isIndexable(context: Context) = false
override fun onCreate(context: PreferenceLifecycleContext) {
super.onCreate(context)
batteryBroadcastReceiver = BatteryBroadcastReceiver(context).apply {
setBatteryChangedListener {
if (it != BATTERY_NOT_PRESENT) {
context.notifyPreferenceChange(KEY)
}
}
}
}
override fun onStart(context: PreferenceLifecycleContext) {
super.onStart(context)
batteryBroadcastReceiver?.register();
}
override fun onStop(context: PreferenceLifecycleContext) {
super.onStop(context)
batteryBroadcastReceiver?.unRegister();
}
companion object {
private const val KEY = "battery_header"
private const val BATTERY_MAX_LEVEL: Long = 100L
private fun quickUpdateHeaderPreference(preference: UsageProgressBarPreference) {
val batteryIntent = BatteryUtils.getBatteryIntent(preference.context) ?: return
val batteryLevel: Int = Utils.getBatteryLevel(batteryIntent)
preference.apply {
setUsageSummary(com.android.settings.Utils.formatPercentage(batteryLevel))
setPercent(batteryLevel.toLong(), BATTERY_MAX_LEVEL)
setBottomSummary("")
}
}
}
}
// LINT.ThenChange(BatteryHeaderPreferenceController.java)

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2024 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
@@ -15,59 +16,77 @@
package com.android.settings.fuelgauge;
import static com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.Utils;
import com.android.settingslib.widget.UsageProgressBarPreference;
// LINT.IfChange
/** Controller that update the battery header view */
public class BatteryHeaderPreferenceController extends BasePreferenceController
implements PreferenceControllerMixin, BatteryPreferenceController {
implements PreferenceControllerMixin, LifecycleEventObserver {
private static final String TAG = "BatteryHeaderPreferenceController";
@VisibleForTesting static final String KEY_BATTERY_HEADER = "battery_header";
private static final int BATTERY_MAX_LEVEL = 100;
@VisibleForTesting BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
@VisibleForTesting UsageProgressBarPreference mBatteryUsageProgressBarPref;
private final PowerManager mPowerManager;
private final BatterySettingsFeatureProvider mBatterySettingsFeatureProvider;
private BatteryTip mBatteryTip;
@Nullable @VisibleForTesting BatteryBroadcastReceiver mBatteryBroadcastReceiver;
@Nullable @VisibleForTesting UsageProgressBarPreference mBatteryUsageProgressBarPreference;
public BatteryHeaderPreferenceController(Context context, String key) {
super(context, key);
mPowerManager = context.getSystemService(PowerManager.class);
mBatteryStatusFeatureProvider =
FeatureFactory.getFeatureFactory().getBatteryStatusFeatureProvider();
mBatterySettingsFeatureProvider =
FeatureFactory.getFeatureFactory().getBatterySettingsFeatureProvider();
}
@Override
public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner,
@NonNull Lifecycle.Event event) {
switch (event) {
case ON_CREATE:
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
mBatteryBroadcastReceiver.setBatteryChangedListener(
type -> {
if (type != BATTERY_NOT_PRESENT) {
quickUpdateHeaderPreference();
}
});
break;
case ON_START:
if (mBatteryBroadcastReceiver != null) {
mBatteryBroadcastReceiver.register();
}
break;
case ON_STOP:
if (mBatteryBroadcastReceiver != null) {
mBatteryBroadcastReceiver.unRegister();
}
break;
default:
break;
}
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mBatteryUsageProgressBarPref = screen.findPreference(getPreferenceKey());
// Set up empty space text first to prevent layout flaky before info loaded.
mBatteryUsageProgressBarPref.setBottomSummary(" ");
mBatteryUsageProgressBarPreference = screen.findPreference(getPreferenceKey());
// Hide the bottom summary from the progress bar.
mBatteryUsageProgressBarPreference.setBottomSummary("");
if (com.android.settings.Utils.isBatteryPresent(mContext)) {
quickUpdateHeaderPreference();
} else {
mBatteryUsageProgressBarPref.setVisible(false);
mBatteryUsageProgressBarPreference.setVisible(false);
}
}
@@ -76,105 +95,23 @@ public class BatteryHeaderPreferenceController extends BasePreferenceController
return AVAILABLE_UNSEARCHABLE;
}
private CharSequence generateLabel(BatteryInfo info) {
if (Utils.containsIncompatibleChargers(mContext, TAG)) {
return mContext.getString(
com.android.settingslib.R.string.battery_info_status_not_charging);
}
if (BatteryUtils.isBatteryDefenderOn(info)
|| FeatureFactory.getFeatureFactory()
.getPowerUsageFeatureProvider()
.isExtraDefend()) {
return mContext.getString(
com.android.settingslib.R.string.battery_info_status_charging_on_hold);
}
if (info.remainingLabel != null
&& mBatterySettingsFeatureProvider.isChargingOptimizationMode(mContext)) {
return info.remainingLabel;
}
if (info.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
return info.statusLabel;
}
if (info.pluggedStatus == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
final CharSequence wirelessChargingLabel =
mBatterySettingsFeatureProvider.getWirelessChargingLabel(mContext, info);
if (wirelessChargingLabel != null) {
mBatteryUsageProgressBarPref.setBottomSummaryContentDescription(
mBatterySettingsFeatureProvider
.getWirelessChargingContentDescription(mContext, info));
return wirelessChargingLabel;
}
}
if (info.remainingLabel == null) {
return info.statusLabel;
}
if (info.statusLabel != null && !info.discharging) {
// Charging state
if (com.android.settingslib.fuelgauge.BatteryUtils.isChargingStringV2Enabled()) {
return info.isFastCharging
? mContext.getString(
R.string.battery_state_and_duration,
info.statusLabel,
info.remainingLabel)
: info.remainingLabel;
}
return mContext.getString(
R.string.battery_state_and_duration, info.statusLabel, info.remainingLabel);
} else if (mPowerManager.isPowerSaveMode()) {
// Power save mode is on
final String powerSaverOn =
mContext.getString(R.string.battery_tip_early_heads_up_done_title);
return mContext.getString(
R.string.battery_state_and_duration, powerSaverOn, info.remainingLabel);
} else if (mBatteryTip != null && mBatteryTip.getType() == BatteryTip.TipType.LOW_BATTERY) {
// Low battery state
final String lowBattery = mContext.getString(R.string.low_battery_summary);
return mContext.getString(
R.string.battery_state_and_duration, lowBattery, info.remainingLabel);
} else {
// Discharging state
return info.remainingLabel;
}
}
public void updateHeaderPreference(BatteryInfo info) {
if (!mBatteryStatusFeatureProvider.triggerBatteryStatusUpdate(this, info)) {
mBatteryUsageProgressBarPref.setBottomSummary(generateLabel(info));
}
mBatteryUsageProgressBarPref.setUsageSummary(
formatBatteryPercentageText(info.batteryLevel));
mBatteryUsageProgressBarPref.setPercent(info.batteryLevel, BATTERY_MAX_LEVEL);
}
/** Callback which receives text for the summary line. */
public void updateBatteryStatus(String label, BatteryInfo info) {
final CharSequence summary = label != null ? label : generateLabel(info);
mBatteryUsageProgressBarPref.setBottomSummary(summary);
Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary);
}
/** Updates {@link UsageProgressBarPreference} information. */
public void quickUpdateHeaderPreference() {
if (mBatteryUsageProgressBarPreference == null) {
return;
}
Intent batteryBroadcast =
com.android.settingslib.fuelgauge.BatteryUtils.getBatteryIntent(mContext);
final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
final boolean discharging =
batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
mBatteryUsageProgressBarPref.setUsageSummary(formatBatteryPercentageText(batteryLevel));
mBatteryUsageProgressBarPref.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
}
/** Update summary when battery tips changed. */
public void updateHeaderByBatteryTips(BatteryTip batteryTip, BatteryInfo batteryInfo) {
mBatteryTip = batteryTip;
if (mBatteryTip != null && batteryInfo != null) {
updateHeaderPreference(batteryInfo);
}
mBatteryUsageProgressBarPreference.setUsageSummary(
formatBatteryPercentageText(batteryLevel));
mBatteryUsageProgressBarPreference.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
}
private CharSequence formatBatteryPercentageText(int batteryLevel) {
return com.android.settings.Utils.formatPercentage(batteryLevel);
}
}
// LINT.ThenChange(BatteryHeaderPreference.kt)

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2024 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;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
/** A preference for battery header text. */
public class BatteryHeaderTextPreference extends Preference {
private static final String TAG = "BatteryHeaderTextPreference";
@Nullable private CharSequence mText;
@Nullable private CharSequence mContentDescription;
public BatteryHeaderTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setLayoutResource(R.layout.preference_battery_header_text);
}
@Override
public void onBindViewHolder(PreferenceViewHolder view) {
final TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText(mText);
if (!TextUtils.isEmpty(mContentDescription)) {
textView.setContentDescription(mContentDescription);
}
}
void setText(@Nullable CharSequence text) {
mText = text;
notifyChanged();
}
void setContentDescription(@Nullable CharSequence contentDescription) {
mContentDescription = contentDescription;
notifyChanged();
}
}

View File

@@ -17,11 +17,12 @@
package com.android.settings.fuelgauge;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceScreen;
@@ -31,23 +32,20 @@ import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.Utils;
import com.android.settingslib.widget.UsageProgressBarPreference;
/** Controller that update the battery header view */
public class BatteryHeaderTextPreferenceController extends BasePreferenceController
implements PreferenceControllerMixin, BatteryPreferenceController {
private static final String TAG = "BatteryHeaderPreferenceController";
@VisibleForTesting static final String KEY_BATTERY_HEADER = "battery_header";
private static final int BATTERY_MAX_LEVEL = 100;
@VisibleForTesting BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
@VisibleForTesting UsageProgressBarPreference mBatteryUsageProgressBarPref;
private static final String TAG = "BatteryHeaderTextPreferenceController";
private final PowerManager mPowerManager;
private final BatterySettingsFeatureProvider mBatterySettingsFeatureProvider;
private BatteryTip mBatteryTip;
@Nullable private BatteryTip mBatteryTip;
@VisibleForTesting BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
@Nullable @VisibleForTesting BatteryHeaderTextPreference mBatteryHeaderTextPreference;
public BatteryHeaderTextPreferenceController(Context context, String key) {
super(context, key);
@@ -61,14 +59,11 @@ public class BatteryHeaderTextPreferenceController extends BasePreferenceControl
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mBatteryUsageProgressBarPref = screen.findPreference(getPreferenceKey());
// Set up empty space text first to prevent layout flaky before info loaded.
mBatteryUsageProgressBarPref.setBottomSummary(" ");
mBatteryHeaderTextPreference = screen.findPreference(getPreferenceKey());
if (com.android.settings.Utils.isBatteryPresent(mContext)) {
quickUpdateHeaderPreference();
} else {
mBatteryUsageProgressBarPref.setVisible(false);
if (mBatteryHeaderTextPreference != null
&& !com.android.settings.Utils.isBatteryPresent(mContext)) {
mBatteryHeaderTextPreference.setVisible(false);
}
}
@@ -77,7 +72,8 @@ public class BatteryHeaderTextPreferenceController extends BasePreferenceControl
return AVAILABLE_UNSEARCHABLE;
}
private CharSequence generateLabel(BatteryInfo info) {
@NonNull
private CharSequence generateLabel(@NonNull BatteryInfo info) {
if (Utils.containsIncompatibleChargers(mContext, TAG)) {
return mContext.getString(
com.android.settingslib.R.string.battery_info_status_not_charging);
@@ -99,8 +95,8 @@ public class BatteryHeaderTextPreferenceController extends BasePreferenceControl
if (info.pluggedStatus == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
final CharSequence wirelessChargingLabel =
mBatterySettingsFeatureProvider.getWirelessChargingLabel(mContext, info);
if (wirelessChargingLabel != null) {
mBatteryUsageProgressBarPref.setBottomSummaryContentDescription(
if (mBatteryHeaderTextPreference != null && wirelessChargingLabel != null) {
mBatteryHeaderTextPreference.setContentDescription(
mBatterySettingsFeatureProvider
.getWirelessChargingContentDescription(mContext, info));
return wirelessChargingLabel;
@@ -138,46 +134,33 @@ public class BatteryHeaderTextPreferenceController extends BasePreferenceControl
}
}
/** Updates the battery header. */
public void updateHeaderPreference(BatteryInfo info) {
if (!mBatteryStatusFeatureProvider.triggerBatteryStatusUpdate(this, info)) {
mBatteryUsageProgressBarPref.setBottomSummary(generateLabel(info));
/** Updates the battery header text with the given BatteryInfo. */
public void updateHeaderPreference(@NonNull BatteryInfo info) {
if (mBatteryHeaderTextPreference != null
&& !mBatteryStatusFeatureProvider.triggerBatteryStatusUpdate(this, info)) {
mBatteryHeaderTextPreference.setText(generateLabel(info));
}
mBatteryUsageProgressBarPref.setUsageSummary(
formatBatteryPercentageText(info.batteryLevel));
mBatteryUsageProgressBarPref.setPercent(info.batteryLevel, BATTERY_MAX_LEVEL);
}
/** Callback which receives text for the summary line. */
/** Callback which updates the battery header text with the given label. */
@Override
public void updateBatteryStatus(String label, BatteryInfo info) {
if (mBatteryHeaderTextPreference == null) {
return;
}
final CharSequence summary = label != null ? label : generateLabel(info);
mBatteryUsageProgressBarPref.setBottomSummary(summary);
mBatteryHeaderTextPreference.setText(summary);
Log.d(TAG, "updateBatteryStatus: " + label + " summary: " + summary);
}
/** Updates the battery header quickly. */
public void quickUpdateHeaderPreference() {
Intent batteryBroadcast =
com.android.settingslib.fuelgauge.BatteryUtils.getBatteryIntent(mContext);
final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
final boolean discharging =
batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
mBatteryUsageProgressBarPref.setUsageSummary(formatBatteryPercentageText(batteryLevel));
mBatteryUsageProgressBarPref.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
}
/** Update summary when battery tips changed. */
public void updateHeaderByBatteryTips(BatteryTip batteryTip, BatteryInfo batteryInfo) {
/** Update summary when battery tips are changed. */
public void updateHeaderByBatteryTips(
@Nullable BatteryTip batteryTip, @NonNull BatteryInfo batteryInfo) {
mBatteryTip = batteryTip;
if (mBatteryTip != null && batteryInfo != null) {
updateHeaderPreference(batteryInfo);
}
}
private CharSequence formatBatteryPercentageText(int batteryLevel) {
return com.android.settings.Utils.formatPercentage(batteryLevel);
}
}

View File

@@ -36,7 +36,7 @@ import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.fuelgauge.BatteryHeaderPreferenceController;
import com.android.settings.fuelgauge.BatteryHeaderTextPreferenceController;
import com.android.settings.fuelgauge.BatteryInfo;
import com.android.settings.fuelgauge.BatteryInfoLoader;
import com.android.settings.fuelgauge.BatteryUtils;
@@ -67,7 +67,7 @@ public class PowerUsageSummary extends PowerUsageBase
@VisibleForTesting BatteryUtils mBatteryUtils;
@VisibleForTesting BatteryInfo mBatteryInfo;
@VisibleForTesting BatteryHeaderPreferenceController mBatteryHeaderPreferenceController;
@VisibleForTesting BatteryHeaderTextPreferenceController mBatteryHeaderTextPreferenceController;
@VisibleForTesting BatteryTipPreferenceController mBatteryTipPreferenceController;
@VisibleForTesting boolean mNeedUpdateBatteryTip;
@VisibleForTesting Preference mHelpPreference;
@@ -93,8 +93,8 @@ public class PowerUsageSummary extends PowerUsageBase
@Override
public void onLoadFinished(Loader<BatteryInfo> loader, BatteryInfo batteryInfo) {
mBatteryHeaderPreferenceController.updateHeaderPreference(batteryInfo);
mBatteryHeaderPreferenceController.updateHeaderByBatteryTips(
mBatteryHeaderTextPreferenceController.updateHeaderPreference(batteryInfo);
mBatteryHeaderTextPreferenceController.updateHeaderByBatteryTips(
mBatteryTipPreferenceController.getCurrentBatteryTip(), batteryInfo);
mBatteryInfo = batteryInfo;
}
@@ -116,7 +116,7 @@ public class PowerUsageSummary extends PowerUsageBase
@Override
public void onLoadFinished(Loader<List<BatteryTip>> loader, List<BatteryTip> data) {
mBatteryTipPreferenceController.updateBatteryTips(data);
mBatteryHeaderPreferenceController.updateHeaderByBatteryTips(
mBatteryHeaderTextPreferenceController.updateHeaderByBatteryTips(
mBatteryTipPreferenceController.getCurrentBatteryTip(), mBatteryInfo);
}
@@ -129,7 +129,7 @@ public class PowerUsageSummary extends PowerUsageBase
super.onAttach(context);
final Activity activity = getActivity();
mBatteryHeaderPreferenceController = use(BatteryHeaderPreferenceController.class);
mBatteryHeaderTextPreferenceController = use(BatteryHeaderTextPreferenceController.class);
mBatteryTipPreferenceController = use(BatteryTipPreferenceController.class);
mBatteryTipPreferenceController.setActivity(activity);
@@ -250,15 +250,6 @@ public class PowerUsageSummary extends PowerUsageBase
mNeedUpdateBatteryTip = icicle == null || mBatteryTipPreferenceController.needUpdate();
}
@Override
protected void restartBatteryStatsLoader(@BatteryUpdateType int refreshType) {
super.restartBatteryStatsLoader(refreshType);
// Update battery header if battery is present.
if (mIsBatteryPresent) {
mBatteryHeaderPreferenceController.quickUpdateHeaderPreference();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

View File

@@ -19,6 +19,7 @@ import android.content.Context
import com.android.settings.R
import com.android.settings.display.BatteryPercentageSwitchPreference
import com.android.settings.flags.Flags
import com.android.settings.fuelgauge.BatteryHeaderPreference
import com.android.settingslib.metadata.PreferenceAvailabilityProvider
import com.android.settingslib.metadata.PreferenceIconProvider
import com.android.settingslib.metadata.ProvidePreferenceScreen
@@ -53,8 +54,10 @@ class PowerUsageSummaryScreen :
R.drawable.ic_settings_battery_white
}
override fun getPreferenceHierarchy(context: Context) =
preferenceHierarchy(this) { +BatteryPercentageSwitchPreference() }
override fun getPreferenceHierarchy(context: Context) = preferenceHierarchy(this) {
+BatteryHeaderPreference()
+BatteryPercentageSwitchPreference()
}
companion object {
const val KEY = "power_usage_summary_screen"

View File

@@ -34,6 +34,7 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import java.util.Arrays;
import java.util.List;
/** Preference controller for Satellite functions in mobile network settings. */
@@ -99,11 +100,12 @@ public class SatelliteSettingsPreferenceCategoryController
class CarrierRoamingNtnModeCallback extends TelephonyCallback implements
TelephonyCallback.CarrierRoamingNtnModeListener {
@Override
public void onCarrierRoamingNtnAvailableServicesChanged(List<Integer> availableServices) {
public void onCarrierRoamingNtnAvailableServicesChanged(int[] availableServices) {
CarrierRoamingNtnModeListener.super.onCarrierRoamingNtnAvailableServicesChanged(
availableServices);
boolean isSmsAvailable = availableServices.contains(SERVICE_TYPE_SMS);
boolean isDataAvailable = availableServices.contains(SERVICE_TYPE_DATA);
List<Integer> availableServicesList = Arrays.stream(availableServices).boxed().toList();
boolean isSmsAvailable = availableServicesList.contains(SERVICE_TYPE_SMS);
boolean isDataAvailable = availableServicesList.contains(SERVICE_TYPE_DATA);
Log.i(TAG, "isSmsAvailable : " + isSmsAvailable
+ " / isDataAvailable " + isDataAvailable);
if (mPreferenceCategory == null) {

View File

@@ -34,6 +34,7 @@ import androidx.navigation.navArgument
import com.android.settings.R
import com.android.settings.applications.AppInfoBase
import com.android.settings.applications.appinfo.AppInfoDashboardFragment
import com.android.settings.development.Enable16kUtils
import com.android.settings.flags.Flags
import com.android.settings.spa.SpaActivity.Companion.startSpaActivity
import com.android.settings.spa.app.appcompat.UserAspectRatioAppPreference
@@ -169,6 +170,7 @@ private fun AppInfoSettings(packageInfoPresenter: PackageInfoPresenter) {
InteractAcrossProfilesDetailsPreference(app)
AlarmsAndRemindersAppListProvider.InfoPageEntryItem(app)
WriteSystemPreferencesAppListProvider.InfoPageEntryItem(app)
Enable16KbAppCompatPreference(app)
}
Category(title = stringResource(R.string.app_install_details_group_title)) {

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2024 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.spa.app.appinfo
import android.content.Context
import android.content.pm.ApplicationInfo
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.settings.R
import com.android.settings.development.Enable16kUtils
import com.android.settings.flags.Flags
import com.android.settingslib.spa.framework.compose.OverridableFlow
import com.android.settingslib.spa.widget.preference.SwitchPreference
import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
import kotlinx.coroutines.flow.flow
@Composable
fun Enable16KbAppCompatPreference(
app: ApplicationInfo
) {
val context = LocalContext.current
val presenter = remember(app) { Enable16KbAppCompatSwitchPresenter(context, app) }
if (!presenter.isAvailable()) return
val isCheckedState = presenter.isCheckedFlow.collectAsStateWithLifecycle(initialValue = null)
SwitchPreference(remember {
object : SwitchPreferenceModel {
override val title =
context.getString(R.string.enable_16k_app_compat_title)
override val summary = {
context.getString(R.string.enable_16k_app_compat_details)
}
override val checked = {
isCheckedState.value
}
override val onCheckedChange = presenter::onCheckedChange
}
})
}
private class Enable16KbAppCompatSwitchPresenter(context: Context, private val app: ApplicationInfo) {
private val packageManager = context.packageManager
fun isAvailable(): Boolean {
return Enable16kUtils.isUsing16kbPages() && Flags.pageSizeAppCompatSetting()
}
private val isChecked = OverridableFlow(flow {
emit(packageManager.isPageSizeCompatEnabled(app.packageName))
})
val isCheckedFlow = isChecked.flow
fun onCheckedChange(newChecked: Boolean) {
try {
packageManager.setPageSizeAppCompatFlagsSettingsOverride(app.packageName, newChecked)
isChecked.override(newChecked)
} catch (e: RuntimeException) {
Log.e("Enable16KbAppCompat", "Failed to set" +
"setPageSizeAppCompatModeSettingsOverride", e);
}
}
}

View File

@@ -104,7 +104,6 @@ public class BluetoothDetailsCompanionAppsControllerTest extends
associationId,
/* userId */ 0,
packageName,
/* tag */ null,
MacAddress.fromString(mCachedDevice.getAddress()),
/* displayName */ null,
/* deviceProfile */ "",
@@ -116,7 +115,8 @@ public class BluetoothDetailsCompanionAppsControllerTest extends
/* timeApprovedMs */ System.currentTimeMillis(),
/* lastTimeConnected */ Long.MAX_VALUE,
/* systemDataSyncFlags */ -1,
/* deviceIcon */ null);
/* deviceIcon */ null,
/* deviceId */ null);
mAssociations.add(association);
showScreen(mController);

View File

@@ -17,31 +17,21 @@ package com.android.settings.fuelgauge;
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.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.hardware.usb.UsbPortStatus;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.os.SystemProperties;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.SmartBatteryTip;
import com.android.settings.testutils.BatteryTestUtils;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settings.widget.EntityHeaderController;
@@ -56,10 +46,9 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowPowerManager;
// LINT.IfChange
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowEntityHeaderController.class, ShadowUtils.class})
public class BatteryHeaderPreferenceControllerTest {
@@ -67,23 +56,17 @@ public class BatteryHeaderPreferenceControllerTest {
private static final String PREF_KEY = "battery_header";
private static final int BATTERY_LEVEL = 60;
private static final int BATTERY_MAX_LEVEL = 100;
private static final String TIME_LEFT = "2h30min";
private static final String BATTERY_STATUS = "Charging";
@Mock private PreferenceScreen mPreferenceScreen;
@Mock private BatteryInfo mBatteryInfo;
@Mock private BatteryBroadcastReceiver mBatteryBroadcastReceiver;
@Mock private EntityHeaderController mEntityHeaderController;
@Mock private UsageProgressBarPreference mBatteryUsageProgressBarPref;
@Mock private BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
@Mock private UsbPort mUsbPort;
@Mock private UsageProgressBarPreference mBatteryUsageProgressBarPreference;
@Mock private UsbManager mUsbManager;
@Mock private UsbPortStatus mUsbPortStatus;
@Mock private LifecycleOwner mLifecycleOwner;
private BatteryHeaderPreferenceController mController;
private Context mContext;
private ShadowPowerManager mShadowPowerManager;
private Intent mBatteryIntent;
private FakeFeatureFactory mFactory;
@Before
public void setUp() {
@@ -99,18 +82,12 @@ public class BatteryHeaderPreferenceControllerTest {
mBatteryIntent.putExtra(BatteryManager.EXTRA_PLUGGED, 1);
doReturn(mBatteryIntent).when(mContext).registerReceiver(any(), any());
doReturn(mBatteryUsageProgressBarPref)
doReturn(mBatteryUsageProgressBarPreference)
.when(mPreferenceScreen)
.findPreference(BatteryHeaderPreferenceController.KEY_BATTERY_HEADER);
mBatteryInfo.batteryLevel = BATTERY_LEVEL;
mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class));
mFactory = FakeFeatureFactory.setupForTest();
.findPreference(PREF_KEY);
mController = spy(new BatteryHeaderPreferenceController(mContext, PREF_KEY));
mController.mBatteryUsageProgressBarPref = mBatteryUsageProgressBarPref;
mController.mBatteryStatusFeatureProvider = mBatteryStatusFeatureProvider;
mController.mBatteryUsageProgressBarPreference = mBatteryUsageProgressBarPreference;
BatteryUtils.setChargingStringV2Enabled(null);
}
@@ -121,383 +98,45 @@ public class BatteryHeaderPreferenceControllerTest {
ShadowUtils.reset();
}
@Test
public void onStateChanged_onCreate_receiverCreated() {
mController.onStateChanged(mLifecycleOwner, Lifecycle.Event.ON_CREATE);
assertThat(mController.mBatteryBroadcastReceiver).isNotNull();
}
@Test
public void onStateChanged_onStart_receiverRegistered() {
mController.mBatteryBroadcastReceiver = mBatteryBroadcastReceiver;
mController.onStateChanged(mLifecycleOwner, Lifecycle.Event.ON_START);
verify(mBatteryBroadcastReceiver).register();
}
@Test
public void onStateChanged_onStop_receiverUnregistered() {
mController.mBatteryBroadcastReceiver = mBatteryBroadcastReceiver;
mController.onStateChanged(mLifecycleOwner, Lifecycle.Event.ON_STOP);
verify(mBatteryBroadcastReceiver).unRegister();
}
@Test
public void displayPreference_displayBatteryLevel() {
mController.displayPreference(mPreferenceScreen);
verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
}
@Test
public void updatePreference_hasRemainingTime_showRemainingLabel() {
mBatteryInfo.remainingLabel = TIME_LEFT;
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
}
@Test
public void updatePreference_updateBatteryInfo() {
setChargingState(/* isDischarging */ true, /* updatedByStatusFeature */ false);
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
}
@Test
public void updatePreference_noRemainingTime_showStatusLabel() {
mBatteryInfo.remainingLabel = null;
mBatteryInfo.statusLabel = BATTERY_STATUS;
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(BATTERY_STATUS);
}
@Test
public void updatePreference_statusAnomalous_showStatusLabel() {
mBatteryInfo.remainingLabel = TIME_LEFT;
mBatteryInfo.statusLabel = BATTERY_STATUS;
mBatteryInfo.batteryStatus = BatteryManager.BATTERY_STATUS_NOT_CHARGING;
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(BATTERY_STATUS);
}
@Test
public void updatePreference_charging_showFullText() {
setChargingState(/* isDischarging */ false, /* updatedByStatusFeature */ false);
mController.updateHeaderPreference(mBatteryInfo);
final String expectedResult = BATTERY_STATUS + "" + TIME_LEFT;
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedResult);
}
@Test
public void updatePreference_powerSaverOn_showPowerSaverOn() {
setChargingState(/* isDischarging */ true, /* updatedByStatusFeature */ false);
mShadowPowerManager.setIsPowerSaveMode(true);
mController.updateHeaderPreference(mBatteryInfo);
final String expectedResult = "Battery Saver on • " + TIME_LEFT;
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedResult);
}
@Test
public void updatePreference_triggerBatteryStatusUpdateTrue_updatePercentageAndUsageOnly() {
setChargingState(/* isDischarging */ true, /* updatedByStatusFeature */ true);
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
}
@Test
public void updatePreference_triggerBatteryStatusUpdateFalse_updateBatteryInfo() {
setChargingState(/* isDischarging */ true, /* updatedByStatusFeature */ false);
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
}
@Test
public void updateBatteryStatus_nullLabel_updateSummaryOnly() {
setChargingState(/* isDischarging */ true, /* updatedByStatusFeature */ false);
mController.updateBatteryStatus(null, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
}
@Test
public void updateBatteryStatus_withLabel_showLabelText() {
setChargingState(/* isDischarging */ true, /* updatedByStatusFeature */ false);
final String label = "Update by battery status • " + TIME_LEFT;
mController.updateBatteryStatus(label, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(label);
}
@Test
public void updateBatteryStatus_chargingString_statusWithRemainingLabel() {
var batteryInfo =
arrangeUpdateBatteryStatusTestWithRemainingLabel(
/* remainingLabel= */ "1 hr, 40 min left until full",
/* statusLabel= */ "Charging rapidly",
/* isFastCharging= */ true,
/* isChargingStringV2= */ false);
var expectedChargingString = batteryInfo.statusLabel + "" + batteryInfo.remainingLabel;
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
}
@Test
public void updateBatteryStatus_chargingStringV2FastCharging_statusWithRemainingLabel() {
var batteryInfo =
arrangeUpdateBatteryStatusTestWithRemainingLabel(
/* remainingLabel= */ "Full by 1:30 PM",
/* statusLabel= */ "Fast Charging",
/* isFastCharging= */ true,
/* isChargingStringV2= */ true);
var expectedChargingString = batteryInfo.statusLabel + "" + batteryInfo.remainingLabel;
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
}
@Test
public void updateBatteryStatus_chargingStringV2NonFastCharging_remainingLabel() {
var batteryInfo =
arrangeUpdateBatteryStatusTestWithRemainingLabel(
/* remainingLabel= */ "Fully charged by 11:10 PM",
/* statusLabel= */ "Charging",
/* isFastCharging= */ false,
/* isChargingStringV2= */ true);
var expectedChargingString = batteryInfo.remainingLabel;
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
}
@Test
public void updateBatteryStatus_customizedWirelessChargingLabel_customizedLabel() {
var label = "Customized Wireless Charging Label";
var contentDescription = "Customized Wireless Charging description";
var batteryInfo =
arrangeUpdateBatteryStatusTestWithRemainingLabel(
/* remainingLabel= */ "Full by 1:30 PM",
/* statusLabel= */ "Fast Charging",
/* isFastCharging= */ true,
/* isChargingStringV2= */ true);
batteryInfo.pluggedStatus = BatteryManager.BATTERY_PLUGGED_WIRELESS;
when(mFactory.batterySettingsFeatureProvider.getWirelessChargingLabel(
eq(mContext), any(BatteryInfo.class)))
.thenReturn(label);
when(mFactory.batterySettingsFeatureProvider.getWirelessChargingContentDescription(
eq(mContext), any(BatteryInfo.class)))
.thenReturn(contentDescription);
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(label);
verify(mBatteryUsageProgressBarPref).setBottomSummaryContentDescription(contentDescription);
}
@Test
public void updateBatteryStatus_noCustomizedWirelessChargingLabel_statusWithRemainingLabel() {
var contentDescription = "Customized Wireless Charging description";
var batteryInfo =
arrangeUpdateBatteryStatusTestWithRemainingLabel(
/* remainingLabel= */ "Full by 1:30 PM",
/* statusLabel= */ "Fast Charging",
/* isFastCharging= */ true,
/* isChargingStringV2= */ true);
batteryInfo.pluggedStatus = BatteryManager.BATTERY_PLUGGED_WIRELESS;
var expectedChargingString = batteryInfo.statusLabel + "" + batteryInfo.remainingLabel;
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
verify(mBatteryUsageProgressBarPref, never())
.setBottomSummaryContentDescription(contentDescription);
}
@Test
public void updateBatteryStatus_noCustomizedWirelessChargingLabel_v1StatusWithRemainingLabel() {
var contentDescription = "Customized Wireless Charging description";
var batteryInfo =
arrangeUpdateBatteryStatusTestWithRemainingLabel(
/* remainingLabel= */ "1 hr, 40 min left until full",
/* statusLabel= */ "Charging wirelessly",
/* isFastCharging= */ false,
/* isChargingStringV2= */ false);
batteryInfo.pluggedStatus = BatteryManager.BATTERY_PLUGGED_WIRELESS;
var expectedChargingString = batteryInfo.statusLabel + "" + batteryInfo.remainingLabel;
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
verify(mBatteryUsageProgressBarPref, never())
.setBottomSummaryContentDescription(contentDescription);
}
@Test
public void updateBatteryStatus_chargingOptimizationMode_remainingLabel() {
var batteryInfo =
arrangeUpdateBatteryStatusTestWithRemainingLabel(
/* remainingLabel= */ "Expected remaining label",
/* statusLabel= */ "Fast Charging",
/* isFastCharging= */ true,
/* isChargingStringV2= */ true);
var expectedChargingString = batteryInfo.remainingLabel;
when(mFactory.batterySettingsFeatureProvider.isChargingOptimizationMode(mContext))
.thenReturn(true);
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
}
@Test
public void updateBatteryStatus_chargingOptimizationModeNoRemainingLabel_statusLabel() {
var batteryInfo =
arrangeUpdateBatteryStatusTestWithRemainingLabel(
/* remainingLabel= */ null,
/* statusLabel= */ "Fast Charging",
/* isFastCharging= */ true,
/* isChargingStringV2= */ true);
var expectedChargingString = batteryInfo.statusLabel;
when(mFactory.batterySettingsFeatureProvider.isChargingOptimizationMode(mContext))
.thenReturn(true);
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
}
@Test
public void updateBatteryStatus_notChargingOptimizationMode_statusWithRemainingLabel() {
var batteryInfo =
arrangeUpdateBatteryStatusTestWithRemainingLabel(
/* remainingLabel= */ "Full by 1:30 PM",
/* statusLabel= */ "Fast Charging",
/* isFastCharging= */ true,
/* isChargingStringV2= */ true);
var expectedChargingString = batteryInfo.statusLabel + "" + batteryInfo.remainingLabel;
when(mFactory.batterySettingsFeatureProvider.isChargingOptimizationMode(mContext))
.thenReturn(false);
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
}
@Test
public void updateBatteryStatus_dockDefend_chargingOnHold() {
var expected = "Charging on hold";
mBatteryInfo.isBatteryDefender = false;
when(mFactory.powerUsageFeatureProvider.isExtraDefend()).thenReturn(true);
mController.updateBatteryStatus(/* label= */ null, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expected);
}
@Test
public void updateBatteryStatus_batteryDefender_chargingOnHold() {
var expected = "Charging on hold";
mBatteryInfo.isBatteryDefender = true;
when(mFactory.powerUsageFeatureProvider.isExtraDefend()).thenReturn(false);
mController.updateBatteryStatus(/* label= */ null, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expected);
}
private BatteryInfo arrangeUpdateBatteryStatusTestWithRemainingLabel(
String remainingLabel,
String statusLabel,
boolean isFastCharging,
boolean isChargingStringV2) {
SystemProperties.set(
BatteryUtils.PROPERTY_CHARGING_STRING_V2_KEY, String.valueOf(isChargingStringV2));
mBatteryInfo.isBatteryDefender = false;
mBatteryInfo.remainingLabel = remainingLabel;
mBatteryInfo.statusLabel = statusLabel;
mBatteryInfo.discharging = false;
mBatteryInfo.isFastCharging = isFastCharging;
return mBatteryInfo;
}
@Test
public void updateHeaderByBatteryTips_lowBatteryTip_showLowBattery() {
setChargingState(/* isDischarging */ true, /* updatedByStatusFeature */ false);
BatteryTip lowBatteryTip =
new LowBatteryTip(BatteryTip.StateType.NEW, /* powerSaveModeOn */ false);
mController.updateHeaderByBatteryTips(lowBatteryTip, mBatteryInfo);
final String expectedResult = "Low battery • " + TIME_LEFT;
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedResult);
}
@Test
public void updateHeaderByBatteryTips_notLowBatteryTip_showRemainingLabel() {
setChargingState(/* isDischarging */ true, /* updatedByStatusFeature */ false);
BatteryTip lowBatteryTip = new SmartBatteryTip(BatteryTip.StateType.NEW);
mController.updateHeaderByBatteryTips(lowBatteryTip, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
}
@Test
public void updateHeaderByBatteryTips_noTip_noAction() {
setChargingState(/* isDischarging */ true, /* updatedByStatusFeature */ false);
mController.updateHeaderByBatteryTips(null, mBatteryInfo);
verifyNoInteractions(mBatteryUsageProgressBarPref);
}
@Test
public void updateHeaderByBatteryTips_noBatteryInfo_noAction() {
BatteryTip lowBatteryTip =
new LowBatteryTip(BatteryTip.StateType.NEW, /* powerSaveModeOn */ false);
mController.updateHeaderByBatteryTips(lowBatteryTip, null);
verifyNoInteractions(mBatteryUsageProgressBarPref);
}
@Test
public void updatePreference_isBatteryDefender_showEmptyText() {
mBatteryInfo.isBatteryDefender = true;
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref)
.setBottomSummary(
mContext.getString(
com.android.settingslib.R.string
.battery_info_status_charging_on_hold));
}
@Test
public void updatePreference_incompatibleCharger_showNotChargingState() {
BatteryTestUtils.setupIncompatibleEvent(mUsbPort, mUsbManager, mUsbPortStatus);
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref)
.setBottomSummary(
mContext.getString(
com.android.settingslib.R.string.battery_info_status_not_charging));
verify(mBatteryUsageProgressBarPreference).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPreference).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
}
@Test
public void quickUpdateHeaderPreference_onlyUpdateBatteryLevelAndChargingState() {
mController.quickUpdateHeaderPreference();
verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
verify(mBatteryUsageProgressBarPreference).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPreference).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
}
@Test
@@ -512,26 +151,18 @@ public class BatteryHeaderPreferenceControllerTest {
mController.displayPreference(mPreferenceScreen);
assertThat(mBatteryUsageProgressBarPref.isVisible()).isFalse();
assertThat(mBatteryUsageProgressBarPreference.isVisible()).isFalse();
}
@Test
public void displayPreference_init_showEmptySpace() {
public void displayPreference_init_setEmptyBottomSummary() {
mController.displayPreference(mPreferenceScreen);
verify(mBatteryUsageProgressBarPref).setBottomSummary(" ");
verify(mBatteryUsageProgressBarPreference).setBottomSummary("");
}
private CharSequence formatBatteryPercentageText() {
return com.android.settings.Utils.formatPercentage(BATTERY_LEVEL);
}
private void setChargingState(boolean isDischarging, boolean updatedByStatusFeature) {
mBatteryInfo.remainingLabel = TIME_LEFT;
mBatteryInfo.statusLabel = BATTERY_STATUS;
mBatteryInfo.discharging = isDischarging;
when(mBatteryStatusFeatureProvider.triggerBatteryStatusUpdate(mController, mBatteryInfo))
.thenReturn(updatedByStatusFeature);
}
}
// LINT.ThenChange(BatteryHeaderPreferenceTest.java)

View File

@@ -0,0 +1,85 @@
/*
* Copyright (C) 2024 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
import android.content.BroadcastReceiver
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager.EXTRA_LEVEL
import android.os.BatteryManager.EXTRA_SCALE
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.metadata.PreferenceLifecycleContext
import com.android.settingslib.preference.createAndBindWidget
import com.android.settingslib.widget.UsageProgressBarPreference
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
// LINT.IfChange
@RunWith(AndroidJUnit4::class)
class BatteryHeaderPreferenceTest {
private val mockLifecycleContext = mock<PreferenceLifecycleContext>()
private val mockBatteryBroadcastReceiver = mock<BatteryBroadcastReceiver>()
private val batteryHeaderPreference = BatteryHeaderPreference()
private val context: Context =
object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
override fun registerReceiver(receiver: BroadcastReceiver?, filter: IntentFilter?) =
Intent(Intent.ACTION_BATTERY_CHANGED)
.putExtra(EXTRA_LEVEL, 60)
.putExtra(EXTRA_SCALE, 100)
}
@Test
fun createAndBindWidget_selectableIsFalse() {
val usageProgressBarPreference =
batteryHeaderPreference.createAndBindWidget<UsageProgressBarPreference>(context)
assertThat(usageProgressBarPreference.isSelectable).isFalse()
}
@Test
fun onCreate_createBatteryBroadcastReceiver() {
batteryHeaderPreference.onCreate(mockLifecycleContext)
assertThat(batteryHeaderPreference.batteryBroadcastReceiver).isNotNull()
}
@Test
fun onStart_invokeRegisterMethod() {
batteryHeaderPreference.batteryBroadcastReceiver = mockBatteryBroadcastReceiver
batteryHeaderPreference.onStart(mockLifecycleContext)
verify(mockBatteryBroadcastReceiver).register()
}
@Test
fun onStop_invokeUnRegisterMethod() {
batteryHeaderPreference.batteryBroadcastReceiver = mockBatteryBroadcastReceiver
batteryHeaderPreference.onStop(mockLifecycleContext)
verify(mockBatteryBroadcastReceiver).unRegister()
}
}
// LINT.ThenChange(BatteryHeaderPreferenceControllerTest.java)

View File

@@ -47,7 +47,6 @@ import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.fuelgauge.BatteryUtils;
import com.android.settingslib.widget.UsageProgressBarPreference;
import org.junit.After;
import org.junit.Before;
@@ -65,22 +64,21 @@ import org.robolectric.shadows.ShadowPowerManager;
@Config(shadows = {ShadowEntityHeaderController.class, ShadowUtils.class})
public class BatteryHeaderTextPreferenceControllerTest {
private static final String PREF_KEY = "battery_header";
private static final String PREF_KEY = "battery_header_text";
private static final int BATTERY_LEVEL = 60;
private static final int BATTERY_MAX_LEVEL = 100;
private static final String TIME_LEFT = "2h30min";
private static final String BATTERY_STATUS = "Charging";
@Mock private PreferenceScreen mPreferenceScreen;
@Mock private BatteryInfo mBatteryInfo;
@Mock private EntityHeaderController mEntityHeaderController;
@Mock private UsageProgressBarPreference mBatteryUsageProgressBarPref;
@Mock private BatteryHeaderTextPreference mBatteryHeaderTextPreference;
@Mock private BatteryStatusFeatureProvider mBatteryStatusFeatureProvider;
@Mock private UsbPort mUsbPort;
@Mock private UsbManager mUsbManager;
@Mock private UsbPortStatus mUsbPortStatus;
private BatteryHeaderPreferenceController mController;
private BatteryHeaderTextPreferenceController mController;
private Context mContext;
private ShadowPowerManager mShadowPowerManager;
private Intent mBatteryIntent;
@@ -100,17 +98,17 @@ public class BatteryHeaderTextPreferenceControllerTest {
mBatteryIntent.putExtra(BatteryManager.EXTRA_PLUGGED, 1);
doReturn(mBatteryIntent).when(mContext).registerReceiver(any(), any());
doReturn(mBatteryUsageProgressBarPref)
doReturn(mBatteryHeaderTextPreference)
.when(mPreferenceScreen)
.findPreference(BatteryHeaderPreferenceController.KEY_BATTERY_HEADER);
.findPreference(PREF_KEY);
mBatteryInfo.batteryLevel = BATTERY_LEVEL;
mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class));
mFactory = FakeFeatureFactory.setupForTest();
mController = spy(new BatteryHeaderPreferenceController(mContext, PREF_KEY));
mController.mBatteryUsageProgressBarPref = mBatteryUsageProgressBarPref;
mController = spy(new BatteryHeaderTextPreferenceController(mContext, PREF_KEY));
mController.mBatteryHeaderTextPreference = mBatteryHeaderTextPreference;
mController.mBatteryStatusFeatureProvider = mBatteryStatusFeatureProvider;
BatteryUtils.setChargingStringV2Enabled(null);
@@ -122,21 +120,13 @@ public class BatteryHeaderTextPreferenceControllerTest {
ShadowUtils.reset();
}
@Test
public void displayPreference_displayBatteryLevel() {
mController.displayPreference(mPreferenceScreen);
verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
}
@Test
public void updatePreference_hasRemainingTime_showRemainingLabel() {
mBatteryInfo.remainingLabel = TIME_LEFT;
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
verify(mBatteryHeaderTextPreference).setText(mBatteryInfo.remainingLabel);
}
@Test
@@ -145,9 +135,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
verify(mBatteryHeaderTextPreference).setText(mBatteryInfo.remainingLabel);
}
@Test
@@ -157,7 +145,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(BATTERY_STATUS);
verify(mBatteryHeaderTextPreference).setText(BATTERY_STATUS);
}
@Test
@@ -168,7 +156,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(BATTERY_STATUS);
verify(mBatteryHeaderTextPreference).setText(BATTERY_STATUS);
}
@Test
@@ -178,7 +166,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo);
final String expectedResult = BATTERY_STATUS + "" + TIME_LEFT;
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedResult);
verify(mBatteryHeaderTextPreference).setText(expectedResult);
}
@Test
@@ -189,17 +177,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo);
final String expectedResult = "Battery Saver on • " + TIME_LEFT;
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedResult);
}
@Test
public void updatePreference_triggerBatteryStatusUpdateTrue_updatePercentageAndUsageOnly() {
setChargingState(/* isDischarging */ true, /* updatedByStatusFeature */ true);
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
verify(mBatteryHeaderTextPreference).setText(expectedResult);
}
@Test
@@ -208,9 +186,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
verify(mBatteryHeaderTextPreference).setText(mBatteryInfo.remainingLabel);
}
@Test
@@ -219,7 +195,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(null, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
verify(mBatteryHeaderTextPreference).setText(mBatteryInfo.remainingLabel);
}
@Test
@@ -229,7 +205,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
final String label = "Update by battery status • " + TIME_LEFT;
mController.updateBatteryStatus(label, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(label);
verify(mBatteryHeaderTextPreference).setText(label);
}
@Test
@@ -244,7 +220,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
verify(mBatteryHeaderTextPreference).setText(expectedChargingString);
}
@Test
@@ -259,7 +235,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
verify(mBatteryHeaderTextPreference).setText(expectedChargingString);
}
@Test
@@ -274,7 +250,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
verify(mBatteryHeaderTextPreference).setText(expectedChargingString);
}
@Test
@@ -297,8 +273,8 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(label);
verify(mBatteryUsageProgressBarPref).setBottomSummaryContentDescription(contentDescription);
verify(mBatteryHeaderTextPreference).setText(label);
verify(mBatteryHeaderTextPreference).setContentDescription(contentDescription);
}
@Test
@@ -315,9 +291,8 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
verify(mBatteryUsageProgressBarPref, never())
.setBottomSummaryContentDescription(contentDescription);
verify(mBatteryHeaderTextPreference).setText(expectedChargingString);
verify(mBatteryHeaderTextPreference, never()).setContentDescription(contentDescription);
}
@Test
@@ -334,9 +309,8 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
verify(mBatteryUsageProgressBarPref, never())
.setBottomSummaryContentDescription(contentDescription);
verify(mBatteryHeaderTextPreference).setText(expectedChargingString);
verify(mBatteryHeaderTextPreference, never()).setContentDescription(contentDescription);
}
@Test
@@ -353,7 +327,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
verify(mBatteryHeaderTextPreference).setText(expectedChargingString);
}
@Test
@@ -370,7 +344,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
verify(mBatteryHeaderTextPreference).setText(expectedChargingString);
}
@Test
@@ -387,7 +361,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, batteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedChargingString);
verify(mBatteryHeaderTextPreference).setText(expectedChargingString);
}
@Test
@@ -398,7 +372,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expected);
verify(mBatteryHeaderTextPreference).setText(expected);
}
@Test
@@ -409,7 +383,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateBatteryStatus(/* label= */ null, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(expected);
verify(mBatteryHeaderTextPreference).setText(expected);
}
private BatteryInfo arrangeUpdateBatteryStatusTestWithRemainingLabel(
@@ -436,7 +410,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderByBatteryTips(lowBatteryTip, mBatteryInfo);
final String expectedResult = "Low battery • " + TIME_LEFT;
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedResult);
verify(mBatteryHeaderTextPreference).setText(expectedResult);
}
@Test
@@ -446,7 +420,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderByBatteryTips(lowBatteryTip, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
verify(mBatteryHeaderTextPreference).setText(mBatteryInfo.remainingLabel);
}
@Test
@@ -455,7 +429,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderByBatteryTips(null, mBatteryInfo);
verifyNoInteractions(mBatteryUsageProgressBarPref);
verifyNoInteractions(mBatteryHeaderTextPreference);
}
@Test
@@ -465,7 +439,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderByBatteryTips(lowBatteryTip, null);
verifyNoInteractions(mBatteryUsageProgressBarPref);
verifyNoInteractions(mBatteryHeaderTextPreference);
}
@Test
@@ -474,9 +448,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref)
.setBottomSummary(
mContext.getString(
verify(mBatteryHeaderTextPreference).setText(mContext.getString(
com.android.settingslib.R.string
.battery_info_status_charging_on_hold));
}
@@ -487,20 +459,12 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.updateHeaderPreference(mBatteryInfo);
verify(mBatteryUsageProgressBarPref)
.setBottomSummary(
verify(mBatteryHeaderTextPreference)
.setText(
mContext.getString(
com.android.settingslib.R.string.battery_info_status_not_charging));
}
@Test
public void quickUpdateHeaderPreference_onlyUpdateBatteryLevelAndChargingState() {
mController.quickUpdateHeaderPreference();
verify(mBatteryUsageProgressBarPref).setUsageSummary(formatBatteryPercentageText());
verify(mBatteryUsageProgressBarPref).setPercent(BATTERY_LEVEL, BATTERY_MAX_LEVEL);
}
@Test
public void getAvailabilityStatus_returnAvailableUnsearchable() {
assertThat(mController.getAvailabilityStatus())
@@ -513,18 +477,7 @@ public class BatteryHeaderTextPreferenceControllerTest {
mController.displayPreference(mPreferenceScreen);
assertThat(mBatteryUsageProgressBarPref.isVisible()).isFalse();
}
@Test
public void displayPreference_init_showEmptySpace() {
mController.displayPreference(mPreferenceScreen);
verify(mBatteryUsageProgressBarPref).setBottomSummary(" ");
}
private CharSequence formatBatteryPercentageText() {
return com.android.settings.Utils.formatPercentage(BATTERY_LEVEL);
assertThat(mBatteryHeaderTextPreference.isVisible()).isFalse();
}
private void setChargingState(boolean isDischarging, boolean updatedByStatusFeature) {

View File

@@ -26,6 +26,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.admin.DevicePolicyManager;
import android.app.admin.DevicePolicyResourcesManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.UserInfo;
@@ -83,6 +84,8 @@ public class LocationInjectedServicesPreferenceControllerTest {
private AppSettingsInjector mSettingsInjector;
@Mock
private DevicePolicyManager mDevicePolicyManager;
@Mock
private DevicePolicyResourcesManager mDevicePolicyResourcesManager;
private Context mContext;
private LocationInjectedServicesPreferenceController mController;
@@ -104,6 +107,7 @@ public class LocationInjectedServicesPreferenceControllerTest {
when(mCategoryPrimary.getKey()).thenReturn(key);
when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
.thenReturn(mDevicePolicyManager);
when(mDevicePolicyManager.getResources()).thenReturn(mDevicePolicyResourcesManager);
}
@Test
@@ -276,6 +280,7 @@ public class LocationInjectedServicesPreferenceControllerTest {
UserHandle.of(userId),
enforcingUsers);
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(componentName);
when(mDevicePolicyResourcesManager.getString(any(), any())).thenReturn(any());
mController.displayPreference(mScreen);

View File

@@ -45,8 +45,6 @@ import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import java.util.List;
@RunWith(AndroidJUnit4.class)
public class SatelliteSettingsPreferenceCategoryControllerTest {
private static final String KEY = "key";
@@ -113,7 +111,7 @@ public class SatelliteSettingsPreferenceCategoryControllerTest {
mController.displayPreference(preferenceScreen);
mController.mCarrierRoamingNtnModeCallback.onCarrierRoamingNtnAvailableServicesChanged(
List.of(SERVICE_TYPE_DATA));
new int[]{SERVICE_TYPE_DATA});
assertThat(preferenceCategory.getTitle()).isEqualTo(
mContext.getString(R.string.satellite_setting_connectivity));
@@ -131,7 +129,7 @@ public class SatelliteSettingsPreferenceCategoryControllerTest {
mController.displayPreference(preferenceScreen);
mController.mCarrierRoamingNtnModeCallback.onCarrierRoamingNtnAvailableServicesChanged(
List.of(SERVICE_TYPE_SMS));
new int[]{SERVICE_TYPE_SMS});
assertThat(preferenceCategory.getTitle()).isEqualTo(
mContext.getString(R.string.satellite_setting_title));