blob: b9b38c3d650df0e5512a55baf5f3f38b345a8203 [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
Patrick Dubroydea9e932010-09-22 15:04:29 -070068
Winson Chung321e9ee2010-08-09 13:37:56 -070069 public AllAppsPagedView(Context context) {
70 this(context, null);
71 }
72
73 public AllAppsPagedView(Context context, AttributeSet attrs) {
74 this(context, attrs, 0);
75 }
76
77 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
78 super(context, attrs, defStyle);
79 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
80 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
81 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
82 mInflater = LayoutInflater.from(context);
Winson Chung8b534782011-02-23 13:43:59 -080083 mApps = new ArrayList<ApplicationInfo>();
84 mFilteredApps = new ArrayList<ApplicationInfo>();
Winson Chung321e9ee2010-08-09 13:37:56 -070085 a.recycle();
86 setSoundEffectsEnabled(false);
Michael Jurka72b079e2010-12-10 01:03:53 -080087
88 Resources r = context.getResources();
89 setDragSlopeThreshold(
90 r.getInteger(R.integer.config_allAppsDrawerDragSlopeThreshold) / 100.0f);
Winson Chung321e9ee2010-08-09 13:37:56 -070091 }
92
93 @Override
Winson Chung7da10252010-10-28 16:07:04 -070094 protected void init() {
95 super.init();
96 mCenterPagesVertically = false;
97 }
98
Michael Jurkaabded662011-03-04 12:06:57 -080099 void allowHardwareLayerCreation() {
100 // This is called after the first time we launch into All Apps. Before that point,
101 // there's no need for hardware layers here since there's a hardware layer set on the
102 // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here
103 // before the animation is done slows down the animation
104 if (mAllowHardwareLayerCreation) {
105 return;
106 }
107 mAllowHardwareLayerCreation = true;
108 int childCount = getChildCount();
109 for (int i = 0; i < childCount; i++) {
110 PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(i);
111 page.allowHardwareLayerCreation();
112 }
113 }
114
Winson Chung7da10252010-10-28 16:07:04 -0700115 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700116 public void setLauncher(Launcher launcher) {
117 mLauncher = launcher;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700118 mLauncher.setAllAppsPagedView(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700119 }
120
121 @Override
122 public void setDragController(DragController dragger) {
123 mDragController = dragger;
124 }
125
126 public void setAppFilter(int filterType) {
127 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700128 if (mApps != null) {
129 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700130 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700131 invalidatePageData();
132 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700133 }
134
135 @Override
136 public void zoom(float zoom, boolean animate) {
137 mZoom = zoom;
138 cancelLongPress();
139
140 if (isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700141 if (animate) {
142 startAnimation(AnimationUtils.loadAnimation(getContext(),
143 R.anim.all_apps_2d_fade_in));
144 } else {
145 onAnimationEnd();
146 }
147 } else {
148 if (animate) {
149 startAnimation(AnimationUtils.loadAnimation(getContext(),
150 R.anim.all_apps_2d_fade_out));
151 } else {
152 onAnimationEnd();
153 }
154 }
155 }
156
157 protected void onAnimationEnd() {
158 if (!isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700160
161 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 } else {
163 mZoom = 1.0f;
164 }
165
166 if (mLauncher != null)
167 mLauncher.zoomed(mZoom);
168 }
169
170 private int getChildIndexForGrandChild(View v) {
171 final int childCount = getChildCount();
172 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800173 final Page layout = (Page) getChildAt(i);
174 if (layout.indexOfChildOnPage(v) > -1) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700175 return i;
176 }
177 }
178 return -1;
179 }
180
181 @Override
182 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700183 // if we are already in a choice mode, then just change the selection
184 if (v instanceof Checkable) {
185 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700186 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700187 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700188 // Uncheck all the other grandchildren, and toggle the clicked one
189 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700190 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700191 c.setChecked(!wasChecked);
192 } else {
193 c.toggle();
194 }
195 if (getCheckedGrandchildren().size() == 0) {
196 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700197 }
198
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700199 return;
200 }
201 }
202
203 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700204 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700205 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700206 final ApplicationInfo app = (ApplicationInfo) v.getTag();
207
Winson Chung80baf5a2010-08-09 16:03:15 -0700208 // animate some feedback to the click
209 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700210 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700211 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700212 mLauncher.startActivitySafely(app.intent, app);
213 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700215
216 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700217 }
218 }
219
Patrick Dubroycd953712011-02-28 15:16:42 -0800220 private void setupDragMode(ApplicationInfo info) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800221 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE);
Patrick Dubroycd953712011-02-28 15:16:42 -0800222
223 // Only show the uninstall button if the app is uninstallable.
224 if ((info.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
225 DeleteZone allAppsDeleteZone = (DeleteZone)
226 mLauncher.findViewById(R.id.all_apps_delete_zone);
227 allAppsDeleteZone.setDragAndDropEnabled(true);
228
229 if ((info.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
230 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
231 } else {
232 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
233 }
234 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800235
Adam Cohencdc30d52010-12-01 15:09:47 -0800236 ApplicationInfoDropTarget allAppsInfoButton =
237 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
238 allAppsInfoButton.setDragAndDropEnabled(true);
Adam Cohencdc30d52010-12-01 15:09:47 -0800239 }
240
241 private void tearDownDragMode() {
242 post(new Runnable() {
243 // Once the drag operation has fully completed, hence the post, we want to disable the
244 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
245 // live in the workspace
246 public void run() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800247 DeleteZone allAppsDeleteZone =
248 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
Michael Jurkac31820d2011-01-16 16:57:05 -0800249 // if onDestroy was called on Launcher, we might have already deleted the
250 // all apps delete zone / info button, so check if they are null
251 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
Michael Jurkab8e14472010-12-20 16:06:10 -0800252
Adam Cohencdc30d52010-12-01 15:09:47 -0800253 ApplicationInfoDropTarget allAppsInfoButton =
254 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
Michael Jurkac31820d2011-01-16 16:57:05 -0800255 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800256 }
257 });
258 resetCheckedGrandchildren();
259 mDragController.removeDropTarget(this);
260 }
261
Winson Chung321e9ee2010-08-09 13:37:56 -0700262 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800263 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800264 if (!v.isInTouchMode()) return false;
265 if (!super.beginDragging(v)) return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700266
267 ApplicationInfo app = (ApplicationInfo) v.getTag();
268 app = new ApplicationInfo(app);
269
Patrick Dubroycd953712011-02-28 15:16:42 -0800270 // Start drag mode after the item is selected
271 setupDragMode(app);
272
Michael Jurkad3ef3062010-11-23 16:23:58 -0800273 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800274 final TextView tv = (TextView) v;
275 final Drawable icon = tv.getCompoundDrawables()[1];
276 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Michael Jurkad3ef3062010-11-23 16:23:58 -0800277 Bitmap.Config.ARGB_8888);
278 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800279 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800280 icon.draw(c);
Winson Chungcd4bc492010-12-09 18:52:32 -0800281
282 // We toggle the checked state _after_ we create the view for the drag in case toggling the
283 // checked state changes the view's look
284 if (v instanceof Checkable) {
285 // In preparation for drag, we always reset the checked grand children regardless of
286 // what choice mode we are in
287 resetCheckedGrandchildren();
288
289 // Toggle the selection on the dragged app
290 Checkable checkable = (Checkable) v;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800291
292 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
293 // of the drag of the item. (The fade-in will occur when all checked states are
294 // disabled when dragging ends)
Winson Chungcd4bc492010-12-09 18:52:32 -0800295 checkable.toggle();
296 }
297
298 // Start the drag
Winson Chung400438b2011-01-16 17:53:48 -0800299 mLauncher.lockScreenOrientation();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800300 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
301 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
Winson Chungcd4bc492010-12-09 18:52:32 -0800302 b.recycle();
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 return true;
304 }
305
306 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800307 public void onDragViewVisible() {
308 }
309
310 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800311 public void onDropCompleted(View target, Object dragInfo, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700312 // close the choice action mode if we have a proper drop
313 if (target != this) {
314 endChoiceMode();
315 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800316 tearDownDragMode();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800317 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800318 mLauncher.unlockScreenOrientation();
Winson Chung321e9ee2010-08-09 13:37:56 -0700319 }
320
321 @Override
322 public boolean isVisible() {
323 return mZoom > 0.001f;
324 }
325
326 @Override
327 public boolean isAnimating() {
328 return (getAnimation() != null);
329 }
330
331 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
332 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
333 if (mAppFilter == ALL_APPS_FLAG) {
334 return apps;
335 } else {
336 final int length = apps.size();
337 for (int i = 0; i < length; ++i) {
338 ApplicationInfo info = apps.get(i);
339 if ((info.flags & mAppFilter) > 0) {
340 filteredApps.add(info);
341 }
342 }
Winson Chung78403fe2011-01-21 15:38:02 -0800343 Collections.sort(filteredApps, LauncherModel.APP_INSTALL_TIME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700344 }
345 return filteredApps;
346 }
347
348 @Override
349 public void setApps(ArrayList<ApplicationInfo> list) {
350 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700351 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung04998342011-01-05 13:54:43 -0800353 mPageViewIconCache.retainAllApps(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700354 invalidatePageData();
355 }
356
Winson Chung80baf5a2010-08-09 16:03:15 -0700357 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
358 // we add it in place, in alphabetical order
359 final int count = list.size();
360 for (int i = 0; i < count; ++i) {
361 final ApplicationInfo info = list.get(i);
362 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
363 if (index < 0) {
364 mApps.add(-(index + 1), info);
Winson Chung452821f2011-01-14 16:39:47 -0800365 } else {
366 mApps.add(index, info);
Winson Chung80baf5a2010-08-09 16:03:15 -0700367 }
368 }
369 mFilteredApps = rebuildFilteredApps(mApps);
370 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 @Override
372 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700373 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700374 invalidatePageData();
375 }
376
Winson Chung80baf5a2010-08-09 16:03:15 -0700377 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung10fefb12010-11-01 11:57:06 -0700378 // End the choice mode if any of the items in the list that are being removed are
379 // currently selected
380 ArrayList<Checkable> checkedList = getCheckedGrandchildren();
381 HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>();
382 for (Checkable checked : checkedList) {
383 PagedViewIcon icon = (PagedViewIcon) checked;
384 checkedAppInfos.add((ApplicationInfo) icon.getTag());
385 }
386 for (ApplicationInfo info : list) {
387 if (checkedAppInfos.contains(info)) {
388 endChoiceMode();
389 break;
390 }
391 }
392
393 // Loop through all the apps and remove apps that have the same component
Winson Chung321e9ee2010-08-09 13:37:56 -0700394 final int length = list.size();
395 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700396 final ApplicationInfo info = list.get(i);
397 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700398 if (removeIndex > -1) {
399 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800400 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung321e9ee2010-08-09 13:37:56 -0700401 }
402 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700403 mFilteredApps = rebuildFilteredApps(mApps);
404 }
Michael Jurkaabded662011-03-04 12:06:57 -0800405
Winson Chung80baf5a2010-08-09 16:03:15 -0700406 @Override
407 public void removeApps(ArrayList<ApplicationInfo> list) {
408 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700409 invalidatePageData();
410 }
411
412 @Override
413 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700414 removeAppsWithoutInvalidate(list);
415 addAppsWithoutInvalidate(list);
416 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700417 }
418
419 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
Winson Chung8b534782011-02-23 13:43:59 -0800420 if (item != null && item.intent != null) {
421 ComponentName removeComponent = item.intent.getComponent();
422 final int length = list.size();
423 for (int i = 0; i < length; ++i) {
424 ApplicationInfo info = list.get(i);
425 if (info.intent.getComponent().equals(removeComponent)) {
426 return i;
427 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700428 }
429 }
430 return -1;
431 }
432
433 @Override
434 public void dumpState() {
435 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
436 }
437
438 @Override
439 public void surrender() {
440 // do nothing?
441 }
442
443 @Override
444 public void syncPages() {
Winson Chung96785572010-10-14 13:37:13 -0700445 // ensure that we have the right number of pages (min of 1, since we have placeholders)
446 int numPages = Math.max(1,
447 (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)));
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 int curNumPages = getChildCount();
449 // remove any extra pages after the "last" page
450 int extraPageDiff = curNumPages - numPages;
451 for (int i = 0; i < extraPageDiff; ++i) {
Michael Jurkaabded662011-03-04 12:06:57 -0800452 PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(numPages);
Winson Chung321e9ee2010-08-09 13:37:56 -0700453 removeViewAt(numPages);
454 }
455 // add any necessary pages
456 for (int i = curNumPages; i < numPages; ++i) {
457 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
Michael Jurkaabded662011-03-04 12:06:57 -0800458 if (mAllowHardwareLayerCreation) {
459 layout.allowHardwareLayerCreation();
460 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700461 layout.setCellCount(mCellCountX, mCellCountY);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700462 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
463 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700464 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 addView(layout);
466 }
467
468 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700469 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700470 }
471
472 @Override
473 public void syncPageItems(int page) {
Winson Chung96785572010-10-14 13:37:13 -0700474 // Ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700475 final int cellsPerPage = mCellCountX * mCellCountY;
476 final int startIndex = page * cellsPerPage;
477 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700479
Winson Chung96785572010-10-14 13:37:13 -0700480 if (!mFilteredApps.isEmpty()) {
Michael Jurka8245a862011-02-01 17:53:59 -0800481 int curNumPageItems = layout.getPageChildCount();
Winson Chung96785572010-10-14 13:37:13 -0700482 int numPageItems = endIndex - startIndex;
Winson Chung80baf5a2010-08-09 16:03:15 -0700483
Winson Chung96785572010-10-14 13:37:13 -0700484 // If we were previously an empty page, then restart anew
485 boolean wasEmptyPage = false;
486 if (curNumPageItems == 1) {
Michael Jurka8245a862011-02-01 17:53:59 -0800487 View icon = layout.getChildOnPageAt(0);
Winson Chung96785572010-10-14 13:37:13 -0700488 if (icon.getTag() == null) {
489 wasEmptyPage = true;
490 }
491 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700492
Winson Chung96785572010-10-14 13:37:13 -0700493 if (wasEmptyPage) {
494 // Remove all the previous items
495 curNumPageItems = 0;
Michael Jurka8245a862011-02-01 17:53:59 -0800496 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700497 } else {
498 // Remove any extra items
499 int extraPageItemsDiff = curNumPageItems - numPageItems;
500 for (int i = 0; i < extraPageItemsDiff; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800501 layout.removeViewOnPageAt(numPageItems);
Winson Chung96785572010-10-14 13:37:13 -0700502 }
503 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700504
Winson Chung96785572010-10-14 13:37:13 -0700505 // Add any necessary items
506 for (int i = curNumPageItems; i < numPageItems; ++i) {
507 TextView text = (TextView) mInflater.inflate(
508 R.layout.all_apps_paged_view_application, layout, false);
509 text.setOnClickListener(this);
510 text.setOnLongClickListener(this);
Michael Jurka72b079e2010-12-10 01:03:53 -0800511 text.setOnTouchListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700512
Winson Chung96785572010-10-14 13:37:13 -0700513 layout.addViewToCellLayout(text, -1, i,
514 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
515 }
516
517 // Actually reapply to the existing text views
Winson Chung04998342011-01-05 13:54:43 -0800518 final int numPages = getPageCount();
Winson Chung96785572010-10-14 13:37:13 -0700519 for (int i = startIndex; i < endIndex; ++i) {
520 final int index = i - startIndex;
521 final ApplicationInfo info = mFilteredApps.get(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800522 PagedViewIcon icon = (PagedViewIcon) layout.getChildOnPageAt(index);
Winson Chung04998342011-01-05 13:54:43 -0800523 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chung96785572010-10-14 13:37:13 -0700524
525 PagedViewCellLayout.LayoutParams params =
526 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
527 params.cellX = index % mCellCountX;
528 params.cellY = index / mCellCountX;
529 }
530
531 // Default to left-aligned icons
532 layout.enableCenteredContent(false);
533 } else {
534 // There are no items, so show the user a small message
535 TextView icon = (TextView) mInflater.inflate(
536 R.layout.all_apps_no_items_placeholder, layout, false);
537 switch (mAppFilter) {
Winson Chung96785572010-10-14 13:37:13 -0700538 case ApplicationInfo.DOWNLOADED_FLAG:
539 icon.setText(mContext.getString(R.string.all_apps_no_downloads));
540 break;
541 default: break;
542 }
543
544 // Center-align the message
545 layout.enableCenteredContent(true);
Michael Jurka8245a862011-02-01 17:53:59 -0800546 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700547 layout.addViewToCellLayout(icon, -1, 0,
Winson Chung532a52a2011-01-18 12:18:00 -0800548 new PagedViewCellLayout.LayoutParams(0, 0, 4, 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 }
Michael Jurkac5e49022011-02-16 12:04:02 -0800550 layout.createHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700551 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700552
553 /*
554 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
555 * to the workspace.
556 */
557 @Override
558 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
559 DragView dragView, Object dragInfo) {
560 return false;
561 }
562 @Override
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700563 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
564 int yOffset, DragView dragView, Object dragInfo) {
565 return null;
566 }
567 @Override
568 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
569 DragView dragView, Object dragInfo) {}
570 @Override
571 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
572 DragView dragView, Object dragInfo) {}
573 @Override
574 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
575 DragView dragView, Object dragInfo) {}
576 @Override
577 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
578 DragView dragView, Object dragInfo) {}
Michael Jurka0280c3b2010-09-17 15:00:07 -0700579
580 public boolean isDropEnabled() {
581 return true;
582 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700583}