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 static android.provider.Settings.Secure.DOZE_PULSE_ON_PICK_UP;
     20 
     21 import android.annotation.UserIdInt;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.SharedPreferences;
     25 import android.os.UserHandle;
     26 import android.provider.Settings;
     27 import android.support.annotation.VisibleForTesting;
     28 import android.text.TextUtils;
     29 
     30 import com.android.internal.hardware.AmbientDisplayConfiguration;
     31 import com.android.settings.R;
     32 import com.android.settings.search.DatabaseIndexingUtils;
     33 import com.android.settings.search.InlineSwitchPayload;
     34 import com.android.settings.search.ResultPayload;
     35 
     36 public class PickupGesturePreferenceController extends GesturePreferenceController {
     37 
     38     private final int ON = 1;
     39     private final int OFF = 0;
     40 
     41     private static final String PREF_KEY_VIDEO = "gesture_pick_up_video";
     42     private final String mPickUpPrefKey;
     43 
     44     private final String SECURE_KEY = DOZE_PULSE_ON_PICK_UP;
     45 
     46     private AmbientDisplayConfiguration mAmbientConfig;
     47     @UserIdInt
     48     private final int mUserId;
     49 
     50     public PickupGesturePreferenceController(Context context, String key) {
     51         super(context, key);
     52         mUserId = UserHandle.myUserId();
     53         mPickUpPrefKey = key;
     54     }
     55 
     56     public PickupGesturePreferenceController setConfig(AmbientDisplayConfiguration config) {
     57         mAmbientConfig = config;
     58         return this;
     59     }
     60 
     61     public static boolean isSuggestionComplete(Context context, SharedPreferences prefs) {
     62         AmbientDisplayConfiguration ambientConfig = new AmbientDisplayConfiguration(context);
     63         return prefs.getBoolean(PickupGestureSettings.PREF_KEY_SUGGESTION_COMPLETE, false)
     64                 || !ambientConfig.pulseOnPickupAvailable();
     65     }
     66 
     67     @Override
     68     public int getAvailabilityStatus() {
     69         if (mAmbientConfig == null) {
     70             mAmbientConfig = new AmbientDisplayConfiguration(mContext);
     71         }
     72 
     73         // No hardware support for Pickup Gesture
     74         if (!mAmbientConfig.dozePulsePickupSensorAvailable()) {
     75             return UNSUPPORTED_ON_DEVICE;
     76         }
     77 
     78         // Can't change Pickup Gesture when AOD is enabled.
     79         if (!mAmbientConfig.ambientDisplayAvailable()) {
     80             return DISABLED_DEPENDENT_SETTING;
     81         }
     82 
     83         return AVAILABLE;
     84     }
     85 
     86     @Override
     87     public boolean isSliceable() {
     88         return TextUtils.equals(getPreferenceKey(), "gesture_pick_up");
     89     }
     90 
     91     @Override
     92     protected String getVideoPrefKey() {
     93         return PREF_KEY_VIDEO;
     94     }
     95 
     96     @Override
     97     public boolean isChecked() {
     98         return mAmbientConfig.pulseOnPickupEnabled(mUserId);
     99     }
    100 
    101     @Override
    102     public String getPreferenceKey() {
    103         return mPickUpPrefKey;
    104     }
    105 
    106     @Override
    107     public boolean setChecked(boolean isChecked) {
    108         return Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY,
    109                 isChecked ? ON : OFF);
    110     }
    111 
    112     @Override
    113     public boolean canHandleClicks() {
    114         return pulseOnPickupCanBeModified();
    115     }
    116 
    117     @Override
    118     public ResultPayload getResultPayload() {
    119         final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
    120                 PickupGestureSettings.class.getName(), mPickUpPrefKey,
    121                 mContext.getString(R.string.display_settings));
    122 
    123         return new InlineSwitchPayload(SECURE_KEY, ResultPayload.SettingsSource.SECURE,
    124                 ON /* onValue */, intent, isAvailable(), ON /* defaultValue */);
    125     }
    126 
    127     @VisibleForTesting
    128     boolean pulseOnPickupCanBeModified() {
    129         return mAmbientConfig.pulseOnPickupCanBeModified(mUserId);
    130     }
    131 }
    132