blob: ee7bfc0562f64edf07ffb216b465ae4d35c25e6d [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;
32import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -080033import android.widget.TabWidget;
34import android.widget.TextView;
Patrick Dubroy558654c2010-07-23 16:48:11 -070035
Michael Jurka8edd75c2010-12-17 20:15:06 -080036import java.util.ArrayList;
Patrick Dubroy558654c2010-07-23 16:48:11 -070037
38/**
39 * Implements a tabbed version of AllApps2D.
40 */
Michael Jurkaabded662011-03-04 12:06:57 -080041public class AllAppsTabbed extends TabHost implements AllAppsView, LauncherTransitionable {
Patrick Dubroy558654c2010-07-23 16:48:11 -070042
43 private static final String TAG = "Launcher.AllAppsTabbed";
44
Patrick Dubroy3d605d52010-07-29 13:59:29 -070045 private static final String TAG_ALL = "ALL";
Patrick Dubroy3d605d52010-07-29 13:59:29 -070046 private static final String TAG_DOWNLOADED = "DOWNLOADED";
47
Winson Chung321e9ee2010-08-09 13:37:56 -070048 private AllAppsPagedView mAllApps;
Michael Jurka9c6fbed2011-03-02 17:41:34 -080049 private AllAppsBackground mBackground;
50 private Launcher mLauncher;
Patrick Dubroy3d605d52010-07-29 13:59:29 -070051 private Context mContext;
Winson Chung49767ae2010-11-29 14:48:30 -080052 private final LayoutInflater mInflater;
Michael Jurka6c7a03a2011-01-16 18:43:58 -080053 private boolean mFirstLayout = true;
Patrick Dubroy558654c2010-07-23 16:48:11 -070054
55 public AllAppsTabbed(Context context, AttributeSet attrs) {
56 super(context, attrs);
Patrick Dubroy3d605d52010-07-29 13:59:29 -070057 mContext = context;
Winson Chung49767ae2010-11-29 14:48:30 -080058 mInflater = LayoutInflater.from(context);
Patrick Dubroy558654c2010-07-23 16:48:11 -070059 }
60
61 @Override
62 protected void onFinishInflate() {
Winson Chung321e9ee2010-08-09 13:37:56 -070063 // setup the tab host
64 setup();
65
Patrick Dubroy558654c2010-07-23 16:48:11 -070066 try {
Winson Chung321e9ee2010-08-09 13:37:56 -070067 mAllApps = (AllAppsPagedView) findViewById(R.id.all_apps_paged_view);
68 if (mAllApps == null) throw new Resources.NotFoundException();
Michael Jurka9c6fbed2011-03-02 17:41:34 -080069 mBackground = (AllAppsBackground) findViewById(R.id.all_apps_background);
70 if (mBackground == null) throw new Resources.NotFoundException();
Patrick Dubroy558654c2010-07-23 16:48:11 -070071 } catch (Resources.NotFoundException e) {
72 Log.e(TAG, "Can't find necessary layout elements for AllAppsTabbed");
73 }
Patrick Dubroy558654c2010-07-23 16:48:11 -070074
Winson Chung321e9ee2010-08-09 13:37:56 -070075 // share the same AllApps workspace across all the tabs
Patrick Dubroy558654c2010-07-23 16:48:11 -070076 TabContentFactory contentFactory = new TabContentFactory() {
77 public View createTabContent(String tag) {
Winson Chung321e9ee2010-08-09 13:37:56 -070078 return mAllApps;
Patrick Dubroy558654c2010-07-23 16:48:11 -070079 }
80 };
81
Michael Jurka12ac0d62011-02-23 11:48:32 -080082 // Create the tabs and wire them up properly
Winson Chung49767ae2010-11-29 14:48:30 -080083 TextView tabView;
84 TabWidget tabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
85 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
86 tabView.setText(mContext.getString(R.string.all_apps_tab_all));
87 addTab(newTabSpec(TAG_ALL).setIndicator(tabView).setContent(contentFactory));
Patrick Dubroy3d605d52010-07-29 13:59:29 -070088
Winson Chung49767ae2010-11-29 14:48:30 -080089 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
90 tabView.setText(mContext.getString(R.string.all_apps_tab_downloaded));
91 addTab(newTabSpec(TAG_DOWNLOADED).setIndicator(tabView).setContent(contentFactory));
Patrick Dubroy3d605d52010-07-29 13:59:29 -070092
93 setOnTabChangedListener(new OnTabChangeListener() {
94 public void onTabChanged(String tabId) {
Winson Chung80baf5a2010-08-09 16:03:15 -070095 // animate the changing of the tab content by fading pages in and out
Winson Chungbbc60d82010-11-11 16:34:41 -080096 final Resources res = getResources();
97 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
Winson Chung80baf5a2010-08-09 16:03:15 -070098 final float alpha = mAllApps.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -070099 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mAllApps, "alpha", alpha, 0.0f).
100 setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800101 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700102 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800103 public void onAnimationEnd(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700104 String tag = getCurrentTabTag();
105 if (tag == TAG_ALL) {
106 mAllApps.setAppFilter(AllAppsPagedView.ALL_APPS_FLAG);
Winson Chung80baf5a2010-08-09 16:03:15 -0700107 } else if (tag == TAG_DOWNLOADED) {
108 mAllApps.setAppFilter(ApplicationInfo.DOWNLOADED_FLAG);
109 }
110
111 final float alpha = mAllApps.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700112 ObjectAnimator.ofFloat(mAllApps, "alpha", alpha, 1.0f).
113 setDuration(duration).start();
Winson Chung80baf5a2010-08-09 16:03:15 -0700114 }
115 });
116 alphaAnim.start();
Patrick Dubroy3d605d52010-07-29 13:59:29 -0700117 }
118 });
Patrick Dubroy558654c2010-07-23 16:48:11 -0700119
Michael Jurka12ac0d62011-02-23 11:48:32 -0800120 // Set the width of the tab bar properly
121 int pageWidth = mAllApps.getPageContentWidth();
122 View allAppsTabBar = (View) findViewById(R.id.all_apps_tab_bar);
123 if (allAppsTabBar == null) throw new Resources.NotFoundException();
124 int tabWidgetPadding = 0;
125 final int childCount = tabWidget.getChildCount();
126 if (childCount > 0) {
127 tabWidgetPadding += tabWidget.getChildAt(0).getPaddingLeft() * 2;
128 }
129 allAppsTabBar.getLayoutParams().width = pageWidth + tabWidgetPadding;
130
Winson Chung321e9ee2010-08-09 13:37:56 -0700131 // It needs to be INVISIBLE so that it will be measured in the layout.
132 // Otherwise the animations is messed up when we show it for the first time.
133 setVisibility(INVISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700134 }
135
136 @Override
137 public void setLauncher(Launcher launcher) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700138 mAllApps.setLauncher(launcher);
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800139 mLauncher = launcher;
Patrick Dubroy558654c2010-07-23 16:48:11 -0700140 }
141
142 @Override
143 public void setDragController(DragController dragger) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700144 mAllApps.setDragController(dragger);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700145 }
146
147 @Override
148 public void zoom(float zoom, boolean animate) {
149 // NOTE: animate parameter is ignored for the TabHost itself
150 setVisibility((zoom == 0.0f) ? View.GONE : View.VISIBLE);
Winson Chung321e9ee2010-08-09 13:37:56 -0700151 mAllApps.zoom(zoom, animate);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700152 }
153
154 @Override
155 public void setVisibility(int visibility) {
Michael Jurka6c7a03a2011-01-16 18:43:58 -0800156 if (visibility == View.GONE && mFirstLayout) {
157 // It needs to be INVISIBLE so that it will be measured in the layout.
158 // Otherwise the animations is messed up when we show it for the first time.
159 visibility = View.INVISIBLE;
160 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700161 final boolean isVisible = (visibility == View.VISIBLE);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700162 super.setVisibility(visibility);
Winson Chung80baf5a2010-08-09 16:03:15 -0700163 float zoom = (isVisible ? 1.0f : 0.0f);
Winson Chung321e9ee2010-08-09 13:37:56 -0700164 mAllApps.zoom(zoom, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700165 }
166
167 @Override
168 public boolean isVisible() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700169 return mAllApps.isVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -0700170 }
171
172 @Override
Michael Jurka6c7a03a2011-01-16 18:43:58 -0800173 protected void onLayout(boolean changed, int l, int t, int r, int b) {
174 mFirstLayout = false;
175 super.onLayout(changed, l, t, r, b);
176 }
177
178 @Override
Patrick Dubroyff5f0402010-07-26 15:23:26 -0700179 public boolean isAnimating() {
180 return (getAnimation() != null);
181 }
182
183 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800184 public void onLauncherTransitionStart(Animator animation) {
185 if (animation != null) {
186 // Turn on hardware layers for performance
187 setLayerType(LAYER_TYPE_HARDWARE, null);
188 // Re-enable the rendering of the dimmed background in All Apps for performance reasons
Michael Jurkacc6c8842011-03-09 14:38:14 -0800189 // if we're fading it in
190 if (mLauncher.getWorkspace().getBackgroundAlpha() == 0f) {
191 mLauncher.getWorkspace().disableBackground();
192 mBackground.setVisibility(VISIBLE);
193 }
Michael Jurkaabded662011-03-04 12:06:57 -0800194 // just a sanity check that we don't build a layer before a call to onLayout
195 if (!mFirstLayout) {
196 // force building the layer at the beginning of the animation, so you don't get a
197 // blip early in the animation
198 buildLayer();
199 }
200 }
Michael Jurka2763be32011-02-24 11:19:57 -0800201 }
202
203 @Override
Michael Jurkaabded662011-03-04 12:06:57 -0800204 public void onLauncherTransitionEnd(Animator animation) {
205 if (animation != null) {
206 setLayerType(LAYER_TYPE_NONE, null);
207 // To improve the performance of the first time All Apps is run, we initially keep
208 // hardware layers in AllAppsPagedView disabled since AllAppsTabbed itself is drawn in a
209 // hardware layer, and creating additional hardware layers slows down the animation. We
210 // create them here, after the animation is over.
211 }
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800212 // Move the rendering of the dimmed background to workspace after the all apps animation
213 // is done, so that the background is not rendered *above* the mini workspace screens
Michael Jurkacc6c8842011-03-09 14:38:14 -0800214 if (mBackground.getVisibility() != GONE) {
215 mLauncher.getWorkspace().enableBackground();
216 mBackground.setVisibility(GONE);
217 }
Michael Jurkaabded662011-03-04 12:06:57 -0800218 mAllApps.allowHardwareLayerCreation();
Michael Jurka2763be32011-02-24 11:19:57 -0800219 }
220
221 @Override
Patrick Dubroy558654c2010-07-23 16:48:11 -0700222 public void setApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 mAllApps.setApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700224 }
225
226 @Override
227 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 mAllApps.addApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700229 }
230
231 @Override
232 public void removeApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 mAllApps.removeApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700234 }
235
236 @Override
237 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700238 mAllApps.updateApps(list);
Patrick Dubroy558654c2010-07-23 16:48:11 -0700239 }
240
241 @Override
242 public void dumpState() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 mAllApps.dumpState();
Patrick Dubroy558654c2010-07-23 16:48:11 -0700244 }
245
246 @Override
247 public void surrender() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700248 mAllApps.surrender();
Patrick Dubroy558654c2010-07-23 16:48:11 -0700249 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700250
251 @Override
252 public boolean onTouchEvent(MotionEvent ev) {
253 if (ev.getY() > mAllApps.getBottom()) {
254 return false;
255 }
256 return true;
257 }
Patrick Dubroy558654c2010-07-23 16:48:11 -0700258}