blob: 7772f447c5d083339c78b590f7dde406c773fe62 [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
Winson Chungc6f10b92011-11-14 11:39:07 -0800205 int[] visiblePageRange = new int[2];
206 mAppsCustomizePane.getVisiblePages(visiblePageRange);
207 if (visiblePageRange[0] == -1 && visiblePageRange[1] == -1) {
208 // If we can't get the visible page ranges, then just skip the animation
209 reloadCurrentPage();
210 return;
211 }
212 ArrayList<View> visiblePages = new ArrayList<View>();
213 for (int i = visiblePageRange[0]; i <= visiblePageRange[1]; i++) {
214 visiblePages.add(mAppsCustomizePane.getPageAt(i));
215 }
Michael Jurka141dbd02011-11-03 13:50:45 -0700216
217 // We want the pages to be rendered in exactly the same way as they were when
218 // their parent was mAppsCustomizePane -- so set the scroll on mAnimationBuffer
219 // to be exactly the same as mAppsCustomizePane, and below, set the left/top
220 // parameters to be correct for each of the pages
221 mAnimationBuffer.scrollTo(mAppsCustomizePane.getScrollX(), 0);
222
Michael Jurka141dbd02011-11-03 13:50:45 -0700223 // mAppsCustomizePane renders its children in reverse order, so
224 // add the pages to mAnimationBuffer in reverse order to match that behavior
225 for (int i = visiblePages.size() - 1; i >= 0; i--) {
226 View child = visiblePages.get(i);
Winson Chungc6f10b92011-11-14 11:39:07 -0800227 if (child instanceof PagedViewCellLayout) {
228 ((PagedViewCellLayout) child).resetChildrenOnKeyListeners();
229 } else if (child instanceof PagedViewGridLayout) {
230 ((PagedViewGridLayout) child).resetChildrenOnKeyListeners();
231 }
Michael Jurka141dbd02011-11-03 13:50:45 -0700232 PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(false);
233 mAppsCustomizePane.removeView(child);
234 PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(true);
235 mAnimationBuffer.setAlpha(1f);
236 mAnimationBuffer.setVisibility(View.VISIBLE);
237 LayoutParams p = new FrameLayout.LayoutParams(child.getWidth(),
238 child.getHeight());
239 p.setMargins((int) child.getLeft(), (int) child.getTop(), 0, 0);
240 mAnimationBuffer.addView(child, p);
241 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700242
243 // Toggle the new content
244 onTabChangedStart();
245 onTabChangedEnd(type);
246
247 // Animate the transition
248 ObjectAnimator outAnim = ObjectAnimator.ofFloat(mAnimationBuffer, "alpha", 0f);
249 outAnim.addListener(new AnimatorListenerAdapter() {
250 @Override
251 public void onAnimationEnd(Animator animation) {
252 mAnimationBuffer.setVisibility(View.GONE);
Michael Jurka141dbd02011-11-03 13:50:45 -0700253 mAnimationBuffer.removeAllViews();
254 }
255 @Override
256 public void onAnimationCancel(Animator animation) {
257 mAnimationBuffer.setVisibility(View.GONE);
258 mAnimationBuffer.removeAllViews();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700259 }
260 });
261 ObjectAnimator inAnim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
262 inAnim.addListener(new AnimatorListenerAdapter() {
263 @Override
264 public void onAnimationEnd(Animator animation) {
Winson Chung097e6c72011-10-20 13:58:30 -0700265 reloadCurrentPage();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700266 }
267 });
268 AnimatorSet animSet = new AnimatorSet();
269 animSet.playTogether(outAnim, inAnim);
270 animSet.setDuration(duration);
271 animSet.start();
272 }
273 });
274 }
275
276 public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) {
277 mSuppressContentCallback = true;
278 setCurrentTabByTag(getTabTagForContentType(type));
Winson Chung785d2eb2011-04-14 16:08:02 -0700279 }
280
281 /**
282 * Returns the content type for the specified tab tag.
283 */
284 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
285 if (tag.equals(APPS_TAB_TAG)) {
286 return AppsCustomizePagedView.ContentType.Applications;
287 } else if (tag.equals(WIDGETS_TAB_TAG)) {
288 return AppsCustomizePagedView.ContentType.Widgets;
289 }
290 return AppsCustomizePagedView.ContentType.Applications;
291 }
292
293 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700294 * Returns the tab tag for a given content type.
295 */
296 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
297 if (type == AppsCustomizePagedView.ContentType.Applications) {
298 return APPS_TAB_TAG;
299 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
300 return WIDGETS_TAB_TAG;
301 }
302 return APPS_TAB_TAG;
303 }
304
305 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700306 * Disable focus on anything under this view in the hierarchy if we are not visible.
307 */
308 @Override
309 public int getDescendantFocusability() {
310 if (getVisibility() != View.VISIBLE) {
311 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
312 }
313 return super.getDescendantFocusability();
314 }
315
Winson Chungc100e8e2011-08-09 16:02:43 -0700316 void reset() {
317 if (mInTransition) {
318 // Defer to after the transition to reset
319 mResetAfterTransition = true;
320 } else {
321 // Reset immediately
322 mAppsCustomizePane.reset();
323 }
324 }
325
Winson Chung785d2eb2011-04-14 16:08:02 -0700326 /* LauncherTransitionable overrides */
327 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700328 public void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700329 mInTransition = true;
Michael Jurka19e0fc52011-07-22 18:00:21 -0700330 // isHardwareAccelerated() checks if we're attached to a window and if that
331 // window is HW accelerated-- we were sometimes not attached to a window
332 // and buildLayer was throwing an IllegalStateException
333 if (animation != null && isHardwareAccelerated()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700334 // Turn on hardware layers for performance
335 setLayerType(LAYER_TYPE_HARDWARE, null);
336
337 // force building the layer at the beginning of the animation, so you don't get a
338 // blip early in the animation
339 buildLayer();
340 }
Michael Jurka430e8a52011-08-08 15:52:14 -0700341 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
342 mAppsCustomizePane.showScrollingIndicator(false);
343 }
Winson Chungc100e8e2011-08-09 16:02:43 -0700344 if (mResetAfterTransition) {
345 mAppsCustomizePane.reset();
346 mResetAfterTransition = false;
347 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700348 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700349
Winson Chung785d2eb2011-04-14 16:08:02 -0700350 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700351 public void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700352 mInTransition = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700353 if (animation != null) {
354 setLayerType(LAYER_TYPE_NONE, null);
355 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700356
Winson Chung7d7541e2011-09-16 20:14:36 -0700357 if (!toWorkspace) {
358 // Dismiss the cling if necessary
359 l.dismissWorkspaceCling(null);
360
361 if (!LauncherApplication.isScreenLarge()) {
362 mAppsCustomizePane.hideScrollingIndicator(false);
363 }
Winson Chung5a808352011-06-27 19:08:49 -0700364 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700365 }
Winson Chung70d72102011-08-12 11:24:35 -0700366
367 boolean isTransitioning() {
368 return mInTransition;
369 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700370}