blob: 87d255e60babc611c152d3cce8c52062862f3683 [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 Jurka7ef959b2011-02-23 11:48:32 -080068 private int mPageContentWidth;
Patrick Dubroydea9e932010-09-22 15:04:29 -070069
Winson Chung321e9ee2010-08-09 13:37:56 -070070 public AllAppsPagedView(Context context) {
71 this(context, null);
72 }
73
74 public AllAppsPagedView(Context context, AttributeSet attrs) {
75 this(context, attrs, 0);
76 }
77
78 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
79 super(context, attrs, defStyle);
80 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
81 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
82 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
83 mInflater = LayoutInflater.from(context);
Winson Chung8b534782011-02-23 13:43:59 -080084 mApps = new ArrayList<ApplicationInfo>();
85 mFilteredApps = new ArrayList<ApplicationInfo>();
Winson Chung321e9ee2010-08-09 13:37:56 -070086 a.recycle();
87 setSoundEffectsEnabled(false);
Michael Jurka72b079e2010-12-10 01:03:53 -080088
89 Resources r = context.getResources();
90 setDragSlopeThreshold(
91 r.getInteger(R.integer.config_allAppsDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7ef959b2011-02-23 11:48:32 -080092
93 // Create a dummy page and set it up to find out the content width (used by our parent)
94 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
95 setupPage(layout);
96 mPageContentWidth = layout.getContentWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -070097 }
98
99 @Override
Winson Chung7da10252010-10-28 16:07:04 -0700100 protected void init() {
101 super.init();
102 mCenterPagesVertically = false;
103 }
104
Michael Jurkaabded662011-03-04 12:06:57 -0800105 void allowHardwareLayerCreation() {
106 // This is called after the first time we launch into All Apps. Before that point,
107 // there's no need for hardware layers here since there's a hardware layer set on the
108 // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here
109 // before the animation is done slows down the animation
110 if (mAllowHardwareLayerCreation) {
111 return;
112 }
113 mAllowHardwareLayerCreation = true;
114 int childCount = getChildCount();
115 for (int i = 0; i < childCount; i++) {
116 PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(i);
117 page.allowHardwareLayerCreation();
118 }
119 }
120
Winson Chung7da10252010-10-28 16:07:04 -0700121 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700122 public void setLauncher(Launcher launcher) {
123 mLauncher = launcher;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700124 mLauncher.setAllAppsPagedView(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700125 }
126
127 @Override
128 public void setDragController(DragController dragger) {
129 mDragController = dragger;
130 }
131
132 public void setAppFilter(int filterType) {
133 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700134 if (mApps != null) {
135 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700136 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700137 invalidatePageData();
138 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700139 }
140
141 @Override
142 public void zoom(float zoom, boolean animate) {
143 mZoom = zoom;
144 cancelLongPress();
145
146 if (isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700147 if (animate) {
148 startAnimation(AnimationUtils.loadAnimation(getContext(),
149 R.anim.all_apps_2d_fade_in));
150 } else {
151 onAnimationEnd();
152 }
153 } else {
154 if (animate) {
155 startAnimation(AnimationUtils.loadAnimation(getContext(),
156 R.anim.all_apps_2d_fade_out));
157 } else {
158 onAnimationEnd();
159 }
160 }
161 }
162
163 protected void onAnimationEnd() {
164 if (!isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700165 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700166
167 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700168 } else {
169 mZoom = 1.0f;
170 }
171
172 if (mLauncher != null)
173 mLauncher.zoomed(mZoom);
174 }
175
176 private int getChildIndexForGrandChild(View v) {
177 final int childCount = getChildCount();
178 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800179 final Page layout = (Page) getChildAt(i);
180 if (layout.indexOfChildOnPage(v) > -1) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700181 return i;
182 }
183 }
184 return -1;
185 }
186
187 @Override
188 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700189 // if we are already in a choice mode, then just change the selection
190 if (v instanceof Checkable) {
191 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700192 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700193 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700194 // Uncheck all the other grandchildren, and toggle the clicked one
195 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700196 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700197 c.setChecked(!wasChecked);
198 } else {
199 c.toggle();
200 }
201 if (getCheckedGrandchildren().size() == 0) {
202 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700203 }
204
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700205 return;
206 }
207 }
208
209 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700211 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700212 final ApplicationInfo app = (ApplicationInfo) v.getTag();
213
Winson Chung80baf5a2010-08-09 16:03:15 -0700214 // animate some feedback to the click
215 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700217 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 mLauncher.startActivitySafely(app.intent, app);
219 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700221
222 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 }
224 }
225
Patrick Dubroycd953712011-02-28 15:16:42 -0800226 private void setupDragMode(ApplicationInfo info) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800227 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE);
Patrick Dubroycd953712011-02-28 15:16:42 -0800228
229 // Only show the uninstall button if the app is uninstallable.
230 if ((info.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
231 DeleteZone allAppsDeleteZone = (DeleteZone)
232 mLauncher.findViewById(R.id.all_apps_delete_zone);
233 allAppsDeleteZone.setDragAndDropEnabled(true);
234
235 if ((info.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
236 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
237 } else {
238 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
239 }
240 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800241
Adam Cohencdc30d52010-12-01 15:09:47 -0800242 ApplicationInfoDropTarget allAppsInfoButton =
243 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
244 allAppsInfoButton.setDragAndDropEnabled(true);
Adam Cohencdc30d52010-12-01 15:09:47 -0800245 }
246
247 private void tearDownDragMode() {
248 post(new Runnable() {
249 // Once the drag operation has fully completed, hence the post, we want to disable the
250 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
251 // live in the workspace
252 public void run() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800253 DeleteZone allAppsDeleteZone =
254 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
Michael Jurkac31820d2011-01-16 16:57:05 -0800255 // if onDestroy was called on Launcher, we might have already deleted the
256 // all apps delete zone / info button, so check if they are null
257 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
Michael Jurkab8e14472010-12-20 16:06:10 -0800258
Adam Cohencdc30d52010-12-01 15:09:47 -0800259 ApplicationInfoDropTarget allAppsInfoButton =
260 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
Michael Jurkac31820d2011-01-16 16:57:05 -0800261 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800262 }
263 });
264 resetCheckedGrandchildren();
265 mDragController.removeDropTarget(this);
266 }
267
Winson Chung321e9ee2010-08-09 13:37:56 -0700268 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800269 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800270 if (!v.isInTouchMode()) return false;
271 if (!super.beginDragging(v)) return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700272
273 ApplicationInfo app = (ApplicationInfo) v.getTag();
274 app = new ApplicationInfo(app);
275
Patrick Dubroycd953712011-02-28 15:16:42 -0800276 // Start drag mode after the item is selected
277 setupDragMode(app);
278
Michael Jurkad3ef3062010-11-23 16:23:58 -0800279 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800280 final TextView tv = (TextView) v;
281 final Drawable icon = tv.getCompoundDrawables()[1];
282 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Michael Jurkad3ef3062010-11-23 16:23:58 -0800283 Bitmap.Config.ARGB_8888);
284 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800285 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800286 icon.draw(c);
Winson Chungcd4bc492010-12-09 18:52:32 -0800287
288 // We toggle the checked state _after_ we create the view for the drag in case toggling the
289 // checked state changes the view's look
290 if (v instanceof Checkable) {
291 // In preparation for drag, we always reset the checked grand children regardless of
292 // what choice mode we are in
293 resetCheckedGrandchildren();
294
295 // Toggle the selection on the dragged app
296 Checkable checkable = (Checkable) v;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800297
298 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
299 // of the drag of the item. (The fade-in will occur when all checked states are
300 // disabled when dragging ends)
Winson Chungcd4bc492010-12-09 18:52:32 -0800301 checkable.toggle();
302 }
303
304 // Start the drag
Winson Chung400438b2011-01-16 17:53:48 -0800305 mLauncher.lockScreenOrientation();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800306 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
307 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
Winson Chungcd4bc492010-12-09 18:52:32 -0800308 b.recycle();
Winson Chung321e9ee2010-08-09 13:37:56 -0700309 return true;
310 }
311
312 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800313 public void onDragViewVisible() {
314 }
315
316 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800317 public void onDropCompleted(View target, Object dragInfo, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700318 // close the choice action mode if we have a proper drop
319 if (target != this) {
320 endChoiceMode();
321 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800322 tearDownDragMode();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800323 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800324 mLauncher.unlockScreenOrientation();
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 }
326
Michael Jurka7ef959b2011-02-23 11:48:32 -0800327 int getPageContentWidth() {
328 return mPageContentWidth;
329 }
330
Winson Chung321e9ee2010-08-09 13:37:56 -0700331 @Override
332 public boolean isVisible() {
333 return mZoom > 0.001f;
334 }
335
336 @Override
337 public boolean isAnimating() {
338 return (getAnimation() != null);
339 }
340
341 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
342 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
343 if (mAppFilter == ALL_APPS_FLAG) {
344 return apps;
345 } else {
346 final int length = apps.size();
347 for (int i = 0; i < length; ++i) {
348 ApplicationInfo info = apps.get(i);
349 if ((info.flags & mAppFilter) > 0) {
350 filteredApps.add(info);
351 }
352 }
Winson Chung78403fe2011-01-21 15:38:02 -0800353 Collections.sort(filteredApps, LauncherModel.APP_INSTALL_TIME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 }
355 return filteredApps;
356 }
357
358 @Override
359 public void setApps(ArrayList<ApplicationInfo> list) {
360 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700361 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700362 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung04998342011-01-05 13:54:43 -0800363 mPageViewIconCache.retainAllApps(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700364 invalidatePageData();
365 }
366
Winson Chung80baf5a2010-08-09 16:03:15 -0700367 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
368 // we add it in place, in alphabetical order
369 final int count = list.size();
370 for (int i = 0; i < count; ++i) {
371 final ApplicationInfo info = list.get(i);
372 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
373 if (index < 0) {
374 mApps.add(-(index + 1), info);
Winson Chung452821f2011-01-14 16:39:47 -0800375 } else {
376 mApps.add(index, info);
Winson Chung80baf5a2010-08-09 16:03:15 -0700377 }
378 }
379 mFilteredApps = rebuildFilteredApps(mApps);
380 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700381 @Override
382 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700383 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700384 invalidatePageData();
385 }
386
Winson Chung80baf5a2010-08-09 16:03:15 -0700387 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung10fefb12010-11-01 11:57:06 -0700388 // End the choice mode if any of the items in the list that are being removed are
389 // currently selected
390 ArrayList<Checkable> checkedList = getCheckedGrandchildren();
391 HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>();
392 for (Checkable checked : checkedList) {
393 PagedViewIcon icon = (PagedViewIcon) checked;
394 checkedAppInfos.add((ApplicationInfo) icon.getTag());
395 }
396 for (ApplicationInfo info : list) {
397 if (checkedAppInfos.contains(info)) {
398 endChoiceMode();
399 break;
400 }
401 }
402
403 // Loop through all the apps and remove apps that have the same component
Winson Chung321e9ee2010-08-09 13:37:56 -0700404 final int length = list.size();
405 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700406 final ApplicationInfo info = list.get(i);
407 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700408 if (removeIndex > -1) {
409 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800410 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung321e9ee2010-08-09 13:37:56 -0700411 }
412 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700413 mFilteredApps = rebuildFilteredApps(mApps);
414 }
Michael Jurkaabded662011-03-04 12:06:57 -0800415
Winson Chung80baf5a2010-08-09 16:03:15 -0700416 @Override
417 public void removeApps(ArrayList<ApplicationInfo> list) {
418 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700419 invalidatePageData();
420 }
421
422 @Override
423 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700424 removeAppsWithoutInvalidate(list);
425 addAppsWithoutInvalidate(list);
426 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700427 }
428
429 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
Winson Chung8b534782011-02-23 13:43:59 -0800430 if (item != null && item.intent != null) {
431 ComponentName removeComponent = item.intent.getComponent();
432 final int length = list.size();
433 for (int i = 0; i < length; ++i) {
434 ApplicationInfo info = list.get(i);
435 if (info.intent.getComponent().equals(removeComponent)) {
436 return i;
437 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700438 }
439 }
440 return -1;
441 }
442
443 @Override
444 public void dumpState() {
445 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
446 }
447
448 @Override
449 public void surrender() {
450 // do nothing?
451 }
452
Michael Jurka7ef959b2011-02-23 11:48:32 -0800453 private void setupPage(PagedViewCellLayout layout) {
454 layout.setCellCount(mCellCountX, mCellCountY);
455 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
456 mPageLayoutPaddingBottom);
457 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
458 }
459
Winson Chung321e9ee2010-08-09 13:37:56 -0700460 @Override
461 public void syncPages() {
Winson Chung96785572010-10-14 13:37:13 -0700462 // ensure that we have the right number of pages (min of 1, since we have placeholders)
463 int numPages = Math.max(1,
464 (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)));
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 int curNumPages = getChildCount();
466 // remove any extra pages after the "last" page
467 int extraPageDiff = curNumPages - numPages;
468 for (int i = 0; i < extraPageDiff; ++i) {
469 removeViewAt(numPages);
470 }
471 // add any necessary pages
472 for (int i = curNumPages; i < numPages; ++i) {
473 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
Michael Jurkaabded662011-03-04 12:06:57 -0800474 if (mAllowHardwareLayerCreation) {
475 layout.allowHardwareLayerCreation();
476 }
Michael Jurka7ef959b2011-02-23 11:48:32 -0800477 setupPage(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 addView(layout);
479 }
480
481 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700482 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 }
484
485 @Override
486 public void syncPageItems(int page) {
Winson Chung96785572010-10-14 13:37:13 -0700487 // Ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700488 final int cellsPerPage = mCellCountX * mCellCountY;
489 final int startIndex = page * cellsPerPage;
490 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700491 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700492
Winson Chung96785572010-10-14 13:37:13 -0700493 if (!mFilteredApps.isEmpty()) {
Michael Jurka8245a862011-02-01 17:53:59 -0800494 int curNumPageItems = layout.getPageChildCount();
Winson Chung96785572010-10-14 13:37:13 -0700495 int numPageItems = endIndex - startIndex;
Winson Chung80baf5a2010-08-09 16:03:15 -0700496
Winson Chung96785572010-10-14 13:37:13 -0700497 // If we were previously an empty page, then restart anew
498 boolean wasEmptyPage = false;
499 if (curNumPageItems == 1) {
Michael Jurka8245a862011-02-01 17:53:59 -0800500 View icon = layout.getChildOnPageAt(0);
Winson Chung96785572010-10-14 13:37:13 -0700501 if (icon.getTag() == null) {
502 wasEmptyPage = true;
503 }
504 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700505
Winson Chung96785572010-10-14 13:37:13 -0700506 if (wasEmptyPage) {
507 // Remove all the previous items
508 curNumPageItems = 0;
Michael Jurka8245a862011-02-01 17:53:59 -0800509 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700510 } else {
511 // Remove any extra items
512 int extraPageItemsDiff = curNumPageItems - numPageItems;
513 for (int i = 0; i < extraPageItemsDiff; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800514 layout.removeViewOnPageAt(numPageItems);
Winson Chung96785572010-10-14 13:37:13 -0700515 }
516 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700517
Winson Chung96785572010-10-14 13:37:13 -0700518 // Add any necessary items
519 for (int i = curNumPageItems; i < numPageItems; ++i) {
520 TextView text = (TextView) mInflater.inflate(
521 R.layout.all_apps_paged_view_application, layout, false);
522 text.setOnClickListener(this);
523 text.setOnLongClickListener(this);
Michael Jurka72b079e2010-12-10 01:03:53 -0800524 text.setOnTouchListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700525
Winson Chung96785572010-10-14 13:37:13 -0700526 layout.addViewToCellLayout(text, -1, i,
527 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
528 }
529
530 // Actually reapply to the existing text views
Winson Chung04998342011-01-05 13:54:43 -0800531 final int numPages = getPageCount();
Winson Chung96785572010-10-14 13:37:13 -0700532 for (int i = startIndex; i < endIndex; ++i) {
533 final int index = i - startIndex;
534 final ApplicationInfo info = mFilteredApps.get(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800535 PagedViewIcon icon = (PagedViewIcon) layout.getChildOnPageAt(index);
Winson Chung04998342011-01-05 13:54:43 -0800536 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chung96785572010-10-14 13:37:13 -0700537
538 PagedViewCellLayout.LayoutParams params =
539 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
540 params.cellX = index % mCellCountX;
541 params.cellY = index / mCellCountX;
542 }
543
544 // Default to left-aligned icons
545 layout.enableCenteredContent(false);
546 } else {
547 // There are no items, so show the user a small message
548 TextView icon = (TextView) mInflater.inflate(
549 R.layout.all_apps_no_items_placeholder, layout, false);
550 switch (mAppFilter) {
Winson Chung96785572010-10-14 13:37:13 -0700551 case ApplicationInfo.DOWNLOADED_FLAG:
552 icon.setText(mContext.getString(R.string.all_apps_no_downloads));
553 break;
554 default: break;
555 }
556
557 // Center-align the message
558 layout.enableCenteredContent(true);
Michael Jurka8245a862011-02-01 17:53:59 -0800559 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700560 layout.addViewToCellLayout(icon, -1, 0,
Winson Chung532a52a2011-01-18 12:18:00 -0800561 new PagedViewCellLayout.LayoutParams(0, 0, 4, 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700562 }
Michael Jurkac5e49022011-02-16 12:04:02 -0800563 layout.createHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700564 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700565
566 /*
567 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
568 * to the workspace.
569 */
570 @Override
571 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
572 DragView dragView, Object dragInfo) {
573 return false;
574 }
575 @Override
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700576 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
577 int yOffset, DragView dragView, Object dragInfo) {
578 return null;
579 }
580 @Override
581 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
582 DragView dragView, Object dragInfo) {}
583 @Override
584 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
585 DragView dragView, Object dragInfo) {}
586 @Override
587 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
588 DragView dragView, Object dragInfo) {}
589 @Override
590 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
591 DragView dragView, Object dragInfo) {}
Michael Jurka0280c3b2010-09-17 15:00:07 -0700592
593 public boolean isDropEnabled() {
594 return true;
595 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700596}