1 /* 2 * Copyright (C) 2011 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; 18 19 import static android.view.HapticFeedbackConstants.CLOCK_TICK; 20 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.database.ContentObserver; 24 import android.hardware.input.InputManager; 25 import android.os.Handler; 26 import android.os.Parcel; 27 import android.os.Parcelable; 28 import android.provider.Settings; 29 import android.util.AttributeSet; 30 import android.view.View; 31 import android.widget.SeekBar; 32 33 public class PointerSpeedPreference extends SeekBarDialogPreference implements 34 SeekBar.OnSeekBarChangeListener { 35 private final InputManager mIm; 36 private SeekBar mSeekBar; 37 38 private int mOldSpeed; 39 private boolean mRestoredOldState; 40 41 private boolean mTouchInProgress; 42 private int mLastProgress = -1; 43 44 private ContentObserver mSpeedObserver = new ContentObserver(new Handler()) { 45 @Override 46 public void onChange(boolean selfChange) { 47 onSpeedChanged(); 48 } 49 }; 50 PointerSpeedPreference(Context context, AttributeSet attrs)51 public PointerSpeedPreference(Context context, AttributeSet attrs) { 52 super(context, attrs); 53 mIm = (InputManager)getContext().getSystemService(Context.INPUT_SERVICE); 54 } 55 56 @Override onClick()57 protected void onClick() { 58 super.onClick(); 59 60 getContext().getContentResolver().registerContentObserver( 61 Settings.System.getUriFor(Settings.System.POINTER_SPEED), true, 62 mSpeedObserver); 63 64 mRestoredOldState = false; 65 } 66 67 @Override onBindDialogView(View view)68 protected void onBindDialogView(View view) { 69 super.onBindDialogView(view); 70 71 mSeekBar = getSeekBar(view); 72 mSeekBar.setMax(InputManager.MAX_POINTER_SPEED - InputManager.MIN_POINTER_SPEED); 73 mOldSpeed = mIm.getPointerSpeed(getContext()); 74 mSeekBar.setProgress(mOldSpeed - InputManager.MIN_POINTER_SPEED); 75 mSeekBar.setOnSeekBarChangeListener(this); 76 mSeekBar.setContentDescription(getTitle()); 77 } 78 onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch)79 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { 80 if (!mTouchInProgress) { 81 mIm.tryPointerSpeed(progress + InputManager.MIN_POINTER_SPEED); 82 } 83 if (progress != mLastProgress) { 84 seekBar.performHapticFeedback(CLOCK_TICK); 85 mLastProgress = progress; 86 } 87 } 88 onStartTrackingTouch(SeekBar seekBar)89 public void onStartTrackingTouch(SeekBar seekBar) { 90 mTouchInProgress = true; 91 } 92 onStopTrackingTouch(SeekBar seekBar)93 public void onStopTrackingTouch(SeekBar seekBar) { 94 mTouchInProgress = false; 95 mIm.tryPointerSpeed(seekBar.getProgress() + InputManager.MIN_POINTER_SPEED); 96 } 97 onSpeedChanged()98 private void onSpeedChanged() { 99 int speed = mIm.getPointerSpeed(getContext()); 100 mSeekBar.setProgress(speed - InputManager.MIN_POINTER_SPEED); 101 } 102 103 @Override onDialogClosed(boolean positiveResult)104 protected void onDialogClosed(boolean positiveResult) { 105 super.onDialogClosed(positiveResult); 106 107 final ContentResolver resolver = getContext().getContentResolver(); 108 109 if (positiveResult) { 110 mIm.setPointerSpeed(getContext(), 111 mSeekBar.getProgress() + InputManager.MIN_POINTER_SPEED); 112 } else { 113 restoreOldState(); 114 } 115 116 resolver.unregisterContentObserver(mSpeedObserver); 117 } 118 restoreOldState()119 private void restoreOldState() { 120 if (mRestoredOldState) return; 121 122 mIm.tryPointerSpeed(mOldSpeed); 123 mRestoredOldState = true; 124 } 125 126 @Override onSaveInstanceState()127 protected Parcelable onSaveInstanceState() { 128 final Parcelable superState = super.onSaveInstanceState(); 129 if (getDialog() == null || !getDialog().isShowing()) return superState; 130 131 // Save the dialog state 132 final SavedState myState = new SavedState(superState); 133 myState.progress = mSeekBar.getProgress(); 134 myState.oldSpeed = mOldSpeed; 135 136 // Restore the old state when the activity or dialog is being paused 137 restoreOldState(); 138 return myState; 139 } 140 141 @Override onRestoreInstanceState(Parcelable state)142 protected void onRestoreInstanceState(Parcelable state) { 143 if (state == null || !state.getClass().equals(SavedState.class)) { 144 // Didn't save state for us in onSaveInstanceState 145 super.onRestoreInstanceState(state); 146 return; 147 } 148 149 SavedState myState = (SavedState) state; 150 super.onRestoreInstanceState(myState.getSuperState()); 151 mOldSpeed = myState.oldSpeed; 152 mIm.tryPointerSpeed(myState.progress + InputManager.MIN_POINTER_SPEED); 153 } 154 155 private static class SavedState extends BaseSavedState { 156 int progress; 157 int oldSpeed; 158 SavedState(Parcel source)159 public SavedState(Parcel source) { 160 super(source); 161 progress = source.readInt(); 162 oldSpeed = source.readInt(); 163 } 164 165 @Override writeToParcel(Parcel dest, int flags)166 public void writeToParcel(Parcel dest, int flags) { 167 super.writeToParcel(dest, flags); 168 dest.writeInt(progress); 169 dest.writeInt(oldSpeed); 170 } 171 SavedState(Parcelable superState)172 public SavedState(Parcelable superState) { 173 super(superState); 174 } 175 176 public static final Parcelable.Creator<SavedState> CREATOR = 177 new Parcelable.Creator<SavedState>() { 178 179 public SavedState createFromParcel(Parcel in) { 180 return new SavedState(in); 181 } 182 183 public SavedState[] newArray(int size) { 184 return new SavedState[size]; 185 } 186 }; 187 } 188 } 189 190