Home | History | Annotate | Download | only in gestures
      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.ContentResolver;
     20 import android.content.Context;
     21 import android.database.ContentObserver;
     22 import android.net.Uri;
     23 import android.provider.Settings;
     24 import android.support.v7.preference.Preference;
     25 import android.support.v7.preference.PreferenceScreen;
     26 
     27 import com.android.settings.SeekBarPreference;
     28 import com.android.settings.core.PreferenceController;
     29 import com.android.settings.core.lifecycle.Lifecycle;
     30 import com.android.settings.core.lifecycle.LifecycleObserver;
     31 import com.android.settings.core.lifecycle.events.OnPause;
     32 import com.android.settings.core.lifecycle.events.OnResume;
     33 import com.android.settings.overlay.FeatureFactory;
     34 
     35 public class AssistGestureSensitivityPreferenceController extends PreferenceController
     36         implements Preference.OnPreferenceChangeListener, LifecycleObserver, OnPause, OnResume {
     37 
     38     private static final String PREF_KEY_ASSIST_GESTURE_SENSITIVITY = "gesture_assist_sensitivity";
     39 
     40     private final AssistGestureFeatureProvider mFeatureProvider;
     41     private final SettingObserver mSettingObserver;
     42 
     43     private PreferenceScreen mScreen;
     44     private SeekBarPreference mPreference;
     45 
     46     public AssistGestureSensitivityPreferenceController(Context context, Lifecycle lifecycle) {
     47         super(context);
     48         mFeatureProvider = FeatureFactory.getFactory(context).getAssistGestureFeatureProvider();
     49         mSettingObserver = new SettingObserver();
     50 
     51         if (lifecycle != null) {
     52             lifecycle.addObserver(this);
     53         }
     54     }
     55 
     56     @Override
     57     public void onResume() {
     58         mSettingObserver.register(mContext.getContentResolver(), true /* register */);
     59         updatePreference();
     60     }
     61 
     62     @Override
     63     public void onPause() {
     64         mSettingObserver.register(mContext.getContentResolver(), false /* register */);
     65     }
     66 
     67     @Override
     68     public boolean isAvailable() {
     69         // The sensitivity control is contingent on the assist gesture being supported and the
     70         // gesture being enabled.
     71         final int gestureEnabled = Settings.Secure.getInt(
     72                 mContext.getContentResolver(),
     73                 Settings.Secure.ASSIST_GESTURE_ENABLED,
     74                 1);
     75         return (gestureEnabled == 1) && mFeatureProvider.isSupported(mContext);
     76     }
     77 
     78     @Override
     79     public void displayPreference(PreferenceScreen screen) {
     80         mScreen = screen;
     81         mPreference = (SeekBarPreference) screen.findPreference(getPreferenceKey());
     82         // Call super last or AbstractPreferenceController might remove the preference from the
     83         // screen (if !isAvailable()) before we can save a reference to it.
     84         super.displayPreference(screen);
     85     }
     86 
     87     @Override
     88     public void updateState(Preference preference) {
     89         super.updateState(preference);
     90         updatePreference();
     91     }
     92 
     93     private void updatePreference() {
     94         if (mPreference == null) {
     95             return;
     96         }
     97 
     98         if (isAvailable()) {
     99             if (mScreen.findPreference(getPreferenceKey()) == null) {
    100                 mScreen.addPreference(mPreference);
    101             }
    102         } else {
    103             mScreen.removePreference(mPreference);
    104         }
    105 
    106         final int sensitivity = Settings.Secure.getInt(
    107                 mContext.getContentResolver(),
    108                 Settings.Secure.ASSIST_GESTURE_SENSITIVITY,
    109                 mPreference.getProgress());
    110         mPreference.setProgress(sensitivity);
    111     }
    112 
    113     @Override
    114     public boolean onPreferenceChange(Preference preference, Object newValue) {
    115         final int sensitivity = (int) newValue;
    116         Settings.Secure.putInt(mContext.getContentResolver(),
    117                 Settings.Secure.ASSIST_GESTURE_SENSITIVITY, sensitivity);
    118         return true;
    119     }
    120 
    121     @Override
    122     public String getPreferenceKey() {
    123         return PREF_KEY_ASSIST_GESTURE_SENSITIVITY;
    124     }
    125 
    126     class SettingObserver extends ContentObserver {
    127 
    128         private final Uri ASSIST_GESTURE_ENABLED_URI =
    129                 Settings.Secure.getUriFor(Settings.Secure.ASSIST_GESTURE_ENABLED);
    130         private final Uri ASSIST_GESTURE_SENSITIVITY_URI =
    131                 Settings.Secure.getUriFor(Settings.Secure.ASSIST_GESTURE_SENSITIVITY);
    132 
    133         public SettingObserver() {
    134             super(null /* handler */);
    135         }
    136 
    137         public void register(ContentResolver cr, boolean register) {
    138             if (register) {
    139                 cr.registerContentObserver(ASSIST_GESTURE_ENABLED_URI, false, this);
    140                 cr.registerContentObserver(ASSIST_GESTURE_SENSITIVITY_URI, false, this);
    141             } else {
    142                 cr.unregisterContentObserver(this);
    143             }
    144         }
    145 
    146         @Override
    147         public void onChange(boolean selfChange) {
    148             updatePreference();
    149         }
    150     }
    151 }
    152