Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2013 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.Path;
     23 import android.view.View;
     24 
     25 import com.android.inputmethod.keyboard.PointerTracker;
     26 import com.android.inputmethod.latin.R;
     27 import com.android.inputmethod.latin.utils.CoordinateUtils;
     28 
     29 /**
     30  * Draw rubber band preview graphics during sliding key input.
     31  */
     32 public final class SlidingKeyInputPreview extends AbstractDrawingPreview {
     33     private final float mPreviewBodyRadius;
     34 
     35     private boolean mShowsSlidingKeyInputPreview;
     36     private final int[] mPreviewFrom = CoordinateUtils.newInstance();
     37     private final int[] mPreviewTo = CoordinateUtils.newInstance();
     38 
     39     // TODO: Finalize the rubber band preview implementation.
     40     private final RoundedLine mRoundedLine = new RoundedLine();
     41     private final Paint mPaint = new Paint();
     42 
     43     public SlidingKeyInputPreview(final View drawingView, final TypedArray mainKeyboardViewAttr) {
     44         super(drawingView);
     45         final int previewColor = mainKeyboardViewAttr.getColor(
     46                 R.styleable.MainKeyboardView_slidingKeyInputPreviewColor, 0);
     47         final float previewRadius = mainKeyboardViewAttr.getDimension(
     48                 R.styleable.MainKeyboardView_slidingKeyInputPreviewWidth, 0) / 2.0f;
     49         final int PERCENTAGE_INT = 100;
     50         final float previewBodyRatio = (float)mainKeyboardViewAttr.getInt(
     51                 R.styleable.MainKeyboardView_slidingKeyInputPreviewBodyRatio, PERCENTAGE_INT)
     52                 / (float)PERCENTAGE_INT;
     53         mPreviewBodyRadius = previewRadius * previewBodyRatio;
     54         final int previewShadowRatioInt = mainKeyboardViewAttr.getInt(
     55                 R.styleable.MainKeyboardView_slidingKeyInputPreviewShadowRatio, 0);
     56         if (previewShadowRatioInt > 0) {
     57             final float previewShadowRatio = (float)previewShadowRatioInt / (float)PERCENTAGE_INT;
     58             final float shadowRadius = previewRadius * previewShadowRatio;
     59             mPaint.setShadowLayer(shadowRadius, 0.0f, 0.0f, previewColor);
     60         }
     61         mPaint.setColor(previewColor);
     62     }
     63 
     64     public void dismissSlidingKeyInputPreview() {
     65         mShowsSlidingKeyInputPreview = false;
     66         getDrawingView().invalidate();
     67     }
     68 
     69     /**
     70      * Draws the preview
     71      * @param canvas The canvas where the preview is drawn.
     72      */
     73     @Override
     74     public void drawPreview(final Canvas canvas) {
     75         if (!isPreviewEnabled() || !mShowsSlidingKeyInputPreview) {
     76             return;
     77         }
     78 
     79         // TODO: Finalize the rubber band preview implementation.
     80         final float radius = mPreviewBodyRadius;
     81         final Path path = mRoundedLine.makePath(
     82                 CoordinateUtils.x(mPreviewFrom), CoordinateUtils.y(mPreviewFrom), radius,
     83                 CoordinateUtils.x(mPreviewTo), CoordinateUtils.y(mPreviewTo), radius);
     84         canvas.drawPath(path, mPaint);
     85     }
     86 
     87     /**
     88      * Set the position of the preview.
     89      * @param tracker The new location of the preview is based on the points in PointerTracker.
     90      */
     91     @Override
     92     public void setPreviewPosition(final PointerTracker tracker) {
     93         tracker.getDownCoordinates(mPreviewFrom);
     94         tracker.getLastCoordinates(mPreviewTo);
     95         mShowsSlidingKeyInputPreview = true;
     96         getDrawingView().invalidate();
     97     }
     98 }
     99