Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ObjectAnimator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.res.Resources; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.view.LayoutInflater; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 26 | import android.view.MotionEvent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 29 | import android.view.ViewPropertyAnimator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 30 | import android.widget.TabHost; |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 31 | import android.widget.TabWidget; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 32 | import android.widget.TextView; |
| 33 | |
| 34 | import com.android.launcher.R; |
| 35 | |
| 36 | public 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"; |
| 42 | |
| 43 | private final LayoutInflater mLayoutInflater; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 44 | private ViewGroup mTabs; |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 45 | private ViewGroup mTabsContainer; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 46 | private AppsCustomizePagedView mAppsCustomizePane; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 47 | |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 48 | private boolean mInTransition; |
| 49 | private boolean mResetAfterTransition; |
| 50 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 51 | public AppsCustomizeTabHost(Context context, AttributeSet attrs) { |
| 52 | super(context, attrs); |
| 53 | mLayoutInflater = LayoutInflater.from(context); |
| 54 | } |
| 55 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 56 | /** |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 57 | * Convenience methods to select specific tabs. We want to set the content type immediately |
| 58 | * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view |
| 59 | * reflects the new content (but doesn't do the animation and logic associated with changing |
| 60 | * tabs manually). |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 61 | */ |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 62 | private void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) { |
| 63 | onTabChangedStart(); |
| 64 | onTabChangedEnd(type); |
| 65 | } |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 66 | void selectAppsTab() { |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 67 | setContentTypeImmediate(AppsCustomizePagedView.ContentType.Applications); |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 68 | setCurrentTabByTag(APPS_TAB_TAG); |
| 69 | } |
| 70 | void selectWidgetsTab() { |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 71 | setContentTypeImmediate(AppsCustomizePagedView.ContentType.Widgets); |
| 72 | mAppsCustomizePane.setCurrentPageToWidgets(); |
| 73 | |
| 74 | setCurrentTabByTag(WIDGETS_TAB_TAG); |
| 75 | } |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 76 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Setup the tab host and create all necessary tabs. |
| 79 | */ |
| 80 | @Override |
| 81 | protected void onFinishInflate() { |
| 82 | // Setup the tab host |
| 83 | setup(); |
| 84 | |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 85 | final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container); |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 86 | final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs); |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 87 | final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView) |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 88 | findViewById(R.id.apps_customize_pane_content); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 89 | mTabs = tabs; |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 90 | mTabsContainer = tabsContainer; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 91 | mAppsCustomizePane = appsCustomizePane; |
| 92 | if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 93 | |
| 94 | // Configure the tabs content factory to return the same paged view (that we change the |
| 95 | // content filter on) |
| 96 | TabContentFactory contentFactory = new TabContentFactory() { |
| 97 | public View createTabContent(String tag) { |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 98 | return appsCustomizePane; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 99 | } |
| 100 | }; |
| 101 | |
| 102 | // Create the tabs |
| 103 | TextView tabView; |
| 104 | tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false); |
| 105 | tabView.setText(mContext.getString(R.string.all_apps_button_label)); |
| 106 | addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory)); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 107 | tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false); |
| 108 | tabView.setText(mContext.getString(R.string.widgets_tab_label)); |
| 109 | addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory)); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 110 | setOnTabChangedListener(this); |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 111 | |
| 112 | // Setup the key listener to jump between the last tab view and the market icon |
| 113 | AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener(); |
| 114 | View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1); |
| 115 | lastTab.setOnKeyListener(keyListener); |
| 116 | View shopButton = findViewById(R.id.market_button); |
| 117 | shopButton.setOnKeyListener(keyListener); |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 118 | |
| 119 | // Hide the tab bar until we measure |
| 120 | mTabsContainer.setAlpha(0f); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 121 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 122 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 123 | @Override |
| 124 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 125 | boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0); |
| 126 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 127 | |
| 128 | // Set the width of the tab list to the content width |
| 129 | if (remeasureTabWidth) { |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 130 | int contentWidth = mAppsCustomizePane.getPageContentWidth(); |
| 131 | if (contentWidth > 0) { |
| 132 | // Set the width and show the tab bar (if we have a loading graphic, we can switch |
| 133 | // it off here) |
| 134 | mTabs.getLayoutParams().width = contentWidth; |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 135 | mTabsContainer.setAlpha(1f); |
Michael Jurka | c57b7a8 | 2011-08-09 22:02:20 -0700 | [diff] [blame] | 136 | post(new Runnable() { |
| 137 | public void run() { |
| 138 | mTabs.requestLayout(); |
| 139 | } |
| 140 | }); |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 141 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 142 | } |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 143 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public boolean onTouchEvent(MotionEvent event) { |
| 148 | // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall |
| 149 | // through to the workspace and trigger showWorkspace() |
| 150 | if (event.getY() < mAppsCustomizePane.getBottom()) { |
| 151 | return true; |
| 152 | } |
| 153 | return super.onTouchEvent(event); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 156 | private void onTabChangedStart() { |
| 157 | mAppsCustomizePane.hideScrollingIndicator(false); |
| 158 | } |
| 159 | private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) { |
| 160 | mAppsCustomizePane.setContentType(type); |
| 161 | } |
| 162 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 163 | @Override |
| 164 | public void onTabChanged(String tabId) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 165 | final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId); |
| 166 | if (!mAppsCustomizePane.isContentType(type)) { |
| 167 | // Animate the changing of the tab content by fading pages in and out |
| 168 | final Resources res = getResources(); |
| 169 | final int duration = res.getInteger(R.integer.config_tabTransitionDuration); |
| 170 | |
| 171 | ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 0f); |
| 172 | anim.setDuration(duration); |
| 173 | anim.addListener(new AnimatorListenerAdapter() { |
| 174 | @Override |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 175 | public void onAnimationStart(android.animation.Animator animation) { |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 176 | onTabChangedStart(); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 177 | } |
| 178 | @Override |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 179 | public void onAnimationEnd(android.animation.Animator animation) { |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 180 | onTabChangedEnd(type); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 181 | |
| 182 | ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f); |
| 183 | anim.setDuration(duration); |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 184 | anim.addListener(new AnimatorListenerAdapter() { |
| 185 | @Override |
| 186 | public void onAnimationEnd(android.animation.Animator animation) { |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 187 | if (!LauncherApplication.isScreenLarge()) { |
| 188 | mAppsCustomizePane.flashScrollingIndicator(); |
| 189 | } |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 190 | } |
| 191 | }); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 192 | anim.start(); |
| 193 | } |
| 194 | }); |
| 195 | anim.start(); |
| 196 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns the content type for the specified tab tag. |
| 201 | */ |
| 202 | public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) { |
| 203 | if (tag.equals(APPS_TAB_TAG)) { |
| 204 | return AppsCustomizePagedView.ContentType.Applications; |
| 205 | } else if (tag.equals(WIDGETS_TAB_TAG)) { |
| 206 | return AppsCustomizePagedView.ContentType.Widgets; |
| 207 | } |
| 208 | return AppsCustomizePagedView.ContentType.Applications; |
| 209 | } |
| 210 | |
| 211 | /** |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 212 | * Returns the tab tag for a given content type. |
| 213 | */ |
| 214 | public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) { |
| 215 | if (type == AppsCustomizePagedView.ContentType.Applications) { |
| 216 | return APPS_TAB_TAG; |
| 217 | } else if (type == AppsCustomizePagedView.ContentType.Widgets) { |
| 218 | return WIDGETS_TAB_TAG; |
| 219 | } |
| 220 | return APPS_TAB_TAG; |
| 221 | } |
| 222 | |
| 223 | /** |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 224 | * Disable focus on anything under this view in the hierarchy if we are not visible. |
| 225 | */ |
| 226 | @Override |
| 227 | public int getDescendantFocusability() { |
| 228 | if (getVisibility() != View.VISIBLE) { |
| 229 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 230 | } |
| 231 | return super.getDescendantFocusability(); |
| 232 | } |
| 233 | |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 234 | void reset() { |
| 235 | if (mInTransition) { |
| 236 | // Defer to after the transition to reset |
| 237 | mResetAfterTransition = true; |
| 238 | } else { |
| 239 | // Reset immediately |
| 240 | mAppsCustomizePane.reset(); |
| 241 | } |
| 242 | } |
| 243 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 244 | /* LauncherTransitionable overrides */ |
| 245 | @Override |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 246 | public void onLauncherTransitionStart(Animator animation, boolean toWorkspace) { |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 247 | mInTransition = true; |
Michael Jurka | 19e0fc5 | 2011-07-22 18:00:21 -0700 | [diff] [blame] | 248 | // isHardwareAccelerated() checks if we're attached to a window and if that |
| 249 | // window is HW accelerated-- we were sometimes not attached to a window |
| 250 | // and buildLayer was throwing an IllegalStateException |
| 251 | if (animation != null && isHardwareAccelerated()) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 252 | // Turn on hardware layers for performance |
| 253 | setLayerType(LAYER_TYPE_HARDWARE, null); |
| 254 | |
| 255 | // force building the layer at the beginning of the animation, so you don't get a |
| 256 | // blip early in the animation |
| 257 | buildLayer(); |
| 258 | } |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 259 | if (!toWorkspace && !LauncherApplication.isScreenLarge()) { |
| 260 | mAppsCustomizePane.showScrollingIndicator(false); |
| 261 | } |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 262 | if (mResetAfterTransition) { |
| 263 | mAppsCustomizePane.reset(); |
| 264 | mResetAfterTransition = false; |
| 265 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 266 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 267 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 268 | @Override |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 269 | public void onLauncherTransitionEnd(Animator animation, boolean toWorkspace) { |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 270 | mInTransition = false; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 271 | if (animation != null) { |
| 272 | setLayerType(LAYER_TYPE_NONE, null); |
| 273 | } |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 274 | |
Winson Chung | 4afe9b3 | 2011-07-27 17:46:20 -0700 | [diff] [blame] | 275 | if (!toWorkspace && !LauncherApplication.isScreenLarge()) { |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 276 | mAppsCustomizePane.hideScrollingIndicator(false); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 277 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 278 | } |
| 279 | } |