blob: bdd7066f2beaae688ff3a4f371fa1aad007932ee [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
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070019import com.android.launcher.R;
Winson Chung321e9ee2010-08-09 13:37:56 -070020
21import android.content.ComponentName;
22import android.content.Context;
23import android.content.res.TypedArray;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070024import android.graphics.Rect;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.util.AttributeSet;
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070026import android.util.Log;
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
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070036import java.util.ArrayList;
37import java.util.Collections;
Winson Chung321e9ee2010-08-09 13:37:56 -070038
39/**
40 * An implementation of PagedView that populates the pages of the workspace
41 * with all of the user's applications.
42 */
43public class AllAppsPagedView extends PagedView
Winson Chung5f2aa4e2010-08-20 14:49:25 -070044 implements AllAppsView, View.OnClickListener, View.OnLongClickListener, DragSource,
45 DropTarget, ActionMode.Callback {
Winson Chung321e9ee2010-08-09 13:37:56 -070046
47 private static final String TAG = "AllAppsPagedView";
48 private static final boolean DEBUG = false;
49
Patrick Dubroy9f7aec82010-09-06 11:03:37 -070050 private static final int MENU_DELETE_APP = 1;
51 private static final int MENU_APP_INFO = 2;
52
Winson Chung321e9ee2010-08-09 13:37:56 -070053 private Launcher mLauncher;
54 private DragController mDragController;
55
56 // preserve compatibility with 3D all apps:
57 // 0.0 -> hidden
58 // 1.0 -> shown and opaque
59 // intermediate values -> partially shown & partially opaque
60 private float mZoom;
61
62 // set of all applications
63 private ArrayList<ApplicationInfo> mApps;
64 private ArrayList<ApplicationInfo> mFilteredApps;
65
66 // the types of applications to filter
67 static final int ALL_APPS_FLAG = -1;
68 private int mAppFilter = ALL_APPS_FLAG;
69
70 private int mCellCountX;
71 private int mCellCountY;
72
73 private final LayoutInflater mInflater;
74
75 public AllAppsPagedView(Context context) {
76 this(context, null);
77 }
78
79 public AllAppsPagedView(Context context, AttributeSet attrs) {
80 this(context, attrs, 0);
81 }
82
83 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
84 super(context, attrs, defStyle);
85 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
86 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
87 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
88 mInflater = LayoutInflater.from(context);
89 a.recycle();
90 setSoundEffectsEnabled(false);
91 }
92
93 @Override
94 public void setLauncher(Launcher launcher) {
95 mLauncher = launcher;
96 }
97
98 @Override
99 public void setDragController(DragController dragger) {
100 mDragController = dragger;
101 }
102
103 public void setAppFilter(int filterType) {
104 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700105 if (mApps != null) {
106 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700107 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700108 invalidatePageData();
109 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700110 }
111
112 @Override
113 public void zoom(float zoom, boolean animate) {
114 mZoom = zoom;
115 cancelLongPress();
116
117 if (isVisible()) {
118 getParent().bringChildToFront(this);
119 setVisibility(View.VISIBLE);
120 if (animate) {
121 startAnimation(AnimationUtils.loadAnimation(getContext(),
122 R.anim.all_apps_2d_fade_in));
123 } else {
124 onAnimationEnd();
125 }
126 } else {
127 if (animate) {
128 startAnimation(AnimationUtils.loadAnimation(getContext(),
129 R.anim.all_apps_2d_fade_out));
130 } else {
131 onAnimationEnd();
132 }
133 }
134 }
135
136 protected void onAnimationEnd() {
137 if (!isVisible()) {
138 setVisibility(View.GONE);
139 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700140
141 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700142 } else {
143 mZoom = 1.0f;
144 }
145
146 if (mLauncher != null)
147 mLauncher.zoomed(mZoom);
148 }
149
150 private int getChildIndexForGrandChild(View v) {
151 final int childCount = getChildCount();
152 for (int i = 0; i < childCount; ++i) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700153 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700154 if (layout.indexOfChild(v) > -1) {
155 return i;
156 }
157 }
158 return -1;
159 }
160
161 @Override
162 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700163 // if we are already in a choice mode, then just change the selection
164 if (v instanceof Checkable) {
165 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700166 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700167 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700168 // Uncheck all the other grandchildren, and toggle the clicked one
169 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700170 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700171 c.setChecked(!wasChecked);
172 } else {
173 c.toggle();
174 }
175 if (getCheckedGrandchildren().size() == 0) {
176 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700177 }
178
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700179 return;
180 }
181 }
182
183 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700184 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700185 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700186 final ApplicationInfo app = (ApplicationInfo) v.getTag();
187
Winson Chung80baf5a2010-08-09 16:03:15 -0700188 // animate some feedback to the click
189 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700191 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 mLauncher.startActivitySafely(app.intent, app);
193 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700194 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700195
196 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 }
198 }
199
200 @Override
201 public boolean onLongClick(View v) {
202 if (!v.isInTouchMode()) {
203 return false;
204 }
205
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700206 // start the choice mode, and select the item that was long-pressed
207 if (isChoiceMode(CHOICE_MODE_NONE)) {
208 startChoiceMode(CHOICE_MODE_SINGLE, this);
209 }
210
211 if (v instanceof Checkable) {
212 // In preparation for drag, we always reset the checked grand children regardless of
213 // what choice mode we are in
214 resetCheckedGrandchildren();
215
216 // Toggle the selection on the dragged app
217 Checkable c = (Checkable) v;
218 c.toggle();
219 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700220
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 ApplicationInfo app = (ApplicationInfo) v.getTag();
222 app = new ApplicationInfo(app);
223
224 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 return true;
226 }
227
228 @Override
229 public void onDropCompleted(View target, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700230 // close the choice action mode if we have a proper drop
231 if (target != this) {
232 endChoiceMode();
233 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 }
235
236 @Override
237 public boolean isVisible() {
238 return mZoom > 0.001f;
239 }
240
241 @Override
242 public boolean isAnimating() {
243 return (getAnimation() != null);
244 }
245
246 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
247 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
248 if (mAppFilter == ALL_APPS_FLAG) {
249 return apps;
250 } else {
251 final int length = apps.size();
252 for (int i = 0; i < length; ++i) {
253 ApplicationInfo info = apps.get(i);
254 if ((info.flags & mAppFilter) > 0) {
255 filteredApps.add(info);
256 }
257 }
258 }
259 return filteredApps;
260 }
261
262 @Override
263 public void setApps(ArrayList<ApplicationInfo> list) {
264 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700265 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung241c3b42010-08-25 16:53:03 -0700267 mPageViewIconCache.clear();
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 invalidatePageData();
269 }
270
Winson Chung80baf5a2010-08-09 16:03:15 -0700271 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
272 // we add it in place, in alphabetical order
273 final int count = list.size();
274 for (int i = 0; i < count; ++i) {
275 final ApplicationInfo info = list.get(i);
276 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
277 if (index < 0) {
278 mApps.add(-(index + 1), info);
279 }
280 }
281 mFilteredApps = rebuildFilteredApps(mApps);
282 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700283 @Override
284 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700285 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700286 invalidatePageData();
287 }
288
Winson Chung80baf5a2010-08-09 16:03:15 -0700289 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700290 // loop through all the apps and remove apps that have the same component
291 final int length = list.size();
292 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700293 final ApplicationInfo info = list.get(i);
294 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700295 if (removeIndex > -1) {
296 mApps.remove(removeIndex);
Winson Chung241c3b42010-08-25 16:53:03 -0700297 mPageViewIconCache.removeOutline(info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 }
299 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700300 mFilteredApps = rebuildFilteredApps(mApps);
301 }
302 @Override
303 public void removeApps(ArrayList<ApplicationInfo> list) {
304 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 invalidatePageData();
306 }
307
308 @Override
309 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700310 removeAppsWithoutInvalidate(list);
311 addAppsWithoutInvalidate(list);
312 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700313 }
314
315 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
316 ComponentName removeComponent = item.intent.getComponent();
317 final int length = list.size();
318 for (int i = 0; i < length; ++i) {
319 ApplicationInfo info = list.get(i);
320 if (info.intent.getComponent().equals(removeComponent)) {
321 return i;
322 }
323 }
324 return -1;
325 }
326
327 @Override
328 public void dumpState() {
329 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
330 }
331
332 @Override
333 public void surrender() {
334 // do nothing?
335 }
336
337 @Override
338 public void syncPages() {
339 // ensure that we have the right number of pages
340 int numPages = (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY));
341 int curNumPages = getChildCount();
342 // remove any extra pages after the "last" page
343 int extraPageDiff = curNumPages - numPages;
344 for (int i = 0; i < extraPageDiff; ++i) {
345 removeViewAt(numPages);
346 }
347 // add any necessary pages
348 for (int i = curNumPages; i < numPages; ++i) {
349 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
350 layout.setCellCount(mCellCountX, mCellCountY);
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 addView(layout);
352 }
353
354 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700355 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700356 }
357
358 @Override
359 public void syncPageItems(int page) {
360 // ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700361 final int cellsPerPage = mCellCountX * mCellCountY;
362 final int startIndex = page * cellsPerPage;
363 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700364 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700365
366 final int curNumPageItems = layout.getChildCount();
367 final int numPageItems = endIndex - startIndex;
368
369 // remove any extra items
370 int extraPageItemsDiff = curNumPageItems - numPageItems;
371 for (int i = 0; i < extraPageItemsDiff; ++i) {
372 layout.removeViewAt(numPageItems);
373 }
374 // add any necessary items
375 for (int i = curNumPageItems; i < numPageItems; ++i) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700376 TextView text = (TextView) mInflater.inflate(R.layout.all_apps_paged_view_application, layout, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700377 text.setOnClickListener(this);
378 text.setOnLongClickListener(this);
379
380 layout.addViewToCellLayout(text, -1, i,
381 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
382 }
383
384 // actually reapply to the existing text views
385 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700386 final int index = i - startIndex;
387 final ApplicationInfo info = mFilteredApps.get(i);
388 PagedViewIcon icon = (PagedViewIcon) layout.getChildAt(index);
389 icon.applyFromApplicationInfo(info, mPageViewIconCache);
Winson Chung321e9ee2010-08-09 13:37:56 -0700390
Winson Chung80baf5a2010-08-09 16:03:15 -0700391 PagedViewCellLayout.LayoutParams params =
Winson Chung241c3b42010-08-25 16:53:03 -0700392 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
Winson Chung80baf5a2010-08-09 16:03:15 -0700393 params.cellX = index % mCellCountX;
394 params.cellY = index / mCellCountX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700395 }
396 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700397
398 @Override
399 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700400 mode.setTitle(R.string.cab_selection_text);
401 menu.add(0, MENU_APP_INFO, 0, R.string.cab_menu_app_info)
402 .setIcon(R.drawable.info_button);
403 menu.add(0, MENU_DELETE_APP, 0, R.string.cab_menu_delete_app)
404 .setIcon(R.drawable.delete_zone_selector);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700405 return true;
406 }
407
408 @Override
409 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700410 mDragController.addDropTarget(this);
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700411 return true;
412 }
413
414 @Override
415 public void onDestroyActionMode(ActionMode mode) {
416 mDragController.removeDropTarget(this);
417 endChoiceMode();
418 }
419
420 @Override
421 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700422 final int id = item.getItemId();
423
424 // Assumes that we are in CHOICE_MODE_SINGLE
425 final View chosenView = (View) getSingleCheckedGrandchild();
426 final ApplicationInfo appInfo = (ApplicationInfo) chosenView.getTag();
427
428 if (id == MENU_APP_INFO) {
429 mLauncher.startApplicationDetailsActivity(appInfo.componentName);
430 } else if (id == MENU_DELETE_APP) {
431 mLauncher.startApplicationUninstallActivity(appInfo.componentName);
432 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700433 return false;
434 }
435
436 /*
437 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
438 * to the workspace.
439 */
440 @Override
441 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
442 DragView dragView, Object dragInfo) {
443 return false;
444 }
445 @Override
446 public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset,
447 DragView dragView, Object dragInfo, Rect recycle) {
448 return null;
449 }
450 @Override
451 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
452 int yOffset, DragView dragView, Object dragInfo) {
453 return null;
454 }
455 @Override
456 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
457 DragView dragView, Object dragInfo) {}
458 @Override
459 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
460 DragView dragView, Object dragInfo) {}
461 @Override
462 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
463 DragView dragView, Object dragInfo) {}
464 @Override
465 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
466 DragView dragView, Object dragInfo) {}
Winson Chung321e9ee2010-08-09 13:37:56 -0700467}