• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_AMBIENT_DISPLAY;
19 
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.UserHandle;
23 import android.provider.Settings;
24 import android.support.annotation.VisibleForTesting;
25 import android.support.v7.preference.Preference;
26 import android.text.TextUtils;
27 
28 import com.android.internal.hardware.AmbientDisplayConfiguration;
29 import com.android.settings.R;
30 import com.android.settings.core.TogglePreferenceController;
31 import com.android.settings.overlay.FeatureFactory;
32 import com.android.settings.search.DatabaseIndexingUtils;
33 import com.android.settings.search.InlineSwitchPayload;
34 import com.android.settings.search.ResultPayload;
35 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
36 
37 public class AmbientDisplayNotificationsPreferenceController extends
38         TogglePreferenceController implements Preference.OnPreferenceChangeListener {
39 
40     private final int ON = 1;
41     private final int OFF = 0;
42 
43     @VisibleForTesting
44     static final String KEY_AMBIENT_DISPLAY_NOTIFICATIONS = "ambient_display_notification";
45     private static final int MY_USER = UserHandle.myUserId();
46 
47     private final MetricsFeatureProvider mMetricsFeatureProvider;
48     private AmbientDisplayConfiguration mConfig;
49 
AmbientDisplayNotificationsPreferenceController(Context context, String key)50     public AmbientDisplayNotificationsPreferenceController(Context context, String key) {
51         super(context, key);
52         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
53     }
54 
55     /**
56      * Set AmbientDisplayConfiguration for this controller, please call in onAttach of fragment
57      *
58      * @param config AmbientDisplayConfiguration for this controller
59      */
setConfig( AmbientDisplayConfiguration config)60     public AmbientDisplayNotificationsPreferenceController setConfig(
61             AmbientDisplayConfiguration config) {
62         mConfig = config;
63         return this;
64     }
65 
66     @Override
handlePreferenceTreeClick(Preference preference)67     public boolean handlePreferenceTreeClick(Preference preference) {
68         if (KEY_AMBIENT_DISPLAY_NOTIFICATIONS.equals(preference.getKey())) {
69             mMetricsFeatureProvider.action(mContext, ACTION_AMBIENT_DISPLAY);
70         }
71         return false;
72     }
73 
74     @Override
isChecked()75     public boolean isChecked() {
76         return mConfig.pulseOnNotificationEnabled(MY_USER);
77     }
78 
79     @Override
setChecked(boolean isChecked)80     public boolean setChecked(boolean isChecked) {
81         Settings.Secure.putInt(mContext.getContentResolver(), DOZE_ENABLED, isChecked ? ON : OFF);
82         return true;
83     }
84 
85     @Override
getAvailabilityStatus()86     public int getAvailabilityStatus() {
87         if (mConfig == null) {
88             mConfig = new AmbientDisplayConfiguration(mContext);
89         }
90         return mConfig.pulseOnNotificationAvailable() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
91     }
92 
93     @Override
isSliceable()94     public boolean isSliceable() {
95         return TextUtils.equals(getPreferenceKey(), "ambient_display_notification");
96     }
97 
98     @Override
99     //TODO (b/69808376): Remove result payload
getResultPayload()100     public ResultPayload getResultPayload() {
101         final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
102                 AmbientDisplaySettings.class.getName(), KEY_AMBIENT_DISPLAY_NOTIFICATIONS,
103                 mContext.getString(R.string.ambient_display_screen_title));
104 
105         return new InlineSwitchPayload(Settings.Secure.DOZE_ENABLED,
106                 ResultPayload.SettingsSource.SECURE, ON /* onValue */, intent, isAvailable(),
107                 ON /* defaultValue */);
108     }
109 }
110