1 /*
2  * Copyright (C) 2021 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.display;
18 
19 import android.content.Context;
20 import android.os.PowerManager;
21 
22 import androidx.annotation.VisibleForTesting;
23 import androidx.preference.PreferenceScreen;
24 
25 import com.android.settings.R;
26 import com.android.settingslib.widget.BannerMessagePreference;
27 
28 /**
29  * The controller of Screen attention's battery saver warning preference.
30  * The preference appears when Screen Attention feature is disabled by battery saver mode.
31  */
32 public class AdaptiveSleepBatterySaverPreferenceController {
33 
34     @VisibleForTesting
35     BannerMessagePreference mPreference;
36     private final PowerManager mPowerManager;
37     private final Context mContext;
38 
AdaptiveSleepBatterySaverPreferenceController(Context context)39     public AdaptiveSleepBatterySaverPreferenceController(Context context) {
40         mPowerManager = context.getSystemService(PowerManager.class);
41         mContext = context;
42     }
43 
44     /**
45      * Adds the controlled preference to the provided preference screen.
46      */
addToScreen(PreferenceScreen screen)47     public void addToScreen(PreferenceScreen screen) {
48         initializePreference();
49         screen.addPreference(mPreference);
50         updateVisibility();
51     }
52 
53     /**
54      * Need this because all controller tests use RoboElectric. No easy way to mock this service,
55      * so we mock the call we need
56      */
57     @VisibleForTesting
isPowerSaveMode()58     boolean isPowerSaveMode() {
59         return mPowerManager.isPowerSaveMode();
60     }
61 
62     /**
63      * Refreshes the visibility of the preference.
64      */
updateVisibility()65     public void updateVisibility() {
66         initializePreference();
67         mPreference.setVisible(isPowerSaveMode());
68     }
69 
initializePreference()70     private void initializePreference() {
71         if (mPreference == null) {
72             mPreference = new BannerMessagePreference(mContext);
73             mPreference.setTitle(R.string.ambient_camera_summary_battery_saver_on);
74             mPreference.setPositiveButtonText(R.string.disable_text);
75             mPreference.setPositiveButtonOnClickListener(
76                     p -> mPowerManager.setPowerSaveModeEnabled(false));
77         }
78     }
79 }
80