Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [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 | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 19 | import java.util.Random; |
Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [diff] [blame] | 20 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
| 23 | import android.animation.ObjectAnimator; |
| 24 | import android.animation.ValueAnimator; |
| 25 | import android.content.Context; |
| 26 | import android.content.res.Resources; |
| 27 | import android.util.AttributeSet; |
| 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 30 | import android.view.ViewGroup; |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 31 | import android.widget.TabHost; |
| 32 | import android.widget.TabWidget; |
| 33 | import android.widget.TextView; |
| 34 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 35 | import com.android.launcher.R; |
| 36 | import com.android.launcher2.CustomizePagedView.CustomizationType; |
| 37 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 38 | public class CustomizeTrayTabHost extends TabHost implements LauncherTransitionable { |
| 39 | // tags for the customization tabs |
| 40 | private static final String WIDGETS_TAG = "widgets"; |
| 41 | private static final String APPLICATIONS_TAG = "applications"; |
| 42 | private static final String SHORTCUTS_TAG = "shortcuts"; |
| 43 | private static final String WALLPAPERS_TAG = "wallpapers"; |
| 44 | |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 45 | private boolean mFirstLayout = true; |
| 46 | |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 47 | // How much of the vertical space this control should attempt to fill |
| 48 | private float mVerticalFillPercentage; |
| 49 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 50 | private final LayoutInflater mInflater; |
| 51 | private Context mContext; |
Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [diff] [blame] | 52 | |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 53 | private CustomizePagedView mCustomizePagedView; |
| 54 | |
Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [diff] [blame] | 55 | public CustomizeTrayTabHost(Context context, AttributeSet attrs) { |
| 56 | super(context, attrs); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 57 | mContext = context; |
| 58 | mInflater = LayoutInflater.from(context); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | protected void onFinishInflate() { |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 63 | final Resources res = getResources(); |
| 64 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 65 | setup(); |
| 66 | |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 67 | mCustomizePagedView = |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 68 | (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents); |
| 69 | |
| 70 | // Configure tabs |
| 71 | TabContentFactory contentFactory = new TabContentFactory() { |
| 72 | public View createTabContent(String tag) { |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 73 | return mCustomizePagedView; |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
| 77 | TextView tabView; |
| 78 | TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs); |
| 79 | |
Michael Jurka | 20bb6c8 | 2011-05-25 16:26:16 -0700 | [diff] [blame] | 80 | tabView = (TextView) mInflater.inflate( |
| 81 | R.layout.customize_tab_widget_indicator, tabWidget, false); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 82 | tabView.setText(mContext.getString(R.string.widgets_tab_label)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 83 | addTab(newTabSpec(WIDGETS_TAG) |
| 84 | .setIndicator(tabView).setContent(contentFactory)); |
Michael Jurka | 20bb6c8 | 2011-05-25 16:26:16 -0700 | [diff] [blame] | 85 | tabView = (TextView) mInflater.inflate( |
| 86 | R.layout.customize_tab_widget_indicator, tabWidget, false); |
Michael Jurka | f8e0c76 | 2011-06-03 14:13:42 -0700 | [diff] [blame] | 87 | tabView.setText(mContext.getString(R.string.all_apps_tab_apps)); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 88 | addTab(newTabSpec(APPLICATIONS_TAG) |
| 89 | .setIndicator(tabView).setContent(contentFactory)); |
Michael Jurka | 20bb6c8 | 2011-05-25 16:26:16 -0700 | [diff] [blame] | 90 | tabView = (TextView) mInflater.inflate( |
| 91 | R.layout.customize_tab_widget_indicator, tabWidget, false); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 92 | tabView.setText(mContext.getString(R.string.wallpapers_tab_label)); |
| 93 | addTab(newTabSpec(WALLPAPERS_TAG) |
| 94 | .setIndicator(tabView).setContent(contentFactory)); |
Michael Jurka | 20bb6c8 | 2011-05-25 16:26:16 -0700 | [diff] [blame] | 95 | tabView = (TextView) mInflater.inflate( |
| 96 | R.layout.customize_tab_widget_indicator, tabWidget, false); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 97 | tabView.setText(mContext.getString(R.string.shortcuts_tab_label)); |
| 98 | addTab(newTabSpec(SHORTCUTS_TAG) |
| 99 | .setIndicator(tabView).setContent(contentFactory)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 100 | |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 101 | mVerticalFillPercentage = |
| 102 | res.getInteger(R.integer.customization_drawer_verticalFillPercentage) / 100f; |
| 103 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 104 | setOnTabChangedListener(new OnTabChangeListener() { |
| 105 | public void onTabChanged(String tabId) { |
| 106 | final CustomizePagedView.CustomizationType newType = |
| 107 | getCustomizeFilterForTabTag(tabId); |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 108 | if (newType != mCustomizePagedView.getCustomizationFilter()) { |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 109 | // animate the changing of the tab content by fading pages in and out |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 110 | final int duration = res.getInteger(R.integer.config_tabTransitionDuration); |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 111 | final float alpha = mCustomizePagedView.getAlpha(); |
| 112 | ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView, |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 113 | "alpha", alpha, 0.0f); |
| 114 | alphaAnim.setDuration(duration); |
| 115 | alphaAnim.addListener(new AnimatorListenerAdapter() { |
| 116 | @Override |
| 117 | public void onAnimationEnd(Animator animation) { |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 118 | mCustomizePagedView.setCustomizationFilter(newType); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 119 | |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 120 | final float alpha = mCustomizePagedView.getAlpha(); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 121 | ValueAnimator alphaAnim = ObjectAnimator.ofFloat( |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 122 | mCustomizePagedView, "alpha", alpha, 1.0f); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 123 | alphaAnim.setDuration(duration); |
| 124 | alphaAnim.start(); |
| 125 | } |
| 126 | }); |
| 127 | alphaAnim.start(); |
| 128 | } |
| 129 | } |
| 130 | }); |
Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 134 | public void onLauncherTransitionStart(Animator animation) { |
| 135 | if (animation != null) { |
| 136 | setLayerType(LAYER_TYPE_HARDWARE, null); |
| 137 | // just a sanity check that we don't build a layer before a call to onLayout |
| 138 | if (!mFirstLayout) { |
| 139 | // force building the layer at the beginning of the animation, so you don't get a |
| 140 | // blip early in the animation |
| 141 | buildLayer(); |
| 142 | } |
| 143 | } |
Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 147 | public void onLauncherTransitionEnd(Animator animation) { |
| 148 | if (animation != null) { |
| 149 | setLayerType(LAYER_TYPE_NONE, null); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | @Override |
Patrick Dubroy | 4a5ad09 | 2011-05-23 16:15:09 -0700 | [diff] [blame] | 154 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 155 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 156 | |
| 157 | // If there's extra room, try to grow to fill it |
| 158 | if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) { |
| 159 | final int availableHeight = MeasureSpec.getSize(heightMeasureSpec); |
| 160 | final int finalHeight = Math.max(getMeasuredHeight(), |
| 161 | (int) (availableHeight * mVerticalFillPercentage)); |
| 162 | |
| 163 | // Measure a second time with EXACTLY so that we get sized correctly |
| 164 | heightMeasureSpec = MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY); |
| 165 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @Override |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 170 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
Michael Jurka | 4c6016f | 2011-05-17 18:21:03 -0700 | [diff] [blame] | 171 | if (mFirstLayout) { |
| 172 | mFirstLayout = false; |
| 173 | |
| 174 | final CustomizePagedView customizePagedView = |
| 175 | (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents); |
| 176 | TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs); |
| 177 | // Set the width of the tab bar properly |
| 178 | int pageWidth = customizePagedView.getPageContentWidth(); |
| 179 | TabWidget customizeTabBar = (TabWidget) findViewById(com.android.internal.R.id.tabs); |
| 180 | if (customizeTabBar == null) throw new Resources.NotFoundException(); |
| 181 | int tabWidgetPadding = 0; |
| 182 | final int childCount = tabWidget.getChildCount(); |
| 183 | if (childCount > 0) { |
| 184 | tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2; |
| 185 | } |
| 186 | customizeTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding; |
| 187 | } |
Michael Jurka | abded66 | 2011-03-04 12:06:57 -0800 | [diff] [blame] | 188 | super.onLayout(changed, l, t, r, b); |
Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [diff] [blame] | 189 | } |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 190 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 191 | @Override |
| 192 | public int getDescendantFocusability() { |
| 193 | if (getVisibility() != View.VISIBLE) { |
| 194 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 195 | } |
| 196 | return super.getDescendantFocusability(); |
| 197 | } |
| 198 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 199 | CustomizationType getCustomizeFilterForTabTag(String tag) { |
| 200 | if (tag.equals(WIDGETS_TAG)) { |
| 201 | return CustomizationType.WidgetCustomization; |
| 202 | } else if (tag.equals(APPLICATIONS_TAG)) { |
| 203 | return CustomizationType.ApplicationCustomization; |
| 204 | } else if (tag.equals(WALLPAPERS_TAG)) { |
| 205 | return CustomizePagedView.CustomizationType.WallpaperCustomization; |
| 206 | } else if (tag.equals(SHORTCUTS_TAG)) { |
| 207 | return CustomizePagedView.CustomizationType.ShortcutCustomization; |
| 208 | } |
| 209 | return CustomizationType.WidgetCustomization; |
| 210 | } |
Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [diff] [blame] | 211 | } |