blob: d37b1c9029ae67b64de19de381a28dc07933494b [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;
Winson Chungf314b0e2011-08-16 11:54:27 -070051 private ImageView 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);
77 mAppsCustomizePane.setCurrentPageToWidgets();
78
79 setCurrentTabByTag(WIDGETS_TAB_TAG);
80 }
Winson Chung55b65502011-05-26 12:03:43 -070081
Winson Chung785d2eb2011-04-14 16:08:02 -070082 /**
83 * Setup the tab host and create all necessary tabs.
84 */
85 @Override
86 protected void onFinishInflate() {
87 // Setup the tab host
88 setup();
89
Winson Chungfd3385f2011-06-15 19:51:24 -070090 final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container);
Winson Chungfaa13252011-06-13 18:15:54 -070091 final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070092 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070093 findViewById(R.id.apps_customize_pane_content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070094 mTabs = tabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070095 mTabsContainer = tabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070096 mAppsCustomizePane = appsCustomizePane;
Winson Chungf314b0e2011-08-16 11:54:27 -070097 mAnimationBuffer = (ImageView) findViewById(R.id.animation_buffer);
Winson Chung4179b4e2011-05-31 17:28:12 -070098 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -070099
100 // Configure the tabs content factory to return the same paged view (that we change the
101 // content filter on)
102 TabContentFactory contentFactory = new TabContentFactory() {
103 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -0700104 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -0700105 }
106 };
107
108 // Create the tabs
109 TextView tabView;
Winson Chungd2c1f802011-10-14 12:26:54 -0700110 String label;
111 label = mContext.getString(R.string.all_apps_button_label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700112 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
Winson Chungd2c1f802011-10-14 12:26:54 -0700113 tabView.setText(label);
114 tabView.setContentDescription(label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700115 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chungd2c1f802011-10-14 12:26:54 -0700116 label = mContext.getString(R.string.widgets_tab_label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700117 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
Winson Chungd2c1f802011-10-14 12:26:54 -0700118 tabView.setText(label);
119 tabView.setContentDescription(label);
Winson Chung785d2eb2011-04-14 16:08:02 -0700120 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700121 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -0700122
123 // Setup the key listener to jump between the last tab view and the market icon
124 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
125 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
126 lastTab.setOnKeyListener(keyListener);
127 View shopButton = findViewById(R.id.market_button);
128 shopButton.setOnKeyListener(keyListener);
Winson Chungfd3385f2011-06-15 19:51:24 -0700129
130 // Hide the tab bar until we measure
131 mTabsContainer.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700132 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700133
Winson Chungf0ea4d32011-06-06 14:27:16 -0700134 @Override
135 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
136 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
137 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
138
139 // Set the width of the tab list to the content width
140 if (remeasureTabWidth) {
Winson Chungfd3385f2011-06-15 19:51:24 -0700141 int contentWidth = mAppsCustomizePane.getPageContentWidth();
142 if (contentWidth > 0) {
143 // Set the width and show the tab bar (if we have a loading graphic, we can switch
144 // it off here)
145 mTabs.getLayoutParams().width = contentWidth;
Michael Jurkac57b7a82011-08-09 22:02:20 -0700146 post(new Runnable() {
147 public void run() {
148 mTabs.requestLayout();
Winson Chung7ed37742011-09-08 15:45:51 -0700149 mTabsContainer.setAlpha(1f);
Michael Jurkac57b7a82011-08-09 22:02:20 -0700150 }
151 });
Winson Chungfd3385f2011-06-15 19:51:24 -0700152 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700153 }
Winson Chungfd3385f2011-06-15 19:51:24 -0700154 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Winson Chung4179b4e2011-05-31 17:28:12 -0700155 }
156
157 @Override
158 public boolean onTouchEvent(MotionEvent event) {
159 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
160 // through to the workspace and trigger showWorkspace()
161 if (event.getY() < mAppsCustomizePane.getBottom()) {
162 return true;
163 }
164 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700165 }
166
Winson Chung5a808352011-06-27 19:08:49 -0700167 private void onTabChangedStart() {
168 mAppsCustomizePane.hideScrollingIndicator(false);
169 }
170 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);
177 if (!mAppsCustomizePane.isContentType(type)) {
178 // Animate the changing of the tab content by fading pages in and out
179 final Resources res = getResources();
180 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
181
Winson Chungf314b0e2011-08-16 11:54:27 -0700182 // We post a runnable here because there is a delay while the first page is loading and
183 // the feedback from having changed the tab almost feels better than having it stick
184 post(new Runnable() {
Winson Chungb44b5242011-06-13 11:32:14 -0700185 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -0700186 public void run() {
187 // Setup the animation buffer
188 Bitmap b = Bitmap.createBitmap(mAppsCustomizePane.getMeasuredWidth(),
189 mAppsCustomizePane.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
190 Canvas c = new Canvas(b);
191 mAppsCustomizePane.draw(c);
192 mAppsCustomizePane.setAlpha(0f);
193 mAnimationBuffer.setImageBitmap(b);
194 mAnimationBuffer.setAlpha(1f);
195 mAnimationBuffer.setVisibility(View.VISIBLE);
196 c.setBitmap(null);
197 b = null;
198
199 // Toggle the new content
Winson Chung5a808352011-06-27 19:08:49 -0700200 onTabChangedStart();
Winson Chung5a808352011-06-27 19:08:49 -0700201 onTabChangedEnd(type);
Winson Chungb44b5242011-06-13 11:32:14 -0700202
Winson Chungf314b0e2011-08-16 11:54:27 -0700203 // Animate the transition
204 ObjectAnimator outAnim = ObjectAnimator.ofFloat(mAnimationBuffer, "alpha", 0f);
205 outAnim.addListener(new AnimatorListenerAdapter() {
Winson Chung3ac74c52011-06-30 17:39:37 -0700206 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -0700207 public void onAnimationEnd(Animator animation) {
208 mAnimationBuffer.setVisibility(View.GONE);
209 mAnimationBuffer.setImageBitmap(null);
210 }
211 });
212 ObjectAnimator inAnim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
213 inAnim.addListener(new AnimatorListenerAdapter() {
214 @Override
215 public void onAnimationEnd(Animator animation) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700216 if (!LauncherApplication.isScreenLarge()) {
217 mAppsCustomizePane.flashScrollingIndicator();
218 }
Winson Chungf314b0e2011-08-16 11:54:27 -0700219 mAppsCustomizePane.loadAssociatedPages(
220 mAppsCustomizePane.getCurrentPage());
Winson Chung3ac74c52011-06-30 17:39:37 -0700221 }
222 });
Winson Chungf314b0e2011-08-16 11:54:27 -0700223 AnimatorSet animSet = new AnimatorSet();
224 animSet.playTogether(outAnim, inAnim);
225 animSet.setDuration(duration);
226 animSet.start();
Winson Chungb44b5242011-06-13 11:32:14 -0700227 }
228 });
Winson Chungb44b5242011-06-13 11:32:14 -0700229 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700230 }
231
232 /**
233 * Returns the content type for the specified tab tag.
234 */
235 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
236 if (tag.equals(APPS_TAB_TAG)) {
237 return AppsCustomizePagedView.ContentType.Applications;
238 } else if (tag.equals(WIDGETS_TAB_TAG)) {
239 return AppsCustomizePagedView.ContentType.Widgets;
240 }
241 return AppsCustomizePagedView.ContentType.Applications;
242 }
243
244 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700245 * Returns the tab tag for a given content type.
246 */
247 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
248 if (type == AppsCustomizePagedView.ContentType.Applications) {
249 return APPS_TAB_TAG;
250 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
251 return WIDGETS_TAB_TAG;
252 }
253 return APPS_TAB_TAG;
254 }
255
256 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700257 * Disable focus on anything under this view in the hierarchy if we are not visible.
258 */
259 @Override
260 public int getDescendantFocusability() {
261 if (getVisibility() != View.VISIBLE) {
262 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
263 }
264 return super.getDescendantFocusability();
265 }
266
Winson Chungc100e8e2011-08-09 16:02:43 -0700267 void reset() {
268 if (mInTransition) {
269 // Defer to after the transition to reset
270 mResetAfterTransition = true;
271 } else {
272 // Reset immediately
273 mAppsCustomizePane.reset();
274 }
275 }
276
Winson Chung785d2eb2011-04-14 16:08:02 -0700277 /* LauncherTransitionable overrides */
278 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700279 public void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700280 mInTransition = true;
Michael Jurka19e0fc52011-07-22 18:00:21 -0700281 // isHardwareAccelerated() checks if we're attached to a window and if that
282 // window is HW accelerated-- we were sometimes not attached to a window
283 // and buildLayer was throwing an IllegalStateException
284 if (animation != null && isHardwareAccelerated()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700285 // Turn on hardware layers for performance
286 setLayerType(LAYER_TYPE_HARDWARE, null);
287
288 // force building the layer at the beginning of the animation, so you don't get a
289 // blip early in the animation
290 buildLayer();
291 }
Michael Jurka430e8a52011-08-08 15:52:14 -0700292 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
293 mAppsCustomizePane.showScrollingIndicator(false);
294 }
Winson Chungc100e8e2011-08-09 16:02:43 -0700295 if (mResetAfterTransition) {
296 mAppsCustomizePane.reset();
297 mResetAfterTransition = false;
298 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700299 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700300
Winson Chung785d2eb2011-04-14 16:08:02 -0700301 @Override
Winson Chung7d7541e2011-09-16 20:14:36 -0700302 public void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace) {
Winson Chungc100e8e2011-08-09 16:02:43 -0700303 mInTransition = false;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700304 if (animation != null) {
305 setLayerType(LAYER_TYPE_NONE, null);
306 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700307
Winson Chung7d7541e2011-09-16 20:14:36 -0700308 if (!toWorkspace) {
309 // Dismiss the cling if necessary
310 l.dismissWorkspaceCling(null);
311
312 if (!LauncherApplication.isScreenLarge()) {
313 mAppsCustomizePane.hideScrollingIndicator(false);
314 }
Winson Chung5a808352011-06-27 19:08:49 -0700315 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700316 }
Winson Chung70d72102011-08-12 11:24:35 -0700317
318 boolean isTransitioning() {
319 return mInTransition;
320 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700321}