1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.widget;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.content.Context;
21 import android.graphics.Rect;
22 import android.os.Build;
23 import android.util.AttributeSet;
24 import android.view.MotionEvent;
25 import android.view.View;
26 import android.widget.LinearLayout;
27 
28 
29 /**
30  * Like a normal linear layout, but supports dispatching all otherwise unhandled
31  * touch events to a particular descendant.  This is for the unlock screen, so
32  * that a wider range of touch events than just the lock pattern widget can kick
33  * off a lock pattern if the finger is eventually dragged into the bounds of the
34  * lock pattern view.
35  */
36 public class LinearLayoutWithDefaultTouchRecepient extends LinearLayout {
37 
38     private final Rect mTempRect = new Rect();
39     private View mDefaultTouchRecepient;
40 
41     @UnsupportedAppUsage
LinearLayoutWithDefaultTouchRecepient(Context context)42     public LinearLayoutWithDefaultTouchRecepient(Context context) {
43         super(context);
44     }
45 
LinearLayoutWithDefaultTouchRecepient(Context context, AttributeSet attrs)46     public LinearLayoutWithDefaultTouchRecepient(Context context, AttributeSet attrs) {
47         super(context, attrs);
48     }
49 
50     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
setDefaultTouchRecepient(View defaultTouchRecepient)51     public void setDefaultTouchRecepient(View defaultTouchRecepient) {
52         mDefaultTouchRecepient = defaultTouchRecepient;
53     }
54 
55     @Override
dispatchTouchEvent(MotionEvent ev)56     public boolean dispatchTouchEvent(MotionEvent ev) {
57         if (mDefaultTouchRecepient == null) {
58             return super.dispatchTouchEvent(ev);
59         }
60 
61         if (super.dispatchTouchEvent(ev)) {
62             return true;
63         }
64         mTempRect.set(0, 0, 0, 0);
65         offsetRectIntoDescendantCoords(mDefaultTouchRecepient, mTempRect);
66         ev.setLocation(ev.getX() + mTempRect.left, ev.getY() + mTempRect.top);
67         return mDefaultTouchRecepient.dispatchTouchEvent(ev);
68     }
69 
70 }
71