Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bf547ff6 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Adding TransformingTouchDelegate to allow transforming touch events before

sending to the delegate.

Change-Id: I1dfb727fe433bed05dd5ac3d0f779ede4ec1d292
parent 77ba6b9c
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -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 static final Property<PageIndicatorLineCaret, Integer> PAINT_ALPHA
@@ -135,6 +134,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
@@ -149,9 +155,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
+87 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}