blob: 1f38d8d6e776059e7ee088e00fa37af8fc53f8d3 [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;
Adam Cohene3e27a82011-04-15 12:07:39 -070027import android.graphics.Rect;
Michael Jurkad3ef3062010-11-23 16:23:58 -080028import android.graphics.drawable.Drawable;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.util.AttributeSet;
30import android.view.LayoutInflater;
31import android.view.View;
Winson Chung321e9ee2010-08-09 13:37:56 -070032import android.view.animation.AnimationUtils;
Winson Chung5f2aa4e2010-08-20 14:49:25 -070033import android.widget.Checkable;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.widget.TextView;
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;
Patrick Dubroydea9e932010-09-22 15:04:29 -070070
Winson Chung321e9ee2010-08-09 13:37:56 -070071 public AllAppsPagedView(Context context) {
72 this(context, null);
73 }
74
75 public AllAppsPagedView(Context context, AttributeSet attrs) {
76 this(context, attrs, 0);
77 }
78
79 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
80 super(context, attrs, defStyle);
81 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
82 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
83 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
84 mInflater = LayoutInflater.from(context);
Winson Chung8b534782011-02-23 13:43:59 -080085 mApps = new ArrayList<ApplicationInfo>();
86 mFilteredApps = new ArrayList<ApplicationInfo>();
Winson Chung321e9ee2010-08-09 13:37:56 -070087 a.recycle();
88 setSoundEffectsEnabled(false);
Michael Jurka72b079e2010-12-10 01:03:53 -080089
90 Resources r = context.getResources();
91 setDragSlopeThreshold(
92 r.getInteger(R.integer.config_allAppsDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7ef959b2011-02-23 11:48:32 -080093
94 // Create a dummy page and set it up to find out the content width (used by our parent)
95 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
96 setupPage(layout);
97 mPageContentWidth = layout.getContentWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -070098 }
99
100 @Override
Winson Chung7da10252010-10-28 16:07:04 -0700101 protected void init() {
102 super.init();
103 mCenterPagesVertically = false;
104 }
105
Michael Jurkaabded662011-03-04 12:06:57 -0800106 void allowHardwareLayerCreation() {
107 // This is called after the first time we launch into All Apps. Before that point,
108 // there's no need for hardware layers here since there's a hardware layer set on the
109 // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here
110 // before the animation is done slows down the animation
111 if (mAllowHardwareLayerCreation) {
112 return;
113 }
114 mAllowHardwareLayerCreation = true;
115 int childCount = getChildCount();
116 for (int i = 0; i < childCount; i++) {
117 PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(i);
118 page.allowHardwareLayerCreation();
119 }
120 }
121
Winson Chung7da10252010-10-28 16:07:04 -0700122 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700123 public void setLauncher(Launcher launcher) {
124 mLauncher = launcher;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700125 mLauncher.setAllAppsPagedView(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700126 }
127
128 @Override
129 public void setDragController(DragController dragger) {
130 mDragController = dragger;
131 }
132
133 public void setAppFilter(int filterType) {
134 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700135 if (mApps != null) {
136 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700137 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700138 invalidatePageData();
139 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700140 }
141
142 @Override
143 public void zoom(float zoom, boolean animate) {
144 mZoom = zoom;
145 cancelLongPress();
146
147 if (isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700148 if (animate) {
149 startAnimation(AnimationUtils.loadAnimation(getContext(),
150 R.anim.all_apps_2d_fade_in));
151 } else {
152 onAnimationEnd();
153 }
154 } else {
155 if (animate) {
156 startAnimation(AnimationUtils.loadAnimation(getContext(),
157 R.anim.all_apps_2d_fade_out));
158 } else {
159 onAnimationEnd();
160 }
161 }
162 }
163
164 protected void onAnimationEnd() {
165 if (!isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700166 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700167
168 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700169 } else {
170 mZoom = 1.0f;
171 }
172
173 if (mLauncher != null)
174 mLauncher.zoomed(mZoom);
175 }
176
177 private int getChildIndexForGrandChild(View v) {
178 final int childCount = getChildCount();
179 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800180 final Page layout = (Page) getChildAt(i);
181 if (layout.indexOfChildOnPage(v) > -1) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700182 return i;
183 }
184 }
185 return -1;
186 }
187
188 @Override
189 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700190 // if we are already in a choice mode, then just change the selection
191 if (v instanceof Checkable) {
192 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700193 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700194 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700195 // Uncheck all the other grandchildren, and toggle the clicked one
196 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700197 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700198 c.setChecked(!wasChecked);
199 } else {
200 c.toggle();
201 }
202 if (getCheckedGrandchildren().size() == 0) {
203 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700204 }
205
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700206 return;
207 }
208 }
209
210 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700211 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700212 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700213 final ApplicationInfo app = (ApplicationInfo) v.getTag();
214
Winson Chung80baf5a2010-08-09 16:03:15 -0700215 // animate some feedback to the click
216 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700217 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700218 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700219 mLauncher.startActivitySafely(app.intent, app);
220 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700222
223 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 }
225 }
226
Patrick Dubroycd953712011-02-28 15:16:42 -0800227 private void setupDragMode(ApplicationInfo info) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800228 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE);
Patrick Dubroycd953712011-02-28 15:16:42 -0800229
230 // Only show the uninstall button if the app is uninstallable.
231 if ((info.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
232 DeleteZone allAppsDeleteZone = (DeleteZone)
233 mLauncher.findViewById(R.id.all_apps_delete_zone);
234 allAppsDeleteZone.setDragAndDropEnabled(true);
235
236 if ((info.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
237 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
238 } else {
239 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
240 }
241 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800242
Adam Cohencdc30d52010-12-01 15:09:47 -0800243 ApplicationInfoDropTarget allAppsInfoButton =
244 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
245 allAppsInfoButton.setDragAndDropEnabled(true);
Adam Cohencdc30d52010-12-01 15:09:47 -0800246 }
247
248 private void tearDownDragMode() {
249 post(new Runnable() {
250 // Once the drag operation has fully completed, hence the post, we want to disable the
251 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
252 // live in the workspace
253 public void run() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800254 DeleteZone allAppsDeleteZone =
255 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
Michael Jurkac31820d2011-01-16 16:57:05 -0800256 // if onDestroy was called on Launcher, we might have already deleted the
257 // all apps delete zone / info button, so check if they are null
258 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
Michael Jurkab8e14472010-12-20 16:06:10 -0800259
Adam Cohencdc30d52010-12-01 15:09:47 -0800260 ApplicationInfoDropTarget allAppsInfoButton =
261 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
Michael Jurkac31820d2011-01-16 16:57:05 -0800262 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800263 }
264 });
265 resetCheckedGrandchildren();
266 mDragController.removeDropTarget(this);
267 }
268
Winson Chung321e9ee2010-08-09 13:37:56 -0700269 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800270 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800271 if (!v.isInTouchMode()) return false;
272 if (!super.beginDragging(v)) return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700273
274 ApplicationInfo app = (ApplicationInfo) v.getTag();
275 app = new ApplicationInfo(app);
276
Patrick Dubroycd953712011-02-28 15:16:42 -0800277 // Start drag mode after the item is selected
278 setupDragMode(app);
279
Michael Jurkad3ef3062010-11-23 16:23:58 -0800280 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800281 final TextView tv = (TextView) v;
282 final Drawable icon = tv.getCompoundDrawables()[1];
283 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Michael Jurkad3ef3062010-11-23 16:23:58 -0800284 Bitmap.Config.ARGB_8888);
285 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800286 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800287 icon.draw(c);
Winson Chungcd4bc492010-12-09 18:52:32 -0800288
Adam Cohene3e27a82011-04-15 12:07:39 -0700289 Rect dragRect = null;
290 if (v instanceof TextView) {
291 int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size);
292 int top = v.getPaddingTop();
293 int left = (b.getWidth() - iconSize) / 2;
294 int right = left + iconSize;
295 int bottom = top + iconSize;
296 dragRect = new Rect(left, top, right, bottom);
297 }
298
Winson Chungcd4bc492010-12-09 18:52:32 -0800299 // We toggle the checked state _after_ we create the view for the drag in case toggling the
300 // checked state changes the view's look
301 if (v instanceof Checkable) {
302 // In preparation for drag, we always reset the checked grand children regardless of
303 // what choice mode we are in
304 resetCheckedGrandchildren();
305
306 // Toggle the selection on the dragged app
307 Checkable checkable = (Checkable) v;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800308
309 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
310 // of the drag of the item. (The fade-in will occur when all checked states are
311 // disabled when dragging ends)
Winson Chungcd4bc492010-12-09 18:52:32 -0800312 checkable.toggle();
313 }
314
315 // Start the drag
Winson Chung400438b2011-01-16 17:53:48 -0800316 mLauncher.lockScreenOrientation();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800317 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Adam Cohene3e27a82011-04-15 12:07:39 -0700318 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, dragRect);
Winson Chungcd4bc492010-12-09 18:52:32 -0800319 b.recycle();
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 return true;
321 }
322
323 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800324 public void onDragViewVisible() {
325 }
326
327 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800328 public void onDropCompleted(View target, Object dragInfo, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700329 // close the choice action mode if we have a proper drop
330 if (target != this) {
331 endChoiceMode();
332 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800333 tearDownDragMode();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800334 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800335 mLauncher.unlockScreenOrientation();
Winson Chung321e9ee2010-08-09 13:37:56 -0700336 }
337
Michael Jurka7ef959b2011-02-23 11:48:32 -0800338 int getPageContentWidth() {
339 return mPageContentWidth;
340 }
341
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 @Override
343 public boolean isVisible() {
344 return mZoom > 0.001f;
345 }
346
347 @Override
348 public boolean isAnimating() {
349 return (getAnimation() != null);
350 }
351
352 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
353 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
354 if (mAppFilter == ALL_APPS_FLAG) {
355 return apps;
356 } else {
357 final int length = apps.size();
358 for (int i = 0; i < length; ++i) {
359 ApplicationInfo info = apps.get(i);
360 if ((info.flags & mAppFilter) > 0) {
361 filteredApps.add(info);
362 }
363 }
Winson Chung78403fe2011-01-21 15:38:02 -0800364 Collections.sort(filteredApps, LauncherModel.APP_INSTALL_TIME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700365 }
366 return filteredApps;
367 }
368
369 @Override
370 public void setApps(ArrayList<ApplicationInfo> list) {
371 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700372 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700373 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung04998342011-01-05 13:54:43 -0800374 mPageViewIconCache.retainAllApps(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700375 invalidatePageData();
376 }
377
Winson Chung80baf5a2010-08-09 16:03:15 -0700378 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
379 // we add it in place, in alphabetical order
380 final int count = list.size();
381 for (int i = 0; i < count; ++i) {
382 final ApplicationInfo info = list.get(i);
383 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
384 if (index < 0) {
385 mApps.add(-(index + 1), info);
Winson Chung452821f2011-01-14 16:39:47 -0800386 } else {
387 mApps.add(index, info);
Winson Chung80baf5a2010-08-09 16:03:15 -0700388 }
389 }
390 mFilteredApps = rebuildFilteredApps(mApps);
391 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700392 @Override
393 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700394 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700395 invalidatePageData();
396 }
397
Winson Chung80baf5a2010-08-09 16:03:15 -0700398 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung10fefb12010-11-01 11:57:06 -0700399 // End the choice mode if any of the items in the list that are being removed are
400 // currently selected
401 ArrayList<Checkable> checkedList = getCheckedGrandchildren();
402 HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>();
403 for (Checkable checked : checkedList) {
404 PagedViewIcon icon = (PagedViewIcon) checked;
405 checkedAppInfos.add((ApplicationInfo) icon.getTag());
406 }
407 for (ApplicationInfo info : list) {
408 if (checkedAppInfos.contains(info)) {
409 endChoiceMode();
410 break;
411 }
412 }
413
414 // Loop through all the apps and remove apps that have the same component
Winson Chung321e9ee2010-08-09 13:37:56 -0700415 final int length = list.size();
416 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700417 final ApplicationInfo info = list.get(i);
418 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700419 if (removeIndex > -1) {
420 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800421 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung321e9ee2010-08-09 13:37:56 -0700422 }
423 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700424 mFilteredApps = rebuildFilteredApps(mApps);
425 }
Michael Jurkaabded662011-03-04 12:06:57 -0800426
Winson Chung80baf5a2010-08-09 16:03:15 -0700427 @Override
428 public void removeApps(ArrayList<ApplicationInfo> list) {
429 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700430 invalidatePageData();
431 }
432
433 @Override
434 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700435 removeAppsWithoutInvalidate(list);
436 addAppsWithoutInvalidate(list);
437 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700438 }
439
440 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
Winson Chung8b534782011-02-23 13:43:59 -0800441 if (item != null && item.intent != null) {
442 ComponentName removeComponent = item.intent.getComponent();
443 final int length = list.size();
444 for (int i = 0; i < length; ++i) {
445 ApplicationInfo info = list.get(i);
446 if (info.intent.getComponent().equals(removeComponent)) {
447 return i;
448 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700449 }
450 }
451 return -1;
452 }
453
454 @Override
455 public void dumpState() {
456 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
457 }
458
459 @Override
460 public void surrender() {
461 // do nothing?
462 }
463
Winson Chung337cd9d2011-03-30 10:39:30 -0700464 public void reset() {
465 setCurrentPage(0);
466 invalidatePageData();
467 }
468
Michael Jurka7ef959b2011-02-23 11:48:32 -0800469 private void setupPage(PagedViewCellLayout layout) {
470 layout.setCellCount(mCellCountX, mCellCountY);
471 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
472 mPageLayoutPaddingBottom);
473 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
474 }
475
Winson Chung321e9ee2010-08-09 13:37:56 -0700476 @Override
477 public void syncPages() {
Winson Chung96785572010-10-14 13:37:13 -0700478 // ensure that we have the right number of pages (min of 1, since we have placeholders)
479 int numPages = Math.max(1,
480 (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)));
Winson Chung321e9ee2010-08-09 13:37:56 -0700481 int curNumPages = getChildCount();
482 // remove any extra pages after the "last" page
483 int extraPageDiff = curNumPages - numPages;
484 for (int i = 0; i < extraPageDiff; ++i) {
485 removeViewAt(numPages);
486 }
487 // add any necessary pages
488 for (int i = curNumPages; i < numPages; ++i) {
489 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
Michael Jurkaabded662011-03-04 12:06:57 -0800490 if (mAllowHardwareLayerCreation) {
491 layout.allowHardwareLayerCreation();
492 }
Michael Jurka7ef959b2011-02-23 11:48:32 -0800493 setupPage(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -0700494 addView(layout);
495 }
496
497 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700498 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700499 }
500
501 @Override
502 public void syncPageItems(int page) {
Winson Chung96785572010-10-14 13:37:13 -0700503 // Ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700504 final int cellsPerPage = mCellCountX * mCellCountY;
505 final int startIndex = page * cellsPerPage;
506 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700507 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700508
Winson Chung96785572010-10-14 13:37:13 -0700509 if (!mFilteredApps.isEmpty()) {
Michael Jurka8245a862011-02-01 17:53:59 -0800510 int curNumPageItems = layout.getPageChildCount();
Winson Chung96785572010-10-14 13:37:13 -0700511 int numPageItems = endIndex - startIndex;
Winson Chung80baf5a2010-08-09 16:03:15 -0700512
Winson Chung96785572010-10-14 13:37:13 -0700513 // If we were previously an empty page, then restart anew
514 boolean wasEmptyPage = false;
515 if (curNumPageItems == 1) {
Michael Jurka8245a862011-02-01 17:53:59 -0800516 View icon = layout.getChildOnPageAt(0);
Winson Chung96785572010-10-14 13:37:13 -0700517 if (icon.getTag() == null) {
518 wasEmptyPage = true;
519 }
520 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700521
Winson Chung96785572010-10-14 13:37:13 -0700522 if (wasEmptyPage) {
523 // Remove all the previous items
524 curNumPageItems = 0;
Michael Jurka8245a862011-02-01 17:53:59 -0800525 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700526 } else {
527 // Remove any extra items
528 int extraPageItemsDiff = curNumPageItems - numPageItems;
529 for (int i = 0; i < extraPageItemsDiff; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800530 layout.removeViewOnPageAt(numPageItems);
Winson Chung96785572010-10-14 13:37:13 -0700531 }
532 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700533
Winson Chung96785572010-10-14 13:37:13 -0700534 // Add any necessary items
535 for (int i = curNumPageItems; i < numPageItems; ++i) {
536 TextView text = (TextView) mInflater.inflate(
537 R.layout.all_apps_paged_view_application, layout, false);
538 text.setOnClickListener(this);
539 text.setOnLongClickListener(this);
Michael Jurka72b079e2010-12-10 01:03:53 -0800540 text.setOnTouchListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700541
Winson Chung96785572010-10-14 13:37:13 -0700542 layout.addViewToCellLayout(text, -1, i,
543 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
544 }
545
546 // Actually reapply to the existing text views
Winson Chung04998342011-01-05 13:54:43 -0800547 final int numPages = getPageCount();
Winson Chung96785572010-10-14 13:37:13 -0700548 for (int i = startIndex; i < endIndex; ++i) {
549 final int index = i - startIndex;
550 final ApplicationInfo info = mFilteredApps.get(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800551 PagedViewIcon icon = (PagedViewIcon) layout.getChildOnPageAt(index);
Winson Chung04998342011-01-05 13:54:43 -0800552 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chung96785572010-10-14 13:37:13 -0700553
554 PagedViewCellLayout.LayoutParams params =
555 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
556 params.cellX = index % mCellCountX;
557 params.cellY = index / mCellCountX;
558 }
559
560 // Default to left-aligned icons
561 layout.enableCenteredContent(false);
562 } else {
563 // There are no items, so show the user a small message
564 TextView icon = (TextView) mInflater.inflate(
565 R.layout.all_apps_no_items_placeholder, layout, false);
566 switch (mAppFilter) {
Winson Chung96785572010-10-14 13:37:13 -0700567 case ApplicationInfo.DOWNLOADED_FLAG:
568 icon.setText(mContext.getString(R.string.all_apps_no_downloads));
569 break;
570 default: break;
571 }
572
573 // Center-align the message
574 layout.enableCenteredContent(true);
Michael Jurka8245a862011-02-01 17:53:59 -0800575 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700576 layout.addViewToCellLayout(icon, -1, 0,
Winson Chung532a52a2011-01-18 12:18:00 -0800577 new PagedViewCellLayout.LayoutParams(0, 0, 4, 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700578 }
Michael Jurkac5e49022011-02-16 12:04:02 -0800579 layout.createHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700580 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700581
582 /*
583 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
584 * to the workspace.
585 */
586 @Override
587 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
588 DragView dragView, Object dragInfo) {
589 return false;
590 }
591 @Override
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700592 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
593 int yOffset, DragView dragView, Object dragInfo) {
594 return null;
595 }
596 @Override
597 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
598 DragView dragView, Object dragInfo) {}
599 @Override
600 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
601 DragView dragView, Object dragInfo) {}
602 @Override
603 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
604 DragView dragView, Object dragInfo) {}
605 @Override
606 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
607 DragView dragView, Object dragInfo) {}
Michael Jurka0280c3b2010-09-17 15:00:07 -0700608
609 public boolean isDropEnabled() {
610 return true;
611 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700612}