1 /*
2  * Copyright (C) 2016 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.DOZE_ENABLED;
17 
18 import android.app.settings.SettingsEnums;
19 import android.content.Context;
20 import android.hardware.display.AmbientDisplayConfiguration;
21 import android.os.UserHandle;
22 import android.provider.Settings;
23 import android.text.TextUtils;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.preference.Preference;
27 
28 import com.android.settings.core.TogglePreferenceController;
29 import com.android.settings.overlay.FeatureFactory;
30 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
31 
32 public class AmbientDisplayNotificationsPreferenceController extends
33         TogglePreferenceController implements Preference.OnPreferenceChangeListener {
34 
35     private final int ON = 1;
36     private final int OFF = 0;
37 
38     @VisibleForTesting
39     static final String KEY_AMBIENT_DISPLAY_NOTIFICATIONS = "ambient_display_notification";
40     private static final int MY_USER = UserHandle.myUserId();
41 
42     private final MetricsFeatureProvider mMetricsFeatureProvider;
43     private AmbientDisplayConfiguration mConfig;
44 
AmbientDisplayNotificationsPreferenceController(Context context, String key)45     public AmbientDisplayNotificationsPreferenceController(Context context, String key) {
46         super(context, key);
47         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
48     }
49 
50     /**
51      * Set AmbientDisplayConfiguration for this controller, please call in onAttach of fragment
52      *
53      * @param config AmbientDisplayConfiguration for this controller
54      */
setConfig( AmbientDisplayConfiguration config)55     public AmbientDisplayNotificationsPreferenceController setConfig(
56             AmbientDisplayConfiguration config) {
57         mConfig = config;
58         return this;
59     }
60 
61     @Override
handlePreferenceTreeClick(Preference preference)62     public boolean handlePreferenceTreeClick(Preference preference) {
63         if (KEY_AMBIENT_DISPLAY_NOTIFICATIONS.equals(preference.getKey())) {
64             mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_AMBIENT_DISPLAY);
65         }
66         return false;
67     }
68 
69     @Override
isChecked()70     public boolean isChecked() {
71         return getAmbientConfig().pulseOnNotificationEnabled(MY_USER);
72     }
73 
74     @Override
setChecked(boolean isChecked)75     public boolean setChecked(boolean isChecked) {
76         Settings.Secure.putInt(mContext.getContentResolver(), DOZE_ENABLED, isChecked ? ON : OFF);
77         return true;
78     }
79 
80     @Override
getAvailabilityStatus()81     public int getAvailabilityStatus() {
82         return getAmbientConfig().pulseOnNotificationAvailable()
83                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
84     }
85 
86     @Override
isSliceable()87     public boolean isSliceable() {
88         return TextUtils.equals(getPreferenceKey(), "ambient_display_notification");
89     }
90 
getAmbientConfig()91     private AmbientDisplayConfiguration getAmbientConfig() {
92         if (mConfig == null) {
93             mConfig = new AmbientDisplayConfiguration(mContext);
94         }
95 
96         return mConfig;
97     }
98 }
99