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