blob: ed408df48e55a742481cfef5b37ad8289bedca1f [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 /**
55 * Convenience methods to select specific tabs
56 */
Winson Chung55b65502011-05-26 12:03:43 -070057 void selectAppsTab() {
58 setCurrentTabByTag(APPS_TAB_TAG);
59 }
60 void selectWidgetsTab() {
61 setCurrentTabByTag(WIDGETS_TAB_TAG);
62 }
63
Winson Chung785d2eb2011-04-14 16:08:02 -070064 /**
65 * Setup the tab host and create all necessary tabs.
66 */
67 @Override
68 protected void onFinishInflate() {
69 // Setup the tab host
70 setup();
71
Winson Chungfd3385f2011-06-15 19:51:24 -070072 final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container);
Winson Chungfaa13252011-06-13 18:15:54 -070073 final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070074 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070075 findViewById(R.id.apps_customize_pane_content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070076 mTabs = tabs;
Winson Chungfd3385f2011-06-15 19:51:24 -070077 mTabsContainer = tabsContainer;
Winson Chung4179b4e2011-05-31 17:28:12 -070078 mAppsCustomizePane = appsCustomizePane;
79 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -070080
81 // Configure the tabs content factory to return the same paged view (that we change the
82 // content filter on)
83 TabContentFactory contentFactory = new TabContentFactory() {
84 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -070085 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070086 }
87 };
88
89 // Create the tabs
90 TextView tabView;
91 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
92 tabView.setText(mContext.getString(R.string.all_apps_button_label));
93 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -070094 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
95 tabView.setText(mContext.getString(R.string.widgets_tab_label));
96 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -070097 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -070098
99 // Setup the key listener to jump between the last tab view and the market icon
100 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
101 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
102 lastTab.setOnKeyListener(keyListener);
103 View shopButton = findViewById(R.id.market_button);
104 shopButton.setOnKeyListener(keyListener);
Winson Chungfd3385f2011-06-15 19:51:24 -0700105
106 // Hide the tab bar until we measure
107 mTabsContainer.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700108 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700109
Winson Chungf0ea4d32011-06-06 14:27:16 -0700110 @Override
111 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
112 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
113 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
114
115 // Set the width of the tab list to the content width
116 if (remeasureTabWidth) {
Winson Chungfd3385f2011-06-15 19:51:24 -0700117 int contentWidth = mAppsCustomizePane.getPageContentWidth();
118 if (contentWidth > 0) {
119 // Set the width and show the tab bar (if we have a loading graphic, we can switch
120 // it off here)
121 mTabs.getLayoutParams().width = contentWidth;
122 mTabsContainer.animate().alpha(1f).setDuration(sTabBarFadeInDuration);
123 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700124 }
Winson Chungfd3385f2011-06-15 19:51:24 -0700125 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Winson Chung4179b4e2011-05-31 17:28:12 -0700126 }
127
128 @Override
129 public boolean onTouchEvent(MotionEvent event) {
130 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
131 // through to the workspace and trigger showWorkspace()
132 if (event.getY() < mAppsCustomizePane.getBottom()) {
133 return true;
134 }
135 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700136 }
137
138 @Override
139 public void onTabChanged(String tabId) {
Winson Chungb44b5242011-06-13 11:32:14 -0700140 final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId);
141 if (!mAppsCustomizePane.isContentType(type)) {
142 // Animate the changing of the tab content by fading pages in and out
143 final Resources res = getResources();
144 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
145
146 ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 0f);
147 anim.setDuration(duration);
148 anim.addListener(new AnimatorListenerAdapter() {
149 @Override
Winson Chung32174c82011-07-19 15:47:55 -0700150 public void onAnimationStart(android.animation.Animator animation) {
151 mAppsCustomizePane.hideScrollingIndicator(false);
152 }
153 @Override
Winson Chungb44b5242011-06-13 11:32:14 -0700154 public void onAnimationEnd(android.animation.Animator animation) {
155 mAppsCustomizePane.setContentType(type);
156
157 ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
158 anim.setDuration(duration);
Winson Chung3ac74c52011-06-30 17:39:37 -0700159 anim.addListener(new AnimatorListenerAdapter() {
160 @Override
161 public void onAnimationEnd(android.animation.Animator animation) {
162 mAppsCustomizePane.flashScrollingIndicator();
163 }
164 });
Winson Chungb44b5242011-06-13 11:32:14 -0700165 anim.start();
166 }
167 });
168 anim.start();
169 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700170 }
171
172 /**
173 * Returns the content type for the specified tab tag.
174 */
175 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
176 if (tag.equals(APPS_TAB_TAG)) {
177 return AppsCustomizePagedView.ContentType.Applications;
178 } else if (tag.equals(WIDGETS_TAB_TAG)) {
179 return AppsCustomizePagedView.ContentType.Widgets;
180 }
181 return AppsCustomizePagedView.ContentType.Applications;
182 }
183
184 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700185 * Returns the tab tag for a given content type.
186 */
187 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
188 if (type == AppsCustomizePagedView.ContentType.Applications) {
189 return APPS_TAB_TAG;
190 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
191 return WIDGETS_TAB_TAG;
192 }
193 return APPS_TAB_TAG;
194 }
195
196 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700197 * Disable focus on anything under this view in the hierarchy if we are not visible.
198 */
199 @Override
200 public int getDescendantFocusability() {
201 if (getVisibility() != View.VISIBLE) {
202 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
203 }
204 return super.getDescendantFocusability();
205 }
206
207 /* LauncherTransitionable overrides */
208 @Override
Winson Chungf0ea4d32011-06-06 14:27:16 -0700209 public void onLauncherTransitionStart(Animator animation) {
210 if (animation != null) {
211 // Turn on hardware layers for performance
212 setLayerType(LAYER_TYPE_HARDWARE, null);
213
214 // force building the layer at the beginning of the animation, so you don't get a
215 // blip early in the animation
216 buildLayer();
217 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700218 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700219
Winson Chung785d2eb2011-04-14 16:08:02 -0700220 @Override
Winson Chungf0ea4d32011-06-06 14:27:16 -0700221 public void onLauncherTransitionEnd(Animator animation) {
222 if (animation != null) {
223 setLayerType(LAYER_TYPE_NONE, null);
224 }
Winson Chung3ac74c52011-06-30 17:39:37 -0700225
226 mAppsCustomizePane.flashScrollingIndicator();
Winson Chung785d2eb2011-04-14 16:08:02 -0700227 }
228}