1 /*
2  * Copyright (C) 2019 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 package com.android.settings.display;
15 
16 import static android.provider.Settings.System.ADAPTIVE_SLEEP;
17 
18 import android.Manifest;
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.TogglePreferenceController;
25 
26 
27 public class AdaptiveSleepPreferenceController extends TogglePreferenceController {
28     public static final String PREF_NAME = "adaptive_sleep";
29     private static final String SYSTEM_KEY = ADAPTIVE_SLEEP;
30     private static final int DEFAULT_VALUE = 0;
31 
AdaptiveSleepPreferenceController(Context context, String key)32     public AdaptiveSleepPreferenceController(Context context, String key) {
33         super(context, key);
34     }
35 
36     @Override
isChecked()37     public boolean isChecked() {
38         return hasSufficientPermission(mContext.getPackageManager()) && Settings.System.getInt(
39                 mContext.getContentResolver(), SYSTEM_KEY, DEFAULT_VALUE) != DEFAULT_VALUE;
40     }
41 
42     @Override
setChecked(boolean isChecked)43     public boolean setChecked(boolean isChecked) {
44         Settings.System.putInt(mContext.getContentResolver(), SYSTEM_KEY,
45                 isChecked ? 1 : DEFAULT_VALUE);
46         return true;
47     }
48 
49     @Override
50     @AvailabilityStatus
getAvailabilityStatus()51     public int getAvailabilityStatus() {
52         return isControllerAvailable(mContext);
53     }
54 
55     @Override
getSummary()56     public CharSequence getSummary() {
57         return mContext.getText(isChecked()
58                 ? R.string.adaptive_sleep_summary_on
59                 : R.string.adaptive_sleep_summary_off);
60     }
61 
isControllerAvailable(Context context)62     public static int isControllerAvailable(Context context) {
63         return context.getResources().getBoolean(
64                 com.android.internal.R.bool.config_adaptive_sleep_available)
65                 ? AVAILABLE_UNSEARCHABLE
66                 : UNSUPPORTED_ON_DEVICE;
67     }
68 
hasSufficientPermission(PackageManager packageManager)69     static boolean hasSufficientPermission(PackageManager packageManager) {
70         final String attentionPackage = packageManager.getAttentionServicePackageName();
71         return attentionPackage != null && packageManager.checkPermission(
72                 Manifest.permission.CAMERA, attentionPackage) == PackageManager.PERMISSION_GRANTED;
73     }
74 }
75