1 /*
2  * Copyright (C) 2019 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.KeyEvent;
22 import android.widget.FrameLayout;
23 
24 import androidx.annotation.NonNull;
25 
26 /**
27  * This class provides the functionality to handle the keyEvent correctly for two-panel settings
28  * layout.
29  */
30 public class TwoPanelSettingsRootView extends FrameLayout {
31     private OnKeyListener mOnBackKeyListener;
32 
TwoPanelSettingsRootView(Context context)33     public TwoPanelSettingsRootView(Context context) {
34         super(context);
35     }
36 
TwoPanelSettingsRootView(Context context, AttributeSet attrs)37     public TwoPanelSettingsRootView(Context context, AttributeSet attrs) {
38         super(context, attrs);
39     }
40 
TwoPanelSettingsRootView(Context context, AttributeSet attrs, int defStyleAttr)41     public TwoPanelSettingsRootView(Context context, AttributeSet attrs, int defStyleAttr) {
42         super(context, attrs, defStyleAttr);
43     }
44 
setOnBackKeyListener(OnKeyListener backKeyListener)45     public void setOnBackKeyListener(OnKeyListener backKeyListener) {
46         mOnBackKeyListener = backKeyListener;
47     }
48 
49     @Override
dispatchKeyEvent(@onNull KeyEvent event)50     public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
51         boolean handled = false;
52         if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK
53                 || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
54                 || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN
55                 || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
56                 || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT)
57                 && mOnBackKeyListener != null) {
58             handled = mOnBackKeyListener.onKey(this, event.getKeyCode(), event);
59         }
60         return handled || super.dispatchKeyEvent(event);
61     }
62 }
63