• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29     private final String SYSTEM_KEY = ADAPTIVE_SLEEP;
30     private final int DEFAULT_VALUE = 0;
31 
32     final boolean hasSufficientPermissions;
33 
AdaptiveSleepPreferenceController(Context context, String key)34     public AdaptiveSleepPreferenceController(Context context, String key) {
35         super(context, key);
36 
37         final PackageManager packageManager = mContext.getPackageManager();
38         final String attentionPackage = packageManager.getAttentionServicePackageName();
39         hasSufficientPermissions = attentionPackage != null && packageManager.checkPermission(
40                 Manifest.permission.CAMERA, attentionPackage) == PackageManager.PERMISSION_GRANTED;
41     }
42 
43     @Override
isChecked()44     public boolean isChecked() {
45         return hasSufficientPermissions && Settings.System.getInt(mContext.getContentResolver(),
46                 SYSTEM_KEY, DEFAULT_VALUE) != DEFAULT_VALUE;
47     }
48 
49 
50     @Override
setChecked(boolean isChecked)51     public boolean setChecked(boolean isChecked) {
52         Settings.System.putInt(mContext.getContentResolver(), SYSTEM_KEY,
53                 isChecked ? 1 : DEFAULT_VALUE);
54         return true;
55     }
56 
57     @Override
58     @AvailabilityStatus
getAvailabilityStatus()59     public int getAvailabilityStatus() {
60         return mContext.getResources().getBoolean(
61                 com.android.internal.R.bool.config_adaptive_sleep_available)
62                 ? AVAILABLE_UNSEARCHABLE
63                 : UNSUPPORTED_ON_DEVICE;
64     }
65 
66     @Override
getSummary()67     public CharSequence getSummary() {
68         return mContext.getText(isChecked()
69                 ? R.string.adaptive_sleep_summary_on
70                 : R.string.adaptive_sleep_summary_off);
71     }
72 }
73