2013-01-21 10:17:17 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2014-06-16 22:15:47 +00:00
|
|
|
import android.os.Handler;
|
2013-01-21 10:17:17 +00:00
|
|
|
import android.util.SparseArray;
|
|
|
|
|
|
|
|
import com.android.inputmethod.keyboard.PointerTracker;
|
|
|
|
|
|
|
|
/**
|
2013-12-24 09:31:49 +00:00
|
|
|
* Draw preview graphics of multiple gesture trails during gesture input.
|
2013-01-21 10:17:17 +00:00
|
|
|
*/
|
2014-06-16 22:15:47 +00:00
|
|
|
public final class GestureTrailsDrawingPreview extends AbstractDrawingPreview implements Runnable {
|
2014-05-23 11:18:17 +00:00
|
|
|
private final SparseArray<GestureTrailDrawingPoints> mGestureTrails = new SparseArray<>();
|
2013-12-24 09:31:49 +00:00
|
|
|
private final GestureTrailDrawingParams mDrawingParams;
|
2013-01-21 10:17:17 +00:00
|
|
|
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();
|
2013-05-13 04:12:12 +00:00
|
|
|
private final Rect mGestureTrailBoundsRect = new Rect(); // per trail
|
2013-01-21 10:17:17 +00:00
|
|
|
|
2014-06-16 22:15:47 +00:00
|
|
|
private final Handler mDrawingHandler = new Handler();
|
2013-01-21 10:17:17 +00:00
|
|
|
|
2014-06-16 22:15:47 +00:00
|
|
|
public GestureTrailsDrawingPreview(final TypedArray mainKeyboardViewAttr) {
|
2013-12-24 09:31:49 +00:00
|
|
|
mDrawingParams = new GestureTrailDrawingParams(mainKeyboardViewAttr);
|
2013-01-21 10:17:17 +00:00
|
|
|
final Paint gesturePaint = new Paint();
|
|
|
|
gesturePaint.setAntiAlias(true);
|
|
|
|
gesturePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
|
|
|
|
mGesturePaint = gesturePaint;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-04-07 07:10:48 +00:00
|
|
|
public void setKeyboardViewGeometry(final int[] originCoords, final int width,
|
|
|
|
final int height) {
|
|
|
|
super.setKeyboardViewGeometry(originCoords, width, height);
|
2013-12-24 09:31:49 +00:00
|
|
|
mOffscreenOffsetY = (int)(height
|
|
|
|
* GestureStrokeRecognitionPoints.EXTRA_GESTURE_TRAIL_AREA_ABOVE_KEYBOARD_RATIO);
|
2013-01-21 10:17:17 +00:00
|
|
|
mOffscreenWidth = width;
|
|
|
|
mOffscreenHeight = mOffscreenOffsetY + height;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-12-24 09:26:55 +00:00
|
|
|
public void onDeallocateMemory() {
|
2013-07-30 11:29:19 +00:00
|
|
|
freeOffscreenBuffer();
|
|
|
|
}
|
|
|
|
|
2013-01-21 10:17:17 +00:00
|
|
|
private void freeOffscreenBuffer() {
|
2013-07-30 11:29:19 +00:00
|
|
|
mOffscreenCanvas.setBitmap(null);
|
2013-08-02 09:51:57 +00:00
|
|
|
mOffscreenCanvas.setMatrix(null);
|
2013-01-21 10:17:17 +00:00
|
|
|
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();
|
2013-05-13 04:12:12 +00:00
|
|
|
boolean needsUpdatingGestureTrail = false;
|
2013-01-21 10:17:17 +00:00
|
|
|
// Draw gesture trails to offscreen buffer.
|
2013-05-13 04:12:12 +00:00
|
|
|
synchronized (mGestureTrails) {
|
2013-01-21 10:17:17 +00:00
|
|
|
// Trails count == fingers count that have ever been active.
|
2013-05-13 04:12:12 +00:00
|
|
|
final int trailsCount = mGestureTrails.size();
|
2013-01-21 10:17:17 +00:00
|
|
|
for (int index = 0; index < trailsCount; index++) {
|
2013-12-24 09:31:49 +00:00
|
|
|
final GestureTrailDrawingPoints trail = mGestureTrails.valueAt(index);
|
2013-05-13 04:12:12 +00:00
|
|
|
needsUpdatingGestureTrail |= trail.drawGestureTrail(offscreenCanvas, paint,
|
2013-12-24 09:31:49 +00:00
|
|
|
mGestureTrailBoundsRect, mDrawingParams);
|
2013-05-13 04:12:12 +00:00
|
|
|
// {@link #mGestureTrailBoundsRect} has bounding box of the trail.
|
|
|
|
dirtyRect.union(mGestureTrailBoundsRect);
|
2013-01-21 10:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-13 04:12:12 +00:00
|
|
|
return needsUpdatingGestureTrail;
|
2013-01-21 10:17:17 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 22:15:47 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
// Update preview.
|
|
|
|
invalidateDrawingView();
|
|
|
|
}
|
|
|
|
|
2013-01-21 10:17:17 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2013-05-13 04:12:12 +00:00
|
|
|
final boolean needsUpdatingGestureTrail = drawGestureTrails(
|
2013-01-21 10:17:17 +00:00
|
|
|
mOffscreenCanvas, mGesturePaint, mDirtyRect);
|
2013-05-13 04:12:12 +00:00
|
|
|
if (needsUpdatingGestureTrail) {
|
2014-06-16 22:15:47 +00:00
|
|
|
mDrawingHandler.removeCallbacks(this);
|
|
|
|
mDrawingHandler.postDelayed(this, mDrawingParams.mUpdateInterval);
|
2013-01-21 10:17:17 +00:00
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
}
|
2013-12-24 09:31:49 +00:00
|
|
|
GestureTrailDrawingPoints trail;
|
2013-05-13 04:12:12 +00:00
|
|
|
synchronized (mGestureTrails) {
|
|
|
|
trail = mGestureTrails.get(tracker.mPointerId);
|
2013-01-21 10:17:17 +00:00
|
|
|
if (trail == null) {
|
2013-12-24 09:31:49 +00:00
|
|
|
trail = new GestureTrailDrawingPoints();
|
2013-05-13 04:12:12 +00:00
|
|
|
mGestureTrails.put(tracker.mPointerId, trail);
|
2013-01-21 10:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-24 09:31:49 +00:00
|
|
|
trail.addStroke(tracker.getGestureStrokeDrawingPoints(), tracker.getDownTime());
|
2013-01-21 10:17:17 +00:00
|
|
|
|
|
|
|
// TODO: Should narrow the invalidate region.
|
2014-06-16 22:15:47 +00:00
|
|
|
invalidateDrawingView();
|
2013-01-21 10:17:17 +00:00
|
|
|
}
|
|
|
|
}
|