AppsCustomizeTabHost.java revision 3d845e8d8921eebe0b072b7856b660b4350921e1
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 17package com.android.launcher2; 18 19import android.animation.Animator; 20import android.animation.AnimatorListenerAdapter; 21import android.animation.AnimatorSet; 22import android.animation.ObjectAnimator; 23import android.content.Context; 24import android.content.res.Resources; 25import android.util.AttributeSet; 26import android.view.LayoutInflater; 27import android.view.MotionEvent; 28import android.view.View; 29import android.view.ViewGroup; 30import android.widget.FrameLayout; 31import android.widget.LinearLayout; 32import android.widget.TabHost; 33import android.widget.TabWidget; 34import android.widget.TextView; 35 36import com.android.launcher.R; 37 38import java.util.ArrayList; 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; 48 private ViewGroup mTabs; 49 private ViewGroup mTabsContainer; 50 private AppsCustomizePagedView mAppsCustomizePane; 51 private boolean mSuppressContentCallback = false; 52 private FrameLayout mAnimationBuffer; 53 private LinearLayout mContent; 54 55 private boolean mInTransition; 56 private boolean mResetAfterTransition; 57 private Animator mLauncherTransition; 58 59 public AppsCustomizeTabHost(Context context, AttributeSet attrs) { 60 super(context, attrs); 61 mLayoutInflater = LayoutInflater.from(context); 62 } 63 64 /** 65 * Convenience methods to select specific tabs. We want to set the content type immediately 66 * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view 67 * reflects the new content (but doesn't do the animation and logic associated with changing 68 * tabs manually). 69 */ 70 private void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) { 71 onTabChangedStart(); 72 onTabChangedEnd(type); 73 } 74 void selectAppsTab() { 75 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Applications); 76 setCurrentTabByTag(APPS_TAB_TAG); 77 } 78 void selectWidgetsTab() { 79 setContentTypeImmediate(AppsCustomizePagedView.ContentType.Widgets); 80 setCurrentTabByTag(WIDGETS_TAB_TAG); 81 } 82 83 /** 84 * Setup the tab host and create all necessary tabs. 85 */ 86 @Override 87 protected void onFinishInflate() { 88 // Setup the tab host 89 setup(); 90 91 final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container); 92 final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs); 93 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView) 94 findViewById(R.id.apps_customize_pane_content); 95 mTabs = tabs; 96 mTabsContainer = tabsContainer; 97 mAppsCustomizePane = appsCustomizePane; 98 mAnimationBuffer = (FrameLayout) findViewById(R.id.animation_buffer); 99 mContent = (LinearLayout) findViewById(R.id.apps_customize_content); 100 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException(); 101 102 // Configure the tabs content factory to return the same paged view (that we change the 103 // content filter on) 104 TabContentFactory contentFactory = new TabContentFactory() { 105 public View createTabContent(String tag) { 106 return appsCustomizePane; 107 } 108 }; 109 110 // Create the tabs 111 TextView tabView; 112 String label; 113 label = mContext.getString(R.string.all_apps_button_label); 114 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false); 115 tabView.setText(label); 116 tabView.setContentDescription(label); 117 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory)); 118 label = mContext.getString(R.string.widgets_tab_label); 119 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false); 120 tabView.setText(label); 121 tabView.setContentDescription(label); 122 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory)); 123 setOnTabChangedListener(this); 124 125 // Setup the key listener to jump between the last tab view and the market icon 126 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener(); 127 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1); 128 lastTab.setOnKeyListener(keyListener); 129 View shopButton = findViewById(R.id.market_button); 130 shopButton.setOnKeyListener(keyListener); 131 132 // Hide the tab bar until we measure 133 mTabsContainer.setAlpha(0f); 134 } 135 136 @Override 137 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 138 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0); 139 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 140 141 // Set the width of the tab list to the content width 142 if (remeasureTabWidth) { 143 int contentWidth = mAppsCustomizePane.getPageContentWidth(); 144 if (contentWidth > 0 && mTabs.getLayoutParams().width != contentWidth) { 145 // Set the width and show the tab bar 146 mTabs.getLayoutParams().width = contentWidth; 147 post(new Runnable() { 148 public void run() { 149 mTabs.requestLayout(); 150 mTabsContainer.setAlpha(1f); 151 } 152 }); 153 } 154 } 155 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 156 } 157 158 @Override 159 public boolean onTouchEvent(MotionEvent event) { 160 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall 161 // through to the workspace and trigger showWorkspace() 162 if (event.getY() < mAppsCustomizePane.getBottom()) { 163 return true; 164 } 165 return super.onTouchEvent(event); 166 } 167 168 private void onTabChangedStart() { 169 mAppsCustomizePane.hideScrollingIndicator(false); 170 } 171 172 private void reloadCurrentPage() { 173 if (!LauncherApplication.isScreenLarge()) { 174 mAppsCustomizePane.flashScrollingIndicator(); 175 } 176 mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage()); 177 mAppsCustomizePane.requestFocus(); 178 } 179 180 private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) { 181 mAppsCustomizePane.setContentType(type); 182 } 183 184 @Override 185 public void onTabChanged(String tabId) { 186 final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId); 187 if (mSuppressContentCallback) { 188 mSuppressContentCallback = false; 189 return; 190 } 191 192 // Animate the changing of the tab content by fading pages in and out 193 final Resources res = getResources(); 194 final int duration = res.getInteger(R.integer.config_tabTransitionDuration); 195 196 // We post a runnable here because there is a delay while the first page is loading and 197 // the feedback from having changed the tab almost feels better than having it stick 198 post(new Runnable() { 199 @Override 200 public void run() { 201 if (mAppsCustomizePane.getMeasuredWidth() <= 0 || 202 mAppsCustomizePane.getMeasuredHeight() <= 0) { 203 reloadCurrentPage(); 204 return; 205 } 206 207 // Take the visible pages and re-parent them temporarily to mAnimatorBuffer 208 // and then cross fade to the new pages 209 int[] visiblePageRange = new int[2]; 210 mAppsCustomizePane.getVisiblePages(visiblePageRange); 211 if (visiblePageRange[0] == -1 && visiblePageRange[1] == -1) { 212 // If we can't get the visible page ranges, then just skip the animation 213 reloadCurrentPage(); 214 return; 215 } 216 ArrayList<View> visiblePages = new ArrayList<View>(); 217 for (int i = visiblePageRange[0]; i <= visiblePageRange[1]; i++) { 218 visiblePages.add(mAppsCustomizePane.getPageAt(i)); 219 } 220 221 // We want the pages to be rendered in exactly the same way as they were when 222 // their parent was mAppsCustomizePane -- so set the scroll on mAnimationBuffer 223 // to be exactly the same as mAppsCustomizePane, and below, set the left/top 224 // parameters to be correct for each of the pages 225 mAnimationBuffer.scrollTo(mAppsCustomizePane.getScrollX(), 0); 226 227 // mAppsCustomizePane renders its children in reverse order, so 228 // add the pages to mAnimationBuffer in reverse order to match that behavior 229 for (int i = visiblePages.size() - 1; i >= 0; i--) { 230 View child = visiblePages.get(i); 231 if (child instanceof PagedViewCellLayout) { 232 ((PagedViewCellLayout) child).resetChildrenOnKeyListeners(); 233 } else if (child instanceof PagedViewGridLayout) { 234 ((PagedViewGridLayout) child).resetChildrenOnKeyListeners(); 235 } 236 PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(false); 237 mAppsCustomizePane.removeView(child); 238 PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(true); 239 mAnimationBuffer.setAlpha(1f); 240 mAnimationBuffer.setVisibility(View.VISIBLE); 241 LayoutParams p = new FrameLayout.LayoutParams(child.getWidth(), 242 child.getHeight()); 243 p.setMargins((int) child.getLeft(), (int) child.getTop(), 0, 0); 244 mAnimationBuffer.addView(child, p); 245 } 246 247 // Toggle the new content 248 onTabChangedStart(); 249 onTabChangedEnd(type); 250 251 // Animate the transition 252 ObjectAnimator outAnim = ObjectAnimator.ofFloat(mAnimationBuffer, "alpha", 0f); 253 outAnim.addListener(new AnimatorListenerAdapter() { 254 @Override 255 public void onAnimationEnd(Animator animation) { 256 mAnimationBuffer.setVisibility(View.GONE); 257 mAnimationBuffer.removeAllViews(); 258 } 259 @Override 260 public void onAnimationCancel(Animator animation) { 261 mAnimationBuffer.setVisibility(View.GONE); 262 mAnimationBuffer.removeAllViews(); 263 } 264 }); 265 ObjectAnimator inAnim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f); 266 inAnim.addListener(new AnimatorListenerAdapter() { 267 @Override 268 public void onAnimationEnd(Animator animation) { 269 reloadCurrentPage(); 270 } 271 }); 272 AnimatorSet animSet = new AnimatorSet(); 273 animSet.playTogether(outAnim, inAnim); 274 animSet.setDuration(duration); 275 animSet.start(); 276 } 277 }); 278 } 279 280 public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) { 281 mSuppressContentCallback = true; 282 setCurrentTabByTag(getTabTagForContentType(type)); 283 } 284 285 /** 286 * Returns the content type for the specified tab tag. 287 */ 288 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) { 289 if (tag.equals(APPS_TAB_TAG)) { 290 return AppsCustomizePagedView.ContentType.Applications; 291 } else if (tag.equals(WIDGETS_TAB_TAG)) { 292 return AppsCustomizePagedView.ContentType.Widgets; 293 } 294 return AppsCustomizePagedView.ContentType.Applications; 295 } 296 297 /** 298 * Returns the tab tag for a given content type. 299 */ 300 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) { 301 if (type == AppsCustomizePagedView.ContentType.Applications) { 302 return APPS_TAB_TAG; 303 } else if (type == AppsCustomizePagedView.ContentType.Widgets) { 304 return WIDGETS_TAB_TAG; 305 } 306 return APPS_TAB_TAG; 307 } 308 309 /** 310 * Disable focus on anything under this view in the hierarchy if we are not visible. 311 */ 312 @Override 313 public int getDescendantFocusability() { 314 if (getVisibility() != View.VISIBLE) { 315 return ViewGroup.FOCUS_BLOCK_DESCENDANTS; 316 } 317 return super.getDescendantFocusability(); 318 } 319 320 void reset() { 321 if (mInTransition) { 322 // Defer to after the transition to reset 323 mResetAfterTransition = true; 324 } else { 325 // Reset immediately 326 mAppsCustomizePane.reset(); 327 } 328 } 329 330 private void enableAndBuildHardwareLayer() { 331 // isHardwareAccelerated() checks if we're attached to a window and if that 332 // window is HW accelerated-- we were sometimes not attached to a window 333 // and buildLayer was throwing an IllegalStateException 334 if (isHardwareAccelerated()) { 335 // Turn on hardware layers for performance 336 setLayerType(LAYER_TYPE_HARDWARE, null); 337 338 // force building the layer, so you don't get a blip early in an animation 339 // when the layer is created layer 340 buildLayer(); 341 } 342 } 343 344 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 345 super.onLayout(changed, left, top, right, bottom); 346 if (mLauncherTransition != null) { 347 enableAndBuildHardwareLayer(); 348 mLauncherTransition.start(); 349 mLauncherTransition = null; 350 } 351 } 352 353 /* LauncherTransitionable overrides */ 354 @Override 355 public boolean onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace) { 356 mInTransition = true; 357 boolean delayLauncherTransitionUntilLayout = false; 358 boolean animated = (animation != null); 359 mLauncherTransition = null; 360 361 // if the content wasn't visible before, delay the launcher animation until after a call 362 // to layout -- this prevents a blip 363 if (animated && mContent.getVisibility() == GONE) { 364 mLauncherTransition = animation; 365 delayLauncherTransitionUntilLayout = true; 366 } 367 mContent.setVisibility(VISIBLE); 368 369 if (animated && !delayLauncherTransitionUntilLayout) { 370 enableAndBuildHardwareLayer(); 371 } 372 373 if (!toWorkspace && !LauncherApplication.isScreenLarge()) { 374 mAppsCustomizePane.showScrollingIndicator(false); 375 } 376 if (mResetAfterTransition) { 377 mAppsCustomizePane.reset(); 378 mResetAfterTransition = false; 379 } 380 return delayLauncherTransitionUntilLayout; 381 } 382 383 @Override 384 public void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace) { 385 mInTransition = false; 386 if (animation != null) { 387 setLayerType(LAYER_TYPE_NONE, null); 388 } 389 390 if (!toWorkspace) { 391 // Dismiss the cling if necessary 392 l.dismissWorkspaceCling(null); 393 394 if (!LauncherApplication.isScreenLarge()) { 395 mAppsCustomizePane.hideScrollingIndicator(false); 396 } 397 } 398 } 399 400 boolean isTransitioning() { 401 return mInTransition; 402 } 403} 404