blob: 09833d44915d7aad530998079a283a7480d46587 [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.graphics.Rect;
Aurimas Liutikasfd52c142018-04-17 09:50:46 -070020import androidx.annotation.VisibleForTesting;
Jason Monk9262c942017-07-28 14:35:13 -040021import android.util.AttributeSet;
22import android.util.Log;
23import android.util.Pair;
24import android.view.MotionEvent;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.FrameLayout;
28
29import com.android.systemui.R;
30
31import java.util.ArrayList;
32import java.util.Comparator;
33
34/**
35 * Redirects touches that aren't handled by any child view to the nearest
36 * clickable child. Only takes effect on <sw600dp.
37 */
38public class NearestTouchFrame extends FrameLayout {
39
40 private final ArrayList<View> mClickableChildren = new ArrayList<>();
41 private final boolean mIsActive;
42 private final int[] mTmpInt = new int[2];
43 private final int[] mOffset = new int[2];
44 private View mTouchingChild;
45
46 public NearestTouchFrame(Context context, AttributeSet attrs) {
Jason Monke57e9dc2017-08-04 10:59:13 -040047 this(context, attrs, context.getResources().getConfiguration());
48 }
49
50 @VisibleForTesting
51 NearestTouchFrame(Context context, AttributeSet attrs, Configuration c) {
Jason Monk9262c942017-07-28 14:35:13 -040052 super(context, attrs);
Jason Monke57e9dc2017-08-04 10:59:13 -040053 mIsActive = c.smallestScreenWidthDp < 600;
Jason Monk9262c942017-07-28 14:35:13 -040054 }
55
56 @Override
57 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
58 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
59 mClickableChildren.clear();
60 addClickableChildren(this);
61 }
62
63 @Override
64 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
65 super.onLayout(changed, left, top, right, bottom);
66 getLocationInWindow(mOffset);
67 }
68
69 private void addClickableChildren(ViewGroup group) {
70 final int N = group.getChildCount();
71 for (int i = 0; i < N; i++) {
72 View child = group.getChildAt(i);
73 if (child.isClickable()) {
74 mClickableChildren.add(child);
75 } else if (child instanceof ViewGroup) {
76 addClickableChildren((ViewGroup) child);
77 }
78 }
79 }
80
81 @Override
82 public boolean onTouchEvent(MotionEvent event) {
83 if (mIsActive) {
84 if (event.getAction() == MotionEvent.ACTION_DOWN) {
85 mTouchingChild = findNearestChild(event);
86 }
87 if (mTouchingChild != null) {
88 event.offsetLocation(mTouchingChild.getWidth() / 2 - event.getX(),
89 mTouchingChild.getHeight() / 2 - event.getY());
Jason Monk3dd17a72017-09-06 14:10:39 -040090 return mTouchingChild.getVisibility() == VISIBLE
91 && mTouchingChild.dispatchTouchEvent(event);
Jason Monk9262c942017-07-28 14:35:13 -040092 }
93 }
94 return super.onTouchEvent(event);
95 }
96
97 private View findNearestChild(MotionEvent event) {
Rohan Shah2adfe952018-03-05 10:00:10 -080098 return mClickableChildren
99 .stream()
100 .filter(v -> v.isAttachedToWindow())
101 .map(v -> new Pair<>(distance(v, event), v))
102 .min(Comparator.comparingInt(f -> f.first))
103 .get().second;
Jason Monk9262c942017-07-28 14:35:13 -0400104 }
105
106 private int distance(View v, MotionEvent event) {
107 v.getLocationInWindow(mTmpInt);
108 int left = mTmpInt[0] - mOffset[0];
109 int top = mTmpInt[1] - mOffset[1];
110 int right = left + v.getWidth();
111 int bottom = top + v.getHeight();
112
113 int x = Math.min(Math.abs(left - (int) event.getX()),
114 Math.abs((int) event.getX() - right));
115 int y = Math.min(Math.abs(top - (int) event.getY()),
116 Math.abs((int) event.getY() - bottom));
117
118 return Math.max(x, y);
119 }
120}