diff options
| -rw-r--r-- | core/java/android/inputmethodservice/KeyboardView.java | 289 | ||||
| -rw-r--r-- | core/java/android/widget/AdapterViewFlipper.java | 30 | ||||
| -rw-r--r-- | core/java/android/widget/AnalogClock.java | 3 | ||||
| -rw-r--r-- | core/java/android/widget/Chronometer.java | 15 | ||||
| -rw-r--r-- | core/java/android/widget/DigitalClock.java | 14 | ||||
| -rw-r--r-- | core/java/android/widget/Editor.java | 15 | ||||
| -rw-r--r-- | core/java/android/widget/Gallery.java | 13 | ||||
| -rw-r--r-- | core/java/android/widget/MediaController.java | 43 | ||||
| -rw-r--r-- | core/java/android/widget/QuickContactBadge.java | 8 | ||||
| -rw-r--r-- | core/java/android/widget/SlidingDrawer.java | 26 | ||||
| -rw-r--r-- | core/java/android/widget/TextClock.java | 33 | ||||
| -rw-r--r-- | core/java/android/widget/ViewFlipper.java | 22 | ||||
| -rw-r--r-- | core/java/android/widget/ZoomButton.java | 7 |
13 files changed, 261 insertions, 257 deletions
diff --git a/core/java/android/inputmethodservice/KeyboardView.java b/core/java/android/inputmethodservice/KeyboardView.java index b777e8c6f389..dc2aa3a8bf3a 100644 --- a/core/java/android/inputmethodservice/KeyboardView.java +++ b/core/java/android/inputmethodservice/KeyboardView.java @@ -1,12 +1,12 @@ /* * Copyright (C) 2008-2009 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -57,7 +57,7 @@ import java.util.Map; /** * A view that renders a virtual {@link Keyboard}. It handles rendering of keys and * detecting key presses and touch movements. - * + * * @attr ref android.R.styleable#KeyboardView_keyBackground * @attr ref android.R.styleable#KeyboardView_keyPreviewLayout * @attr ref android.R.styleable#KeyboardView_keyPreviewOffset @@ -73,7 +73,7 @@ public class KeyboardView extends View implements View.OnClickListener { * Listener for virtual keyboard events. */ public interface OnKeyboardActionListener { - + /** * Called when the user presses a key. This is sent before the {@link #onKey} is called. * For keys that repeat, this is only called once. @@ -81,7 +81,7 @@ public class KeyboardView extends View implements View.OnClickListener { * key, the value will be zero. */ void onPress(int primaryCode); - + /** * Called when the user releases a key. This is sent after the {@link #onKey} is called. * For keys that repeat, this is only called once. @@ -106,22 +106,22 @@ public class KeyboardView extends View implements View.OnClickListener { * @param text the sequence of characters to be displayed. */ void onText(CharSequence text); - + /** * Called when the user quickly moves the finger from right to left. */ void swipeLeft(); - + /** * Called when the user quickly moves the finger from left to right. */ void swipeRight(); - + /** * Called when the user quickly moves the finger from up to down. */ void swipeDown(); - + /** * Called when the user quickly moves the finger from down to up. */ @@ -131,8 +131,8 @@ public class KeyboardView extends View implements View.OnClickListener { private static final boolean DEBUG = false; private static final int NOT_A_KEY = -1; private static final int[] KEY_DELETE = { Keyboard.KEYCODE_DELETE }; - private static final int[] LONG_PRESSABLE_STATE_SET = { R.attr.state_long_pressable }; - + private static final int[] LONG_PRESSABLE_STATE_SET = { R.attr.state_long_pressable }; + private Keyboard mKeyboard; private int mCurrentKeyIndex = NOT_A_KEY; private int mLabelTextSize; @@ -141,7 +141,7 @@ public class KeyboardView extends View implements View.OnClickListener { private float mShadowRadius; private int mShadowColor; private float mBackgroundDimAmount; - + private TextView mPreviewText; private PopupWindow mPreviewPopup; private int mPreviewTextSizeLarge; @@ -162,7 +162,7 @@ public class KeyboardView extends View implements View.OnClickListener { /** Listener for {@link OnKeyboardActionListener}. */ private OnKeyboardActionListener mKeyboardActionListener; - + private static final int MSG_SHOW_PREVIEW = 1; private static final int MSG_REMOVE_PREVIEW = 2; private static final int MSG_REPEAT = 3; @@ -171,7 +171,7 @@ public class KeyboardView extends View implements View.OnClickListener { private static final int DELAY_BEFORE_PREVIEW = 0; private static final int DELAY_AFTER_PREVIEW = 70; private static final int DEBOUNCE_TIME = 70; - + private int mVerticalCorrection; private int mProximityThreshold; @@ -187,10 +187,10 @@ public class KeyboardView extends View implements View.OnClickListener { private int mStartY; private boolean mProximityCorrectOn; - + private Paint mPaint; private Rect mPadding; - + private long mDownTime; private long mLastMoveTime; private int mLastKey; @@ -253,28 +253,7 @@ public class KeyboardView extends View implements View.OnClickListener { /** Whether the requirement of a headset to hear passwords if accessibility is enabled is announced. */ private boolean mHeadsetRequiredToHearPasswordsAnnounced; - Handler mHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - switch (msg.what) { - case MSG_SHOW_PREVIEW: - showKey(msg.arg1); - break; - case MSG_REMOVE_PREVIEW: - mPreviewText.setVisibility(INVISIBLE); - break; - case MSG_REPEAT: - if (repeatKey()) { - Message repeat = Message.obtain(this, MSG_REPEAT); - sendMessageDelayed(repeat, REPEAT_INTERVAL); - } - break; - case MSG_LONGPRESS: - openPopupIfRequired((MotionEvent) msg.obj); - break; - } - } - }; + Handler mHandler; public KeyboardView(Context context, AttributeSet attrs) { this(context, attrs, com.android.internal.R.attr.keyboardViewStyle); @@ -298,7 +277,7 @@ public class KeyboardView extends View implements View.OnClickListener { int keyTextSize = 0; int n = a.getIndexCount(); - + for (int i = 0; i < n; i++) { int attr = a.getIndex(i); @@ -338,7 +317,7 @@ public class KeyboardView extends View implements View.OnClickListener { break; } } - + a = mContext.obtainStyledAttributes( com.android.internal.R.styleable.Theme); mBackgroundDimAmount = a.getFloat(android.R.styleable.Theme_backgroundDimAmount, 0.5f); @@ -352,16 +331,16 @@ public class KeyboardView extends View implements View.OnClickListener { } else { mShowPreview = false; } - + mPreviewPopup.setTouchable(false); - + mPopupKeyboard = new PopupWindow(context); mPopupKeyboard.setBackgroundDrawable(null); //mPopupKeyboard.setClippingEnabled(false); - + mPopupParent = this; //mPredicting = true; - + mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setTextSize(keyTextSize); @@ -380,64 +359,94 @@ public class KeyboardView extends View implements View.OnClickListener { mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); resetMultiTap(); - initGestureDetector(); } + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + initGestureDetector(); + if (mHandler == null) { + mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_SHOW_PREVIEW: + showKey(msg.arg1); + break; + case MSG_REMOVE_PREVIEW: + mPreviewText.setVisibility(INVISIBLE); + break; + case MSG_REPEAT: + if (repeatKey()) { + Message repeat = Message.obtain(this, MSG_REPEAT); + sendMessageDelayed(repeat, REPEAT_INTERVAL); + } + break; + case MSG_LONGPRESS: + openPopupIfRequired((MotionEvent) msg.obj); + break; + } + } + }; + } + } private void initGestureDetector() { - mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { - @Override - public boolean onFling(MotionEvent me1, MotionEvent me2, - float velocityX, float velocityY) { - if (mPossiblePoly) return false; - final float absX = Math.abs(velocityX); - final float absY = Math.abs(velocityY); - float deltaX = me2.getX() - me1.getX(); - float deltaY = me2.getY() - me1.getY(); - int travelX = getWidth() / 2; // Half the keyboard width - int travelY = getHeight() / 2; // Half the keyboard height - mSwipeTracker.computeCurrentVelocity(1000); - final float endingVelocityX = mSwipeTracker.getXVelocity(); - final float endingVelocityY = mSwipeTracker.getYVelocity(); - boolean sendDownKey = false; - if (velocityX > mSwipeThreshold && absY < absX && deltaX > travelX) { - if (mDisambiguateSwipe && endingVelocityX < velocityX / 4) { - sendDownKey = true; - } else { - swipeRight(); - return true; - } - } else if (velocityX < -mSwipeThreshold && absY < absX && deltaX < -travelX) { - if (mDisambiguateSwipe && endingVelocityX > velocityX / 4) { - sendDownKey = true; - } else { - swipeLeft(); - return true; - } - } else if (velocityY < -mSwipeThreshold && absX < absY && deltaY < -travelY) { - if (mDisambiguateSwipe && endingVelocityY > velocityY / 4) { - sendDownKey = true; - } else { - swipeUp(); - return true; - } - } else if (velocityY > mSwipeThreshold && absX < absY / 2 && deltaY > travelY) { - if (mDisambiguateSwipe && endingVelocityY < velocityY / 4) { - sendDownKey = true; - } else { - swipeDown(); - return true; + if (mGestureDetector == null) { + mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { + @Override + public boolean onFling(MotionEvent me1, MotionEvent me2, + float velocityX, float velocityY) { + if (mPossiblePoly) return false; + final float absX = Math.abs(velocityX); + final float absY = Math.abs(velocityY); + float deltaX = me2.getX() - me1.getX(); + float deltaY = me2.getY() - me1.getY(); + int travelX = getWidth() / 2; // Half the keyboard width + int travelY = getHeight() / 2; // Half the keyboard height + mSwipeTracker.computeCurrentVelocity(1000); + final float endingVelocityX = mSwipeTracker.getXVelocity(); + final float endingVelocityY = mSwipeTracker.getYVelocity(); + boolean sendDownKey = false; + if (velocityX > mSwipeThreshold && absY < absX && deltaX > travelX) { + if (mDisambiguateSwipe && endingVelocityX < velocityX / 4) { + sendDownKey = true; + } else { + swipeRight(); + return true; + } + } else if (velocityX < -mSwipeThreshold && absY < absX && deltaX < -travelX) { + if (mDisambiguateSwipe && endingVelocityX > velocityX / 4) { + sendDownKey = true; + } else { + swipeLeft(); + return true; + } + } else if (velocityY < -mSwipeThreshold && absX < absY && deltaY < -travelY) { + if (mDisambiguateSwipe && endingVelocityY > velocityY / 4) { + sendDownKey = true; + } else { + swipeUp(); + return true; + } + } else if (velocityY > mSwipeThreshold && absX < absY / 2 && deltaY > travelY) { + if (mDisambiguateSwipe && endingVelocityY < velocityY / 4) { + sendDownKey = true; + } else { + swipeDown(); + return true; + } } - } - if (sendDownKey) { - detectAndSendKey(mDownKey, mStartX, mStartY, me1.getEventTime()); + if (sendDownKey) { + detectAndSendKey(mDownKey, mStartX, mStartY, me1.getEventTime()); + } + return false; } - return false; - } - }); + }); - mGestureDetector.setIsLongpressEnabled(false); + mGestureDetector.setIsLongpressEnabled(false); + } } public void setOnKeyboardActionListener(OnKeyboardActionListener listener) { @@ -487,7 +496,7 @@ public class KeyboardView extends View implements View.OnClickListener { public Keyboard getKeyboard() { return mKeyboard; } - + /** * Sets the state of the shift key of the keyboard, if any. * @param shifted whether or not to enable the state of the shift key @@ -520,7 +529,7 @@ public class KeyboardView extends View implements View.OnClickListener { /** * Enables or disables the key feedback popup. This is a popup that shows a magnified - * version of the depressed key. By default the preview is enabled. + * version of the depressed key. By default the preview is enabled. * @param previewEnabled whether or not to enable the key feedback popup * @see #isPreviewEnabled() */ @@ -536,14 +545,14 @@ public class KeyboardView extends View implements View.OnClickListener { public boolean isPreviewEnabled() { return mShowPreview; } - + public void setVerticalCorrection(int verticalOffset) { - + } public void setPopupParent(View v) { mPopupParent = v; } - + public void setPopupOffset(int x, int y) { mMiniKeyboardOffsetX = x; mMiniKeyboardOffsetY = y; @@ -569,9 +578,9 @@ public class KeyboardView extends View implements View.OnClickListener { return mProximityCorrectOn; } - /** + /** * Popup keyboard close button clicked. - * @hide + * @hide */ public void onClick(View v) { dismissPopupKeyboard(); @@ -654,9 +663,9 @@ public class KeyboardView extends View implements View.OnClickListener { } final Canvas canvas = mCanvas; canvas.clipRect(mDirtyRect, Op.REPLACE); - + if (mKeyboard == null) return; - + final Paint paint = mPaint; final Drawable keyBackground = mKeyBackground; final Rect clipRegion = mClipRegion; @@ -689,15 +698,15 @@ public class KeyboardView extends View implements View.OnClickListener { // Switch the character to uppercase if shift is pressed String label = key.label == null? null : adjustCase(key.label).toString(); - + final Rect bounds = keyBackground.getBounds(); - if (key.width != bounds.right || + if (key.width != bounds.right || key.height != bounds.bottom) { keyBackground.setBounds(0, 0, key.width, key.height); } canvas.translate(key.x + kbdPaddingLeft, key.y + kbdPaddingTop); keyBackground.draw(canvas); - + if (label != null) { // For characters, use large font. For labels like "Done", use small font. if (label.length() > 1 && key.codes.length < 2) { @@ -719,12 +728,12 @@ public class KeyboardView extends View implements View.OnClickListener { // Turn off drop shadow paint.setShadowLayer(0, 0, 0, 0); } else if (key.icon != null) { - final int drawableX = (key.width - padding.left - padding.right + final int drawableX = (key.width - padding.left - padding.right - key.icon.getIntrinsicWidth()) / 2 + padding.left; - final int drawableY = (key.height - padding.top - padding.bottom + final int drawableY = (key.height - padding.top - padding.bottom - key.icon.getIntrinsicHeight()) / 2 + padding.top; canvas.translate(drawableX, drawableY); - key.icon.setBounds(0, 0, + key.icon.setBounds(0, 0, key.icon.getIntrinsicWidth(), key.icon.getIntrinsicHeight()); key.icon.draw(canvas); canvas.translate(-drawableX, -drawableY); @@ -748,7 +757,7 @@ public class KeyboardView extends View implements View.OnClickListener { paint.setColor(0xFF00FF00); canvas.drawCircle((mStartX + mLastX) / 2, (mStartY + mLastY) / 2, 2, paint); } - + mDrawPending = false; mDirtyRect.setEmpty(); } @@ -769,8 +778,8 @@ public class KeyboardView extends View implements View.OnClickListener { primaryIndex = nearestKeyIndices[i]; } - if (((mProximityCorrectOn - && (dist = key.squaredDistanceFrom(x, y)) < mProximityThreshold) + if (((mProximityCorrectOn + && (dist = key.squaredDistanceFrom(x, y)) < mProximityThreshold) || isInside) && key.codes[0] > 32) { // Find insertion point @@ -779,9 +788,9 @@ public class KeyboardView extends View implements View.OnClickListener { closestKeyDist = dist; closestKey = nearestKeyIndices[i]; } - + if (allKeys == null) continue; - + for (int j = 0; j < mDistances.length; j++) { if (mDistances[j] > dist) { // Make space for nCodes codes @@ -846,11 +855,11 @@ public class KeyboardView extends View implements View.OnClickListener { return adjustCase(key.label); } } - + private void showPreview(int keyIndex) { int oldKeyIndex = mCurrentKeyIndex; final PopupWindow previewPopup = mPreviewPopup; - + mCurrentKeyIndex = keyIndex; // Release the old key and press the new key final Key[] keys = mKeys; @@ -884,7 +893,7 @@ public class KeyboardView extends View implements View.OnClickListener { if (previewPopup.isShowing()) { if (keyIndex == NOT_A_KEY) { mHandler.sendMessageDelayed(mHandler - .obtainMessage(MSG_REMOVE_PREVIEW), + .obtainMessage(MSG_REMOVE_PREVIEW), DELAY_AFTER_PREVIEW); } } @@ -894,20 +903,20 @@ public class KeyboardView extends View implements View.OnClickListener { showKey(keyIndex); } else { mHandler.sendMessageDelayed( - mHandler.obtainMessage(MSG_SHOW_PREVIEW, keyIndex, 0), + mHandler.obtainMessage(MSG_SHOW_PREVIEW, keyIndex, 0), DELAY_BEFORE_PREVIEW); } } } } - + private void showKey(final int keyIndex) { final PopupWindow previewPopup = mPreviewPopup; final Key[] keys = mKeys; if (keyIndex < 0 || keyIndex >= mKeys.length) return; Key key = keys[keyIndex]; if (key.icon != null) { - mPreviewText.setCompoundDrawables(null, null, null, + mPreviewText.setCompoundDrawables(null, null, null, key.iconPreview != null ? key.iconPreview : key.icon); mPreviewText.setText(null); } else { @@ -921,9 +930,9 @@ public class KeyboardView extends View implements View.OnClickListener { mPreviewText.setTypeface(Typeface.DEFAULT); } } - mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), + mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); - int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width + int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width + mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight()); final int popupHeight = mPreviewHeight; LayoutParams lp = mPreviewText.getLayoutParams(); @@ -969,7 +978,7 @@ public class KeyboardView extends View implements View.OnClickListener { } else { previewPopup.setWidth(popupWidth); previewPopup.setHeight(popupHeight); - previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY, + previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY, mPopupPreviewX, mPopupPreviewY); } mPreviewText.setVisibility(VISIBLE); @@ -1030,7 +1039,7 @@ public class KeyboardView extends View implements View.OnClickListener { /** * Requests a redraw of the entire keyboard. Calling {@link #invalidate} is not sufficient - * because the keyboard renders the keys to an off-screen buffer and an invalidate() only + * because the keyboard renders the keys to an off-screen buffer and an invalidate() only * draws the cached buffer. * @see #invalidateKey(int) */ @@ -1054,10 +1063,10 @@ public class KeyboardView extends View implements View.OnClickListener { } final Key key = mKeys[keyIndex]; mInvalidatedKey = key; - mDirtyRect.union(key.x + mPaddingLeft, key.y + mPaddingTop, + mDirtyRect.union(key.x + mPaddingLeft, key.y + mPaddingTop, key.x + key.width + mPaddingLeft, key.y + key.height + mPaddingTop); onBufferDraw(); - invalidate(key.x + mPaddingLeft, key.y + mPaddingTop, + invalidate(key.x + mPaddingLeft, key.y + mPaddingTop, key.x + key.width + mPaddingLeft, key.y + key.height + mPaddingTop); } @@ -1070,7 +1079,7 @@ public class KeyboardView extends View implements View.OnClickListener { return false; } - Key popupKey = mKeys[mCurrentKey]; + Key popupKey = mKeys[mCurrentKey]; boolean result = onLongPress(popupKey); if (result) { mAbortKey = true; @@ -1105,12 +1114,12 @@ public class KeyboardView extends View implements View.OnClickListener { mKeyboardActionListener.onKey(primaryCode, keyCodes); dismissPopupKeyboard(); } - + public void onText(CharSequence text) { mKeyboardActionListener.onText(text); dismissPopupKeyboard(); } - + public void swipeLeft() { } public void swipeRight() { } public void swipeUp() { } @@ -1125,7 +1134,7 @@ public class KeyboardView extends View implements View.OnClickListener { //mInputView.setSuggest(mSuggest); Keyboard keyboard; if (popupKey.popupCharacters != null) { - keyboard = new Keyboard(getContext(), popupKeyboardId, + keyboard = new Keyboard(getContext(), popupKeyboardId, popupKey.popupCharacters, -1, getPaddingLeft() + getPaddingRight()); } else { keyboard = new Keyboard(getContext(), popupKeyboardId); @@ -1133,9 +1142,9 @@ public class KeyboardView extends View implements View.OnClickListener { mMiniKeyboard.setKeyboard(keyboard); mMiniKeyboard.setPopupParent(this); mMiniKeyboardContainer.measure( - MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST), + MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST)); - + mMiniKeyboardCache.put(popupKey, mMiniKeyboardContainer); } else { mMiniKeyboard = (KeyboardView) mMiniKeyboardContainer.findViewById( @@ -1184,7 +1193,7 @@ public class KeyboardView extends View implements View.OnClickListener { @Override public boolean onTouchEvent(MotionEvent me) { - // Convert multi-pointer up/down events to single up/down events to + // Convert multi-pointer up/down events to single up/down events to // deal with the typical multi-pointer behavior of two-thumb typing final int pointerCount = me.getPointerCount(); final int action = me.getAction(); @@ -1250,7 +1259,7 @@ public class KeyboardView extends View implements View.OnClickListener { mHandler.removeMessages(MSG_LONGPRESS); return true; } - + // Needs to be called after the gesture detector gets a turn, as it may have // displayed the mini keyboard if (mMiniKeyboardOnScreen && action != MotionEvent.ACTION_CANCEL) { @@ -1272,7 +1281,7 @@ public class KeyboardView extends View implements View.OnClickListener { mDownTime = me.getEventTime(); mLastMoveTime = mDownTime; checkMultiTap(eventTime, keyIndex); - mKeyboardActionListener.onPress(keyIndex != NOT_A_KEY ? + mKeyboardActionListener.onPress(keyIndex != NOT_A_KEY ? mKeys[keyIndex].codes[0] : 0); if (mCurrentKey >= 0 && mKeys[mCurrentKey].repeatable) { mRepeatKeyIndex = mCurrentKey; @@ -1371,11 +1380,11 @@ public class KeyboardView extends View implements View.OnClickListener { detectAndSendKey(mCurrentKey, key.x, key.y, mLastTapTime); return true; } - + protected void swipeRight() { mKeyboardActionListener.swipeRight(); } - + protected void swipeLeft() { mKeyboardActionListener.swipeLeft(); } @@ -1393,7 +1402,7 @@ public class KeyboardView extends View implements View.OnClickListener { mPreviewPopup.dismiss(); } removeMessages(); - + dismissPopupKeyboard(); mBuffer = null; mCanvas = null; @@ -1434,7 +1443,7 @@ public class KeyboardView extends View implements View.OnClickListener { mLastTapTime = -1; mInMultiTap = false; } - + private void checkMultiTap(long eventTime, int keyIndex) { if (keyIndex == NOT_A_KEY) return; Key key = mKeys[keyIndex]; diff --git a/core/java/android/widget/AdapterViewFlipper.java b/core/java/android/widget/AdapterViewFlipper.java index a105b403690f..18d74705773a 100644 --- a/core/java/android/widget/AdapterViewFlipper.java +++ b/core/java/android/widget/AdapterViewFlipper.java @@ -21,7 +21,6 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.res.TypedArray; -import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.Log; @@ -112,7 +111,7 @@ public class AdapterViewFlipper extends AdapterViewAnimator { // home screen. Therefore, we register the receiver as the current // user not the one the context is for. getContext().registerReceiverAsUser(mReceiver, android.os.Process.myUserHandle(), - filter, null, mHandler); + filter, null, getHandler()); if (mAutoStart) { @@ -194,9 +193,8 @@ public class AdapterViewFlipper extends AdapterViewAnimator { // if the flipper is currently flipping automatically, and showNext() is called // we should we should make sure to reset the timer if (mRunning) { - mHandler.removeMessages(FLIP_MSG); - Message msg = mHandler.obtainMessage(FLIP_MSG); - mHandler.sendMessageDelayed(msg, mFlipInterval); + removeCallbacks(mFlipRunnable); + postDelayed(mFlipRunnable, mFlipInterval); } super.showNext(); } @@ -210,9 +208,8 @@ public class AdapterViewFlipper extends AdapterViewAnimator { // if the flipper is currently flipping automatically, and showPrevious() is called // we should we should make sure to reset the timer if (mRunning) { - mHandler.removeMessages(FLIP_MSG); - Message msg = mHandler.obtainMessage(FLIP_MSG); - mHandler.sendMessageDelayed(msg, mFlipInterval); + removeCallbacks(mFlipRunnable); + postDelayed(mFlipRunnable, mFlipInterval); } super.showPrevious(); } @@ -241,10 +238,9 @@ public class AdapterViewFlipper extends AdapterViewAnimator { if (running != mRunning) { if (running) { showOnly(mWhichChild, flipNow); - Message msg = mHandler.obtainMessage(FLIP_MSG); - mHandler.sendMessageDelayed(msg, mFlipInterval); + postDelayed(mFlipRunnable, mFlipInterval); } else { - mHandler.removeMessages(FLIP_MSG); + removeCallbacks(mFlipRunnable); } mRunning = running; } @@ -277,15 +273,11 @@ public class AdapterViewFlipper extends AdapterViewAnimator { return mAutoStart; } - private final int FLIP_MSG = 1; - - private final Handler mHandler = new Handler() { + private final Runnable mFlipRunnable = new Runnable() { @Override - public void handleMessage(Message msg) { - if (msg.what == FLIP_MSG) { - if (mRunning) { - showNext(); - } + public void run() { + if (mRunning) { + showNext(); } } }; diff --git a/core/java/android/widget/AnalogClock.java b/core/java/android/widget/AnalogClock.java index 45eee349d5dd..7f5e21330b19 100644 --- a/core/java/android/widget/AnalogClock.java +++ b/core/java/android/widget/AnalogClock.java @@ -56,7 +56,6 @@ public class AnalogClock extends View { private boolean mAttached; - private final Handler mHandler = new Handler(); private float mMinutes; private float mHour; private boolean mChanged; @@ -121,7 +120,7 @@ public class AnalogClock extends View { // home screen. Therefore, we register the receiver as the current // user not the one the context is for. getContext().registerReceiverAsUser(mIntentReceiver, - android.os.Process.myUserHandle(), filter, null, mHandler); + android.os.Process.myUserHandle(), filter, null, getHandler()); } // NOTE: It's safe to do these after registering the receiver since the receiver always runs diff --git a/core/java/android/widget/Chronometer.java b/core/java/android/widget/Chronometer.java index ebb54ff39d06..4d707e369992 100644 --- a/core/java/android/widget/Chronometer.java +++ b/core/java/android/widget/Chronometer.java @@ -73,8 +73,6 @@ public class Chronometer extends TextView { private OnChronometerTickListener mOnChronometerTickListener; private StringBuilder mRecycle = new StringBuilder(8); - private static final int TICK_WHAT = 2; - /** * Initialize this Chronometer object. * Sets the base to the current time. @@ -259,20 +257,21 @@ public class Chronometer extends TextView { if (running) { updateText(SystemClock.elapsedRealtime()); dispatchChronometerTick(); - mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT), 1000); + postDelayed(mTickRunnable, 1000); } else { - mHandler.removeMessages(TICK_WHAT); + removeCallbacks(mTickRunnable); } mRunning = running; } } - - private Handler mHandler = new Handler() { - public void handleMessage(Message m) { + + private final Runnable mTickRunnable = new Runnable() { + @Override + public void run() { if (mRunning) { updateText(SystemClock.elapsedRealtime()); dispatchChronometerTick(); - sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000); + postDelayed(mTickRunnable, 1000); } } }; diff --git a/core/java/android/widget/DigitalClock.java b/core/java/android/widget/DigitalClock.java index 9e442f9d0392..1df164308076 100644 --- a/core/java/android/widget/DigitalClock.java +++ b/core/java/android/widget/DigitalClock.java @@ -60,18 +60,18 @@ public class DigitalClock extends TextView { if (mCalendar == null) { mCalendar = Calendar.getInstance(); } - - mFormatChangeObserver = new FormatChangeObserver(); - getContext().getContentResolver().registerContentObserver( - Settings.System.CONTENT_URI, true, mFormatChangeObserver); - - setFormat(); } @Override protected void onAttachedToWindow() { mTickerStopped = false; super.onAttachedToWindow(); + + mFormatChangeObserver = new FormatChangeObserver(); + getContext().getContentResolver().registerContentObserver( + Settings.System.CONTENT_URI, true, mFormatChangeObserver); + setFormat(); + mHandler = new Handler(); /** @@ -95,6 +95,8 @@ public class DigitalClock extends TextView { protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mTickerStopped = true; + getContext().getContentResolver().unregisterContentObserver( + mFormatChangeObserver); } private void setFormat() { diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index 018cc02c530e..24183be5eabf 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -45,7 +45,6 @@ import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.os.Bundle; -import android.os.Handler; import android.os.Parcel; import android.os.Parcelable; import android.os.ParcelableParcel; @@ -2072,14 +2071,14 @@ public class Editor { if (shouldBlink()) { mShowCursor = SystemClock.uptimeMillis(); if (mBlink == null) mBlink = new Blink(); - mBlink.removeCallbacks(mBlink); - mBlink.postAtTime(mBlink, mShowCursor + BLINK); + mTextView.removeCallbacks(mBlink); + mTextView.postDelayed(mBlink, BLINK); } else { - if (mBlink != null) mBlink.removeCallbacks(mBlink); + if (mBlink != null) mTextView.removeCallbacks(mBlink); } } - private class Blink extends Handler implements Runnable { + private class Blink implements Runnable { private boolean mCancelled; public void run() { @@ -2087,20 +2086,20 @@ public class Editor { return; } - removeCallbacks(Blink.this); + mTextView.removeCallbacks(this); if (shouldBlink()) { if (mTextView.getLayout() != null) { mTextView.invalidateCursorPath(); } - postAtTime(this, SystemClock.uptimeMillis() + BLINK); + mTextView.postDelayed(this, BLINK); } } void cancel() { if (!mCancelled) { - removeCallbacks(Blink.this); + mTextView.removeCallbacks(this); mCancelled = true; } } diff --git a/core/java/android/widget/Gallery.java b/core/java/android/widget/Gallery.java index b187c1c47ac9..9ebbe36da1c5 100644 --- a/core/java/android/widget/Gallery.java +++ b/core/java/android/widget/Gallery.java @@ -201,9 +201,6 @@ public class Gallery extends AbsSpinner implements GestureDetector.OnGestureList public Gallery(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - - mGestureDetector = new GestureDetector(context, this); - mGestureDetector.setIsLongpressEnabled(true); final TypedArray a = context.obtainStyledAttributes( attrs, com.android.internal.R.styleable.Gallery, defStyleAttr, defStyleRes); @@ -236,6 +233,16 @@ public class Gallery extends AbsSpinner implements GestureDetector.OnGestureList mGroupFlags |= FLAG_SUPPORT_STATIC_TRANSFORMATIONS; } + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + + if (mGestureDetector == null) { + mGestureDetector = new GestureDetector(getContext(), this); + mGestureDetector.setIsLongpressEnabled(true); + } + } + /** * Whether or not to callback on any {@link #getOnItemSelectedListener()} * while the items are being flinged. If false, only the final selected item diff --git a/core/java/android/widget/MediaController.java b/core/java/android/widget/MediaController.java index ff2f22be6db6..80086374ebd3 100644 --- a/core/java/android/widget/MediaController.java +++ b/core/java/android/widget/MediaController.java @@ -20,8 +20,6 @@ import android.content.Context; import android.content.res.Resources; import android.graphics.PixelFormat; import android.media.AudioManager; -import android.os.Handler; -import android.os.Message; import android.util.AttributeSet; import android.util.Log; import android.view.Gravity; @@ -83,8 +81,6 @@ public class MediaController extends FrameLayout { private boolean mShowing; private boolean mDragging; private static final int sDefaultTimeout = 3000; - private static final int FADE_OUT = 1; - private static final int SHOW_PROGRESS = 2; private final boolean mUseFastForward; private boolean mFromXml; private boolean mListenersSet; @@ -373,12 +369,11 @@ public class MediaController extends FrameLayout { // cause the progress bar to be updated even if mShowing // was already true. This happens, for example, if we're // paused with the progress bar showing the user hits play. - mHandler.sendEmptyMessage(SHOW_PROGRESS); + post(mShowProgress); if (timeout != 0 && !mAccessibilityManager.isTouchExplorationEnabled()) { - mHandler.removeMessages(FADE_OUT); - Message msg = mHandler.obtainMessage(FADE_OUT); - mHandler.sendMessageDelayed(msg, timeout); + removeCallbacks(mFadeOut); + postDelayed(mFadeOut, timeout); } } @@ -395,7 +390,7 @@ public class MediaController extends FrameLayout { if (mShowing) { try { - mHandler.removeMessages(SHOW_PROGRESS); + removeCallbacks(mShowProgress); mWindowManager.removeView(mDecor); } catch (IllegalArgumentException ex) { Log.w("MediaController", "already removed"); @@ -404,21 +399,19 @@ public class MediaController extends FrameLayout { } } - private final Handler mHandler = new Handler() { + private final Runnable mFadeOut = new Runnable() { @Override - public void handleMessage(Message msg) { - int pos; - switch (msg.what) { - case FADE_OUT: - hide(); - break; - case SHOW_PROGRESS: - pos = setProgress(); - if (!mDragging && mShowing && mPlayer.isPlaying()) { - msg = obtainMessage(SHOW_PROGRESS); - sendMessageDelayed(msg, 1000 - (pos % 1000)); - } - break; + public void run() { + hide(); + } + }; + + private final Runnable mShowProgress = new Runnable() { + @Override + public void run() { + int pos = setProgress(); + if (!mDragging && mShowing && mPlayer.isPlaying()) { + postDelayed(mShowProgress, 1000 - (pos % 1000)); } } }; @@ -587,7 +580,7 @@ public class MediaController extends FrameLayout { // the seekbar and b) once the user is done dragging the thumb // we will post one of these messages to the queue again and // this ensures that there will be exactly one message queued up. - mHandler.removeMessages(SHOW_PROGRESS); + removeCallbacks(mShowProgress); } @Override @@ -615,7 +608,7 @@ public class MediaController extends FrameLayout { // Ensure that progress is properly updated in the future, // the call to show() does not guarantee this because it is a // no-op if we are already showing. - mHandler.sendEmptyMessage(SHOW_PROGRESS); + post(mShowProgress); } }; diff --git a/core/java/android/widget/QuickContactBadge.java b/core/java/android/widget/QuickContactBadge.java index e241d4cb9b13..8c15cde4764f 100644 --- a/core/java/android/widget/QuickContactBadge.java +++ b/core/java/android/widget/QuickContactBadge.java @@ -96,10 +96,16 @@ public class QuickContactBadge extends ImageView implements OnClickListener { com.android.internal.R.styleable.Theme_quickContactBadgeOverlay); styledAttributes.recycle(); + setOnClickListener(this); + } + + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + if (!isInEditMode()) { mQueryHandler = new QueryHandler(mContext.getContentResolver()); } - setOnClickListener(this); } @Override diff --git a/core/java/android/widget/SlidingDrawer.java b/core/java/android/widget/SlidingDrawer.java index 9c442369971d..a55e77db4d16 100644 --- a/core/java/android/widget/SlidingDrawer.java +++ b/core/java/android/widget/SlidingDrawer.java @@ -93,7 +93,6 @@ public class SlidingDrawer extends ViewGroup { private static final float MAXIMUM_MAJOR_VELOCITY = 200.0f; private static final float MAXIMUM_ACCELERATION = 2000.0f; private static final int VELOCITY_UNITS = 1000; - private static final int MSG_ANIMATE = 1000; private static final int ANIMATION_FRAME_DURATION = 1000 / 60; private static final int EXPANDED_FULL_OPEN = -10001; @@ -123,7 +122,6 @@ public class SlidingDrawer extends ViewGroup { private OnDrawerCloseListener mOnDrawerCloseListener; private OnDrawerScrollListener mOnDrawerScrollListener; - private final Handler mHandler = new SlidingHandler(); private float mAnimatedAcceleration; private float mAnimatedVelocity; private float mAnimationPosition; @@ -553,8 +551,8 @@ public class SlidingDrawer extends ViewGroup { mAnimationLastTime = now; mCurrentAnimationTime = now + ANIMATION_FRAME_DURATION; mAnimating = true; - mHandler.removeMessages(MSG_ANIMATE); - mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ANIMATE), mCurrentAnimationTime); + removeCallbacks(mSlidingRunnable); + postDelayed(mSlidingRunnable, ANIMATION_FRAME_DURATION); stopTracking(); } @@ -569,7 +567,7 @@ public class SlidingDrawer extends ViewGroup { (mVertical ? getHeight() - mHandleHeight : getWidth() - mHandleWidth); moveHandle((int) mAnimationPosition); mAnimating = true; - mHandler.removeMessages(MSG_ANIMATE); + removeCallbacks(mSlidingRunnable); long now = SystemClock.uptimeMillis(); mAnimationLastTime = now; mCurrentAnimationTime = now + ANIMATION_FRAME_DURATION; @@ -577,7 +575,7 @@ public class SlidingDrawer extends ViewGroup { } else { if (mAnimating) { mAnimating = false; - mHandler.removeMessages(MSG_ANIMATE); + removeCallbacks(mSlidingRunnable); } moveHandle(position); } @@ -709,8 +707,7 @@ public class SlidingDrawer extends ViewGroup { } else { moveHandle((int) mAnimationPosition); mCurrentAnimationTime += ANIMATION_FRAME_DURATION; - mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ANIMATE), - mCurrentAnimationTime); + postDelayed(mSlidingRunnable, ANIMATION_FRAME_DURATION); } } } @@ -974,13 +971,10 @@ public class SlidingDrawer extends ViewGroup { } } - private class SlidingHandler extends Handler { - public void handleMessage(Message m) { - switch (m.what) { - case MSG_ANIMATE: - doAnimation(); - break; - } + private final Runnable mSlidingRunnable = new Runnable() { + @Override + public void run() { + doAnimation(); } - } + }; } diff --git a/core/java/android/widget/TextClock.java b/core/java/android/widget/TextClock.java index bcde3158866a..ff10287f3f89 100644 --- a/core/java/android/widget/TextClock.java +++ b/core/java/android/widget/TextClock.java @@ -137,7 +137,13 @@ public class TextClock extends TextView { private boolean mShowCurrentUserTime; - private final ContentObserver mFormatChangeObserver = new ContentObserver(new Handler()) { + private ContentObserver mFormatChangeObserver; + private class FormatChangeObserver extends ContentObserver { + + public FormatChangeObserver(Handler handler) { + super(handler); + } + @Override public void onChange(boolean selfChange) { chooseFormat(); @@ -553,13 +559,18 @@ public class TextClock extends TextView { } private void registerObserver() { - final ContentResolver resolver = getContext().getContentResolver(); - if (mShowCurrentUserTime) { - resolver.registerContentObserver(Settings.System.CONTENT_URI, true, - mFormatChangeObserver, UserHandle.USER_ALL); - } else { - resolver.registerContentObserver(Settings.System.CONTENT_URI, true, - mFormatChangeObserver); + if (isAttachedToWindow()) { + if (mFormatChangeObserver == null) { + mFormatChangeObserver = new FormatChangeObserver(getHandler()); + } + final ContentResolver resolver = getContext().getContentResolver(); + if (mShowCurrentUserTime) { + resolver.registerContentObserver(Settings.System.CONTENT_URI, true, + mFormatChangeObserver, UserHandle.USER_ALL); + } else { + resolver.registerContentObserver(Settings.System.CONTENT_URI, true, + mFormatChangeObserver); + } } } @@ -568,8 +579,10 @@ public class TextClock extends TextView { } private void unregisterObserver() { - final ContentResolver resolver = getContext().getContentResolver(); - resolver.unregisterContentObserver(mFormatChangeObserver); + if (mFormatChangeObserver != null) { + final ContentResolver resolver = getContext().getContentResolver(); + resolver.unregisterContentObserver(mFormatChangeObserver); + } } private void onTimeChanged() { diff --git a/core/java/android/widget/ViewFlipper.java b/core/java/android/widget/ViewFlipper.java index 94e7ba19ee4c..65af7aa28a6b 100644 --- a/core/java/android/widget/ViewFlipper.java +++ b/core/java/android/widget/ViewFlipper.java @@ -96,7 +96,7 @@ public class ViewFlipper extends ViewAnimator { // home screen. Therefore, we register the receiver as the current // user not the one the context is for. getContext().registerReceiverAsUser(mReceiver, android.os.Process.myUserHandle(), - filter, null, mHandler); + filter, null, getHandler()); if (mAutoStart) { // Automatically start when requested @@ -173,10 +173,9 @@ public class ViewFlipper extends ViewAnimator { if (running != mRunning) { if (running) { showOnly(mWhichChild, flipNow); - Message msg = mHandler.obtainMessage(FLIP_MSG); - mHandler.sendMessageDelayed(msg, mFlipInterval); + postDelayed(mFlipRunnable, mFlipInterval); } else { - mHandler.removeMessages(FLIP_MSG); + removeCallbacks(mFlipRunnable); } mRunning = running; } @@ -209,17 +208,12 @@ public class ViewFlipper extends ViewAnimator { return mAutoStart; } - private final int FLIP_MSG = 1; - - private final Handler mHandler = new Handler() { + private final Runnable mFlipRunnable = new Runnable() { @Override - public void handleMessage(Message msg) { - if (msg.what == FLIP_MSG) { - if (mRunning) { - showNext(); - msg = obtainMessage(FLIP_MSG); - sendMessageDelayed(msg, mFlipInterval); - } + public void run() { + if (mRunning) { + showNext(); + postDelayed(mFlipRunnable, mFlipInterval); } } }; diff --git a/core/java/android/widget/ZoomButton.java b/core/java/android/widget/ZoomButton.java index 0255bdbd3c27..7d4f8ed08ba9 100644 --- a/core/java/android/widget/ZoomButton.java +++ b/core/java/android/widget/ZoomButton.java @@ -17,7 +17,6 @@ package android.widget; import android.content.Context; -import android.os.Handler; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.MotionEvent; @@ -26,12 +25,11 @@ import android.view.View.OnLongClickListener; public class ZoomButton extends ImageButton implements OnLongClickListener { - private final Handler mHandler; private final Runnable mRunnable = new Runnable() { public void run() { if (hasOnClickListeners() && mIsInLongpress && isEnabled()) { callOnClick(); - mHandler.postDelayed(this, mZoomSpeed); + postDelayed(this, mZoomSpeed); } } }; @@ -53,7 +51,6 @@ public class ZoomButton extends ImageButton implements OnLongClickListener { public ZoomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - mHandler = new Handler(); setOnLongClickListener(this); } @@ -72,7 +69,7 @@ public class ZoomButton extends ImageButton implements OnLongClickListener { public boolean onLongClick(View v) { mIsInLongpress = true; - mHandler.post(mRunnable); + post(mRunnable); return true; } |