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

Commit ead02358 authored by Ivan Chiang's avatar Ivan Chiang Committed by Android (Google) Code Review
Browse files

Merge "Refactor the inSelectRegion method in holders"

parents 713e2f9b 9ae4d377
Loading
Loading
Loading
Loading
+0 −16
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.documentsui.dirlist;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Rect;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
@@ -40,8 +39,6 @@ public abstract class DocumentHolder
        extends RecyclerView.ViewHolder implements View.OnKeyListener {

    static final float DISABLED_ALPHA = 0.3f;
    private static final int[] sCoord = new int[2];
    private static final Rect sViewRect = new Rect();

    protected final Context mContext;

@@ -154,17 +151,4 @@ public abstract class DocumentHolder
    static ViewPropertyAnimator fade(ImageView view, float alpha) {
        return view.animate().setDuration(Shared.CHECK_ANIMATION_DURATION).alpha(alpha);
    }

    static boolean isTouchInViewRegion(View view, MotionEvent event) {
        if (view == null || event == null || !view.isAttachedToWindow()) {
            return false;
        }

        view.getLocationOnScreen(sCoord);

        sViewRect.set(sCoord[0], sCoord[1], sCoord[0] + view.getMeasuredWidth(),
                sCoord[1] + view.getMeasuredHeight());

        return sViewRect.contains((int) event.getRawX(), (int) event.getRawY());
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import static com.android.documentsui.base.DocumentInfo.getCursorString;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Rect;
import android.provider.DocumentsContract.Document;
import android.view.MotionEvent;
import android.view.View;
@@ -29,6 +28,7 @@ import android.widget.ImageView;
import android.widget.TextView;

import com.android.documentsui.R;
import com.android.documentsui.ui.Views;

final class GridDirectoryHolder extends DocumentHolder {

@@ -69,7 +69,7 @@ final class GridDirectoryHolder extends DocumentHolder {

    @Override
    public boolean inSelectRegion(MotionEvent event) {
        return DocumentHolder.isTouchInViewRegion(mIconLayout, event);
        return Views.isEventOver(event, mIconLayout);
    }

    /**
+2 −2
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import androidx.annotation.ColorInt;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Rect;
import android.provider.DocumentsContract.Document;
import android.text.format.Formatter;
import android.view.MotionEvent;
@@ -36,6 +35,7 @@ import com.android.documentsui.R;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.Shared;
import com.android.documentsui.roots.RootCursorWrapper;
import com.android.documentsui.ui.Views;

final class GridDocumentHolder extends DocumentHolder {

@@ -114,7 +114,7 @@ final class GridDocumentHolder extends DocumentHolder {

    @Override
    public boolean inSelectRegion(MotionEvent event) {
        return DocumentHolder.isTouchInViewRegion(mIconLayout, event);
        return Views.isEventOver(event, mIconLayout);
    }

    /**
+2 −1
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.Lookup;
import com.android.documentsui.base.Shared;
import com.android.documentsui.roots.RootCursorWrapper;
import com.android.documentsui.ui.Views;

final class ListDocumentHolder extends DocumentHolder {

@@ -140,7 +141,7 @@ final class ListDocumentHolder extends DocumentHolder {

    @Override
    public boolean inSelectRegion(MotionEvent event) {
        return DocumentHolder.isTouchInViewRegion(mIconLayout, event);
        return Views.isEventOver(event, mIconLayout);
    }

    /**
+49 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.documentsui.ui;

import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;

/**
 * A utility class for working with Views.
 */
public final class Views {

    private Views() {}

    /**
     * Return whether the event is in the view's region
     * @param event the motion event
     * @param view the view to check the selection region
     * @return True, if the event is in the region. Otherwise, return false.
     */
    public static boolean isEventOver(MotionEvent event, View view) {
        if (view == null || event == null || !view.isAttachedToWindow()) {
            return false;
        }

        final int[] coord = new int[2];
        view.getLocationOnScreen(coord);

        final Rect viewRect = new Rect(coord[0], coord[1], coord[0] + view.getMeasuredWidth(),
                coord[1] + view.getMeasuredHeight());

        return viewRect.contains((int) event.getRawX(), (int) event.getRawY());
    }
}