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.app.Fragment; 18 import android.os.Bundle; 19 import android.support.annotation.VisibleForTesting; 20 import android.support.v7.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; 26 import com.android.settings.core.PreferenceControllerMixin; 27 import com.android.settingslib.core.AbstractPreferenceController; 28 29 /** 30 * Controller that jumps to high power optimization fragment 31 */ 32 public class BatteryOptimizationPreferenceController extends AbstractPreferenceController 33 implements PreferenceControllerMixin { 34 35 private static final String KEY_BACKGROUND_ACTIVITY = "battery_optimization"; 36 37 38 private PowerWhitelistBackend mBackend; 39 private Fragment mFragment; 40 private SettingsActivity mSettingsActivity; 41 private String mPackageName; 42 BatteryOptimizationPreferenceController(SettingsActivity settingsActivity, Fragment fragment, String packageName)43 public BatteryOptimizationPreferenceController(SettingsActivity settingsActivity, 44 Fragment fragment, String packageName) { 45 super(settingsActivity); 46 mFragment = fragment; 47 mSettingsActivity = settingsActivity; 48 mPackageName = packageName; 49 mBackend = PowerWhitelistBackend.getInstance(); 50 } 51 52 @VisibleForTesting BatteryOptimizationPreferenceController(SettingsActivity settingsActivity, Fragment fragment, String packageName, PowerWhitelistBackend backend)53 BatteryOptimizationPreferenceController(SettingsActivity settingsActivity, 54 Fragment fragment, String packageName, PowerWhitelistBackend backend) { 55 super(settingsActivity); 56 mFragment = fragment; 57 mSettingsActivity = settingsActivity; 58 mPackageName = packageName; 59 mBackend = backend; 60 } 61 62 @Override isAvailable()63 public boolean isAvailable() { 64 return true; 65 } 66 67 @Override updateState(Preference preference)68 public void updateState(Preference preference) { 69 final boolean isWhitelisted = mBackend.isWhitelisted(mPackageName); 70 preference.setSummary(isWhitelisted ? R.string.high_power_on : R.string.high_power_off); 71 } 72 73 @Override getPreferenceKey()74 public String getPreferenceKey() { 75 return KEY_BACKGROUND_ACTIVITY; 76 } 77 78 @Override handlePreferenceTreeClick(Preference preference)79 public boolean handlePreferenceTreeClick(Preference preference) { 80 if (!KEY_BACKGROUND_ACTIVITY.equals(preference.getKey())) { 81 return false; 82 } 83 84 Bundle args = new Bundle(1); 85 args.putString(ManageApplications.EXTRA_CLASSNAME, 86 Settings.HighPowerApplicationsActivity.class.getName()); 87 mSettingsActivity.startPreferencePanel(mFragment, ManageApplications.class.getName(), args, 88 R.string.high_power_apps, null, null, 0); 89 return true; 90 } 91 92 } 93