blob: 765c41dd2d9f64ff53f847943c1cfb65aa7c984f [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
Michael Jurka12ac0d62011-02-23 11:48:32 -080044 private final LayoutInflater mInflater;
45 private Context mContext;
Michael Jurka2763be32011-02-24 11:19:57 -080046
47 public CustomizeTrayTabHost(Context context, AttributeSet attrs) {
48 super(context, attrs);
Michael Jurka12ac0d62011-02-23 11:48:32 -080049 mContext = context;
50 mInflater = LayoutInflater.from(context);
51 }
52
53 @Override
54 protected void onFinishInflate() {
55 setup();
56
57 final CustomizePagedView customizePagedView =
58 (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents);
59
60 // Configure tabs
61 TabContentFactory contentFactory = new TabContentFactory() {
62 public View createTabContent(String tag) {
63 return customizePagedView;
64 }
65 };
66
67 TextView tabView;
68 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
69
70 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
71 tabView.setText(mContext.getString(R.string.widgets_tab_label));
72 addTab(newTabSpec(WIDGETS_TAG).setIndicator(tabView).setContent(contentFactory));
73 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
74 tabView.setText(mContext.getString(R.string.applications_tab_label));
75 addTab(newTabSpec(APPLICATIONS_TAG)
76 .setIndicator(tabView).setContent(contentFactory));
77 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
78 tabView.setText(mContext.getString(R.string.wallpapers_tab_label));
79 addTab(newTabSpec(WALLPAPERS_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.shortcuts_tab_label));
83 addTab(newTabSpec(SHORTCUTS_TAG)
84 .setIndicator(tabView).setContent(contentFactory));
85 setOnTabChangedListener(new OnTabChangeListener() {
86 public void onTabChanged(String tabId) {
87 final CustomizePagedView.CustomizationType newType =
88 getCustomizeFilterForTabTag(tabId);
89 if (newType != customizePagedView.getCustomizationFilter()) {
90 // animate the changing of the tab content by fading pages in and out
91 final Resources res = getResources();
92 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
93 final float alpha = customizePagedView.getAlpha();
94 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(customizePagedView,
95 "alpha", alpha, 0.0f);
96 alphaAnim.setDuration(duration);
97 alphaAnim.addListener(new AnimatorListenerAdapter() {
98 @Override
99 public void onAnimationEnd(Animator animation) {
100 customizePagedView.setCustomizationFilter(newType);
101
102 final float alpha = customizePagedView.getAlpha();
103 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
104 customizePagedView, "alpha", alpha, 1.0f);
105 alphaAnim.setDuration(duration);
106 alphaAnim.start();
107 }
108 });
109 alphaAnim.start();
110 }
111 }
112 });
Michael Jurka2763be32011-02-24 11:19:57 -0800113 }
114
115 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800116 public void onLauncherTransitionStart(Animator animation) {
117 if (animation != null) {
118 setLayerType(LAYER_TYPE_HARDWARE, null);
119 // just a sanity check that we don't build a layer before a call to onLayout
120 if (!mFirstLayout) {
121 // force building the layer at the beginning of the animation, so you don't get a
122 // blip early in the animation
123 buildLayer();
124 }
125 }
Michael Jurka2763be32011-02-24 11:19:57 -0800126 }
127
128 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800129 public void onLauncherTransitionEnd(Animator animation) {
130 if (animation != null) {
131 setLayerType(LAYER_TYPE_NONE, null);
132 }
133 }
134
135 @Override
136 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurkaea2daff2011-05-17 18:21:03 -0700137 if (mFirstLayout) {
138 mFirstLayout = false;
139
140 final CustomizePagedView customizePagedView =
141 (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents);
142 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
143 // Set the width of the tab bar properly
144 int pageWidth = customizePagedView.getPageContentWidth();
145 TabWidget customizeTabBar = (TabWidget) findViewById(com.android.internal.R.id.tabs);
146 if (customizeTabBar == null) throw new Resources.NotFoundException();
147 int tabWidgetPadding = 0;
148 final int childCount = tabWidget.getChildCount();
149 if (childCount > 0) {
150 tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
151 }
152 customizeTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
153 }
Michael Jurkaabded662011-03-04 12:06:57 -0800154 super.onLayout(changed, l, t, r, b);
Michael Jurka2763be32011-02-24 11:19:57 -0800155 }
Michael Jurka12ac0d62011-02-23 11:48:32 -0800156
157 CustomizationType getCustomizeFilterForTabTag(String tag) {
158 if (tag.equals(WIDGETS_TAG)) {
159 return CustomizationType.WidgetCustomization;
160 } else if (tag.equals(APPLICATIONS_TAG)) {
161 return CustomizationType.ApplicationCustomization;
162 } else if (tag.equals(WALLPAPERS_TAG)) {
163 return CustomizePagedView.CustomizationType.WallpaperCustomization;
164 } else if (tag.equals(SHORTCUTS_TAG)) {
165 return CustomizePagedView.CustomizationType.ShortcutCustomization;
166 }
167 return CustomizationType.WidgetCustomization;
168 }
Michael Jurka2763be32011-02-24 11:19:57 -0800169}