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