blob: 25f67cb481cbd51360b6f7422af0b4accb630af1 [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;
Winson Chungf314b0e2011-08-16 11:54:27 -070025import android.graphics.Bitmap;
26import android.graphics.Canvas;
Winson Chung785d2eb2011-04-14 16:08:02 -070027import android.util.AttributeSet;
28import android.view.LayoutInflater;
Winson Chung4179b4e2011-05-31 17:28:12 -070029import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.view.View;
31import android.view.ViewGroup;
Winson Chungf314b0e2011-08-16 11:54:27 -070032import android.view.animation.DecelerateInterpolator;
33import android.widget.ImageView;
Winson Chung785d2eb2011-04-14 16:08:02 -070034import android.widget.TabHost;
Winson Chungfaa13252011-06-13 18:15:54 -070035import android.widget.TabWidget;
Winson Chung785d2eb2011-04-14 16:08:02 -070036import android.widget.TextView;
37
38import com.android.launcher.R;
39
40public 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;
Winson Chungf314b0e2011-08-16 11:54:27 -070052 private ImageView mAnimationBuffer;
Winson Chung785d2eb2011-04-14 16:08:02 -070053
Winson Chungc100e8e2011-08-09 16:02:43 -070054 private boolean mInTransition;
55 private boolean mResetAfterTransition;
56
Winson Chung785d2eb2011-04-14 16:08:02 -070057 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
58 super(context, attrs);
59 mLayoutInflater = LayoutInflater.from(context);
60 }
61
Winson Chungf0ea4d32011-06-06 14:27:16 -070062 /**
Winson Chung5a808352011-06-27 19:08:49 -070063 * Convenience methods to select specific tabs. We want to set the content type immediately
64 * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view
65 * reflects the new content (but doesn't do the animation and logic associated with changing
66 * tabs manually).
Winson Chungf0ea4d32011-06-06 14:27:16 -070067 */
Winson Chung5a808352011-06-27 19:08:49 -070068 private void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) {
69 onTabChangedStart();
70 onTabChangedEnd(type);
71 }
Winson Chung55b65502011-05-26 12:03:43 -070072 void selectAppsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070073 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Applications);
Winson Chung55b65502011-05-26 12:03:43 -070074 setCurrentTabByTag(APPS_TAB_TAG);
75 }
76 void selectWidgetsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070077 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Widgets);
78 mAppsCustomizePane.setCurrentPageToWidgets();
79
80 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;
Winson Chungf314b0e2011-08-16 11:54:27 -070098 mAnimationBuffer = (ImageView) findViewById(R.id.animation_buffer);
Winson Chung4179b4e2011-05-31 17:28:12 -070099 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -0700100
101 // Configure the tabs content factory to return the same paged view (that we change the
102 // content filter on)
103 TabContentFactory contentFactory = new TabContentFactory() {
104 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -0700105 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -0700106 }
107 };
108
109 // Create the tabs
110 TextView tabView;
111 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
112 tabView.setText(mContext.getString(R.string.all_apps_button_label));
113 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700114 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
115 tabView.setText(mContext.getString(R.string.widgets_tab_label));
116 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700117 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -0700118
119 // Setup the key listener to jump between the last tab view and the market icon
120 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
121 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
122 lastTab.setOnKeyListener(keyListener);
123 View shopButton = findViewById(R.id.market_button);
124 shopButton.setOnKeyListener(keyListener);
Winson Chungfd3385f2011-06-15 19:51:24 -0700125
126 // Hide the tab bar until we measure
127 mTabsContainer.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700128 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700129
Winson Chungf0ea4d32011-06-06 14:27:16 -0700130 @Override
131 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
132 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
133 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
134
135 // Set the width of the tab list to the content width
136 if (remeasureTabWidth) {
Winson Chungfd3385f2011-06-15 19:51:24 -0700137 int contentWidth = mAppsCustomizePane.getPageContentWidth();
138 if (contentWidth > 0) {
139 // Set the width and show the tab bar (if we have a loading graphic, we can switch
140 // it off here)
141 mTabs.getLayoutParams().width = contentWidth;
Michael Jurkac57b7a82011-08-09 22:02:20 -0700142 post(new Runnable() {
143 public void run() {
144 mTabs.requestLayout();
Winson Chung7ed37742011-09-08 15:45:51 -0700145 mTabsContainer.setAlpha(1f);
Michael Jurkac57b7a82011-08-09 22:02:20 -0700146 }
147 });
Winson Chungfd3385f2011-06-15 19:51:24 -0700148 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700149 }
Winson Chungfd3385f2011-06-15 19:51:24 -0700150 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Winson Chung4179b4e2011-05-31 17:28:12 -0700151 }
152
153 @Override
154 public boolean onTouchEvent(MotionEvent event) {
155 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
156 // through to the workspace and trigger showWorkspace()
157 if (event.getY() < mAppsCustomizePane.getBottom()) {
158 return true;
159 }
160 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700161 }
162
Winson Chung5a808352011-06-27 19:08:49 -0700163 private void onTabChangedStart() {
164 mAppsCustomizePane.hideScrollingIndicator(false);
165 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700166
Winson Chung5a808352011-06-27 19:08:49 -0700167 private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) {
168 mAppsCustomizePane.setContentType(type);
169 }
170
Winson Chung785d2eb2011-04-14 16:08:02 -0700171 @Override
172 public void onTabChanged(String tabId) {
Winson Chungb44b5242011-06-13 11:32:14 -0700173 final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700174 if (mSuppressContentCallback) {
175 mSuppressContentCallback = false;
176 return;
Winson Chungb44b5242011-06-13 11:32:14 -0700177 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700178
179 // Animate the changing of the tab content by fading pages in and out
180 final Resources res = getResources();
181 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
182
183 // We post a runnable here because there is a delay while the first page is loading and
184 // the feedback from having changed the tab almost feels better than having it stick
185 post(new Runnable() {
186 @Override
187 public void run() {
188 // Setup the animation buffer
189 Bitmap b = Bitmap.createBitmap(mAppsCustomizePane.getMeasuredWidth(),
190 mAppsCustomizePane.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
191 Canvas c = new Canvas(b);
192 mAppsCustomizePane.draw(c);
193 mAppsCustomizePane.setAlpha(0f);
194 mAnimationBuffer.setImageBitmap(b);
195 mAnimationBuffer.setAlpha(1f);
196 mAnimationBuffer.setVisibility(View.VISIBLE);
197 c.setBitmap(null);
198 b = null;
199
200 // Toggle the new content
201 onTabChangedStart();
202 onTabChangedEnd(type);
203
204 // Animate the transition
205 ObjectAnimator outAnim = ObjectAnimator.ofFloat(mAnimationBuffer, "alpha", 0f);
206 outAnim.addListener(new AnimatorListenerAdapter() {
207 @Override
208 public void onAnimationEnd(Animator animation) {
209 mAnimationBuffer.setVisibility(View.GONE);
210 mAnimationBuffer.setImageBitmap(null);
211 }
212 });
213 ObjectAnimator inAnim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
214 inAnim.addListener(new AnimatorListenerAdapter() {
215 @Override
216 public void onAnimationEnd(Animator animation) {
217 if (!LauncherApplication.isScreenLarge()) {
218 mAppsCustomizePane.flashScrollingIndicator();
219 }
220 mAppsCustomizePane.loadAssociatedPages(
221 mAppsCustomizePane.getCurrentPage());
222 }
223 });
224 AnimatorSet animSet = new AnimatorSet();
225 animSet.playTogether(outAnim, inAnim);
226 animSet.setDuration(duration);
227 animSet.start();
228 }
229 });
230 }
231
232 public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) {
233 mSuppressContentCallback = true;
234 setCurrentTabByTag(getTabTagForContentType(type));
Winson Chung785d2eb2011-04-14 16:08:02 -0700235 }
236
237 /**
238 * Returns the content type for the specified tab tag.
239 */
240 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
241 if (tag.equals(APPS_TAB_TAG)) {
242 return AppsCustomizePagedView.ContentType.Applications;
243 } else if (tag.equals(WIDGETS_TAB_TAG)) {
244 return AppsCustomizePagedView.ContentType.Widgets;
245 }
246 return AppsCustomizePagedView.ContentType.Applications;
247 }
248
249 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700250 * Returns the tab tag for a given content type.
251 */
252 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
253 if (type == AppsCustomizePagedView.ContentType.Applications) {
254 return APPS_TAB_TAG;
255 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
256 return WIDGETS_TAB_TAG;
257 }
258 return APPS_TAB_TAG;
259 }
260
261 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700262 * Disable focus on anything under this view in the hierarchy if we are not visible.
263 */
264 @Override
265 public int getDescendantFocusability() {
266 if (getVisibility() != View.VISIBLE) {
267 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
268 }
269 return super.getDescendantFocusability();
270 }
271
Winson Chungc100e8e2011-08-09 16:02:43 -0700272 void reset() {
273 if (mInTransition) {
274 // Defer to after the transition to reset
275 mResetAfterTransition = true;
276 } else {
277 // Reset immediately
278 mAppsCustomizePane.reset();
279 }
280 }
281
Winson Chung785d2eb2011-04-14 16:08:02 -0700282 /* LauncherTransitionable overrides */
283 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700284 public void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700285 mInTransition = true;
Michael Jurka19e0fc52011-07-22 18:00:21 -0700286 // isHardwareAccelerated() checks if we're attached to a window and if that
287 // window is HW accelerated-- we were sometimes not attached to a window
288 // and buildLayer was throwing an IllegalStateException
289 if (animation != null && isHardwareAccelerated()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700290 // Turn on hardware layers for performance
291 setLayerType(LAYER_TYPE_HARDWARE, null);
292
293 // force building the layer at the beginning of the animation, so you don't get a
294 // blip early in the animation
295 buildLayer();
296 }
Michael Jurka430e8a52011-08-08 15:52:14 -0700297 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
298 mAppsCustomizePane.showScrollingIndicator(false);
299 }
Winson Chungc100e8e2011-08-09 16:02:43 -0700300 if (mResetAfterTransition) {
301 mAppsCustomizePane.reset();
302 mResetAfterTransition = false;
303 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700304 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700305
Winson Chung785d2eb2011-04-14 16:08:02 -0700306 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700307 public void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700308 mInTransition = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700309 if (animation != null) {
310 setLayerType(LAYER_TYPE_NONE, null);
311 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700312
Winson Chung7d7541e2011-09-16 20:14:36 -0700313 if (!toWorkspace) {
314 // Dismiss the cling if necessary
315 l.dismissWorkspaceCling(null);
316
317 if (!LauncherApplication.isScreenLarge()) {
318 mAppsCustomizePane.hideScrollingIndicator(false);
319 }
Winson Chung5a808352011-06-27 19:08:49 -0700320 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700321 }
Winson Chung70d72102011-08-12 11:24:35 -0700322
323 boolean isTransitioning() {
324 return mInTransition;
325 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700326}