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