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 17 package com.android.settings.gestures; 18 19 import static android.provider.Settings.Secure.ASSIST_GESTURE_ENABLED; 20 import static android.provider.Settings.Secure.ASSIST_GESTURE_SILENCE_ALERTS_ENABLED; 21 22 import android.content.Context; 23 import android.content.Intent; 24 import android.provider.Settings; 25 import android.support.v7.preference.Preference; 26 import android.support.v7.preference.PreferenceScreen; 27 import android.support.v7.preference.TwoStatePreference; 28 29 import com.android.internal.annotations.VisibleForTesting; 30 import com.android.settings.R; 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.lifecycle.Lifecycle; 36 import com.android.settingslib.core.lifecycle.events.OnResume; 37 38 public class AssistGestureSettingsPreferenceController extends GesturePreferenceController 39 implements OnResume { 40 41 private static final String PREF_KEY_VIDEO = "gesture_assist_video"; 42 43 private static final String SECURE_KEY_ASSIST = ASSIST_GESTURE_ENABLED; 44 private static final String SECURE_KEY_SILENCE = ASSIST_GESTURE_SILENCE_ALERTS_ENABLED; 45 private static final int ON = 1; 46 private static final int OFF = 0; 47 48 private final String mAssistGesturePrefKey; 49 private final AssistGestureFeatureProvider mFeatureProvider; 50 private boolean mWasAvailable; 51 52 private PreferenceScreen mScreen; 53 private Preference mPreference; 54 55 @VisibleForTesting 56 boolean mAssistOnly; 57 58 public AssistGestureSettingsPreferenceController(Context context, Lifecycle lifecycle, 59 String key, boolean assistOnly) { 60 super(context, lifecycle); 61 mFeatureProvider = FeatureFactory.getFactory(context).getAssistGestureFeatureProvider(); 62 mWasAvailable = isAvailable(); 63 mAssistGesturePrefKey = key; 64 mAssistOnly = assistOnly; 65 } 66 67 @Override 68 public boolean isAvailable() { 69 if (mAssistOnly) { 70 return mFeatureProvider.isSupported(mContext); 71 } else { 72 return mFeatureProvider.isSensorAvailable(mContext); 73 } 74 } 75 76 @Override 77 public void displayPreference(PreferenceScreen screen) { 78 mScreen = screen; 79 mPreference = screen.findPreference(getPreferenceKey()); 80 // Call super last or AbstractPreferenceController might remove the preference from the 81 // screen (if !isAvailable()) before we can save a reference to it. 82 super.displayPreference(screen); 83 } 84 85 @Override 86 public void onResume() { 87 if (mWasAvailable != isAvailable()) { 88 // Only update the preference visibility if the availability has changed -- otherwise 89 // the preference may be incorrectly added to screens with collapsed sections. 90 updatePreference(); 91 mWasAvailable = isAvailable(); 92 } 93 } 94 95 private void updatePreference() { 96 if (mPreference == null) { 97 return; 98 } 99 100 if (isAvailable()) { 101 if (mScreen.findPreference(getPreferenceKey()) == null) { 102 mScreen.addPreference(mPreference); 103 } 104 } else { 105 mScreen.removePreference(mPreference); 106 } 107 } 108 109 private boolean isAssistGestureEnabled() { 110 return Settings.Secure.getInt(mContext.getContentResolver(), 111 SECURE_KEY_ASSIST, ON) != 0; 112 } 113 114 private boolean isSilenceGestureEnabled() { 115 return Settings.Secure.getInt(mContext.getContentResolver(), 116 SECURE_KEY_SILENCE, ON) != 0; 117 } 118 119 @Override 120 public void updateState(Preference preference) { 121 boolean isEnabled = isAssistGestureEnabled() && mFeatureProvider.isSupported(mContext); 122 123 if (!mAssistOnly) { 124 isEnabled = isEnabled || isSilenceGestureEnabled(); 125 } 126 127 if (preference != null) { 128 if (preference instanceof TwoStatePreference) { 129 ((TwoStatePreference) preference).setChecked(isSwitchPrefEnabled()); 130 } else { 131 preference.setSummary(isEnabled 132 ? R.string.gesture_setting_on 133 : R.string.gesture_setting_off); 134 } 135 } 136 } 137 138 @Override 139 public boolean onPreferenceChange(Preference preference, Object newValue) { 140 final boolean enabled = (boolean) newValue; 141 Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY_ASSIST, 142 enabled ? ON : OFF); 143 updateState(preference); 144 return true; 145 } 146 147 @Override 148 protected String getVideoPrefKey() { 149 return PREF_KEY_VIDEO; 150 } 151 152 @Override 153 public String getPreferenceKey() { 154 return mAssistGesturePrefKey; 155 } 156 157 @Override 158 protected boolean isSwitchPrefEnabled() { 159 // Does nothing 160 return true; 161 } 162 163 @Override 164 public ResultPayload getResultPayload() { 165 final Intent intent = DatabaseIndexingUtils.buildSubsettingIntent(mContext, 166 AssistGestureSettings.class.getName(), mAssistGesturePrefKey, 167 mContext.getString(R.string.display_settings)); 168 169 return new InlineSwitchPayload(SECURE_KEY_ASSIST, ResultPayload.SettingsSource.SECURE, 170 ON /* onValue */, intent, isAvailable(), ON /* defaultValue */); 171 } 172 } 173