blob: b4eaa49498671ba5232bd063ff665f8afb1985a8 [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
Michael Jurkaaf91de02010-11-23 16:23:58 -080019import com.android.launcher.R;
Winson Chung321e9ee2010-08-09 13:37:56 -070020
21import android.content.ComponentName;
22import android.content.Context;
Michael Jurka72b079e2010-12-10 01:03:53 -080023import android.content.res.Resources;
Winson Chung321e9ee2010-08-09 13:37:56 -070024import android.content.res.TypedArray;
Michael Jurkad3ef3062010-11-23 16:23:58 -080025import android.graphics.Bitmap;
26import android.graphics.Canvas;
27import android.graphics.drawable.Drawable;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.util.AttributeSet;
29import android.view.LayoutInflater;
30import android.view.View;
Winson Chung321e9ee2010-08-09 13:37:56 -070031import android.view.animation.AnimationUtils;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070032import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070033import android.widget.TextView;
34
Michael Jurkaaf91de02010-11-23 16:23:58 -080035import java.util.ArrayList;
36import java.util.Collections;
37import java.util.HashSet;
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 */
Michael Jurka72b079e2010-12-10 01:03:53 -080043public class AllAppsPagedView extends PagedViewWithDraggableItems implements AllAppsView,
44 View.OnClickListener, DragSource, DropTarget {
Winson Chung321e9ee2010-08-09 13:37:56 -070045
46 private static final String TAG = "AllAppsPagedView";
Winson Chung321e9ee2010-08-09 13:37:56 -070047
48 private Launcher mLauncher;
49 private DragController mDragController;
50
51 // preserve compatibility with 3D all apps:
52 // 0.0 -> hidden
53 // 1.0 -> shown and opaque
54 // intermediate values -> partially shown & partially opaque
55 private float mZoom;
56
57 // set of all applications
58 private ArrayList<ApplicationInfo> mApps;
59 private ArrayList<ApplicationInfo> mFilteredApps;
60
61 // the types of applications to filter
62 static final int ALL_APPS_FLAG = -1;
63 private int mAppFilter = ALL_APPS_FLAG;
64
Winson Chung321e9ee2010-08-09 13:37:56 -070065 private final LayoutInflater mInflater;
Michael Jurkaabded662011-03-04 12:06:57 -080066 private boolean mAllowHardwareLayerCreation;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Michael Jurka12ac0d62011-02-23 11:48:32 -080068 private int mPageContentWidth;
Patrick Dubroydea9e932010-09-22 15:04:29 -070069
Patrick Dubroye61c6a32011-05-19 16:48:48 -070070 private int mLastMeasureWidth = -1;
71 private int mLastMeasureHeight = -1;
72
Winson Chung321e9ee2010-08-09 13:37:56 -070073 public AllAppsPagedView(Context context) {
74 this(context, null);
75 }
76
77 public AllAppsPagedView(Context context, AttributeSet attrs) {
78 this(context, attrs, 0);
79 }
80
81 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
82 super(context, attrs, defStyle);
83 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
Winson Chung321e9ee2010-08-09 13:37:56 -070084 mInflater = LayoutInflater.from(context);
Winson Chung8b534782011-02-23 13:43:59 -080085 mApps = new ArrayList<ApplicationInfo>();
86 mFilteredApps = new ArrayList<ApplicationInfo>();
Winson Chung321e9ee2010-08-09 13:37:56 -070087 a.recycle();
88 setSoundEffectsEnabled(false);
Michael Jurka72b079e2010-12-10 01:03:53 -080089
90 Resources r = context.getResources();
91 setDragSlopeThreshold(
92 r.getInteger(R.integer.config_allAppsDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka12ac0d62011-02-23 11:48:32 -080093
94 // Create a dummy page and set it up to find out the content width (used by our parent)
95 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
96 setupPage(layout);
97 mPageContentWidth = layout.getContentWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -070098 }
99
100 @Override
Winson Chung7da10252010-10-28 16:07:04 -0700101 protected void init() {
102 super.init();
103 mCenterPagesVertically = false;
104 }
105
Michael Jurkaea2daff2011-05-17 18:21:03 -0700106 @Override
107 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Patrick Dubroye61c6a32011-05-19 16:48:48 -0700108 final int width = MeasureSpec.getSize(widthMeasureSpec);
109 final int height = MeasureSpec.getSize(heightMeasureSpec);
Michael Jurkaea2daff2011-05-17 18:21:03 -0700110
Patrick Dubroye61c6a32011-05-19 16:48:48 -0700111 if (mLastMeasureWidth != width || mLastMeasureHeight != height) {
Michael Jurkaea2daff2011-05-17 18:21:03 -0700112 // Create a dummy page and set it up to find out the content width (used by our parent)
113 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
114 setupPage(layout);
115 mPageContentWidth = layout.getContentWidth();
Patrick Dubroye61c6a32011-05-19 16:48:48 -0700116
117 mCellCountX = determineCellCountX(width, layout);
118 mCellCountY = determineCellCountY(height, layout);
119 mLastMeasureWidth = width;
120 mLastMeasureHeight = height;
Michael Jurkaea2daff2011-05-17 18:21:03 -0700121 }
Patrick Dubroye61c6a32011-05-19 16:48:48 -0700122 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
123 }
124
125 @Override
126 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
127 if (mFirstLayout) {
128 invalidatePageData();
129
130 // invalidatePageData() is what causes the child pages to be created. We need the
131 // children to be measured before layout, so force a new measure here.
132 measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
133 MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
134 }
135 super.onLayout(changed, left, top, right, bottom);
136 }
137
138 private int determineCellCountX(int availableWidth, PagedViewCellLayout layout) {
139 int cellCountX = 0;
140 final int cellWidth = layout.getCellWidth();
141
142 // Subtract padding for current page and adjacent pages
143 availableWidth -= mPageLayoutPaddingLeft * 2 + mPageLayoutPaddingRight * 2;
144
145 availableWidth -= cellWidth; // Assume at least one column
146 cellCountX = 1 + availableWidth / (cellWidth + mPageLayoutWidthGap);
147 availableWidth = availableWidth % (cellWidth + mPageLayoutWidthGap);
148
149 // Ensures that we show at least 30% of the holo icons on each side
150 final int minLeftoverWidth = (int) (cellWidth * 0.6f);
151
152 // Reserve room for the holo outlines
153 if (cellCountX <= 4) {
154 // When we're really tight on space, just pack the icons a bit closer together
155 final int missingWidth = minLeftoverWidth - availableWidth;
156 if (missingWidth > 0) {
157 mPageLayoutWidthGap -= Math.ceil(missingWidth * 1.0f / (cellCountX - 1));
158 }
159 } else {
160 if (cellCountX >= 8) {
161 // Carve out a few extra columns for very large widths
162 cellCountX = (int) (cellCountX * 0.9f);
163 } else if (availableWidth < minLeftoverWidth) {
164 cellCountX -= 1;
165 }
166 }
167 return cellCountX;
168 }
169
170 private int determineCellCountY(int availableHeight, PagedViewCellLayout layout) {
171 final int cellHeight = layout.getCellHeight();
172 final int screenHeight = mLauncher.getResources().getDisplayMetrics().heightPixels;
173
174 availableHeight -= mPageLayoutPaddingTop + mPageLayoutPaddingBottom;
175 availableHeight -= cellHeight; // Assume at least one row
176 availableHeight -= screenHeight * 0.16f;
177 return (1 + availableHeight / (cellHeight + mPageLayoutHeightGap));
Michael Jurkaea2daff2011-05-17 18:21:03 -0700178 }
179
Michael Jurkaabded662011-03-04 12:06:57 -0800180 void allowHardwareLayerCreation() {
181 // This is called after the first time we launch into All Apps. Before that point,
182 // there's no need for hardware layers here since there's a hardware layer set on the
183 // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here
184 // before the animation is done slows down the animation
185 if (mAllowHardwareLayerCreation) {
186 return;
187 }
188 mAllowHardwareLayerCreation = true;
189 int childCount = getChildCount();
190 for (int i = 0; i < childCount; i++) {
191 PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(i);
192 page.allowHardwareLayerCreation();
193 }
194 }
195
Winson Chung7da10252010-10-28 16:07:04 -0700196 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700197 public void setLauncher(Launcher launcher) {
198 mLauncher = launcher;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700199 mLauncher.setAllAppsPagedView(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 }
201
202 @Override
203 public void setDragController(DragController dragger) {
204 mDragController = dragger;
205 }
206
207 public void setAppFilter(int filterType) {
208 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700209 if (mApps != null) {
210 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700211 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700212 invalidatePageData();
213 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 }
215
216 @Override
217 public void zoom(float zoom, boolean animate) {
218 mZoom = zoom;
219 cancelLongPress();
220
221 if (isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 if (animate) {
223 startAnimation(AnimationUtils.loadAnimation(getContext(),
224 R.anim.all_apps_2d_fade_in));
225 } else {
226 onAnimationEnd();
227 }
228 } else {
229 if (animate) {
230 startAnimation(AnimationUtils.loadAnimation(getContext(),
231 R.anim.all_apps_2d_fade_out));
232 } else {
233 onAnimationEnd();
234 }
235 }
236 }
237
238 protected void onAnimationEnd() {
239 if (!isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700241
242 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 } else {
244 mZoom = 1.0f;
245 }
246
247 if (mLauncher != null)
248 mLauncher.zoomed(mZoom);
249 }
250
251 private int getChildIndexForGrandChild(View v) {
252 final int childCount = getChildCount();
253 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800254 final Page layout = (Page) getChildAt(i);
255 if (layout.indexOfChildOnPage(v) > -1) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700256 return i;
257 }
258 }
259 return -1;
260 }
261
262 @Override
263 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700264 // if we are already in a choice mode, then just change the selection
265 if (v instanceof Checkable) {
266 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700267 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700268 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700269 // Uncheck all the other grandchildren, and toggle the clicked one
270 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700271 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700272 c.setChecked(!wasChecked);
273 } else {
274 c.toggle();
275 }
276 if (getCheckedGrandchildren().size() == 0) {
277 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700278 }
279
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700280 return;
281 }
282 }
283
284 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700286 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700287 final ApplicationInfo app = (ApplicationInfo) v.getTag();
288
Winson Chung80baf5a2010-08-09 16:03:15 -0700289 // animate some feedback to the click
290 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700291 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700292 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700293 mLauncher.startActivitySafely(app.intent, app);
294 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700295 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700296
297 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 }
299 }
300
Patrick Dubroycd953712011-02-28 15:16:42 -0800301 private void setupDragMode(ApplicationInfo info) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800302 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE);
Patrick Dubroycd953712011-02-28 15:16:42 -0800303
304 // Only show the uninstall button if the app is uninstallable.
305 if ((info.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
306 DeleteZone allAppsDeleteZone = (DeleteZone)
307 mLauncher.findViewById(R.id.all_apps_delete_zone);
308 allAppsDeleteZone.setDragAndDropEnabled(true);
309
310 if ((info.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
311 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
312 } else {
313 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
314 }
315 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800316
Adam Cohencdc30d52010-12-01 15:09:47 -0800317 ApplicationInfoDropTarget allAppsInfoButton =
318 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
319 allAppsInfoButton.setDragAndDropEnabled(true);
Adam Cohencdc30d52010-12-01 15:09:47 -0800320 }
321
322 private void tearDownDragMode() {
323 post(new Runnable() {
324 // Once the drag operation has fully completed, hence the post, we want to disable the
325 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
326 // live in the workspace
327 public void run() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800328 DeleteZone allAppsDeleteZone =
329 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
Michael Jurkac31820d2011-01-16 16:57:05 -0800330 // if onDestroy was called on Launcher, we might have already deleted the
331 // all apps delete zone / info button, so check if they are null
332 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
Michael Jurkab8e14472010-12-20 16:06:10 -0800333
Adam Cohencdc30d52010-12-01 15:09:47 -0800334 ApplicationInfoDropTarget allAppsInfoButton =
335 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
Michael Jurkac31820d2011-01-16 16:57:05 -0800336 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800337 }
338 });
339 resetCheckedGrandchildren();
340 mDragController.removeDropTarget(this);
341 }
342
Winson Chung321e9ee2010-08-09 13:37:56 -0700343 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800344 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800345 if (!v.isInTouchMode()) return false;
346 if (!super.beginDragging(v)) return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700347
348 ApplicationInfo app = (ApplicationInfo) v.getTag();
349 app = new ApplicationInfo(app);
350
Patrick Dubroycd953712011-02-28 15:16:42 -0800351 // Start drag mode after the item is selected
352 setupDragMode(app);
353
Michael Jurkad3ef3062010-11-23 16:23:58 -0800354 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800355 final TextView tv = (TextView) v;
356 final Drawable icon = tv.getCompoundDrawables()[1];
357 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Michael Jurkad3ef3062010-11-23 16:23:58 -0800358 Bitmap.Config.ARGB_8888);
359 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800360 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800361 icon.draw(c);
Winson Chungcd4bc492010-12-09 18:52:32 -0800362
363 // We toggle the checked state _after_ we create the view for the drag in case toggling the
364 // checked state changes the view's look
365 if (v instanceof Checkable) {
366 // In preparation for drag, we always reset the checked grand children regardless of
367 // what choice mode we are in
368 resetCheckedGrandchildren();
369
370 // Toggle the selection on the dragged app
371 Checkable checkable = (Checkable) v;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800372
373 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
374 // of the drag of the item. (The fade-in will occur when all checked states are
375 // disabled when dragging ends)
Winson Chungcd4bc492010-12-09 18:52:32 -0800376 checkable.toggle();
377 }
378
379 // Start the drag
Winson Chung400438b2011-01-16 17:53:48 -0800380 mLauncher.lockScreenOrientation();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800381 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
382 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
Winson Chungcd4bc492010-12-09 18:52:32 -0800383 b.recycle();
Winson Chung321e9ee2010-08-09 13:37:56 -0700384 return true;
385 }
386
387 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800388 public void onDragViewVisible() {
389 }
390
391 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800392 public void onDropCompleted(View target, Object dragInfo, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700393 // close the choice action mode if we have a proper drop
394 if (target != this) {
395 endChoiceMode();
396 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800397 tearDownDragMode();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800398 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800399 mLauncher.unlockScreenOrientation();
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 }
401
Michael Jurka12ac0d62011-02-23 11:48:32 -0800402 int getPageContentWidth() {
403 return mPageContentWidth;
404 }
405
Winson Chung321e9ee2010-08-09 13:37:56 -0700406 @Override
407 public boolean isVisible() {
408 return mZoom > 0.001f;
409 }
410
411 @Override
412 public boolean isAnimating() {
413 return (getAnimation() != null);
414 }
415
416 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
417 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
418 if (mAppFilter == ALL_APPS_FLAG) {
419 return apps;
420 } else {
421 final int length = apps.size();
422 for (int i = 0; i < length; ++i) {
423 ApplicationInfo info = apps.get(i);
424 if ((info.flags & mAppFilter) > 0) {
425 filteredApps.add(info);
426 }
427 }
Winson Chung78403fe2011-01-21 15:38:02 -0800428 Collections.sort(filteredApps, LauncherModel.APP_INSTALL_TIME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700429 }
430 return filteredApps;
431 }
432
433 @Override
434 public void setApps(ArrayList<ApplicationInfo> list) {
435 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700436 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700437 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung04998342011-01-05 13:54:43 -0800438 mPageViewIconCache.retainAllApps(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700439 invalidatePageData();
440 }
441
Winson Chung80baf5a2010-08-09 16:03:15 -0700442 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
443 // we add it in place, in alphabetical order
444 final int count = list.size();
445 for (int i = 0; i < count; ++i) {
446 final ApplicationInfo info = list.get(i);
447 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
448 if (index < 0) {
449 mApps.add(-(index + 1), info);
Winson Chung452821f2011-01-14 16:39:47 -0800450 } else {
451 mApps.add(index, info);
Winson Chung80baf5a2010-08-09 16:03:15 -0700452 }
453 }
454 mFilteredApps = rebuildFilteredApps(mApps);
455 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 @Override
457 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700458 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 invalidatePageData();
460 }
461
Winson Chung80baf5a2010-08-09 16:03:15 -0700462 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung10fefb12010-11-01 11:57:06 -0700463 // End the choice mode if any of the items in the list that are being removed are
464 // currently selected
465 ArrayList<Checkable> checkedList = getCheckedGrandchildren();
466 HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>();
467 for (Checkable checked : checkedList) {
468 PagedViewIcon icon = (PagedViewIcon) checked;
469 checkedAppInfos.add((ApplicationInfo) icon.getTag());
470 }
471 for (ApplicationInfo info : list) {
472 if (checkedAppInfos.contains(info)) {
473 endChoiceMode();
474 break;
475 }
476 }
477
478 // Loop through all the apps and remove apps that have the same component
Winson Chung321e9ee2010-08-09 13:37:56 -0700479 final int length = list.size();
480 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700481 final ApplicationInfo info = list.get(i);
482 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 if (removeIndex > -1) {
484 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800485 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 }
487 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700488 mFilteredApps = rebuildFilteredApps(mApps);
489 }
Michael Jurkaabded662011-03-04 12:06:57 -0800490
Winson Chung80baf5a2010-08-09 16:03:15 -0700491 @Override
492 public void removeApps(ArrayList<ApplicationInfo> list) {
493 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700494 invalidatePageData();
495 }
496
497 @Override
498 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700499 removeAppsWithoutInvalidate(list);
500 addAppsWithoutInvalidate(list);
501 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 }
503
504 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
Winson Chung8b534782011-02-23 13:43:59 -0800505 if (item != null && item.intent != null) {
506 ComponentName removeComponent = item.intent.getComponent();
507 final int length = list.size();
508 for (int i = 0; i < length; ++i) {
509 ApplicationInfo info = list.get(i);
510 if (info.intent.getComponent().equals(removeComponent)) {
511 return i;
512 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700513 }
514 }
515 return -1;
516 }
517
518 @Override
519 public void dumpState() {
520 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
521 }
522
523 @Override
524 public void surrender() {
525 // do nothing?
526 }
527
Michael Jurka12ac0d62011-02-23 11:48:32 -0800528 private void setupPage(PagedViewCellLayout layout) {
529 layout.setCellCount(mCellCountX, mCellCountY);
530 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
531 mPageLayoutPaddingBottom);
532 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
533 }
534
Winson Chung321e9ee2010-08-09 13:37:56 -0700535 @Override
536 public void syncPages() {
Patrick Dubroye61c6a32011-05-19 16:48:48 -0700537 if (mCellCountX <= 0 || mCellCountY <= 0) {
Michael Jurkaea2daff2011-05-17 18:21:03 -0700538 // We don't know our size yet, which means we haven't calculated cell count x/y;
539 // onMeasure will call us once we figure out our size
540 return;
541 }
Winson Chung96785572010-10-14 13:37:13 -0700542 // ensure that we have the right number of pages (min of 1, since we have placeholders)
543 int numPages = Math.max(1,
544 (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)));
Winson Chung321e9ee2010-08-09 13:37:56 -0700545 int curNumPages = getChildCount();
546 // remove any extra pages after the "last" page
547 int extraPageDiff = curNumPages - numPages;
548 for (int i = 0; i < extraPageDiff; ++i) {
549 removeViewAt(numPages);
550 }
551 // add any necessary pages
552 for (int i = curNumPages; i < numPages; ++i) {
553 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
Michael Jurkaabded662011-03-04 12:06:57 -0800554 if (mAllowHardwareLayerCreation) {
555 layout.allowHardwareLayerCreation();
556 }
Michael Jurka12ac0d62011-02-23 11:48:32 -0800557 setupPage(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -0700558 addView(layout);
559 }
560
561 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700562 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700563 }
564
565 @Override
566 public void syncPageItems(int page) {
Winson Chung96785572010-10-14 13:37:13 -0700567 // Ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700568 final int cellsPerPage = mCellCountX * mCellCountY;
569 final int startIndex = page * cellsPerPage;
570 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700571 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700572
Winson Chung96785572010-10-14 13:37:13 -0700573 if (!mFilteredApps.isEmpty()) {
Michael Jurka8245a862011-02-01 17:53:59 -0800574 int curNumPageItems = layout.getPageChildCount();
Winson Chung96785572010-10-14 13:37:13 -0700575 int numPageItems = endIndex - startIndex;
Winson Chung80baf5a2010-08-09 16:03:15 -0700576
Winson Chung96785572010-10-14 13:37:13 -0700577 // If we were previously an empty page, then restart anew
578 boolean wasEmptyPage = false;
579 if (curNumPageItems == 1) {
Michael Jurka8245a862011-02-01 17:53:59 -0800580 View icon = layout.getChildOnPageAt(0);
Winson Chung96785572010-10-14 13:37:13 -0700581 if (icon.getTag() == null) {
582 wasEmptyPage = true;
583 }
584 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700585
Winson Chung96785572010-10-14 13:37:13 -0700586 if (wasEmptyPage) {
587 // Remove all the previous items
588 curNumPageItems = 0;
Michael Jurka8245a862011-02-01 17:53:59 -0800589 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700590 } else {
591 // Remove any extra items
592 int extraPageItemsDiff = curNumPageItems - numPageItems;
593 for (int i = 0; i < extraPageItemsDiff; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800594 layout.removeViewOnPageAt(numPageItems);
Winson Chung96785572010-10-14 13:37:13 -0700595 }
596 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700597
Winson Chung96785572010-10-14 13:37:13 -0700598 // Add any necessary items
599 for (int i = curNumPageItems; i < numPageItems; ++i) {
600 TextView text = (TextView) mInflater.inflate(
601 R.layout.all_apps_paged_view_application, layout, false);
602 text.setOnClickListener(this);
603 text.setOnLongClickListener(this);
Michael Jurka72b079e2010-12-10 01:03:53 -0800604 text.setOnTouchListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700605
Winson Chung96785572010-10-14 13:37:13 -0700606 layout.addViewToCellLayout(text, -1, i,
607 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
608 }
609
610 // Actually reapply to the existing text views
Winson Chung04998342011-01-05 13:54:43 -0800611 final int numPages = getPageCount();
Winson Chung96785572010-10-14 13:37:13 -0700612 for (int i = startIndex; i < endIndex; ++i) {
613 final int index = i - startIndex;
614 final ApplicationInfo info = mFilteredApps.get(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800615 PagedViewIcon icon = (PagedViewIcon) layout.getChildOnPageAt(index);
Winson Chung04998342011-01-05 13:54:43 -0800616 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chung96785572010-10-14 13:37:13 -0700617
618 PagedViewCellLayout.LayoutParams params =
619 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
620 params.cellX = index % mCellCountX;
621 params.cellY = index / mCellCountX;
622 }
623
624 // Default to left-aligned icons
625 layout.enableCenteredContent(false);
626 } else {
627 // There are no items, so show the user a small message
628 TextView icon = (TextView) mInflater.inflate(
629 R.layout.all_apps_no_items_placeholder, layout, false);
630 switch (mAppFilter) {
Winson Chung96785572010-10-14 13:37:13 -0700631 case ApplicationInfo.DOWNLOADED_FLAG:
632 icon.setText(mContext.getString(R.string.all_apps_no_downloads));
633 break;
634 default: break;
635 }
636
637 // Center-align the message
638 layout.enableCenteredContent(true);
Michael Jurka8245a862011-02-01 17:53:59 -0800639 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700640 layout.addViewToCellLayout(icon, -1, 0,
Winson Chung532a52a2011-01-18 12:18:00 -0800641 new PagedViewCellLayout.LayoutParams(0, 0, 4, 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700642 }
Michael Jurkac5e49022011-02-16 12:04:02 -0800643 layout.createHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700644 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700645
646 /*
647 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
648 * to the workspace.
649 */
650 @Override
651 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
652 DragView dragView, Object dragInfo) {
653 return false;
654 }
655 @Override
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700656 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
657 int yOffset, DragView dragView, Object dragInfo) {
658 return null;
659 }
660 @Override
661 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
662 DragView dragView, Object dragInfo) {}
663 @Override
664 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
665 DragView dragView, Object dragInfo) {}
666 @Override
667 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
668 DragView dragView, Object dragInfo) {}
669 @Override
670 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
671 DragView dragView, Object dragInfo) {}
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672
673 public boolean isDropEnabled() {
674 return true;
675 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700676}