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 android.content.Context; 20 import android.net.Uri; 21 import android.provider.Settings; 22 import android.support.v7.preference.Preference; 23 import android.support.v7.preference.PreferenceScreen; 24 25 import com.android.settings.applications.assist.AssistSettingObserver; 26 import com.android.settings.core.lifecycle.Lifecycle; 27 import com.android.settings.core.lifecycle.events.OnPause; 28 import com.android.settings.core.lifecycle.events.OnResume; 29 import com.android.settings.overlay.FeatureFactory; 30 31 import java.util.Arrays; 32 import java.util.List; 33 34 public class AssistGesturePreferenceController extends GesturePreferenceController 35 implements OnPause, OnResume { 36 37 private static final String PREF_KEY_VIDEO = "gesture_assist_video"; 38 private final String mAssistGesturePrefKey; 39 40 private final AssistGestureFeatureProvider mFeatureProvider; 41 private final SettingObserver mSettingObserver; 42 private boolean mWasAvailable; 43 44 private PreferenceScreen mScreen; 45 private Preference mPreference; 46 AssistGesturePreferenceController(Context context, Lifecycle lifecycle, String key)47 public AssistGesturePreferenceController(Context context, Lifecycle lifecycle, String key) { 48 super(context, lifecycle); 49 mFeatureProvider = FeatureFactory.getFactory(context).getAssistGestureFeatureProvider(); 50 mSettingObserver = new SettingObserver(); 51 mWasAvailable = isAvailable(); 52 mAssistGesturePrefKey = key; 53 } 54 55 @Override isAvailable()56 public boolean isAvailable() { 57 return mFeatureProvider.isSupported(mContext); 58 } 59 60 @Override displayPreference(PreferenceScreen screen)61 public void displayPreference(PreferenceScreen screen) { 62 mScreen = screen; 63 mPreference = screen.findPreference(getPreferenceKey()); 64 // Call super last or AbstractPreferenceController might remove the preference from the 65 // screen (if !isAvailable()) before we can save a reference to it. 66 super.displayPreference(screen); 67 } 68 69 @Override onResume()70 public void onResume() { 71 mSettingObserver.register(mContext.getContentResolver(), true /* register */); 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 80 @Override onPause()81 public void onPause() { 82 mSettingObserver.register(mContext.getContentResolver(), false /* register */); 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 99 @Override onPreferenceChange(Preference preference, Object newValue)100 public boolean onPreferenceChange(Preference preference, Object newValue) { 101 final boolean enabled = (boolean) newValue; 102 Settings.Secure.putInt(mContext.getContentResolver(), 103 Settings.Secure.ASSIST_GESTURE_ENABLED, enabled ? 1 : 0); 104 return true; 105 } 106 107 @Override getVideoPrefKey()108 protected String getVideoPrefKey() { 109 return PREF_KEY_VIDEO; 110 } 111 112 @Override getPreferenceKey()113 public String getPreferenceKey() { 114 return mAssistGesturePrefKey; 115 } 116 117 @Override isSwitchPrefEnabled()118 protected boolean isSwitchPrefEnabled() { 119 final int assistGestureEnabled = Settings.Secure.getInt(mContext.getContentResolver(), 120 Settings.Secure.ASSIST_GESTURE_ENABLED, 1); 121 return assistGestureEnabled != 0; 122 } 123 124 class SettingObserver extends AssistSettingObserver { 125 126 private final Uri ASSIST_GESTURE_ENABLED_URI = 127 Settings.Secure.getUriFor(Settings.Secure.ASSIST_GESTURE_ENABLED); 128 129 @Override getSettingUris()130 protected List<Uri> getSettingUris() { 131 return Arrays.asList(ASSIST_GESTURE_ENABLED_URI); 132 } 133 134 @Override onSettingChange()135 public void onSettingChange() { 136 if (mWasAvailable != isAvailable()) { 137 updatePreference(); 138 mWasAvailable = isAvailable(); 139 } 140 } 141 } 142 } 143