blob: 6306bdae414f690d24282558d4dc427dd3f27826 [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
Winson Chung97d85d22011-04-13 11:27:36 -070019import java.util.Random;
Michael Jurka2763be32011-02-24 11:19:57 -080020
Michael Jurka7ef959b2011-02-23 11:48:32 -080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
23import android.animation.ObjectAnimator;
24import android.animation.ValueAnimator;
25import android.content.Context;
26import android.content.res.Resources;
27import android.util.AttributeSet;
28import android.view.LayoutInflater;
29import android.view.View;
Winson Chung97d85d22011-04-13 11:27:36 -070030import android.view.ViewGroup;
Michael Jurka7ef959b2011-02-23 11:48:32 -080031import android.widget.TabHost;
32import android.widget.TabWidget;
33import android.widget.TextView;
34
Winson Chung97d85d22011-04-13 11:27:36 -070035import com.android.launcher.R;
36import com.android.launcher2.CustomizePagedView.CustomizationType;
37
Michael Jurka7ef959b2011-02-23 11:48:32 -080038public 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 Jurkaabded662011-03-04 12:06:57 -080045 private boolean mFirstLayout = true;
46
Patrick Dubroy4a5ad092011-05-23 16:15:09 -070047 // How much of the vertical space this control should attempt to fill
48 private float mVerticalFillPercentage;
49
Michael Jurka7ef959b2011-02-23 11:48:32 -080050 private final LayoutInflater mInflater;
51 private Context mContext;
Michael Jurka2763be32011-02-24 11:19:57 -080052
Patrick Dubroy4a5ad092011-05-23 16:15:09 -070053 private CustomizePagedView mCustomizePagedView;
54
Michael Jurka2763be32011-02-24 11:19:57 -080055 public CustomizeTrayTabHost(Context context, AttributeSet attrs) {
56 super(context, attrs);
Michael Jurka7ef959b2011-02-23 11:48:32 -080057 mContext = context;
58 mInflater = LayoutInflater.from(context);
59 }
60
61 @Override
62 protected void onFinishInflate() {
Patrick Dubroy4a5ad092011-05-23 16:15:09 -070063 final Resources res = getResources();
64
Michael Jurka7ef959b2011-02-23 11:48:32 -080065 setup();
66
Patrick Dubroy4a5ad092011-05-23 16:15:09 -070067 mCustomizePagedView =
Michael Jurka7ef959b2011-02-23 11:48:32 -080068 (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents);
69
70 // Configure tabs
71 TabContentFactory contentFactory = new TabContentFactory() {
72 public View createTabContent(String tag) {
Patrick Dubroy4a5ad092011-05-23 16:15:09 -070073 return mCustomizePagedView;
Michael Jurka7ef959b2011-02-23 11:48:32 -080074 }
75 };
76
77 TextView tabView;
78 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
79
80 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
81 tabView.setText(mContext.getString(R.string.widgets_tab_label));
Winson Chung97d85d22011-04-13 11:27:36 -070082 addTab(newTabSpec(WIDGETS_TAG)
83 .setIndicator(tabView).setContent(contentFactory));
Michael Jurka7ef959b2011-02-23 11:48:32 -080084 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
85 tabView.setText(mContext.getString(R.string.applications_tab_label));
86 addTab(newTabSpec(APPLICATIONS_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.wallpapers_tab_label));
90 addTab(newTabSpec(WALLPAPERS_TAG)
91 .setIndicator(tabView).setContent(contentFactory));
92 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
93 tabView.setText(mContext.getString(R.string.shortcuts_tab_label));
94 addTab(newTabSpec(SHORTCUTS_TAG)
95 .setIndicator(tabView).setContent(contentFactory));
Winson Chung97d85d22011-04-13 11:27:36 -070096
Patrick Dubroy4a5ad092011-05-23 16:15:09 -070097 mVerticalFillPercentage =
98 res.getInteger(R.integer.customization_drawer_verticalFillPercentage) / 100f;
99
Michael Jurka7ef959b2011-02-23 11:48:32 -0800100 setOnTabChangedListener(new OnTabChangeListener() {
101 public void onTabChanged(String tabId) {
102 final CustomizePagedView.CustomizationType newType =
103 getCustomizeFilterForTabTag(tabId);
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700104 if (newType != mCustomizePagedView.getCustomizationFilter()) {
Michael Jurka7ef959b2011-02-23 11:48:32 -0800105 // animate the changing of the tab content by fading pages in and out
Winson Chung785d2eb2011-04-14 16:08:02 -0700106 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700107 final float alpha = mCustomizePagedView.getAlpha();
108 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Michael Jurka7ef959b2011-02-23 11:48:32 -0800109 "alpha", alpha, 0.0f);
110 alphaAnim.setDuration(duration);
111 alphaAnim.addListener(new AnimatorListenerAdapter() {
112 @Override
113 public void onAnimationEnd(Animator animation) {
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700114 mCustomizePagedView.setCustomizationFilter(newType);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800115
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700116 final float alpha = mCustomizePagedView.getAlpha();
Michael Jurka7ef959b2011-02-23 11:48:32 -0800117 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700118 mCustomizePagedView, "alpha", alpha, 1.0f);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800119 alphaAnim.setDuration(duration);
120 alphaAnim.start();
121 }
122 });
123 alphaAnim.start();
124 }
125 }
126 });
Michael Jurka2763be32011-02-24 11:19:57 -0800127 }
128
129 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800130 public void onLauncherTransitionStart(Animator animation) {
131 if (animation != null) {
132 setLayerType(LAYER_TYPE_HARDWARE, null);
133 // just a sanity check that we don't build a layer before a call to onLayout
134 if (!mFirstLayout) {
135 // force building the layer at the beginning of the animation, so you don't get a
136 // blip early in the animation
137 buildLayer();
138 }
139 }
Michael Jurka2763be32011-02-24 11:19:57 -0800140 }
141
142 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800143 public void onLauncherTransitionEnd(Animator animation) {
144 if (animation != null) {
145 setLayerType(LAYER_TYPE_NONE, null);
146 }
147 }
148
149 @Override
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700150 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
151 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
152
153 // If there's extra room, try to grow to fill it
154 if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
155 final int availableHeight = MeasureSpec.getSize(heightMeasureSpec);
156 final int finalHeight = Math.max(getMeasuredHeight(),
157 (int) (availableHeight * mVerticalFillPercentage));
158
159 // Measure a second time with EXACTLY so that we get sized correctly
160 heightMeasureSpec = MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY);
161 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
162 }
163 }
164
165 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800166 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka4c6016f2011-05-17 18:21:03 -0700167 if (mFirstLayout) {
168 mFirstLayout = false;
169
170 final CustomizePagedView customizePagedView =
171 (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents);
172 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
173 // Set the width of the tab bar properly
174 int pageWidth = customizePagedView.getPageContentWidth();
175 TabWidget customizeTabBar = (TabWidget) findViewById(com.android.internal.R.id.tabs);
176 if (customizeTabBar == null) throw new Resources.NotFoundException();
177 int tabWidgetPadding = 0;
178 final int childCount = tabWidget.getChildCount();
179 if (childCount > 0) {
180 tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
181 }
182 customizeTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
183 }
Michael Jurkaabded662011-03-04 12:06:57 -0800184 super.onLayout(changed, l, t, r, b);
Michael Jurka2763be32011-02-24 11:19:57 -0800185 }
Michael Jurka7ef959b2011-02-23 11:48:32 -0800186
Winson Chung97d85d22011-04-13 11:27:36 -0700187 @Override
188 public int getDescendantFocusability() {
189 if (getVisibility() != View.VISIBLE) {
190 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
191 }
192 return super.getDescendantFocusability();
193 }
194
Michael Jurka7ef959b2011-02-23 11:48:32 -0800195 CustomizationType getCustomizeFilterForTabTag(String tag) {
196 if (tag.equals(WIDGETS_TAG)) {
197 return CustomizationType.WidgetCustomization;
198 } else if (tag.equals(APPLICATIONS_TAG)) {
199 return CustomizationType.ApplicationCustomization;
200 } else if (tag.equals(WALLPAPERS_TAG)) {
201 return CustomizePagedView.CustomizationType.WallpaperCustomization;
202 } else if (tag.equals(SHORTCUTS_TAG)) {
203 return CustomizePagedView.CustomizationType.ShortcutCustomization;
204 }
205 return CustomizationType.WidgetCustomization;
206 }
Michael Jurka2763be32011-02-24 11:19:57 -0800207}