blob: 96de5a7335ad15febc7703d3e12abdecf134cdbd [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;
Winson Chung97d85d22011-04-13 11:27:36 -070038import java.util.Random;
Patrick Dubroy558654c2010-07-23 16:48:11 -070039
40/**
41 * Implements a tabbed version of AllApps2D.
42 */
Michael Jurkaabded662011-03-04 12:06:57 -080043public class AllAppsTabbed extends TabHost implements AllAppsView, LauncherTransitionable {
Patrick Dubroy558654c2010-07-23 16:48:11 -070044
45 private static final String TAG = "Launcher.AllAppsTabbed";
46
Patrick Dubroy3d605d52010-07-29 13:59:29 -070047 private static final String TAG_ALL = "ALL";
Patrick Dubroy3d605d52010-07-29 13:59:29 -070048 private static final String TAG_DOWNLOADED = "DOWNLOADED";
49
Winson Chung321e9ee2010-08-09 13:37:56 -070050 private AllAppsPagedView mAllApps;
Michael Jurka9c6fbed2011-03-02 17:41:34 -080051 private AllAppsBackground mBackground;
52 private Launcher mLauncher;
Patrick Dubroy3d605d52010-07-29 13:59:29 -070053 private Context mContext;
Winson Chung49767ae2010-11-29 14:48:30 -080054 private final LayoutInflater mInflater;
Michael Jurka6c7a03a2011-01-16 18:43:58 -080055 private boolean mFirstLayout = true;
Patrick Dubroy558654c2010-07-23 16:48:11 -070056
57 public AllAppsTabbed(Context context, AttributeSet attrs) {
58 super(context, attrs);
Patrick Dubroy3d605d52010-07-29 13:59:29 -070059 mContext = context;
Winson Chung49767ae2010-11-29 14:48:30 -080060 mInflater = LayoutInflater.from(context);
Patrick Dubroy558654c2010-07-23 16:48:11 -070061 }
62
63 @Override
64 protected void onFinishInflate() {
Winson Chung321e9ee2010-08-09 13:37:56 -070065 // setup the tab host
66 setup();
67
Patrick Dubroy558654c2010-07-23 16:48:11 -070068 try {
Winson Chung321e9ee2010-08-09 13:37:56 -070069 mAllApps = (AllAppsPagedView) findViewById(R.id.all_apps_paged_view);
70 if (mAllApps == null) throw new Resources.NotFoundException();
Michael Jurka9c6fbed2011-03-02 17:41:34 -080071 mBackground = (AllAppsBackground) findViewById(R.id.all_apps_background);
72 if (mBackground == null) throw new Resources.NotFoundException();
Patrick Dubroy558654c2010-07-23 16:48:11 -070073 } catch (Resources.NotFoundException e) {
74 Log.e(TAG, "Can't find necessary layout elements for AllAppsTabbed");
75 }
Patrick Dubroy558654c2010-07-23 16:48:11 -070076
Winson Chung321e9ee2010-08-09 13:37:56 -070077 // share the same AllApps workspace across all the tabs
Patrick Dubroy558654c2010-07-23 16:48:11 -070078 TabContentFactory contentFactory = new TabContentFactory() {
79 public View createTabContent(String tag) {
Winson Chung321e9ee2010-08-09 13:37:56 -070080 return mAllApps;
Patrick Dubroy558654c2010-07-23 16:48:11 -070081 }
82 };
83
Michael Jurka7ef959b2011-02-23 11:48:32 -080084 // Create the tabs and wire them up properly
Winson Chung97d85d22011-04-13 11:27:36 -070085 AllAppsTabKeyEventListener keyListener = new AllAppsTabKeyEventListener();
Winson Chung49767ae2010-11-29 14:48:30 -080086 TextView tabView;
87 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
88 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
89 tabView.setText(mContext.getString(R.string.all_apps_tab_all));
90 addTab(newTabSpec(TAG_ALL).setIndicator(tabView).setContent(contentFactory));
Patrick Dubroy3d605d52010-07-29 13:59:29 -070091
Winson Chung49767ae2010-11-29 14:48:30 -080092 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
93 tabView.setText(mContext.getString(R.string.all_apps_tab_downloaded));
94 addTab(newTabSpec(TAG_DOWNLOADED).setIndicator(tabView).setContent(contentFactory));
Patrick Dubroy3d605d52010-07-29 13:59:29 -070095
Winson Chung97d85d22011-04-13 11:27:36 -070096 // Setup the key listener to jump between the last tab view and the market icon
97 View lastTab = tabWidget.getChildTabViewAt(tabWidget.getTabCount() - 1);
98 lastTab.setOnKeyListener(keyListener);
99 View shopButton = findViewById(R.id.market_button);
100 shopButton.setOnKeyListener(keyListener);
101
Patrick Dubroy3d605d52010-07-29 13:59:29 -0700102 setOnTabChangedListener(new OnTabChangeListener() {
103 public void onTabChanged(String tabId) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700104 // animate the changing of the tab content by fading pages in and out
Winson Chungbbc60d82010-11-11 16:34:41 -0800105 final Resources res = getResources();
Winson Chung785d2eb2011-04-14 16:08:02 -0700106 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700107 final float alpha = mAllApps.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700108 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mAllApps, "alpha", alpha, 0.0f).
109 setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800110 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700111 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800112 public void onAnimationEnd(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700113 String tag = getCurrentTabTag();
114 if (tag == TAG_ALL) {
115 mAllApps.setAppFilter(AllAppsPagedView.ALL_APPS_FLAG);
Winson Chung80baf5a2010-08-09 16:03:15 -0700116 } else if (tag == TAG_DOWNLOADED) {
117 mAllApps.setAppFilter(ApplicationInfo.DOWNLOADED_FLAG);
118 }
119
120 final float alpha = mAllApps.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700121 ObjectAnimator.ofFloat(mAllApps, "alpha", alpha, 1.0f).
122 setDuration(duration).start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700123 }
124 });
125 alphaAnim.start();
Patrick Dubroy3d605d52010-07-29 13:59:29 -0700126 }
127 });
Patrick Dubroy558654c2010-07-23 16:48:11 -0700128
Michael Jurka7ef959b2011-02-23 11:48:32 -0800129 // Set the width of the tab bar properly
130 int pageWidth = mAllApps.getPageContentWidth();
131 View allAppsTabBar = (View) findViewById(R.id.all_apps_tab_bar);
132 if (allAppsTabBar == null) throw new Resources.NotFoundException();
133 int tabWidgetPadding = 0;
134 final int childCount = tabWidget.getChildCount();
135 if (childCount > 0) {
136 tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
137 }
138 allAppsTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
139
Winson Chung321e9ee2010-08-09 13:37:56 -0700140 // It needs to be INVISIBLE so that it will be measured in the layout.
141 // Otherwise the animations is messed up when we show it for the first time.
142 setVisibility(INVISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700143 }
144
145 @Override
Winson Chung785d2eb2011-04-14 16:08:02 -0700146 public void setup(Launcher launcher, DragController dragController) {
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800147 mLauncher = launcher;
Winson Chung785d2eb2011-04-14 16:08:02 -0700148 mAllApps.setup(launcher, dragController);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700149 }
150
151 @Override
152 public void zoom(float zoom, boolean animate) {
153 // NOTE: animate parameter is ignored for the TabHost itself
154 setVisibility((zoom == 0.0f) ? View.GONE : View.VISIBLE);
Winson Chung321e9ee2010-08-09 13:37:56 -0700155 mAllApps.zoom(zoom, animate);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700156 }
157
158 @Override
159 public void setVisibility(int visibility) {
Michael Jurka6c7a03a2011-01-16 18:43:58 -0800160 if (visibility == View.GONE && mFirstLayout) {
161 // It needs to be INVISIBLE so that it will be measured in the layout.
162 // Otherwise the animations is messed up when we show it for the first time.
163 visibility = View.INVISIBLE;
164 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700165 final boolean isVisible = (visibility == View.VISIBLE);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700166 super.setVisibility(visibility);
Winson Chung80baf5a2010-08-09 16:03:15 -0700167 float zoom = (isVisible ? 1.0f : 0.0f);
Winson Chung321e9ee2010-08-09 13:37:56 -0700168 mAllApps.zoom(zoom, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700169 }
170
171 @Override
172 public boolean isVisible() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700173 return mAllApps.isVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -0700174 }
175
176 @Override
Michael Jurka6c7a03a2011-01-16 18:43:58 -0800177 protected void onLayout(boolean changed, int l, int t, int r, int b) {
178 mFirstLayout = false;
179 super.onLayout(changed, l, t, r, b);
180 }
181
182 @Override
Patrick Dubroyff5f0402010-07-26 15:23:26 -0700183 public boolean isAnimating() {
184 return (getAnimation() != null);
185 }
186
187 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800188 public void onLauncherTransitionStart(Animator animation) {
189 if (animation != null) {
190 // Turn on hardware layers for performance
191 setLayerType(LAYER_TYPE_HARDWARE, null);
192 // Re-enable the rendering of the dimmed background in All Apps for performance reasons
Michael Jurkacc6c8842011-03-09 14:38:14 -0800193 // if we're fading it in
194 if (mLauncher.getWorkspace().getBackgroundAlpha() == 0f) {
195 mLauncher.getWorkspace().disableBackground();
196 mBackground.setVisibility(VISIBLE);
197 }
Michael Jurkaabded662011-03-04 12:06:57 -0800198 // just a sanity check that we don't build a layer before a call to onLayout
199 if (!mFirstLayout) {
200 // force building the layer at the beginning of the animation, so you don't get a
201 // blip early in the animation
202 buildLayer();
203 }
204 }
Michael Jurka2763be32011-02-24 11:19:57 -0800205 }
206
207 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800208 public void onLauncherTransitionEnd(Animator animation) {
209 if (animation != null) {
210 setLayerType(LAYER_TYPE_NONE, null);
211 // To improve the performance of the first time All Apps is run, we initially keep
212 // hardware layers in AllAppsPagedView disabled since AllAppsTabbed itself is drawn in a
213 // hardware layer, and creating additional hardware layers slows down the animation. We
214 // create them here, after the animation is over.
215 }
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800216 // Move the rendering of the dimmed background to workspace after the all apps animation
217 // is done, so that the background is not rendered *above* the mini workspace screens
Michael Jurkacc6c8842011-03-09 14:38:14 -0800218 if (mBackground.getVisibility() != GONE) {
219 mLauncher.getWorkspace().enableBackground();
220 mBackground.setVisibility(GONE);
221 }
Michael Jurkaabded662011-03-04 12:06:57 -0800222 mAllApps.allowHardwareLayerCreation();
Michael Jurka2763be32011-02-24 11:19:57 -0800223 }
224
225 @Override
Patrick Dubroy558654c2010-07-23 16:48:11 -0700226 public void setApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 mAllApps.setApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700228 }
229
230 @Override
231 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 mAllApps.addApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700233 }
234
235 @Override
236 public void removeApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 mAllApps.removeApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700238 }
239
240 @Override
241 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 mAllApps.updateApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700243 }
244
245 @Override
246 public void dumpState() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 mAllApps.dumpState();
Patrick Dubroy558654c2010-07-23 16:48:11 -0700248 }
249
250 @Override
251 public void surrender() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700252 mAllApps.surrender();
Patrick Dubroy558654c2010-07-23 16:48:11 -0700253 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700254
Winson Chung337cd9d2011-03-30 10:39:30 -0700255 public void reset() {
256 mAllApps.reset();
257 }
258
Adam Lesinski6b879f02010-11-04 16:15:23 -0700259 @Override
260 public boolean onTouchEvent(MotionEvent ev) {
261 if (ev.getY() > mAllApps.getBottom()) {
262 return false;
263 }
264 return true;
265 }
Winson Chung97d85d22011-04-13 11:27:36 -0700266
267 @Override
268 public int getDescendantFocusability() {
269 if (getVisibility() != View.VISIBLE) {
270 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
271 }
272 return super.getDescendantFocusability();
273 }
Patrick Dubroy558654c2010-07-23 16:48:11 -0700274}