blob: e40524d3c324f0f04d81883ce59a5be41fca33de [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
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
17package com.android.launcher2;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.util.AttributeSet;
22import android.view.LayoutInflater;
Winson Chung4179b4e2011-05-31 17:28:12 -070023import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070024import android.view.View;
25import android.view.ViewGroup;
26import android.widget.TabHost;
Winson Chung785d2eb2011-04-14 16:08:02 -070027import android.widget.TextView;
28
29import com.android.launcher.R;
30
31public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable,
32 TabHost.OnTabChangeListener {
33 static final String LOG_TAG = "AppsCustomizeTabHost";
34
35 private static final String APPS_TAB_TAG = "APPS";
36 private static final String WIDGETS_TAB_TAG = "WIDGETS";
37
38 private final LayoutInflater mLayoutInflater;
Winson Chung4179b4e2011-05-31 17:28:12 -070039 private AppsCustomizePagedView mAppsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070040
41 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
42 super(context, attrs);
43 mLayoutInflater = LayoutInflater.from(context);
44 }
45
Winson Chung55b65502011-05-26 12:03:43 -070046 void selectAppsTab() {
47 setCurrentTabByTag(APPS_TAB_TAG);
48 }
49 void selectWidgetsTab() {
50 setCurrentTabByTag(WIDGETS_TAB_TAG);
51 }
52
Winson Chung785d2eb2011-04-14 16:08:02 -070053 /**
54 * Setup the tab host and create all necessary tabs.
55 */
56 @Override
57 protected void onFinishInflate() {
58 // Setup the tab host
59 setup();
60
61 final ViewGroup tabs = (ViewGroup) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070062 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070063 findViewById(R.id.apps_customize_pane_content);
Winson Chung4179b4e2011-05-31 17:28:12 -070064 mAppsCustomizePane = appsCustomizePane;
65 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -070066
67 // Configure the tabs content factory to return the same paged view (that we change the
68 // content filter on)
69 TabContentFactory contentFactory = new TabContentFactory() {
70 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -070071 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070072 }
73 };
74
75 // Create the tabs
76 TextView tabView;
77 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
78 tabView.setText(mContext.getString(R.string.all_apps_button_label));
79 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -070080 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
81 tabView.setText(mContext.getString(R.string.widgets_tab_label));
82 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -070083 setOnTabChangedListener(this);
84
85 // Set the width of the tab bar to match the content (for now)
Winson Chung4179b4e2011-05-31 17:28:12 -070086 tabs.getLayoutParams().width = mAppsCustomizePane.getPageContentWidth();
87 }
88
89 @Override
90 public boolean onTouchEvent(MotionEvent event) {
91 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
92 // through to the workspace and trigger showWorkspace()
93 if (event.getY() < mAppsCustomizePane.getBottom()) {
94 return true;
95 }
96 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -070097 }
98
99 @Override
100 public void onTabChanged(String tabId) {
Winson Chung4179b4e2011-05-31 17:28:12 -0700101 mAppsCustomizePane.setContentType(getContentTypeForTabTag(tabId));
Winson Chung785d2eb2011-04-14 16:08:02 -0700102 }
103
104 /**
105 * Returns the content type for the specified tab tag.
106 */
107 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
108 if (tag.equals(APPS_TAB_TAG)) {
109 return AppsCustomizePagedView.ContentType.Applications;
110 } else if (tag.equals(WIDGETS_TAB_TAG)) {
111 return AppsCustomizePagedView.ContentType.Widgets;
112 }
113 return AppsCustomizePagedView.ContentType.Applications;
114 }
115
116 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700117 * Returns the tab tag for a given content type.
118 */
119 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
120 if (type == AppsCustomizePagedView.ContentType.Applications) {
121 return APPS_TAB_TAG;
122 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
123 return WIDGETS_TAB_TAG;
124 }
125 return APPS_TAB_TAG;
126 }
127
128 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700129 * Disable focus on anything under this view in the hierarchy if we are not visible.
130 */
131 @Override
132 public int getDescendantFocusability() {
133 if (getVisibility() != View.VISIBLE) {
134 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
135 }
136 return super.getDescendantFocusability();
137 }
138
139 /* LauncherTransitionable overrides */
140 @Override
141 public void onLauncherTransitionStart(android.animation.Animator animation) {
142 // TODO-APPS_CUSTOMIZE: see AllAppsTabbed.onLauncherTransitionStart();
143 }
144 @Override
145 public void onLauncherTransitionEnd(android.animation.Animator animation) {
146 // TODO-APPS_CUSTOMIZE: see AllAppsTabbed.onLauncherTransitionEnd();
147 }
148}