1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.fuelgauge; 16 17 import android.os.Bundle; 18 19 import androidx.annotation.VisibleForTesting; 20 import androidx.preference.Preference; 21 22 import com.android.settings.R; 23 import com.android.settings.Settings; 24 import com.android.settings.SettingsActivity; 25 import com.android.settings.applications.manageapplications.ManageApplications; 26 import com.android.settings.core.PreferenceControllerMixin; 27 import com.android.settings.core.SubSettingLauncher; 28 import com.android.settings.dashboard.DashboardFragment; 29 import com.android.settingslib.core.AbstractPreferenceController; 30 import com.android.settingslib.fuelgauge.PowerAllowlistBackend; 31 32 /** 33 * Controller that jumps to high power optimization fragment 34 */ 35 public class BatteryOptimizationPreferenceController extends AbstractPreferenceController 36 implements PreferenceControllerMixin { 37 38 private static final String KEY_BACKGROUND_ACTIVITY = "battery_optimization"; 39 40 41 private PowerAllowlistBackend mBackend; 42 private DashboardFragment mFragment; 43 private SettingsActivity mSettingsActivity; 44 private String mPackageName; 45 BatteryOptimizationPreferenceController(SettingsActivity settingsActivity, DashboardFragment fragment, String packageName)46 public BatteryOptimizationPreferenceController(SettingsActivity settingsActivity, 47 DashboardFragment fragment, String packageName) { 48 super(settingsActivity); 49 mFragment = fragment; 50 mSettingsActivity = settingsActivity; 51 mPackageName = packageName; 52 mBackend = PowerAllowlistBackend.getInstance(mSettingsActivity); 53 } 54 55 @VisibleForTesting BatteryOptimizationPreferenceController(SettingsActivity settingsActivity, DashboardFragment fragment, String packageName, PowerAllowlistBackend backend)56 BatteryOptimizationPreferenceController(SettingsActivity settingsActivity, 57 DashboardFragment fragment, String packageName, PowerAllowlistBackend backend) { 58 super(settingsActivity); 59 mFragment = fragment; 60 mSettingsActivity = settingsActivity; 61 mPackageName = packageName; 62 mBackend = backend; 63 } 64 65 @Override isAvailable()66 public boolean isAvailable() { 67 return true; 68 } 69 70 @Override updateState(Preference preference)71 public void updateState(Preference preference) { 72 mBackend.refreshList(); 73 final boolean isAllowlisted = mBackend.isAllowlisted(mPackageName); 74 preference.setSummary(isAllowlisted ? R.string.high_power_on : R.string.high_power_off); 75 } 76 77 @Override getPreferenceKey()78 public String getPreferenceKey() { 79 return KEY_BACKGROUND_ACTIVITY; 80 } 81 82 @Override handlePreferenceTreeClick(Preference preference)83 public boolean handlePreferenceTreeClick(Preference preference) { 84 if (!KEY_BACKGROUND_ACTIVITY.equals(preference.getKey())) { 85 return false; 86 } 87 88 final Bundle args = new Bundle(); 89 args.putString(ManageApplications.EXTRA_CLASSNAME, 90 Settings.HighPowerApplicationsActivity.class.getName()); 91 new SubSettingLauncher(mSettingsActivity) 92 .setDestination(ManageApplications.class.getName()) 93 .setArguments(args) 94 .setTitleRes(R.string.high_power_apps) 95 .setSourceMetricsCategory(mFragment.getMetricsCategory()) 96 .launch(); 97 98 return true; 99 } 100 101 } 102