blob: 25001b19afa2aa8a8c765d74bc6e917544f76674 [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 Chungb44b5242011-06-13 11:32:14 -070020import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.content.Context;
23import android.content.res.Resources;
24import android.util.AttributeSet;
25import android.view.LayoutInflater;
Winson Chung4179b4e2011-05-31 17:28:12 -070026import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070027import android.view.View;
28import android.view.ViewGroup;
Winson Chungb44b5242011-06-13 11:32:14 -070029import android.view.ViewPropertyAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.widget.TabHost;
Winson Chungfaa13252011-06-13 18:15:54 -070031import android.widget.TabWidget;
Winson Chung785d2eb2011-04-14 16:08:02 -070032import android.widget.TextView;
33
34import com.android.launcher.R;
35
36public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable,
37 TabHost.OnTabChangeListener {
38 static final String LOG_TAG = "AppsCustomizeTabHost";
39
40 private static final String APPS_TAB_TAG = "APPS";
41 private static final String WIDGETS_TAB_TAG = "WIDGETS";
42
43 private final LayoutInflater mLayoutInflater;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044 private ViewGroup mTabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070045 private ViewGroup mTabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070046 private AppsCustomizePagedView mAppsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070047
48 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
49 super(context, attrs);
50 mLayoutInflater = LayoutInflater.from(context);
51 }
52
Winson Chungf0ea4d32011-06-06 14:27:16 -070053 /**
Winson Chung5a808352011-06-27 19:08:49 -070054 * Convenience methods to select specific tabs. We want to set the content type immediately
55 * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view
56 * reflects the new content (but doesn't do the animation and logic associated with changing
57 * tabs manually).
Winson Chungf0ea4d32011-06-06 14:27:16 -070058 */
Winson Chung5a808352011-06-27 19:08:49 -070059 private void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) {
60 onTabChangedStart();
61 onTabChangedEnd(type);
62 }
Winson Chung55b65502011-05-26 12:03:43 -070063 void selectAppsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070064 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Applications);
Winson Chung55b65502011-05-26 12:03:43 -070065 setCurrentTabByTag(APPS_TAB_TAG);
66 }
67 void selectWidgetsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070068 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Widgets);
69 mAppsCustomizePane.setCurrentPageToWidgets();
70
71 setCurrentTabByTag(WIDGETS_TAB_TAG);
72 }
Winson Chung55b65502011-05-26 12:03:43 -070073
Winson Chung785d2eb2011-04-14 16:08:02 -070074 /**
75 * Setup the tab host and create all necessary tabs.
76 */
77 @Override
78 protected void onFinishInflate() {
79 // Setup the tab host
80 setup();
81
Winson Chungfd3385f2011-06-15 19:51:24 -070082 final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container);
Winson Chungfaa13252011-06-13 18:15:54 -070083 final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070084 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070085 findViewById(R.id.apps_customize_pane_content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070086 mTabs = tabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070087 mTabsContainer = tabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070088 mAppsCustomizePane = appsCustomizePane;
89 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -070090
91 // Configure the tabs content factory to return the same paged view (that we change the
92 // content filter on)
93 TabContentFactory contentFactory = new TabContentFactory() {
94 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -070095 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070096 }
97 };
98
99 // Create the tabs
100 TextView tabView;
101 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
102 tabView.setText(mContext.getString(R.string.all_apps_button_label));
103 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700104 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
105 tabView.setText(mContext.getString(R.string.widgets_tab_label));
106 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700107 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -0700108
109 // Setup the key listener to jump between the last tab view and the market icon
110 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
111 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
112 lastTab.setOnKeyListener(keyListener);
113 View shopButton = findViewById(R.id.market_button);
114 shopButton.setOnKeyListener(keyListener);
Winson Chungfd3385f2011-06-15 19:51:24 -0700115
116 // Hide the tab bar until we measure
117 mTabsContainer.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700118 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700119
Winson Chungf0ea4d32011-06-06 14:27:16 -0700120 @Override
121 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
122 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
123 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
124
125 // Set the width of the tab list to the content width
126 if (remeasureTabWidth) {
Winson Chungfd3385f2011-06-15 19:51:24 -0700127 int contentWidth = mAppsCustomizePane.getPageContentWidth();
128 if (contentWidth > 0) {
129 // Set the width and show the tab bar (if we have a loading graphic, we can switch
130 // it off here)
131 mTabs.getLayoutParams().width = contentWidth;
Winson Chungf30ad5f2011-08-08 10:55:42 -0700132 mTabsContainer.setAlpha(1f);
Winson Chungfd3385f2011-06-15 19:51:24 -0700133 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700134 }
Winson Chungfd3385f2011-06-15 19:51:24 -0700135 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Winson Chung4179b4e2011-05-31 17:28:12 -0700136 }
137
138 @Override
139 public boolean onTouchEvent(MotionEvent event) {
140 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
141 // through to the workspace and trigger showWorkspace()
142 if (event.getY() < mAppsCustomizePane.getBottom()) {
143 return true;
144 }
145 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700146 }
147
Winson Chung5a808352011-06-27 19:08:49 -0700148 private void onTabChangedStart() {
149 mAppsCustomizePane.hideScrollingIndicator(false);
150 }
151 private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) {
152 mAppsCustomizePane.setContentType(type);
153 }
154
Winson Chung785d2eb2011-04-14 16:08:02 -0700155 @Override
156 public void onTabChanged(String tabId) {
Winson Chungb44b5242011-06-13 11:32:14 -0700157 final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId);
158 if (!mAppsCustomizePane.isContentType(type)) {
159 // Animate the changing of the tab content by fading pages in and out
160 final Resources res = getResources();
161 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
162
163 ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 0f);
164 anim.setDuration(duration);
165 anim.addListener(new AnimatorListenerAdapter() {
166 @Override
Winson Chung32174c82011-07-19 15:47:55 -0700167 public void onAnimationStart(android.animation.Animator animation) {
Winson Chung5a808352011-06-27 19:08:49 -0700168 onTabChangedStart();
Winson Chung32174c82011-07-19 15:47:55 -0700169 }
170 @Override
Winson Chungb44b5242011-06-13 11:32:14 -0700171 public void onAnimationEnd(android.animation.Animator animation) {
Winson Chung5a808352011-06-27 19:08:49 -0700172 onTabChangedEnd(type);
Winson Chungb44b5242011-06-13 11:32:14 -0700173
174 ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
175 anim.setDuration(duration);
Winson Chung3ac74c52011-06-30 17:39:37 -0700176 anim.addListener(new AnimatorListenerAdapter() {
177 @Override
178 public void onAnimationEnd(android.animation.Animator animation) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700179 if (!LauncherApplication.isScreenLarge()) {
180 mAppsCustomizePane.flashScrollingIndicator();
181 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700182 }
183 });
Winson Chungb44b5242011-06-13 11:32:14 -0700184 anim.start();
185 }
186 });
187 anim.start();
188 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700189 }
190
191 /**
192 * Returns the content type for the specified tab tag.
193 */
194 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
195 if (tag.equals(APPS_TAB_TAG)) {
196 return AppsCustomizePagedView.ContentType.Applications;
197 } else if (tag.equals(WIDGETS_TAB_TAG)) {
198 return AppsCustomizePagedView.ContentType.Widgets;
199 }
200 return AppsCustomizePagedView.ContentType.Applications;
201 }
202
203 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700204 * Returns the tab tag for a given content type.
205 */
206 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
207 if (type == AppsCustomizePagedView.ContentType.Applications) {
208 return APPS_TAB_TAG;
209 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
210 return WIDGETS_TAB_TAG;
211 }
212 return APPS_TAB_TAG;
213 }
214
215 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700216 * Disable focus on anything under this view in the hierarchy if we are not visible.
217 */
218 @Override
219 public int getDescendantFocusability() {
220 if (getVisibility() != View.VISIBLE) {
221 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
222 }
223 return super.getDescendantFocusability();
224 }
225
226 /* LauncherTransitionable overrides */
227 @Override
Winson Chung5a808352011-06-27 19:08:49 -0700228 public void onLauncherTransitionStart(Animator animation, boolean toWorkspace) {
Michael Jurka19e0fc52011-07-22 18:00:21 -0700229 // isHardwareAccelerated() checks if we're attached to a window and if that
230 // window is HW accelerated-- we were sometimes not attached to a window
231 // and buildLayer was throwing an IllegalStateException
232 if (animation != null && isHardwareAccelerated()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700233 // Turn on hardware layers for performance
234 setLayerType(LAYER_TYPE_HARDWARE, null);
235
236 // force building the layer at the beginning of the animation, so you don't get a
237 // blip early in the animation
238 buildLayer();
239 }
Michael Jurka430e8a52011-08-08 15:52:14 -0700240 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
241 mAppsCustomizePane.showScrollingIndicator(false);
242 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700243 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700244
Winson Chung785d2eb2011-04-14 16:08:02 -0700245 @Override
Winson Chung5a808352011-06-27 19:08:49 -0700246 public void onLauncherTransitionEnd(Animator animation, boolean toWorkspace) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700247 if (animation != null) {
248 setLayerType(LAYER_TYPE_NONE, null);
249 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700250
Winson Chung4afe9b32011-07-27 17:46:20 -0700251 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
Michael Jurka430e8a52011-08-08 15:52:14 -0700252 mAppsCustomizePane.hideScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -0700253 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700254 }
255}