blob: e0d248eb9bc760609b497a06664e6b27071dd088 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -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
19import java.util.ArrayList;
20import java.util.Collections;
21import java.util.Comparator;
22
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.res.TypedArray;
26import android.graphics.drawable.BitmapDrawable;
27import android.util.AttributeSet;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.animation.AlphaAnimation;
31import android.view.animation.Animation;
32import android.view.animation.AnimationUtils;
33import android.view.animation.Animation.AnimationListener;
34import android.widget.TextView;
35
36import com.android.launcher.R;
37
38/**
39 * An implementation of PagedView that populates the pages of the workspace
40 * with all of the user's applications.
41 */
42public class AllAppsPagedView extends PagedView
43 implements AllAppsView, View.OnClickListener, View.OnLongClickListener, DragSource,
44 PagedViewCellLayout.DimmedBitmapSetupListener {
45
46 private static final String TAG = "AllAppsPagedView";
47 private static final boolean DEBUG = false;
48
49 private Launcher mLauncher;
50 private DragController mDragController;
51
52 // preserve compatibility with 3D all apps:
53 // 0.0 -> hidden
54 // 1.0 -> shown and opaque
55 // intermediate values -> partially shown & partially opaque
56 private float mZoom;
57
58 // set of all applications
59 private ArrayList<ApplicationInfo> mApps;
60 private ArrayList<ApplicationInfo> mFilteredApps;
61
62 // the types of applications to filter
63 static final int ALL_APPS_FLAG = -1;
64 private int mAppFilter = ALL_APPS_FLAG;
65
66 private int mCellCountX;
67 private int mCellCountY;
68
69 private final LayoutInflater mInflater;
70
71 public AllAppsPagedView(Context context) {
72 this(context, null);
73 }
74
75 public AllAppsPagedView(Context context, AttributeSet attrs) {
76 this(context, attrs, 0);
77 }
78
79 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
80 super(context, attrs, defStyle);
81 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
82 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
83 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
84 mInflater = LayoutInflater.from(context);
85 a.recycle();
86 setSoundEffectsEnabled(false);
87 }
88
89 @Override
90 public void setLauncher(Launcher launcher) {
91 mLauncher = launcher;
92 }
93
94 @Override
95 public void setDragController(DragController dragger) {
96 mDragController = dragger;
97 }
98
99 public void setAppFilter(int filterType) {
100 mAppFilter = filterType;
101 mFilteredApps = rebuildFilteredApps(mApps);
102 setCurrentScreen(0);
103 invalidatePageData();
104 }
105
106 @Override
107 public void zoom(float zoom, boolean animate) {
108 mZoom = zoom;
109 cancelLongPress();
110
111 if (isVisible()) {
112 getParent().bringChildToFront(this);
113 setVisibility(View.VISIBLE);
114 if (animate) {
115 startAnimation(AnimationUtils.loadAnimation(getContext(),
116 R.anim.all_apps_2d_fade_in));
117 } else {
118 onAnimationEnd();
119 }
120 } else {
121 if (animate) {
122 startAnimation(AnimationUtils.loadAnimation(getContext(),
123 R.anim.all_apps_2d_fade_out));
124 } else {
125 onAnimationEnd();
126 }
127 }
128 }
129
130 protected void onAnimationEnd() {
131 if (!isVisible()) {
132 setVisibility(View.GONE);
133 mZoom = 0.0f;
134 } else {
135 mZoom = 1.0f;
136 }
137
138 if (mLauncher != null)
139 mLauncher.zoomed(mZoom);
140 }
141
142 private int getChildIndexForGrandChild(View v) {
143 final int childCount = getChildCount();
144 for (int i = 0; i < childCount; ++i) {
145 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
146 if (layout.indexOfChild(v) > -1) {
147 return i;
148 }
149 }
150 return -1;
151 }
152
153 @Override
154 public void onClick(View v) {
155 int childIndex = getChildIndexForGrandChild(v);
156 if (childIndex == getCurrentScreen()) {
157 final ApplicationInfo app = (ApplicationInfo) v.getTag();
158
159 AlphaAnimation anim = new AlphaAnimation(1.0f, 0.65f);
160 anim.setDuration(100);
161 anim.setFillAfter(true);
162 anim.setRepeatMode(AlphaAnimation.REVERSE);
163 anim.setRepeatCount(1);
164 anim.setAnimationListener(new AnimationListener() {
165 @Override
166 public void onAnimationStart(Animation animation) {}
167 @Override
168 public void onAnimationRepeat(Animation animation) {
169 mLauncher.startActivitySafely(app.intent, app);
170 }
171 @Override
172 public void onAnimationEnd(Animation animation) {}
173 });
174 v.startAnimation(anim);
175 }
176 }
177
178 @Override
179 public boolean onLongClick(View v) {
180 if (!v.isInTouchMode()) {
181 return false;
182 }
183
184 ApplicationInfo app = (ApplicationInfo) v.getTag();
185 app = new ApplicationInfo(app);
186
187 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
188 mLauncher.closeAllApps(true);
189 return true;
190 }
191
192 @Override
193 public void onDropCompleted(View target, boolean success) {
194 // do nothing
195 }
196
197 @Override
198 public boolean isVisible() {
199 return mZoom > 0.001f;
200 }
201
202 @Override
203 public boolean isAnimating() {
204 return (getAnimation() != null);
205 }
206
207 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
208 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
209 if (mAppFilter == ALL_APPS_FLAG) {
210 return apps;
211 } else {
212 final int length = apps.size();
213 for (int i = 0; i < length; ++i) {
214 ApplicationInfo info = apps.get(i);
215 if ((info.flags & mAppFilter) > 0) {
216 filteredApps.add(info);
217 }
218 }
219 }
220 return filteredApps;
221 }
222
223 @Override
224 public void setApps(ArrayList<ApplicationInfo> list) {
225 mApps = list;
226 Collections.sort(mApps, new Comparator<ApplicationInfo>() {
227 @Override
228 public int compare(ApplicationInfo object1, ApplicationInfo object2) {
229 return object1.title.toString().compareTo(object2.title.toString());
230 }
231 });
232 mFilteredApps = rebuildFilteredApps(mApps);
233 invalidatePageData();
234 }
235
236 @Override
237 public void addApps(ArrayList<ApplicationInfo> list) {
238 // TODO: we need to add it in place, in alphabetical order
239 mApps.addAll(list);
240 mFilteredApps.addAll(rebuildFilteredApps(list));
241 invalidatePageData();
242 }
243
244 @Override
245 public void removeApps(ArrayList<ApplicationInfo> list) {
246 // loop through all the apps and remove apps that have the same component
247 final int length = list.size();
248 for (int i = 0; i < length; ++i) {
249 int removeIndex = findAppByComponent(mApps, list.get(i));
250 if (removeIndex > -1) {
251 mApps.remove(removeIndex);
252 }
253 }
254 mFilteredApps = rebuildFilteredApps(list);
255 invalidatePageData();
256 }
257
258 @Override
259 public void updateApps(ArrayList<ApplicationInfo> list) {
260 removeApps(list);
261 addApps(list);
262 }
263
264 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
265 ComponentName removeComponent = item.intent.getComponent();
266 final int length = list.size();
267 for (int i = 0; i < length; ++i) {
268 ApplicationInfo info = list.get(i);
269 if (info.intent.getComponent().equals(removeComponent)) {
270 return i;
271 }
272 }
273 return -1;
274 }
275
276 @Override
277 public void dumpState() {
278 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
279 }
280
281 @Override
282 public void surrender() {
283 // do nothing?
284 }
285
286 @Override
287 public void syncPages() {
288 // ensure that we have the right number of pages
289 int numPages = (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY));
290 int curNumPages = getChildCount();
291 // remove any extra pages after the "last" page
292 int extraPageDiff = curNumPages - numPages;
293 for (int i = 0; i < extraPageDiff; ++i) {
294 removeViewAt(numPages);
295 }
296 // add any necessary pages
297 for (int i = curNumPages; i < numPages; ++i) {
298 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
299 layout.setCellCount(mCellCountX, mCellCountY);
300 layout.setDimmedBitmapSetupListener(this);
301 addView(layout);
302 }
303
304 // bound the current page
305 setCurrentScreen(Math.max(0, Math.min(numPages - 1, getCurrentScreen())));
306 }
307
308 @Override
309 public void syncPageItems(int page) {
310 // ensure that we have the right number of items on the pages
311 int numCells = mCellCountX * mCellCountY;
312 int startIndex = page * numCells;
313 int endIndex = Math.min(startIndex + numCells, mFilteredApps.size());
314 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
315 // TODO: we can optimize by just re-applying to existing views
316 layout.removeAllViews();
317 for (int i = startIndex; i < endIndex; ++i) {
318 ApplicationInfo info = mFilteredApps.get(i);
319 TextView text = (TextView) mInflater.inflate(R.layout.all_apps_paged_view_application, layout, false);
320 text.setCompoundDrawablesWithIntrinsicBounds(null,
321 new BitmapDrawable(info.iconBitmap), null, null);
322 text.setText(info.title);
323 text.setTag(info);
324 text.setOnClickListener(this);
325 text.setOnLongClickListener(this);
326
327 int index = i - startIndex;
328 layout.addViewToCellLayout(text, index, i,
329 new PagedViewCellLayout.LayoutParams(index % mCellCountX, index / mCellCountX, 1, 1));
330 }
331 }
332
333 @Override
334 public void onPreUpdateDimmedBitmap(PagedViewCellLayout layout) {
335 // disable all children text for now
336 final int childCount = layout.getChildCount();
337 for (int i = 0; i < childCount; ++i) {
338 TextView text = (TextView) layout.getChildAt(i);
339 text.setText("");
340 }
341 }
342 @Override
343 public void onPostUpdateDimmedBitmap(PagedViewCellLayout layout) {
344 // re-enable all children text
345 final int childCount = layout.getChildCount();
346 for (int i = 0; i < childCount; ++i) {
347 TextView text = (TextView) layout.getChildAt(i);
348 final ApplicationInfo info = (ApplicationInfo) text.getTag();
349 text.setText(info.title);
350 }
351 }
352}