Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.statusbar.phone; |
| 16 | |
| 17 | import android.content.Context; |
Jason Monk | e57e9dc | 2017-08-04 10:59:13 -0400 | [diff] [blame] | 18 | import android.content.res.Configuration; |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 19 | import android.util.AttributeSet; |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 20 | import android.util.Pair; |
| 21 | import android.view.MotionEvent; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
| 24 | import android.widget.FrameLayout; |
| 25 | |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 26 | import androidx.annotation.VisibleForTesting; |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Comparator; |
| 30 | |
| 31 | /** |
| 32 | * Redirects touches that aren't handled by any child view to the nearest |
| 33 | * clickable child. Only takes effect on <sw600dp. |
| 34 | */ |
| 35 | public class NearestTouchFrame extends FrameLayout { |
| 36 | |
| 37 | private final ArrayList<View> mClickableChildren = new ArrayList<>(); |
| 38 | private final boolean mIsActive; |
| 39 | private final int[] mTmpInt = new int[2]; |
| 40 | private final int[] mOffset = new int[2]; |
| 41 | private View mTouchingChild; |
| 42 | |
| 43 | public NearestTouchFrame(Context context, AttributeSet attrs) { |
Jason Monk | e57e9dc | 2017-08-04 10:59:13 -0400 | [diff] [blame] | 44 | this(context, attrs, context.getResources().getConfiguration()); |
| 45 | } |
| 46 | |
| 47 | @VisibleForTesting |
| 48 | NearestTouchFrame(Context context, AttributeSet attrs, Configuration c) { |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 49 | super(context, attrs); |
Jason Monk | e57e9dc | 2017-08-04 10:59:13 -0400 | [diff] [blame] | 50 | mIsActive = c.smallestScreenWidthDp < 600; |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 55 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 56 | mClickableChildren.clear(); |
| 57 | addClickableChildren(this); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 62 | super.onLayout(changed, left, top, right, bottom); |
| 63 | getLocationInWindow(mOffset); |
| 64 | } |
| 65 | |
| 66 | private void addClickableChildren(ViewGroup group) { |
| 67 | final int N = group.getChildCount(); |
| 68 | for (int i = 0; i < N; i++) { |
| 69 | View child = group.getChildAt(i); |
| 70 | if (child.isClickable()) { |
| 71 | mClickableChildren.add(child); |
| 72 | } else if (child instanceof ViewGroup) { |
| 73 | addClickableChildren((ViewGroup) child); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean onTouchEvent(MotionEvent event) { |
| 80 | if (mIsActive) { |
| 81 | if (event.getAction() == MotionEvent.ACTION_DOWN) { |
| 82 | mTouchingChild = findNearestChild(event); |
| 83 | } |
| 84 | if (mTouchingChild != null) { |
| 85 | event.offsetLocation(mTouchingChild.getWidth() / 2 - event.getX(), |
| 86 | mTouchingChild.getHeight() / 2 - event.getY()); |
Jason Monk | 3dd17a7 | 2017-09-06 14:10:39 -0400 | [diff] [blame] | 87 | return mTouchingChild.getVisibility() == VISIBLE |
| 88 | && mTouchingChild.dispatchTouchEvent(event); |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | return super.onTouchEvent(event); |
| 92 | } |
| 93 | |
| 94 | private View findNearestChild(MotionEvent event) { |
Matthew Ng | 7d6a5fe | 2018-09-05 17:45:43 -0700 | [diff] [blame] | 95 | if (mClickableChildren.isEmpty()) { |
| 96 | return null; |
| 97 | } |
Rohan Shah | 2adfe95 | 2018-03-05 10:00:10 -0800 | [diff] [blame] | 98 | return mClickableChildren |
| 99 | .stream() |
Matthew Ng | 8181876 | 2019-01-18 11:35:54 -0800 | [diff] [blame] | 100 | .filter(View::isAttachedToWindow) |
Rohan Shah | 2adfe95 | 2018-03-05 10:00:10 -0800 | [diff] [blame] | 101 | .map(v -> new Pair<>(distance(v, event), v)) |
| 102 | .min(Comparator.comparingInt(f -> f.first)) |
Matthew Ng | 8181876 | 2019-01-18 11:35:54 -0800 | [diff] [blame] | 103 | .map(data -> data.second) |
| 104 | .orElse(null); |
Jason Monk | 9262c94 | 2017-07-28 14:35:13 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private int distance(View v, MotionEvent event) { |
| 108 | v.getLocationInWindow(mTmpInt); |
| 109 | int left = mTmpInt[0] - mOffset[0]; |
| 110 | int top = mTmpInt[1] - mOffset[1]; |
| 111 | int right = left + v.getWidth(); |
| 112 | int bottom = top + v.getHeight(); |
| 113 | |
| 114 | int x = Math.min(Math.abs(left - (int) event.getX()), |
| 115 | Math.abs((int) event.getX() - right)); |
| 116 | int y = Math.min(Math.abs(top - (int) event.getY()), |
| 117 | Math.abs((int) event.getY() - bottom)); |
| 118 | |
| 119 | return Math.max(x, y); |
| 120 | } |
| 121 | } |