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.content.Intent; 20 import android.os.UserHandle; 21 import android.provider.Settings; 22 import android.text.TextUtils; 23 24 import com.android.internal.hardware.AmbientDisplayConfiguration; 25 import com.android.settings.R; 26 import com.android.settings.core.TogglePreferenceController; 27 import com.android.settings.search.DatabaseIndexingUtils; 28 import com.android.settings.search.InlineSwitchPayload; 29 import com.android.settings.search.ResultPayload; 30 31 public class AmbientDisplayAlwaysOnPreferenceController extends TogglePreferenceController { 32 33 private final int ON = 1; 34 private final int OFF = 0; 35 36 private static final int MY_USER = UserHandle.myUserId(); 37 38 private AmbientDisplayConfiguration mConfig; 39 private OnPreferenceChangedCallback mCallback; 40 41 public interface OnPreferenceChangedCallback { onPreferenceChanged()42 void onPreferenceChanged(); 43 } 44 AmbientDisplayAlwaysOnPreferenceController(Context context, String key)45 public AmbientDisplayAlwaysOnPreferenceController(Context context, String key) { 46 super(context, key); 47 } 48 49 @Override getAvailabilityStatus()50 public int getAvailabilityStatus() { 51 if (mConfig == null) { 52 mConfig = new AmbientDisplayConfiguration(mContext); 53 } 54 return isAvailable(mConfig) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 55 } 56 57 @Override isSliceable()58 public boolean isSliceable() { 59 return TextUtils.equals(getPreferenceKey(), "ambient_display_always_on"); 60 } 61 62 @Override isChecked()63 public boolean isChecked() { 64 return mConfig.alwaysOnEnabled(MY_USER); 65 } 66 67 @Override setChecked(boolean isChecked)68 public boolean setChecked(boolean isChecked) { 69 int enabled = isChecked ? ON : OFF; 70 Settings.Secure.putInt( 71 mContext.getContentResolver(), Settings.Secure.DOZE_ALWAYS_ON, enabled); 72 if (mCallback != null) { 73 mCallback.onPreferenceChanged(); 74 } 75 return true; 76 } 77 setConfig( AmbientDisplayConfiguration config)78 public AmbientDisplayAlwaysOnPreferenceController setConfig( 79 AmbientDisplayConfiguration config) { 80 mConfig = config; 81 return this; 82 } 83 setCallback( OnPreferenceChangedCallback callback)84 public AmbientDisplayAlwaysOnPreferenceController setCallback( 85 OnPreferenceChangedCallback callback) { 86 mCallback = callback; 87 return this; 88 } 89 isAlwaysOnEnabled(AmbientDisplayConfiguration config)90 public static boolean isAlwaysOnEnabled(AmbientDisplayConfiguration config) { 91 return config.alwaysOnEnabled(MY_USER); 92 } 93 isAvailable(AmbientDisplayConfiguration config)94 public static boolean isAvailable(AmbientDisplayConfiguration config) { 95 return config.alwaysOnAvailableForUser(MY_USER); 96 } 97 accessibilityInversionEnabled(AmbientDisplayConfiguration config)98 public static boolean accessibilityInversionEnabled(AmbientDisplayConfiguration config) { 99 return config.accessibilityInversionEnabled(MY_USER); 100 } 101 102 @Override getResultPayload()103 public ResultPayload getResultPayload() { 104 final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext, 105 AmbientDisplaySettings.class.getName(), getPreferenceKey(), 106 mContext.getString(R.string.ambient_display_screen_title)); 107 108 return new InlineSwitchPayload(Settings.Secure.DOZE_ALWAYS_ON, 109 ResultPayload.SettingsSource.SECURE, ON /* onValue */, intent, isAvailable(), 110 ON /* defaultValue */); 111 } 112 } 113