2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.gestures;
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.Build;
24 import android.os.Bundle;
25 import android.os.SystemProperties;
26 import android.provider.SearchIndexableResource;
27 import android.provider.Settings.Secure;
28 import android.support.v7.preference.Preference;
29 import android.support.v7.widget.RecyclerView;
30 import android.text.TextUtils;
31 import android.util.Log;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
36 import com.android.internal.logging.MetricsProto.MetricsEvent;
37 import com.android.settings.R;
38 import com.android.settings.search.BaseSearchIndexProvider;
39 import com.android.settings.search.Indexable;
40 import com.android.settings.SettingsPreferenceFragment;
42 import java.util.ArrayList;
43 import java.util.List;
46 * Top level fragment for gesture settings.
47 * This will create individual switch preference for each gesture and handle updates when each
48 * preference is updated
50 public class GestureSettings extends SettingsPreferenceFragment implements
51 Preference.OnPreferenceChangeListener, Indexable {
53 private static final String TAG = "GestureSettings";
54 private static final String PREF_KEY_DOUBLE_TAP_POWER = "gesture_double_tap_power";
55 private static final String PREF_KEY_DOUBLE_TWIST = "gesture_double_twist";
56 private static final String PREF_KEY_PICK_UP_AND_NUDGE = "gesture_pick_up_and_nudge";
57 private static final String PREF_KEY_SWIPE_DOWN_FINGERPRINT = "gesture_swipe_down_fingerprint";
58 private static final String DEBUG_DOZE_COMPONENT = "debug.doze.component";
59 private static final String ARG_SCROLL_TO_PREFERENCE = "gesture_scroll_to_preference";
61 private int mScrollPosition = -1;
64 public void onCreate(Bundle savedInstanceState) {
65 super.onCreate(savedInstanceState);
66 addPreferencesFromResource(R.xml.gesture_settings);
67 Context context = getActivity();
69 // Double tap power for camera
70 if (isCameraDoubleTapPowerGestureAvailable(getResources())) {
71 int cameraDisabled = Secure.getInt(
72 getContentResolver(), Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0);
73 GesturePreference preference =
74 (GesturePreference) findPreference(PREF_KEY_DOUBLE_TAP_POWER);
75 preference.setChecked(cameraDisabled == 0);
76 preference.setOnPreferenceChangeListener(this);
78 removePreference(PREF_KEY_DOUBLE_TAP_POWER);
82 if (isDozeAvailable(context)) {
83 GesturePreference preference =
84 (GesturePreference) findPreference(PREF_KEY_PICK_UP_AND_NUDGE);
85 int dozeEnabled = Secure.getInt(getContentResolver(), Secure.DOZE_ENABLED, 1);
86 preference.setChecked(dozeEnabled != 0);
87 preference.setOnPreferenceChangeListener(this);
89 removePreference(PREF_KEY_PICK_UP_AND_NUDGE);
92 // Fingerprint slide for notifications
93 if (isSystemUINavigationAvailable(context)) {
94 GesturePreference preference =
95 (GesturePreference) findPreference(PREF_KEY_SWIPE_DOWN_FINGERPRINT);
96 preference.setChecked(isSystemUINavigationEnabled(context));
97 preference.setOnPreferenceChangeListener(this);
99 removePreference(PREF_KEY_SWIPE_DOWN_FINGERPRINT);
102 // Double twist for camera mode
103 if (isDoubleTwistAvailable(context)) {
104 int doubleTwistEnabled = Secure.getInt(
105 getContentResolver(), Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1);
106 GesturePreference preference =
107 (GesturePreference) findPreference(PREF_KEY_DOUBLE_TWIST);
108 preference.setChecked(doubleTwistEnabled != 0);
109 preference.setOnPreferenceChangeListener(this);
111 removePreference(PREF_KEY_DOUBLE_TWIST);
114 if (savedInstanceState == null) {
115 final Bundle args = getArguments();
116 if (args != null && args.containsKey(ARG_SCROLL_TO_PREFERENCE)) {
117 String prefKey = args.getString(ARG_SCROLL_TO_PREFERENCE);
118 GesturePreference pref = (GesturePreference) findPreference(prefKey);
120 mScrollPosition = pref.getOrder();
128 public View onCreateView(LayoutInflater inflater, ViewGroup container,
129 Bundle savedInstanceState) {
130 View view = super.onCreateView(inflater, container, savedInstanceState);
131 if (mScrollPosition >= 0) {
132 getListView().scrollToPosition(mScrollPosition);
138 public boolean onPreferenceChange(Preference preference, Object newValue) {
139 boolean enabled = (boolean) newValue;
140 String key = preference.getKey();
141 if (PREF_KEY_DOUBLE_TAP_POWER.equals(key)) {
142 Secure.putInt(getContentResolver(),
143 Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, enabled ? 0 : 1);
144 } else if (PREF_KEY_PICK_UP_AND_NUDGE.equals(key)) {
145 Secure.putInt(getContentResolver(), Secure.DOZE_ENABLED, enabled ? 1 : 0);
146 } else if (PREF_KEY_SWIPE_DOWN_FINGERPRINT.equals(key)) {
147 Secure.putInt(getContentResolver(),
148 Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, enabled ? 1 : 0);
149 } else if (PREF_KEY_DOUBLE_TWIST.equals(key)) {
150 Secure.putInt(getContentResolver(),
151 Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled ? 1 : 0);
157 protected int getHelpResource() {
158 return R.string.help_url_gestures;
162 protected int getMetricsCategory() {
163 return MetricsEvent.SETTINGS_GESTURES;
166 private static boolean isCameraDoubleTapPowerGestureAvailable(Resources res) {
167 return res.getBoolean(
168 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
171 private static boolean isDozeAvailable(Context context) {
172 String name = Build.IS_DEBUGGABLE ? SystemProperties.get(DEBUG_DOZE_COMPONENT) : null;
173 if (TextUtils.isEmpty(name)) {
174 name = context.getResources().getString(
175 com.android.internal.R.string.config_dozeComponent);
177 return !TextUtils.isEmpty(name);
180 private static boolean isSystemUINavigationAvailable(Context context) {
181 return context.getResources().getBoolean(
182 com.android.internal.R.bool.config_supportSystemNavigationKeys);
185 private static boolean isSystemUINavigationEnabled(Context context) {
186 return Secure.getInt(context.getContentResolver(), Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, 1)
190 private static boolean isDoubleTwistAvailable(Context context) {
191 Resources resources = context.getResources();
192 String name = resources.getString(R.string.gesture_double_twist_sensor_name);
193 String vendor = resources.getString(R.string.gesture_double_twist_sensor_vendor);
194 if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(vendor)) {
195 SensorManager sensorManager =
196 (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
197 for (Sensor s : sensorManager.getSensorList(Sensor.TYPE_ALL)) {
198 if (name.equals(s.getName()) && vendor.equals(s.getVendor())) {
206 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
207 new BaseSearchIndexProvider() {
209 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
211 ArrayList<SearchIndexableResource> result =
212 new ArrayList<SearchIndexableResource>();
214 SearchIndexableResource sir = new SearchIndexableResource(context);
215 sir.xmlResId = R.xml.gesture_settings;
222 public List<String> getNonIndexableKeys(Context context) {
223 ArrayList<String> result = new ArrayList<String>();
224 if (!isCameraDoubleTapPowerGestureAvailable(context.getResources())) {
225 result.add(PREF_KEY_DOUBLE_TAP_POWER);
227 if (!isDozeAvailable(context)) {
228 result.add(PREF_KEY_PICK_UP_AND_NUDGE);
230 if (!isSystemUINavigationAvailable(context)) {
231 result.add(PREF_KEY_SWIPE_DOWN_FINGERPRINT);
233 if (!isDoubleTwistAvailable(context)) {
234 result.add(PREF_KEY_DOUBLE_TWIST);