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 | |
Michael Jurka | 7ef959b | 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 | |
| 50 | public CustomizeTrayTabHost(Context context, AttributeSet attrs) { |
| 51 | super(context, attrs); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 52 | mContext = context; |
| 53 | mInflater = LayoutInflater.from(context); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected void onFinishInflate() { |
| 58 | setup(); |
| 59 | |
| 60 | final CustomizePagedView customizePagedView = |
| 61 | (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents); |
| 62 | |
| 63 | // Configure tabs |
| 64 | TabContentFactory contentFactory = new TabContentFactory() { |
| 65 | public View createTabContent(String tag) { |
| 66 | return customizePagedView; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | TextView tabView; |
| 71 | TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs); |
| 72 | |
| 73 | tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false); |
| 74 | tabView.setText(mContext.getString(R.string.widgets_tab_label)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 75 | addTab(newTabSpec(WIDGETS_TAG) |
| 76 | .setIndicator(tabView).setContent(contentFactory)); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 77 | tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false); |
| 78 | tabView.setText(mContext.getString(R.string.applications_tab_label)); |
| 79 | addTab(newTabSpec(APPLICATIONS_TAG) |
| 80 | .setIndicator(tabView).setContent(contentFactory)); |
| 81 | tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false); |
| 82 | tabView.setText(mContext.getString(R.string.wallpapers_tab_label)); |
| 83 | addTab(newTabSpec(WALLPAPERS_TAG) |
| 84 | .setIndicator(tabView).setContent(contentFactory)); |
| 85 | tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false); |
| 86 | tabView.setText(mContext.getString(R.string.shortcuts_tab_label)); |
| 87 | addTab(newTabSpec(SHORTCUTS_TAG) |
| 88 | .setIndicator(tabView).setContent(contentFactory)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 89 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 90 | setOnTabChangedListener(new OnTabChangeListener() { |
| 91 | public void onTabChanged(String tabId) { |
| 92 | final CustomizePagedView.CustomizationType newType = |
| 93 | getCustomizeFilterForTabTag(tabId); |
| 94 | if (newType != customizePagedView.getCustomizationFilter()) { |
| 95 | // animate the changing of the tab content by fading pages in and out |
| 96 | final Resources res = getResources(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 97 | final int duration = res.getInteger(R.integer.config_tabTransitionDuration); |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 98 | final float alpha = customizePagedView.getAlpha(); |
| 99 | ValueAnimator alphaAnim = ObjectAnimator.ofFloat(customizePagedView, |
| 100 | "alpha", alpha, 0.0f); |
| 101 | alphaAnim.setDuration(duration); |
| 102 | alphaAnim.addListener(new AnimatorListenerAdapter() { |
| 103 | @Override |
| 104 | public void onAnimationEnd(Animator animation) { |
| 105 | customizePagedView.setCustomizationFilter(newType); |
| 106 | |
| 107 | final float alpha = customizePagedView.getAlpha(); |
| 108 | ValueAnimator alphaAnim = ObjectAnimator.ofFloat( |
| 109 | customizePagedView, "alpha", alpha, 1.0f); |
| 110 | alphaAnim.setDuration(duration); |
| 111 | alphaAnim.start(); |
| 112 | } |
| 113 | }); |
| 114 | alphaAnim.start(); |
| 115 | } |
| 116 | } |
| 117 | }); |
| 118 | |
| 119 | // Set the width of the tab bar properly |
| 120 | int pageWidth = customizePagedView.getPageContentWidth(); |
| 121 | TabWidget customizeTabBar = (TabWidget) findViewById(com.android.internal.R.id.tabs); |
| 122 | if (customizeTabBar == null) throw new Resources.NotFoundException(); |
| 123 | int tabWidgetPadding = 0; |
| 124 | final int childCount = tabWidget.getChildCount(); |
| 125 | if (childCount > 0) { |
| 126 | tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2; |
| 127 | } |
| 128 | customizeTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding; |
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 |
| 152 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 153 | mFirstLayout = false; |
| 154 | super.onLayout(changed, l, t, r, b); |
Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [diff] [blame] | 155 | } |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 156 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 157 | @Override |
| 158 | public int getDescendantFocusability() { |
| 159 | if (getVisibility() != View.VISIBLE) { |
| 160 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 161 | } |
| 162 | return super.getDescendantFocusability(); |
| 163 | } |
| 164 | |
Michael Jurka | 7ef959b | 2011-02-23 11:48:32 -0800 | [diff] [blame] | 165 | CustomizationType getCustomizeFilterForTabTag(String tag) { |
| 166 | if (tag.equals(WIDGETS_TAG)) { |
| 167 | return CustomizationType.WidgetCustomization; |
| 168 | } else if (tag.equals(APPLICATIONS_TAG)) { |
| 169 | return CustomizationType.ApplicationCustomization; |
| 170 | } else if (tag.equals(WALLPAPERS_TAG)) { |
| 171 | return CustomizePagedView.CustomizationType.WallpaperCustomization; |
| 172 | } else if (tag.equals(SHORTCUTS_TAG)) { |
| 173 | return CustomizePagedView.CustomizationType.ShortcutCustomization; |
| 174 | } |
| 175 | return CustomizationType.WidgetCustomization; |
| 176 | } |
Michael Jurka | 2763be3 | 2011-02-24 11:19:57 -0800 | [diff] [blame] | 177 | } |