blob: 5780818597278f9ab3be629832d0b1cb6400e89b [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
Michael Jurka20bb6c82011-05-25 16:26:16 -070080 tabView = (TextView) mInflater.inflate(
81 R.layout.customize_tab_widget_indicator, tabWidget, false);
Michael Jurka7ef959b2011-02-23 11:48:32 -080082 tabView.setText(mContext.getString(R.string.widgets_tab_label));
Winson Chung97d85d22011-04-13 11:27:36 -070083 addTab(newTabSpec(WIDGETS_TAG)
84 .setIndicator(tabView).setContent(contentFactory));
Michael Jurka20bb6c82011-05-25 16:26:16 -070085 tabView = (TextView) mInflater.inflate(
86 R.layout.customize_tab_widget_indicator, tabWidget, false);
Michael Jurkaf8e0c762011-06-03 14:13:42 -070087 tabView.setText(mContext.getString(R.string.all_apps_tab_apps));
Michael Jurka7ef959b2011-02-23 11:48:32 -080088 addTab(newTabSpec(APPLICATIONS_TAG)
89 .setIndicator(tabView).setContent(contentFactory));
Michael Jurka20bb6c82011-05-25 16:26:16 -070090 tabView = (TextView) mInflater.inflate(
91 R.layout.customize_tab_widget_indicator, tabWidget, false);
Michael Jurka7ef959b2011-02-23 11:48:32 -080092 tabView.setText(mContext.getString(R.string.wallpapers_tab_label));
93 addTab(newTabSpec(WALLPAPERS_TAG)
94 .setIndicator(tabView).setContent(contentFactory));
Michael Jurka20bb6c82011-05-25 16:26:16 -070095 tabView = (TextView) mInflater.inflate(
96 R.layout.customize_tab_widget_indicator, tabWidget, false);
Michael Jurka7ef959b2011-02-23 11:48:32 -080097 tabView.setText(mContext.getString(R.string.shortcuts_tab_label));
98 addTab(newTabSpec(SHORTCUTS_TAG)
99 .setIndicator(tabView).setContent(contentFactory));
Winson Chung97d85d22011-04-13 11:27:36 -0700100
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700101 mVerticalFillPercentage =
102 res.getInteger(R.integer.customization_drawer_verticalFillPercentage) / 100f;
103
Michael Jurka7ef959b2011-02-23 11:48:32 -0800104 setOnTabChangedListener(new OnTabChangeListener() {
105 public void onTabChanged(String tabId) {
106 final CustomizePagedView.CustomizationType newType =
107 getCustomizeFilterForTabTag(tabId);
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700108 if (newType != mCustomizePagedView.getCustomizationFilter()) {
Michael Jurka7ef959b2011-02-23 11:48:32 -0800109 // animate the changing of the tab content by fading pages in and out
Winson Chung785d2eb2011-04-14 16:08:02 -0700110 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700111 final float alpha = mCustomizePagedView.getAlpha();
112 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Michael Jurka7ef959b2011-02-23 11:48:32 -0800113 "alpha", alpha, 0.0f);
114 alphaAnim.setDuration(duration);
115 alphaAnim.addListener(new AnimatorListenerAdapter() {
116 @Override
117 public void onAnimationEnd(Animator animation) {
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700118 mCustomizePagedView.setCustomizationFilter(newType);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800119
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700120 final float alpha = mCustomizePagedView.getAlpha();
Michael Jurka7ef959b2011-02-23 11:48:32 -0800121 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700122 mCustomizePagedView, "alpha", alpha, 1.0f);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800123 alphaAnim.setDuration(duration);
124 alphaAnim.start();
125 }
126 });
127 alphaAnim.start();
128 }
129 }
130 });
Michael Jurka2763be32011-02-24 11:19:57 -0800131 }
132
133 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800134 public void onLauncherTransitionStart(Animator animation) {
135 if (animation != null) {
136 setLayerType(LAYER_TYPE_HARDWARE, null);
137 // just a sanity check that we don't build a layer before a call to onLayout
138 if (!mFirstLayout) {
139 // force building the layer at the beginning of the animation, so you don't get a
140 // blip early in the animation
141 buildLayer();
142 }
143 }
Michael Jurka2763be32011-02-24 11:19:57 -0800144 }
145
146 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800147 public void onLauncherTransitionEnd(Animator animation) {
148 if (animation != null) {
149 setLayerType(LAYER_TYPE_NONE, null);
150 }
151 }
152
153 @Override
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700154 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
155 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
156
157 // If there's extra room, try to grow to fill it
158 if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
159 final int availableHeight = MeasureSpec.getSize(heightMeasureSpec);
160 final int finalHeight = Math.max(getMeasuredHeight(),
161 (int) (availableHeight * mVerticalFillPercentage));
162
163 // Measure a second time with EXACTLY so that we get sized correctly
164 heightMeasureSpec = MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY);
165 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
166 }
167 }
168
169 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800170 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka4c6016f2011-05-17 18:21:03 -0700171 if (mFirstLayout) {
172 mFirstLayout = false;
173
174 final CustomizePagedView customizePagedView =
175 (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents);
176 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
177 // Set the width of the tab bar properly
178 int pageWidth = customizePagedView.getPageContentWidth();
179 TabWidget customizeTabBar = (TabWidget) findViewById(com.android.internal.R.id.tabs);
180 if (customizeTabBar == null) throw new Resources.NotFoundException();
181 int tabWidgetPadding = 0;
182 final int childCount = tabWidget.getChildCount();
183 if (childCount > 0) {
184 tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
185 }
186 customizeTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
187 }
Michael Jurkaabded662011-03-04 12:06:57 -0800188 super.onLayout(changed, l, t, r, b);
Michael Jurka2763be32011-02-24 11:19:57 -0800189 }
Michael Jurka7ef959b2011-02-23 11:48:32 -0800190
Winson Chung97d85d22011-04-13 11:27:36 -0700191 @Override
192 public int getDescendantFocusability() {
193 if (getVisibility() != View.VISIBLE) {
194 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
195 }
196 return super.getDescendantFocusability();
197 }
198
Michael Jurka7ef959b2011-02-23 11:48:32 -0800199 CustomizationType getCustomizeFilterForTabTag(String tag) {
200 if (tag.equals(WIDGETS_TAG)) {
201 return CustomizationType.WidgetCustomization;
202 } else if (tag.equals(APPLICATIONS_TAG)) {
203 return CustomizationType.ApplicationCustomization;
204 } else if (tag.equals(WALLPAPERS_TAG)) {
205 return CustomizePagedView.CustomizationType.WallpaperCustomization;
206 } else if (tag.equals(SHORTCUTS_TAG)) {
207 return CustomizePagedView.CustomizationType.ShortcutCustomization;
208 }
209 return CustomizationType.WidgetCustomization;
210 }
Michael Jurka2763be32011-02-24 11:19:57 -0800211}