blob: 3e0402509bd767c73b9ee201b1cca34f370704f7 [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 });
113
114 // Set the width of the tab bar properly
115 int pageWidth = customizePagedView.getPageContentWidth();
116 TabWidget customizeTabBar = (TabWidget) findViewById(com.android.internal.R.id.tabs);
117 if (customizeTabBar == null) throw new Resources.NotFoundException();
118 int tabWidgetPadding = 0;
119 final int childCount = tabWidget.getChildCount();
120 if (childCount > 0) {
121 tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
122 }
123 customizeTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
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
147 protected void onLayout(boolean changed, int l, int t, int r, int b) {
148 mFirstLayout = false;
149 super.onLayout(changed, l, t, r, b);
Michael Jurka2763be32011-02-24 11:19:57 -0800150 }
Michael Jurka12ac0d62011-02-23 11:48:32 -0800151
152 CustomizationType getCustomizeFilterForTabTag(String tag) {
153 if (tag.equals(WIDGETS_TAG)) {
154 return CustomizationType.WidgetCustomization;
155 } else if (tag.equals(APPLICATIONS_TAG)) {
156 return CustomizationType.ApplicationCustomization;
157 } else if (tag.equals(WALLPAPERS_TAG)) {
158 return CustomizePagedView.CustomizationType.WallpaperCustomization;
159 } else if (tag.equals(SHORTCUTS_TAG)) {
160 return CustomizePagedView.CustomizationType.ShortcutCustomization;
161 }
162 return CustomizationType.WidgetCustomization;
163 }
Michael Jurka2763be32011-02-24 11:19:57 -0800164}