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.settings.gestures; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.content.res.Resources; 22 import android.content.res.TypedArray; 23 import android.os.Bundle; 24 import android.provider.Settings; 25 import android.view.WindowManager; 26 27 import com.android.settings.R; 28 import com.android.settings.dashboard.DashboardFragment; 29 import com.android.settings.search.BaseSearchIndexProvider; 30 import com.android.settings.widget.LabeledSeekBarPreference; 31 import com.android.settings.widget.SeekBarPreference; 32 import com.android.settingslib.search.SearchIndexable; 33 34 /** 35 * A fragment to include all the settings related to Gesture Navigation mode. 36 */ 37 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) 38 public class GestureNavigationSettingsFragment extends DashboardFragment { 39 40 public static final String TAG = "GestureNavigationSettingsFragment"; 41 42 public static final String GESTURE_NAVIGATION_SETTINGS = 43 "com.android.settings.GESTURE_NAVIGATION_SETTINGS"; 44 45 private static final String LEFT_EDGE_SEEKBAR_KEY = "gesture_left_back_sensitivity"; 46 private static final String RIGHT_EDGE_SEEKBAR_KEY = "gesture_right_back_sensitivity"; 47 48 private WindowManager mWindowManager; 49 private BackGestureIndicatorView mIndicatorView; 50 51 private float[] mBackGestureInsetScales; 52 private float mDefaultBackGestureInset; 53 GestureNavigationSettingsFragment()54 public GestureNavigationSettingsFragment() { 55 super(); 56 } 57 58 @Override onCreate(Bundle savedInstanceState)59 public void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 62 mIndicatorView = new BackGestureIndicatorView(getActivity()); 63 mWindowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE); 64 } 65 66 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)67 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 68 super.onCreatePreferences(savedInstanceState, rootKey); 69 70 final Resources res = getActivity().getResources(); 71 mDefaultBackGestureInset = res.getDimensionPixelSize( 72 com.android.internal.R.dimen.config_backGestureInset); 73 mBackGestureInsetScales = getFloatArray(res.obtainTypedArray( 74 com.android.internal.R.array.config_backGestureInsetScales)); 75 76 initSeekBarPreference(LEFT_EDGE_SEEKBAR_KEY); 77 initSeekBarPreference(RIGHT_EDGE_SEEKBAR_KEY); 78 } 79 80 @Override onResume()81 public void onResume() { 82 super.onResume(); 83 84 mWindowManager.addView(mIndicatorView, mIndicatorView.getLayoutParams( 85 getActivity().getWindow().getAttributes())); 86 } 87 88 @Override onPause()89 public void onPause() { 90 super.onPause(); 91 92 mWindowManager.removeView(mIndicatorView); 93 } 94 95 @Override getPreferenceScreenResId()96 protected int getPreferenceScreenResId() { 97 return R.xml.gesture_navigation_settings; 98 } 99 100 @Override getHelpResource()101 public int getHelpResource() { 102 // TODO(b/146001201): Replace with gesture navigation help page when ready. 103 return R.string.help_uri_default; 104 } 105 106 @Override getLogTag()107 protected String getLogTag() { 108 return TAG; 109 } 110 111 @Override getMetricsCategory()112 public int getMetricsCategory() { 113 return SettingsEnums.SETTINGS_GESTURE_NAV_BACK_SENSITIVITY_DLG; 114 } 115 initSeekBarPreference(final String key)116 private void initSeekBarPreference(final String key) { 117 final LabeledSeekBarPreference pref = getPreferenceScreen().findPreference(key); 118 pref.setContinuousUpdates(true); 119 pref.setHapticFeedbackMode(SeekBarPreference.HAPTIC_FEEDBACK_MODE_ON_TICKS); 120 121 final String settingsKey = key == LEFT_EDGE_SEEKBAR_KEY 122 ? Settings.Secure.BACK_GESTURE_INSET_SCALE_LEFT 123 : Settings.Secure.BACK_GESTURE_INSET_SCALE_RIGHT; 124 final float initScale = Settings.Secure.getFloat( 125 getContext().getContentResolver(), settingsKey, 1.0f); 126 127 // Find the closest value to initScale 128 float minDistance = Float.MAX_VALUE; 129 int minDistanceIndex = -1; 130 for (int i = 0; i < mBackGestureInsetScales.length; i++) { 131 float d = Math.abs(mBackGestureInsetScales[i] - initScale); 132 if (d < minDistance) { 133 minDistance = d; 134 minDistanceIndex = i; 135 } 136 } 137 pref.setProgress(minDistanceIndex); 138 139 pref.setOnPreferenceChangeListener((p, v) -> { 140 final int width = (int) (mDefaultBackGestureInset * mBackGestureInsetScales[(int) v]); 141 mIndicatorView.setIndicatorWidth(width, key == LEFT_EDGE_SEEKBAR_KEY); 142 return true; 143 }); 144 145 pref.setOnPreferenceChangeStopListener((p, v) -> { 146 mIndicatorView.setIndicatorWidth(0, key == LEFT_EDGE_SEEKBAR_KEY); 147 final float scale = mBackGestureInsetScales[(int) v]; 148 Settings.Secure.putFloat(getContext().getContentResolver(), settingsKey, scale); 149 return true; 150 }); 151 } 152 getFloatArray(TypedArray array)153 private static float[] getFloatArray(TypedArray array) { 154 int length = array.length(); 155 float[] floatArray = new float[length]; 156 for (int i = 0; i < length; i++) { 157 floatArray[i] = array.getFloat(i, 1.0f); 158 } 159 array.recycle(); 160 return floatArray; 161 } 162 163 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 164 new BaseSearchIndexProvider(R.xml.gesture_navigation_settings) { 165 166 @Override 167 protected boolean isPageSearchEnabled(Context context) { 168 return SystemNavigationPreferenceController.isGestureAvailable(context); 169 } 170 }; 171 172 } 173