blob: 2c47895d2e263610a78ef945c9bcd2c2edd4046f [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 });
118
119 // Set the width of the tab bar properly
120 int pageWidth = customizePagedView.getPageContentWidth();
121 TabWidget customizeTabBar = (TabWidget) findViewById(com.android.internal.R.id.tabs);
122 if (customizeTabBar == null) throw new Resources.NotFoundException();
123 int tabWidgetPadding = 0;
124 final int childCount = tabWidget.getChildCount();
125 if (childCount > 0) {
126 tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
127 }
128 customizeTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
Michael Jurka2763be32011-02-24 11:19:57 -0800129 }
130
131 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800132 public void onLauncherTransitionStart(Animator animation) {
133 if (animation != null) {
134 setLayerType(LAYER_TYPE_HARDWARE, null);
135 // just a sanity check that we don't build a layer before a call to onLayout
136 if (!mFirstLayout) {
137 // force building the layer at the beginning of the animation, so you don't get a
138 // blip early in the animation
139 buildLayer();
140 }
141 }
Michael Jurka2763be32011-02-24 11:19:57 -0800142 }
143
144 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800145 public void onLauncherTransitionEnd(Animator animation) {
146 if (animation != null) {
147 setLayerType(LAYER_TYPE_NONE, null);
148 }
149 }
150
151 @Override
152 protected void onLayout(boolean changed, int l, int t, int r, int b) {
153 mFirstLayout = false;
154 super.onLayout(changed, l, t, r, b);
Michael Jurka2763be32011-02-24 11:19:57 -0800155 }
Michael Jurka7ef959b2011-02-23 11:48:32 -0800156
Winson Chung97d85d22011-04-13 11:27:36 -0700157 @Override
158 public int getDescendantFocusability() {
159 if (getVisibility() != View.VISIBLE) {
160 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
161 }
162 return super.getDescendantFocusability();
163 }
164
Michael Jurka7ef959b2011-02-23 11:48:32 -0800165 CustomizationType getCustomizeFilterForTabTag(String tag) {
166 if (tag.equals(WIDGETS_TAG)) {
167 return CustomizationType.WidgetCustomization;
168 } else if (tag.equals(APPLICATIONS_TAG)) {
169 return CustomizationType.ApplicationCustomization;
170 } else if (tag.equals(WALLPAPERS_TAG)) {
171 return CustomizePagedView.CustomizationType.WallpaperCustomization;
172 } else if (tag.equals(SHORTCUTS_TAG)) {
173 return CustomizePagedView.CustomizationType.ShortcutCustomization;
174 }
175 return CustomizationType.WidgetCustomization;
176 }
Michael Jurka2763be32011-02-24 11:19:57 -0800177}