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