From cf915ddc878699909365dd599a0e154552e244e2 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Tue, 24 Dec 2013 18:03:17 +0900 Subject: Rename classes related to AbstractDrawingPreview Change-Id: Ife2e2c08b2b6fbf9ec33cf259f276878816b291c --- .../inputmethod/keyboard/MainKeyboardView.java | 74 ++++---- .../keyboard/internal/AbstractDrawingPreview.java | 5 +- .../internal/DrawingPreviewPlacerView.java | 83 ++++++++ .../internal/GestureFloatingPreviewText.java | 174 ----------------- .../GestureFloatingTextDrawingPreview.java | 174 +++++++++++++++++ .../internal/GestureTrailsDrawingPreview.java | 208 +++++++++++++++++++++ .../keyboard/internal/GestureTrailsPreview.java | 207 -------------------- .../keyboard/internal/PreviewPlacerView.java | 83 -------- .../internal/SlidingKeyInputDrawingPreview.java | 99 ++++++++++ .../keyboard/internal/SlidingKeyInputPreview.java | 98 ---------- 10 files changed, 604 insertions(+), 601 deletions(-) create mode 100644 java/src/com/android/inputmethod/keyboard/internal/DrawingPreviewPlacerView.java delete mode 100644 java/src/com/android/inputmethod/keyboard/internal/GestureFloatingPreviewText.java create mode 100644 java/src/com/android/inputmethod/keyboard/internal/GestureFloatingTextDrawingPreview.java create mode 100644 java/src/com/android/inputmethod/keyboard/internal/GestureTrailsDrawingPreview.java delete mode 100644 java/src/com/android/inputmethod/keyboard/internal/GestureTrailsPreview.java delete mode 100644 java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java create mode 100644 java/src/com/android/inputmethod/keyboard/internal/SlidingKeyInputDrawingPreview.java delete mode 100644 java/src/com/android/inputmethod/keyboard/internal/SlidingKeyInputPreview.java (limited to 'java/src') diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index 992603b43..07bd7a651 100644 --- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java @@ -49,13 +49,13 @@ import com.android.inputmethod.accessibility.AccessibilityUtils; import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy; import com.android.inputmethod.annotations.ExternallyReferenced; import com.android.inputmethod.keyboard.internal.DrawingHandler; -import com.android.inputmethod.keyboard.internal.GestureFloatingPreviewText; -import com.android.inputmethod.keyboard.internal.GestureTrailsPreview; +import com.android.inputmethod.keyboard.internal.DrawingPreviewPlacerView; +import com.android.inputmethod.keyboard.internal.GestureFloatingTextDrawingPreview; +import com.android.inputmethod.keyboard.internal.GestureTrailsDrawingPreview; import com.android.inputmethod.keyboard.internal.KeyDrawParams; import com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams; import com.android.inputmethod.keyboard.internal.NonDistinctMultitouchHelper; -import com.android.inputmethod.keyboard.internal.PreviewPlacerView; -import com.android.inputmethod.keyboard.internal.SlidingKeyInputPreview; +import com.android.inputmethod.keyboard.internal.SlidingKeyInputDrawingPreview; import com.android.inputmethod.keyboard.internal.TimerHandler; import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.LatinImeLogger; @@ -152,12 +152,12 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack private final ObjectAnimator mAltCodeKeyWhileTypingFadeinAnimator; private int mAltCodeKeyWhileTypingAnimAlpha = Constants.Color.ALPHA_OPAQUE; - // Preview placer view - private final PreviewPlacerView mPreviewPlacerView; + // Drawing preview placer view + private final DrawingPreviewPlacerView mDrawingPreviewPlacerView; private final int[] mOriginCoords = CoordinateUtils.newInstance(); - private final GestureFloatingPreviewText mGestureFloatingPreviewText; - private final GestureTrailsPreview mGestureTrailsPreview; - private final SlidingKeyInputPreview mSlidingKeyInputPreview; + private final GestureFloatingTextDrawingPreview mGestureFloatingTextDrawingPreview; + private final GestureTrailsDrawingPreview mGestureTrailsDrawingPreview; + private final SlidingKeyInputDrawingPreview mSlidingKeyInputDrawingPreview; // Key preview private static final boolean FADE_OUT_KEY_TOP_LETTER_WHEN_KEY_IS_PRESSED = false; @@ -212,7 +212,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack public MainKeyboardView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); - mPreviewPlacerView = new PreviewPlacerView(context, attrs); + mDrawingPreviewPlacerView = new DrawingPreviewPlacerView(context, attrs); final TypedArray mainKeyboardViewAttr = context.obtainStyledAttributes( attrs, R.styleable.MainKeyboardView, defStyle, R.style.MainKeyboardView); @@ -290,17 +290,17 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack mGestureFloatingPreviewTextLingerTimeout = mainKeyboardViewAttr.getInt( R.styleable.MainKeyboardView_gestureFloatingPreviewTextLingerTimeout, 0); - mGestureFloatingPreviewText = new GestureFloatingPreviewText( - mPreviewPlacerView, mainKeyboardViewAttr); - mPreviewPlacerView.addPreview(mGestureFloatingPreviewText); + mGestureFloatingTextDrawingPreview = new GestureFloatingTextDrawingPreview( + mDrawingPreviewPlacerView, mainKeyboardViewAttr); + mDrawingPreviewPlacerView.addPreview(mGestureFloatingTextDrawingPreview); - mGestureTrailsPreview = new GestureTrailsPreview( - mPreviewPlacerView, mainKeyboardViewAttr); - mPreviewPlacerView.addPreview(mGestureTrailsPreview); + mGestureTrailsDrawingPreview = new GestureTrailsDrawingPreview( + mDrawingPreviewPlacerView, mainKeyboardViewAttr); + mDrawingPreviewPlacerView.addPreview(mGestureTrailsDrawingPreview); - mSlidingKeyInputPreview = new SlidingKeyInputPreview( - mPreviewPlacerView, mainKeyboardViewAttr); - mPreviewPlacerView.addPreview(mSlidingKeyInputPreview); + mSlidingKeyInputDrawingPreview = new SlidingKeyInputDrawingPreview( + mDrawingPreviewPlacerView, mainKeyboardViewAttr); + mDrawingPreviewPlacerView.addPreview(mSlidingKeyInputDrawingPreview); mainKeyboardViewAttr.recycle(); mMoreKeysKeyboardContainer = LayoutInflater.from(getContext()) @@ -321,7 +321,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack @Override public void setHardwareAcceleratedDrawingEnabled(final boolean enabled) { super.setHardwareAcceleratedDrawingEnabled(enabled); - mPreviewPlacerView.setHardwareAcceleratedDrawingEnabled(enabled); + mDrawingPreviewPlacerView.setHardwareAcceleratedDrawingEnabled(enabled); } private ObjectAnimator loadObjectAnimator(final int resId, final Object target) { @@ -460,7 +460,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack } private void locatePreviewPlacerView() { - if (mPreviewPlacerView.getParent() != null) { + if (mDrawingPreviewPlacerView.getParent() != null) { return; } final int width = getWidth(); @@ -483,10 +483,10 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack final ViewGroup windowContentView = (ViewGroup)rootView.findViewById(android.R.id.content); // Note: It'd be very weird if we get null by android.R.id.content. if (windowContentView == null) { - Log.w(TAG, "Cannot find android.R.id.content view to add PreviewPlacerView"); + Log.w(TAG, "Cannot find android.R.id.content view to add DrawingPreviewPlacerView"); } else { - windowContentView.addView(mPreviewPlacerView); - mPreviewPlacerView.setKeyboardViewGeometry(mOriginCoords, width, height); + windowContentView.addView(mDrawingPreviewPlacerView); + mDrawingPreviewPlacerView.setKeyboardViewGeometry(mOriginCoords, width, height); } } @@ -516,8 +516,8 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack previewTextView = new TextView(context); } locatePreviewPlacerView(); - mPreviewPlacerView.addView( - previewTextView, ViewLayoutUtils.newLayoutParam(mPreviewPlacerView, 0, 0)); + mDrawingPreviewPlacerView.addView( + previewTextView, ViewLayoutUtils.newLayoutParam(mDrawingPreviewPlacerView, 0, 0)); return previewTextView; } @@ -757,31 +757,31 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack } public void setSlidingKeyInputPreviewEnabled(final boolean enabled) { - mSlidingKeyInputPreview.setPreviewEnabled(enabled); + mSlidingKeyInputDrawingPreview.setPreviewEnabled(enabled); } @Override public void showSlidingKeyInputPreview(final PointerTracker tracker) { locatePreviewPlacerView(); - mSlidingKeyInputPreview.setPreviewPosition(tracker); + mSlidingKeyInputDrawingPreview.setPreviewPosition(tracker); } @Override public void dismissSlidingKeyInputPreview() { - mSlidingKeyInputPreview.dismissSlidingKeyInputPreview(); + mSlidingKeyInputDrawingPreview.dismissSlidingKeyInputPreview(); } private void setGesturePreviewMode(final boolean isGestureTrailEnabled, final boolean isGestureFloatingPreviewTextEnabled) { - mGestureFloatingPreviewText.setPreviewEnabled(isGestureFloatingPreviewTextEnabled); - mGestureTrailsPreview.setPreviewEnabled(isGestureTrailEnabled); + mGestureFloatingTextDrawingPreview.setPreviewEnabled(isGestureFloatingPreviewTextEnabled); + mGestureTrailsDrawingPreview.setPreviewEnabled(isGestureTrailEnabled); } // Implements {@link DrawingHandler.Callbacks} method. @Override public void showGestureFloatingPreviewText(final SuggestedWords suggestedWords) { locatePreviewPlacerView(); - mGestureFloatingPreviewText.setSuggetedWords(suggestedWords); + mGestureFloatingTextDrawingPreview.setSuggetedWords(suggestedWords); } public void dismissGestureFloatingPreviewText() { @@ -794,9 +794,9 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack final boolean showsFloatingPreviewText) { locatePreviewPlacerView(); if (showsFloatingPreviewText) { - mGestureFloatingPreviewText.setPreviewPosition(tracker); + mGestureFloatingTextDrawingPreview.setPreviewPosition(tracker); } - mGestureTrailsPreview.setPreviewPosition(tracker); + mGestureTrailsDrawingPreview.setPreviewPosition(tracker); } // Note that this method is called from a non-UI thread. @@ -826,7 +826,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); - mPreviewPlacerView.removeAllViews(); + mDrawingPreviewPlacerView.removeAllViews(); // Notify the ResearchLogger (development only diagnostics) that the keyboard view has // been detached. This is needed to invalidate the reference of {@link MainKeyboardView} // to null. @@ -930,7 +930,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack @Override public void onShowMoreKeysPanel(final MoreKeysPanel panel) { locatePreviewPlacerView(); - panel.showInParent(mPreviewPlacerView); + panel.showInParent(mDrawingPreviewPlacerView); mMoreKeysPanel = panel; dimEntireKeyboard(true /* dimmed */); } @@ -1228,6 +1228,6 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack @Override public void deallocateMemory() { super.deallocateMemory(); - mGestureTrailsPreview.deallocateMemory(); + mGestureTrailsDrawingPreview.deallocateMemory(); } } diff --git a/java/src/com/android/inputmethod/keyboard/internal/AbstractDrawingPreview.java b/java/src/com/android/inputmethod/keyboard/internal/AbstractDrawingPreview.java index b814fc162..32352ce04 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/AbstractDrawingPreview.java +++ b/java/src/com/android/inputmethod/keyboard/internal/AbstractDrawingPreview.java @@ -22,8 +22,9 @@ import android.view.View; import com.android.inputmethod.keyboard.PointerTracker; /** - * Abstract base class for previews that are drawn on PreviewPlacerView, e.g., - * GestureFloatingPrevewText, GestureTrail, and SlidingKeyInputPreview. + * Abstract base class for previews that are drawn on DrawingPreviewPlacerView, e.g., + * GestureFloatingTextDrawingPreview, GestureTrailsDrawingPreview, and + * SlidingKeyInputDrawingPreview. */ public abstract class AbstractDrawingPreview { private final View mDrawingView; diff --git a/java/src/com/android/inputmethod/keyboard/internal/DrawingPreviewPlacerView.java b/java/src/com/android/inputmethod/keyboard/internal/DrawingPreviewPlacerView.java new file mode 100644 index 000000000..14baf35d2 --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/internal/DrawingPreviewPlacerView.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * 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 License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.keyboard.internal; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffXfermode; +import android.util.AttributeSet; +import android.widget.RelativeLayout; + +import com.android.inputmethod.latin.utils.CollectionUtils; +import com.android.inputmethod.latin.utils.CoordinateUtils; + +import java.util.ArrayList; + +public final class DrawingPreviewPlacerView extends RelativeLayout { + private final int[] mKeyboardViewOrigin = CoordinateUtils.newInstance(); + + private final ArrayList mPreviews = CollectionUtils.newArrayList(); + + public DrawingPreviewPlacerView(final Context context, final AttributeSet attrs) { + super(context, attrs); + setWillNotDraw(false); + } + + public void setHardwareAcceleratedDrawingEnabled(final boolean enabled) { + if (!enabled) return; + final Paint layerPaint = new Paint(); + layerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); + setLayerType(LAYER_TYPE_HARDWARE, layerPaint); + } + + public void addPreview(final AbstractDrawingPreview preview) { + mPreviews.add(preview); + } + + public void setKeyboardViewGeometry(final int[] originCoords, final int width, + final int height) { + CoordinateUtils.copy(mKeyboardViewOrigin, originCoords); + final int count = mPreviews.size(); + for (int i = 0; i < count; i++) { + mPreviews.get(i).setKeyboardGeometry(originCoords, width, height); + } + } + + @Override + protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + final int count = mPreviews.size(); + for (int i = 0; i < count; i++) { + mPreviews.get(i).onDetachFromWindow(); + } + } + + @Override + public void onDraw(final Canvas canvas) { + super.onDraw(canvas); + final int originX = CoordinateUtils.x(mKeyboardViewOrigin); + final int originY = CoordinateUtils.y(mKeyboardViewOrigin); + canvas.translate(originX, originY); + final int count = mPreviews.size(); + for (int i = 0; i < count; i++) { + mPreviews.get(i).drawPreview(canvas); + } + canvas.translate(-originX, -originY); + } +} diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureFloatingPreviewText.java b/java/src/com/android/inputmethod/keyboard/internal/GestureFloatingPreviewText.java deleted file mode 100644 index c6dd9e100..000000000 --- a/java/src/com/android/inputmethod/keyboard/internal/GestureFloatingPreviewText.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * 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 License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.keyboard.internal; - -import android.content.res.TypedArray; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.Paint.Align; -import android.graphics.Rect; -import android.graphics.RectF; -import android.text.TextUtils; -import android.view.View; - -import com.android.inputmethod.keyboard.PointerTracker; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.utils.CoordinateUtils; - -/** - * The class for single gesture preview text. The class for multiple gesture preview text will be - * derived from it. - * - * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextSize - * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextColor - * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextOffset - * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewColor - * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewHorizontalPadding - * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewVerticalPadding - * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewRoundRadius - */ -public class GestureFloatingPreviewText extends AbstractDrawingPreview { - protected static final class GesturePreviewTextParams { - public final int mGesturePreviewTextOffset; - public final int mGesturePreviewTextHeight; - public final float mGesturePreviewHorizontalPadding; - public final float mGesturePreviewVerticalPadding; - public final float mGesturePreviewRoundRadius; - - private final int mGesturePreviewTextSize; - private final int mGesturePreviewTextColor; - private final int mGesturePreviewColor; - private final Paint mPaint = new Paint(); - - private static final char[] TEXT_HEIGHT_REFERENCE_CHAR = { 'M' }; - - public GesturePreviewTextParams(final TypedArray mainKeyboardViewAttr) { - mGesturePreviewTextSize = mainKeyboardViewAttr.getDimensionPixelSize( - R.styleable.MainKeyboardView_gestureFloatingPreviewTextSize, 0); - mGesturePreviewTextColor = mainKeyboardViewAttr.getColor( - R.styleable.MainKeyboardView_gestureFloatingPreviewTextColor, 0); - mGesturePreviewTextOffset = mainKeyboardViewAttr.getDimensionPixelOffset( - R.styleable.MainKeyboardView_gestureFloatingPreviewTextOffset, 0); - mGesturePreviewColor = mainKeyboardViewAttr.getColor( - R.styleable.MainKeyboardView_gestureFloatingPreviewColor, 0); - mGesturePreviewHorizontalPadding = mainKeyboardViewAttr.getDimension( - R.styleable.MainKeyboardView_gestureFloatingPreviewHorizontalPadding, 0.0f); - mGesturePreviewVerticalPadding = mainKeyboardViewAttr.getDimension( - R.styleable.MainKeyboardView_gestureFloatingPreviewVerticalPadding, 0.0f); - mGesturePreviewRoundRadius = mainKeyboardViewAttr.getDimension( - R.styleable.MainKeyboardView_gestureFloatingPreviewRoundRadius, 0.0f); - - final Paint textPaint = getTextPaint(); - final Rect textRect = new Rect(); - textPaint.getTextBounds(TEXT_HEIGHT_REFERENCE_CHAR, 0, 1, textRect); - mGesturePreviewTextHeight = textRect.height(); - } - - public Paint getTextPaint() { - mPaint.setAntiAlias(true); - mPaint.setTextAlign(Align.CENTER); - mPaint.setTextSize(mGesturePreviewTextSize); - mPaint.setColor(mGesturePreviewTextColor); - return mPaint; - } - - public Paint getBackgroundPaint() { - mPaint.setColor(mGesturePreviewColor); - return mPaint; - } - } - - private final GesturePreviewTextParams mParams; - private final RectF mGesturePreviewRectangle = new RectF(); - private int mPreviewTextX; - private int mPreviewTextY; - private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY; - private final int[] mLastPointerCoords = CoordinateUtils.newInstance(); - - public GestureFloatingPreviewText(final View drawingView, final TypedArray typedArray) { - super(drawingView); - mParams = new GesturePreviewTextParams(typedArray); - } - - public void setSuggetedWords(final SuggestedWords suggestedWords) { - if (!isPreviewEnabled()) { - return; - } - mSuggestedWords = suggestedWords; - updatePreviewPosition(); - } - - @Override - public void setPreviewPosition(final PointerTracker tracker) { - if (!isPreviewEnabled()) { - return; - } - tracker.getLastCoordinates(mLastPointerCoords); - updatePreviewPosition(); - } - - /** - * Draws gesture preview text - * @param canvas The canvas where preview text is drawn. - */ - @Override - public void drawPreview(final Canvas canvas) { - if (!isPreviewEnabled() || mSuggestedWords.isEmpty() - || TextUtils.isEmpty(mSuggestedWords.getWord(0))) { - return; - } - final float round = mParams.mGesturePreviewRoundRadius; - canvas.drawRoundRect( - mGesturePreviewRectangle, round, round, mParams.getBackgroundPaint()); - final String text = mSuggestedWords.getWord(0); - canvas.drawText(text, mPreviewTextX, mPreviewTextY, mParams.getTextPaint()); - } - - /** - * Updates gesture preview text position based on mLastPointerCoords. - */ - protected void updatePreviewPosition() { - if (mSuggestedWords.isEmpty() || TextUtils.isEmpty(mSuggestedWords.getWord(0))) { - getDrawingView().invalidate(); - return; - } - final String text = mSuggestedWords.getWord(0); - - final RectF rectangle = mGesturePreviewRectangle; - - final int textHeight = mParams.mGesturePreviewTextHeight; - final float textWidth = mParams.getTextPaint().measureText(text); - final float hPad = mParams.mGesturePreviewHorizontalPadding; - final float vPad = mParams.mGesturePreviewVerticalPadding; - final float rectWidth = textWidth + hPad * 2.0f; - final float rectHeight = textHeight + vPad * 2.0f; - - final int displayWidth = getDrawingView().getResources().getDisplayMetrics().widthPixels; - final float rectX = Math.min( - Math.max(CoordinateUtils.x(mLastPointerCoords) - rectWidth / 2.0f, 0.0f), - displayWidth - rectWidth); - final float rectY = CoordinateUtils.y(mLastPointerCoords) - - mParams.mGesturePreviewTextOffset - rectHeight; - rectangle.set(rectX, rectY, rectX + rectWidth, rectY + rectHeight); - - mPreviewTextX = (int)(rectX + hPad + textWidth / 2.0f); - mPreviewTextY = (int)(rectY + vPad) + textHeight; - // TODO: Should narrow the invalidate region. - getDrawingView().invalidate(); - } -} diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureFloatingTextDrawingPreview.java b/java/src/com/android/inputmethod/keyboard/internal/GestureFloatingTextDrawingPreview.java new file mode 100644 index 000000000..179031316 --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/internal/GestureFloatingTextDrawingPreview.java @@ -0,0 +1,174 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * 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 License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.keyboard.internal; + +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.graphics.Rect; +import android.graphics.RectF; +import android.text.TextUtils; +import android.view.View; + +import com.android.inputmethod.keyboard.PointerTracker; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.SuggestedWords; +import com.android.inputmethod.latin.utils.CoordinateUtils; + +/** + * The class for single gesture preview text. The class for multiple gesture preview text will be + * derived from it. + * + * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextSize + * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextColor + * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextOffset + * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewColor + * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewHorizontalPadding + * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewVerticalPadding + * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewRoundRadius + */ +public class GestureFloatingTextDrawingPreview extends AbstractDrawingPreview { + protected static final class GesturePreviewTextParams { + public final int mGesturePreviewTextOffset; + public final int mGesturePreviewTextHeight; + public final float mGesturePreviewHorizontalPadding; + public final float mGesturePreviewVerticalPadding; + public final float mGesturePreviewRoundRadius; + + private final int mGesturePreviewTextSize; + private final int mGesturePreviewTextColor; + private final int mGesturePreviewColor; + private final Paint mPaint = new Paint(); + + private static final char[] TEXT_HEIGHT_REFERENCE_CHAR = { 'M' }; + + public GesturePreviewTextParams(final TypedArray mainKeyboardViewAttr) { + mGesturePreviewTextSize = mainKeyboardViewAttr.getDimensionPixelSize( + R.styleable.MainKeyboardView_gestureFloatingPreviewTextSize, 0); + mGesturePreviewTextColor = mainKeyboardViewAttr.getColor( + R.styleable.MainKeyboardView_gestureFloatingPreviewTextColor, 0); + mGesturePreviewTextOffset = mainKeyboardViewAttr.getDimensionPixelOffset( + R.styleable.MainKeyboardView_gestureFloatingPreviewTextOffset, 0); + mGesturePreviewColor = mainKeyboardViewAttr.getColor( + R.styleable.MainKeyboardView_gestureFloatingPreviewColor, 0); + mGesturePreviewHorizontalPadding = mainKeyboardViewAttr.getDimension( + R.styleable.MainKeyboardView_gestureFloatingPreviewHorizontalPadding, 0.0f); + mGesturePreviewVerticalPadding = mainKeyboardViewAttr.getDimension( + R.styleable.MainKeyboardView_gestureFloatingPreviewVerticalPadding, 0.0f); + mGesturePreviewRoundRadius = mainKeyboardViewAttr.getDimension( + R.styleable.MainKeyboardView_gestureFloatingPreviewRoundRadius, 0.0f); + + final Paint textPaint = getTextPaint(); + final Rect textRect = new Rect(); + textPaint.getTextBounds(TEXT_HEIGHT_REFERENCE_CHAR, 0, 1, textRect); + mGesturePreviewTextHeight = textRect.height(); + } + + public Paint getTextPaint() { + mPaint.setAntiAlias(true); + mPaint.setTextAlign(Align.CENTER); + mPaint.setTextSize(mGesturePreviewTextSize); + mPaint.setColor(mGesturePreviewTextColor); + return mPaint; + } + + public Paint getBackgroundPaint() { + mPaint.setColor(mGesturePreviewColor); + return mPaint; + } + } + + private final GesturePreviewTextParams mParams; + private final RectF mGesturePreviewRectangle = new RectF(); + private int mPreviewTextX; + private int mPreviewTextY; + private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY; + private final int[] mLastPointerCoords = CoordinateUtils.newInstance(); + + public GestureFloatingTextDrawingPreview(final View drawingView, final TypedArray typedArray) { + super(drawingView); + mParams = new GesturePreviewTextParams(typedArray); + } + + public void setSuggetedWords(final SuggestedWords suggestedWords) { + if (!isPreviewEnabled()) { + return; + } + mSuggestedWords = suggestedWords; + updatePreviewPosition(); + } + + @Override + public void setPreviewPosition(final PointerTracker tracker) { + if (!isPreviewEnabled()) { + return; + } + tracker.getLastCoordinates(mLastPointerCoords); + updatePreviewPosition(); + } + + /** + * Draws gesture preview text + * @param canvas The canvas where preview text is drawn. + */ + @Override + public void drawPreview(final Canvas canvas) { + if (!isPreviewEnabled() || mSuggestedWords.isEmpty() + || TextUtils.isEmpty(mSuggestedWords.getWord(0))) { + return; + } + final float round = mParams.mGesturePreviewRoundRadius; + canvas.drawRoundRect( + mGesturePreviewRectangle, round, round, mParams.getBackgroundPaint()); + final String text = mSuggestedWords.getWord(0); + canvas.drawText(text, mPreviewTextX, mPreviewTextY, mParams.getTextPaint()); + } + + /** + * Updates gesture preview text position based on mLastPointerCoords. + */ + protected void updatePreviewPosition() { + if (mSuggestedWords.isEmpty() || TextUtils.isEmpty(mSuggestedWords.getWord(0))) { + getDrawingView().invalidate(); + return; + } + final String text = mSuggestedWords.getWord(0); + + final RectF rectangle = mGesturePreviewRectangle; + + final int textHeight = mParams.mGesturePreviewTextHeight; + final float textWidth = mParams.getTextPaint().measureText(text); + final float hPad = mParams.mGesturePreviewHorizontalPadding; + final float vPad = mParams.mGesturePreviewVerticalPadding; + final float rectWidth = textWidth + hPad * 2.0f; + final float rectHeight = textHeight + vPad * 2.0f; + + final int displayWidth = getDrawingView().getResources().getDisplayMetrics().widthPixels; + final float rectX = Math.min( + Math.max(CoordinateUtils.x(mLastPointerCoords) - rectWidth / 2.0f, 0.0f), + displayWidth - rectWidth); + final float rectY = CoordinateUtils.y(mLastPointerCoords) + - mParams.mGesturePreviewTextOffset - rectHeight; + rectangle.set(rectX, rectY, rectX + rectWidth, rectY + rectHeight); + + mPreviewTextX = (int)(rectX + hPad + textWidth / 2.0f); + mPreviewTextY = (int)(rectY + vPad) + textHeight; + // TODO: Should narrow the invalidate region. + getDrawingView().invalidate(); + } +} diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureTrailsDrawingPreview.java b/java/src/com/android/inputmethod/keyboard/internal/GestureTrailsDrawingPreview.java new file mode 100644 index 000000000..2d23ddb75 --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/internal/GestureTrailsDrawingPreview.java @@ -0,0 +1,208 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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 License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.keyboard.internal; + +import android.content.res.TypedArray; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffXfermode; +import android.graphics.Rect; +import android.os.Message; +import android.util.SparseArray; +import android.view.View; + +import com.android.inputmethod.keyboard.PointerTracker; +import com.android.inputmethod.keyboard.internal.GestureTrail.Params; +import com.android.inputmethod.latin.utils.CollectionUtils; +import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper; + +/** + * Draw gesture trail preview graphics during gesture. + */ +public final class GestureTrailsDrawingPreview extends AbstractDrawingPreview { + private final SparseArray mGestureTrails = CollectionUtils.newSparseArray(); + private final Params mGestureTrailParams; + private final Paint mGesturePaint; + private int mOffscreenWidth; + private int mOffscreenHeight; + private int mOffscreenOffsetY; + private Bitmap mOffscreenBuffer; + private final Canvas mOffscreenCanvas = new Canvas(); + private final Rect mOffscreenSrcRect = new Rect(); + private final Rect mDirtyRect = new Rect(); + private final Rect mGestureTrailBoundsRect = new Rect(); // per trail + + private final DrawingHandler mDrawingHandler; + + private static final class DrawingHandler + extends LeakGuardHandlerWrapper { + private static final int MSG_UPDATE_GESTURE_TRAIL = 0; + + private final Params mGestureTrailParams; + + public DrawingHandler(final GestureTrailsDrawingPreview ownerInstance, + final Params gestureTrailParams) { + super(ownerInstance); + mGestureTrailParams = gestureTrailParams; + } + + @Override + public void handleMessage(final Message msg) { + final GestureTrailsDrawingPreview preview = getOwnerInstance(); + if (preview == null) { + return; + } + switch (msg.what) { + case MSG_UPDATE_GESTURE_TRAIL: + preview.getDrawingView().invalidate(); + break; + } + } + + public void postUpdateGestureTrailPreview() { + removeMessages(MSG_UPDATE_GESTURE_TRAIL); + sendMessageDelayed(obtainMessage(MSG_UPDATE_GESTURE_TRAIL), + mGestureTrailParams.mUpdateInterval); + } + } + + public GestureTrailsDrawingPreview(final View drawingView, + final TypedArray mainKeyboardViewAttr) { + super(drawingView); + mGestureTrailParams = new Params(mainKeyboardViewAttr); + mDrawingHandler = new DrawingHandler(this, mGestureTrailParams); + final Paint gesturePaint = new Paint(); + gesturePaint.setAntiAlias(true); + gesturePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); + mGesturePaint = gesturePaint; + } + + @Override + public void setKeyboardGeometry(final int[] originCoords, final int width, final int height) { + mOffscreenOffsetY = (int)( + height * GestureStroke.EXTRA_GESTURE_TRAIL_AREA_ABOVE_KEYBOARD_RATIO); + mOffscreenWidth = width; + mOffscreenHeight = mOffscreenOffsetY + height; + } + + @Override + public void onDetachFromWindow() { + freeOffscreenBuffer(); + } + + public void deallocateMemory() { + freeOffscreenBuffer(); + } + + private void freeOffscreenBuffer() { + mOffscreenCanvas.setBitmap(null); + mOffscreenCanvas.setMatrix(null); + if (mOffscreenBuffer != null) { + mOffscreenBuffer.recycle(); + mOffscreenBuffer = null; + } + } + + private void mayAllocateOffscreenBuffer() { + if (mOffscreenBuffer != null && mOffscreenBuffer.getWidth() == mOffscreenWidth + && mOffscreenBuffer.getHeight() == mOffscreenHeight) { + return; + } + freeOffscreenBuffer(); + mOffscreenBuffer = Bitmap.createBitmap( + mOffscreenWidth, mOffscreenHeight, Bitmap.Config.ARGB_8888); + mOffscreenCanvas.setBitmap(mOffscreenBuffer); + mOffscreenCanvas.translate(0, mOffscreenOffsetY); + } + + private boolean drawGestureTrails(final Canvas offscreenCanvas, final Paint paint, + final Rect dirtyRect) { + // Clear previous dirty rectangle. + if (!dirtyRect.isEmpty()) { + paint.setColor(Color.TRANSPARENT); + paint.setStyle(Paint.Style.FILL); + offscreenCanvas.drawRect(dirtyRect, paint); + } + dirtyRect.setEmpty(); + boolean needsUpdatingGestureTrail = false; + // Draw gesture trails to offscreen buffer. + synchronized (mGestureTrails) { + // Trails count == fingers count that have ever been active. + final int trailsCount = mGestureTrails.size(); + for (int index = 0; index < trailsCount; index++) { + final GestureTrail trail = mGestureTrails.valueAt(index); + needsUpdatingGestureTrail |= trail.drawGestureTrail(offscreenCanvas, paint, + mGestureTrailBoundsRect, mGestureTrailParams); + // {@link #mGestureTrailBoundsRect} has bounding box of the trail. + dirtyRect.union(mGestureTrailBoundsRect); + } + } + return needsUpdatingGestureTrail; + } + + /** + * Draws the preview + * @param canvas The canvas where the preview is drawn. + */ + @Override + public void drawPreview(final Canvas canvas) { + if (!isPreviewEnabled()) { + return; + } + mayAllocateOffscreenBuffer(); + // Draw gesture trails to offscreen buffer. + final boolean needsUpdatingGestureTrail = drawGestureTrails( + mOffscreenCanvas, mGesturePaint, mDirtyRect); + if (needsUpdatingGestureTrail) { + mDrawingHandler.postUpdateGestureTrailPreview(); + } + // Transfer offscreen buffer to screen. + if (!mDirtyRect.isEmpty()) { + mOffscreenSrcRect.set(mDirtyRect); + mOffscreenSrcRect.offset(0, mOffscreenOffsetY); + canvas.drawBitmap(mOffscreenBuffer, mOffscreenSrcRect, mDirtyRect, null); + // Note: Defer clearing the dirty rectangle here because we will get cleared + // rectangle on the canvas. + } + } + + /** + * Set the position of the preview. + * @param tracker The new location of the preview is based on the points in PointerTracker. + */ + @Override + public void setPreviewPosition(final PointerTracker tracker) { + if (!isPreviewEnabled()) { + return; + } + GestureTrail trail; + synchronized (mGestureTrails) { + trail = mGestureTrails.get(tracker.mPointerId); + if (trail == null) { + trail = new GestureTrail(); + mGestureTrails.put(tracker.mPointerId, trail); + } + } + trail.addStroke(tracker.getGestureStrokeWithPreviewPoints(), tracker.getDownTime()); + + // TODO: Should narrow the invalidate region. + getDrawingView().invalidate(); + } +} diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureTrailsPreview.java b/java/src/com/android/inputmethod/keyboard/internal/GestureTrailsPreview.java deleted file mode 100644 index 8b413e5e2..000000000 --- a/java/src/com/android/inputmethod/keyboard/internal/GestureTrailsPreview.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * 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 License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.keyboard.internal; - -import android.content.res.TypedArray; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffXfermode; -import android.graphics.Rect; -import android.os.Message; -import android.util.SparseArray; -import android.view.View; - -import com.android.inputmethod.keyboard.PointerTracker; -import com.android.inputmethod.keyboard.internal.GestureTrail.Params; -import com.android.inputmethod.latin.utils.CollectionUtils; -import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper; - -/** - * Draw gesture trail preview graphics during gesture. - */ -public final class GestureTrailsPreview extends AbstractDrawingPreview { - private final SparseArray mGestureTrails = CollectionUtils.newSparseArray(); - private final Params mGestureTrailParams; - private final Paint mGesturePaint; - private int mOffscreenWidth; - private int mOffscreenHeight; - private int mOffscreenOffsetY; - private Bitmap mOffscreenBuffer; - private final Canvas mOffscreenCanvas = new Canvas(); - private final Rect mOffscreenSrcRect = new Rect(); - private final Rect mDirtyRect = new Rect(); - private final Rect mGestureTrailBoundsRect = new Rect(); // per trail - - private final DrawingHandler mDrawingHandler; - - private static final class DrawingHandler - extends LeakGuardHandlerWrapper { - private static final int MSG_UPDATE_GESTURE_TRAIL = 0; - - private final Params mGestureTrailParams; - - public DrawingHandler(final GestureTrailsPreview ownerInstance, - final Params gestureTrailParams) { - super(ownerInstance); - mGestureTrailParams = gestureTrailParams; - } - - @Override - public void handleMessage(final Message msg) { - final GestureTrailsPreview preview = getOwnerInstance(); - if (preview == null) { - return; - } - switch (msg.what) { - case MSG_UPDATE_GESTURE_TRAIL: - preview.getDrawingView().invalidate(); - break; - } - } - - public void postUpdateGestureTrailPreview() { - removeMessages(MSG_UPDATE_GESTURE_TRAIL); - sendMessageDelayed(obtainMessage(MSG_UPDATE_GESTURE_TRAIL), - mGestureTrailParams.mUpdateInterval); - } - } - - public GestureTrailsPreview(final View drawingView, final TypedArray mainKeyboardViewAttr) { - super(drawingView); - mGestureTrailParams = new Params(mainKeyboardViewAttr); - mDrawingHandler = new DrawingHandler(this, mGestureTrailParams); - final Paint gesturePaint = new Paint(); - gesturePaint.setAntiAlias(true); - gesturePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); - mGesturePaint = gesturePaint; - } - - @Override - public void setKeyboardGeometry(final int[] originCoords, final int width, final int height) { - mOffscreenOffsetY = (int)( - height * GestureStroke.EXTRA_GESTURE_TRAIL_AREA_ABOVE_KEYBOARD_RATIO); - mOffscreenWidth = width; - mOffscreenHeight = mOffscreenOffsetY + height; - } - - @Override - public void onDetachFromWindow() { - freeOffscreenBuffer(); - } - - public void deallocateMemory() { - freeOffscreenBuffer(); - } - - private void freeOffscreenBuffer() { - mOffscreenCanvas.setBitmap(null); - mOffscreenCanvas.setMatrix(null); - if (mOffscreenBuffer != null) { - mOffscreenBuffer.recycle(); - mOffscreenBuffer = null; - } - } - - private void mayAllocateOffscreenBuffer() { - if (mOffscreenBuffer != null && mOffscreenBuffer.getWidth() == mOffscreenWidth - && mOffscreenBuffer.getHeight() == mOffscreenHeight) { - return; - } - freeOffscreenBuffer(); - mOffscreenBuffer = Bitmap.createBitmap( - mOffscreenWidth, mOffscreenHeight, Bitmap.Config.ARGB_8888); - mOffscreenCanvas.setBitmap(mOffscreenBuffer); - mOffscreenCanvas.translate(0, mOffscreenOffsetY); - } - - private boolean drawGestureTrails(final Canvas offscreenCanvas, final Paint paint, - final Rect dirtyRect) { - // Clear previous dirty rectangle. - if (!dirtyRect.isEmpty()) { - paint.setColor(Color.TRANSPARENT); - paint.setStyle(Paint.Style.FILL); - offscreenCanvas.drawRect(dirtyRect, paint); - } - dirtyRect.setEmpty(); - boolean needsUpdatingGestureTrail = false; - // Draw gesture trails to offscreen buffer. - synchronized (mGestureTrails) { - // Trails count == fingers count that have ever been active. - final int trailsCount = mGestureTrails.size(); - for (int index = 0; index < trailsCount; index++) { - final GestureTrail trail = mGestureTrails.valueAt(index); - needsUpdatingGestureTrail |= trail.drawGestureTrail(offscreenCanvas, paint, - mGestureTrailBoundsRect, mGestureTrailParams); - // {@link #mGestureTrailBoundsRect} has bounding box of the trail. - dirtyRect.union(mGestureTrailBoundsRect); - } - } - return needsUpdatingGestureTrail; - } - - /** - * Draws the preview - * @param canvas The canvas where the preview is drawn. - */ - @Override - public void drawPreview(final Canvas canvas) { - if (!isPreviewEnabled()) { - return; - } - mayAllocateOffscreenBuffer(); - // Draw gesture trails to offscreen buffer. - final boolean needsUpdatingGestureTrail = drawGestureTrails( - mOffscreenCanvas, mGesturePaint, mDirtyRect); - if (needsUpdatingGestureTrail) { - mDrawingHandler.postUpdateGestureTrailPreview(); - } - // Transfer offscreen buffer to screen. - if (!mDirtyRect.isEmpty()) { - mOffscreenSrcRect.set(mDirtyRect); - mOffscreenSrcRect.offset(0, mOffscreenOffsetY); - canvas.drawBitmap(mOffscreenBuffer, mOffscreenSrcRect, mDirtyRect, null); - // Note: Defer clearing the dirty rectangle here because we will get cleared - // rectangle on the canvas. - } - } - - /** - * Set the position of the preview. - * @param tracker The new location of the preview is based on the points in PointerTracker. - */ - @Override - public void setPreviewPosition(final PointerTracker tracker) { - if (!isPreviewEnabled()) { - return; - } - GestureTrail trail; - synchronized (mGestureTrails) { - trail = mGestureTrails.get(tracker.mPointerId); - if (trail == null) { - trail = new GestureTrail(); - mGestureTrails.put(tracker.mPointerId, trail); - } - } - trail.addStroke(tracker.getGestureStrokeWithPreviewPoints(), tracker.getDownTime()); - - // TODO: Should narrow the invalidate region. - getDrawingView().invalidate(); - } -} diff --git a/java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java b/java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java deleted file mode 100644 index 4c8607da8..000000000 --- a/java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * 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 License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.keyboard.internal; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffXfermode; -import android.util.AttributeSet; -import android.widget.RelativeLayout; - -import com.android.inputmethod.latin.utils.CollectionUtils; -import com.android.inputmethod.latin.utils.CoordinateUtils; - -import java.util.ArrayList; - -public final class PreviewPlacerView extends RelativeLayout { - private final int[] mKeyboardViewOrigin = CoordinateUtils.newInstance(); - - private final ArrayList mPreviews = CollectionUtils.newArrayList(); - - public PreviewPlacerView(final Context context, final AttributeSet attrs) { - super(context, attrs); - setWillNotDraw(false); - } - - public void setHardwareAcceleratedDrawingEnabled(final boolean enabled) { - if (!enabled) return; - final Paint layerPaint = new Paint(); - layerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); - setLayerType(LAYER_TYPE_HARDWARE, layerPaint); - } - - public void addPreview(final AbstractDrawingPreview preview) { - mPreviews.add(preview); - } - - public void setKeyboardViewGeometry(final int[] originCoords, final int width, - final int height) { - CoordinateUtils.copy(mKeyboardViewOrigin, originCoords); - final int count = mPreviews.size(); - for (int i = 0; i < count; i++) { - mPreviews.get(i).setKeyboardGeometry(originCoords, width, height); - } - } - - @Override - protected void onDetachedFromWindow() { - super.onDetachedFromWindow(); - final int count = mPreviews.size(); - for (int i = 0; i < count; i++) { - mPreviews.get(i).onDetachFromWindow(); - } - } - - @Override - public void onDraw(final Canvas canvas) { - super.onDraw(canvas); - final int originX = CoordinateUtils.x(mKeyboardViewOrigin); - final int originY = CoordinateUtils.y(mKeyboardViewOrigin); - canvas.translate(originX, originY); - final int count = mPreviews.size(); - for (int i = 0; i < count; i++) { - mPreviews.get(i).drawPreview(canvas); - } - canvas.translate(-originX, -originY); - } -} diff --git a/java/src/com/android/inputmethod/keyboard/internal/SlidingKeyInputDrawingPreview.java b/java/src/com/android/inputmethod/keyboard/internal/SlidingKeyInputDrawingPreview.java new file mode 100644 index 000000000..aaa4072c9 --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/internal/SlidingKeyInputDrawingPreview.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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 License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.keyboard.internal; + +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Path; +import android.view.View; + +import com.android.inputmethod.keyboard.PointerTracker; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.utils.CoordinateUtils; + +/** + * Draw rubber band preview graphics during sliding key input. + */ +public final class SlidingKeyInputDrawingPreview extends AbstractDrawingPreview { + private final float mPreviewBodyRadius; + + private boolean mShowsSlidingKeyInputPreview; + private final int[] mPreviewFrom = CoordinateUtils.newInstance(); + private final int[] mPreviewTo = CoordinateUtils.newInstance(); + + // TODO: Finalize the rubber band preview implementation. + private final RoundedLine mRoundedLine = new RoundedLine(); + private final Paint mPaint = new Paint(); + + public SlidingKeyInputDrawingPreview(final View drawingView, + final TypedArray mainKeyboardViewAttr) { + super(drawingView); + final int previewColor = mainKeyboardViewAttr.getColor( + R.styleable.MainKeyboardView_slidingKeyInputPreviewColor, 0); + final float previewRadius = mainKeyboardViewAttr.getDimension( + R.styleable.MainKeyboardView_slidingKeyInputPreviewWidth, 0) / 2.0f; + final int PERCENTAGE_INT = 100; + final float previewBodyRatio = (float)mainKeyboardViewAttr.getInt( + R.styleable.MainKeyboardView_slidingKeyInputPreviewBodyRatio, PERCENTAGE_INT) + / (float)PERCENTAGE_INT; + mPreviewBodyRadius = previewRadius * previewBodyRatio; + final int previewShadowRatioInt = mainKeyboardViewAttr.getInt( + R.styleable.MainKeyboardView_slidingKeyInputPreviewShadowRatio, 0); + if (previewShadowRatioInt > 0) { + final float previewShadowRatio = (float)previewShadowRatioInt / (float)PERCENTAGE_INT; + final float shadowRadius = previewRadius * previewShadowRatio; + mPaint.setShadowLayer(shadowRadius, 0.0f, 0.0f, previewColor); + } + mPaint.setColor(previewColor); + } + + public void dismissSlidingKeyInputPreview() { + mShowsSlidingKeyInputPreview = false; + getDrawingView().invalidate(); + } + + /** + * Draws the preview + * @param canvas The canvas where the preview is drawn. + */ + @Override + public void drawPreview(final Canvas canvas) { + if (!isPreviewEnabled() || !mShowsSlidingKeyInputPreview) { + return; + } + + // TODO: Finalize the rubber band preview implementation. + final float radius = mPreviewBodyRadius; + final Path path = mRoundedLine.makePath( + CoordinateUtils.x(mPreviewFrom), CoordinateUtils.y(mPreviewFrom), radius, + CoordinateUtils.x(mPreviewTo), CoordinateUtils.y(mPreviewTo), radius); + canvas.drawPath(path, mPaint); + } + + /** + * Set the position of the preview. + * @param tracker The new location of the preview is based on the points in PointerTracker. + */ + @Override + public void setPreviewPosition(final PointerTracker tracker) { + tracker.getDownCoordinates(mPreviewFrom); + tracker.getLastCoordinates(mPreviewTo); + mShowsSlidingKeyInputPreview = true; + getDrawingView().invalidate(); + } +} diff --git a/java/src/com/android/inputmethod/keyboard/internal/SlidingKeyInputPreview.java b/java/src/com/android/inputmethod/keyboard/internal/SlidingKeyInputPreview.java deleted file mode 100644 index 2787ebfb9..000000000 --- a/java/src/com/android/inputmethod/keyboard/internal/SlidingKeyInputPreview.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * 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 License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.keyboard.internal; - -import android.content.res.TypedArray; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.Path; -import android.view.View; - -import com.android.inputmethod.keyboard.PointerTracker; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.utils.CoordinateUtils; - -/** - * Draw rubber band preview graphics during sliding key input. - */ -public final class SlidingKeyInputPreview extends AbstractDrawingPreview { - private final float mPreviewBodyRadius; - - private boolean mShowsSlidingKeyInputPreview; - private final int[] mPreviewFrom = CoordinateUtils.newInstance(); - private final int[] mPreviewTo = CoordinateUtils.newInstance(); - - // TODO: Finalize the rubber band preview implementation. - private final RoundedLine mRoundedLine = new RoundedLine(); - private final Paint mPaint = new Paint(); - - public SlidingKeyInputPreview(final View drawingView, final TypedArray mainKeyboardViewAttr) { - super(drawingView); - final int previewColor = mainKeyboardViewAttr.getColor( - R.styleable.MainKeyboardView_slidingKeyInputPreviewColor, 0); - final float previewRadius = mainKeyboardViewAttr.getDimension( - R.styleable.MainKeyboardView_slidingKeyInputPreviewWidth, 0) / 2.0f; - final int PERCENTAGE_INT = 100; - final float previewBodyRatio = (float)mainKeyboardViewAttr.getInt( - R.styleable.MainKeyboardView_slidingKeyInputPreviewBodyRatio, PERCENTAGE_INT) - / (float)PERCENTAGE_INT; - mPreviewBodyRadius = previewRadius * previewBodyRatio; - final int previewShadowRatioInt = mainKeyboardViewAttr.getInt( - R.styleable.MainKeyboardView_slidingKeyInputPreviewShadowRatio, 0); - if (previewShadowRatioInt > 0) { - final float previewShadowRatio = (float)previewShadowRatioInt / (float)PERCENTAGE_INT; - final float shadowRadius = previewRadius * previewShadowRatio; - mPaint.setShadowLayer(shadowRadius, 0.0f, 0.0f, previewColor); - } - mPaint.setColor(previewColor); - } - - public void dismissSlidingKeyInputPreview() { - mShowsSlidingKeyInputPreview = false; - getDrawingView().invalidate(); - } - - /** - * Draws the preview - * @param canvas The canvas where the preview is drawn. - */ - @Override - public void drawPreview(final Canvas canvas) { - if (!isPreviewEnabled() || !mShowsSlidingKeyInputPreview) { - return; - } - - // TODO: Finalize the rubber band preview implementation. - final float radius = mPreviewBodyRadius; - final Path path = mRoundedLine.makePath( - CoordinateUtils.x(mPreviewFrom), CoordinateUtils.y(mPreviewFrom), radius, - CoordinateUtils.x(mPreviewTo), CoordinateUtils.y(mPreviewTo), radius); - canvas.drawPath(path, mPaint); - } - - /** - * Set the position of the preview. - * @param tracker The new location of the preview is based on the points in PointerTracker. - */ - @Override - public void setPreviewPosition(final PointerTracker tracker) { - tracker.getDownCoordinates(mPreviewFrom); - tracker.getLastCoordinates(mPreviewTo); - mShowsSlidingKeyInputPreview = true; - getDrawingView().invalidate(); - } -} -- cgit v1.2.3-83-g751a