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 android.content.Context; 17 import android.content.Intent; 18 import android.os.UserHandle; 19 import android.provider.Settings; 20 import android.support.annotation.VisibleForTesting; 21 import android.support.v14.preference.SwitchPreference; 22 import android.support.v7.preference.Preference; 23 24 import com.android.internal.hardware.AmbientDisplayConfiguration; 25 import com.android.settings.R; 26 import com.android.settings.core.PreferenceControllerMixin; 27 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 28 import com.android.settings.search.DatabaseIndexingUtils; 29 import com.android.settings.search.InlineSwitchPayload; 30 import com.android.settings.search.ResultPayload; 31 import com.android.settingslib.core.AbstractPreferenceController; 32 33 import static android.provider.Settings.Secure.DOZE_ENABLED; 34 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_AMBIENT_DISPLAY; 35 36 public class AmbientDisplayNotificationsPreferenceController extends 37 AbstractPreferenceController implements PreferenceControllerMixin, 38 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 final AmbientDisplayConfiguration mConfig; 49 50 public AmbientDisplayNotificationsPreferenceController(Context context, 51 AmbientDisplayConfiguration config, MetricsFeatureProvider metricsFeatureProvider) { 52 super(context); 53 mMetricsFeatureProvider = metricsFeatureProvider; 54 mConfig = config; 55 } 56 57 @Override 58 public String getPreferenceKey() { 59 return KEY_AMBIENT_DISPLAY_NOTIFICATIONS; 60 } 61 62 @Override 63 public boolean handlePreferenceTreeClick(Preference preference) { 64 if (KEY_AMBIENT_DISPLAY_NOTIFICATIONS.equals(preference.getKey())) { 65 mMetricsFeatureProvider.action(mContext, ACTION_AMBIENT_DISPLAY); 66 } 67 return false; 68 } 69 70 @Override 71 public void updateState(Preference preference) { 72 ((SwitchPreference) preference).setChecked(mConfig.pulseOnNotificationEnabled(MY_USER)); 73 } 74 75 @Override 76 public boolean onPreferenceChange(Preference preference, Object newValue) { 77 boolean value = (Boolean) newValue; 78 Settings.Secure.putInt(mContext.getContentResolver(), DOZE_ENABLED, value ? ON : OFF); 79 return true; 80 } 81 82 @Override 83 public boolean isAvailable() { 84 return mConfig.pulseOnNotificationAvailable(); 85 } 86 87 @Override 88 public ResultPayload getResultPayload() { 89 final Intent intent = DatabaseIndexingUtils.buildSubsettingIntent(mContext, 90 AmbientDisplaySettings.class.getName(), KEY_AMBIENT_DISPLAY_NOTIFICATIONS, 91 mContext.getString(R.string.ambient_display_screen_title)); 92 93 return new InlineSwitchPayload(Settings.Secure.DOZE_ENABLED, 94 ResultPayload.SettingsSource.SECURE, ON /* onValue */, intent, isAvailable(), 95 ON /* defaultValue */); 96 } 97 } 98