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