blob: 0aa77245a99e68ff18f85fe205daecb272f782a8 [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;
Winson Chung321e9ee2010-08-09 13:37:56 -070021
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.res.TypedArray;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070025import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.util.AttributeSet;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070027import android.view.ActionMode;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.view.LayoutInflater;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070029import android.view.Menu;
30import android.view.MenuItem;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.view.View;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.view.animation.AnimationUtils;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070033import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import 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
Winson Chung5f2aa4e2010-08-20 14:49:25 -070043 implements AllAppsView, View.OnClickListener, View.OnLongClickListener, DragSource,
44 DropTarget, ActionMode.Callback {
Winson Chung321e9ee2010-08-09 13:37:56 -070045
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;
Winson Chung80baf5a2010-08-09 16:03:15 -0700101 if (mApps != null) {
102 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700103 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700104 invalidatePageData();
105 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700106 }
107
108 @Override
109 public void zoom(float zoom, boolean animate) {
110 mZoom = zoom;
111 cancelLongPress();
112
113 if (isVisible()) {
114 getParent().bringChildToFront(this);
115 setVisibility(View.VISIBLE);
116 if (animate) {
117 startAnimation(AnimationUtils.loadAnimation(getContext(),
118 R.anim.all_apps_2d_fade_in));
119 } else {
120 onAnimationEnd();
121 }
122 } else {
123 if (animate) {
124 startAnimation(AnimationUtils.loadAnimation(getContext(),
125 R.anim.all_apps_2d_fade_out));
126 } else {
127 onAnimationEnd();
128 }
129 }
130 }
131
132 protected void onAnimationEnd() {
133 if (!isVisible()) {
134 setVisibility(View.GONE);
135 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700136
137 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700138 } else {
139 mZoom = 1.0f;
140 }
141
142 if (mLauncher != null)
143 mLauncher.zoomed(mZoom);
144 }
145
146 private int getChildIndexForGrandChild(View v) {
147 final int childCount = getChildCount();
148 for (int i = 0; i < childCount; ++i) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700149 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700150 if (layout.indexOfChild(v) > -1) {
151 return i;
152 }
153 }
154 return -1;
155 }
156
157 @Override
158 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700159 // if we are already in a choice mode, then just change the selection
160 if (v instanceof Checkable) {
161 if (!isChoiceMode(CHOICE_MODE_NONE)) {
162 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
163 // reset all the previously checked items if in single selection mode
164 resetCheckedGrandchildren();
165 }
166
167 // then toggle this one
168 Checkable c = (Checkable) v;
169 c.toggle();
170 return;
171 }
172 }
173
174 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700175 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700176 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700177 final ApplicationInfo app = (ApplicationInfo) v.getTag();
178
Winson Chung80baf5a2010-08-09 16:03:15 -0700179 // animate some feedback to the click
180 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700181 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700182 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700183 mLauncher.startActivitySafely(app.intent, app);
184 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700185 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700186
187 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700188 }
189 }
190
191 @Override
192 public boolean onLongClick(View v) {
193 if (!v.isInTouchMode()) {
194 return false;
195 }
196
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700197 /* Uncomment this to enable selection mode with the action bar
198
199 // start the choice mode, and select the item that was long-pressed
200 if (isChoiceMode(CHOICE_MODE_NONE)) {
201 startChoiceMode(CHOICE_MODE_SINGLE, this);
202 }
203
204 if (v instanceof Checkable) {
205 // In preparation for drag, we always reset the checked grand children regardless of
206 // what choice mode we are in
207 resetCheckedGrandchildren();
208
209 // Toggle the selection on the dragged app
210 Checkable c = (Checkable) v;
211 c.toggle();
212 }
213 */
214
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 ApplicationInfo app = (ApplicationInfo) v.getTag();
216 app = new ApplicationInfo(app);
217
218 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 return true;
220 }
221
222 @Override
223 public void onDropCompleted(View target, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700224 // close the choice action mode if we have a proper drop
225 if (target != this) {
226 endChoiceMode();
227 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700228 }
229
230 @Override
231 public boolean isVisible() {
232 return mZoom > 0.001f;
233 }
234
235 @Override
236 public boolean isAnimating() {
237 return (getAnimation() != null);
238 }
239
240 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
241 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
242 if (mAppFilter == ALL_APPS_FLAG) {
243 return apps;
244 } else {
245 final int length = apps.size();
246 for (int i = 0; i < length; ++i) {
247 ApplicationInfo info = apps.get(i);
248 if ((info.flags & mAppFilter) > 0) {
249 filteredApps.add(info);
250 }
251 }
252 }
253 return filteredApps;
254 }
255
256 @Override
257 public void setApps(ArrayList<ApplicationInfo> list) {
258 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700259 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700260 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung241c3b42010-08-25 16:53:03 -0700261 mPageViewIconCache.clear();
Winson Chung321e9ee2010-08-09 13:37:56 -0700262 invalidatePageData();
263 }
264
Winson Chung80baf5a2010-08-09 16:03:15 -0700265 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
266 // we add it in place, in alphabetical order
267 final int count = list.size();
268 for (int i = 0; i < count; ++i) {
269 final ApplicationInfo info = list.get(i);
270 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
271 if (index < 0) {
272 mApps.add(-(index + 1), info);
273 }
274 }
275 mFilteredApps = rebuildFilteredApps(mApps);
276 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 @Override
278 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700279 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700280 invalidatePageData();
281 }
282
Winson Chung80baf5a2010-08-09 16:03:15 -0700283 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700284 // loop through all the apps and remove apps that have the same component
285 final int length = list.size();
286 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700287 final ApplicationInfo info = list.get(i);
288 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700289 if (removeIndex > -1) {
290 mApps.remove(removeIndex);
Winson Chung241c3b42010-08-25 16:53:03 -0700291 mPageViewIconCache.removeOutline(info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700292 }
293 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700294 mFilteredApps = rebuildFilteredApps(mApps);
295 }
296 @Override
297 public void removeApps(ArrayList<ApplicationInfo> list) {
298 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700299 invalidatePageData();
300 }
301
302 @Override
303 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700304 removeAppsWithoutInvalidate(list);
305 addAppsWithoutInvalidate(list);
306 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700307 }
308
309 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
310 ComponentName removeComponent = item.intent.getComponent();
311 final int length = list.size();
312 for (int i = 0; i < length; ++i) {
313 ApplicationInfo info = list.get(i);
314 if (info.intent.getComponent().equals(removeComponent)) {
315 return i;
316 }
317 }
318 return -1;
319 }
320
321 @Override
322 public void dumpState() {
323 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
324 }
325
326 @Override
327 public void surrender() {
328 // do nothing?
329 }
330
331 @Override
332 public void syncPages() {
333 // ensure that we have the right number of pages
334 int numPages = (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY));
335 int curNumPages = getChildCount();
336 // remove any extra pages after the "last" page
337 int extraPageDiff = curNumPages - numPages;
338 for (int i = 0; i < extraPageDiff; ++i) {
339 removeViewAt(numPages);
340 }
341 // add any necessary pages
342 for (int i = curNumPages; i < numPages; ++i) {
343 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
344 layout.setCellCount(mCellCountX, mCellCountY);
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 addView(layout);
346 }
347
348 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700349 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700350 }
351
352 @Override
353 public void syncPageItems(int page) {
354 // ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700355 final int cellsPerPage = mCellCountX * mCellCountY;
356 final int startIndex = page * cellsPerPage;
357 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700358 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700359
360 final int curNumPageItems = layout.getChildCount();
361 final int numPageItems = endIndex - startIndex;
362
363 // remove any extra items
364 int extraPageItemsDiff = curNumPageItems - numPageItems;
365 for (int i = 0; i < extraPageItemsDiff; ++i) {
366 layout.removeViewAt(numPageItems);
367 }
368 // add any necessary items
369 for (int i = curNumPageItems; i < numPageItems; ++i) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700370 TextView text = (TextView) mInflater.inflate(R.layout.all_apps_paged_view_application, layout, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700371 text.setOnClickListener(this);
372 text.setOnLongClickListener(this);
373
374 layout.addViewToCellLayout(text, -1, i,
375 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
376 }
377
378 // actually reapply to the existing text views
379 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700380 final int index = i - startIndex;
381 final ApplicationInfo info = mFilteredApps.get(i);
382 PagedViewIcon icon = (PagedViewIcon) layout.getChildAt(index);
383 icon.applyFromApplicationInfo(info, mPageViewIconCache);
Winson Chung321e9ee2010-08-09 13:37:56 -0700384
Winson Chung80baf5a2010-08-09 16:03:15 -0700385 PagedViewCellLayout.LayoutParams params =
Winson Chung241c3b42010-08-25 16:53:03 -0700386 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
Winson Chung80baf5a2010-08-09 16:03:15 -0700387 params.cellX = index % mCellCountX;
388 params.cellY = index / mCellCountX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 }
390 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700391
392 @Override
393 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
394 mDragController.addDropTarget(this);
395
396 // REST TO BE IMPLEMENTED BY PAT
397 mode.setTitle("Customization title goes here");
398 return true;
399 }
400
401 @Override
402 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
403 return true;
404 }
405
406 @Override
407 public void onDestroyActionMode(ActionMode mode) {
408 mDragController.removeDropTarget(this);
409 endChoiceMode();
410 }
411
412 @Override
413 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
414 // TO BE IMPLEMENTED BY PAT
415 // get the checked grandchild, and handle the action here
416 return false;
417 }
418
419 /*
420 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
421 * to the workspace.
422 */
423 @Override
424 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
425 DragView dragView, Object dragInfo) {
426 return false;
427 }
428 @Override
429 public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
430 DragView dragView, Object dragInfo, Rect recycle) {
431 return null;
432 }
433 @Override
434 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
435 int yOffset, DragView dragView, Object dragInfo) {
436 return null;
437 }
438 @Override
439 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
440 DragView dragView, Object dragInfo) {}
441 @Override
442 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
443 DragView dragView, Object dragInfo) {}
444 @Override
445 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
446 DragView dragView, Object dragInfo) {}
447 @Override
448 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
449 DragView dragView, Object dragInfo) {}
Winson Chung321e9ee2010-08-09 13:37:56 -0700450}