blob: 897a7fb5178c8f6114cb4b009cc221733e92c833 [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()) {
Michael Jurkab737ee62011-11-15 15:57:22 -0800174 mAppsCustomizePane.flashScrollingIndicator(true);
Winson Chung097e6c72011-10-20 13:58:30 -0700175 }
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
Winson Chungc6f10b92011-11-14 11:39:07 -0800209 int[] visiblePageRange = new int[2];
210 mAppsCustomizePane.getVisiblePages(visiblePageRange);
211 if (visiblePageRange[0] == -1 && visiblePageRange[1] == -1) {
212 // If we can't get the visible page ranges, then just skip the animation
213 reloadCurrentPage();
214 return;
215 }
216 ArrayList<View> visiblePages = new ArrayList<View>();
217 for (int i = visiblePageRange[0]; i <= visiblePageRange[1]; i++) {
218 visiblePages.add(mAppsCustomizePane.getPageAt(i));
219 }
Michael Jurka141dbd02011-11-03 13:50:45 -0700220
221 // We want the pages to be rendered in exactly the same way as they were when
222 // their parent was mAppsCustomizePane -- so set the scroll on mAnimationBuffer
223 // to be exactly the same as mAppsCustomizePane, and below, set the left/top
224 // parameters to be correct for each of the pages
225 mAnimationBuffer.scrollTo(mAppsCustomizePane.getScrollX(), 0);
226
Michael Jurka141dbd02011-11-03 13:50:45 -0700227 // mAppsCustomizePane renders its children in reverse order, so
228 // add the pages to mAnimationBuffer in reverse order to match that behavior
229 for (int i = visiblePages.size() - 1; i >= 0; i--) {
230 View child = visiblePages.get(i);
Winson Chungc6f10b92011-11-14 11:39:07 -0800231 if (child instanceof PagedViewCellLayout) {
232 ((PagedViewCellLayout) child).resetChildrenOnKeyListeners();
233 } else if (child instanceof PagedViewGridLayout) {
234 ((PagedViewGridLayout) child).resetChildrenOnKeyListeners();
235 }
Michael Jurka141dbd02011-11-03 13:50:45 -0700236 PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(false);
237 mAppsCustomizePane.removeView(child);
238 PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(true);
239 mAnimationBuffer.setAlpha(1f);
240 mAnimationBuffer.setVisibility(View.VISIBLE);
241 LayoutParams p = new FrameLayout.LayoutParams(child.getWidth(),
242 child.getHeight());
243 p.setMargins((int) child.getLeft(), (int) child.getTop(), 0, 0);
244 mAnimationBuffer.addView(child, p);
245 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700246
247 // Toggle the new content
248 onTabChangedStart();
249 onTabChangedEnd(type);
250
251 // Animate the transition
252 ObjectAnimator outAnim = ObjectAnimator.ofFloat(mAnimationBuffer, "alpha", 0f);
253 outAnim.addListener(new AnimatorListenerAdapter() {
254 @Override
255 public void onAnimationEnd(Animator animation) {
256 mAnimationBuffer.setVisibility(View.GONE);
Michael Jurka141dbd02011-11-03 13:50:45 -0700257 mAnimationBuffer.removeAllViews();
258 }
259 @Override
260 public void onAnimationCancel(Animator animation) {
261 mAnimationBuffer.setVisibility(View.GONE);
262 mAnimationBuffer.removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700263 }
264 });
265 ObjectAnimator inAnim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
266 inAnim.addListener(new AnimatorListenerAdapter() {
267 @Override
268 public void onAnimationEnd(Animator animation) {
Winson Chung097e6c72011-10-20 13:58:30 -0700269 reloadCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700270 }
271 });
272 AnimatorSet animSet = new AnimatorSet();
273 animSet.playTogether(outAnim, inAnim);
274 animSet.setDuration(duration);
275 animSet.start();
276 }
277 });
278 }
279
280 public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) {
281 mSuppressContentCallback = true;
282 setCurrentTabByTag(getTabTagForContentType(type));
Winson Chung785d2eb2011-04-14 16:08:02 -0700283 }
284
285 /**
286 * Returns the content type for the specified tab tag.
287 */
288 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
289 if (tag.equals(APPS_TAB_TAG)) {
290 return AppsCustomizePagedView.ContentType.Applications;
291 } else if (tag.equals(WIDGETS_TAB_TAG)) {
292 return AppsCustomizePagedView.ContentType.Widgets;
293 }
294 return AppsCustomizePagedView.ContentType.Applications;
295 }
296
297 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700298 * Returns the tab tag for a given content type.
299 */
300 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
301 if (type == AppsCustomizePagedView.ContentType.Applications) {
302 return APPS_TAB_TAG;
303 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
304 return WIDGETS_TAB_TAG;
305 }
306 return APPS_TAB_TAG;
307 }
308
309 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700310 * Disable focus on anything under this view in the hierarchy if we are not visible.
311 */
312 @Override
313 public int getDescendantFocusability() {
314 if (getVisibility() != View.VISIBLE) {
315 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
316 }
317 return super.getDescendantFocusability();
318 }
319
Winson Chungc100e8e2011-08-09 16:02:43 -0700320 void reset() {
321 if (mInTransition) {
322 // Defer to after the transition to reset
323 mResetAfterTransition = true;
324 } else {
325 // Reset immediately
326 mAppsCustomizePane.reset();
327 }
328 }
329
Michael Jurka1899a362011-11-03 13:50:45 -0700330 private void enableAndBuildHardwareLayer() {
Michael Jurka19e0fc52011-07-22 18:00:21 -0700331 // isHardwareAccelerated() checks if we're attached to a window and if that
332 // window is HW accelerated-- we were sometimes not attached to a window
333 // and buildLayer was throwing an IllegalStateException
Michael Jurka1899a362011-11-03 13:50:45 -0700334 if (isHardwareAccelerated()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700335 // Turn on hardware layers for performance
336 setLayerType(LAYER_TYPE_HARDWARE, null);
337
Michael Jurka1899a362011-11-03 13:50:45 -0700338 // force building the layer, so you don't get a blip early in an animation
339 // when the layer is created layer
Winson Chungf0ea4d32011-06-06 14:27:16 -0700340 buildLayer();
341 }
Michael Jurka1899a362011-11-03 13:50:45 -0700342 }
343
344 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
345 super.onLayout(changed, left, top, right, bottom);
346 if (mLauncherTransition != null) {
347 enableAndBuildHardwareLayer();
348 mLauncherTransition.start();
349 mLauncherTransition = null;
350 }
351 }
352
353 /* LauncherTransitionable overrides */
354 @Override
355 public boolean onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace) {
356 mInTransition = true;
357 boolean delayLauncherTransitionUntilLayout = false;
Michael Jurka3d845e82011-11-15 17:04:31 -0800358 boolean animated = (animation != null);
Michael Jurka1899a362011-11-03 13:50:45 -0700359 mLauncherTransition = null;
360
Michael Jurka3d845e82011-11-15 17:04:31 -0800361 // if the content wasn't visible before, delay the launcher animation until after a call
Michael Jurka1899a362011-11-03 13:50:45 -0700362 // to layout -- this prevents a blip
Michael Jurka3d845e82011-11-15 17:04:31 -0800363 if (animated && mContent.getVisibility() == GONE) {
364 mLauncherTransition = animation;
365 delayLauncherTransitionUntilLayout = true;
366 }
367 mContent.setVisibility(VISIBLE);
368
369 if (animated && !delayLauncherTransitionUntilLayout) {
370 enableAndBuildHardwareLayer();
Michael Jurka1899a362011-11-03 13:50:45 -0700371 }
372
Michael Jurka430e8a52011-08-08 15:52:14 -0700373 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
374 mAppsCustomizePane.showScrollingIndicator(false);
375 }
Winson Chungc100e8e2011-08-09 16:02:43 -0700376 if (mResetAfterTransition) {
377 mAppsCustomizePane.reset();
378 mResetAfterTransition = false;
379 }
Michael Jurka1899a362011-11-03 13:50:45 -0700380 return delayLauncherTransitionUntilLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -0700381 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700382
Winson Chung785d2eb2011-04-14 16:08:02 -0700383 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700384 public void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700385 mInTransition = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700386 if (animation != null) {
387 setLayerType(LAYER_TYPE_NONE, null);
388 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700389
Winson Chung7d7541e2011-09-16 20:14:36 -0700390 if (!toWorkspace) {
391 // Dismiss the cling if necessary
392 l.dismissWorkspaceCling(null);
393
394 if (!LauncherApplication.isScreenLarge()) {
395 mAppsCustomizePane.hideScrollingIndicator(false);
396 }
Winson Chung5a808352011-06-27 19:08:49 -0700397 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700398 }
Winson Chung70d72102011-08-12 11:24:35 -0700399
400 boolean isTransitioning() {
401 return mInTransition;
402 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700403}