blob: b765e17781e3217960c004883179ca0f5945f432 [file] [log] [blame]
Patrick Dubroy558654c2010-07-23 16:48:11 -07001/*
2 * Copyright (C) 2010 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 Jurka8edd75c2010-12-17 20:15:06 -080019import com.android.launcher.R;
Patrick Dubroy558654c2010-07-23 16:48:11 -070020
Winson Chung80baf5a2010-08-09 16:03:15 -070021import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080022import android.animation.AnimatorListenerAdapter;
Chet Haaseb1254a62010-09-07 13:35:00 -070023import android.animation.ObjectAnimator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Patrick Dubroy558654c2010-07-23 16:48:11 -070025import android.content.Context;
26import android.content.res.Resources;
27import android.util.AttributeSet;
28import android.util.Log;
Winson Chung49767ae2010-11-29 14:48:30 -080029import android.view.LayoutInflater;
Adam Lesinski6b879f02010-11-04 16:15:23 -070030import android.view.MotionEvent;
Patrick Dubroy558654c2010-07-23 16:48:11 -070031import android.view.View;
Winson Chung97d85d22011-04-13 11:27:36 -070032import android.view.ViewGroup;
Patrick Dubroy558654c2010-07-23 16:48:11 -070033import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -080034import android.widget.TabWidget;
35import android.widget.TextView;
Patrick Dubroy558654c2010-07-23 16:48:11 -070036
Michael Jurka8edd75c2010-12-17 20:15:06 -080037import java.util.ArrayList;
Patrick Dubroy558654c2010-07-23 16:48:11 -070038
39/**
40 * Implements a tabbed version of AllApps2D.
41 */
Michael Jurkaabded662011-03-04 12:06:57 -080042public class AllAppsTabbed extends TabHost implements AllAppsView, LauncherTransitionable {
Patrick Dubroy558654c2010-07-23 16:48:11 -070043
44 private static final String TAG = "Launcher.AllAppsTabbed";
45
Patrick Dubroy3d605d52010-07-29 13:59:29 -070046 private static final String TAG_ALL = "ALL";
Patrick Dubroy3d605d52010-07-29 13:59:29 -070047 private static final String TAG_DOWNLOADED = "DOWNLOADED";
48
Winson Chung321e9ee2010-08-09 13:37:56 -070049 private AllAppsPagedView mAllApps;
Michael Jurka9c6fbed2011-03-02 17:41:34 -080050 private AllAppsBackground mBackground;
51 private Launcher mLauncher;
Patrick Dubroy3d605d52010-07-29 13:59:29 -070052 private Context mContext;
Winson Chung49767ae2010-11-29 14:48:30 -080053 private final LayoutInflater mInflater;
Michael Jurka6c7a03a2011-01-16 18:43:58 -080054 private boolean mFirstLayout = true;
Patrick Dubroy558654c2010-07-23 16:48:11 -070055
56 public AllAppsTabbed(Context context, AttributeSet attrs) {
57 super(context, attrs);
Patrick Dubroy3d605d52010-07-29 13:59:29 -070058 mContext = context;
Winson Chung49767ae2010-11-29 14:48:30 -080059 mInflater = LayoutInflater.from(context);
Patrick Dubroy558654c2010-07-23 16:48:11 -070060 }
61
62 @Override
63 protected void onFinishInflate() {
Winson Chung321e9ee2010-08-09 13:37:56 -070064 // setup the tab host
65 setup();
66
Patrick Dubroy558654c2010-07-23 16:48:11 -070067 try {
Winson Chung321e9ee2010-08-09 13:37:56 -070068 mAllApps = (AllAppsPagedView) findViewById(R.id.all_apps_paged_view);
69 if (mAllApps == null) throw new Resources.NotFoundException();
Michael Jurka9c6fbed2011-03-02 17:41:34 -080070 mBackground = (AllAppsBackground) findViewById(R.id.all_apps_background);
71 if (mBackground == null) throw new Resources.NotFoundException();
Patrick Dubroy558654c2010-07-23 16:48:11 -070072 } catch (Resources.NotFoundException e) {
73 Log.e(TAG, "Can't find necessary layout elements for AllAppsTabbed");
74 }
Patrick Dubroy558654c2010-07-23 16:48:11 -070075
Winson Chung321e9ee2010-08-09 13:37:56 -070076 // share the same AllApps workspace across all the tabs
Patrick Dubroy558654c2010-07-23 16:48:11 -070077 TabContentFactory contentFactory = new TabContentFactory() {
78 public View createTabContent(String tag) {
Winson Chung321e9ee2010-08-09 13:37:56 -070079 return mAllApps;
Patrick Dubroy558654c2010-07-23 16:48:11 -070080 }
81 };
82
Michael Jurka7ef959b2011-02-23 11:48:32 -080083 // Create the tabs and wire them up properly
Winson Chungfaa13252011-06-13 18:15:54 -070084 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
Winson Chung49767ae2010-11-29 14:48:30 -080085 TextView tabView;
86 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
87 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
88 tabView.setText(mContext.getString(R.string.all_apps_tab_all));
89 addTab(newTabSpec(TAG_ALL).setIndicator(tabView).setContent(contentFactory));
Patrick Dubroy3d605d52010-07-29 13:59:29 -070090
Winson Chung49767ae2010-11-29 14:48:30 -080091 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
92 tabView.setText(mContext.getString(R.string.all_apps_tab_downloaded));
93 addTab(newTabSpec(TAG_DOWNLOADED).setIndicator(tabView).setContent(contentFactory));
Patrick Dubroy3d605d52010-07-29 13:59:29 -070094
Winson Chung97d85d22011-04-13 11:27:36 -070095 // Setup the key listener to jump between the last tab view and the market icon
96 View lastTab = tabWidget.getChildTabViewAt(tabWidget.getTabCount() - 1);
97 lastTab.setOnKeyListener(keyListener);
98 View shopButton = findViewById(R.id.market_button);
99 shopButton.setOnKeyListener(keyListener);
100
Patrick Dubroy3d605d52010-07-29 13:59:29 -0700101 setOnTabChangedListener(new OnTabChangeListener() {
102 public void onTabChanged(String tabId) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700103 // animate the changing of the tab content by fading pages in and out
Winson Chungbbc60d82010-11-11 16:34:41 -0800104 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -0700105 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700106 final float alpha = mAllApps.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700107 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mAllApps, "alpha", alpha, 0.0f).
108 setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800109 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700110 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800111 public void onAnimationEnd(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700112 String tag = getCurrentTabTag();
113 if (tag == TAG_ALL) {
114 mAllApps.setAppFilter(AllAppsPagedView.ALL_APPS_FLAG);
Winson Chung80baf5a2010-08-09 16:03:15 -0700115 } else if (tag == TAG_DOWNLOADED) {
116 mAllApps.setAppFilter(ApplicationInfo.DOWNLOADED_FLAG);
117 }
118
119 final float alpha = mAllApps.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700120 ObjectAnimator.ofFloat(mAllApps, "alpha", alpha, 1.0f).
121 setDuration(duration).start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700122 }
123 });
124 alphaAnim.start();
Patrick Dubroy3d605d52010-07-29 13:59:29 -0700125 }
126 });
Patrick Dubroy558654c2010-07-23 16:48:11 -0700127
Michael Jurka7ef959b2011-02-23 11:48:32 -0800128
Winson Chung321e9ee2010-08-09 13:37:56 -0700129 // It needs to be INVISIBLE so that it will be measured in the layout.
130 // Otherwise the animations is messed up when we show it for the first time.
131 setVisibility(INVISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700132 }
133
134 @Override
Winson Chung785d2eb2011-04-14 16:08:02 -0700135 public void setup(Launcher launcher, DragController dragController) {
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800136 mLauncher = launcher;
Winson Chung785d2eb2011-04-14 16:08:02 -0700137 mAllApps.setup(launcher, dragController);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700138 }
139
140 @Override
141 public void zoom(float zoom, boolean animate) {
142 // NOTE: animate parameter is ignored for the TabHost itself
143 setVisibility((zoom == 0.0f) ? View.GONE : View.VISIBLE);
Winson Chung321e9ee2010-08-09 13:37:56 -0700144 mAllApps.zoom(zoom, animate);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700145 }
146
147 @Override
148 public void setVisibility(int visibility) {
Michael Jurka6c7a03a2011-01-16 18:43:58 -0800149 if (visibility == View.GONE && mFirstLayout) {
150 // It needs to be INVISIBLE so that it will be measured in the layout.
151 // Otherwise the animations is messed up when we show it for the first time.
152 visibility = View.INVISIBLE;
153 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700154 final boolean isVisible = (visibility == View.VISIBLE);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700155 super.setVisibility(visibility);
Winson Chung80baf5a2010-08-09 16:03:15 -0700156 float zoom = (isVisible ? 1.0f : 0.0f);
Winson Chung321e9ee2010-08-09 13:37:56 -0700157 mAllApps.zoom(zoom, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700158 }
159
160 @Override
161 public boolean isVisible() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 return mAllApps.isVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -0700163 }
164
165 @Override
Michael Jurka6c7a03a2011-01-16 18:43:58 -0800166 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Michael Jurka4c6016f2011-05-17 18:21:03 -0700167 if (mFirstLayout) {
168 mFirstLayout = false;
Michael Jurka4c6016f2011-05-17 18:21:03 -0700169 }
Michael Jurka983e3fd2011-05-26 17:10:29 -0700170 // Set the width of the tab bar properly
171 int pageWidth = mAllApps.getPageContentWidth();
172 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
173 View allAppsTabBar = (View) findViewById(R.id.all_apps_tab_bar);
174 if (allAppsTabBar == null) throw new Resources.NotFoundException();
175 int tabWidgetPadding = 0;
176 final int childCount = tabWidget.getChildCount();
177 if (childCount > 0) {
178 tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
179 }
180
181 int newWidth = Math.min(getMeasuredWidth(), pageWidth + tabWidgetPadding);
182 if (newWidth != allAppsTabBar.getLayoutParams().width) {
183 allAppsTabBar.getLayoutParams().width = newWidth;
184 post(new Runnable() {
185 public void run() {
186 requestLayout();
187 }
188 });
189 }
190
Michael Jurka6c7a03a2011-01-16 18:43:58 -0800191 super.onLayout(changed, l, t, r, b);
192 }
193
194 @Override
Patrick Dubroyff5f0402010-07-26 15:23:26 -0700195 public boolean isAnimating() {
196 return (getAnimation() != null);
197 }
198
199 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800200 public void onLauncherTransitionStart(Animator animation) {
201 if (animation != null) {
202 // Turn on hardware layers for performance
203 setLayerType(LAYER_TYPE_HARDWARE, null);
204 // Re-enable the rendering of the dimmed background in All Apps for performance reasons
Michael Jurkacc6c8842011-03-09 14:38:14 -0800205 // if we're fading it in
206 if (mLauncher.getWorkspace().getBackgroundAlpha() == 0f) {
207 mLauncher.getWorkspace().disableBackground();
208 mBackground.setVisibility(VISIBLE);
209 }
Michael Jurkaabded662011-03-04 12:06:57 -0800210 // just a sanity check that we don't build a layer before a call to onLayout
211 if (!mFirstLayout) {
212 // force building the layer at the beginning of the animation, so you don't get a
213 // blip early in the animation
214 buildLayer();
215 }
216 }
Michael Jurka2763be32011-02-24 11:19:57 -0800217 }
218
219 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800220 public void onLauncherTransitionEnd(Animator animation) {
221 if (animation != null) {
222 setLayerType(LAYER_TYPE_NONE, null);
223 // To improve the performance of the first time All Apps is run, we initially keep
224 // hardware layers in AllAppsPagedView disabled since AllAppsTabbed itself is drawn in a
225 // hardware layer, and creating additional hardware layers slows down the animation. We
226 // create them here, after the animation is over.
227 }
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800228 // Move the rendering of the dimmed background to workspace after the all apps animation
229 // is done, so that the background is not rendered *above* the mini workspace screens
Michael Jurkacc6c8842011-03-09 14:38:14 -0800230 if (mBackground.getVisibility() != GONE) {
231 mLauncher.getWorkspace().enableBackground();
232 mBackground.setVisibility(GONE);
233 }
Michael Jurkaabded662011-03-04 12:06:57 -0800234 mAllApps.allowHardwareLayerCreation();
Michael Jurka2763be32011-02-24 11:19:57 -0800235 }
236
237 @Override
Patrick Dubroy558654c2010-07-23 16:48:11 -0700238 public void setApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 mAllApps.setApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700240 }
241
242 @Override
243 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 mAllApps.addApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700245 }
246
247 @Override
248 public void removeApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 mAllApps.removeApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700250 }
251
252 @Override
253 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 mAllApps.updateApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700255 }
256
257 @Override
258 public void dumpState() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700259 mAllApps.dumpState();
Patrick Dubroy558654c2010-07-23 16:48:11 -0700260 }
261
262 @Override
263 public void surrender() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700264 mAllApps.surrender();
Patrick Dubroy558654c2010-07-23 16:48:11 -0700265 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700266
Winson Chung337cd9d2011-03-30 10:39:30 -0700267 public void reset() {
268 mAllApps.reset();
269 }
270
Adam Lesinski6b879f02010-11-04 16:15:23 -0700271 @Override
272 public boolean onTouchEvent(MotionEvent ev) {
273 if (ev.getY() > mAllApps.getBottom()) {
274 return false;
275 }
276 return true;
277 }
Winson Chung97d85d22011-04-13 11:27:36 -0700278
279 @Override
280 public int getDescendantFocusability() {
281 if (getVisibility() != View.VISIBLE) {
282 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
283 }
284 return super.getDescendantFocusability();
285 }
Patrick Dubroy558654c2010-07-23 16:48:11 -0700286}