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