1 /*
2  * Copyright (C) 2016 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 package com.android.documentsui.sidebar;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.KeyEvent;
21 import android.widget.ListView;
22 
23 import com.android.documentsui.base.Features;
24 
25 /**
26  * The list in the navigation drawer. This class exists for the purpose of overriding the key
27  * handler on ListView. Ignoring keystrokes (e.g. the tab key) cannot be properly done using
28  * View.OnKeyListener.
29  */
30 public class RootsList extends ListView {
31 
32     private final Features mFeatures;
33 
34     // Multiple constructors are needed to handle all the different ways this View could be
35     // constructed by the framework. Don't remove them!
RootsList(Context context)36     public RootsList(Context context) {
37         super(context);
38         mFeatures = Features.create(getContext());
39     }
40 
RootsList(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)41     public RootsList(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
42         super(context, attrs, defStyleAttr, defStyleRes);
43         mFeatures = Features.create(getContext());
44     }
45 
RootsList(Context context, AttributeSet attrs, int defStyleAttr)46     public RootsList(Context context, AttributeSet attrs, int defStyleAttr) {
47         super(context, attrs, defStyleAttr);
48         mFeatures = Features.create(getContext());
49     }
50 
RootsList(Context context, AttributeSet attrs)51     public RootsList(Context context, AttributeSet attrs) {
52         super(context, attrs);
53         mFeatures = Features.create(getContext());
54     }
55 
56     @Override
onKeyDown(int keyCode, KeyEvent event)57     public boolean onKeyDown(int keyCode, KeyEvent event) {
58         switch (keyCode) {
59             // Ignore tab key events - this causes them to bubble up to the global key handler where
60             // they are appropriately handled. See BaseActivity.onKeyDown.
61             case KeyEvent.KEYCODE_TAB:
62                 return mFeatures.isSystemKeyboardNavigationEnabled()
63                         && super.onKeyDown(keyCode, event);
64             // Prevent left/right arrow keystrokes from shifting focus away from the roots list.
65             case KeyEvent.KEYCODE_DPAD_LEFT:
66             case KeyEvent.KEYCODE_DPAD_RIGHT:
67                 return true;
68             default:
69                 return super.onKeyDown(keyCode, event);
70         }
71     }
72 }
73