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