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.res.Resources;
     21 import android.hardware.Sensor;
     22 import android.hardware.SensorManager;
     23 import android.os.UserHandle;
     24 import android.os.UserManager;
     25 import android.provider.Settings;
     26 import android.support.annotation.VisibleForTesting;
     27 import android.support.v7.preference.Preference;
     28 import android.text.TextUtils;
     29 
     30 import com.android.settings.R;
     31 import com.android.settings.Utils;
     32 import com.android.settings.core.lifecycle.Lifecycle;
     33 
     34 public class DoubleTwistPreferenceController extends GesturePreferenceController {
     35 
     36     private static final String PREF_KEY_VIDEO = "gesture_double_twist_video";
     37     private final String mDoubleTwistPrefKey;
     38     private final UserManager mUserManager;
     39 
     40     public DoubleTwistPreferenceController(Context context, Lifecycle lifecycle, String key) {
     41         super(context, lifecycle);
     42         mDoubleTwistPrefKey = key;
     43         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
     44     }
     45 
     46     @Override
     47     public boolean isAvailable() {
     48         return hasSensor(R.string.gesture_double_twist_sensor_name,
     49                 R.string.gesture_double_twist_sensor_vendor);
     50     }
     51 
     52     @Override
     53     protected String getVideoPrefKey() {
     54         return PREF_KEY_VIDEO;
     55     }
     56 
     57     @Override
     58     public String getPreferenceKey() {
     59         return mDoubleTwistPrefKey;
     60     }
     61 
     62     @Override
     63     public boolean onPreferenceChange(Preference preference, Object newValue) {
     64         final int enabled = (boolean) newValue ? 1 : 0;
     65         Settings.Secure.putInt(mContext.getContentResolver(),
     66                 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled);
     67         final int managedProfileUserId = getManagedProfileUserId();
     68         if (managedProfileUserId != UserHandle.USER_NULL) {
     69             Settings.Secure.putIntForUser(mContext.getContentResolver(),
     70                 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled, managedProfileUserId);
     71         }
     72         return true;
     73     }
     74 
     75     @Override
     76     protected boolean isSwitchPrefEnabled() {
     77         final int doubleTwistEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
     78                 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1);
     79         return doubleTwistEnabled != 0;
     80     }
     81 
     82     @VisibleForTesting
     83     int getManagedProfileUserId() {
     84         return Utils.getManagedProfileId(mUserManager, UserHandle.myUserId());
     85     }
     86 
     87     private boolean hasSensor(int nameResId, int vendorResId) {
     88         final Resources resources = mContext.getResources();
     89         final String name = resources.getString(nameResId);
     90         final String vendor = resources.getString(vendorResId);
     91         if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(vendor)) {
     92             final SensorManager sensorManager =
     93                     (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
     94             for (Sensor s : sensorManager.getSensorList(Sensor.TYPE_ALL)) {
     95                 if (name.equals(s.getName()) && vendor.equals(s.getVendor())) {
     96                     return true;
     97                 }
     98             }
     99         }
    100         return false;
    101     }
    102 }
    103