blob: 1c4b9dbdb5f0dda24becdda0b1d36807b840ee8a [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(
Winson Chung785d2eb2011-04-14 16:08:02 -070093 r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold) / 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 Chung785d2eb2011-04-14 16:08:02 -0700124 public void setup(Launcher launcher, DragController dragController) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700125 mLauncher = launcher;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700126 mLauncher.setAllAppsPagedView(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700127 mDragController = dragController;
Winson Chung321e9ee2010-08-09 13:37:56 -0700128 }
129
130 public void setAppFilter(int filterType) {
131 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700132 if (mApps != null) {
133 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700134 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700135 invalidatePageData();
136 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700137 }
138
Winson Chung20f71112011-04-06 14:22:04 -0700139 void resetSuccessfulDropFlag() {
140 mHasMadeSuccessfulDrop = false;
141 }
142
Winson Chung321e9ee2010-08-09 13:37:56 -0700143 @Override
144 public void zoom(float zoom, boolean animate) {
145 mZoom = zoom;
146 cancelLongPress();
147
148 if (isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700149 if (animate) {
150 startAnimation(AnimationUtils.loadAnimation(getContext(),
151 R.anim.all_apps_2d_fade_in));
152 } else {
153 onAnimationEnd();
154 }
155 } else {
156 if (animate) {
157 startAnimation(AnimationUtils.loadAnimation(getContext(),
158 R.anim.all_apps_2d_fade_out));
159 } else {
160 onAnimationEnd();
161 }
162 }
163 }
164
165 protected void onAnimationEnd() {
166 if (!isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700167 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700168
169 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700170 } else {
171 mZoom = 1.0f;
172 }
173
174 if (mLauncher != null)
175 mLauncher.zoomed(mZoom);
176 }
177
178 private int getChildIndexForGrandChild(View v) {
179 final int childCount = getChildCount();
180 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800181 final Page layout = (Page) getChildAt(i);
182 if (layout.indexOfChildOnPage(v) > -1) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700183 return i;
184 }
185 }
186 return -1;
187 }
188
189 @Override
190 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700191 // if we are already in a choice mode, then just change the selection
192 if (v instanceof Checkable) {
193 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700194 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700195 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700196 // Uncheck all the other grandchildren, and toggle the clicked one
197 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700198 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700199 c.setChecked(!wasChecked);
200 } else {
201 c.toggle();
202 }
203 if (getCheckedGrandchildren().size() == 0) {
204 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700205 }
206
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700207 return;
208 }
209 }
210
211 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700212 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700213 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700214 final ApplicationInfo app = (ApplicationInfo) v.getTag();
215
Winson Chung80baf5a2010-08-09 16:03:15 -0700216 // animate some feedback to the click
217 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700219 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700220 mLauncher.startActivitySafely(app.intent, app);
221 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700223
224 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700225 }
226 }
227
Patrick Dubroycd953712011-02-28 15:16:42 -0800228 private void setupDragMode(ApplicationInfo info) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800229 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE);
Patrick Dubroycd953712011-02-28 15:16:42 -0800230
231 // Only show the uninstall button if the app is uninstallable.
232 if ((info.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
233 DeleteZone allAppsDeleteZone = (DeleteZone)
234 mLauncher.findViewById(R.id.all_apps_delete_zone);
235 allAppsDeleteZone.setDragAndDropEnabled(true);
236
237 if ((info.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
238 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
239 } else {
240 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
241 }
242 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800243
Adam Cohencdc30d52010-12-01 15:09:47 -0800244 ApplicationInfoDropTarget allAppsInfoButton =
245 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
246 allAppsInfoButton.setDragAndDropEnabled(true);
Adam Cohencdc30d52010-12-01 15:09:47 -0800247 }
248
249 private void tearDownDragMode() {
250 post(new Runnable() {
251 // Once the drag operation has fully completed, hence the post, we want to disable the
252 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
253 // live in the workspace
254 public void run() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800255 DeleteZone allAppsDeleteZone =
256 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
Michael Jurkac31820d2011-01-16 16:57:05 -0800257 // if onDestroy was called on Launcher, we might have already deleted the
258 // all apps delete zone / info button, so check if they are null
259 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
Michael Jurkab8e14472010-12-20 16:06:10 -0800260
Adam Cohencdc30d52010-12-01 15:09:47 -0800261 ApplicationInfoDropTarget allAppsInfoButton =
262 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
Michael Jurkac31820d2011-01-16 16:57:05 -0800263 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800264 }
265 });
266 resetCheckedGrandchildren();
267 mDragController.removeDropTarget(this);
268 }
269
Winson Chung321e9ee2010-08-09 13:37:56 -0700270 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800271 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800272 if (!v.isInTouchMode()) return false;
273 if (!super.beginDragging(v)) return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700274
275 ApplicationInfo app = (ApplicationInfo) v.getTag();
276 app = new ApplicationInfo(app);
277
Patrick Dubroycd953712011-02-28 15:16:42 -0800278 // Start drag mode after the item is selected
279 setupDragMode(app);
280
Michael Jurkad3ef3062010-11-23 16:23:58 -0800281 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800282 final TextView tv = (TextView) v;
283 final Drawable icon = tv.getCompoundDrawables()[1];
284 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Michael Jurkad3ef3062010-11-23 16:23:58 -0800285 Bitmap.Config.ARGB_8888);
286 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800287 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800288 icon.draw(c);
Winson Chungcd4bc492010-12-09 18:52:32 -0800289
Adam Cohene3e27a82011-04-15 12:07:39 -0700290 Rect dragRect = null;
291 if (v instanceof TextView) {
292 int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size);
293 int top = v.getPaddingTop();
294 int left = (b.getWidth() - iconSize) / 2;
295 int right = left + iconSize;
296 int bottom = top + iconSize;
297 dragRect = new Rect(left, top, right, bottom);
298 }
299
Winson Chungcd4bc492010-12-09 18:52:32 -0800300 // We toggle the checked state _after_ we create the view for the drag in case toggling the
301 // checked state changes the view's look
302 if (v instanceof Checkable) {
303 // In preparation for drag, we always reset the checked grand children regardless of
304 // what choice mode we are in
305 resetCheckedGrandchildren();
306
307 // Toggle the selection on the dragged app
308 Checkable checkable = (Checkable) v;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800309
310 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
311 // of the drag of the item. (The fade-in will occur when all checked states are
312 // disabled when dragging ends)
Winson Chungcd4bc492010-12-09 18:52:32 -0800313 checkable.toggle();
314 }
315
316 // Start the drag
Winson Chung400438b2011-01-16 17:53:48 -0800317 mLauncher.lockScreenOrientation();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800318 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Adam Cohene3e27a82011-04-15 12:07:39 -0700319 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, dragRect);
Winson Chungcd4bc492010-12-09 18:52:32 -0800320 b.recycle();
Winson Chung321e9ee2010-08-09 13:37:56 -0700321 return true;
322 }
323
324 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800325 public void onDragViewVisible() {
326 }
327
328 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800329 public void onDropCompleted(View target, Object dragInfo, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700330 // close the choice action mode if we have a proper drop
331 if (target != this) {
332 endChoiceMode();
333 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800334 tearDownDragMode();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800335 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800336 mLauncher.unlockScreenOrientation();
Winson Chung20f71112011-04-06 14:22:04 -0700337
338 if (!success && !mHasMadeSuccessfulDrop) {
339 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_HIDDEN);
340 } else {
341 mHasMadeSuccessfulDrop |= success;
342 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700343 }
344
Michael Jurka7ef959b2011-02-23 11:48:32 -0800345 int getPageContentWidth() {
346 return mPageContentWidth;
347 }
348
Winson Chung321e9ee2010-08-09 13:37:56 -0700349 @Override
350 public boolean isVisible() {
351 return mZoom > 0.001f;
352 }
353
354 @Override
355 public boolean isAnimating() {
356 return (getAnimation() != null);
357 }
358
359 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
360 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
361 if (mAppFilter == ALL_APPS_FLAG) {
362 return apps;
363 } else {
364 final int length = apps.size();
365 for (int i = 0; i < length; ++i) {
366 ApplicationInfo info = apps.get(i);
367 if ((info.flags & mAppFilter) > 0) {
368 filteredApps.add(info);
369 }
370 }
Winson Chung78403fe2011-01-21 15:38:02 -0800371 Collections.sort(filteredApps, LauncherModel.APP_INSTALL_TIME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700372 }
373 return filteredApps;
374 }
375
376 @Override
377 public void setApps(ArrayList<ApplicationInfo> list) {
378 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700379 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700380 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung04998342011-01-05 13:54:43 -0800381 mPageViewIconCache.retainAllApps(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700382 invalidatePageData();
383 }
384
Winson Chung80baf5a2010-08-09 16:03:15 -0700385 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
386 // we add it in place, in alphabetical order
387 final int count = list.size();
388 for (int i = 0; i < count; ++i) {
389 final ApplicationInfo info = list.get(i);
390 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
391 if (index < 0) {
392 mApps.add(-(index + 1), info);
Winson Chung452821f2011-01-14 16:39:47 -0800393 } else {
394 mApps.add(index, info);
Winson Chung80baf5a2010-08-09 16:03:15 -0700395 }
396 }
397 mFilteredApps = rebuildFilteredApps(mApps);
398 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700399 @Override
400 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700401 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700402 invalidatePageData();
403 }
404
Winson Chung80baf5a2010-08-09 16:03:15 -0700405 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung10fefb12010-11-01 11:57:06 -0700406 // End the choice mode if any of the items in the list that are being removed are
407 // currently selected
408 ArrayList<Checkable> checkedList = getCheckedGrandchildren();
409 HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>();
410 for (Checkable checked : checkedList) {
411 PagedViewIcon icon = (PagedViewIcon) checked;
412 checkedAppInfos.add((ApplicationInfo) icon.getTag());
413 }
414 for (ApplicationInfo info : list) {
415 if (checkedAppInfos.contains(info)) {
416 endChoiceMode();
417 break;
418 }
419 }
420
421 // Loop through all the apps and remove apps that have the same component
Winson Chung321e9ee2010-08-09 13:37:56 -0700422 final int length = list.size();
423 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700424 final ApplicationInfo info = list.get(i);
425 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700426 if (removeIndex > -1) {
427 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800428 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung321e9ee2010-08-09 13:37:56 -0700429 }
430 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700431 mFilteredApps = rebuildFilteredApps(mApps);
432 }
Michael Jurkaabded662011-03-04 12:06:57 -0800433
Winson Chung80baf5a2010-08-09 16:03:15 -0700434 @Override
435 public void removeApps(ArrayList<ApplicationInfo> list) {
436 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700437 invalidatePageData();
438 }
439
440 @Override
441 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700442 removeAppsWithoutInvalidate(list);
443 addAppsWithoutInvalidate(list);
444 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700445 }
446
447 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
Winson Chung8b534782011-02-23 13:43:59 -0800448 if (item != null && item.intent != null) {
449 ComponentName removeComponent = item.intent.getComponent();
450 final int length = list.size();
451 for (int i = 0; i < length; ++i) {
452 ApplicationInfo info = list.get(i);
453 if (info.intent.getComponent().equals(removeComponent)) {
454 return i;
455 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700456 }
457 }
458 return -1;
459 }
460
461 @Override
462 public void dumpState() {
463 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
464 }
465
466 @Override
467 public void surrender() {
468 // do nothing?
469 }
470
Winson Chung337cd9d2011-03-30 10:39:30 -0700471 public void reset() {
472 setCurrentPage(0);
473 invalidatePageData();
474 }
475
Michael Jurka7ef959b2011-02-23 11:48:32 -0800476 private void setupPage(PagedViewCellLayout layout) {
477 layout.setCellCount(mCellCountX, mCellCountY);
478 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
479 mPageLayoutPaddingBottom);
480 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
481 }
482
Winson Chung321e9ee2010-08-09 13:37:56 -0700483 @Override
484 public void syncPages() {
Winson Chung96785572010-10-14 13:37:13 -0700485 // ensure that we have the right number of pages (min of 1, since we have placeholders)
486 int numPages = Math.max(1,
487 (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)));
Winson Chung321e9ee2010-08-09 13:37:56 -0700488 int curNumPages = getChildCount();
489 // remove any extra pages after the "last" page
490 int extraPageDiff = curNumPages - numPages;
491 for (int i = 0; i < extraPageDiff; ++i) {
492 removeViewAt(numPages);
493 }
494 // add any necessary pages
495 for (int i = curNumPages; i < numPages; ++i) {
496 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
Michael Jurkaabded662011-03-04 12:06:57 -0800497 if (mAllowHardwareLayerCreation) {
498 layout.allowHardwareLayerCreation();
499 }
Michael Jurka7ef959b2011-02-23 11:48:32 -0800500 setupPage(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -0700501 addView(layout);
502 }
503
504 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700505 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700506 }
507
508 @Override
509 public void syncPageItems(int page) {
Winson Chung96785572010-10-14 13:37:13 -0700510 // Ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700511 final int cellsPerPage = mCellCountX * mCellCountY;
512 final int startIndex = page * cellsPerPage;
513 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700514 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700515
Winson Chung96785572010-10-14 13:37:13 -0700516 if (!mFilteredApps.isEmpty()) {
Michael Jurka8245a862011-02-01 17:53:59 -0800517 int curNumPageItems = layout.getPageChildCount();
Winson Chung96785572010-10-14 13:37:13 -0700518 int numPageItems = endIndex - startIndex;
Winson Chung80baf5a2010-08-09 16:03:15 -0700519
Winson Chung96785572010-10-14 13:37:13 -0700520 // If we were previously an empty page, then restart anew
521 boolean wasEmptyPage = false;
522 if (curNumPageItems == 1) {
Michael Jurka8245a862011-02-01 17:53:59 -0800523 View icon = layout.getChildOnPageAt(0);
Winson Chung96785572010-10-14 13:37:13 -0700524 if (icon.getTag() == null) {
525 wasEmptyPage = true;
526 }
527 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700528
Winson Chung96785572010-10-14 13:37:13 -0700529 if (wasEmptyPage) {
530 // Remove all the previous items
531 curNumPageItems = 0;
Michael Jurka8245a862011-02-01 17:53:59 -0800532 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700533 } else {
534 // Remove any extra items
535 int extraPageItemsDiff = curNumPageItems - numPageItems;
536 for (int i = 0; i < extraPageItemsDiff; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800537 layout.removeViewOnPageAt(numPageItems);
Winson Chung96785572010-10-14 13:37:13 -0700538 }
539 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700540
Winson Chung96785572010-10-14 13:37:13 -0700541 // Add any necessary items
542 for (int i = curNumPageItems; i < numPageItems; ++i) {
543 TextView text = (TextView) mInflater.inflate(
544 R.layout.all_apps_paged_view_application, layout, false);
545 text.setOnClickListener(this);
546 text.setOnLongClickListener(this);
Michael Jurka72b079e2010-12-10 01:03:53 -0800547 text.setOnTouchListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700548
Winson Chung96785572010-10-14 13:37:13 -0700549 layout.addViewToCellLayout(text, -1, i,
550 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
551 }
552
553 // Actually reapply to the existing text views
Winson Chung04998342011-01-05 13:54:43 -0800554 final int numPages = getPageCount();
Winson Chung96785572010-10-14 13:37:13 -0700555 for (int i = startIndex; i < endIndex; ++i) {
556 final int index = i - startIndex;
557 final ApplicationInfo info = mFilteredApps.get(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800558 PagedViewIcon icon = (PagedViewIcon) layout.getChildOnPageAt(index);
Michael Jurkab9b8ce92011-05-05 15:05:07 -0700559 icon.applyFromApplicationInfo(
560 info, mPageViewIconCache, true, isHardwareAccelerated() && (numPages > 1));
Winson Chung96785572010-10-14 13:37:13 -0700561
562 PagedViewCellLayout.LayoutParams params =
563 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
564 params.cellX = index % mCellCountX;
565 params.cellY = index / mCellCountX;
566 }
567
568 // Default to left-aligned icons
569 layout.enableCenteredContent(false);
570 } else {
571 // There are no items, so show the user a small message
572 TextView icon = (TextView) mInflater.inflate(
573 R.layout.all_apps_no_items_placeholder, layout, false);
574 switch (mAppFilter) {
Winson Chung96785572010-10-14 13:37:13 -0700575 case ApplicationInfo.DOWNLOADED_FLAG:
576 icon.setText(mContext.getString(R.string.all_apps_no_downloads));
577 break;
578 default: break;
579 }
580
581 // Center-align the message
582 layout.enableCenteredContent(true);
Michael Jurka8245a862011-02-01 17:53:59 -0800583 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700584 layout.addViewToCellLayout(icon, -1, 0,
Winson Chung532a52a2011-01-18 12:18:00 -0800585 new PagedViewCellLayout.LayoutParams(0, 0, 4, 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700586 }
Michael Jurkac5e49022011-02-16 12:04:02 -0800587 layout.createHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700588 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700589
590 /*
591 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
592 * to the workspace.
593 */
594 @Override
595 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
596 DragView dragView, Object dragInfo) {
597 return false;
598 }
599 @Override
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700600 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
601 int yOffset, DragView dragView, Object dragInfo) {
602 return null;
603 }
604 @Override
605 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
606 DragView dragView, Object dragInfo) {}
607 @Override
608 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
609 DragView dragView, Object dragInfo) {}
610 @Override
611 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
612 DragView dragView, Object dragInfo) {}
613 @Override
614 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
615 DragView dragView, Object dragInfo) {}
Michael Jurka0280c3b2010-09-17 15:00:07 -0700616
617 public boolean isDropEnabled() {
618 return true;
619 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700620}