summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2016-07-26 17:55:58 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2016-07-26 17:55:58 +0000
commit8c06310b068291facb3e6e1224f48d866f0c0040 (patch)
treea938b74a855ca59cd01649c04834c72c4e2d1433 /src
parentc664f24de25675ad9c2e732345a9c470686d6971 (diff)
parentbf547ff6f0011378ef3f9caa81f6825ba0fd48de (diff)
downloadandroid_packages_apps_Trebuchet-8c06310b068291facb3e6e1224f48d866f0c0040.tar.gz
android_packages_apps_Trebuchet-8c06310b068291facb3e6e1224f48d866f0c0040.tar.bz2
android_packages_apps_Trebuchet-8c06310b068291facb3e6e1224f48d866f0c0040.zip
Merge "Adding TransformingTouchDelegate to allow transforming touch events before sending to the delegate." into ub-launcher3-calgary
Diffstat (limited to 'src')
-rw-r--r--src/com/android/launcher3/pageindicators/PageIndicatorLineCaret.java15
-rw-r--r--src/com/android/launcher3/util/TransformingTouchDelegate.java87
2 files changed, 97 insertions, 5 deletions
diff --git a/src/com/android/launcher3/pageindicators/PageIndicatorLineCaret.java b/src/com/android/launcher3/pageindicators/PageIndicatorLineCaret.java
index 0b947913e..0d771adb7 100644
--- a/src/com/android/launcher3/pageindicators/PageIndicatorLineCaret.java
+++ b/src/com/android/launcher3/pageindicators/PageIndicatorLineCaret.java
@@ -9,14 +9,12 @@ import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
-import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.graphics.ColorUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Property;
-import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewConfiguration;
@@ -24,6 +22,7 @@ import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.dynamicui.ExtractedColors;
+import com.android.launcher3.util.TransformingTouchDelegate;
/**
* A PageIndicator that briefly shows a fraction of a line when moving between pages.
@@ -61,7 +60,7 @@ public class PageIndicatorLineCaret extends PageIndicator {
private Paint mLinePaint;
private Launcher mLauncher;
private final int mLineHeight;
- private final Rect mTouchHitRect = new Rect();
+ private final TransformingTouchDelegate mTouchDelegate;
private final int mTouchExtensionHeight;
private final int mCaretSizePx;
private final int mCaretWorkspaceOffsetPx;
@@ -142,6 +141,13 @@ public class PageIndicatorLineCaret extends PageIndicator {
mLineHeight = res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_line_height);
mTouchExtensionHeight = res.getDimensionPixelSize(
R.dimen.dynamic_grid_page_indicator_extra_touch_height);
+ mTouchDelegate = new TransformingTouchDelegate(this);
+ }
+
+ @Override
+ protected void onAttachedToWindow() {
+ super.onAttachedToWindow();
+ mLauncher.getDragLayer().setTouchDelegate(mTouchDelegate);
}
@Override
@@ -157,9 +163,8 @@ public class PageIndicatorLineCaret extends PageIndicator {
View parent = mLauncher.getDragLayer();
sTempCoords[0] = sTempCoords[1] = 0;
Utilities.getDescendantCoordRelativeToAncestor(this, parent, sTempCoords, true);
- mTouchHitRect.set(sTempCoords[0], sTempCoords[1], sTempCoords[0] + this.getWidth(),
+ mTouchDelegate.setBounds(sTempCoords[0], sTempCoords[1], sTempCoords[0] + this.getWidth(),
sTempCoords[1] + getHeight() + mTouchExtensionHeight);
- parent.setTouchDelegate(new TouchDelegate(mTouchHitRect, this));
}
@Override
diff --git a/src/com/android/launcher3/util/TransformingTouchDelegate.java b/src/com/android/launcher3/util/TransformingTouchDelegate.java
new file mode 100644
index 000000000..da1de5e77
--- /dev/null
+++ b/src/com/android/launcher3/util/TransformingTouchDelegate.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2016 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.launcher3.util;
+
+import android.graphics.Rect;
+import android.graphics.RectF;
+import android.view.MotionEvent;
+import android.view.TouchDelegate;
+import android.view.View;
+
+/**
+ * This class differs from the framework {@link TouchDelegate} in that it transforms the
+ * coordinates of the motion event to the provided bounds.
+ *
+ * You can also modify the bounds post construction. Since the bounds are available during layout,
+ * this avoids new object creation during every layout.
+ */
+public class TransformingTouchDelegate extends TouchDelegate {
+ private static final Rect sTempRect = new Rect();
+
+ private final RectF mBounds;
+
+ private View mDelegateView;
+ private boolean mDelegateTargeted;
+
+ public TransformingTouchDelegate(View delegateView) {
+ super(sTempRect, delegateView);
+
+ mDelegateView = delegateView;
+ mBounds = new RectF();
+ }
+
+ public void setBounds(int left, int top, int right, int bottom) {
+ mBounds.set(left, top, right, bottom);
+ }
+
+ public void setDelegateView(View view) {
+ mDelegateView = view;
+ }
+
+ /**
+ * Will forward touch events to the delegate view if the event is within the bounds
+ * specified in the constructor.
+ *
+ * @param event The touch event to forward
+ * @return True if the event was forwarded to the delegate, false otherwise.
+ */
+ public boolean onTouchEvent(MotionEvent event) {
+ boolean sendToDelegate = false;
+ switch (event.getAction()) {
+ case MotionEvent.ACTION_DOWN:
+ mDelegateTargeted = mBounds.contains(event.getX(), event.getY());
+ if (mDelegateTargeted) {
+ sendToDelegate = true;
+ }
+ break;
+ case MotionEvent.ACTION_MOVE:
+ sendToDelegate = mDelegateTargeted;
+ break;
+ case MotionEvent.ACTION_UP:
+ case MotionEvent.ACTION_CANCEL:
+ sendToDelegate = mDelegateTargeted;
+ mDelegateTargeted = false;
+ break;
+ }
+ boolean handled = false;
+ if (sendToDelegate) {
+ event.offsetLocation(-mBounds.left, -mBounds.top);
+ handled = mDelegateView.dispatchTouchEvent(event);
+ event.offsetLocation(mBounds.left, mBounds.top);
+ }
+ return handled;
+ }
+}