1 /*
2  * Copyright (C) 2020 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.gestures;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.provider.Settings;
22 
23 import com.android.settings.R;
24 import com.android.settings.core.BasePreferenceController;
25 
26 public class PowerMenuPreferenceController extends BasePreferenceController {
27 
28     private static final String KEY = "gesture_power_menu_summary";
29     private static final String CONTROLS_ENABLED_SETTING = Settings.Secure.CONTROLS_ENABLED;
30     private static final String CARDS_ENABLED_SETTING =
31             Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED;
32     private static final String CARDS_AVAILABLE_SETTING =
33             Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE;
34 
PowerMenuPreferenceController(Context context, String key)35     public PowerMenuPreferenceController(Context context, String key) {
36         super(context, key);
37     }
38 
39     @Override
getSummary()40     public CharSequence getSummary() {
41         boolean controlsVisible = isControlsAvailable()
42                 && Settings.Secure.getInt(mContext.getContentResolver(),
43                         CONTROLS_ENABLED_SETTING, 1) == 1;
44         boolean cardsVisible = isCardsAvailable()
45                 && Settings.Secure.getInt(mContext.getContentResolver(),
46                         CARDS_ENABLED_SETTING, 0) == 1;
47         if (controlsVisible && cardsVisible) {
48             return mContext.getText(R.string.power_menu_cards_passes_device_controls);
49         } else if (controlsVisible) {
50             return mContext.getText(R.string.power_menu_device_controls);
51         } else if (cardsVisible) {
52             return mContext.getText(R.string.power_menu_cards_passes);
53         } else {
54             return mContext.getText(R.string.power_menu_none);
55         }
56     }
57 
58     @Override
getAvailabilityStatus()59     public int getAvailabilityStatus() {
60         return isCardsAvailable() || isControlsAvailable() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
61     }
62 
isControlsAvailable()63     private boolean isControlsAvailable() {
64         return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CONTROLS);
65     }
66 
isCardsAvailable()67     private boolean isCardsAvailable() {
68         return Settings.Secure.getInt(mContext.getContentResolver(),
69                 CARDS_AVAILABLE_SETTING, 0) == 1;
70     }
71 }
72