Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.inputmethod.keyboard.internal;
     18 
     19 import android.content.res.TypedArray;
     20 import android.graphics.Canvas;
     21 import android.graphics.Paint;
     22 import android.graphics.Paint.Align;
     23 import android.graphics.Rect;
     24 import android.graphics.RectF;
     25 import android.text.TextUtils;
     26 import android.view.View;
     27 
     28 import com.android.inputmethod.keyboard.PointerTracker;
     29 import com.android.inputmethod.latin.R;
     30 import com.android.inputmethod.latin.SuggestedWords;
     31 import com.android.inputmethod.latin.utils.CoordinateUtils;
     32 
     33 /**
     34  * The class for single gesture preview text. The class for multiple gesture preview text will be
     35  * derived from it.
     36  *
     37  * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextSize
     38  * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextColor
     39  * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextOffset
     40  * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewColor
     41  * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewHorizontalPadding
     42  * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewVerticalPadding
     43  * @attr ref R.styleable#KeyboardView_gestureFloatingPreviewRoundRadius
     44  */
     45 public class GestureFloatingPreviewText extends AbstractDrawingPreview {
     46     protected static final class GesturePreviewTextParams {
     47         public final int mGesturePreviewTextOffset;
     48         public final int mGesturePreviewTextHeight;
     49         public final float mGesturePreviewHorizontalPadding;
     50         public final float mGesturePreviewVerticalPadding;
     51         public final float mGesturePreviewRoundRadius;
     52 
     53         private final int mGesturePreviewTextSize;
     54         private final int mGesturePreviewTextColor;
     55         private final int mGesturePreviewColor;
     56         private final Paint mPaint = new Paint();
     57 
     58         private static final char[] TEXT_HEIGHT_REFERENCE_CHAR = { 'M' };
     59 
     60         public GesturePreviewTextParams(final TypedArray mainKeyboardViewAttr) {
     61             mGesturePreviewTextSize = mainKeyboardViewAttr.getDimensionPixelSize(
     62                     R.styleable.MainKeyboardView_gestureFloatingPreviewTextSize, 0);
     63             mGesturePreviewTextColor = mainKeyboardViewAttr.getColor(
     64                     R.styleable.MainKeyboardView_gestureFloatingPreviewTextColor, 0);
     65             mGesturePreviewTextOffset = mainKeyboardViewAttr.getDimensionPixelOffset(
     66                     R.styleable.MainKeyboardView_gestureFloatingPreviewTextOffset, 0);
     67             mGesturePreviewColor = mainKeyboardViewAttr.getColor(
     68                     R.styleable.MainKeyboardView_gestureFloatingPreviewColor, 0);
     69             mGesturePreviewHorizontalPadding = mainKeyboardViewAttr.getDimension(
     70                     R.styleable.MainKeyboardView_gestureFloatingPreviewHorizontalPadding, 0.0f);
     71             mGesturePreviewVerticalPadding = mainKeyboardViewAttr.getDimension(
     72                     R.styleable.MainKeyboardView_gestureFloatingPreviewVerticalPadding, 0.0f);
     73             mGesturePreviewRoundRadius = mainKeyboardViewAttr.getDimension(
     74                     R.styleable.MainKeyboardView_gestureFloatingPreviewRoundRadius, 0.0f);
     75 
     76             final Paint textPaint = getTextPaint();
     77             final Rect textRect = new Rect();
     78             textPaint.getTextBounds(TEXT_HEIGHT_REFERENCE_CHAR, 0, 1, textRect);
     79             mGesturePreviewTextHeight = textRect.height();
     80         }
     81 
     82         public Paint getTextPaint() {
     83             mPaint.setAntiAlias(true);
     84             mPaint.setTextAlign(Align.CENTER);
     85             mPaint.setTextSize(mGesturePreviewTextSize);
     86             mPaint.setColor(mGesturePreviewTextColor);
     87             return mPaint;
     88         }
     89 
     90         public Paint getBackgroundPaint() {
     91             mPaint.setColor(mGesturePreviewColor);
     92             return mPaint;
     93         }
     94     }
     95 
     96     private final GesturePreviewTextParams mParams;
     97     private final RectF mGesturePreviewRectangle = new RectF();
     98     private int mPreviewTextX;
     99     private int mPreviewTextY;
    100     private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
    101     private final int[] mLastPointerCoords = CoordinateUtils.newInstance();
    102 
    103     public GestureFloatingPreviewText(final View drawingView, final TypedArray typedArray) {
    104         super(drawingView);
    105         mParams = new GesturePreviewTextParams(typedArray);
    106     }
    107 
    108     public void setSuggetedWords(final SuggestedWords suggestedWords) {
    109         if (!isPreviewEnabled()) {
    110             return;
    111         }
    112         mSuggestedWords = suggestedWords;
    113         updatePreviewPosition();
    114     }
    115 
    116     @Override
    117     public void setPreviewPosition(final PointerTracker tracker) {
    118         if (!isPreviewEnabled()) {
    119             return;
    120         }
    121         tracker.getLastCoordinates(mLastPointerCoords);
    122         updatePreviewPosition();
    123     }
    124 
    125     /**
    126      * Draws gesture preview text
    127      * @param canvas The canvas where preview text is drawn.
    128      */
    129     @Override
    130     public void drawPreview(final Canvas canvas) {
    131         if (!isPreviewEnabled() || mSuggestedWords.isEmpty()
    132                 || TextUtils.isEmpty(mSuggestedWords.getWord(0))) {
    133             return;
    134         }
    135         final float round = mParams.mGesturePreviewRoundRadius;
    136         canvas.drawRoundRect(
    137                 mGesturePreviewRectangle, round, round, mParams.getBackgroundPaint());
    138         final String text = mSuggestedWords.getWord(0);
    139         canvas.drawText(text, mPreviewTextX, mPreviewTextY, mParams.getTextPaint());
    140     }
    141 
    142     /**
    143      * Updates gesture preview text position based on mLastPointerCoords.
    144      */
    145     protected void updatePreviewPosition() {
    146         if (mSuggestedWords.isEmpty() || TextUtils.isEmpty(mSuggestedWords.getWord(0))) {
    147             getDrawingView().invalidate();
    148             return;
    149         }
    150         final String text = mSuggestedWords.getWord(0);
    151 
    152         final RectF rectangle = mGesturePreviewRectangle;
    153 
    154         final int textHeight = mParams.mGesturePreviewTextHeight;
    155         final float textWidth = mParams.getTextPaint().measureText(text);
    156         final float hPad = mParams.mGesturePreviewHorizontalPadding;
    157         final float vPad = mParams.mGesturePreviewVerticalPadding;
    158         final float rectWidth = textWidth + hPad * 2.0f;
    159         final float rectHeight = textHeight + vPad * 2.0f;
    160 
    161         final int displayWidth = getDrawingView().getResources().getDisplayMetrics().widthPixels;
    162         final float rectX = Math.min(
    163                 Math.max(CoordinateUtils.x(mLastPointerCoords) - rectWidth / 2.0f, 0.0f),
    164                 displayWidth - rectWidth);
    165         final float rectY = CoordinateUtils.y(mLastPointerCoords)
    166                 - mParams.mGesturePreviewTextOffset - rectHeight;
    167         rectangle.set(rectX, rectY, rectX + rectWidth, rectY + rectHeight);
    168 
    169         mPreviewTextX = (int)(rectX + hPad + textWidth / 2.0f);
    170         mPreviewTextY = (int)(rectY + vPad) + textHeight;
    171         // TODO: Should narrow the invalidate region.
    172         getDrawingView().invalidate();
    173     }
    174 }
    175