• 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.Secure.ADAPTIVE_SLEEP;
17 
18 import android.Manifest;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ResolveInfo;
23 import android.provider.Settings;
24 import android.service.attention.AttentionService;
25 import android.text.TextUtils;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.TogglePreferenceController;
29 
30 
31 public class AdaptiveSleepPreferenceController extends TogglePreferenceController {
32     public static final String PREF_NAME = "adaptive_sleep";
33     private static final String SYSTEM_KEY = ADAPTIVE_SLEEP;
34     private static final int DEFAULT_VALUE = 0;
35 
AdaptiveSleepPreferenceController(Context context, String key)36     public AdaptiveSleepPreferenceController(Context context, String key) {
37         super(context, key);
38     }
39 
40     @Override
isChecked()41     public boolean isChecked() {
42         return hasSufficientPermission(mContext.getPackageManager()) && Settings.Secure.getInt(
43                 mContext.getContentResolver(), SYSTEM_KEY, DEFAULT_VALUE) != DEFAULT_VALUE;
44     }
45 
46     @Override
setChecked(boolean isChecked)47     public boolean setChecked(boolean isChecked) {
48         Settings.Secure.putInt(mContext.getContentResolver(), SYSTEM_KEY,
49                 isChecked ? 1 : DEFAULT_VALUE);
50         return true;
51     }
52 
53     @Override
54     @AvailabilityStatus
getAvailabilityStatus()55     public int getAvailabilityStatus() {
56         return isControllerAvailable(mContext);
57     }
58 
59     @Override
getSummary()60     public CharSequence getSummary() {
61         return mContext.getText(isChecked()
62                 ? R.string.adaptive_sleep_summary_on
63                 : R.string.adaptive_sleep_summary_off);
64     }
65 
isControllerAvailable(Context context)66     public static int isControllerAvailable(Context context) {
67         return context.getResources().getBoolean(
68                 com.android.internal.R.bool.config_adaptive_sleep_available)
69                 && isAttentionServiceAvailable(context)
70                 ? AVAILABLE_UNSEARCHABLE
71                 : UNSUPPORTED_ON_DEVICE;
72     }
73 
isAttentionServiceAvailable(Context context)74     private static boolean isAttentionServiceAvailable(Context context) {
75         final PackageManager packageManager = context.getPackageManager();
76         final String resolvePackage = packageManager.getAttentionServicePackageName();
77         if (TextUtils.isEmpty(resolvePackage)) {
78             return false;
79         }
80         final Intent intent = new Intent(AttentionService.SERVICE_INTERFACE).setPackage(
81                 resolvePackage);
82         final ResolveInfo resolveInfo = packageManager.resolveService(intent,
83                 PackageManager.MATCH_SYSTEM_ONLY);
84         return resolveInfo != null && resolveInfo.serviceInfo != null;
85     }
86 
hasSufficientPermission(PackageManager packageManager)87     static boolean hasSufficientPermission(PackageManager packageManager) {
88         final String attentionPackage = packageManager.getAttentionServicePackageName();
89         return attentionPackage != null && packageManager.checkPermission(
90                 Manifest.permission.CAMERA, attentionPackage) == PackageManager.PERMISSION_GRANTED;
91     }
92 }
93