/*
* Copyright (C) 2009 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 android.gesture;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.AnimationUtils;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.FrameLayout;
import android.os.SystemClock;
import android.annotation.Widget;
import com.android.internal.R;
import java.util.ArrayList;
/**
* A transparent overlay for gesture input that can be placed on top of other
* widgets or contain other widgets.
*
* @attr ref android.R.styleable#GestureOverlayView_eventsInterceptionEnabled
* @attr ref android.R.styleable#GestureOverlayView_fadeDuration
* @attr ref android.R.styleable#GestureOverlayView_fadeOffset
* @attr ref android.R.styleable#GestureOverlayView_fadeEnabled
* @attr ref android.R.styleable#GestureOverlayView_gestureStrokeWidth
* @attr ref android.R.styleable#GestureOverlayView_gestureStrokeAngleThreshold
* @attr ref android.R.styleable#GestureOverlayView_gestureStrokeLengthThreshold
* @attr ref android.R.styleable#GestureOverlayView_gestureStrokeSquarenessThreshold
* @attr ref android.R.styleable#GestureOverlayView_gestureStrokeType
* @attr ref android.R.styleable#GestureOverlayView_gestureColor
* @attr ref android.R.styleable#GestureOverlayView_orientation
* @attr ref android.R.styleable#GestureOverlayView_uncertainGestureColor
*/
@Widget
public class GestureOverlayView extends FrameLayout {
public static final int GESTURE_STROKE_TYPE_SINGLE = 0;
public static final int GESTURE_STROKE_TYPE_MULTIPLE = 1;
public static final int ORIENTATION_HORIZONTAL = 0;
public static final int ORIENTATION_VERTICAL = 1;
private static final int FADE_ANIMATION_RATE = 16;
private static final boolean GESTURE_RENDERING_ANTIALIAS = true;
private static final boolean DITHER_FLAG = true;
private final Paint mGesturePaint = new Paint();
private long mFadeDuration = 150;
private long mFadeOffset = 420;
private long mFadingStart;
private boolean mFadingHasStarted;
private boolean mFadeEnabled = true;
private int mCurrentColor;
private int mCertainGestureColor = 0xFFFFFF00;
private int mUncertainGestureColor = 0x48FFFF00;
private float mGestureStrokeWidth = 12.0f;
private int mInvalidateExtraBorder = 10;
private int mGestureStrokeType = GESTURE_STROKE_TYPE_SINGLE;
private float mGestureStrokeLengthThreshold = 50.0f;
private float mGestureStrokeSquarenessTreshold = 0.275f;
private float mGestureStrokeAngleThreshold = 40.0f;
private int mOrientation = ORIENTATION_VERTICAL;
private final Rect mInvalidRect = new Rect();
private final Path mPath = new Path();
private boolean mGestureVisible = true;
private float mX;
private float mY;
private float mCurveEndX;
private float mCurveEndY;
private float mTotalLength;
private boolean mIsGesturing = false;
private boolean mPreviousWasGesturing = false;
private boolean mInterceptEvents = true;
private boolean mIsListeningForGestures;
private boolean mResetGesture;
// current gesture
private Gesture mCurrentGesture;
private final ArrayList<GesturePoint> mStrokeBuffer = new ArrayList<GesturePoint>(100);
// TODO: Make this a list of WeakReferences
private final ArrayList<OnGestureListener> mOnGestureListeners =
new ArrayList<OnGestureListener>();
// TODO: Make this a list of WeakReferences
private final ArrayList<OnGesturePerformedListener> mOnGesturePerformedListeners =
new ArrayList<OnGesturePerformedListener>();
// TODO: Make this a list of WeakReferences
private final ArrayList<OnGesturingListener> mOnGesturingListeners =
new ArrayList<OnGesturingListener>();
private boolean mHandleGestureActions;
// fading out effect
private boolean mIsFadingOut = false;
private float mFadingAlpha = 1.0f;
private final AccelerateDecelerateInterpolator mInterpolator =
new AccelerateDecelerateInterpolator();
private final FadeOutRunnable mFadingOut = new FadeOutRunnable();
public GestureOverlayView(Context context) {
super(context);
init();
}
public GestureOverlayView(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.gestureOverlayViewStyle);
}
public GestureOverlayView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.GestureOverlayView, defStyle, 0);
mGestureStrokeWidth = a.getFloat(R.styleable.GestureOverlayView_gestureStrokeWidth,
mGestureStrokeWidth);
mInvalidateExtraBorder = Math.max(1, ((int) mGestureStrokeWidth) - 1);
mCertainGestureColor = a.getColor(R.styleable.GestureOverlayView_gestureColor,
mCertainGestureColor);
mUncertainGestureColor = a.getColor(R.styleable.GestureOverlayView_uncertainGestureColor,
mUncertainGestureColor);
mFadeDuration = a.getInt(R.styleable.GestureOverlayView_fadeDuration, (int) mFadeDuration);
mFadeOffset = a.getInt(R.styleable.GestureOverlayView_fadeOffset, (int) mFadeOffset);
mGestureStrokeType = a.getInt(R.styleable.GestureOverlayView_gestureStrokeType,
mGestureStrokeType);
mGestureStrokeLengthThreshold = a.getFloat(
R.styleable.GestureOverlayView_gestureStrokeLengthThreshold,
mGestureStrokeLengthThreshold);
mGestureStrokeAngleThreshold = a.getFloat(
R.styleable.GestureOverlayView_gestureStrokeAngleThreshold,
mGestureStrokeAngleThreshold);
mGestureStrokeSquarenessTreshold = a.getFloat(
R.styleable.GestureOverlayView_gestureStrokeSquarenessThreshold,
mGestureStrokeSquarenessTreshold);
mInterceptEvents = a.getBoolean(R.styleable.GestureOverlayView_eventsInterceptionEnabled,
mInterceptEvents);
mFadeEnabled = a.getBoolean(R.styleable.GestureOverlayView_fadeEnabled,
mFadeEnabled);
mOrientation = a.getInt(R.styleable.GestureOverlayView_orientation, mOrientation);
a.recycle();
init();
}
private void init() {
setWillNotDraw(false);
final Paint gesturePaint = mGesturePaint;
gesturePaint.setAntiAlias(GESTURE_RENDERING_ANTIALIAS);
gesturePaint.setColor(mCertainGestureColor);
gesturePaint.setStyle(Paint.Style.STROKE);
gesturePaint.setStrokeJoin(Paint.Join.ROUND);
gesturePaint.setStrokeCap(Paint.Cap.ROUND);
gesturePaint.setStrokeWidth(mGestureStrokeWidth);
gesturePaint.setDither(DITHER_FLAG);
mCurrentColor = mCertainGestureColor;
setPaintAlpha(255);
}
public ArrayList<GesturePoint> getCurrentStroke() {
return mStrokeBuffer;
}
public int getOrientation() {
return mOrientation;
}
public void setOrientation(int orientation) {
mOrientation = orientation;
}
public v