blob: b6bc5dc5215ba047cb46a592119e9c68422409ac [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;
21import android.animation.ObjectAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.content.Context;
23import android.content.res.Resources;
24import android.util.AttributeSet;
25import android.view.LayoutInflater;
Winson Chung4179b4e2011-05-31 17:28:12 -070026import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070027import android.view.View;
28import android.view.ViewGroup;
Winson Chungb44b5242011-06-13 11:32:14 -070029import android.view.ViewPropertyAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.widget.TabHost;
Winson Chungfaa13252011-06-13 18:15:54 -070031import android.widget.TabWidget;
Winson Chung785d2eb2011-04-14 16:08:02 -070032import android.widget.TextView;
33
34import com.android.launcher.R;
35
36public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable,
37 TabHost.OnTabChangeListener {
38 static final String LOG_TAG = "AppsCustomizeTabHost";
39
40 private static final String APPS_TAB_TAG = "APPS";
41 private static final String WIDGETS_TAB_TAG = "WIDGETS";
Winson Chungfd3385f2011-06-15 19:51:24 -070042 private static final int sTabBarFadeInDuration = 150;
Winson Chung785d2eb2011-04-14 16:08:02 -070043
44 private final LayoutInflater mLayoutInflater;
Winson Chungf0ea4d32011-06-06 14:27:16 -070045 private ViewGroup mTabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070046 private ViewGroup mTabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070047 private AppsCustomizePagedView mAppsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070048
49 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
50 super(context, attrs);
51 mLayoutInflater = LayoutInflater.from(context);
52 }
53
Winson Chungf0ea4d32011-06-06 14:27:16 -070054 /**
Winson Chung5a808352011-06-27 19:08:49 -070055 * Convenience methods to select specific tabs. We want to set the content type immediately
56 * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view
57 * reflects the new content (but doesn't do the animation and logic associated with changing
58 * tabs manually).
Winson Chungf0ea4d32011-06-06 14:27:16 -070059 */
Winson Chung5a808352011-06-27 19:08:49 -070060 private void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) {
61 onTabChangedStart();
62 onTabChangedEnd(type);
63 }
Winson Chung55b65502011-05-26 12:03:43 -070064 void selectAppsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070065 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Applications);
Winson Chung55b65502011-05-26 12:03:43 -070066 setCurrentTabByTag(APPS_TAB_TAG);
67 }
68 void selectWidgetsTab() {
Winson Chung5a808352011-06-27 19:08:49 -070069 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Widgets);
70 mAppsCustomizePane.setCurrentPageToWidgets();
71
72 setCurrentTabByTag(WIDGETS_TAB_TAG);
73 }
74 void selectShortcutsTab() {
75 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Widgets);
76 mAppsCustomizePane.setCurrentPageToShortcuts();
77
Winson Chung55b65502011-05-26 12:03:43 -070078 setCurrentTabByTag(WIDGETS_TAB_TAG);
79 }
80
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;
96 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;
108 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
109 tabView.setText(mContext.getString(R.string.all_apps_button_label));
110 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700111 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
112 tabView.setText(mContext.getString(R.string.widgets_tab_label));
113 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -0700114 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -0700115
116 // Setup the key listener to jump between the last tab view and the market icon
117 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
118 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
119 lastTab.setOnKeyListener(keyListener);
120 View shopButton = findViewById(R.id.market_button);
121 shopButton.setOnKeyListener(keyListener);
Winson Chungfd3385f2011-06-15 19:51:24 -0700122
123 // Hide the tab bar until we measure
124 mTabsContainer.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700125 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700126
Winson Chungf0ea4d32011-06-06 14:27:16 -0700127 @Override
128 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
129 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
130 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
131
132 // Set the width of the tab list to the content width
133 if (remeasureTabWidth) {
Winson Chungfd3385f2011-06-15 19:51:24 -0700134 int contentWidth = mAppsCustomizePane.getPageContentWidth();
135 if (contentWidth > 0) {
136 // Set the width and show the tab bar (if we have a loading graphic, we can switch
137 // it off here)
138 mTabs.getLayoutParams().width = contentWidth;
139 mTabsContainer.animate().alpha(1f).setDuration(sTabBarFadeInDuration);
140 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700141 }
Winson Chungfd3385f2011-06-15 19:51:24 -0700142 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Winson Chung4179b4e2011-05-31 17:28:12 -0700143 }
144
145 @Override
146 public boolean onTouchEvent(MotionEvent event) {
147 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
148 // through to the workspace and trigger showWorkspace()
149 if (event.getY() < mAppsCustomizePane.getBottom()) {
150 return true;
151 }
152 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700153 }
154
Winson Chung5a808352011-06-27 19:08:49 -0700155 private void onTabChangedStart() {
156 mAppsCustomizePane.hideScrollingIndicator(false);
157 }
158 private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) {
159 mAppsCustomizePane.setContentType(type);
160 }
161
Winson Chung785d2eb2011-04-14 16:08:02 -0700162 @Override
163 public void onTabChanged(String tabId) {
Winson Chungb44b5242011-06-13 11:32:14 -0700164 final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId);
165 if (!mAppsCustomizePane.isContentType(type)) {
166 // Animate the changing of the tab content by fading pages in and out
167 final Resources res = getResources();
168 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
169
170 ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 0f);
171 anim.setDuration(duration);
172 anim.addListener(new AnimatorListenerAdapter() {
173 @Override
Winson Chung32174c82011-07-19 15:47:55 -0700174 public void onAnimationStart(android.animation.Animator animation) {
Winson Chung5a808352011-06-27 19:08:49 -0700175 onTabChangedStart();
Winson Chung32174c82011-07-19 15:47:55 -0700176 }
177 @Override
Winson Chungb44b5242011-06-13 11:32:14 -0700178 public void onAnimationEnd(android.animation.Animator animation) {
Winson Chung5a808352011-06-27 19:08:49 -0700179 onTabChangedEnd(type);
Winson Chungb44b5242011-06-13 11:32:14 -0700180
181 ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
182 anim.setDuration(duration);
Winson Chung3ac74c52011-06-30 17:39:37 -0700183 anim.addListener(new AnimatorListenerAdapter() {
184 @Override
185 public void onAnimationEnd(android.animation.Animator animation) {
Winson Chung4afe9b32011-07-27 17:46:20 -0700186 if (!LauncherApplication.isScreenLarge()) {
187 mAppsCustomizePane.flashScrollingIndicator();
188 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700189 }
190 });
Winson Chungb44b5242011-06-13 11:32:14 -0700191 anim.start();
192 }
193 });
194 anim.start();
195 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700196 }
197
198 /**
199 * Returns the content type for the specified tab tag.
200 */
201 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
202 if (tag.equals(APPS_TAB_TAG)) {
203 return AppsCustomizePagedView.ContentType.Applications;
204 } else if (tag.equals(WIDGETS_TAB_TAG)) {
205 return AppsCustomizePagedView.ContentType.Widgets;
206 }
207 return AppsCustomizePagedView.ContentType.Applications;
208 }
209
210 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700211 * Returns the tab tag for a given content type.
212 */
213 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
214 if (type == AppsCustomizePagedView.ContentType.Applications) {
215 return APPS_TAB_TAG;
216 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
217 return WIDGETS_TAB_TAG;
218 }
219 return APPS_TAB_TAG;
220 }
221
222 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700223 * Disable focus on anything under this view in the hierarchy if we are not visible.
224 */
225 @Override
226 public int getDescendantFocusability() {
227 if (getVisibility() != View.VISIBLE) {
228 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
229 }
230 return super.getDescendantFocusability();
231 }
232
233 /* LauncherTransitionable overrides */
234 @Override
Winson Chung5a808352011-06-27 19:08:49 -0700235 public void onLauncherTransitionStart(Animator animation, boolean toWorkspace) {
Michael Jurka19e0fc52011-07-22 18:00:21 -0700236 // isHardwareAccelerated() checks if we're attached to a window and if that
237 // window is HW accelerated-- we were sometimes not attached to a window
238 // and buildLayer was throwing an IllegalStateException
239 if (animation != null && isHardwareAccelerated()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700240 // Turn on hardware layers for performance
241 setLayerType(LAYER_TYPE_HARDWARE, null);
242
243 // force building the layer at the beginning of the animation, so you don't get a
244 // blip early in the animation
245 buildLayer();
246 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700247 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700248
Winson Chung785d2eb2011-04-14 16:08:02 -0700249 @Override
Winson Chung5a808352011-06-27 19:08:49 -0700250 public void onLauncherTransitionEnd(Animator animation, boolean toWorkspace) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700251 if (animation != null) {
252 setLayerType(LAYER_TYPE_NONE, null);
253 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700254
Winson Chung4afe9b32011-07-27 17:46:20 -0700255 if (!toWorkspace && !LauncherApplication.isScreenLarge()) {
Winson Chung5a808352011-06-27 19:08:49 -0700256 mAppsCustomizePane.flashScrollingIndicator();
257 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700258 }
259}