blob: 522f7d7ac0139f790e15cc3a2df6c4fd63082f67 [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 Jurkaea2daff2011-05-17 18:21:03 -070068 private boolean mFirstMeasure = true;
69
Michael Jurka12ac0d62011-02-23 11:48:32 -080070 private int mPageContentWidth;
Patrick Dubroydea9e932010-09-22 15:04:29 -070071
Winson Chung321e9ee2010-08-09 13:37:56 -070072 public AllAppsPagedView(Context context) {
73 this(context, null);
74 }
75
76 public AllAppsPagedView(Context context, AttributeSet attrs) {
77 this(context, attrs, 0);
78 }
79
80 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
81 super(context, attrs, defStyle);
82 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
83 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
84 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
85 mInflater = LayoutInflater.from(context);
Winson Chung8b534782011-02-23 13:43:59 -080086 mApps = new ArrayList<ApplicationInfo>();
87 mFilteredApps = new ArrayList<ApplicationInfo>();
Winson Chung321e9ee2010-08-09 13:37:56 -070088 a.recycle();
89 setSoundEffectsEnabled(false);
Michael Jurka72b079e2010-12-10 01:03:53 -080090
91 Resources r = context.getResources();
92 setDragSlopeThreshold(
93 r.getInteger(R.integer.config_allAppsDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka12ac0d62011-02-23 11:48:32 -080094
95 // Create a dummy page and set it up to find out the content width (used by our parent)
96 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
97 setupPage(layout);
98 mPageContentWidth = layout.getContentWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -070099 }
100
101 @Override
Winson Chung7da10252010-10-28 16:07:04 -0700102 protected void init() {
103 super.init();
104 mCenterPagesVertically = false;
105 }
106
Michael Jurkaea2daff2011-05-17 18:21:03 -0700107 @Override
108 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
109 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
110
111 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
112 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
113
114 if (mFirstMeasure) {
115 mFirstMeasure = false;
116
117 // TODO: actually calculate mCellCountX/mCellCountY as some function of
118 // widthSize and heightSize
119 //mCellCountX = ?;
120 //mCellCountY = ?;
121
122 // Since mCellCountX/mCellCountY changed, we need to update the pages
123 invalidatePageData();
124
125 // Create a dummy page and set it up to find out the content width (used by our parent)
126 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
127 setupPage(layout);
128 mPageContentWidth = layout.getContentWidth();
129 }
130 }
131
Michael Jurkaabded662011-03-04 12:06:57 -0800132 void allowHardwareLayerCreation() {
133 // This is called after the first time we launch into All Apps. Before that point,
134 // there's no need for hardware layers here since there's a hardware layer set on the
135 // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here
136 // before the animation is done slows down the animation
137 if (mAllowHardwareLayerCreation) {
138 return;
139 }
140 mAllowHardwareLayerCreation = true;
141 int childCount = getChildCount();
142 for (int i = 0; i < childCount; i++) {
143 PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(i);
144 page.allowHardwareLayerCreation();
145 }
146 }
147
Winson Chung7da10252010-10-28 16:07:04 -0700148 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700149 public void setLauncher(Launcher launcher) {
150 mLauncher = launcher;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700151 mLauncher.setAllAppsPagedView(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700152 }
153
154 @Override
155 public void setDragController(DragController dragger) {
156 mDragController = dragger;
157 }
158
159 public void setAppFilter(int filterType) {
160 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700161 if (mApps != null) {
162 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700163 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700164 invalidatePageData();
165 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700166 }
167
168 @Override
169 public void zoom(float zoom, boolean animate) {
170 mZoom = zoom;
171 cancelLongPress();
172
173 if (isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700174 if (animate) {
175 startAnimation(AnimationUtils.loadAnimation(getContext(),
176 R.anim.all_apps_2d_fade_in));
177 } else {
178 onAnimationEnd();
179 }
180 } else {
181 if (animate) {
182 startAnimation(AnimationUtils.loadAnimation(getContext(),
183 R.anim.all_apps_2d_fade_out));
184 } else {
185 onAnimationEnd();
186 }
187 }
188 }
189
190 protected void onAnimationEnd() {
191 if (!isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700193
194 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700195 } else {
196 mZoom = 1.0f;
197 }
198
199 if (mLauncher != null)
200 mLauncher.zoomed(mZoom);
201 }
202
203 private int getChildIndexForGrandChild(View v) {
204 final int childCount = getChildCount();
205 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800206 final Page layout = (Page) getChildAt(i);
207 if (layout.indexOfChildOnPage(v) > -1) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700208 return i;
209 }
210 }
211 return -1;
212 }
213
214 @Override
215 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700216 // if we are already in a choice mode, then just change the selection
217 if (v instanceof Checkable) {
218 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700219 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700220 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700221 // Uncheck all the other grandchildren, and toggle the clicked one
222 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700223 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700224 c.setChecked(!wasChecked);
225 } else {
226 c.toggle();
227 }
228 if (getCheckedGrandchildren().size() == 0) {
229 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700230 }
231
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700232 return;
233 }
234 }
235
236 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700238 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 final ApplicationInfo app = (ApplicationInfo) v.getTag();
240
Winson Chung80baf5a2010-08-09 16:03:15 -0700241 // animate some feedback to the click
242 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700243 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700244 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 mLauncher.startActivitySafely(app.intent, app);
246 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700248
249 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700250 }
251 }
252
Patrick Dubroycd953712011-02-28 15:16:42 -0800253 private void setupDragMode(ApplicationInfo info) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800254 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE);
Patrick Dubroycd953712011-02-28 15:16:42 -0800255
256 // Only show the uninstall button if the app is uninstallable.
257 if ((info.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
258 DeleteZone allAppsDeleteZone = (DeleteZone)
259 mLauncher.findViewById(R.id.all_apps_delete_zone);
260 allAppsDeleteZone.setDragAndDropEnabled(true);
261
262 if ((info.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
263 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
264 } else {
265 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
266 }
267 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800268
Adam Cohencdc30d52010-12-01 15:09:47 -0800269 ApplicationInfoDropTarget allAppsInfoButton =
270 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
271 allAppsInfoButton.setDragAndDropEnabled(true);
Adam Cohencdc30d52010-12-01 15:09:47 -0800272 }
273
274 private void tearDownDragMode() {
275 post(new Runnable() {
276 // Once the drag operation has fully completed, hence the post, we want to disable the
277 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
278 // live in the workspace
279 public void run() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800280 DeleteZone allAppsDeleteZone =
281 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
Michael Jurkac31820d2011-01-16 16:57:05 -0800282 // if onDestroy was called on Launcher, we might have already deleted the
283 // all apps delete zone / info button, so check if they are null
284 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
Michael Jurkab8e14472010-12-20 16:06:10 -0800285
Adam Cohencdc30d52010-12-01 15:09:47 -0800286 ApplicationInfoDropTarget allAppsInfoButton =
287 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
Michael Jurkac31820d2011-01-16 16:57:05 -0800288 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800289 }
290 });
291 resetCheckedGrandchildren();
292 mDragController.removeDropTarget(this);
293 }
294
Winson Chung321e9ee2010-08-09 13:37:56 -0700295 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800296 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800297 if (!v.isInTouchMode()) return false;
298 if (!super.beginDragging(v)) return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700299
300 ApplicationInfo app = (ApplicationInfo) v.getTag();
301 app = new ApplicationInfo(app);
302
Patrick Dubroycd953712011-02-28 15:16:42 -0800303 // Start drag mode after the item is selected
304 setupDragMode(app);
305
Michael Jurkad3ef3062010-11-23 16:23:58 -0800306 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800307 final TextView tv = (TextView) v;
308 final Drawable icon = tv.getCompoundDrawables()[1];
309 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Michael Jurkad3ef3062010-11-23 16:23:58 -0800310 Bitmap.Config.ARGB_8888);
311 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800312 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800313 icon.draw(c);
Winson Chungcd4bc492010-12-09 18:52:32 -0800314
315 // We toggle the checked state _after_ we create the view for the drag in case toggling the
316 // checked state changes the view's look
317 if (v instanceof Checkable) {
318 // In preparation for drag, we always reset the checked grand children regardless of
319 // what choice mode we are in
320 resetCheckedGrandchildren();
321
322 // Toggle the selection on the dragged app
323 Checkable checkable = (Checkable) v;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800324
325 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
326 // of the drag of the item. (The fade-in will occur when all checked states are
327 // disabled when dragging ends)
Winson Chungcd4bc492010-12-09 18:52:32 -0800328 checkable.toggle();
329 }
330
331 // Start the drag
Winson Chung400438b2011-01-16 17:53:48 -0800332 mLauncher.lockScreenOrientation();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800333 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
334 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
Winson Chungcd4bc492010-12-09 18:52:32 -0800335 b.recycle();
Winson Chung321e9ee2010-08-09 13:37:56 -0700336 return true;
337 }
338
339 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800340 public void onDragViewVisible() {
341 }
342
343 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800344 public void onDropCompleted(View target, Object dragInfo, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700345 // close the choice action mode if we have a proper drop
346 if (target != this) {
347 endChoiceMode();
348 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800349 tearDownDragMode();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800350 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800351 mLauncher.unlockScreenOrientation();
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 }
353
Michael Jurka12ac0d62011-02-23 11:48:32 -0800354 int getPageContentWidth() {
355 return mPageContentWidth;
356 }
357
Winson Chung321e9ee2010-08-09 13:37:56 -0700358 @Override
359 public boolean isVisible() {
360 return mZoom > 0.001f;
361 }
362
363 @Override
364 public boolean isAnimating() {
365 return (getAnimation() != null);
366 }
367
368 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
369 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
370 if (mAppFilter == ALL_APPS_FLAG) {
371 return apps;
372 } else {
373 final int length = apps.size();
374 for (int i = 0; i < length; ++i) {
375 ApplicationInfo info = apps.get(i);
376 if ((info.flags & mAppFilter) > 0) {
377 filteredApps.add(info);
378 }
379 }
Winson Chung78403fe2011-01-21 15:38:02 -0800380 Collections.sort(filteredApps, LauncherModel.APP_INSTALL_TIME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700381 }
382 return filteredApps;
383 }
384
385 @Override
386 public void setApps(ArrayList<ApplicationInfo> list) {
387 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700388 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700389 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung04998342011-01-05 13:54:43 -0800390 mPageViewIconCache.retainAllApps(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700391 invalidatePageData();
392 }
393
Winson Chung80baf5a2010-08-09 16:03:15 -0700394 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
395 // we add it in place, in alphabetical order
396 final int count = list.size();
397 for (int i = 0; i < count; ++i) {
398 final ApplicationInfo info = list.get(i);
399 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
400 if (index < 0) {
401 mApps.add(-(index + 1), info);
Winson Chung452821f2011-01-14 16:39:47 -0800402 } else {
403 mApps.add(index, info);
Winson Chung80baf5a2010-08-09 16:03:15 -0700404 }
405 }
406 mFilteredApps = rebuildFilteredApps(mApps);
407 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700408 @Override
409 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700410 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700411 invalidatePageData();
412 }
413
Winson Chung80baf5a2010-08-09 16:03:15 -0700414 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung10fefb12010-11-01 11:57:06 -0700415 // End the choice mode if any of the items in the list that are being removed are
416 // currently selected
417 ArrayList<Checkable> checkedList = getCheckedGrandchildren();
418 HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>();
419 for (Checkable checked : checkedList) {
420 PagedViewIcon icon = (PagedViewIcon) checked;
421 checkedAppInfos.add((ApplicationInfo) icon.getTag());
422 }
423 for (ApplicationInfo info : list) {
424 if (checkedAppInfos.contains(info)) {
425 endChoiceMode();
426 break;
427 }
428 }
429
430 // Loop through all the apps and remove apps that have the same component
Winson Chung321e9ee2010-08-09 13:37:56 -0700431 final int length = list.size();
432 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700433 final ApplicationInfo info = list.get(i);
434 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700435 if (removeIndex > -1) {
436 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800437 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung321e9ee2010-08-09 13:37:56 -0700438 }
439 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700440 mFilteredApps = rebuildFilteredApps(mApps);
441 }
Michael Jurkaabded662011-03-04 12:06:57 -0800442
Winson Chung80baf5a2010-08-09 16:03:15 -0700443 @Override
444 public void removeApps(ArrayList<ApplicationInfo> list) {
445 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700446 invalidatePageData();
447 }
448
449 @Override
450 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700451 removeAppsWithoutInvalidate(list);
452 addAppsWithoutInvalidate(list);
453 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 }
455
456 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
Winson Chung8b534782011-02-23 13:43:59 -0800457 if (item != null && item.intent != null) {
458 ComponentName removeComponent = item.intent.getComponent();
459 final int length = list.size();
460 for (int i = 0; i < length; ++i) {
461 ApplicationInfo info = list.get(i);
462 if (info.intent.getComponent().equals(removeComponent)) {
463 return i;
464 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700465 }
466 }
467 return -1;
468 }
469
470 @Override
471 public void dumpState() {
472 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
473 }
474
475 @Override
476 public void surrender() {
477 // do nothing?
478 }
479
Michael Jurka12ac0d62011-02-23 11:48:32 -0800480 private void setupPage(PagedViewCellLayout layout) {
481 layout.setCellCount(mCellCountX, mCellCountY);
482 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
483 mPageLayoutPaddingBottom);
484 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
485 }
486
Winson Chung321e9ee2010-08-09 13:37:56 -0700487 @Override
488 public void syncPages() {
Michael Jurkaea2daff2011-05-17 18:21:03 -0700489 if (mFirstMeasure) {
490 // We don't know our size yet, which means we haven't calculated cell count x/y;
491 // onMeasure will call us once we figure out our size
492 return;
493 }
Winson Chung96785572010-10-14 13:37:13 -0700494 // ensure that we have the right number of pages (min of 1, since we have placeholders)
495 int numPages = Math.max(1,
496 (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)));
Winson Chung321e9ee2010-08-09 13:37:56 -0700497 int curNumPages = getChildCount();
498 // remove any extra pages after the "last" page
499 int extraPageDiff = curNumPages - numPages;
500 for (int i = 0; i < extraPageDiff; ++i) {
501 removeViewAt(numPages);
502 }
503 // add any necessary pages
504 for (int i = curNumPages; i < numPages; ++i) {
505 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
Michael Jurkaabded662011-03-04 12:06:57 -0800506 if (mAllowHardwareLayerCreation) {
507 layout.allowHardwareLayerCreation();
508 }
Michael Jurka12ac0d62011-02-23 11:48:32 -0800509 setupPage(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -0700510 addView(layout);
511 }
512
513 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700514 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 }
516
517 @Override
518 public void syncPageItems(int page) {
Winson Chung96785572010-10-14 13:37:13 -0700519 // Ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700520 final int cellsPerPage = mCellCountX * mCellCountY;
521 final int startIndex = page * cellsPerPage;
522 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700523 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700524
Winson Chung96785572010-10-14 13:37:13 -0700525 if (!mFilteredApps.isEmpty()) {
Michael Jurka8245a862011-02-01 17:53:59 -0800526 int curNumPageItems = layout.getPageChildCount();
Winson Chung96785572010-10-14 13:37:13 -0700527 int numPageItems = endIndex - startIndex;
Winson Chung80baf5a2010-08-09 16:03:15 -0700528
Winson Chung96785572010-10-14 13:37:13 -0700529 // If we were previously an empty page, then restart anew
530 boolean wasEmptyPage = false;
531 if (curNumPageItems == 1) {
Michael Jurka8245a862011-02-01 17:53:59 -0800532 View icon = layout.getChildOnPageAt(0);
Winson Chung96785572010-10-14 13:37:13 -0700533 if (icon.getTag() == null) {
534 wasEmptyPage = true;
535 }
536 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700537
Winson Chung96785572010-10-14 13:37:13 -0700538 if (wasEmptyPage) {
539 // Remove all the previous items
540 curNumPageItems = 0;
Michael Jurka8245a862011-02-01 17:53:59 -0800541 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700542 } else {
543 // Remove any extra items
544 int extraPageItemsDiff = curNumPageItems - numPageItems;
545 for (int i = 0; i < extraPageItemsDiff; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800546 layout.removeViewOnPageAt(numPageItems);
Winson Chung96785572010-10-14 13:37:13 -0700547 }
548 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700549
Winson Chung96785572010-10-14 13:37:13 -0700550 // Add any necessary items
551 for (int i = curNumPageItems; i < numPageItems; ++i) {
552 TextView text = (TextView) mInflater.inflate(
553 R.layout.all_apps_paged_view_application, layout, false);
554 text.setOnClickListener(this);
555 text.setOnLongClickListener(this);
Michael Jurka72b079e2010-12-10 01:03:53 -0800556 text.setOnTouchListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700557
Winson Chung96785572010-10-14 13:37:13 -0700558 layout.addViewToCellLayout(text, -1, i,
559 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
560 }
561
562 // Actually reapply to the existing text views
Winson Chung04998342011-01-05 13:54:43 -0800563 final int numPages = getPageCount();
Winson Chung96785572010-10-14 13:37:13 -0700564 for (int i = startIndex; i < endIndex; ++i) {
565 final int index = i - startIndex;
566 final ApplicationInfo info = mFilteredApps.get(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800567 PagedViewIcon icon = (PagedViewIcon) layout.getChildOnPageAt(index);
Winson Chung04998342011-01-05 13:54:43 -0800568 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chung96785572010-10-14 13:37:13 -0700569
570 PagedViewCellLayout.LayoutParams params =
571 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
572 params.cellX = index % mCellCountX;
573 params.cellY = index / mCellCountX;
574 }
575
576 // Default to left-aligned icons
577 layout.enableCenteredContent(false);
578 } else {
579 // There are no items, so show the user a small message
580 TextView icon = (TextView) mInflater.inflate(
581 R.layout.all_apps_no_items_placeholder, layout, false);
582 switch (mAppFilter) {
Winson Chung96785572010-10-14 13:37:13 -0700583 case ApplicationInfo.DOWNLOADED_FLAG:
584 icon.setText(mContext.getString(R.string.all_apps_no_downloads));
585 break;
586 default: break;
587 }
588
589 // Center-align the message
590 layout.enableCenteredContent(true);
Michael Jurka8245a862011-02-01 17:53:59 -0800591 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700592 layout.addViewToCellLayout(icon, -1, 0,
Winson Chung532a52a2011-01-18 12:18:00 -0800593 new PagedViewCellLayout.LayoutParams(0, 0, 4, 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700594 }
Michael Jurkac5e49022011-02-16 12:04:02 -0800595 layout.createHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700596 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700597
598 /*
599 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
600 * to the workspace.
601 */
602 @Override
603 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
604 DragView dragView, Object dragInfo) {
605 return false;
606 }
607 @Override
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700608 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
609 int yOffset, DragView dragView, Object dragInfo) {
610 return null;
611 }
612 @Override
613 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
614 DragView dragView, Object dragInfo) {}
615 @Override
616 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
617 DragView dragView, Object dragInfo) {}
618 @Override
619 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
620 DragView dragView, Object dragInfo) {}
621 @Override
622 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
623 DragView dragView, Object dragInfo) {}
Michael Jurka0280c3b2010-09-17 15:00:07 -0700624
625 public boolean isDropEnabled() {
626 return true;
627 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700628}