blob: 1e4821aa8fa81b4dcde44b9be59283f02d215384 [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;
66
Patrick Dubroydea9e932010-09-22 15:04:29 -070067
Winson Chung321e9ee2010-08-09 13:37:56 -070068 public AllAppsPagedView(Context context) {
69 this(context, null);
70 }
71
72 public AllAppsPagedView(Context context, AttributeSet attrs) {
73 this(context, attrs, 0);
74 }
75
76 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
77 super(context, attrs, defStyle);
78 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
79 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
80 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
81 mInflater = LayoutInflater.from(context);
82 a.recycle();
83 setSoundEffectsEnabled(false);
Michael Jurka72b079e2010-12-10 01:03:53 -080084
85 Resources r = context.getResources();
86 setDragSlopeThreshold(
87 r.getInteger(R.integer.config_allAppsDrawerDragSlopeThreshold) / 100.0f);
Winson Chung321e9ee2010-08-09 13:37:56 -070088 }
89
90 @Override
Winson Chung7da10252010-10-28 16:07:04 -070091 protected void init() {
92 super.init();
93 mCenterPagesVertically = false;
94 }
95
96 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -070097 public void setLauncher(Launcher launcher) {
98 mLauncher = launcher;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -070099 mLauncher.setAllAppsPagedView(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700100 }
101
102 @Override
103 public void setDragController(DragController dragger) {
104 mDragController = dragger;
105 }
106
107 public void setAppFilter(int filterType) {
108 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700109 if (mApps != null) {
110 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700111 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700112 invalidatePageData();
113 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700114 }
115
116 @Override
117 public void zoom(float zoom, boolean animate) {
118 mZoom = zoom;
119 cancelLongPress();
120
121 if (isVisible()) {
122 getParent().bringChildToFront(this);
123 setVisibility(View.VISIBLE);
124 if (animate) {
125 startAnimation(AnimationUtils.loadAnimation(getContext(),
126 R.anim.all_apps_2d_fade_in));
127 } else {
128 onAnimationEnd();
129 }
130 } else {
131 if (animate) {
132 startAnimation(AnimationUtils.loadAnimation(getContext(),
133 R.anim.all_apps_2d_fade_out));
134 } else {
135 onAnimationEnd();
136 }
137 }
138 }
139
140 protected void onAnimationEnd() {
141 if (!isVisible()) {
142 setVisibility(View.GONE);
143 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700144
145 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700146 } else {
147 mZoom = 1.0f;
148 }
149
150 if (mLauncher != null)
151 mLauncher.zoomed(mZoom);
152 }
153
154 private int getChildIndexForGrandChild(View v) {
155 final int childCount = getChildCount();
156 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800157 final Page layout = (Page) getChildAt(i);
158 if (layout.indexOfChildOnPage(v) > -1) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 return i;
160 }
161 }
162 return -1;
163 }
164
165 @Override
166 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700167 // if we are already in a choice mode, then just change the selection
168 if (v instanceof Checkable) {
169 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700170 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700171 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700172 // Uncheck all the other grandchildren, and toggle the clicked one
173 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700174 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700175 c.setChecked(!wasChecked);
176 } else {
177 c.toggle();
178 }
179 if (getCheckedGrandchildren().size() == 0) {
180 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700181 }
182
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700183 return;
184 }
185 }
186
187 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700188 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700189 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700190 final ApplicationInfo app = (ApplicationInfo) v.getTag();
191
Winson Chung80baf5a2010-08-09 16:03:15 -0700192 // animate some feedback to the click
193 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700194 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700195 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700196 mLauncher.startActivitySafely(app.intent, app);
197 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700198 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700199
200 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700201 }
202 }
203
Adam Cohencdc30d52010-12-01 15:09:47 -0800204 private void setupDragMode() {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800205 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE);
Michael Jurkab8e14472010-12-20 16:06:10 -0800206 DeleteZone allAppsDeleteZone = (DeleteZone)
207 mLauncher.findViewById(R.id.all_apps_delete_zone);
208 allAppsDeleteZone.setDragAndDropEnabled(true);
209
Adam Cohencdc30d52010-12-01 15:09:47 -0800210 ApplicationInfoDropTarget allAppsInfoButton =
211 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
212 allAppsInfoButton.setDragAndDropEnabled(true);
Adam Cohencdc30d52010-12-01 15:09:47 -0800213 }
214
215 private void tearDownDragMode() {
216 post(new Runnable() {
217 // Once the drag operation has fully completed, hence the post, we want to disable the
218 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
219 // live in the workspace
220 public void run() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800221 DeleteZone allAppsDeleteZone =
222 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
Michael Jurkac31820d2011-01-16 16:57:05 -0800223 // if onDestroy was called on Launcher, we might have already deleted the
224 // all apps delete zone / info button, so check if they are null
225 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
Michael Jurkab8e14472010-12-20 16:06:10 -0800226
Adam Cohencdc30d52010-12-01 15:09:47 -0800227 ApplicationInfoDropTarget allAppsInfoButton =
228 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
Michael Jurkac31820d2011-01-16 16:57:05 -0800229 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800230 }
231 });
232 resetCheckedGrandchildren();
233 mDragController.removeDropTarget(this);
234 }
235
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800237 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800238 if (!v.isInTouchMode()) return false;
239 if (!super.beginDragging(v)) return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700240
Adam Cohencdc30d52010-12-01 15:09:47 -0800241 // Start drag mode after the item is selected
242 setupDragMode();
Patrick Dubroy430c53b2010-09-08 16:01:19 -0700243
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 ApplicationInfo app = (ApplicationInfo) v.getTag();
245 app = new ApplicationInfo(app);
246
Michael Jurkad3ef3062010-11-23 16:23:58 -0800247 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800248 final TextView tv = (TextView) v;
249 final Drawable icon = tv.getCompoundDrawables()[1];
250 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Michael Jurkad3ef3062010-11-23 16:23:58 -0800251 Bitmap.Config.ARGB_8888);
252 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800253 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800254 icon.draw(c);
Winson Chungcd4bc492010-12-09 18:52:32 -0800255
256 // We toggle the checked state _after_ we create the view for the drag in case toggling the
257 // checked state changes the view's look
258 if (v instanceof Checkable) {
259 // In preparation for drag, we always reset the checked grand children regardless of
260 // what choice mode we are in
261 resetCheckedGrandchildren();
262
263 // Toggle the selection on the dragged app
264 Checkable checkable = (Checkable) v;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800265
266 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
267 // of the drag of the item. (The fade-in will occur when all checked states are
268 // disabled when dragging ends)
Winson Chungcd4bc492010-12-09 18:52:32 -0800269 checkable.toggle();
270 }
271
272 // Start the drag
Winson Chung400438b2011-01-16 17:53:48 -0800273 mLauncher.lockScreenOrientation();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800274 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
275 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
Winson Chungcd4bc492010-12-09 18:52:32 -0800276 b.recycle();
Winson Chung321e9ee2010-08-09 13:37:56 -0700277 return true;
278 }
279
280 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800281 public void onDragViewVisible() {
282 }
283
284 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700285 public void onDropCompleted(View target, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700286 // close the choice action mode if we have a proper drop
287 if (target != this) {
288 endChoiceMode();
289 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800290 tearDownDragMode();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800291 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800292 mLauncher.unlockScreenOrientation();
Winson Chung321e9ee2010-08-09 13:37:56 -0700293 }
294
295 @Override
296 public boolean isVisible() {
297 return mZoom > 0.001f;
298 }
299
300 @Override
301 public boolean isAnimating() {
302 return (getAnimation() != null);
303 }
304
305 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
306 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
307 if (mAppFilter == ALL_APPS_FLAG) {
308 return apps;
309 } else {
310 final int length = apps.size();
311 for (int i = 0; i < length; ++i) {
312 ApplicationInfo info = apps.get(i);
313 if ((info.flags & mAppFilter) > 0) {
314 filteredApps.add(info);
315 }
316 }
Winson Chung78403fe2011-01-21 15:38:02 -0800317 Collections.sort(filteredApps, LauncherModel.APP_INSTALL_TIME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 }
319 return filteredApps;
320 }
321
322 @Override
323 public void setApps(ArrayList<ApplicationInfo> list) {
324 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700325 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700326 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung04998342011-01-05 13:54:43 -0800327 mPageViewIconCache.retainAllApps(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700328 invalidatePageData();
329 }
330
Winson Chung80baf5a2010-08-09 16:03:15 -0700331 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
332 // we add it in place, in alphabetical order
333 final int count = list.size();
334 for (int i = 0; i < count; ++i) {
335 final ApplicationInfo info = list.get(i);
336 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
337 if (index < 0) {
338 mApps.add(-(index + 1), info);
Winson Chung452821f2011-01-14 16:39:47 -0800339 } else {
340 mApps.add(index, info);
Winson Chung80baf5a2010-08-09 16:03:15 -0700341 }
342 }
343 mFilteredApps = rebuildFilteredApps(mApps);
344 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 @Override
346 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700347 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700348 invalidatePageData();
349 }
350
Winson Chung80baf5a2010-08-09 16:03:15 -0700351 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung10fefb12010-11-01 11:57:06 -0700352 // End the choice mode if any of the items in the list that are being removed are
353 // currently selected
354 ArrayList<Checkable> checkedList = getCheckedGrandchildren();
355 HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>();
356 for (Checkable checked : checkedList) {
357 PagedViewIcon icon = (PagedViewIcon) checked;
358 checkedAppInfos.add((ApplicationInfo) icon.getTag());
359 }
360 for (ApplicationInfo info : list) {
361 if (checkedAppInfos.contains(info)) {
362 endChoiceMode();
363 break;
364 }
365 }
366
367 // Loop through all the apps and remove apps that have the same component
Winson Chung321e9ee2010-08-09 13:37:56 -0700368 final int length = list.size();
369 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700370 final ApplicationInfo info = list.get(i);
371 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700372 if (removeIndex > -1) {
373 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800374 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung321e9ee2010-08-09 13:37:56 -0700375 }
376 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700377 mFilteredApps = rebuildFilteredApps(mApps);
378 }
379 @Override
380 public void removeApps(ArrayList<ApplicationInfo> list) {
381 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700382 invalidatePageData();
383 }
384
385 @Override
386 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700387 removeAppsWithoutInvalidate(list);
388 addAppsWithoutInvalidate(list);
389 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700390 }
391
392 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
393 ComponentName removeComponent = item.intent.getComponent();
394 final int length = list.size();
395 for (int i = 0; i < length; ++i) {
396 ApplicationInfo info = list.get(i);
397 if (info.intent.getComponent().equals(removeComponent)) {
398 return i;
399 }
400 }
401 return -1;
402 }
403
404 @Override
405 public void dumpState() {
406 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
407 }
408
409 @Override
410 public void surrender() {
411 // do nothing?
412 }
413
414 @Override
415 public void syncPages() {
Winson Chung96785572010-10-14 13:37:13 -0700416 // ensure that we have the right number of pages (min of 1, since we have placeholders)
417 int numPages = Math.max(1,
418 (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)));
Winson Chung321e9ee2010-08-09 13:37:56 -0700419 int curNumPages = getChildCount();
420 // remove any extra pages after the "last" page
421 int extraPageDiff = curNumPages - numPages;
422 for (int i = 0; i < extraPageDiff; ++i) {
423 removeViewAt(numPages);
424 }
425 // add any necessary pages
426 for (int i = curNumPages; i < numPages; ++i) {
427 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
Michael Jurkac0759f52011-02-03 16:47:14 -0800428 layout.enableHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700429 layout.setCellCount(mCellCountX, mCellCountY);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700430 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
431 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700432 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 addView(layout);
434 }
435
436 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700437 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700438 }
439
440 @Override
441 public void syncPageItems(int page) {
Winson Chung96785572010-10-14 13:37:13 -0700442 // Ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700443 final int cellsPerPage = mCellCountX * mCellCountY;
444 final int startIndex = page * cellsPerPage;
445 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700446 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700447
Winson Chung96785572010-10-14 13:37:13 -0700448 if (!mFilteredApps.isEmpty()) {
Michael Jurka8245a862011-02-01 17:53:59 -0800449 int curNumPageItems = layout.getPageChildCount();
Winson Chung96785572010-10-14 13:37:13 -0700450 int numPageItems = endIndex - startIndex;
Winson Chung80baf5a2010-08-09 16:03:15 -0700451
Winson Chung96785572010-10-14 13:37:13 -0700452 // If we were previously an empty page, then restart anew
453 boolean wasEmptyPage = false;
454 if (curNumPageItems == 1) {
Michael Jurka8245a862011-02-01 17:53:59 -0800455 View icon = layout.getChildOnPageAt(0);
Winson Chung96785572010-10-14 13:37:13 -0700456 if (icon.getTag() == null) {
457 wasEmptyPage = true;
458 }
459 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700460
Winson Chung96785572010-10-14 13:37:13 -0700461 if (wasEmptyPage) {
462 // Remove all the previous items
463 curNumPageItems = 0;
Michael Jurka8245a862011-02-01 17:53:59 -0800464 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700465 } else {
466 // Remove any extra items
467 int extraPageItemsDiff = curNumPageItems - numPageItems;
468 for (int i = 0; i < extraPageItemsDiff; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800469 layout.removeViewOnPageAt(numPageItems);
Winson Chung96785572010-10-14 13:37:13 -0700470 }
471 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700472
Winson Chung96785572010-10-14 13:37:13 -0700473 // Add any necessary items
474 for (int i = curNumPageItems; i < numPageItems; ++i) {
475 TextView text = (TextView) mInflater.inflate(
476 R.layout.all_apps_paged_view_application, layout, false);
477 text.setOnClickListener(this);
478 text.setOnLongClickListener(this);
Michael Jurka72b079e2010-12-10 01:03:53 -0800479 text.setOnTouchListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700480
Winson Chung96785572010-10-14 13:37:13 -0700481 layout.addViewToCellLayout(text, -1, i,
482 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
483 }
484
485 // Actually reapply to the existing text views
Winson Chung04998342011-01-05 13:54:43 -0800486 final int numPages = getPageCount();
Winson Chung96785572010-10-14 13:37:13 -0700487 for (int i = startIndex; i < endIndex; ++i) {
488 final int index = i - startIndex;
489 final ApplicationInfo info = mFilteredApps.get(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800490 PagedViewIcon icon = (PagedViewIcon) layout.getChildOnPageAt(index);
Winson Chung04998342011-01-05 13:54:43 -0800491 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chung96785572010-10-14 13:37:13 -0700492
493 PagedViewCellLayout.LayoutParams params =
494 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
495 params.cellX = index % mCellCountX;
496 params.cellY = index / mCellCountX;
497 }
498
499 // Default to left-aligned icons
500 layout.enableCenteredContent(false);
501 } else {
502 // There are no items, so show the user a small message
503 TextView icon = (TextView) mInflater.inflate(
504 R.layout.all_apps_no_items_placeholder, layout, false);
505 switch (mAppFilter) {
Winson Chung96785572010-10-14 13:37:13 -0700506 case ApplicationInfo.DOWNLOADED_FLAG:
507 icon.setText(mContext.getString(R.string.all_apps_no_downloads));
508 break;
509 default: break;
510 }
511
512 // Center-align the message
513 layout.enableCenteredContent(true);
Michael Jurka8245a862011-02-01 17:53:59 -0800514 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700515 layout.addViewToCellLayout(icon, -1, 0,
Winson Chung532a52a2011-01-18 12:18:00 -0800516 new PagedViewCellLayout.LayoutParams(0, 0, 4, 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700517 }
Michael Jurkac5e49022011-02-16 12:04:02 -0800518 layout.createHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700519 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700520
521 /*
522 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
523 * to the workspace.
524 */
525 @Override
526 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
527 DragView dragView, Object dragInfo) {
528 return false;
529 }
530 @Override
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700531 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
532 int yOffset, DragView dragView, Object dragInfo) {
533 return null;
534 }
535 @Override
536 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
537 DragView dragView, Object dragInfo) {}
538 @Override
539 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
540 DragView dragView, Object dragInfo) {}
541 @Override
542 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
543 DragView dragView, Object dragInfo) {}
544 @Override
545 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
546 DragView dragView, Object dragInfo) {}
Michael Jurka0280c3b2010-09-17 15:00:07 -0700547
548 public boolean isDropEnabled() {
549 return true;
550 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700551}