blob: 5c8812df3eafcb52d5562676a5756c5db374549a [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
Winson Chung321e9ee2010-08-09 13:37:56 -070019import android.content.ComponentName;
20import android.content.Context;
Michael Jurka72b079e2010-12-10 01:03:53 -080021import android.content.res.Resources;
Winson Chung321e9ee2010-08-09 13:37:56 -070022import android.content.res.TypedArray;
Michael Jurkad3ef3062010-11-23 16:23:58 -080023import android.graphics.Bitmap;
24import android.graphics.Canvas;
Adam Cohene3e27a82011-04-15 12:07:39 -070025import android.graphics.Rect;
Michael Jurkad3ef3062010-11-23 16:23:58 -080026import android.graphics.drawable.Drawable;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.util.AttributeSet;
28import android.view.LayoutInflater;
29import android.view.View;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.view.animation.AnimationUtils;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070031import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.widget.TextView;
33
Adam Cohenc0dcf592011-06-01 15:30:43 -070034import com.android.launcher.R;
35
Michael Jurkaaf91de02010-11-23 16:23:58 -080036import java.util.ArrayList;
37import java.util.Collections;
38import java.util.HashSet;
Winson Chung321e9ee2010-08-09 13:37:56 -070039
40/**
41 * An implementation of PagedView that populates the pages of the workspace
42 * with all of the user's applications.
43 */
Michael Jurka72b079e2010-12-10 01:03:53 -080044public class AllAppsPagedView extends PagedViewWithDraggableItems implements AllAppsView,
45 View.OnClickListener, DragSource, DropTarget {
Winson Chung321e9ee2010-08-09 13:37:56 -070046
47 private static final String TAG = "AllAppsPagedView";
Winson Chung321e9ee2010-08-09 13:37:56 -070048
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
Winson Chung321e9ee2010-08-09 13:37:56 -070066 private final LayoutInflater mInflater;
Michael Jurkaabded662011-03-04 12:06:57 -080067 private boolean mAllowHardwareLayerCreation;
Winson Chung321e9ee2010-08-09 13:37:56 -070068
Michael Jurka7ef959b2011-02-23 11:48:32 -080069 private int mPageContentWidth;
Winson Chung20f71112011-04-06 14:22:04 -070070 private boolean mHasMadeSuccessfulDrop;
Patrick Dubroydea9e932010-09-22 15:04:29 -070071
Patrick Dubroy244d74c2011-05-19 16:48:48 -070072 private int mLastMeasureWidth = -1;
73 private int mLastMeasureHeight = -1;
Michael Jurka87b14902011-05-25 22:13:09 -070074 private boolean mWaitingToInitPages = true;
Patrick Dubroy244d74c2011-05-19 16:48:48 -070075
Patrick Dubroy4a5ad092011-05-23 16:15:09 -070076 private int mMaxCellCountY;
77
Winson Chung321e9ee2010-08-09 13:37:56 -070078 public AllAppsPagedView(Context context) {
79 this(context, null);
80 }
81
82 public AllAppsPagedView(Context context, AttributeSet attrs) {
83 this(context, attrs, 0);
84 }
85
86 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
87 super(context, attrs, defStyle);
88 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
Winson Chung321e9ee2010-08-09 13:37:56 -070089 mInflater = LayoutInflater.from(context);
Winson Chung8b534782011-02-23 13:43:59 -080090 mApps = new ArrayList<ApplicationInfo>();
91 mFilteredApps = new ArrayList<ApplicationInfo>();
Winson Chung321e9ee2010-08-09 13:37:56 -070092 a.recycle();
93 setSoundEffectsEnabled(false);
Michael Jurka72b079e2010-12-10 01:03:53 -080094
Patrick Dubroy4a5ad092011-05-23 16:15:09 -070095 final Resources r = context.getResources();
Michael Jurka72b079e2010-12-10 01:03:53 -080096 setDragSlopeThreshold(
Winson Chung785d2eb2011-04-14 16:08:02 -070097 r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold) / 100.0f);
Patrick Dubroy4a5ad092011-05-23 16:15:09 -070098 mMaxCellCountY = r.getInteger(R.integer.all_apps_view_maxCellCountY);
Winson Chung321e9ee2010-08-09 13:37:56 -070099 }
100
101 @Override
Winson Chung7da10252010-10-28 16:07:04 -0700102 protected void init() {
103 super.init();
104 mCenterPagesVertically = false;
105 }
106
Michael Jurka4c6016f2011-05-17 18:21:03 -0700107 @Override
108 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Patrick Dubroy244d74c2011-05-19 16:48:48 -0700109 final int width = MeasureSpec.getSize(widthMeasureSpec);
110 final int height = MeasureSpec.getSize(heightMeasureSpec);
Michael Jurka4c6016f2011-05-17 18:21:03 -0700111
Patrick Dubroy244d74c2011-05-19 16:48:48 -0700112 if (mLastMeasureWidth != width || mLastMeasureHeight != height) {
Michael Jurka4c6016f2011-05-17 18:21:03 -0700113 // Create a dummy page and set it up to find out the content width (used by our parent)
114 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
115 setupPage(layout);
116 mPageContentWidth = layout.getContentWidth();
Patrick Dubroy244d74c2011-05-19 16:48:48 -0700117
118 mCellCountX = determineCellCountX(width, layout);
119 mCellCountY = determineCellCountY(height, layout);
120 mLastMeasureWidth = width;
121 mLastMeasureHeight = height;
Michael Jurka983e3fd2011-05-26 17:10:29 -0700122 postInvalidatePageData(true);
Michael Jurka4c6016f2011-05-17 18:21:03 -0700123 }
Patrick Dubroy244d74c2011-05-19 16:48:48 -0700124 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
125 }
126
127 @Override
128 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurka87b14902011-05-25 22:13:09 -0700129 if (mWaitingToInitPages) {
130 mWaitingToInitPages = false;
Michael Jurka983e3fd2011-05-26 17:10:29 -0700131 postInvalidatePageData(false);
Patrick Dubroy244d74c2011-05-19 16:48:48 -0700132 }
133 super.onLayout(changed, left, top, right, bottom);
134 }
135
136 private int determineCellCountX(int availableWidth, PagedViewCellLayout layout) {
137 int cellCountX = 0;
138 final int cellWidth = layout.getCellWidth();
139
140 // Subtract padding for current page and adjacent pages
141 availableWidth -= mPageLayoutPaddingLeft * 2 + mPageLayoutPaddingRight * 2;
142
143 availableWidth -= cellWidth; // Assume at least one column
144 cellCountX = 1 + availableWidth / (cellWidth + mPageLayoutWidthGap);
145 availableWidth = availableWidth % (cellWidth + mPageLayoutWidthGap);
146
147 // Ensures that we show at least 30% of the holo icons on each side
148 final int minLeftoverWidth = (int) (cellWidth * 0.6f);
149
150 // Reserve room for the holo outlines
151 if (cellCountX <= 4) {
152 // When we're really tight on space, just pack the icons a bit closer together
153 final int missingWidth = minLeftoverWidth - availableWidth;
154 if (missingWidth > 0) {
155 mPageLayoutWidthGap -= Math.ceil(missingWidth * 1.0f / (cellCountX - 1));
156 }
157 } else {
158 if (cellCountX >= 8) {
159 // Carve out a few extra columns for very large widths
160 cellCountX = (int) (cellCountX * 0.9f);
161 } else if (availableWidth < minLeftoverWidth) {
162 cellCountX -= 1;
163 }
164 }
165 return cellCountX;
166 }
167
168 private int determineCellCountY(int availableHeight, PagedViewCellLayout layout) {
169 final int cellHeight = layout.getCellHeight();
170 final int screenHeight = mLauncher.getResources().getDisplayMetrics().heightPixels;
171
172 availableHeight -= mPageLayoutPaddingTop + mPageLayoutPaddingBottom;
173 availableHeight -= cellHeight; // Assume at least one row
Michael Jurka25dfc082011-06-02 17:43:52 -0700174 Resources r = getContext().getResources();
Michael Jurka8e41ad22011-06-03 18:42:05 -0700175 float scaleFactor = r.getInteger(R.integer.config_appsCustomizeZoomScaleFactor) / 100f;
Michael Jurka25dfc082011-06-02 17:43:52 -0700176 availableHeight -= screenHeight * scaleFactor;
Patrick Dubroy4a5ad092011-05-23 16:15:09 -0700177 if (availableHeight > 0) {
178 return Math.min(mMaxCellCountY,
179 1 + availableHeight / (cellHeight + mPageLayoutHeightGap));
180 }
181 return 0;
182 }
183
184 int getCellCountX() {
185 return mCellCountX;
186 }
187
188 int getCellCountY() {
189 return mCellCountY;
Michael Jurka4c6016f2011-05-17 18:21:03 -0700190 }
191
Michael Jurkaabded662011-03-04 12:06:57 -0800192 void allowHardwareLayerCreation() {
193 // This is called after the first time we launch into All Apps. Before that point,
194 // there's no need for hardware layers here since there's a hardware layer set on the
195 // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here
196 // before the animation is done slows down the animation
197 if (mAllowHardwareLayerCreation) {
198 return;
199 }
200 mAllowHardwareLayerCreation = true;
201 int childCount = getChildCount();
202 for (int i = 0; i < childCount; i++) {
203 PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(i);
204 page.allowHardwareLayerCreation();
205 }
206 }
207
Winson Chung7da10252010-10-28 16:07:04 -0700208 @Override
Winson Chung785d2eb2011-04-14 16:08:02 -0700209 public void setup(Launcher launcher, DragController dragController) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 mLauncher = launcher;
Winson Chung785d2eb2011-04-14 16:08:02 -0700211 mDragController = dragController;
Winson Chung321e9ee2010-08-09 13:37:56 -0700212 }
213
214 public void setAppFilter(int filterType) {
215 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700216 if (mApps != null) {
217 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700218 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700219 invalidatePageData();
220 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 }
222
Winson Chung20f71112011-04-06 14:22:04 -0700223 void resetSuccessfulDropFlag() {
224 mHasMadeSuccessfulDrop = false;
225 }
226
Winson Chung321e9ee2010-08-09 13:37:56 -0700227 @Override
228 public void zoom(float zoom, boolean animate) {
229 mZoom = zoom;
230 cancelLongPress();
231
232 if (isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 if (animate) {
234 startAnimation(AnimationUtils.loadAnimation(getContext(),
235 R.anim.all_apps_2d_fade_in));
236 } else {
237 onAnimationEnd();
238 }
239 } else {
240 if (animate) {
241 startAnimation(AnimationUtils.loadAnimation(getContext(),
242 R.anim.all_apps_2d_fade_out));
243 } else {
244 onAnimationEnd();
245 }
246 }
247 }
248
249 protected void onAnimationEnd() {
250 if (!isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700251 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700252
253 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700254 } else {
255 mZoom = 1.0f;
256 }
257
258 if (mLauncher != null)
259 mLauncher.zoomed(mZoom);
260 }
261
262 private int getChildIndexForGrandChild(View v) {
263 final int childCount = getChildCount();
264 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800265 final Page layout = (Page) getChildAt(i);
266 if (layout.indexOfChildOnPage(v) > -1) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 return i;
268 }
269 }
270 return -1;
271 }
272
273 @Override
274 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700275 // if we are already in a choice mode, then just change the selection
276 if (v instanceof Checkable) {
277 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700278 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700279 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700280 // Uncheck all the other grandchildren, and toggle the clicked one
281 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700282 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700283 c.setChecked(!wasChecked);
284 } else {
285 c.toggle();
286 }
287 if (getCheckedGrandchildren().size() == 0) {
288 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700289 }
290
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700291 return;
292 }
293 }
294
295 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700296 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700297 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 final ApplicationInfo app = (ApplicationInfo) v.getTag();
299
Winson Chung80baf5a2010-08-09 16:03:15 -0700300 // animate some feedback to the click
301 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700302 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700303 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700304 mLauncher.startActivitySafely(app.intent, app);
305 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700306 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700307
308 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 }
310 }
311
Patrick Dubroycd953712011-02-28 15:16:42 -0800312 private void setupDragMode(ApplicationInfo info) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800313 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE);
Patrick Dubroycd953712011-02-28 15:16:42 -0800314
315 // Only show the uninstall button if the app is uninstallable.
316 if ((info.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
317 DeleteZone allAppsDeleteZone = (DeleteZone)
318 mLauncher.findViewById(R.id.all_apps_delete_zone);
319 allAppsDeleteZone.setDragAndDropEnabled(true);
320
321 if ((info.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
322 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
323 } else {
324 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
325 }
326 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800327
Adam Cohencdc30d52010-12-01 15:09:47 -0800328 ApplicationInfoDropTarget allAppsInfoButton =
329 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
330 allAppsInfoButton.setDragAndDropEnabled(true);
Adam Cohencdc30d52010-12-01 15:09:47 -0800331 }
332
333 private void tearDownDragMode() {
334 post(new Runnable() {
335 // Once the drag operation has fully completed, hence the post, we want to disable the
336 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
337 // live in the workspace
338 public void run() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800339 DeleteZone allAppsDeleteZone =
340 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
Michael Jurkac31820d2011-01-16 16:57:05 -0800341 // if onDestroy was called on Launcher, we might have already deleted the
342 // all apps delete zone / info button, so check if they are null
343 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
Michael Jurkab8e14472010-12-20 16:06:10 -0800344
Adam Cohencdc30d52010-12-01 15:09:47 -0800345 ApplicationInfoDropTarget allAppsInfoButton =
346 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
Michael Jurkac31820d2011-01-16 16:57:05 -0800347 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800348 }
349 });
350 resetCheckedGrandchildren();
351 mDragController.removeDropTarget(this);
352 }
353
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800355 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800356 if (!v.isInTouchMode()) return false;
357 if (!super.beginDragging(v)) return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700358
359 ApplicationInfo app = (ApplicationInfo) v.getTag();
360 app = new ApplicationInfo(app);
361
Patrick Dubroycd953712011-02-28 15:16:42 -0800362 // Start drag mode after the item is selected
363 setupDragMode(app);
364
Michael Jurkad3ef3062010-11-23 16:23:58 -0800365 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800366 final TextView tv = (TextView) v;
367 final Drawable icon = tv.getCompoundDrawables()[1];
368 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Michael Jurkad3ef3062010-11-23 16:23:58 -0800369 Bitmap.Config.ARGB_8888);
370 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800371 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800372 icon.draw(c);
Winson Chungcd4bc492010-12-09 18:52:32 -0800373
Adam Cohene3e27a82011-04-15 12:07:39 -0700374 Rect dragRect = null;
375 if (v instanceof TextView) {
376 int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size);
377 int top = v.getPaddingTop();
378 int left = (b.getWidth() - iconSize) / 2;
379 int right = left + iconSize;
380 int bottom = top + iconSize;
381 dragRect = new Rect(left, top, right, bottom);
382 }
383
Winson Chungcd4bc492010-12-09 18:52:32 -0800384 // We toggle the checked state _after_ we create the view for the drag in case toggling the
385 // checked state changes the view's look
386 if (v instanceof Checkable) {
387 // In preparation for drag, we always reset the checked grand children regardless of
388 // what choice mode we are in
389 resetCheckedGrandchildren();
390
391 // Toggle the selection on the dragged app
392 Checkable checkable = (Checkable) v;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800393
394 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
395 // of the drag of the item. (The fade-in will occur when all checked states are
396 // disabled when dragging ends)
Winson Chungcd4bc492010-12-09 18:52:32 -0800397 checkable.toggle();
398 }
399
400 // Start the drag
Winson Chung400438b2011-01-16 17:53:48 -0800401 mLauncher.lockScreenOrientation();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800402 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Adam Cohene3e27a82011-04-15 12:07:39 -0700403 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, dragRect);
Winson Chungcd4bc492010-12-09 18:52:32 -0800404 b.recycle();
Winson Chung321e9ee2010-08-09 13:37:56 -0700405 return true;
406 }
407
408 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800409 public void onDragViewVisible() {
410 }
411
412 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700413 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700414 // close the choice action mode if we have a proper drop
415 if (target != this) {
416 endChoiceMode();
417 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800418 tearDownDragMode();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800419 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800420 mLauncher.unlockScreenOrientation();
Winson Chung20f71112011-04-06 14:22:04 -0700421
422 if (!success && !mHasMadeSuccessfulDrop) {
423 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_HIDDEN);
424 } else {
425 mHasMadeSuccessfulDrop |= success;
426 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 }
428
Michael Jurka7ef959b2011-02-23 11:48:32 -0800429 int getPageContentWidth() {
430 return mPageContentWidth;
431 }
432
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 @Override
434 public boolean isVisible() {
435 return mZoom > 0.001f;
436 }
437
438 @Override
439 public boolean isAnimating() {
440 return (getAnimation() != null);
441 }
442
443 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
444 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
445 if (mAppFilter == ALL_APPS_FLAG) {
446 return apps;
447 } else {
448 final int length = apps.size();
449 for (int i = 0; i < length; ++i) {
450 ApplicationInfo info = apps.get(i);
451 if ((info.flags & mAppFilter) > 0) {
452 filteredApps.add(info);
453 }
454 }
Winson Chung78403fe2011-01-21 15:38:02 -0800455 Collections.sort(filteredApps, LauncherModel.APP_INSTALL_TIME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 }
457 return filteredApps;
458 }
459
460 @Override
461 public void setApps(ArrayList<ApplicationInfo> list) {
462 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700463 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700464 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung04998342011-01-05 13:54:43 -0800465 mPageViewIconCache.retainAllApps(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700466 invalidatePageData();
467 }
468
Winson Chung80baf5a2010-08-09 16:03:15 -0700469 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
470 // we add it in place, in alphabetical order
471 final int count = list.size();
472 for (int i = 0; i < count; ++i) {
473 final ApplicationInfo info = list.get(i);
474 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
475 if (index < 0) {
476 mApps.add(-(index + 1), info);
Winson Chung452821f2011-01-14 16:39:47 -0800477 } else {
478 mApps.add(index, info);
Winson Chung80baf5a2010-08-09 16:03:15 -0700479 }
480 }
481 mFilteredApps = rebuildFilteredApps(mApps);
482 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 @Override
484 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700485 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700486 invalidatePageData();
487 }
488
Winson Chung80baf5a2010-08-09 16:03:15 -0700489 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung10fefb12010-11-01 11:57:06 -0700490 // End the choice mode if any of the items in the list that are being removed are
491 // currently selected
492 ArrayList<Checkable> checkedList = getCheckedGrandchildren();
493 HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>();
494 for (Checkable checked : checkedList) {
495 PagedViewIcon icon = (PagedViewIcon) checked;
496 checkedAppInfos.add((ApplicationInfo) icon.getTag());
497 }
498 for (ApplicationInfo info : list) {
499 if (checkedAppInfos.contains(info)) {
500 endChoiceMode();
501 break;
502 }
503 }
504
505 // Loop through all the apps and remove apps that have the same component
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 final int length = list.size();
507 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700508 final ApplicationInfo info = list.get(i);
509 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700510 if (removeIndex > -1) {
511 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800512 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung321e9ee2010-08-09 13:37:56 -0700513 }
514 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700515 mFilteredApps = rebuildFilteredApps(mApps);
516 }
Michael Jurkaabded662011-03-04 12:06:57 -0800517
Winson Chung80baf5a2010-08-09 16:03:15 -0700518 @Override
519 public void removeApps(ArrayList<ApplicationInfo> list) {
520 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700521 invalidatePageData();
522 }
523
524 @Override
525 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700526 removeAppsWithoutInvalidate(list);
527 addAppsWithoutInvalidate(list);
528 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700529 }
530
531 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
Winson Chung8b534782011-02-23 13:43:59 -0800532 if (item != null && item.intent != null) {
533 ComponentName removeComponent = item.intent.getComponent();
534 final int length = list.size();
535 for (int i = 0; i < length; ++i) {
536 ApplicationInfo info = list.get(i);
537 if (info.intent.getComponent().equals(removeComponent)) {
538 return i;
539 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700540 }
541 }
542 return -1;
543 }
544
545 @Override
546 public void dumpState() {
547 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
548 }
549
550 @Override
551 public void surrender() {
552 // do nothing?
553 }
554
Winson Chung337cd9d2011-03-30 10:39:30 -0700555 public void reset() {
556 setCurrentPage(0);
557 invalidatePageData();
558 }
559
Michael Jurka7ef959b2011-02-23 11:48:32 -0800560 private void setupPage(PagedViewCellLayout layout) {
561 layout.setCellCount(mCellCountX, mCellCountY);
562 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
563 mPageLayoutPaddingBottom);
564 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
565 }
566
Winson Chung321e9ee2010-08-09 13:37:56 -0700567 @Override
Michael Jurka87b14902011-05-25 22:13:09 -0700568 protected void invalidatePageData() {
569 if (mWaitingToInitPages || mCellCountX <= 0 || mCellCountY <= 0) {
Michael Jurka4c6016f2011-05-17 18:21:03 -0700570 // We don't know our size yet, which means we haven't calculated cell count x/y;
571 // onMeasure will call us once we figure out our size
572 return;
573 }
Michael Jurka87b14902011-05-25 22:13:09 -0700574 super.invalidatePageData();
575 }
576
577 @Override
578 public void syncPages() {
Winson Chung96785572010-10-14 13:37:13 -0700579 // ensure that we have the right number of pages (min of 1, since we have placeholders)
580 int numPages = Math.max(1,
581 (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)));
Winson Chung321e9ee2010-08-09 13:37:56 -0700582 int curNumPages = getChildCount();
583 // remove any extra pages after the "last" page
584 int extraPageDiff = curNumPages - numPages;
585 for (int i = 0; i < extraPageDiff; ++i) {
586 removeViewAt(numPages);
587 }
588 // add any necessary pages
589 for (int i = curNumPages; i < numPages; ++i) {
590 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
Michael Jurkaabded662011-03-04 12:06:57 -0800591 if (mAllowHardwareLayerCreation) {
592 layout.allowHardwareLayerCreation();
593 }
Michael Jurka7ef959b2011-02-23 11:48:32 -0800594 setupPage(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -0700595 addView(layout);
596 }
597
598 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700599 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700600 }
601
602 @Override
603 public void syncPageItems(int page) {
Winson Chung96785572010-10-14 13:37:13 -0700604 // Ensure that we have the right number of items on the pages
Winson Chung63257c12011-05-05 17:06:13 -0700605 final int numPages = getPageCount();
Winson Chung80baf5a2010-08-09 16:03:15 -0700606 final int cellsPerPage = mCellCountX * mCellCountY;
607 final int startIndex = page * cellsPerPage;
608 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700609 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700610
Winson Chung96785572010-10-14 13:37:13 -0700611 if (!mFilteredApps.isEmpty()) {
Michael Jurka8245a862011-02-01 17:53:59 -0800612 int curNumPageItems = layout.getPageChildCount();
Winson Chung96785572010-10-14 13:37:13 -0700613 int numPageItems = endIndex - startIndex;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700614 boolean createHolographicOutlines = (numPages > 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700615
Winson Chung96785572010-10-14 13:37:13 -0700616 // If we were previously an empty page, then restart anew
617 boolean wasEmptyPage = false;
618 if (curNumPageItems == 1) {
Michael Jurka8245a862011-02-01 17:53:59 -0800619 View icon = layout.getChildOnPageAt(0);
Winson Chung96785572010-10-14 13:37:13 -0700620 if (icon.getTag() == null) {
621 wasEmptyPage = true;
622 }
623 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700624
Winson Chung96785572010-10-14 13:37:13 -0700625 if (wasEmptyPage) {
626 // Remove all the previous items
627 curNumPageItems = 0;
Michael Jurka8245a862011-02-01 17:53:59 -0800628 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700629 } else {
630 // Remove any extra items
631 int extraPageItemsDiff = curNumPageItems - numPageItems;
632 for (int i = 0; i < extraPageItemsDiff; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800633 layout.removeViewOnPageAt(numPageItems);
Winson Chung96785572010-10-14 13:37:13 -0700634 }
635 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700636
Winson Chung96785572010-10-14 13:37:13 -0700637 // Add any necessary items
638 for (int i = curNumPageItems; i < numPageItems; ++i) {
639 TextView text = (TextView) mInflater.inflate(
640 R.layout.all_apps_paged_view_application, layout, false);
641 text.setOnClickListener(this);
642 text.setOnLongClickListener(this);
Michael Jurka72b079e2010-12-10 01:03:53 -0800643 text.setOnTouchListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700644
Winson Chung96785572010-10-14 13:37:13 -0700645 layout.addViewToCellLayout(text, -1, i,
Winson Chung6a70e9f2011-05-17 16:24:49 -0700646 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
Winson Chung96785572010-10-14 13:37:13 -0700647 }
648
649 // Actually reapply to the existing text views
650 for (int i = startIndex; i < endIndex; ++i) {
651 final int index = i - startIndex;
652 final ApplicationInfo info = mFilteredApps.get(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800653 PagedViewIcon icon = (PagedViewIcon) layout.getChildOnPageAt(index);
Michael Jurkab9b8ce92011-05-05 15:05:07 -0700654 icon.applyFromApplicationInfo(
Winson Chung63257c12011-05-05 17:06:13 -0700655 info, mPageViewIconCache, true, createHolographicOutlines);
Winson Chung96785572010-10-14 13:37:13 -0700656
657 PagedViewCellLayout.LayoutParams params =
658 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
659 params.cellX = index % mCellCountX;
660 params.cellY = index / mCellCountX;
661 }
662
Winson Chung6a70e9f2011-05-17 16:24:49 -0700663 // We should try and sync all the holographic icons after adding/removing new items
664 layout.reloadHolographicIcons(createHolographicOutlines);
665
Winson Chung96785572010-10-14 13:37:13 -0700666 // Default to left-aligned icons
667 layout.enableCenteredContent(false);
668 } else {
669 // There are no items, so show the user a small message
670 TextView icon = (TextView) mInflater.inflate(
671 R.layout.all_apps_no_items_placeholder, layout, false);
672 switch (mAppFilter) {
Winson Chung96785572010-10-14 13:37:13 -0700673 case ApplicationInfo.DOWNLOADED_FLAG:
674 icon.setText(mContext.getString(R.string.all_apps_no_downloads));
675 break;
676 default: break;
677 }
678
679 // Center-align the message
Winson Chungdd259022011-05-10 15:59:42 -0700680 final boolean createHolographicOutlines = (numPages > 1);
Winson Chung96785572010-10-14 13:37:13 -0700681 layout.enableCenteredContent(true);
Michael Jurka8245a862011-02-01 17:53:59 -0800682 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700683 layout.addViewToCellLayout(icon, -1, 0,
Winson Chung6a70e9f2011-05-17 16:24:49 -0700684 new PagedViewCellLayout.LayoutParams(0, 0, 4, 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700685 }
Michael Jurkac5e49022011-02-16 12:04:02 -0800686 layout.createHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700687 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700688
689 /*
690 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
691 * to the workspace.
692 */
Adam Cohencb3382b2011-05-24 14:07:08 -0700693 public boolean acceptDrop(DragObject d) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700694 return false;
695 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700696 public DropTarget getDropTargetDelegate(DragObject d) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700697 return null;
698 }
Adam Cohencb3382b2011-05-24 14:07:08 -0700699 public void onDragEnter(DragObject d) {}
700 public void onDragExit(DragObject d) {}
701 public void onDragOver(DragObject d) {}
702 public void onDrop(DragObject d) {}
Michael Jurka0280c3b2010-09-17 15:00:07 -0700703
704 public boolean isDropEnabled() {
705 return true;
706 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700707}