1 /*
2  * Copyright (C) 2011 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.launcher3;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.View;
22 import android.widget.TabWidget;
23 
24 public class FocusOnlyTabWidget extends TabWidget {
FocusOnlyTabWidget(Context context)25     public FocusOnlyTabWidget(Context context) {
26         super(context);
27     }
28 
FocusOnlyTabWidget(Context context, AttributeSet attrs)29     public FocusOnlyTabWidget(Context context, AttributeSet attrs) {
30         super(context, attrs);
31     }
32 
FocusOnlyTabWidget(Context context, AttributeSet attrs, int defStyle)33     public FocusOnlyTabWidget(Context context, AttributeSet attrs, int defStyle) {
34         super(context, attrs, defStyle);
35     }
36 
getSelectedTab()37     public View getSelectedTab() {
38         final int count = getTabCount();
39         for (int i = 0; i < count; ++i) {
40             View v = getChildTabViewAt(i);
41             if (v.isSelected()) {
42                 return v;
43             }
44         }
45         return null;
46     }
47 
getChildTabIndex(View v)48     public int getChildTabIndex(View v) {
49         final int tabCount = getTabCount();
50         for (int i = 0; i < tabCount; ++i) {
51             if (getChildTabViewAt(i) == v) {
52                 return i;
53             }
54         }
55         return -1;
56     }
57 
setCurrentTabToFocusedTab()58     public void setCurrentTabToFocusedTab() {
59         View tab = null;
60         int index = -1;
61         final int count = getTabCount();
62         for (int i = 0; i < count; ++i) {
63             View v = getChildTabViewAt(i);
64             if (v.hasFocus()) {
65                 tab = v;
66                 index = i;
67                 break;
68             }
69         }
70         if (index > -1) {
71             super.setCurrentTab(index);
72             super.onFocusChange(tab, true);
73         }
74     }
superOnFocusChange(View v, boolean hasFocus)75     public void superOnFocusChange(View v, boolean hasFocus) {
76         super.onFocusChange(v, hasFocus);
77     }
78 
79     @Override
onFocusChange(android.view.View v, boolean hasFocus)80     public void onFocusChange(android.view.View v, boolean hasFocus) {
81         if (v == this && hasFocus && getTabCount() > 0) {
82             getSelectedTab().requestFocus();
83             return;
84         }
85     }
86 }
87