1 /* 2 * Copyright (C) 2019 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.car.developeroptions.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.provider.Settings; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.car.developeroptions.R; 30 import com.android.car.developeroptions.overlay.FeatureFactory; 31 32 public class AssistGestureSettingsPreferenceController extends GesturePreferenceController { 33 34 private static final String PREF_KEY_VIDEO = "gesture_assist_video"; 35 36 private static final String SECURE_KEY_ASSIST = ASSIST_GESTURE_ENABLED; 37 private static final String SECURE_KEY_SILENCE = ASSIST_GESTURE_SILENCE_ALERTS_ENABLED; 38 private static final int ON = 1; 39 private static final int OFF = 0; 40 41 private final AssistGestureFeatureProvider mFeatureProvider; 42 private boolean mWasAvailable; 43 44 private PreferenceScreen mScreen; 45 private Preference mPreference; 46 47 @VisibleForTesting 48 boolean mAssistOnly; 49 AssistGestureSettingsPreferenceController(Context context, String key)50 public AssistGestureSettingsPreferenceController(Context context, String key) { 51 super(context, key); 52 mFeatureProvider = FeatureFactory.getFactory(context).getAssistGestureFeatureProvider(); 53 mWasAvailable = isAvailable(); 54 } 55 56 @Override getAvailabilityStatus()57 public int getAvailabilityStatus() { 58 final boolean isAvailable = mAssistOnly ? mFeatureProvider.isSupported(mContext) 59 : mFeatureProvider.isSensorAvailable(mContext); 60 return isAvailable ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 61 } 62 63 @Override displayPreference(PreferenceScreen screen)64 public void displayPreference(PreferenceScreen screen) { 65 mScreen = screen; 66 mPreference = screen.findPreference(getPreferenceKey()); 67 super.displayPreference(screen); 68 } 69 70 @Override onResume()71 public void onResume() { 72 if (mWasAvailable != isAvailable()) { 73 // Only update the preference visibility if the availability has changed -- otherwise 74 // the preference may be incorrectly added to screens with collapsed sections. 75 updatePreference(); 76 mWasAvailable = isAvailable(); 77 } 78 } 79 setAssistOnly(boolean assistOnly)80 public AssistGestureSettingsPreferenceController setAssistOnly(boolean assistOnly) { 81 mAssistOnly = assistOnly; 82 return this; 83 } 84 updatePreference()85 private void updatePreference() { 86 if (mPreference == null) { 87 return; 88 } 89 90 if (isAvailable()) { 91 if (mScreen.findPreference(getPreferenceKey()) == null) { 92 mScreen.addPreference(mPreference); 93 } 94 } else { 95 mScreen.removePreference(mPreference); 96 } 97 } 98 isAssistGestureEnabled()99 private boolean isAssistGestureEnabled() { 100 return Settings.Secure.getInt(mContext.getContentResolver(), 101 SECURE_KEY_ASSIST, ON) != 0; 102 } 103 isSilenceGestureEnabled()104 private boolean isSilenceGestureEnabled() { 105 return Settings.Secure.getInt(mContext.getContentResolver(), 106 SECURE_KEY_SILENCE, ON) != 0; 107 } 108 109 @Override setChecked(boolean isChecked)110 public boolean setChecked(boolean isChecked) { 111 return Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY_ASSIST, 112 isChecked ? ON : OFF); 113 } 114 115 @Override getVideoPrefKey()116 protected String getVideoPrefKey() { 117 return PREF_KEY_VIDEO; 118 } 119 120 @Override getSummary()121 public CharSequence getSummary() { 122 boolean isEnabled = isAssistGestureEnabled() && mFeatureProvider.isSupported(mContext); 123 if (!mAssistOnly) { 124 isEnabled = isEnabled || isSilenceGestureEnabled(); 125 } 126 return mContext.getText( 127 isEnabled ? R.string.gesture_setting_on : R.string.gesture_setting_off); 128 } 129 130 @Override isChecked()131 public boolean isChecked() { 132 return Settings.Secure.getInt(mContext.getContentResolver(), SECURE_KEY_ASSIST, OFF) == ON; 133 } 134 } 135