blob: c261bfb9cdec9f02b5ed6310484d1d6f7196ae74 [file] [log] [blame]
Michael Jurka2763be32011-02-24 11:19:57 -08001/*
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
Michael Jurka12ac0d62011-02-23 11:48:32 -080019import com.android.launcher.R;
20import com.android.launcher2.CustomizePagedView.CustomizationType;
Michael Jurka2763be32011-02-24 11:19:57 -080021
Michael Jurka12ac0d62011-02-23 11:48:32 -080022import android.animation.Animator;
23import android.animation.AnimatorListenerAdapter;
24import android.animation.ObjectAnimator;
25import android.animation.ValueAnimator;
26import android.content.Context;
27import android.content.res.Resources;
28import android.util.AttributeSet;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.widget.TabHost;
32import android.widget.TabWidget;
33import android.widget.TextView;
34
35public 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 Jurkaabded662011-03-04 12:06:57 -080042 private boolean mFirstLayout = true;
43
Patrick Dubroy1a009332011-05-23 16:15:09 -070044 // How much of the vertical space this control should attempt to fill
45 private float mVerticalFillPercentage;
46
Michael Jurka12ac0d62011-02-23 11:48:32 -080047 private final LayoutInflater mInflater;
48 private Context mContext;
Michael Jurka2763be32011-02-24 11:19:57 -080049
Patrick Dubroy1a009332011-05-23 16:15:09 -070050 private CustomizePagedView mCustomizePagedView;
51
Michael Jurka2763be32011-02-24 11:19:57 -080052 public CustomizeTrayTabHost(Context context, AttributeSet attrs) {
53 super(context, attrs);
Michael Jurka12ac0d62011-02-23 11:48:32 -080054 mContext = context;
55 mInflater = LayoutInflater.from(context);
56 }
57
58 @Override
59 protected void onFinishInflate() {
Patrick Dubroy1a009332011-05-23 16:15:09 -070060 final Resources res = getResources();
61
Michael Jurka12ac0d62011-02-23 11:48:32 -080062 setup();
63
Patrick Dubroy1a009332011-05-23 16:15:09 -070064 mCustomizePagedView =
Michael Jurka12ac0d62011-02-23 11:48:32 -080065 (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents);
66
67 // Configure tabs
68 TabContentFactory contentFactory = new TabContentFactory() {
69 public View createTabContent(String tag) {
Patrick Dubroy1a009332011-05-23 16:15:09 -070070 return mCustomizePagedView;
Michael Jurka12ac0d62011-02-23 11:48:32 -080071 }
72 };
73
74 TextView tabView;
75 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
76
Michael Jurka661a2662011-05-25 16:26:16 -070077 tabView = (TextView) mInflater.inflate(
78 R.layout.customize_tab_widget_indicator, tabWidget, false);
Michael Jurka12ac0d62011-02-23 11:48:32 -080079 tabView.setText(mContext.getString(R.string.widgets_tab_label));
Michael Jurka661a2662011-05-25 16:26:16 -070080 addTab(newTabSpec(WIDGETS_TAG)
81 .setIndicator(tabView).setContent(contentFactory));
82 tabView = (TextView) mInflater.inflate(
83 R.layout.customize_tab_widget_indicator, tabWidget, false);
Michael Jurkaf8e0c762011-06-03 14:13:42 -070084 tabView.setText(mContext.getString(R.string.all_apps_tab_apps));
Michael Jurka12ac0d62011-02-23 11:48:32 -080085 addTab(newTabSpec(APPLICATIONS_TAG)
86 .setIndicator(tabView).setContent(contentFactory));
Michael Jurka661a2662011-05-25 16:26:16 -070087 tabView = (TextView) mInflater.inflate(
88 R.layout.customize_tab_widget_indicator, tabWidget, false);
Michael Jurka12ac0d62011-02-23 11:48:32 -080089 tabView.setText(mContext.getString(R.string.wallpapers_tab_label));
90 addTab(newTabSpec(WALLPAPERS_TAG)
91 .setIndicator(tabView).setContent(contentFactory));
Michael Jurka661a2662011-05-25 16:26:16 -070092 tabView = (TextView) mInflater.inflate(
93 R.layout.customize_tab_widget_indicator, tabWidget, false);
Michael Jurka12ac0d62011-02-23 11:48:32 -080094 tabView.setText(mContext.getString(R.string.shortcuts_tab_label));
95 addTab(newTabSpec(SHORTCUTS_TAG)
96 .setIndicator(tabView).setContent(contentFactory));
Patrick Dubroy1a009332011-05-23 16:15:09 -070097
98 mVerticalFillPercentage =
99 res.getInteger(R.integer.customization_drawer_verticalFillPercentage) / 100f;
100
Michael Jurka12ac0d62011-02-23 11:48:32 -0800101 setOnTabChangedListener(new OnTabChangeListener() {
102 public void onTabChanged(String tabId) {
103 final CustomizePagedView.CustomizationType newType =
104 getCustomizeFilterForTabTag(tabId);
Patrick Dubroy1a009332011-05-23 16:15:09 -0700105 if (newType != mCustomizePagedView.getCustomizationFilter()) {
Michael Jurka12ac0d62011-02-23 11:48:32 -0800106 // 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 Dubroy1a009332011-05-23 16:15:09 -0700109 final float alpha = mCustomizePagedView.getAlpha();
110 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Michael Jurka12ac0d62011-02-23 11:48:32 -0800111 "alpha", alpha, 0.0f);
112 alphaAnim.setDuration(duration);
113 alphaAnim.addListener(new AnimatorListenerAdapter() {
114 @Override
115 public void onAnimationEnd(Animator animation) {
Patrick Dubroy1a009332011-05-23 16:15:09 -0700116 mCustomizePagedView.setCustomizationFilter(newType);
Michael Jurka12ac0d62011-02-23 11:48:32 -0800117
Patrick Dubroy1a009332011-05-23 16:15:09 -0700118 final float alpha = mCustomizePagedView.getAlpha();
Michael Jurka12ac0d62011-02-23 11:48:32 -0800119 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Patrick Dubroy1a009332011-05-23 16:15:09 -0700120 mCustomizePagedView, "alpha", alpha, 1.0f);
Michael Jurka12ac0d62011-02-23 11:48:32 -0800121 alphaAnim.setDuration(duration);
122 alphaAnim.start();
123 }
124 });
125 alphaAnim.start();
126 }
127 }
128 });
Michael Jurka2763be32011-02-24 11:19:57 -0800129 }
130
131 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800132 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 Jurka2763be32011-02-24 11:19:57 -0800142 }
143
144 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800145 public void onLauncherTransitionEnd(Animator animation) {
146 if (animation != null) {
147 setLayerType(LAYER_TYPE_NONE, null);
148 }
149 }
150
151 @Override
Patrick Dubroy1a009332011-05-23 16:15:09 -0700152 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 Jurkaabded662011-03-04 12:06:57 -0800168 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurkaea2daff2011-05-17 18:21:03 -0700169 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 Jurkaabded662011-03-04 12:06:57 -0800186 super.onLayout(changed, l, t, r, b);
Michael Jurka2763be32011-02-24 11:19:57 -0800187 }
Michael Jurka12ac0d62011-02-23 11:48:32 -0800188
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 Jurka2763be32011-02-24 11:19:57 -0800201}