blob: ab50cf1619cc530cee19e94f3c4a7e3bc2ef3323 [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
Michael Jurka7ef959b2011-02-23 11:48:32 -080047 private final LayoutInflater mInflater;
48 private Context mContext;
Michael Jurka2763be32011-02-24 11:19:57 -080049
50 public CustomizeTrayTabHost(Context context, AttributeSet attrs) {
51 super(context, attrs);
Michael Jurka7ef959b2011-02-23 11:48:32 -080052 mContext = context;
53 mInflater = LayoutInflater.from(context);
54 }
55
56 @Override
57 protected void onFinishInflate() {
58 setup();
59
60 final CustomizePagedView customizePagedView =
61 (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents);
62
63 // Configure tabs
64 TabContentFactory contentFactory = new TabContentFactory() {
65 public View createTabContent(String tag) {
66 return customizePagedView;
67 }
68 };
69
70 TextView tabView;
71 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
72
73 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
74 tabView.setText(mContext.getString(R.string.widgets_tab_label));
Winson Chung97d85d22011-04-13 11:27:36 -070075 addTab(newTabSpec(WIDGETS_TAG)
76 .setIndicator(tabView).setContent(contentFactory));
Michael Jurka7ef959b2011-02-23 11:48:32 -080077 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
78 tabView.setText(mContext.getString(R.string.applications_tab_label));
79 addTab(newTabSpec(APPLICATIONS_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.wallpapers_tab_label));
83 addTab(newTabSpec(WALLPAPERS_TAG)
84 .setIndicator(tabView).setContent(contentFactory));
85 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
86 tabView.setText(mContext.getString(R.string.shortcuts_tab_label));
87 addTab(newTabSpec(SHORTCUTS_TAG)
88 .setIndicator(tabView).setContent(contentFactory));
Winson Chung97d85d22011-04-13 11:27:36 -070089
Michael Jurka7ef959b2011-02-23 11:48:32 -080090 setOnTabChangedListener(new OnTabChangeListener() {
91 public void onTabChanged(String tabId) {
92 final CustomizePagedView.CustomizationType newType =
93 getCustomizeFilterForTabTag(tabId);
94 if (newType != customizePagedView.getCustomizationFilter()) {
95 // animate the changing of the tab content by fading pages in and out
96 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -070097 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
Michael Jurka7ef959b2011-02-23 11:48:32 -080098 final float alpha = customizePagedView.getAlpha();
99 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(customizePagedView,
100 "alpha", alpha, 0.0f);
101 alphaAnim.setDuration(duration);
102 alphaAnim.addListener(new AnimatorListenerAdapter() {
103 @Override
104 public void onAnimationEnd(Animator animation) {
105 customizePagedView.setCustomizationFilter(newType);
106
107 final float alpha = customizePagedView.getAlpha();
108 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
109 customizePagedView, "alpha", alpha, 1.0f);
110 alphaAnim.setDuration(duration);
111 alphaAnim.start();
112 }
113 });
114 alphaAnim.start();
115 }
116 }
117 });
Michael Jurka2763be32011-02-24 11:19:57 -0800118 }
119
120 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800121 public void onLauncherTransitionStart(Animator animation) {
122 if (animation != null) {
123 setLayerType(LAYER_TYPE_HARDWARE, null);
124 // just a sanity check that we don't build a layer before a call to onLayout
125 if (!mFirstLayout) {
126 // force building the layer at the beginning of the animation, so you don't get a
127 // blip early in the animation
128 buildLayer();
129 }
130 }
Michael Jurka2763be32011-02-24 11:19:57 -0800131 }
132
133 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800134 public void onLauncherTransitionEnd(Animator animation) {
135 if (animation != null) {
136 setLayerType(LAYER_TYPE_NONE, null);
137 }
138 }
139
140 @Override
141 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka4c6016f2011-05-17 18:21:03 -0700142 if (mFirstLayout) {
143 mFirstLayout = false;
144
145 final CustomizePagedView customizePagedView =
146 (CustomizePagedView) findViewById(R.id.customization_drawer_tab_contents);
147 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
148 // Set the width of the tab bar properly
149 int pageWidth = customizePagedView.getPageContentWidth();
150 TabWidget customizeTabBar = (TabWidget) findViewById(com.android.internal.R.id.tabs);
151 if (customizeTabBar == null) throw new Resources.NotFoundException();
152 int tabWidgetPadding = 0;
153 final int childCount = tabWidget.getChildCount();
154 if (childCount > 0) {
155 tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
156 }
157 customizeTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
158 }
Michael Jurkaabded662011-03-04 12:06:57 -0800159 super.onLayout(changed, l, t, r, b);
Michael Jurka2763be32011-02-24 11:19:57 -0800160 }
Michael Jurka7ef959b2011-02-23 11:48:32 -0800161
Winson Chung97d85d22011-04-13 11:27:36 -0700162 @Override
163 public int getDescendantFocusability() {
164 if (getVisibility() != View.VISIBLE) {
165 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
166 }
167 return super.getDescendantFocusability();
168 }
169
Michael Jurka7ef959b2011-02-23 11:48:32 -0800170 CustomizationType getCustomizeFilterForTabTag(String tag) {
171 if (tag.equals(WIDGETS_TAG)) {
172 return CustomizationType.WidgetCustomization;
173 } else if (tag.equals(APPLICATIONS_TAG)) {
174 return CustomizationType.ApplicationCustomization;
175 } else if (tag.equals(WALLPAPERS_TAG)) {
176 return CustomizePagedView.CustomizationType.WallpaperCustomization;
177 } else if (tag.equals(SHORTCUTS_TAG)) {
178 return CustomizePagedView.CustomizationType.ShortcutCustomization;
179 }
180 return CustomizationType.WidgetCustomization;
181 }
Michael Jurka2763be32011-02-24 11:19:57 -0800182}