blob: 3d3c1ff3a9cf38597e1e70ae4cdf45c63e42fa97 [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;
Winson Chung20f71112011-04-06 14:22:04 -070070 private boolean mHasMadeSuccessfulDrop;
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 Jurka7ef959b2011-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 Jurkaabded662011-03-04 12:06:57 -0800107 void allowHardwareLayerCreation() {
108 // This is called after the first time we launch into All Apps. Before that point,
109 // there's no need for hardware layers here since there's a hardware layer set on the
110 // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here
111 // before the animation is done slows down the animation
112 if (mAllowHardwareLayerCreation) {
113 return;
114 }
115 mAllowHardwareLayerCreation = true;
116 int childCount = getChildCount();
117 for (int i = 0; i < childCount; i++) {
118 PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(i);
119 page.allowHardwareLayerCreation();
120 }
121 }
122
Winson Chung7da10252010-10-28 16:07:04 -0700123 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -0700124 public void setLauncher(Launcher launcher) {
125 mLauncher = launcher;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700126 mLauncher.setAllAppsPagedView(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700127 }
128
129 @Override
130 public void setDragController(DragController dragger) {
131 mDragController = dragger;
132 }
133
134 public void setAppFilter(int filterType) {
135 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700136 if (mApps != null) {
137 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700138 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700139 invalidatePageData();
140 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700141 }
142
Winson Chung20f71112011-04-06 14:22:04 -0700143 void resetSuccessfulDropFlag() {
144 mHasMadeSuccessfulDrop = false;
145 }
146
Winson Chung321e9ee2010-08-09 13:37:56 -0700147 @Override
148 public void zoom(float zoom, boolean animate) {
149 mZoom = zoom;
150 cancelLongPress();
151
152 if (isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700153 if (animate) {
154 startAnimation(AnimationUtils.loadAnimation(getContext(),
155 R.anim.all_apps_2d_fade_in));
156 } else {
157 onAnimationEnd();
158 }
159 } else {
160 if (animate) {
161 startAnimation(AnimationUtils.loadAnimation(getContext(),
162 R.anim.all_apps_2d_fade_out));
163 } else {
164 onAnimationEnd();
165 }
166 }
167 }
168
169 protected void onAnimationEnd() {
170 if (!isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700171 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700172
173 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700174 } else {
175 mZoom = 1.0f;
176 }
177
178 if (mLauncher != null)
179 mLauncher.zoomed(mZoom);
180 }
181
182 private int getChildIndexForGrandChild(View v) {
183 final int childCount = getChildCount();
184 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800185 final Page layout = (Page) getChildAt(i);
186 if (layout.indexOfChildOnPage(v) > -1) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700187 return i;
188 }
189 }
190 return -1;
191 }
192
193 @Override
194 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700195 // if we are already in a choice mode, then just change the selection
196 if (v instanceof Checkable) {
197 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700198 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700199 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700200 // Uncheck all the other grandchildren, and toggle the clicked one
201 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700202 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700203 c.setChecked(!wasChecked);
204 } else {
205 c.toggle();
206 }
207 if (getCheckedGrandchildren().size() == 0) {
208 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700209 }
210
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700211 return;
212 }
213 }
214
215 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700216 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700217 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 final ApplicationInfo app = (ApplicationInfo) v.getTag();
219
Winson Chung80baf5a2010-08-09 16:03:15 -0700220 // animate some feedback to the click
221 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700223 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700224 mLauncher.startActivitySafely(app.intent, app);
225 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700226 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700227
228 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 }
230 }
231
Patrick Dubroycd953712011-02-28 15:16:42 -0800232 private void setupDragMode(ApplicationInfo info) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800233 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE);
Patrick Dubroycd953712011-02-28 15:16:42 -0800234
235 // Only show the uninstall button if the app is uninstallable.
236 if ((info.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
237 DeleteZone allAppsDeleteZone = (DeleteZone)
238 mLauncher.findViewById(R.id.all_apps_delete_zone);
239 allAppsDeleteZone.setDragAndDropEnabled(true);
240
241 if ((info.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
242 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
243 } else {
244 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
245 }
246 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800247
Adam Cohencdc30d52010-12-01 15:09:47 -0800248 ApplicationInfoDropTarget allAppsInfoButton =
249 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
250 allAppsInfoButton.setDragAndDropEnabled(true);
Adam Cohencdc30d52010-12-01 15:09:47 -0800251 }
252
253 private void tearDownDragMode() {
254 post(new Runnable() {
255 // Once the drag operation has fully completed, hence the post, we want to disable the
256 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
257 // live in the workspace
258 public void run() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800259 DeleteZone allAppsDeleteZone =
260 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
Michael Jurkac31820d2011-01-16 16:57:05 -0800261 // if onDestroy was called on Launcher, we might have already deleted the
262 // all apps delete zone / info button, so check if they are null
263 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
Michael Jurkab8e14472010-12-20 16:06:10 -0800264
Adam Cohencdc30d52010-12-01 15:09:47 -0800265 ApplicationInfoDropTarget allAppsInfoButton =
266 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
Michael Jurkac31820d2011-01-16 16:57:05 -0800267 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800268 }
269 });
270 resetCheckedGrandchildren();
271 mDragController.removeDropTarget(this);
272 }
273
Winson Chung321e9ee2010-08-09 13:37:56 -0700274 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800275 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800276 if (!v.isInTouchMode()) return false;
277 if (!super.beginDragging(v)) return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700278
279 ApplicationInfo app = (ApplicationInfo) v.getTag();
280 app = new ApplicationInfo(app);
281
Patrick Dubroycd953712011-02-28 15:16:42 -0800282 // Start drag mode after the item is selected
283 setupDragMode(app);
284
Michael Jurkad3ef3062010-11-23 16:23:58 -0800285 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800286 final TextView tv = (TextView) v;
287 final Drawable icon = tv.getCompoundDrawables()[1];
288 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Michael Jurkad3ef3062010-11-23 16:23:58 -0800289 Bitmap.Config.ARGB_8888);
290 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800291 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800292 icon.draw(c);
Winson Chungcd4bc492010-12-09 18:52:32 -0800293
Adam Cohene3e27a82011-04-15 12:07:39 -0700294 Rect dragRect = null;
295 if (v instanceof TextView) {
296 int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size);
297 int top = v.getPaddingTop();
298 int left = (b.getWidth() - iconSize) / 2;
299 int right = left + iconSize;
300 int bottom = top + iconSize;
301 dragRect = new Rect(left, top, right, bottom);
302 }
303
Winson Chungcd4bc492010-12-09 18:52:32 -0800304 // We toggle the checked state _after_ we create the view for the drag in case toggling the
305 // checked state changes the view's look
306 if (v instanceof Checkable) {
307 // In preparation for drag, we always reset the checked grand children regardless of
308 // what choice mode we are in
309 resetCheckedGrandchildren();
310
311 // Toggle the selection on the dragged app
312 Checkable checkable = (Checkable) v;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800313
314 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
315 // of the drag of the item. (The fade-in will occur when all checked states are
316 // disabled when dragging ends)
Winson Chungcd4bc492010-12-09 18:52:32 -0800317 checkable.toggle();
318 }
319
320 // Start the drag
Winson Chung400438b2011-01-16 17:53:48 -0800321 mLauncher.lockScreenOrientation();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800322 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Adam Cohene3e27a82011-04-15 12:07:39 -0700323 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, dragRect);
Winson Chungcd4bc492010-12-09 18:52:32 -0800324 b.recycle();
Winson Chung321e9ee2010-08-09 13:37:56 -0700325 return true;
326 }
327
328 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800329 public void onDragViewVisible() {
330 }
331
332 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800333 public void onDropCompleted(View target, Object dragInfo, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700334 // close the choice action mode if we have a proper drop
335 if (target != this) {
336 endChoiceMode();
337 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800338 tearDownDragMode();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800339 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800340 mLauncher.unlockScreenOrientation();
Winson Chung20f71112011-04-06 14:22:04 -0700341
342 if (!success && !mHasMadeSuccessfulDrop) {
343 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_HIDDEN);
344 } else {
345 mHasMadeSuccessfulDrop |= success;
346 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700347 }
348
Michael Jurka7ef959b2011-02-23 11:48:32 -0800349 int getPageContentWidth() {
350 return mPageContentWidth;
351 }
352
Winson Chung321e9ee2010-08-09 13:37:56 -0700353 @Override
354 public boolean isVisible() {
355 return mZoom > 0.001f;
356 }
357
358 @Override
359 public boolean isAnimating() {
360 return (getAnimation() != null);
361 }
362
363 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
364 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
365 if (mAppFilter == ALL_APPS_FLAG) {
366 return apps;
367 } else {
368 final int length = apps.size();
369 for (int i = 0; i < length; ++i) {
370 ApplicationInfo info = apps.get(i);
371 if ((info.flags & mAppFilter) > 0) {
372 filteredApps.add(info);
373 }
374 }
Winson Chung78403fe2011-01-21 15:38:02 -0800375 Collections.sort(filteredApps, LauncherModel.APP_INSTALL_TIME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700376 }
377 return filteredApps;
378 }
379
380 @Override
381 public void setApps(ArrayList<ApplicationInfo> list) {
382 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700383 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700384 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung04998342011-01-05 13:54:43 -0800385 mPageViewIconCache.retainAllApps(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700386 invalidatePageData();
387 }
388
Winson Chung80baf5a2010-08-09 16:03:15 -0700389 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
390 // we add it in place, in alphabetical order
391 final int count = list.size();
392 for (int i = 0; i < count; ++i) {
393 final ApplicationInfo info = list.get(i);
394 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
395 if (index < 0) {
396 mApps.add(-(index + 1), info);
Winson Chung452821f2011-01-14 16:39:47 -0800397 } else {
398 mApps.add(index, info);
Winson Chung80baf5a2010-08-09 16:03:15 -0700399 }
400 }
401 mFilteredApps = rebuildFilteredApps(mApps);
402 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700403 @Override
404 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700405 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700406 invalidatePageData();
407 }
408
Winson Chung80baf5a2010-08-09 16:03:15 -0700409 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung10fefb12010-11-01 11:57:06 -0700410 // End the choice mode if any of the items in the list that are being removed are
411 // currently selected
412 ArrayList<Checkable> checkedList = getCheckedGrandchildren();
413 HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>();
414 for (Checkable checked : checkedList) {
415 PagedViewIcon icon = (PagedViewIcon) checked;
416 checkedAppInfos.add((ApplicationInfo) icon.getTag());
417 }
418 for (ApplicationInfo info : list) {
419 if (checkedAppInfos.contains(info)) {
420 endChoiceMode();
421 break;
422 }
423 }
424
425 // Loop through all the apps and remove apps that have the same component
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 final int length = list.size();
427 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700428 final ApplicationInfo info = list.get(i);
429 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700430 if (removeIndex > -1) {
431 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800432 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung321e9ee2010-08-09 13:37:56 -0700433 }
434 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700435 mFilteredApps = rebuildFilteredApps(mApps);
436 }
Michael Jurkaabded662011-03-04 12:06:57 -0800437
Winson Chung80baf5a2010-08-09 16:03:15 -0700438 @Override
439 public void removeApps(ArrayList<ApplicationInfo> list) {
440 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700441 invalidatePageData();
442 }
443
444 @Override
445 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700446 removeAppsWithoutInvalidate(list);
447 addAppsWithoutInvalidate(list);
448 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700449 }
450
451 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
Winson Chung8b534782011-02-23 13:43:59 -0800452 if (item != null && item.intent != null) {
453 ComponentName removeComponent = item.intent.getComponent();
454 final int length = list.size();
455 for (int i = 0; i < length; ++i) {
456 ApplicationInfo info = list.get(i);
457 if (info.intent.getComponent().equals(removeComponent)) {
458 return i;
459 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700460 }
461 }
462 return -1;
463 }
464
465 @Override
466 public void dumpState() {
467 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
468 }
469
470 @Override
471 public void surrender() {
472 // do nothing?
473 }
474
Winson Chung337cd9d2011-03-30 10:39:30 -0700475 public void reset() {
476 setCurrentPage(0);
477 invalidatePageData();
478 }
479
Michael Jurka7ef959b2011-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() {
Winson Chung96785572010-10-14 13:37:13 -0700489 // ensure that we have the right number of pages (min of 1, since we have placeholders)
490 int numPages = Math.max(1,
491 (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)));
Winson Chung321e9ee2010-08-09 13:37:56 -0700492 int curNumPages = getChildCount();
493 // remove any extra pages after the "last" page
494 int extraPageDiff = curNumPages - numPages;
495 for (int i = 0; i < extraPageDiff; ++i) {
496 removeViewAt(numPages);
497 }
498 // add any necessary pages
499 for (int i = curNumPages; i < numPages; ++i) {
500 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
Michael Jurkaabded662011-03-04 12:06:57 -0800501 if (mAllowHardwareLayerCreation) {
502 layout.allowHardwareLayerCreation();
503 }
Michael Jurka7ef959b2011-02-23 11:48:32 -0800504 setupPage(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -0700505 addView(layout);
506 }
507
508 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700509 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700510 }
511
512 @Override
513 public void syncPageItems(int page) {
Winson Chung96785572010-10-14 13:37:13 -0700514 // Ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700515 final int cellsPerPage = mCellCountX * mCellCountY;
516 final int startIndex = page * cellsPerPage;
517 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700518 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700519
Winson Chung96785572010-10-14 13:37:13 -0700520 if (!mFilteredApps.isEmpty()) {
Michael Jurka8245a862011-02-01 17:53:59 -0800521 int curNumPageItems = layout.getPageChildCount();
Winson Chung96785572010-10-14 13:37:13 -0700522 int numPageItems = endIndex - startIndex;
Winson Chung80baf5a2010-08-09 16:03:15 -0700523
Winson Chung96785572010-10-14 13:37:13 -0700524 // If we were previously an empty page, then restart anew
525 boolean wasEmptyPage = false;
526 if (curNumPageItems == 1) {
Michael Jurka8245a862011-02-01 17:53:59 -0800527 View icon = layout.getChildOnPageAt(0);
Winson Chung96785572010-10-14 13:37:13 -0700528 if (icon.getTag() == null) {
529 wasEmptyPage = true;
530 }
531 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700532
Winson Chung96785572010-10-14 13:37:13 -0700533 if (wasEmptyPage) {
534 // Remove all the previous items
535 curNumPageItems = 0;
Michael Jurka8245a862011-02-01 17:53:59 -0800536 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700537 } else {
538 // Remove any extra items
539 int extraPageItemsDiff = curNumPageItems - numPageItems;
540 for (int i = 0; i < extraPageItemsDiff; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800541 layout.removeViewOnPageAt(numPageItems);
Winson Chung96785572010-10-14 13:37:13 -0700542 }
543 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700544
Winson Chung96785572010-10-14 13:37:13 -0700545 // Add any necessary items
546 for (int i = curNumPageItems; i < numPageItems; ++i) {
547 TextView text = (TextView) mInflater.inflate(
548 R.layout.all_apps_paged_view_application, layout, false);
549 text.setOnClickListener(this);
550 text.setOnLongClickListener(this);
Michael Jurka72b079e2010-12-10 01:03:53 -0800551 text.setOnTouchListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700552
Winson Chung96785572010-10-14 13:37:13 -0700553 layout.addViewToCellLayout(text, -1, i,
554 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
555 }
556
557 // Actually reapply to the existing text views
Winson Chung04998342011-01-05 13:54:43 -0800558 final int numPages = getPageCount();
Winson Chung96785572010-10-14 13:37:13 -0700559 for (int i = startIndex; i < endIndex; ++i) {
560 final int index = i - startIndex;
561 final ApplicationInfo info = mFilteredApps.get(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800562 PagedViewIcon icon = (PagedViewIcon) layout.getChildOnPageAt(index);
Winson Chung04998342011-01-05 13:54:43 -0800563 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chung96785572010-10-14 13:37:13 -0700564
565 PagedViewCellLayout.LayoutParams params =
566 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
567 params.cellX = index % mCellCountX;
568 params.cellY = index / mCellCountX;
569 }
570
571 // Default to left-aligned icons
572 layout.enableCenteredContent(false);
573 } else {
574 // There are no items, so show the user a small message
575 TextView icon = (TextView) mInflater.inflate(
576 R.layout.all_apps_no_items_placeholder, layout, false);
577 switch (mAppFilter) {
Winson Chung96785572010-10-14 13:37:13 -0700578 case ApplicationInfo.DOWNLOADED_FLAG:
579 icon.setText(mContext.getString(R.string.all_apps_no_downloads));
580 break;
581 default: break;
582 }
583
584 // Center-align the message
585 layout.enableCenteredContent(true);
Michael Jurka8245a862011-02-01 17:53:59 -0800586 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700587 layout.addViewToCellLayout(icon, -1, 0,
Winson Chung532a52a2011-01-18 12:18:00 -0800588 new PagedViewCellLayout.LayoutParams(0, 0, 4, 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700589 }
Michael Jurkac5e49022011-02-16 12:04:02 -0800590 layout.createHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700591 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700592
593 /*
594 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
595 * to the workspace.
596 */
597 @Override
598 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
599 DragView dragView, Object dragInfo) {
600 return false;
601 }
602 @Override
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700603 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
604 int yOffset, DragView dragView, Object dragInfo) {
605 return null;
606 }
607 @Override
608 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
609 DragView dragView, Object dragInfo) {}
610 @Override
611 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
612 DragView dragView, Object dragInfo) {}
613 @Override
614 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
615 DragView dragView, Object dragInfo) {}
616 @Override
617 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
618 DragView dragView, Object dragInfo) {}
Michael Jurka0280c3b2010-09-17 15:00:07 -0700619
620 public boolean isDropEnabled() {
621 return true;
622 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700623}