blob: 8d67e5d668b2978f96ec54b3e472ce2b9dfd8a1d [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);
Winson Chung5a808352011-06-27 19:08:49 -070078 setCurrentTabByTag(WIDGETS_TAB_TAG);
79 }
Winson Chung55b65502011-05-26 12:03:43 -070080
Winson Chung785d2eb2011-04-14 16:08:02 -070081 /**
82 * Setup the tab host and create all necessary tabs.
83 */
84 @Override
85 protected void onFinishInflate() {
86 // Setup the tab host
87 setup();
88
Winson Chungfd3385f2011-06-15 19:51:24 -070089 final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container);
Winson Chungfaa13252011-06-13 18:15:54 -070090 final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070091 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070092 findViewById(R.id.apps_customize_pane_content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070093 mTabs = tabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070094 mTabsContainer = tabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070095 mAppsCustomizePane = appsCustomizePane;
Winson Chungf314b0e2011-08-16 11:54:27 -070096 mAnimationBuffer = (ImageView) findViewById(R.id.animation_buffer);
Winson Chung4179b4e2011-05-31 17:28:12 -070097 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -070098
99 // Configure the tabs content factory to return the same paged view (that we change the
100 // content filter on)
101 TabContentFactory contentFactory = new TabContentFactory() {
102 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -0700103 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -0700104 }
105 };
106
107 // Create the tabs
108 TextView tabView;
Winson Chungd2c1f802011-10-14 12:26:54 -0700109 String label;
110 label = mContext.getString(R.string.all_apps_button_label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700111 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
Winson Chungd2c1f802011-10-14 12:26:54 -0700112 tabView.setText(label);
113 tabView.setContentDescription(label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700114 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chungd2c1f802011-10-14 12:26:54 -0700115 label = mContext.getString(R.string.widgets_tab_label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700116 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
Winson Chungd2c1f802011-10-14 12:26:54 -0700117 tabView.setText(label);
118 tabView.setContentDescription(label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700119 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700120 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -0700121
122 // Setup the key listener to jump between the last tab view and the market icon
123 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
124 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
125 lastTab.setOnKeyListener(keyListener);
126 View shopButton = findViewById(R.id.market_button);
127 shopButton.setOnKeyListener(keyListener);
Winson Chungfd3385f2011-06-15 19:51:24 -0700128
129 // Hide the tab bar until we measure
130 mTabsContainer.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700131 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700132
Winson Chungf0ea4d32011-06-06 14:27:16 -0700133 @Override
134 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
135 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
136 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
137
138 // Set the width of the tab list to the content width
139 if (remeasureTabWidth) {
Winson Chungfd3385f2011-06-15 19:51:24 -0700140 int contentWidth = mAppsCustomizePane.getPageContentWidth();
141 if (contentWidth > 0) {
142 // Set the width and show the tab bar (if we have a loading graphic, we can switch
143 // it off here)
144 mTabs.getLayoutParams().width = contentWidth;
Michael Jurkac57b7a82011-08-09 22:02:20 -0700145 post(new Runnable() {
146 public void run() {
147 mTabs.requestLayout();
Winson Chung7ed37742011-09-08 15:45:51 -0700148 mTabsContainer.setAlpha(1f);
Michael Jurkac57b7a82011-08-09 22:02:20 -0700149 }
150 });
Winson Chungfd3385f2011-06-15 19:51:24 -0700151 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700152 }
Winson Chungfd3385f2011-06-15 19:51:24 -0700153 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Winson Chung4179b4e2011-05-31 17:28:12 -0700154 }
155
156 @Override
157 public boolean onTouchEvent(MotionEvent event) {
158 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
159 // through to the workspace and trigger showWorkspace()
160 if (event.getY() < mAppsCustomizePane.getBottom()) {
161 return true;
162 }
163 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700164 }
165
Winson Chung5a808352011-06-27 19:08:49 -0700166 private void onTabChangedStart() {
167 mAppsCustomizePane.hideScrollingIndicator(false);
168 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700169
Winson Chung5a808352011-06-27 19:08:49 -0700170 private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) {
171 mAppsCustomizePane.setContentType(type);
172 }
173
Winson Chung785d2eb2011-04-14 16:08:02 -0700174 @Override
175 public void onTabChanged(String tabId) {
Winson Chungb44b5242011-06-13 11:32:14 -0700176 final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId);
Adam Cohen0cd3b642011-10-14 14:58:00 -0700177 if (mSuppressContentCallback) {
178 mSuppressContentCallback = false;
179 return;
Winson Chungb44b5242011-06-13 11:32:14 -0700180 }
Adam Cohen0cd3b642011-10-14 14:58:00 -0700181
182 // Animate the changing of the tab content by fading pages in and out
183 final Resources res = getResources();
184 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
185
186 // We post a runnable here because there is a delay while the first page is loading and
187 // the feedback from having changed the tab almost feels better than having it stick
188 post(new Runnable() {
189 @Override
190 public void run() {
191 // Setup the animation buffer
192 Bitmap b = Bitmap.createBitmap(mAppsCustomizePane.getMeasuredWidth(),
193 mAppsCustomizePane.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
194 Canvas c = new Canvas(b);
195 mAppsCustomizePane.draw(c);
196 mAppsCustomizePane.setAlpha(0f);
197 mAnimationBuffer.setImageBitmap(b);
198 mAnimationBuffer.setAlpha(1f);
199 mAnimationBuffer.setVisibility(View.VISIBLE);
200 c.setBitmap(null);
201 b = null;
202
203 // Toggle the new content
204 onTabChangedStart();
205 onTabChangedEnd(type);
206
207 // Animate the transition
208 ObjectAnimator outAnim = ObjectAnimator.ofFloat(mAnimationBuffer, "alpha", 0f);
209 outAnim.addListener(new AnimatorListenerAdapter() {
210 @Override
211 public void onAnimationEnd(Animator animation) {
212 mAnimationBuffer.setVisibility(View.GONE);
213 mAnimationBuffer.setImageBitmap(null);
214 }
215 });
216 ObjectAnimator inAnim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
217 inAnim.addListener(new AnimatorListenerAdapter() {
218 @Override
219 public void onAnimationEnd(Animator animation) {
220 if (!LauncherApplication.isScreenLarge()) {
221 mAppsCustomizePane.flashScrollingIndicator();
222 }
223 mAppsCustomizePane.loadAssociatedPages(
224 mAppsCustomizePane.getCurrentPage());
Adam Cohenae4f1552011-10-20 00:15:42 -0700225 mAppsCustomizePane.requestFocus();
Adam Cohen0cd3b642011-10-14 14:58:00 -0700226 }
227 });
228 AnimatorSet animSet = new AnimatorSet();
229 animSet.playTogether(outAnim, inAnim);
230 animSet.setDuration(duration);
231 animSet.start();
232 }
233 });
234 }
235
236 public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) {
237 mSuppressContentCallback = true;
238 setCurrentTabByTag(getTabTagForContentType(type));
Winson Chung785d2eb2011-04-14 16:08:02 -0700239 }
240
241 /**
242 * Returns the content type for the specified tab tag.
243 */
244 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
245 if (tag.equals(APPS_TAB_TAG)) {
246 return AppsCustomizePagedView.ContentType.Applications;
247 } else if (tag.equals(WIDGETS_TAB_TAG)) {
248 return AppsCustomizePagedView.ContentType.Widgets;
249 }
250 return AppsCustomizePagedView.ContentType.Applications;
251 }
252
253 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700254 * Returns the tab tag for a given content type.
255 */
256 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
257 if (type == AppsCustomizePagedView.ContentType.Applications) {
258 return APPS_TAB_TAG;
259 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
260 return WIDGETS_TAB_TAG;
261 }
262 return APPS_TAB_TAG;
263 }
264
265 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700266 * Disable focus on anything under this view in the hierarchy if we are not visible.
267 */
268 @Override
269 public int getDescendantFocusability() {
270 if (getVisibility() != View.VISIBLE) {
271 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
272 }
273 return super.getDescendantFocusability();
274 }
275
Winson Chungc100e8e2011-08-09 16:02:43 -0700276 void reset() {
277 if (mInTransition) {
278 // Defer to after the transition to reset
279 mResetAfterTransition = true;
280 } else {
281 // Reset immediately
282 mAppsCustomizePane.reset();
283 }
284 }
285
Winson Chung785d2eb2011-04-14 16:08:02 -0700286 /* LauncherTransitionable overrides */
287 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700288 public void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700289 mInTransition = true;
Michael Jurka19e0fc52011-07-22 18:00:21 -0700290 // isHardwareAccelerated() checks if we're attached to a window and if that
291 // window is HW accelerated-- we were sometimes not attached to a window
292 // and buildLayer was throwing an IllegalStateException
293 if (animation != null && isHardwareAccelerated()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700294 // Turn on hardware layers for performance
295 setLayerType(LAYER_TYPE_HARDWARE, null);
296
297 // force building the layer at the beginning of the animation, so you don't get a
298 // blip early in the animation
299 buildLayer();
300 }
Michael Jurka430e8a52011-08-08 15:52:14 -0700301 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
302 mAppsCustomizePane.showScrollingIndicator(false);
303 }
Winson Chungc100e8e2011-08-09 16:02:43 -0700304 if (mResetAfterTransition) {
305 mAppsCustomizePane.reset();
306 mResetAfterTransition = false;
307 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700308 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700309
Winson Chung785d2eb2011-04-14 16:08:02 -0700310 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700311 public void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700312 mInTransition = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700313 if (animation != null) {
314 setLayerType(LAYER_TYPE_NONE, null);
315 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700316
Winson Chung7d7541e2011-09-16 20:14:36 -0700317 if (!toWorkspace) {
318 // Dismiss the cling if necessary
319 l.dismissWorkspaceCling(null);
320
321 if (!LauncherApplication.isScreenLarge()) {
322 mAppsCustomizePane.hideScrollingIndicator(false);
323 }
Winson Chung5a808352011-06-27 19:08:49 -0700324 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700325 }
Winson Chung70d72102011-08-12 11:24:35 -0700326
327 boolean isTransitioning() {
328 return mInTransition;
329 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700330}