blob: 8f067faa5cf6894ce972a5f093020ede89abdf6c [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
Winson Chungc100e8e2011-08-09 16:02:43 -070048 private boolean mInTransition;
49 private boolean mResetAfterTransition;
50
Winson Chung785d2eb2011-04-14 16:08:02 -070051 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
52 super(context, attrs);
53 mLayoutInflater = LayoutInflater.from(context);
54 }
55
Winson Chungf0ea4d32011-06-06 14:27:16 -070056 /**
Winson Chung5a808352011-06-27 19:08:49 -070057 * Convenience methods to select specific tabs. We want to set the content type immediately
58 * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view
59 * reflects the new content (but doesn't do the animation and logic associated with changing
60 * tabs manually).
Winson Chungf0ea4d32011-06-06 14:27:16 -070061 */
Winson Chung5a808352011-06-27 19:08:49 -070062 private void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) {
63 onTabChangedStart();
64 onTabChangedEnd(type);
65 }
Winson Chung55b65502011-05-26 12:03:43 -070066 void selectAppsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070067 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Applications);
Winson Chung55b65502011-05-26 12:03:43 -070068 setCurrentTabByTag(APPS_TAB_TAG);
69 }
70 void selectWidgetsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070071 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Widgets);
72 mAppsCustomizePane.setCurrentPageToWidgets();
73
74 setCurrentTabByTag(WIDGETS_TAB_TAG);
75 }
Winson Chung55b65502011-05-26 12:03:43 -070076
Winson Chung785d2eb2011-04-14 16:08:02 -070077 /**
78 * Setup the tab host and create all necessary tabs.
79 */
80 @Override
81 protected void onFinishInflate() {
82 // Setup the tab host
83 setup();
84
Winson Chungfd3385f2011-06-15 19:51:24 -070085 final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container);
Winson Chungfaa13252011-06-13 18:15:54 -070086 final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070087 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070088 findViewById(R.id.apps_customize_pane_content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070089 mTabs = tabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070090 mTabsContainer = tabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070091 mAppsCustomizePane = appsCustomizePane;
92 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -070093
94 // Configure the tabs content factory to return the same paged view (that we change the
95 // content filter on)
96 TabContentFactory contentFactory = new TabContentFactory() {
97 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -070098 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070099 }
100 };
101
102 // Create the tabs
103 TextView tabView;
104 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
105 tabView.setText(mContext.getString(R.string.all_apps_button_label));
106 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700107 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
108 tabView.setText(mContext.getString(R.string.widgets_tab_label));
109 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700110 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -0700111
112 // Setup the key listener to jump between the last tab view and the market icon
113 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
114 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
115 lastTab.setOnKeyListener(keyListener);
116 View shopButton = findViewById(R.id.market_button);
117 shopButton.setOnKeyListener(keyListener);
Winson Chungfd3385f2011-06-15 19:51:24 -0700118
119 // Hide the tab bar until we measure
120 mTabsContainer.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700121 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700122
Winson Chungf0ea4d32011-06-06 14:27:16 -0700123 @Override
124 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
125 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
126 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
127
128 // Set the width of the tab list to the content width
129 if (remeasureTabWidth) {
Winson Chungfd3385f2011-06-15 19:51:24 -0700130 int contentWidth = mAppsCustomizePane.getPageContentWidth();
131 if (contentWidth > 0) {
132 // Set the width and show the tab bar (if we have a loading graphic, we can switch
133 // it off here)
134 mTabs.getLayoutParams().width = contentWidth;
Winson Chungf30ad5f2011-08-08 10:55:42 -0700135 mTabsContainer.setAlpha(1f);
Winson Chungfd3385f2011-06-15 19:51:24 -0700136 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700137 }
Winson Chungfd3385f2011-06-15 19:51:24 -0700138 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Winson Chung4179b4e2011-05-31 17:28:12 -0700139 }
140
141 @Override
142 public boolean onTouchEvent(MotionEvent event) {
143 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
144 // through to the workspace and trigger showWorkspace()
145 if (event.getY() < mAppsCustomizePane.getBottom()) {
146 return true;
147 }
148 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700149 }
150
Winson Chung5a808352011-06-27 19:08:49 -0700151 private void onTabChangedStart() {
152 mAppsCustomizePane.hideScrollingIndicator(false);
153 }
154 private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) {
155 mAppsCustomizePane.setContentType(type);
156 }
157
Winson Chung785d2eb2011-04-14 16:08:02 -0700158 @Override
159 public void onTabChanged(String tabId) {
Winson Chungb44b5242011-06-13 11:32:14 -0700160 final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId);
161 if (!mAppsCustomizePane.isContentType(type)) {
162 // Animate the changing of the tab content by fading pages in and out
163 final Resources res = getResources();
164 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
165
166 ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 0f);
167 anim.setDuration(duration);
168 anim.addListener(new AnimatorListenerAdapter() {
169 @Override
Winson Chung32174c82011-07-19 15:47:55 -0700170 public void onAnimationStart(android.animation.Animator animation) {
Winson Chung5a808352011-06-27 19:08:49 -0700171 onTabChangedStart();
Winson Chung32174c82011-07-19 15:47:55 -0700172 }
173 @Override
Winson Chungb44b5242011-06-13 11:32:14 -0700174 public void onAnimationEnd(android.animation.Animator animation) {
Winson Chung5a808352011-06-27 19:08:49 -0700175 onTabChangedEnd(type);
Winson Chungb44b5242011-06-13 11:32:14 -0700176
177 ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
178 anim.setDuration(duration);
Winson Chung3ac74c52011-06-30 17:39:37 -0700179 anim.addListener(new AnimatorListenerAdapter() {
180 @Override
181 public void onAnimationEnd(android.animation.Animator animation) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700182 if (!LauncherApplication.isScreenLarge()) {
183 mAppsCustomizePane.flashScrollingIndicator();
184 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700185 }
186 });
Winson Chungb44b5242011-06-13 11:32:14 -0700187 anim.start();
188 }
189 });
190 anim.start();
191 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700192 }
193
194 /**
195 * Returns the content type for the specified tab tag.
196 */
197 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
198 if (tag.equals(APPS_TAB_TAG)) {
199 return AppsCustomizePagedView.ContentType.Applications;
200 } else if (tag.equals(WIDGETS_TAB_TAG)) {
201 return AppsCustomizePagedView.ContentType.Widgets;
202 }
203 return AppsCustomizePagedView.ContentType.Applications;
204 }
205
206 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700207 * Returns the tab tag for a given content type.
208 */
209 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
210 if (type == AppsCustomizePagedView.ContentType.Applications) {
211 return APPS_TAB_TAG;
212 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
213 return WIDGETS_TAB_TAG;
214 }
215 return APPS_TAB_TAG;
216 }
217
218 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700219 * Disable focus on anything under this view in the hierarchy if we are not visible.
220 */
221 @Override
222 public int getDescendantFocusability() {
223 if (getVisibility() != View.VISIBLE) {
224 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
225 }
226 return super.getDescendantFocusability();
227 }
228
Winson Chungc100e8e2011-08-09 16:02:43 -0700229 void reset() {
230 if (mInTransition) {
231 // Defer to after the transition to reset
232 mResetAfterTransition = true;
233 } else {
234 // Reset immediately
235 mAppsCustomizePane.reset();
236 }
237 }
238
Winson Chung785d2eb2011-04-14 16:08:02 -0700239 /* LauncherTransitionable overrides */
240 @Override
Winson Chung5a808352011-06-27 19:08:49 -0700241 public void onLauncherTransitionStart(Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700242 mInTransition = true;
Michael Jurka19e0fc52011-07-22 18:00:21 -0700243 // isHardwareAccelerated() checks if we're attached to a window and if that
244 // window is HW accelerated-- we were sometimes not attached to a window
245 // and buildLayer was throwing an IllegalStateException
246 if (animation != null && isHardwareAccelerated()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700247 // Turn on hardware layers for performance
248 setLayerType(LAYER_TYPE_HARDWARE, null);
249
250 // force building the layer at the beginning of the animation, so you don't get a
251 // blip early in the animation
252 buildLayer();
253 }
Michael Jurka430e8a52011-08-08 15:52:14 -0700254 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
255 mAppsCustomizePane.showScrollingIndicator(false);
256 }
Winson Chungc100e8e2011-08-09 16:02:43 -0700257 if (mResetAfterTransition) {
258 mAppsCustomizePane.reset();
259 mResetAfterTransition = false;
260 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700261 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700262
Winson Chung785d2eb2011-04-14 16:08:02 -0700263 @Override
Winson Chung5a808352011-06-27 19:08:49 -0700264 public void onLauncherTransitionEnd(Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700265 mInTransition = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700266 if (animation != null) {
267 setLayerType(LAYER_TYPE_NONE, null);
268 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700269
Winson Chung4afe9b32011-07-27 17:46:20 -0700270 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
Michael Jurka430e8a52011-08-08 15:52:14 -0700271 mAppsCustomizePane.hideScrollingIndicator(false);
Winson Chung5a808352011-06-27 19:08:49 -0700272 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700273 }
274}