1 /*
2  * Copyright (C) 2017 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 package com.android.settings.display;
17 
18 import android.content.Context;
19 import android.hardware.display.AmbientDisplayConfiguration;
20 import android.os.SystemProperties;
21 import android.os.UserHandle;
22 import android.provider.Settings;
23 import android.text.TextUtils;
24 
25 import com.android.settings.core.TogglePreferenceController;
26 
27 public class AmbientDisplayAlwaysOnPreferenceController extends TogglePreferenceController {
28 
29     private final int ON = 1;
30     private final int OFF = 0;
31 
32     private static final int MY_USER = UserHandle.myUserId();
33     private static final String PROP_AWARE_AVAILABLE = "ro.vendor.aware_available";
34 
35     private AmbientDisplayConfiguration mConfig;
36     private OnPreferenceChangedCallback mCallback;
37 
38     public interface OnPreferenceChangedCallback {
onPreferenceChanged()39         void onPreferenceChanged();
40     }
41 
AmbientDisplayAlwaysOnPreferenceController(Context context, String key)42     public AmbientDisplayAlwaysOnPreferenceController(Context context, String key) {
43         super(context, key);
44     }
45 
46     @Override
getAvailabilityStatus()47     public int getAvailabilityStatus() {
48         return isAvailable(getConfig())
49                 && !SystemProperties.getBoolean(PROP_AWARE_AVAILABLE, false) ?
50                 AVAILABLE : UNSUPPORTED_ON_DEVICE;
51     }
52 
53     @Override
isSliceable()54     public boolean isSliceable() {
55         return TextUtils.equals(getPreferenceKey(), "ambient_display_always_on");
56     }
57 
58     @Override
isPublicSlice()59     public boolean isPublicSlice() {
60         return true;
61     }
62 
63     @Override
isChecked()64     public boolean isChecked() {
65         return getConfig().alwaysOnEnabled(MY_USER);
66     }
67 
68     @Override
setChecked(boolean isChecked)69     public boolean setChecked(boolean isChecked) {
70         int enabled = isChecked ? ON : OFF;
71         Settings.Secure.putInt(
72                 mContext.getContentResolver(), Settings.Secure.DOZE_ALWAYS_ON, enabled);
73         if (mCallback != null) {
74             mCallback.onPreferenceChanged();
75         }
76         return true;
77     }
78 
setConfig( AmbientDisplayConfiguration config)79     public AmbientDisplayAlwaysOnPreferenceController setConfig(
80             AmbientDisplayConfiguration config) {
81         mConfig = config;
82         return this;
83     }
84 
setCallback( OnPreferenceChangedCallback callback)85     public AmbientDisplayAlwaysOnPreferenceController setCallback(
86             OnPreferenceChangedCallback callback) {
87         mCallback = callback;
88         return this;
89     }
90 
isAvailable(AmbientDisplayConfiguration config)91     public static boolean isAvailable(AmbientDisplayConfiguration config) {
92         return config.alwaysOnAvailableForUser(MY_USER);
93     }
94 
getConfig()95     private AmbientDisplayConfiguration getConfig() {
96         if (mConfig == null) {
97             mConfig = new AmbientDisplayConfiguration(mContext);
98         }
99         return mConfig;
100     }
101 }
102