blob: b6c55f8b762a77fc7a445cbdcbd117e6fc5b57c9 [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
77 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
78 tabView.setText(mContext.getString(R.string.widgets_tab_label));
79 addTab(newTabSpec(WIDGETS_TAG).setIndicator(tabView).setContent(contentFactory));
80 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
81 tabView.setText(mContext.getString(R.string.applications_tab_label));
82 addTab(newTabSpec(APPLICATIONS_TAG)
83 .setIndicator(tabView).setContent(contentFactory));
84 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
85 tabView.setText(mContext.getString(R.string.wallpapers_tab_label));
86 addTab(newTabSpec(WALLPAPERS_TAG)
87 .setIndicator(tabView).setContent(contentFactory));
88 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
89 tabView.setText(mContext.getString(R.string.shortcuts_tab_label));
90 addTab(newTabSpec(SHORTCUTS_TAG)
91 .setIndicator(tabView).setContent(contentFactory));
Patrick Dubroy1a009332011-05-23 16:15:09 -070092
93 mVerticalFillPercentage =
94 res.getInteger(R.integer.customization_drawer_verticalFillPercentage) / 100f;
95
Michael Jurka12ac0d62011-02-23 11:48:32 -080096 setOnTabChangedListener(new OnTabChangeListener() {
97 public void onTabChanged(String tabId) {
98 final CustomizePagedView.CustomizationType newType =
99 getCustomizeFilterForTabTag(tabId);
Patrick Dubroy1a009332011-05-23 16:15:09 -0700100 if (newType != mCustomizePagedView.getCustomizationFilter()) {
Michael Jurka12ac0d62011-02-23 11:48:32 -0800101 // animate the changing of the tab content by fading pages in and out
102 final Resources res = getResources();
103 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
Patrick Dubroy1a009332011-05-23 16:15:09 -0700104 final float alpha = mCustomizePagedView.getAlpha();
105 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Michael Jurka12ac0d62011-02-23 11:48:32 -0800106 "alpha", alpha, 0.0f);
107 alphaAnim.setDuration(duration);
108 alphaAnim.addListener(new AnimatorListenerAdapter() {
109 @Override
110 public void onAnimationEnd(Animator animation) {
Patrick Dubroy1a009332011-05-23 16:15:09 -0700111 mCustomizePagedView.setCustomizationFilter(newType);
Michael Jurka12ac0d62011-02-23 11:48:32 -0800112
Patrick Dubroy1a009332011-05-23 16:15:09 -0700113 final float alpha = mCustomizePagedView.getAlpha();
Michael Jurka12ac0d62011-02-23 11:48:32 -0800114 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Patrick Dubroy1a009332011-05-23 16:15:09 -0700115 mCustomizePagedView, "alpha", alpha, 1.0f);
Michael Jurka12ac0d62011-02-23 11:48:32 -0800116 alphaAnim.setDuration(duration);
117 alphaAnim.start();
118 }
119 });
120 alphaAnim.start();
121 }
122 }
123 });
Michael Jurka2763be32011-02-24 11:19:57 -0800124 }
125
126 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800127 public void onLauncherTransitionStart(Animator animation) {
128 if (animation != null) {
129 setLayerType(LAYER_TYPE_HARDWARE, null);
130 // just a sanity check that we don't build a layer before a call to onLayout
131 if (!mFirstLayout) {
132 // force building the layer at the beginning of the animation, so you don't get a
133 // blip early in the animation
134 buildLayer();
135 }
136 }
Michael Jurka2763be32011-02-24 11:19:57 -0800137 }
138
139 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800140 public void onLauncherTransitionEnd(Animator animation) {
141 if (animation != null) {
142 setLayerType(LAYER_TYPE_NONE, null);
143 }
144 }
145
146 @Override
Patrick Dubroy1a009332011-05-23 16:15:09 -0700147 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
148 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
149
150 // If there's extra room, try to grow to fill it
151 if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
152 final int availableHeight = MeasureSpec.getSize(heightMeasureSpec);
153 final int finalHeight = Math.max(getMeasuredHeight(),
154 (int) (availableHeight * mVerticalFillPercentage));
155
156 // Measure a second time with EXACTLY so that we get sized correctly
157 heightMeasureSpec = MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY);
158 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
159 }
160 }
161
162 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800163 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurkaea2daff2011-05-17 18:21:03 -0700164 if (mFirstLayout) {
165 mFirstLayout = false;
166
167 final CustomizePagedView customizePagedView =
168 (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents);
169 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
170 // Set the width of the tab bar properly
171 int pageWidth = customizePagedView.getPageContentWidth();
172 TabWidget customizeTabBar = (TabWidget) findViewById(com.android.internal.R.id.tabs);
173 if (customizeTabBar == null) throw new Resources.NotFoundException();
174 int tabWidgetPadding = 0;
175 final int childCount = tabWidget.getChildCount();
176 if (childCount > 0) {
177 tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
178 }
179 customizeTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
180 }
Michael Jurkaabded662011-03-04 12:06:57 -0800181 super.onLayout(changed, l, t, r, b);
Michael Jurka2763be32011-02-24 11:19:57 -0800182 }
Michael Jurka12ac0d62011-02-23 11:48:32 -0800183
184 CustomizationType getCustomizeFilterForTabTag(String tag) {
185 if (tag.equals(WIDGETS_TAG)) {
186 return CustomizationType.WidgetCustomization;
187 } else if (tag.equals(APPLICATIONS_TAG)) {
188 return CustomizationType.ApplicationCustomization;
189 } else if (tag.equals(WALLPAPERS_TAG)) {
190 return CustomizePagedView.CustomizationType.WallpaperCustomization;
191 } else if (tag.equals(SHORTCUTS_TAG)) {
192 return CustomizePagedView.CustomizationType.ShortcutCustomization;
193 }
194 return CustomizationType.WidgetCustomization;
195 }
Michael Jurka2763be32011-02-24 11:19:57 -0800196}