blob: d02280836fb5b510d45d07171c56cd40372f9d2a [file] [log] [blame]
Jason Monk9262c942017-07-28 14:35:13 -04001/*
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
15package com.android.systemui.statusbar.phone;
16
17import android.content.Context;
Jason Monke57e9dc2017-08-04 10:59:13 -040018import android.content.res.Configuration;
Jason Monk9262c942017-07-28 14:35:13 -040019import android.util.AttributeSet;
Jason Monk9262c942017-07-28 14:35:13 -040020import android.util.Pair;
21import android.view.MotionEvent;
22import android.view.View;
23import android.view.ViewGroup;
24import android.widget.FrameLayout;
25
Gus Prevasab336792018-11-14 13:52:20 -050026import androidx.annotation.VisibleForTesting;
Jason Monk9262c942017-07-28 14:35:13 -040027
28import java.util.ArrayList;
29import 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 */
35public 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 Monke57e9dc2017-08-04 10:59:13 -040044 this(context, attrs, context.getResources().getConfiguration());
45 }
46
47 @VisibleForTesting
48 NearestTouchFrame(Context context, AttributeSet attrs, Configuration c) {
Jason Monk9262c942017-07-28 14:35:13 -040049 super(context, attrs);
Jason Monke57e9dc2017-08-04 10:59:13 -040050 mIsActive = c.smallestScreenWidthDp < 600;
Jason Monk9262c942017-07-28 14:35:13 -040051 }
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 Monk3dd17a72017-09-06 14:10:39 -040087 return mTouchingChild.getVisibility() == VISIBLE
88 && mTouchingChild.dispatchTouchEvent(event);
Jason Monk9262c942017-07-28 14:35:13 -040089 }
90 }
91 return super.onTouchEvent(event);
92 }
93
94 private View findNearestChild(MotionEvent event) {
Matthew Ng7d6a5fe2018-09-05 17:45:43 -070095 if (mClickableChildren.isEmpty()) {
96 return null;
97 }
Rohan Shah2adfe952018-03-05 10:00:10 -080098 return mClickableChildren
99 .stream()
Matthew Ng81818762019-01-18 11:35:54 -0800100 .filter(View::isAttachedToWindow)
Rohan Shah2adfe952018-03-05 10:00:10 -0800101 .map(v -> new Pair<>(distance(v, event), v))
102 .min(Comparator.comparingInt(f -> f.first))
Matthew Ng81818762019-01-18 11:35:54 -0800103 .map(data -> data.second)
104 .orElse(null);
Jason Monk9262c942017-07-28 14:35:13 -0400105 }
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}