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