1 /* 2 * Copyright (C) 2021 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.tv.twopanelsettings; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.MotionEvent; 22 import android.view.View; 23 import android.widget.FrameLayout; 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 27 /** 28 * Provides a FrameLayout for {@link TwoPanelSettingsFragment} with ability to intercept touch event 29 * before sent to a corresponding child view. 30 */ 31 public class TwoPanelSettingsFrameLayout extends FrameLayout { 32 /** 33 * Interface definition for a callback to be invoked when a touch event is going to be dispatched 34 * to this view. The callback will be invoked before the touch event is given to the view. 35 */ 36 public interface OnDispatchTouchListener { 37 /** 38 * Called when a touch event is going to be dispatched to a view. This allows listeners to get a 39 * chance to respond before the target view. 40 * 41 * @param v The view the touch event is going to be dispatched to. 42 * @param event The MotionEvent object containing full information about the event. 43 * @return True if the listener has consumed the event, false otherwise. 44 */ onDispatchTouch(View v, MotionEvent event)45 boolean onDispatchTouch(View v, MotionEvent event); 46 } 47 48 private OnDispatchTouchListener mOnDispatchTouchListener; 49 TwoPanelSettingsFrameLayout(@onNull Context context)50 public TwoPanelSettingsFrameLayout(@NonNull Context context) { 51 super(context); 52 } 53 TwoPanelSettingsFrameLayout(@onNull Context context, @Nullable AttributeSet attrs)54 public TwoPanelSettingsFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) { 55 super(context, attrs); 56 } 57 TwoPanelSettingsFrameLayout( @onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)58 public TwoPanelSettingsFrameLayout( 59 @NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 60 super(context, attrs, defStyleAttr); 61 } 62 TwoPanelSettingsFrameLayout( @onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)63 public TwoPanelSettingsFrameLayout( 64 @NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 65 super(context, attrs, defStyleAttr, defStyleRes); 66 } 67 setOnDispatchTouchListener(@ullable OnDispatchTouchListener listener)68 public void setOnDispatchTouchListener(@Nullable OnDispatchTouchListener listener) { 69 mOnDispatchTouchListener = listener; 70 } 71 72 @Override dispatchTouchEvent(MotionEvent ev)73 public boolean dispatchTouchEvent(MotionEvent ev) { 74 boolean handled = false; 75 if (mOnDispatchTouchListener != null) { 76 handled = mOnDispatchTouchListener.onDispatchTouch(this, ev); 77 } 78 return handled || super.dispatchTouchEvent(ev); 79 } 80 } 81