blob: e79d41f5ff34dc67f5934d49f342d740d9d73ffc [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;
Winson Chungf314b0e2011-08-16 11:54:27 -070021import android.animation.AnimatorSet;
Winson Chungb44b5242011-06-13 11:32:14 -070022import android.animation.ObjectAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070023import android.content.Context;
24import android.content.res.Resources;
25import android.util.AttributeSet;
26import android.view.LayoutInflater;
Winson Chung4179b4e2011-05-31 17:28:12 -070027import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070028import android.view.View;
29import android.view.ViewGroup;
Michael Jurka141dbd02011-11-03 13:50:45 -070030import android.widget.FrameLayout;
Michael Jurka1899a362011-11-03 13:50:45 -070031import android.widget.LinearLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070032import android.widget.TabHost;
Winson Chungfaa13252011-06-13 18:15:54 -070033import android.widget.TabWidget;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.widget.TextView;
35
36import com.android.launcher.R;
37
Michael Jurka141dbd02011-11-03 13:50:45 -070038import java.util.ArrayList;
39
Winson Chung785d2eb2011-04-14 16:08:02 -070040public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable,
41 TabHost.OnTabChangeListener {
42 static final String LOG_TAG = "AppsCustomizeTabHost";
43
44 private static final String APPS_TAB_TAG = "APPS";
45 private static final String WIDGETS_TAB_TAG = "WIDGETS";
46
47 private final LayoutInflater mLayoutInflater;
Winson Chungf0ea4d32011-06-06 14:27:16 -070048 private ViewGroup mTabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070049 private ViewGroup mTabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070050 private AppsCustomizePagedView mAppsCustomizePane;
Adam Cohen0cd3b642011-10-14 14:58:00 -070051 private boolean mSuppressContentCallback = false;
Michael Jurka141dbd02011-11-03 13:50:45 -070052 private FrameLayout mAnimationBuffer;
Michael Jurka1899a362011-11-03 13:50:45 -070053 private LinearLayout mContent;
Winson Chung785d2eb2011-04-14 16:08:02 -070054
Winson Chungc100e8e2011-08-09 16:02:43 -070055 private boolean mInTransition;
56 private boolean mResetAfterTransition;
Michael Jurka1899a362011-11-03 13:50:45 -070057 private Animator mLauncherTransition;
Winson Chungc100e8e2011-08-09 16:02:43 -070058
Winson Chung785d2eb2011-04-14 16:08:02 -070059 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
60 super(context, attrs);
61 mLayoutInflater = LayoutInflater.from(context);
62 }
63
Winson Chungf0ea4d32011-06-06 14:27:16 -070064 /**
Winson Chung5a808352011-06-27 19:08:49 -070065 * Convenience methods to select specific tabs. We want to set the content type immediately
66 * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view
67 * reflects the new content (but doesn't do the animation and logic associated with changing
68 * tabs manually).
Winson Chungf0ea4d32011-06-06 14:27:16 -070069 */
Winson Chung5a808352011-06-27 19:08:49 -070070 private void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) {
71 onTabChangedStart();
72 onTabChangedEnd(type);
73 }
Winson Chung55b65502011-05-26 12:03:43 -070074 void selectAppsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070075 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Applications);
Winson Chung55b65502011-05-26 12:03:43 -070076 setCurrentTabByTag(APPS_TAB_TAG);
77 }
78 void selectWidgetsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070079 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Widgets);
Winson Chung5a808352011-06-27 19:08:49 -070080 setCurrentTabByTag(WIDGETS_TAB_TAG);
81 }
Winson Chung55b65502011-05-26 12:03:43 -070082
Winson Chung785d2eb2011-04-14 16:08:02 -070083 /**
84 * Setup the tab host and create all necessary tabs.
85 */
86 @Override
87 protected void onFinishInflate() {
88 // Setup the tab host
89 setup();
90
Winson Chungfd3385f2011-06-15 19:51:24 -070091 final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container);
Winson Chungfaa13252011-06-13 18:15:54 -070092 final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070093 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070094 findViewById(R.id.apps_customize_pane_content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070095 mTabs = tabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070096 mTabsContainer = tabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070097 mAppsCustomizePane = appsCustomizePane;
Michael Jurka141dbd02011-11-03 13:50:45 -070098 mAnimationBuffer = (FrameLayout) findViewById(R.id.animation_buffer);
Michael Jurka1899a362011-11-03 13:50:45 -070099 mContent = (LinearLayout) findViewById(R.id.apps_customize_content);
Winson Chung4179b4e2011-05-31 17:28:12 -0700100 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -0700101
102 // Configure the tabs content factory to return the same paged view (that we change the
103 // content filter on)
104 TabContentFactory contentFactory = new TabContentFactory() {
105 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -0700106 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -0700107 }
108 };
109
110 // Create the tabs
111 TextView tabView;
Winson Chungd2c1f802011-10-14 12:26:54 -0700112 String label;
113 label = mContext.getString(R.string.all_apps_button_label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700114 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
Winson Chungd2c1f802011-10-14 12:26:54 -0700115 tabView.setText(label);
116 tabView.setContentDescription(label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700117 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chungd2c1f802011-10-14 12:26:54 -0700118 label = mContext.getString(R.string.widgets_tab_label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700119 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
Winson Chungd2c1f802011-10-14 12:26:54 -0700120 tabView.setText(label);
121 tabView.setContentDescription(label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700122 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700123 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -0700124
125 // Setup the key listener to jump between the last tab view and the market icon
126 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
127 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
128 lastTab.setOnKeyListener(keyListener);
129 View shopButton = findViewById(R.id.market_button);
130 shopButton.setOnKeyListener(keyListener);
Winson Chungfd3385f2011-06-15 19:51:24 -0700131
132 // Hide the tab bar until we measure
133 mTabsContainer.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700134 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700135
Winson Chungf0ea4d32011-06-06 14:27:16 -0700136 @Override
137 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
138 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
139 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
140
141 // Set the width of the tab list to the content width
142 if (remeasureTabWidth) {
Winson Chungfd3385f2011-06-15 19:51:24 -0700143 int contentWidth = mAppsCustomizePane.getPageContentWidth();
Michael Jurka141dbd02011-11-03 13:50:45 -0700144 if (contentWidth > 0 && mTabs.getLayoutParams().width != contentWidth) {
145 // Set the width and show the tab bar
Winson Chungfd3385f2011-06-15 19:51:24 -0700146 mTabs.getLayoutParams().width = contentWidth;
Michael Jurkac57b7a82011-08-09 22:02:20 -0700147 post(new Runnable() {
148 public void run() {
149 mTabs.requestLayout();
Winson Chung7ed37742011-09-08 15:45:51 -0700150 mTabsContainer.setAlpha(1f);
Michael Jurkac57b7a82011-08-09 22:02:20 -0700151 }
152 });
Winson Chungfd3385f2011-06-15 19:51:24 -0700153 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700154 }
Winson Chungfd3385f2011-06-15 19:51:24 -0700155 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Winson Chung4179b4e2011-05-31 17:28:12 -0700156 }
157
158 @Override
159 public boolean onTouchEvent(MotionEvent event) {
160 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
161 // through to the workspace and trigger showWorkspace()
162 if (event.getY() < mAppsCustomizePane.getBottom()) {
163 return true;
164 }
165 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700166 }
167
Winson Chung5a808352011-06-27 19:08:49 -0700168 private void onTabChangedStart() {
169 mAppsCustomizePane.hideScrollingIndicator(false);
170 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700171
Winson Chung097e6c72011-10-20 13:58:30 -0700172 private void reloadCurrentPage() {
173 if (!LauncherApplication.isScreenLarge()) {
174 mAppsCustomizePane.flashScrollingIndicator();
175 }
176 mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage());
177 mAppsCustomizePane.requestFocus();
178 }
179
Winson Chung5a808352011-06-27 19:08:49 -0700180 private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) {
181 mAppsCustomizePane.setContentType(type);
182 }
183
Winson Chung785d2eb2011-04-14 16:08:02 -0700184 @Override
185 public void onTabChanged(String tabId) {
Winson Chungb44b5242011-06-13 11:32:14 -0700186 final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700187 if (mSuppressContentCallback) {
188 mSuppressContentCallback = false;
189 return;
Winson Chungb44b5242011-06-13 11:32:14 -0700190 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700191
192 // Animate the changing of the tab content by fading pages in and out
193 final Resources res = getResources();
194 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
195
196 // We post a runnable here because there is a delay while the first page is loading and
197 // the feedback from having changed the tab almost feels better than having it stick
198 post(new Runnable() {
199 @Override
200 public void run() {
Winson Chung097e6c72011-10-20 13:58:30 -0700201 if (mAppsCustomizePane.getMeasuredWidth() <= 0 ||
202 mAppsCustomizePane.getMeasuredHeight() <= 0) {
203 reloadCurrentPage();
204 return;
205 }
206
Michael Jurka141dbd02011-11-03 13:50:45 -0700207 // Take the visible pages and re-parent them temporarily to mAnimatorBuffer
208 // and then cross fade to the new pages
209
210 // We want the pages to be rendered in exactly the same way as they were when
211 // their parent was mAppsCustomizePane -- so set the scroll on mAnimationBuffer
212 // to be exactly the same as mAppsCustomizePane, and below, set the left/top
213 // parameters to be correct for each of the pages
214 mAnimationBuffer.scrollTo(mAppsCustomizePane.getScrollX(), 0);
215
216 int[] visiblePageRange = new int[2];
217 mAppsCustomizePane.getVisiblePages(visiblePageRange);
218 ArrayList<View> visiblePages = new ArrayList<View>();
219 for (int i = visiblePageRange[0]; i <= visiblePageRange[1]; i++) {
220 visiblePages.add(mAppsCustomizePane.getPageAt(i));
221 }
222 // mAppsCustomizePane renders its children in reverse order, so
223 // add the pages to mAnimationBuffer in reverse order to match that behavior
224 for (int i = visiblePages.size() - 1; i >= 0; i--) {
225 View child = visiblePages.get(i);
226 PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(false);
227 mAppsCustomizePane.removeView(child);
228 PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(true);
229 mAnimationBuffer.setAlpha(1f);
230 mAnimationBuffer.setVisibility(View.VISIBLE);
231 LayoutParams p = new FrameLayout.LayoutParams(child.getWidth(),
232 child.getHeight());
233 p.setMargins((int) child.getLeft(), (int) child.getTop(), 0, 0);
234 mAnimationBuffer.addView(child, p);
235 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700236
237 // Toggle the new content
238 onTabChangedStart();
239 onTabChangedEnd(type);
240
241 // Animate the transition
242 ObjectAnimator outAnim = ObjectAnimator.ofFloat(mAnimationBuffer, "alpha", 0f);
243 outAnim.addListener(new AnimatorListenerAdapter() {
244 @Override
245 public void onAnimationEnd(Animator animation) {
246 mAnimationBuffer.setVisibility(View.GONE);
Michael Jurka141dbd02011-11-03 13:50:45 -0700247 mAnimationBuffer.removeAllViews();
248 }
249 @Override
250 public void onAnimationCancel(Animator animation) {
251 mAnimationBuffer.setVisibility(View.GONE);
252 mAnimationBuffer.removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700253 }
254 });
255 ObjectAnimator inAnim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
256 inAnim.addListener(new AnimatorListenerAdapter() {
257 @Override
258 public void onAnimationEnd(Animator animation) {
Winson Chung097e6c72011-10-20 13:58:30 -0700259 reloadCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700260 }
261 });
262 AnimatorSet animSet = new AnimatorSet();
263 animSet.playTogether(outAnim, inAnim);
264 animSet.setDuration(duration);
265 animSet.start();
266 }
267 });
268 }
269
270 public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) {
271 mSuppressContentCallback = true;
272 setCurrentTabByTag(getTabTagForContentType(type));
Winson Chung785d2eb2011-04-14 16:08:02 -0700273 }
274
275 /**
276 * Returns the content type for the specified tab tag.
277 */
278 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
279 if (tag.equals(APPS_TAB_TAG)) {
280 return AppsCustomizePagedView.ContentType.Applications;
281 } else if (tag.equals(WIDGETS_TAB_TAG)) {
282 return AppsCustomizePagedView.ContentType.Widgets;
283 }
284 return AppsCustomizePagedView.ContentType.Applications;
285 }
286
287 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700288 * Returns the tab tag for a given content type.
289 */
290 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
291 if (type == AppsCustomizePagedView.ContentType.Applications) {
292 return APPS_TAB_TAG;
293 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
294 return WIDGETS_TAB_TAG;
295 }
296 return APPS_TAB_TAG;
297 }
298
299 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700300 * Disable focus on anything under this view in the hierarchy if we are not visible.
301 */
302 @Override
303 public int getDescendantFocusability() {
304 if (getVisibility() != View.VISIBLE) {
305 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
306 }
307 return super.getDescendantFocusability();
308 }
309
Winson Chungc100e8e2011-08-09 16:02:43 -0700310 void reset() {
311 if (mInTransition) {
312 // Defer to after the transition to reset
313 mResetAfterTransition = true;
314 } else {
315 // Reset immediately
316 mAppsCustomizePane.reset();
317 }
318 }
319
Michael Jurka1899a362011-11-03 13:50:45 -0700320 private void enableAndBuildHardwareLayer() {
Michael Jurka19e0fc52011-07-22 18:00:21 -0700321 // isHardwareAccelerated() checks if we're attached to a window and if that
322 // window is HW accelerated-- we were sometimes not attached to a window
323 // and buildLayer was throwing an IllegalStateException
Michael Jurka1899a362011-11-03 13:50:45 -0700324 if (isHardwareAccelerated()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700325 // Turn on hardware layers for performance
326 setLayerType(LAYER_TYPE_HARDWARE, null);
327
Michael Jurka1899a362011-11-03 13:50:45 -0700328 // force building the layer, so you don't get a blip early in an animation
329 // when the layer is created layer
Winson Chungf0ea4d32011-06-06 14:27:16 -0700330 buildLayer();
331 }
Michael Jurka1899a362011-11-03 13:50:45 -0700332 }
333
334 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
335 super.onLayout(changed, left, top, right, bottom);
336 if (mLauncherTransition != null) {
337 enableAndBuildHardwareLayer();
338 mLauncherTransition.start();
339 mLauncherTransition = null;
340 }
341 }
342
343 /* LauncherTransitionable overrides */
344 @Override
345 public boolean onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace) {
346 mInTransition = true;
347 boolean delayLauncherTransitionUntilLayout = false;
348 mLauncherTransition = null;
349
350 // if the content wasn't visible before, delay the launcher animation until after a cal
351 // to layout -- this prevents a blip
352 if (animation != null) {
353 if (mContent.getVisibility() == GONE) {
354 mLauncherTransition = animation;
355 delayLauncherTransitionUntilLayout = true;
356 }
357 mContent.setVisibility(VISIBLE);
358 if (!delayLauncherTransitionUntilLayout) {
359 enableAndBuildHardwareLayer();
360 }
361 }
362
Michael Jurka430e8a52011-08-08 15:52:14 -0700363 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
364 mAppsCustomizePane.showScrollingIndicator(false);
365 }
Winson Chungc100e8e2011-08-09 16:02:43 -0700366 if (mResetAfterTransition) {
367 mAppsCustomizePane.reset();
368 mResetAfterTransition = false;
369 }
Michael Jurka1899a362011-11-03 13:50:45 -0700370 return delayLauncherTransitionUntilLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -0700371 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700372
Winson Chung785d2eb2011-04-14 16:08:02 -0700373 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700374 public void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700375 mInTransition = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700376 if (animation != null) {
377 setLayerType(LAYER_TYPE_NONE, null);
378 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700379
Winson Chung7d7541e2011-09-16 20:14:36 -0700380 if (!toWorkspace) {
381 // Dismiss the cling if necessary
382 l.dismissWorkspaceCling(null);
383
384 if (!LauncherApplication.isScreenLarge()) {
385 mAppsCustomizePane.hideScrollingIndicator(false);
386 }
Winson Chung5a808352011-06-27 19:08:49 -0700387 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700388 }
Winson Chung70d72102011-08-12 11:24:35 -0700389
390 boolean isTransitioning() {
391 return mInTransition;
392 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700393}