Redesign the update logic of Allow Background Usage Page.
- Use one Controller to manage preferences related to battery Optimization Mode. - Move optimization mode & preferences status update logic from Fragment to Controller. Bug: 373544647 Test: atest AdvancedPowerUsageDetailTest PowerBackgroundUsageDetailTest BatteryOptimizationModePreferenceControllerTest BackgroundUsageAllowabilityPreferenceControllerTest Flag: EXEMPT for simple fix Change-Id: I2b1fed837fb4904e4118ab51c1d0cda36c0f6198
This commit is contained in:
@@ -26,13 +26,10 @@ import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
@@ -48,7 +45,6 @@ import com.android.settings.fuelgauge.batteryusage.BatteryDiffEntry;
|
||||
import com.android.settings.fuelgauge.batteryusage.BatteryEntry;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.widget.EntityHeaderController;
|
||||
import com.android.settingslib.PrimarySwitchPreference;
|
||||
import com.android.settingslib.applications.AppUtils;
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
@@ -67,9 +63,7 @@ import java.util.concurrent.Executors;
|
||||
* 2. Battery related controls for app(i.e uninstall, force stop)
|
||||
*/
|
||||
public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
implements ButtonActionDialogFragment.AppButtonsDialogListener,
|
||||
Preference.OnPreferenceClickListener,
|
||||
Preference.OnPreferenceChangeListener {
|
||||
implements ButtonActionDialogFragment.AppButtonsDialogListener {
|
||||
public static final String TAG = "AdvancedPowerDetail";
|
||||
public static final String EXTRA_UID = "extra_uid";
|
||||
public static final String EXTRA_PACKAGE_NAME = "extra_package_name";
|
||||
@@ -86,7 +80,8 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
public static final String EXTRA_POWER_USAGE_AMOUNT = "extra_power_usage_amount";
|
||||
|
||||
private static final String KEY_PREF_HEADER = "header_view";
|
||||
private static final String KEY_ALLOW_BACKGROUND_USAGE = "allow_background_usage";
|
||||
private static final String KEY_BACKGROUND_USAGE_ALLOWABILITY_CATEGORY =
|
||||
"background_usage_allowability_category";
|
||||
|
||||
private static final int REQUEST_UNINSTALL = 0;
|
||||
private static final int REQUEST_REMOVE_DEVICE_ADMIN = 1;
|
||||
@@ -96,11 +91,9 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
private AppButtonsPreferenceController mAppButtonsPreferenceController;
|
||||
private PowerUsageTimeController mPowerUsageTimeController;
|
||||
|
||||
@VisibleForTesting LayoutPreference mHeaderPreference;
|
||||
@VisibleForTesting ApplicationsState mState;
|
||||
@VisibleForTesting ApplicationsState.AppEntry mAppEntry;
|
||||
@VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils;
|
||||
@VisibleForTesting PrimarySwitchPreference mAllowBackgroundUsagePreference;
|
||||
|
||||
@VisibleForTesting @BatteryOptimizeUtils.OptimizationMode
|
||||
int mOptimizationMode = BatteryOptimizeUtils.MODE_UNKNOWN;
|
||||
@@ -242,17 +235,11 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
|
||||
final Bundle bundle = getArguments();
|
||||
final int uid = bundle.getInt(EXTRA_UID, 0);
|
||||
final String packageName = bundle.getString(EXTRA_PACKAGE_NAME);
|
||||
mBatteryOptimizeUtils = new BatteryOptimizeUtils(getContext(), uid, packageName);
|
||||
mState = ApplicationsState.getInstance(getActivity().getApplication());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
final String packageName = getArguments().getString(EXTRA_PACKAGE_NAME);
|
||||
onCreateBackgroundUsageState(packageName);
|
||||
mHeaderPreference = findPreference(KEY_PREF_HEADER);
|
||||
|
||||
if (packageName != null) {
|
||||
mAppEntry = mState.getEntry(packageName, UserHandle.myUserId());
|
||||
}
|
||||
@@ -264,7 +251,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
|
||||
initHeader();
|
||||
mOptimizationMode = mBatteryOptimizeUtils.getAppOptimizationMode();
|
||||
initFooter();
|
||||
mLogStringBuilder = new StringBuilder("onResume mode = ").append(mOptimizationMode);
|
||||
}
|
||||
|
||||
@@ -299,7 +285,8 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
|
||||
@VisibleForTesting
|
||||
void initHeader() {
|
||||
final View appSnippet = mHeaderPreference.findViewById(R.id.entity_header);
|
||||
final LayoutPreference headerPreference = findPreference(KEY_PREF_HEADER);
|
||||
final View appSnippet = headerPreference.findViewById(R.id.entity_header);
|
||||
final Activity context = getActivity();
|
||||
final Bundle bundle = getArguments();
|
||||
EntityHeaderController controller =
|
||||
@@ -340,31 +327,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
controller.done(true /* rebindActions */);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void initFooter() {
|
||||
final String stateString;
|
||||
final String detailInfoString;
|
||||
final Context context = getContext();
|
||||
|
||||
if (mBatteryOptimizeUtils.isDisabledForOptimizeModeOnly()) {
|
||||
// Present optimized only string when the package name is invalid.
|
||||
stateString = context.getString(R.string.manager_battery_usage_optimized_only);
|
||||
detailInfoString =
|
||||
context.getString(R.string.manager_battery_usage_footer_limited, stateString);
|
||||
} else if (mBatteryOptimizeUtils.isSystemOrDefaultApp()) {
|
||||
// Present unrestricted only string when the package is system or default active app.
|
||||
stateString = context.getString(R.string.manager_battery_usage_unrestricted_only);
|
||||
detailInfoString =
|
||||
context.getString(R.string.manager_battery_usage_footer_limited, stateString);
|
||||
} else {
|
||||
// Present default string to normal app.
|
||||
detailInfoString =
|
||||
context.getString(
|
||||
R.string.manager_battery_usage_allow_background_usage_summary);
|
||||
}
|
||||
mAllowBackgroundUsagePreference.setSummary(detailInfoString);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return SettingsEnums.FUELGAUGE_POWER_USAGE_DETAIL;
|
||||
@@ -384,7 +346,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
final Bundle bundle = getArguments();
|
||||
final int uid = bundle.getInt(EXTRA_UID, 0);
|
||||
final String packageName = bundle.getString(EXTRA_PACKAGE_NAME);
|
||||
|
||||
mAppButtonsPreferenceController =
|
||||
@@ -401,7 +362,12 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
controllers.add(mPowerUsageTimeController);
|
||||
}
|
||||
controllers.add(mAppButtonsPreferenceController);
|
||||
controllers.add(new AllowBackgroundPreferenceController(context, uid, packageName));
|
||||
controllers.add(
|
||||
new BackgroundUsageAllowabilityPreferenceController(
|
||||
context,
|
||||
/* dashboardFragment= */ this,
|
||||
KEY_BACKGROUND_USAGE_ALLOWABILITY_CATEGORY,
|
||||
mBatteryOptimizeUtils));
|
||||
|
||||
return controllers;
|
||||
}
|
||||
@@ -421,34 +387,6 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
if (!(preference instanceof PrimarySwitchPreference)
|
||||
|| !TextUtils.equals(preference.getKey(), KEY_ALLOW_BACKGROUND_USAGE)) {
|
||||
return false;
|
||||
}
|
||||
PowerBackgroundUsageDetail.startPowerBackgroundUsageDetailPage(
|
||||
getContext(), getArguments());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
|
||||
if (!(preference instanceof PrimarySwitchPreference)
|
||||
|| !TextUtils.equals(preference.getKey(), KEY_ALLOW_BACKGROUND_USAGE)) {
|
||||
return false;
|
||||
}
|
||||
if (newValue instanceof Boolean) {
|
||||
final boolean isAllowBackgroundUsage = (boolean) newValue;
|
||||
mBatteryOptimizeUtils.setAppUsageState(
|
||||
isAllowBackgroundUsage
|
||||
? BatteryOptimizeUtils.MODE_OPTIMIZED
|
||||
: BatteryOptimizeUtils.MODE_RESTRICTED,
|
||||
Action.APPLY);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void logMetricCategory(int currentOptimizeMode) {
|
||||
if (currentOptimizeMode == mOptimizationMode) {
|
||||
return;
|
||||
@@ -482,16 +420,4 @@ public class AdvancedPowerUsageDetail extends DashboardFragment
|
||||
getArguments().getInt(EXTRA_POWER_USAGE_AMOUNT));
|
||||
});
|
||||
}
|
||||
|
||||
private void onCreateBackgroundUsageState(String packageName) {
|
||||
mAllowBackgroundUsagePreference = findPreference(KEY_ALLOW_BACKGROUND_USAGE);
|
||||
if (mAllowBackgroundUsagePreference != null) {
|
||||
mAllowBackgroundUsagePreference.setOnPreferenceClickListener(this);
|
||||
mAllowBackgroundUsagePreference.setOnPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
mBatteryOptimizeUtils =
|
||||
new BatteryOptimizeUtils(
|
||||
getContext(), getArguments().getInt(EXTRA_UID), packageName);
|
||||
}
|
||||
}
|
||||
|
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2023 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.fuelgauge;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.PrimarySwitchPreference;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.widget.MainSwitchPreference;
|
||||
|
||||
/** Controller to update the app background usage state */
|
||||
public class AllowBackgroundPreferenceController extends AbstractPreferenceController
|
||||
implements PreferenceControllerMixin {
|
||||
|
||||
private static final String TAG = "AllowBackgroundPreferenceController";
|
||||
|
||||
@VisibleForTesting static final String KEY_ALLOW_BACKGROUND_USAGE = "allow_background_usage";
|
||||
|
||||
@VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils;
|
||||
|
||||
public AllowBackgroundPreferenceController(Context context, int uid, String packageName) {
|
||||
super(context);
|
||||
mBatteryOptimizeUtils = new BatteryOptimizeUtils(context, uid, packageName);
|
||||
}
|
||||
|
||||
private void setChecked(Preference preference, boolean checked) {
|
||||
if (preference instanceof PrimarySwitchPreference) {
|
||||
((PrimarySwitchPreference) preference).setChecked(checked);
|
||||
} else if (preference instanceof MainSwitchPreference) {
|
||||
((MainSwitchPreference) preference).setChecked(checked);
|
||||
}
|
||||
}
|
||||
|
||||
private void setEnabled(Preference preference, boolean enabled) {
|
||||
if (preference instanceof PrimarySwitchPreference) {
|
||||
((PrimarySwitchPreference) preference).setEnabled(enabled);
|
||||
((PrimarySwitchPreference) preference).setSwitchEnabled(enabled);
|
||||
} else if (preference instanceof MainSwitchPreference) {
|
||||
((MainSwitchPreference) preference).setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
setEnabled(preference, mBatteryOptimizeUtils.isOptimizeModeMutable());
|
||||
|
||||
final boolean isAllowBackground =
|
||||
mBatteryOptimizeUtils.getAppOptimizationMode()
|
||||
!= BatteryOptimizeUtils.MODE_RESTRICTED;
|
||||
setChecked(preference, isAllowBackground);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_ALLOW_BACKGROUND_USAGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
return getPreferenceKey().equals(preference.getKey());
|
||||
}
|
||||
}
|
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* 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.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
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.dashboard.DashboardFragment;
|
||||
import com.android.settingslib.PrimarySwitchPreference;
|
||||
|
||||
/** Controller to update the manage battery usage preference in App Battery Usage page */
|
||||
public class BackgroundUsageAllowabilityPreferenceController extends BasePreferenceController
|
||||
implements PreferenceControllerMixin {
|
||||
|
||||
@VisibleForTesting
|
||||
static final String KEY_BACKGROUND_USAGE_ALLOWABILITY_SWITCH =
|
||||
"background_usage_allowability_switch";
|
||||
|
||||
private final BatteryOptimizeUtils mBatteryOptimizeUtils;
|
||||
private final DashboardFragment mDashboardFragment;
|
||||
@Nullable @VisibleForTesting PrimarySwitchPreference mBackgroundUsageAllowabilityPreference;
|
||||
|
||||
public BackgroundUsageAllowabilityPreferenceController(
|
||||
@NonNull Context context,
|
||||
@NonNull DashboardFragment dashboardFragment,
|
||||
@NonNull String preferenceKey,
|
||||
@NonNull BatteryOptimizeUtils batteryOptimizeUtils) {
|
||||
super(context, preferenceKey);
|
||||
mDashboardFragment = dashboardFragment;
|
||||
mBatteryOptimizeUtils = batteryOptimizeUtils;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return AVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(@NonNull Preference preference) {
|
||||
updatePreferences(mBatteryOptimizeUtils.getAppOptimizationMode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(@NonNull PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mBackgroundUsageAllowabilityPreference =
|
||||
screen.findPreference(KEY_BACKGROUND_USAGE_ALLOWABILITY_SWITCH);
|
||||
initPreferences();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void initPreferences() {
|
||||
if (mBackgroundUsageAllowabilityPreference == null) {
|
||||
return;
|
||||
}
|
||||
final String stateString;
|
||||
final String detailInfoString;
|
||||
boolean isPreferenceEnabled = true;
|
||||
if (mBatteryOptimizeUtils.isDisabledForOptimizeModeOnly()) {
|
||||
// Present "Optimized" only string if the package name is invalid.
|
||||
stateString = mContext.getString(R.string.manager_battery_usage_optimized_only);
|
||||
detailInfoString =
|
||||
mContext.getString(R.string.manager_battery_usage_footer_limited, stateString);
|
||||
isPreferenceEnabled = false;
|
||||
} else if (mBatteryOptimizeUtils.isSystemOrDefaultApp()) {
|
||||
// Present "Unrestricted" only string if the package is system important apps.
|
||||
stateString = mContext.getString(R.string.manager_battery_usage_unrestricted_only);
|
||||
detailInfoString =
|
||||
mContext.getString(R.string.manager_battery_usage_footer_limited, stateString);
|
||||
isPreferenceEnabled = false;
|
||||
} else {
|
||||
// Present default string to normal app.
|
||||
detailInfoString =
|
||||
mContext.getString(
|
||||
R.string.manager_battery_usage_allow_background_usage_summary);
|
||||
}
|
||||
mBackgroundUsageAllowabilityPreference.setEnabled(isPreferenceEnabled);
|
||||
mBackgroundUsageAllowabilityPreference.setSwitchEnabled(isPreferenceEnabled);
|
||||
mBackgroundUsageAllowabilityPreference.setSummary(detailInfoString);
|
||||
if (isPreferenceEnabled) {
|
||||
mBackgroundUsageAllowabilityPreference.setOnPreferenceClickListener(
|
||||
preference -> {
|
||||
PowerBackgroundUsageDetail.startPowerBackgroundUsageDetailPage(
|
||||
mContext, mDashboardFragment.getArguments());
|
||||
return true;
|
||||
});
|
||||
mBackgroundUsageAllowabilityPreference.setOnPreferenceChangeListener(
|
||||
(preference, isAllowBackground) -> {
|
||||
handleBatteryOptimizeModeUpdated(
|
||||
(boolean) isAllowBackground
|
||||
? BatteryOptimizeUtils.MODE_OPTIMIZED
|
||||
: BatteryOptimizeUtils.MODE_RESTRICTED);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void handleBatteryOptimizeModeUpdated(int newBatteryOptimizeMode) {
|
||||
if (mBatteryOptimizeUtils.getAppOptimizationMode() == newBatteryOptimizeMode) {
|
||||
Log.w(TAG, "ignore same mode for: " + mBatteryOptimizeUtils.getPackageName());
|
||||
return;
|
||||
}
|
||||
mBatteryOptimizeUtils.setAppUsageState(
|
||||
newBatteryOptimizeMode, BatteryOptimizeHistoricalLogEntry.Action.APPLY);
|
||||
updatePreferences(newBatteryOptimizeMode);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void updatePreferences(int optimizationMode) {
|
||||
if (mBackgroundUsageAllowabilityPreference == null) {
|
||||
return;
|
||||
}
|
||||
mBackgroundUsageAllowabilityPreference.setChecked(
|
||||
optimizationMode != BatteryOptimizeUtils.MODE_RESTRICTED);
|
||||
}
|
||||
}
|
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.widget.MainSwitchPreference;
|
||||
import com.android.settingslib.widget.SelectorWithWidgetPreference;
|
||||
|
||||
/** Controller to update the app background usage mode state in Allow background usage page */
|
||||
public class BatteryOptimizationModePreferenceController extends BasePreferenceController
|
||||
implements PreferenceControllerMixin {
|
||||
|
||||
@VisibleForTesting
|
||||
static final String KEY_BACKGROUND_USAGE_ALLOWABILITY_SWITCH =
|
||||
"background_usage_allowability_switch";
|
||||
|
||||
@VisibleForTesting static final String KEY_OPTIMIZED_PREF = "optimized_preference";
|
||||
@VisibleForTesting static final String KEY_UNRESTRICTED_PREF = "unrestricted_preference";
|
||||
|
||||
private final BatteryOptimizeUtils mBatteryOptimizeUtils;
|
||||
@Nullable @VisibleForTesting MainSwitchPreference mBackgroundUsageAllowabilityPreference;
|
||||
@Nullable @VisibleForTesting SelectorWithWidgetPreference mOptimizedPreference;
|
||||
@Nullable @VisibleForTesting SelectorWithWidgetPreference mUnrestrictedPreference;
|
||||
|
||||
public BatteryOptimizationModePreferenceController(
|
||||
@NonNull Context context,
|
||||
@NonNull String preferenceKey,
|
||||
@NonNull BatteryOptimizeUtils batteryOptimizeUtils) {
|
||||
super(context, preferenceKey);
|
||||
mBatteryOptimizeUtils = batteryOptimizeUtils;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return AVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(@NonNull Preference preference) {
|
||||
updatePreferences(mBatteryOptimizeUtils.getAppOptimizationMode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(@NonNull PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mBackgroundUsageAllowabilityPreference =
|
||||
screen.findPreference(KEY_BACKGROUND_USAGE_ALLOWABILITY_SWITCH);
|
||||
mOptimizedPreference = screen.findPreference(KEY_OPTIMIZED_PREF);
|
||||
mUnrestrictedPreference = screen.findPreference(KEY_UNRESTRICTED_PREF);
|
||||
initPreferences();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void initPreferences() {
|
||||
if (mBackgroundUsageAllowabilityPreference == null
|
||||
|| mOptimizedPreference == null
|
||||
|| mUnrestrictedPreference == null) {
|
||||
return;
|
||||
}
|
||||
final boolean isEnabled = mBatteryOptimizeUtils.isOptimizeModeMutable();
|
||||
mBackgroundUsageAllowabilityPreference.setEnabled(isEnabled);
|
||||
mOptimizedPreference.setEnabled(isEnabled);
|
||||
mUnrestrictedPreference.setEnabled(isEnabled);
|
||||
if (isEnabled) {
|
||||
mBackgroundUsageAllowabilityPreference.setOnPreferenceChangeListener(
|
||||
(preference, isAllowBackground) -> {
|
||||
handleBatteryOptimizeModeUpdated(
|
||||
(boolean) isAllowBackground
|
||||
? BatteryOptimizeUtils.MODE_OPTIMIZED
|
||||
: BatteryOptimizeUtils.MODE_RESTRICTED);
|
||||
return true;
|
||||
});
|
||||
mOptimizedPreference.setOnPreferenceClickListener(
|
||||
preference -> {
|
||||
handleBatteryOptimizeModeUpdated(BatteryOptimizeUtils.MODE_OPTIMIZED);
|
||||
return true;
|
||||
});
|
||||
mUnrestrictedPreference.setOnPreferenceClickListener(
|
||||
preference -> {
|
||||
handleBatteryOptimizeModeUpdated(BatteryOptimizeUtils.MODE_UNRESTRICTED);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void updatePreferences(int optimizationMode) {
|
||||
if (mBackgroundUsageAllowabilityPreference == null
|
||||
|| mOptimizedPreference == null
|
||||
|| mUnrestrictedPreference == null) {
|
||||
return;
|
||||
}
|
||||
final boolean isAllowBackground = optimizationMode != BatteryOptimizeUtils.MODE_RESTRICTED;
|
||||
mBackgroundUsageAllowabilityPreference.setChecked(isAllowBackground);
|
||||
mOptimizedPreference.setEnabled(isAllowBackground);
|
||||
mUnrestrictedPreference.setEnabled(isAllowBackground);
|
||||
mOptimizedPreference.setChecked(optimizationMode == BatteryOptimizeUtils.MODE_OPTIMIZED);
|
||||
mUnrestrictedPreference.setChecked(
|
||||
optimizationMode == BatteryOptimizeUtils.MODE_UNRESTRICTED);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void handleBatteryOptimizeModeUpdated(int newBatteryOptimizeMode) {
|
||||
if (mBatteryOptimizeUtils.getAppOptimizationMode() == newBatteryOptimizeMode) {
|
||||
Log.w(TAG, "ignore same mode for: " + mBatteryOptimizeUtils.getPackageName());
|
||||
return;
|
||||
}
|
||||
mBatteryOptimizeUtils.setAppUsageState(
|
||||
newBatteryOptimizeMode, BatteryOptimizeHistoricalLogEntry.Action.APPLY);
|
||||
updatePreferences(newBatteryOptimizeMode);
|
||||
}
|
||||
}
|
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.widget.SelectorWithWidgetPreference;
|
||||
|
||||
public class OptimizedPreferenceController extends AbstractPreferenceController
|
||||
implements PreferenceControllerMixin {
|
||||
|
||||
private static final String TAG = "OPTIMIZED_PREF";
|
||||
|
||||
@VisibleForTesting static final String KEY_OPTIMIZED_PREF = "optimized_preference";
|
||||
@VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils;
|
||||
|
||||
public OptimizedPreferenceController(Context context, int uid, String packageName) {
|
||||
super(context);
|
||||
mBatteryOptimizeUtils = new BatteryOptimizeUtils(context, uid, packageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
preference.setEnabled(mBatteryOptimizeUtils.isSelectorPreferenceEnabled());
|
||||
|
||||
final boolean isOptimized =
|
||||
mBatteryOptimizeUtils.isDisabledForOptimizeModeOnly()
|
||||
|| mBatteryOptimizeUtils.getAppOptimizationMode()
|
||||
== BatteryOptimizeUtils.MODE_OPTIMIZED;
|
||||
((SelectorWithWidgetPreference) preference).setChecked(isOptimized);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_OPTIMIZED_PREF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
return getPreferenceKey().equals(preference.getKey());
|
||||
}
|
||||
}
|
@@ -24,11 +24,8 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.CompoundButton.OnCheckedChangeListener;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
@@ -44,8 +41,6 @@ import com.android.settingslib.applications.ApplicationsState;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.widget.FooterPreference;
|
||||
import com.android.settingslib.widget.LayoutPreference;
|
||||
import com.android.settingslib.widget.MainSwitchPreference;
|
||||
import com.android.settingslib.widget.SelectorWithWidgetPreference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -53,8 +48,7 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/** Allow background usage fragment for each app */
|
||||
public class PowerBackgroundUsageDetail extends DashboardFragment
|
||||
implements SelectorWithWidgetPreference.OnClickListener, OnCheckedChangeListener {
|
||||
public class PowerBackgroundUsageDetail extends DashboardFragment {
|
||||
private static final String TAG = "PowerBackgroundUsageDetail";
|
||||
|
||||
public static final String EXTRA_UID = "extra_uid";
|
||||
@@ -63,21 +57,15 @@ public class PowerBackgroundUsageDetail extends DashboardFragment
|
||||
public static final String EXTRA_POWER_USAGE_AMOUNT = "extra_power_usage_amount";
|
||||
public static final String EXTRA_ICON_ID = "extra_icon_id";
|
||||
private static final String KEY_PREF_HEADER = "header_view";
|
||||
private static final String KEY_PREF_UNRESTRICTED = "unrestricted_preference";
|
||||
private static final String KEY_PREF_OPTIMIZED = "optimized_preference";
|
||||
private static final String KEY_ALLOW_BACKGROUND_USAGE = "allow_background_usage";
|
||||
private static final String KEY_FOOTER_PREFERENCE = "app_usage_footer_preference";
|
||||
private static final String KEY_BATTERY_OPTIMIZATION_MODE_CATEGORY =
|
||||
"battery_optimization_mode_category";
|
||||
|
||||
private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
@VisibleForTesting LayoutPreference mHeaderPreference;
|
||||
@VisibleForTesting ApplicationsState mState;
|
||||
@VisibleForTesting ApplicationsState.AppEntry mAppEntry;
|
||||
@VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils;
|
||||
@VisibleForTesting SelectorWithWidgetPreference mOptimizePreference;
|
||||
@VisibleForTesting SelectorWithWidgetPreference mUnrestrictedPreference;
|
||||
@VisibleForTesting MainSwitchPreference mMainSwitchPreference;
|
||||
@VisibleForTesting FooterPreference mFooterPreference;
|
||||
@VisibleForTesting StringBuilder mLogStringBuilder;
|
||||
|
||||
@VisibleForTesting @BatteryOptimizeUtils.OptimizationMode
|
||||
@@ -87,17 +75,11 @@ public class PowerBackgroundUsageDetail extends DashboardFragment
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
|
||||
final Bundle bundle = getArguments();
|
||||
final int uid = bundle.getInt(EXTRA_UID, 0);
|
||||
final String packageName = bundle.getString(EXTRA_PACKAGE_NAME);
|
||||
mBatteryOptimizeUtils = new BatteryOptimizeUtils(getContext(), uid, packageName);
|
||||
mState = ApplicationsState.getInstance(getActivity().getApplication());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
final String packageName = getArguments().getString(EXTRA_PACKAGE_NAME);
|
||||
onCreateBackgroundUsageState(packageName);
|
||||
mHeaderPreference = findPreference(KEY_PREF_HEADER);
|
||||
|
||||
if (packageName != null) {
|
||||
mAppEntry = mState.getEntry(packageName, UserHandle.myUserId());
|
||||
}
|
||||
@@ -107,8 +89,8 @@ public class PowerBackgroundUsageDetail extends DashboardFragment
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
initHeader();
|
||||
mOptimizationMode = mBatteryOptimizeUtils.getAppOptimizationMode();
|
||||
initFooter();
|
||||
mOptimizationMode = mBatteryOptimizeUtils.getAppOptimizationMode();
|
||||
mLogStringBuilder = new StringBuilder("onResume mode = ").append(mOptimizationMode);
|
||||
}
|
||||
|
||||
@@ -136,20 +118,6 @@ public class PowerBackgroundUsageDetail extends DashboardFragment
|
||||
Log.d(TAG, "Leave with mode: " + currentOptimizeMode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRadioButtonClicked(SelectorWithWidgetPreference selected) {
|
||||
final String selectedKey = selected == null ? null : selected.getKey();
|
||||
updateSelectorPreferenceState(mUnrestrictedPreference, selectedKey);
|
||||
updateSelectorPreferenceState(mOptimizePreference, selectedKey);
|
||||
mBatteryOptimizeUtils.setAppUsageState(getSelectedPreference(), Action.APPLY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
mMainSwitchPreference.setChecked(isChecked);
|
||||
updateSelectorPreference(isChecked);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return SettingsEnums.FUELGAUGE_POWER_USAGE_MANAGE_BACKGROUND;
|
||||
@@ -157,14 +125,10 @@ public class PowerBackgroundUsageDetail extends DashboardFragment
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
final Bundle bundle = getArguments();
|
||||
final int uid = bundle.getInt(EXTRA_UID, 0);
|
||||
final String packageName = bundle.getString(EXTRA_PACKAGE_NAME);
|
||||
|
||||
controllers.add(new AllowBackgroundPreferenceController(context, uid, packageName));
|
||||
controllers.add(new OptimizedPreferenceController(context, uid, packageName));
|
||||
controllers.add(new UnrestrictedPreferenceController(context, uid, packageName));
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>(1);
|
||||
controllers.add(
|
||||
new BatteryOptimizationModePreferenceController(
|
||||
context, KEY_BATTERY_OPTIMIZATION_MODE_CATEGORY, mBatteryOptimizeUtils));
|
||||
|
||||
return controllers;
|
||||
}
|
||||
@@ -179,26 +143,6 @@ public class PowerBackgroundUsageDetail extends DashboardFragment
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void updateSelectorPreference(boolean isEnabled) {
|
||||
mOptimizePreference.setEnabled(isEnabled);
|
||||
mUnrestrictedPreference.setEnabled(isEnabled);
|
||||
onRadioButtonClicked(isEnabled ? mOptimizePreference : null);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
int getSelectedPreference() {
|
||||
if (!mMainSwitchPreference.isChecked()) {
|
||||
return BatteryOptimizeUtils.MODE_RESTRICTED;
|
||||
} else if (mUnrestrictedPreference.isChecked()) {
|
||||
return BatteryOptimizeUtils.MODE_UNRESTRICTED;
|
||||
} else if (mOptimizePreference.isChecked()) {
|
||||
return BatteryOptimizeUtils.MODE_OPTIMIZED;
|
||||
} else {
|
||||
return BatteryOptimizeUtils.MODE_UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
static void startPowerBackgroundUsageDetailPage(Context context, Bundle args) {
|
||||
new SubSettingLauncher(context)
|
||||
.setDestination(PowerBackgroundUsageDetail.class.getName())
|
||||
@@ -209,7 +153,11 @@ public class PowerBackgroundUsageDetail extends DashboardFragment
|
||||
|
||||
@VisibleForTesting
|
||||
void initHeader() {
|
||||
final View appSnippet = mHeaderPreference.findViewById(R.id.entity_header);
|
||||
final LayoutPreference headerPreference = findPreference(KEY_PREF_HEADER);
|
||||
if (headerPreference == null) {
|
||||
return;
|
||||
}
|
||||
final View appSnippet = headerPreference.findViewById(R.id.entity_header);
|
||||
final Activity context = getActivity();
|
||||
final Bundle bundle = getArguments();
|
||||
EntityHeaderController controller =
|
||||
@@ -239,58 +187,25 @@ public class PowerBackgroundUsageDetail extends DashboardFragment
|
||||
|
||||
@VisibleForTesting
|
||||
void initFooter() {
|
||||
final String stateString;
|
||||
final String footerString;
|
||||
final Context context = getContext();
|
||||
|
||||
if (mBatteryOptimizeUtils.isDisabledForOptimizeModeOnly()) {
|
||||
// Present optimized only string when the package name is invalid.
|
||||
stateString = context.getString(R.string.manager_battery_usage_optimized_only);
|
||||
footerString =
|
||||
context.getString(R.string.manager_battery_usage_footer_limited, stateString);
|
||||
} else if (mBatteryOptimizeUtils.isSystemOrDefaultApp()) {
|
||||
// Present unrestricted only string when the package is system or default active app.
|
||||
stateString = context.getString(R.string.manager_battery_usage_unrestricted_only);
|
||||
footerString =
|
||||
context.getString(R.string.manager_battery_usage_footer_limited, stateString);
|
||||
} else {
|
||||
// Present default string to normal app.
|
||||
footerString = context.getString(R.string.manager_battery_usage_footer);
|
||||
final FooterPreference footerPreference = findPreference(KEY_FOOTER_PREFERENCE);
|
||||
if (footerPreference == null) {
|
||||
return;
|
||||
}
|
||||
mFooterPreference.setTitle(footerString);
|
||||
final Context context = getContext();
|
||||
footerPreference.setTitle(context.getString(R.string.manager_battery_usage_footer));
|
||||
final Intent helpIntent =
|
||||
HelpUtils.getHelpIntent(
|
||||
context,
|
||||
context.getString(R.string.help_url_app_usage_settings),
|
||||
/* backupContext= */ "");
|
||||
if (helpIntent != null) {
|
||||
mFooterPreference.setLearnMoreAction(
|
||||
footerPreference.setLearnMoreAction(
|
||||
v -> startActivityForResult(helpIntent, /* requestCode= */ 0));
|
||||
mFooterPreference.setLearnMoreText(
|
||||
footerPreference.setLearnMoreText(
|
||||
context.getString(R.string.manager_battery_usage_link_a11y));
|
||||
}
|
||||
}
|
||||
|
||||
private void onCreateBackgroundUsageState(String packageName) {
|
||||
mOptimizePreference = findPreference(KEY_PREF_OPTIMIZED);
|
||||
mUnrestrictedPreference = findPreference(KEY_PREF_UNRESTRICTED);
|
||||
mMainSwitchPreference = findPreference(KEY_ALLOW_BACKGROUND_USAGE);
|
||||
mFooterPreference = findPreference(KEY_FOOTER_PREFERENCE);
|
||||
|
||||
mOptimizePreference.setOnClickListener(this);
|
||||
mUnrestrictedPreference.setOnClickListener(this);
|
||||
mMainSwitchPreference.addOnSwitchChangeListener(this);
|
||||
|
||||
mBatteryOptimizeUtils =
|
||||
new BatteryOptimizeUtils(
|
||||
getContext(), getArguments().getInt(EXTRA_UID), packageName);
|
||||
}
|
||||
|
||||
private void updateSelectorPreferenceState(
|
||||
SelectorWithWidgetPreference preference, String selectedKey) {
|
||||
preference.setChecked(TextUtils.equals(selectedKey, preference.getKey()));
|
||||
}
|
||||
|
||||
private void logMetricCategory(int currentOptimizeMode) {
|
||||
if (currentOptimizeMode == mOptimizationMode) {
|
||||
return;
|
||||
|
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.widget.SelectorWithWidgetPreference;
|
||||
|
||||
public class UnrestrictedPreferenceController extends AbstractPreferenceController
|
||||
implements PreferenceControllerMixin {
|
||||
|
||||
private static final String TAG = "UNRESTRICTED_PREF";
|
||||
|
||||
@VisibleForTesting static final String KEY_UNRESTRICTED_PREF = "unrestricted_preference";
|
||||
|
||||
@VisibleForTesting BatteryOptimizeUtils mBatteryOptimizeUtils;
|
||||
|
||||
public UnrestrictedPreferenceController(Context context, int uid, String packageName) {
|
||||
super(context);
|
||||
mBatteryOptimizeUtils = new BatteryOptimizeUtils(context, uid, packageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
preference.setEnabled(mBatteryOptimizeUtils.isSelectorPreferenceEnabled());
|
||||
|
||||
final boolean isUnrestricted =
|
||||
mBatteryOptimizeUtils.getAppOptimizationMode()
|
||||
== BatteryOptimizeUtils.MODE_UNRESTRICTED;
|
||||
((SelectorWithWidgetPreference) preference).setChecked(isUnrestricted);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_UNRESTRICTED_PREF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
return getPreferenceKey().equals(preference.getKey());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user