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.content.res.Resources; 22 import android.hardware.Sensor; 23 import android.hardware.SensorManager; 24 import android.os.UserHandle; 25 import android.os.UserManager; 26 import android.provider.Settings; 27 import android.support.annotation.VisibleForTesting; 28 import android.text.TextUtils; 29 30 import com.android.settings.R; 31 import com.android.settings.Utils; 32 33 public class DoubleTwistPreferenceController extends GesturePreferenceController { 34 35 private final int ON = 1; 36 private final int OFF = 0; 37 38 private static final String PREF_KEY_VIDEO = "gesture_double_twist_video"; 39 private final String mDoubleTwistPrefKey; 40 private final UserManager mUserManager; 41 42 public DoubleTwistPreferenceController(Context context, String key) { 43 super(context, key); 44 mDoubleTwistPrefKey = key; 45 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 46 } 47 48 public static boolean isSuggestionComplete(Context context, SharedPreferences prefs) { 49 return !isGestureAvailable(context) 50 || prefs.getBoolean(DoubleTwistGestureSettings.PREF_KEY_SUGGESTION_COMPLETE, false); 51 } 52 53 public static boolean isGestureAvailable(Context context) { 54 final Resources resources = context.getResources(); 55 final String name = resources.getString(R.string.gesture_double_twist_sensor_name); 56 final String vendor = resources.getString(R.string.gesture_double_twist_sensor_vendor); 57 if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(vendor)) { 58 final SensorManager sensorManager = 59 (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); 60 for (Sensor s : sensorManager.getSensorList(Sensor.TYPE_ALL)) { 61 if (name.equals(s.getName()) && vendor.equals(s.getVendor())) { 62 return true; 63 } 64 } 65 } 66 return false; 67 } 68 69 @Override 70 public int getAvailabilityStatus() { 71 return isGestureAvailable(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 72 } 73 74 @Override 75 public boolean isSliceable() { 76 return TextUtils.equals(getPreferenceKey(), "gesture_double_twist"); 77 } 78 79 @Override 80 protected String getVideoPrefKey() { 81 return PREF_KEY_VIDEO; 82 } 83 84 @Override 85 public String getPreferenceKey() { 86 return mDoubleTwistPrefKey; 87 } 88 89 @Override 90 public boolean setChecked(boolean isChecked) { 91 setDoubleTwistPreference(mContext, mUserManager, isChecked ? ON : OFF); 92 return true; 93 } 94 95 public static void setDoubleTwistPreference(Context context, UserManager userManager, 96 int enabled) { 97 Settings.Secure.putInt(context.getContentResolver(), 98 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled); 99 final int managedProfileUserId = getManagedProfileId(userManager); 100 if (managedProfileUserId != UserHandle.USER_NULL) { 101 Settings.Secure.putIntForUser(context.getContentResolver(), 102 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled, 103 managedProfileUserId); 104 } 105 } 106 107 @Override 108 public boolean isChecked() { 109 final int doubleTwistEnabled = Settings.Secure.getInt(mContext.getContentResolver(), 110 Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, ON); 111 return doubleTwistEnabled != 0; 112 } 113 114 @VisibleForTesting 115 public static int getManagedProfileId(UserManager userManager) { 116 return Utils.getManagedProfileId(userManager, UserHandle.myUserId()); 117 } 118 } 119