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 android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.os.PowerManager;
22 
23 import com.android.settings.R;
24 import com.android.settings.core.SubSettingLauncher;
25 import com.android.settings.fuelgauge.BatterySaverReceiver;
26 import com.android.settings.fuelgauge.batterysaver.BatterySaverSettings;
27 import com.android.settings.homepage.contextualcards.ContextualCard;
28 import com.android.settingslib.fuelgauge.BatterySaverUtils;
29 
30 import java.util.Objects;
31 
32 public class BatterySaverConditionController implements ConditionalCardController,
33         BatterySaverReceiver.BatterySaverListener {
34     static final int ID = Objects.hash("BatterySaverConditionController");
35 
36     private final Context mAppContext;
37     private final ConditionManager mConditionManager;
38     private final BatterySaverReceiver mReceiver;
39     private final PowerManager mPowerManager;
40 
BatterySaverConditionController(Context appContext, ConditionManager conditionManager)41     public BatterySaverConditionController(Context appContext, ConditionManager conditionManager) {
42         mAppContext = appContext;
43         mConditionManager = conditionManager;
44         mPowerManager = appContext.getSystemService(PowerManager.class);
45         mReceiver = new BatterySaverReceiver(appContext);
46         mReceiver.setBatterySaverListener(this);
47     }
48 
49     @Override
getId()50     public long getId() {
51         return ID;
52     }
53 
54     @Override
isDisplayable()55     public boolean isDisplayable() {
56         return mPowerManager.isPowerSaveMode();
57     }
58 
59     @Override
onPrimaryClick(Context context)60     public void onPrimaryClick(Context context) {
61         new SubSettingLauncher(context)
62                 .setDestination(BatterySaverSettings.class.getName())
63                 .setSourceMetricsCategory(SettingsEnums.DASHBOARD_SUMMARY)
64                 .setTitleRes(R.string.battery_saver)
65                 .launch();
66     }
67 
68     @Override
onActionClick()69     public void onActionClick() {
70         BatterySaverUtils.setPowerSaveMode(mAppContext, false,
71                 /*needFirstTimeWarning*/ false);
72     }
73 
74     @Override
buildContextualCard()75     public ContextualCard buildContextualCard() {
76         return new ConditionalContextualCard.Builder()
77                 .setConditionId(ID)
78                 .setMetricsConstant(SettingsEnums.SETTINGS_CONDITION_BATTERY_SAVER)
79                 .setActionText(mAppContext.getText(R.string.condition_turn_off))
80                 .setName(mAppContext.getPackageName() + "/"
81                         + mAppContext.getText(R.string.condition_battery_title))
82                 .setTitleText(mAppContext.getText(R.string.condition_battery_title).toString())
83                 .setSummaryText(mAppContext.getText(R.string.condition_battery_summary).toString())
84                 .setIconDrawable(mAppContext.getDrawable(R.drawable.ic_battery_saver_accent_24dp))
85                 .setViewType(ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH)
86                 .build();
87     }
88 
89     @Override
startMonitoringStateChange()90     public void startMonitoringStateChange() {
91         mReceiver.setListening(true);
92     }
93 
94     @Override
stopMonitoringStateChange()95     public void stopMonitoringStateChange() {
96         mReceiver.setListening(false);
97     }
98 
99     @Override
onPowerSaveModeChanged()100     public void onPowerSaveModeChanged() {
101         mConditionManager.onConditionChanged();
102     }
103 
104     @Override
onBatteryChanged(boolean pluggedIn)105     public void onBatteryChanged(boolean pluggedIn) {
106 
107     }
108 }
109