blob: 1452e0c7a0b8de35c6048e7ff12fef825f9092e8 [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;
Jason Monke57e9dc2017-08-04 10:59:13 -040020import android.support.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) {
98 return mClickableChildren.stream().map(v -> new Pair<>(distance(v, event), v))
99 .min(Comparator.comparingInt(f -> f.first)).get().second;
100 }
101
102 private int distance(View v, MotionEvent event) {
103 v.getLocationInWindow(mTmpInt);
104 int left = mTmpInt[0] - mOffset[0];
105 int top = mTmpInt[1] - mOffset[1];
106 int right = left + v.getWidth();
107 int bottom = top + v.getHeight();
108
109 int x = Math.min(Math.abs(left - (int) event.getX()),
110 Math.abs((int) event.getX() - right));
111 int y = Math.min(Math.abs(top - (int) event.getY()),
112 Math.abs((int) event.getY() - bottom));
113
114 return Math.max(x, y);
115 }
116}