Home | History | Annotate | Download | only in gestures
      1 /*
      2  * Copyright (C) 2016 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.content.SharedPreferences;
     21 import android.provider.Settings;
     22 import android.support.v7.preference.Preference;
     23 
     24 import com.android.settings.Utils;
     25 import com.android.settingslib.core.lifecycle.Lifecycle;
     26 
     27 import static android.provider.Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED;
     28 
     29 public class SwipeToNotificationPreferenceController extends GesturePreferenceController {
     30 
     31     private static final int ON = 1;
     32     private static final int OFF = 0;
     33 
     34     private static final String PREF_KEY_VIDEO = "gesture_swipe_down_fingerprint_video";
     35     private final String mSwipeDownFingerPrefKey;
     36 
     37     private static final String SECURE_KEY = SYSTEM_NAVIGATION_KEYS_ENABLED;
     38 
     39     public SwipeToNotificationPreferenceController(Context context, Lifecycle lifecycle,
     40             String key) {
     41         super(context, lifecycle);
     42         mSwipeDownFingerPrefKey = key;
     43     }
     44 
     45     public static boolean isSuggestionComplete(Context context, SharedPreferences prefs) {
     46         return !isGestureAvailable(context)
     47                 || prefs.getBoolean(SwipeToNotificationSettings.PREF_KEY_SUGGESTION_COMPLETE,
     48                         false);
     49     }
     50 
     51     private static boolean isGestureAvailable(Context context) {
     52         return Utils.hasFingerprintHardware(context) && context.getResources()
     53                 .getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys);
     54     }
     55 
     56     @Override
     57     public String getPreferenceKey() {
     58         return mSwipeDownFingerPrefKey;
     59     }
     60 
     61     @Override
     62     protected String getVideoPrefKey() {
     63         return PREF_KEY_VIDEO;
     64     }
     65 
     66     @Override
     67     public boolean isAvailable() {
     68         return SwipeToNotificationPreferenceController.isAvailable(mContext);
     69     }
     70 
     71     @Override
     72     public boolean onPreferenceChange(Preference preference, Object newValue) {
     73         setSwipeToNotification(mContext, (boolean) newValue);
     74         return true;
     75     }
     76 
     77     @Override
     78     protected boolean isSwitchPrefEnabled() {
     79         return isSwipeToNotificationOn(mContext);
     80     }
     81 
     82     public static boolean isSwipeToNotificationOn(Context context) {
     83         return Settings.Secure.getInt(context.getContentResolver(), SECURE_KEY, OFF) == ON;
     84     }
     85 
     86     public static boolean setSwipeToNotification(Context context, boolean isEnabled) {
     87         return Settings.Secure.putInt(
     88                 context.getContentResolver(), SECURE_KEY, isEnabled ? ON : OFF);
     89     }
     90 
     91     public static boolean isAvailable(Context context) {
     92         return isGestureAvailable(context);
     93     }
     94 }
     95