blob: 41f8d7e1aeec304fbf9a87e75b43e45046066d94 [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;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.widget.TabHost;
Winson Chungfaa13252011-06-13 18:15:54 -070032import android.widget.TabWidget;
Winson Chung785d2eb2011-04-14 16:08:02 -070033import android.widget.TextView;
34
35import com.android.launcher.R;
36
Michael Jurka141dbd02011-11-03 13:50:45 -070037import java.util.ArrayList;
38
Winson Chung785d2eb2011-04-14 16:08:02 -070039public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable,
40 TabHost.OnTabChangeListener {
41 static final String LOG_TAG = "AppsCustomizeTabHost";
42
43 private static final String APPS_TAB_TAG = "APPS";
44 private static final String WIDGETS_TAB_TAG = "WIDGETS";
45
46 private final LayoutInflater mLayoutInflater;
Winson Chungf0ea4d32011-06-06 14:27:16 -070047 private ViewGroup mTabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070048 private ViewGroup mTabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070049 private AppsCustomizePagedView mAppsCustomizePane;
Adam Cohen0cd3b642011-10-14 14:58:00 -070050 private boolean mSuppressContentCallback = false;
Michael Jurka141dbd02011-11-03 13:50:45 -070051 private FrameLayout mAnimationBuffer;
Winson Chung785d2eb2011-04-14 16:08:02 -070052
Winson Chungc100e8e2011-08-09 16:02:43 -070053 private boolean mInTransition;
54 private boolean mResetAfterTransition;
55
Winson Chung785d2eb2011-04-14 16:08:02 -070056 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
57 super(context, attrs);
58 mLayoutInflater = LayoutInflater.from(context);
59 }
60
Winson Chungf0ea4d32011-06-06 14:27:16 -070061 /**
Winson Chung5a808352011-06-27 19:08:49 -070062 * Convenience methods to select specific tabs. We want to set the content type immediately
63 * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view
64 * reflects the new content (but doesn't do the animation and logic associated with changing
65 * tabs manually).
Winson Chungf0ea4d32011-06-06 14:27:16 -070066 */
Winson Chung5a808352011-06-27 19:08:49 -070067 private void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) {
68 onTabChangedStart();
69 onTabChangedEnd(type);
70 }
Winson Chung55b65502011-05-26 12:03:43 -070071 void selectAppsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070072 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Applications);
Winson Chung55b65502011-05-26 12:03:43 -070073 setCurrentTabByTag(APPS_TAB_TAG);
74 }
75 void selectWidgetsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070076 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Widgets);
Winson Chung5a808352011-06-27 19:08:49 -070077 setCurrentTabByTag(WIDGETS_TAB_TAG);
78 }
Winson Chung55b65502011-05-26 12:03:43 -070079
Winson Chung785d2eb2011-04-14 16:08:02 -070080 /**
81 * Setup the tab host and create all necessary tabs.
82 */
83 @Override
84 protected void onFinishInflate() {
85 // Setup the tab host
86 setup();
87
Winson Chungfd3385f2011-06-15 19:51:24 -070088 final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container);
Winson Chungfaa13252011-06-13 18:15:54 -070089 final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070090 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070091 findViewById(R.id.apps_customize_pane_content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070092 mTabs = tabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070093 mTabsContainer = tabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070094 mAppsCustomizePane = appsCustomizePane;
Michael Jurka141dbd02011-11-03 13:50:45 -070095 mAnimationBuffer = (FrameLayout) findViewById(R.id.animation_buffer);
Winson Chung4179b4e2011-05-31 17:28:12 -070096 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -070097
98 // Configure the tabs content factory to return the same paged view (that we change the
99 // content filter on)
100 TabContentFactory contentFactory = new TabContentFactory() {
101 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -0700102 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -0700103 }
104 };
105
106 // Create the tabs
107 TextView tabView;
Winson Chungd2c1f802011-10-14 12:26:54 -0700108 String label;
109 label = mContext.getString(R.string.all_apps_button_label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700110 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
Winson Chungd2c1f802011-10-14 12:26:54 -0700111 tabView.setText(label);
112 tabView.setContentDescription(label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700113 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chungd2c1f802011-10-14 12:26:54 -0700114 label = mContext.getString(R.string.widgets_tab_label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700115 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
Winson Chungd2c1f802011-10-14 12:26:54 -0700116 tabView.setText(label);
117 tabView.setContentDescription(label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700118 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700119 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -0700120
121 // Setup the key listener to jump between the last tab view and the market icon
122 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
123 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
124 lastTab.setOnKeyListener(keyListener);
125 View shopButton = findViewById(R.id.market_button);
126 shopButton.setOnKeyListener(keyListener);
Winson Chungfd3385f2011-06-15 19:51:24 -0700127
128 // Hide the tab bar until we measure
129 mTabsContainer.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700130 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700131
Winson Chungf0ea4d32011-06-06 14:27:16 -0700132 @Override
133 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
134 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
135 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
136
137 // Set the width of the tab list to the content width
138 if (remeasureTabWidth) {
Winson Chungfd3385f2011-06-15 19:51:24 -0700139 int contentWidth = mAppsCustomizePane.getPageContentWidth();
Michael Jurka141dbd02011-11-03 13:50:45 -0700140 if (contentWidth > 0 && mTabs.getLayoutParams().width != contentWidth) {
141 // Set the width and show the tab bar
Winson Chungfd3385f2011-06-15 19:51:24 -0700142 mTabs.getLayoutParams().width = contentWidth;
Michael Jurkac57b7a82011-08-09 22:02:20 -0700143 post(new Runnable() {
144 public void run() {
145 mTabs.requestLayout();
Winson Chung7ed37742011-09-08 15:45:51 -0700146 mTabsContainer.setAlpha(1f);
Michael Jurkac57b7a82011-08-09 22:02:20 -0700147 }
148 });
Winson Chungfd3385f2011-06-15 19:51:24 -0700149 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700150 }
Winson Chungfd3385f2011-06-15 19:51:24 -0700151 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Winson Chung4179b4e2011-05-31 17:28:12 -0700152 }
153
154 @Override
155 public boolean onTouchEvent(MotionEvent event) {
156 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
157 // through to the workspace and trigger showWorkspace()
158 if (event.getY() < mAppsCustomizePane.getBottom()) {
159 return true;
160 }
161 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700162 }
163
Winson Chung5a808352011-06-27 19:08:49 -0700164 private void onTabChangedStart() {
165 mAppsCustomizePane.hideScrollingIndicator(false);
166 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700167
Winson Chung097e6c72011-10-20 13:58:30 -0700168 private void reloadCurrentPage() {
169 if (!LauncherApplication.isScreenLarge()) {
170 mAppsCustomizePane.flashScrollingIndicator();
171 }
172 mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage());
173 mAppsCustomizePane.requestFocus();
174 }
175
Winson Chung5a808352011-06-27 19:08:49 -0700176 private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) {
177 mAppsCustomizePane.setContentType(type);
178 }
179
Winson Chung785d2eb2011-04-14 16:08:02 -0700180 @Override
181 public void onTabChanged(String tabId) {
Winson Chungb44b5242011-06-13 11:32:14 -0700182 final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700183 if (mSuppressContentCallback) {
184 mSuppressContentCallback = false;
185 return;
Winson Chungb44b5242011-06-13 11:32:14 -0700186 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700187
188 // Animate the changing of the tab content by fading pages in and out
189 final Resources res = getResources();
190 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
191
192 // We post a runnable here because there is a delay while the first page is loading and
193 // the feedback from having changed the tab almost feels better than having it stick
194 post(new Runnable() {
195 @Override
196 public void run() {
Winson Chung097e6c72011-10-20 13:58:30 -0700197 if (mAppsCustomizePane.getMeasuredWidth() <= 0 ||
198 mAppsCustomizePane.getMeasuredHeight() <= 0) {
199 reloadCurrentPage();
200 return;
201 }
202
Michael Jurka141dbd02011-11-03 13:50:45 -0700203 // Take the visible pages and re-parent them temporarily to mAnimatorBuffer
204 // and then cross fade to the new pages
205
206 // We want the pages to be rendered in exactly the same way as they were when
207 // their parent was mAppsCustomizePane -- so set the scroll on mAnimationBuffer
208 // to be exactly the same as mAppsCustomizePane, and below, set the left/top
209 // parameters to be correct for each of the pages
210 mAnimationBuffer.scrollTo(mAppsCustomizePane.getScrollX(), 0);
211
212 int[] visiblePageRange = new int[2];
213 mAppsCustomizePane.getVisiblePages(visiblePageRange);
214 ArrayList<View> visiblePages = new ArrayList<View>();
215 for (int i = visiblePageRange[0]; i <= visiblePageRange[1]; i++) {
216 visiblePages.add(mAppsCustomizePane.getPageAt(i));
217 }
218 // mAppsCustomizePane renders its children in reverse order, so
219 // add the pages to mAnimationBuffer in reverse order to match that behavior
220 for (int i = visiblePages.size() - 1; i >= 0; i--) {
221 View child = visiblePages.get(i);
222 PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(false);
223 mAppsCustomizePane.removeView(child);
224 PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(true);
225 mAnimationBuffer.setAlpha(1f);
226 mAnimationBuffer.setVisibility(View.VISIBLE);
227 LayoutParams p = new FrameLayout.LayoutParams(child.getWidth(),
228 child.getHeight());
229 p.setMargins((int) child.getLeft(), (int) child.getTop(), 0, 0);
230 mAnimationBuffer.addView(child, p);
231 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700232
233 // Toggle the new content
234 onTabChangedStart();
235 onTabChangedEnd(type);
236
237 // Animate the transition
238 ObjectAnimator outAnim = ObjectAnimator.ofFloat(mAnimationBuffer, "alpha", 0f);
239 outAnim.addListener(new AnimatorListenerAdapter() {
240 @Override
241 public void onAnimationEnd(Animator animation) {
242 mAnimationBuffer.setVisibility(View.GONE);
Michael Jurka141dbd02011-11-03 13:50:45 -0700243 mAnimationBuffer.removeAllViews();
244 }
245 @Override
246 public void onAnimationCancel(Animator animation) {
247 mAnimationBuffer.setVisibility(View.GONE);
248 mAnimationBuffer.removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700249 }
250 });
251 ObjectAnimator inAnim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
252 inAnim.addListener(new AnimatorListenerAdapter() {
253 @Override
254 public void onAnimationEnd(Animator animation) {
Winson Chung097e6c72011-10-20 13:58:30 -0700255 reloadCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700256 }
257 });
258 AnimatorSet animSet = new AnimatorSet();
259 animSet.playTogether(outAnim, inAnim);
260 animSet.setDuration(duration);
261 animSet.start();
262 }
263 });
264 }
265
266 public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) {
267 mSuppressContentCallback = true;
268 setCurrentTabByTag(getTabTagForContentType(type));
Winson Chung785d2eb2011-04-14 16:08:02 -0700269 }
270
271 /**
272 * Returns the content type for the specified tab tag.
273 */
274 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
275 if (tag.equals(APPS_TAB_TAG)) {
276 return AppsCustomizePagedView.ContentType.Applications;
277 } else if (tag.equals(WIDGETS_TAB_TAG)) {
278 return AppsCustomizePagedView.ContentType.Widgets;
279 }
280 return AppsCustomizePagedView.ContentType.Applications;
281 }
282
283 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700284 * Returns the tab tag for a given content type.
285 */
286 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
287 if (type == AppsCustomizePagedView.ContentType.Applications) {
288 return APPS_TAB_TAG;
289 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
290 return WIDGETS_TAB_TAG;
291 }
292 return APPS_TAB_TAG;
293 }
294
295 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700296 * Disable focus on anything under this view in the hierarchy if we are not visible.
297 */
298 @Override
299 public int getDescendantFocusability() {
300 if (getVisibility() != View.VISIBLE) {
301 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
302 }
303 return super.getDescendantFocusability();
304 }
305
Winson Chungc100e8e2011-08-09 16:02:43 -0700306 void reset() {
307 if (mInTransition) {
308 // Defer to after the transition to reset
309 mResetAfterTransition = true;
310 } else {
311 // Reset immediately
312 mAppsCustomizePane.reset();
313 }
314 }
315
Winson Chung785d2eb2011-04-14 16:08:02 -0700316 /* LauncherTransitionable overrides */
317 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700318 public void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700319 mInTransition = true;
Michael Jurka19e0fc52011-07-22 18:00:21 -0700320 // isHardwareAccelerated() checks if we're attached to a window and if that
321 // window is HW accelerated-- we were sometimes not attached to a window
322 // and buildLayer was throwing an IllegalStateException
323 if (animation != null && isHardwareAccelerated()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700324 // Turn on hardware layers for performance
325 setLayerType(LAYER_TYPE_HARDWARE, null);
326
327 // force building the layer at the beginning of the animation, so you don't get a
328 // blip early in the animation
329 buildLayer();
330 }
Michael Jurka430e8a52011-08-08 15:52:14 -0700331 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
332 mAppsCustomizePane.showScrollingIndicator(false);
333 }
Winson Chungc100e8e2011-08-09 16:02:43 -0700334 if (mResetAfterTransition) {
335 mAppsCustomizePane.reset();
336 mResetAfterTransition = false;
337 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700338 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700339
Winson Chung785d2eb2011-04-14 16:08:02 -0700340 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700341 public void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700342 mInTransition = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700343 if (animation != null) {
344 setLayerType(LAYER_TYPE_NONE, null);
345 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700346
Winson Chung7d7541e2011-09-16 20:14:36 -0700347 if (!toWorkspace) {
348 // Dismiss the cling if necessary
349 l.dismissWorkspaceCling(null);
350
351 if (!LauncherApplication.isScreenLarge()) {
352 mAppsCustomizePane.hideScrollingIndicator(false);
353 }
Winson Chung5a808352011-06-27 19:08:49 -0700354 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700355 }
Winson Chung70d72102011-08-12 11:24:35 -0700356
357 boolean isTransitioning() {
358 return mInTransition;
359 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700360}