1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.homepage.contextualcards.conditional; 18 19 import static com.android.settingslib.fuelgauge.BatterySaverLogging.SAVER_ENABLED_UNKNOWN; 20 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.os.PowerManager; 24 25 import com.android.settings.R; 26 import com.android.settings.core.SubSettingLauncher; 27 import com.android.settings.fuelgauge.BatterySaverReceiver; 28 import com.android.settings.fuelgauge.batterysaver.BatterySaverSettings; 29 import com.android.settings.homepage.contextualcards.ContextualCard; 30 import com.android.settingslib.fuelgauge.BatterySaverUtils; 31 32 import java.util.Objects; 33 34 public class BatterySaverConditionController implements ConditionalCardController, 35 BatterySaverReceiver.BatterySaverListener { 36 static final int ID = Objects.hash("BatterySaverConditionController"); 37 38 private final Context mAppContext; 39 private final ConditionManager mConditionManager; 40 private final BatterySaverReceiver mReceiver; 41 private final PowerManager mPowerManager; 42 BatterySaverConditionController(Context appContext, ConditionManager conditionManager)43 public BatterySaverConditionController(Context appContext, ConditionManager conditionManager) { 44 mAppContext = appContext; 45 mConditionManager = conditionManager; 46 mPowerManager = appContext.getSystemService(PowerManager.class); 47 mReceiver = new BatterySaverReceiver(appContext); 48 mReceiver.setBatterySaverListener(this); 49 } 50 51 @Override getId()52 public long getId() { 53 return ID; 54 } 55 56 @Override isDisplayable()57 public boolean isDisplayable() { 58 return mPowerManager.isPowerSaveMode(); 59 } 60 61 @Override onPrimaryClick(Context context)62 public void onPrimaryClick(Context context) { 63 new SubSettingLauncher(context) 64 .setDestination(BatterySaverSettings.class.getName()) 65 .setSourceMetricsCategory(SettingsEnums.DASHBOARD_SUMMARY) 66 .setTitleRes(R.string.battery_saver) 67 .launch(); 68 } 69 70 @Override onActionClick()71 public void onActionClick() { 72 BatterySaverUtils.setPowerSaveMode(mAppContext, false, 73 /*needFirstTimeWarning*/ false, SAVER_ENABLED_UNKNOWN); 74 } 75 76 @Override buildContextualCard()77 public ContextualCard buildContextualCard() { 78 return new ConditionalContextualCard.Builder() 79 .setConditionId(ID) 80 .setMetricsConstant(SettingsEnums.SETTINGS_CONDITION_BATTERY_SAVER) 81 .setActionText(mAppContext.getText(R.string.condition_turn_off)) 82 .setName(mAppContext.getPackageName() + "/" 83 + mAppContext.getText(R.string.condition_battery_title)) 84 .setTitleText(mAppContext.getText(R.string.condition_battery_title).toString()) 85 .setSummaryText(mAppContext.getText(R.string.condition_battery_summary).toString()) 86 .setIconDrawable(mAppContext.getDrawable(R.drawable.ic_battery_saver_accent_24dp)) 87 .setViewType(ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH) 88 .build(); 89 } 90 91 @Override startMonitoringStateChange()92 public void startMonitoringStateChange() { 93 mReceiver.setListening(true); 94 } 95 96 @Override stopMonitoringStateChange()97 public void stopMonitoringStateChange() { 98 mReceiver.setListening(false); 99 } 100 101 @Override onPowerSaveModeChanged()102 public void onPowerSaveModeChanged() { 103 mConditionManager.onConditionChanged(); 104 } 105 106 @Override onBatteryChanged(boolean pluggedIn)107 public void onBatteryChanged(boolean pluggedIn) { 108 109 } 110 } 111