blob: b5c6327a47b4545be0534170db1319b4beed7b0d [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
Winson Chungf0ea4d32011-06-06 14:27:16 -070019import android.animation.Animator;
Winson Chung785d2eb2011-04-14 16:08:02 -070020import android.content.Context;
21import android.content.res.Resources;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
Winson Chung4179b4e2011-05-31 17:28:12 -070024import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070025import android.view.View;
26import android.view.ViewGroup;
27import android.widget.TabHost;
Winson Chung785d2eb2011-04-14 16:08:02 -070028import android.widget.TextView;
29
30import com.android.launcher.R;
31
32public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable,
33 TabHost.OnTabChangeListener {
34 static final String LOG_TAG = "AppsCustomizeTabHost";
35
36 private static final String APPS_TAB_TAG = "APPS";
37 private static final String WIDGETS_TAB_TAG = "WIDGETS";
38
39 private final LayoutInflater mLayoutInflater;
Winson Chungf0ea4d32011-06-06 14:27:16 -070040 private ViewGroup mTabs;
Winson Chung4179b4e2011-05-31 17:28:12 -070041 private AppsCustomizePagedView mAppsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070042
43 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
44 super(context, attrs);
45 mLayoutInflater = LayoutInflater.from(context);
46 }
47
Winson Chungf0ea4d32011-06-06 14:27:16 -070048 /**
49 * Convenience methods to select specific tabs
50 */
Winson Chung55b65502011-05-26 12:03:43 -070051 void selectAppsTab() {
52 setCurrentTabByTag(APPS_TAB_TAG);
53 }
54 void selectWidgetsTab() {
55 setCurrentTabByTag(WIDGETS_TAB_TAG);
56 }
57
Winson Chung785d2eb2011-04-14 16:08:02 -070058 /**
59 * Setup the tab host and create all necessary tabs.
60 */
61 @Override
62 protected void onFinishInflate() {
63 // Setup the tab host
64 setup();
65
66 final ViewGroup tabs = (ViewGroup) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070067 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070068 findViewById(R.id.apps_customize_pane_content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070069 mTabs = tabs;
Winson Chung4179b4e2011-05-31 17:28:12 -070070 mAppsCustomizePane = appsCustomizePane;
71 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -070072
73 // Configure the tabs content factory to return the same paged view (that we change the
74 // content filter on)
75 TabContentFactory contentFactory = new TabContentFactory() {
76 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -070077 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070078 }
79 };
80
81 // Create the tabs
82 TextView tabView;
83 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
84 tabView.setText(mContext.getString(R.string.all_apps_button_label));
85 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -070086 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
87 tabView.setText(mContext.getString(R.string.widgets_tab_label));
88 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -070089 setOnTabChangedListener(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -070090 }
Winson Chung785d2eb2011-04-14 16:08:02 -070091
Winson Chungf0ea4d32011-06-06 14:27:16 -070092 @Override
93 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
94 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
95 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
96
97 // Set the width of the tab list to the content width
98 if (remeasureTabWidth) {
99 mTabs.getLayoutParams().width = mAppsCustomizePane.getPageContentWidth();
100 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
101 }
Winson Chung4179b4e2011-05-31 17:28:12 -0700102 }
103
104 @Override
105 public boolean onTouchEvent(MotionEvent event) {
106 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
107 // through to the workspace and trigger showWorkspace()
108 if (event.getY() < mAppsCustomizePane.getBottom()) {
109 return true;
110 }
111 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700112 }
113
114 @Override
115 public void onTabChanged(String tabId) {
Winson Chung4179b4e2011-05-31 17:28:12 -0700116 mAppsCustomizePane.setContentType(getContentTypeForTabTag(tabId));
Winson Chung785d2eb2011-04-14 16:08:02 -0700117 }
118
119 /**
120 * Returns the content type for the specified tab tag.
121 */
122 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
123 if (tag.equals(APPS_TAB_TAG)) {
124 return AppsCustomizePagedView.ContentType.Applications;
125 } else if (tag.equals(WIDGETS_TAB_TAG)) {
126 return AppsCustomizePagedView.ContentType.Widgets;
127 }
128 return AppsCustomizePagedView.ContentType.Applications;
129 }
130
131 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700132 * Returns the tab tag for a given content type.
133 */
134 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
135 if (type == AppsCustomizePagedView.ContentType.Applications) {
136 return APPS_TAB_TAG;
137 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
138 return WIDGETS_TAB_TAG;
139 }
140 return APPS_TAB_TAG;
141 }
142
143 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700144 * Disable focus on anything under this view in the hierarchy if we are not visible.
145 */
146 @Override
147 public int getDescendantFocusability() {
148 if (getVisibility() != View.VISIBLE) {
149 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
150 }
151 return super.getDescendantFocusability();
152 }
153
154 /* LauncherTransitionable overrides */
155 @Override
Winson Chungf0ea4d32011-06-06 14:27:16 -0700156 public void onLauncherTransitionStart(Animator animation) {
157 if (animation != null) {
158 // Turn on hardware layers for performance
159 setLayerType(LAYER_TYPE_HARDWARE, null);
160
161 // force building the layer at the beginning of the animation, so you don't get a
162 // blip early in the animation
163 buildLayer();
164 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700165 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700166
Winson Chung785d2eb2011-04-14 16:08:02 -0700167 @Override
Winson Chungf0ea4d32011-06-06 14:27:16 -0700168 public void onLauncherTransitionEnd(Animator animation) {
169 if (animation != null) {
170 setLayerType(LAYER_TYPE_NONE, null);
171 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700172 }
173}