blob: 59ba57bdfa387c20afed1c484b6e6ca0892df0f9 [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 Jurka4c6016f2011-05-17 18:21:03 -070069 private boolean mFirstMeasure = true;
70
Michael Jurka7ef959b2011-02-23 11:48:32 -080071 private int mPageContentWidth;
Winson Chung20f71112011-04-06 14:22:04 -070072 private boolean mHasMadeSuccessfulDrop;
Patrick Dubroydea9e932010-09-22 15:04:29 -070073
Winson Chung321e9ee2010-08-09 13:37:56 -070074 public AllAppsPagedView(Context context) {
75 this(context, null);
76 }
77
78 public AllAppsPagedView(Context context, AttributeSet attrs) {
79 this(context, attrs, 0);
80 }
81
82 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
83 super(context, attrs, defStyle);
84 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
85 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
86 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
87 mInflater = LayoutInflater.from(context);
Winson Chung8b534782011-02-23 13:43:59 -080088 mApps = new ArrayList<ApplicationInfo>();
89 mFilteredApps = new ArrayList<ApplicationInfo>();
Winson Chung321e9ee2010-08-09 13:37:56 -070090 a.recycle();
91 setSoundEffectsEnabled(false);
Michael Jurka72b079e2010-12-10 01:03:53 -080092
93 Resources r = context.getResources();
94 setDragSlopeThreshold(
Winson Chung785d2eb2011-04-14 16:08:02 -070095 r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold) / 100.0f);
Winson Chung321e9ee2010-08-09 13:37:56 -070096 }
97
98 @Override
Winson Chung7da10252010-10-28 16:07:04 -070099 protected void init() {
100 super.init();
101 mCenterPagesVertically = false;
102 }
103
Michael Jurka4c6016f2011-05-17 18:21:03 -0700104 @Override
105 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
106 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
107
108 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
109 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
110
111 if (mFirstMeasure) {
112 mFirstMeasure = false;
113
114 // TODO: actually calculate mCellCountX/mCellCountY as some function of
115 // widthSize and heightSize
116 //mCellCountX = ?;
117 //mCellCountY = ?;
118
119 // Since mCellCountX/mCellCountY changed, we need to update the pages
120 invalidatePageData();
121
122 // Create a dummy page and set it up to find out the content width (used by our parent)
123 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
124 setupPage(layout);
125 mPageContentWidth = layout.getContentWidth();
126 }
127 }
128
Michael Jurkaabded662011-03-04 12:06:57 -0800129 void allowHardwareLayerCreation() {
130 // This is called after the first time we launch into All Apps. Before that point,
131 // there's no need for hardware layers here since there's a hardware layer set on the
132 // parent, AllAppsTabbed, during the AllApps transition -- creating hardware layers here
133 // before the animation is done slows down the animation
134 if (mAllowHardwareLayerCreation) {
135 return;
136 }
137 mAllowHardwareLayerCreation = true;
138 int childCount = getChildCount();
139 for (int i = 0; i < childCount; i++) {
140 PagedViewCellLayout page = (PagedViewCellLayout) getChildAt(i);
141 page.allowHardwareLayerCreation();
142 }
143 }
144
Winson Chung7da10252010-10-28 16:07:04 -0700145 @Override
Winson Chung785d2eb2011-04-14 16:08:02 -0700146 public void setup(Launcher launcher, DragController dragController) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700147 mLauncher = launcher;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700148 mLauncher.setAllAppsPagedView(this);
Winson Chung785d2eb2011-04-14 16:08:02 -0700149 mDragController = dragController;
Winson Chung321e9ee2010-08-09 13:37:56 -0700150 }
151
152 public void setAppFilter(int filterType) {
153 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700154 if (mApps != null) {
155 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -0700156 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700157 invalidatePageData();
158 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 }
160
Winson Chung20f71112011-04-06 14:22:04 -0700161 void resetSuccessfulDropFlag() {
162 mHasMadeSuccessfulDrop = false;
163 }
164
Winson Chung321e9ee2010-08-09 13:37:56 -0700165 @Override
166 public void zoom(float zoom, boolean animate) {
167 mZoom = zoom;
168 cancelLongPress();
169
170 if (isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700171 if (animate) {
172 startAnimation(AnimationUtils.loadAnimation(getContext(),
173 R.anim.all_apps_2d_fade_in));
174 } else {
175 onAnimationEnd();
176 }
177 } else {
178 if (animate) {
179 startAnimation(AnimationUtils.loadAnimation(getContext(),
180 R.anim.all_apps_2d_fade_out));
181 } else {
182 onAnimationEnd();
183 }
184 }
185 }
186
187 protected void onAnimationEnd() {
188 if (!isVisible()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700189 mZoom = 0.0f;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700190
191 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700192 } else {
193 mZoom = 1.0f;
194 }
195
196 if (mLauncher != null)
197 mLauncher.zoomed(mZoom);
198 }
199
200 private int getChildIndexForGrandChild(View v) {
201 final int childCount = getChildCount();
202 for (int i = 0; i < childCount; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800203 final Page layout = (Page) getChildAt(i);
204 if (layout.indexOfChildOnPage(v) > -1) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700205 return i;
206 }
207 }
208 return -1;
209 }
210
211 @Override
212 public void onClick(View v) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700213 // if we are already in a choice mode, then just change the selection
214 if (v instanceof Checkable) {
215 if (!isChoiceMode(CHOICE_MODE_NONE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700216 Checkable c = (Checkable) v;
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700217 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700218 // Uncheck all the other grandchildren, and toggle the clicked one
219 boolean wasChecked = c.isChecked();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700220 resetCheckedGrandchildren();
Patrick Dubroy9f7aec82010-09-06 11:03:37 -0700221 c.setChecked(!wasChecked);
222 } else {
223 c.toggle();
224 }
225 if (getCheckedGrandchildren().size() == 0) {
226 endChoiceMode();
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700227 }
228
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700229 return;
230 }
231 }
232
233 // otherwise continue and launch the application
Winson Chung321e9ee2010-08-09 13:37:56 -0700234 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700235 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 final ApplicationInfo app = (ApplicationInfo) v.getTag();
237
Winson Chung80baf5a2010-08-09 16:03:15 -0700238 // animate some feedback to the click
239 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700240 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700241 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 mLauncher.startActivitySafely(app.intent, app);
243 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700244 });
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700245
246 endChoiceMode();
Winson Chung321e9ee2010-08-09 13:37:56 -0700247 }
248 }
249
Patrick Dubroycd953712011-02-28 15:16:42 -0800250 private void setupDragMode(ApplicationInfo info) {
Michael Jurkaaf91de02010-11-23 16:23:58 -0800251 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE);
Patrick Dubroycd953712011-02-28 15:16:42 -0800252
253 // Only show the uninstall button if the app is uninstallable.
254 if ((info.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) {
255 DeleteZone allAppsDeleteZone = (DeleteZone)
256 mLauncher.findViewById(R.id.all_apps_delete_zone);
257 allAppsDeleteZone.setDragAndDropEnabled(true);
258
259 if ((info.flags & ApplicationInfo.UPDATED_SYSTEM_APP_FLAG) != 0) {
260 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps_system_app);
261 } else {
262 allAppsDeleteZone.setText(R.string.delete_zone_label_all_apps);
263 }
264 }
Michael Jurkab8e14472010-12-20 16:06:10 -0800265
Adam Cohencdc30d52010-12-01 15:09:47 -0800266 ApplicationInfoDropTarget allAppsInfoButton =
267 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
268 allAppsInfoButton.setDragAndDropEnabled(true);
Adam Cohencdc30d52010-12-01 15:09:47 -0800269 }
270
271 private void tearDownDragMode() {
272 post(new Runnable() {
273 // Once the drag operation has fully completed, hence the post, we want to disable the
274 // deleteZone and the appInfoButton in all apps, and re-enable the instance which
275 // live in the workspace
276 public void run() {
Michael Jurkab8e14472010-12-20 16:06:10 -0800277 DeleteZone allAppsDeleteZone =
278 (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone);
Michael Jurkac31820d2011-01-16 16:57:05 -0800279 // if onDestroy was called on Launcher, we might have already deleted the
280 // all apps delete zone / info button, so check if they are null
281 if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false);
Michael Jurkab8e14472010-12-20 16:06:10 -0800282
Adam Cohencdc30d52010-12-01 15:09:47 -0800283 ApplicationInfoDropTarget allAppsInfoButton =
284 (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target);
Michael Jurkac31820d2011-01-16 16:57:05 -0800285 if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false);
Adam Cohencdc30d52010-12-01 15:09:47 -0800286 }
287 });
288 resetCheckedGrandchildren();
289 mDragController.removeDropTarget(this);
290 }
291
Winson Chung321e9ee2010-08-09 13:37:56 -0700292 @Override
Michael Jurka72b079e2010-12-10 01:03:53 -0800293 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800294 if (!v.isInTouchMode()) return false;
295 if (!super.beginDragging(v)) return false;
Winson Chung321e9ee2010-08-09 13:37:56 -0700296
297 ApplicationInfo app = (ApplicationInfo) v.getTag();
298 app = new ApplicationInfo(app);
299
Patrick Dubroycd953712011-02-28 15:16:42 -0800300 // Start drag mode after the item is selected
301 setupDragMode(app);
302
Michael Jurkad3ef3062010-11-23 16:23:58 -0800303 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800304 final TextView tv = (TextView) v;
305 final Drawable icon = tv.getCompoundDrawables()[1];
306 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Michael Jurkad3ef3062010-11-23 16:23:58 -0800307 Bitmap.Config.ARGB_8888);
308 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800309 c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkad3ef3062010-11-23 16:23:58 -0800310 icon.draw(c);
Winson Chungcd4bc492010-12-09 18:52:32 -0800311
Adam Cohene3e27a82011-04-15 12:07:39 -0700312 Rect dragRect = null;
313 if (v instanceof TextView) {
314 int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size);
315 int top = v.getPaddingTop();
316 int left = (b.getWidth() - iconSize) / 2;
317 int right = left + iconSize;
318 int bottom = top + iconSize;
319 dragRect = new Rect(left, top, right, bottom);
320 }
321
Winson Chungcd4bc492010-12-09 18:52:32 -0800322 // We toggle the checked state _after_ we create the view for the drag in case toggling the
323 // checked state changes the view's look
324 if (v instanceof Checkable) {
325 // In preparation for drag, we always reset the checked grand children regardless of
326 // what choice mode we are in
327 resetCheckedGrandchildren();
328
329 // Toggle the selection on the dragged app
330 Checkable checkable = (Checkable) v;
Winson Chung59e1f9a2010-12-21 11:31:54 -0800331
332 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
333 // of the drag of the item. (The fade-in will occur when all checked states are
334 // disabled when dragging ends)
Winson Chungcd4bc492010-12-09 18:52:32 -0800335 checkable.toggle();
336 }
337
338 // Start the drag
Winson Chung400438b2011-01-16 17:53:48 -0800339 mLauncher.lockScreenOrientation();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800340 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Adam Cohene3e27a82011-04-15 12:07:39 -0700341 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, dragRect);
Winson Chungcd4bc492010-12-09 18:52:32 -0800342 b.recycle();
Winson Chung321e9ee2010-08-09 13:37:56 -0700343 return true;
344 }
345
346 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800347 public void onDragViewVisible() {
348 }
349
350 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800351 public void onDropCompleted(View target, Object dragInfo, boolean success) {
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700352 // close the choice action mode if we have a proper drop
353 if (target != this) {
354 endChoiceMode();
355 }
Adam Cohencdc30d52010-12-01 15:09:47 -0800356 tearDownDragMode();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800357 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800358 mLauncher.unlockScreenOrientation();
Winson Chung20f71112011-04-06 14:22:04 -0700359
360 if (!success && !mHasMadeSuccessfulDrop) {
361 mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_HIDDEN);
362 } else {
363 mHasMadeSuccessfulDrop |= success;
364 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700365 }
366
Michael Jurka7ef959b2011-02-23 11:48:32 -0800367 int getPageContentWidth() {
368 return mPageContentWidth;
369 }
370
Winson Chung321e9ee2010-08-09 13:37:56 -0700371 @Override
372 public boolean isVisible() {
373 return mZoom > 0.001f;
374 }
375
376 @Override
377 public boolean isAnimating() {
378 return (getAnimation() != null);
379 }
380
381 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
382 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
383 if (mAppFilter == ALL_APPS_FLAG) {
384 return apps;
385 } else {
386 final int length = apps.size();
387 for (int i = 0; i < length; ++i) {
388 ApplicationInfo info = apps.get(i);
389 if ((info.flags & mAppFilter) > 0) {
390 filteredApps.add(info);
391 }
392 }
Winson Chung78403fe2011-01-21 15:38:02 -0800393 Collections.sort(filteredApps, LauncherModel.APP_INSTALL_TIME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700394 }
395 return filteredApps;
396 }
397
398 @Override
399 public void setApps(ArrayList<ApplicationInfo> list) {
400 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700401 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700402 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung04998342011-01-05 13:54:43 -0800403 mPageViewIconCache.retainAllApps(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700404 invalidatePageData();
405 }
406
Winson Chung80baf5a2010-08-09 16:03:15 -0700407 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
408 // we add it in place, in alphabetical order
409 final int count = list.size();
410 for (int i = 0; i < count; ++i) {
411 final ApplicationInfo info = list.get(i);
412 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
413 if (index < 0) {
414 mApps.add(-(index + 1), info);
Winson Chung452821f2011-01-14 16:39:47 -0800415 } else {
416 mApps.add(index, info);
Winson Chung80baf5a2010-08-09 16:03:15 -0700417 }
418 }
419 mFilteredApps = rebuildFilteredApps(mApps);
420 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700421 @Override
422 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700423 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700424 invalidatePageData();
425 }
426
Winson Chung80baf5a2010-08-09 16:03:15 -0700427 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung10fefb12010-11-01 11:57:06 -0700428 // End the choice mode if any of the items in the list that are being removed are
429 // currently selected
430 ArrayList<Checkable> checkedList = getCheckedGrandchildren();
431 HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>();
432 for (Checkable checked : checkedList) {
433 PagedViewIcon icon = (PagedViewIcon) checked;
434 checkedAppInfos.add((ApplicationInfo) icon.getTag());
435 }
436 for (ApplicationInfo info : list) {
437 if (checkedAppInfos.contains(info)) {
438 endChoiceMode();
439 break;
440 }
441 }
442
443 // Loop through all the apps and remove apps that have the same component
Winson Chung321e9ee2010-08-09 13:37:56 -0700444 final int length = list.size();
445 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700446 final ApplicationInfo info = list.get(i);
447 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700448 if (removeIndex > -1) {
449 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800450 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung321e9ee2010-08-09 13:37:56 -0700451 }
452 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700453 mFilteredApps = rebuildFilteredApps(mApps);
454 }
Michael Jurkaabded662011-03-04 12:06:57 -0800455
Winson Chung80baf5a2010-08-09 16:03:15 -0700456 @Override
457 public void removeApps(ArrayList<ApplicationInfo> list) {
458 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700459 invalidatePageData();
460 }
461
462 @Override
463 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700464 removeAppsWithoutInvalidate(list);
465 addAppsWithoutInvalidate(list);
466 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700467 }
468
469 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
Winson Chung8b534782011-02-23 13:43:59 -0800470 if (item != null && item.intent != null) {
471 ComponentName removeComponent = item.intent.getComponent();
472 final int length = list.size();
473 for (int i = 0; i < length; ++i) {
474 ApplicationInfo info = list.get(i);
475 if (info.intent.getComponent().equals(removeComponent)) {
476 return i;
477 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700478 }
479 }
480 return -1;
481 }
482
483 @Override
484 public void dumpState() {
485 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
486 }
487
488 @Override
489 public void surrender() {
490 // do nothing?
491 }
492
Winson Chung337cd9d2011-03-30 10:39:30 -0700493 public void reset() {
494 setCurrentPage(0);
495 invalidatePageData();
496 }
497
Michael Jurka7ef959b2011-02-23 11:48:32 -0800498 private void setupPage(PagedViewCellLayout layout) {
499 layout.setCellCount(mCellCountX, mCellCountY);
500 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
501 mPageLayoutPaddingBottom);
502 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
503 }
504
Winson Chung321e9ee2010-08-09 13:37:56 -0700505 @Override
506 public void syncPages() {
Michael Jurka4c6016f2011-05-17 18:21:03 -0700507 if (mFirstMeasure) {
508 // We don't know our size yet, which means we haven't calculated cell count x/y;
509 // onMeasure will call us once we figure out our size
510 return;
511 }
Winson Chung96785572010-10-14 13:37:13 -0700512 // ensure that we have the right number of pages (min of 1, since we have placeholders)
513 int numPages = Math.max(1,
514 (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)));
Winson Chung321e9ee2010-08-09 13:37:56 -0700515 int curNumPages = getChildCount();
516 // remove any extra pages after the "last" page
517 int extraPageDiff = curNumPages - numPages;
518 for (int i = 0; i < extraPageDiff; ++i) {
519 removeViewAt(numPages);
520 }
521 // add any necessary pages
522 for (int i = curNumPages; i < numPages; ++i) {
523 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
Michael Jurkaabded662011-03-04 12:06:57 -0800524 if (mAllowHardwareLayerCreation) {
525 layout.allowHardwareLayerCreation();
526 }
Michael Jurka7ef959b2011-02-23 11:48:32 -0800527 setupPage(layout);
Winson Chung321e9ee2010-08-09 13:37:56 -0700528 addView(layout);
529 }
530
531 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700532 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700533 }
534
535 @Override
536 public void syncPageItems(int page) {
Winson Chung96785572010-10-14 13:37:13 -0700537 // Ensure that we have the right number of items on the pages
Winson Chung63257c12011-05-05 17:06:13 -0700538 final int numPages = getPageCount();
Winson Chung80baf5a2010-08-09 16:03:15 -0700539 final int cellsPerPage = mCellCountX * mCellCountY;
540 final int startIndex = page * cellsPerPage;
541 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700542 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700543
Winson Chung96785572010-10-14 13:37:13 -0700544 if (!mFilteredApps.isEmpty()) {
Michael Jurka8245a862011-02-01 17:53:59 -0800545 int curNumPageItems = layout.getPageChildCount();
Winson Chung96785572010-10-14 13:37:13 -0700546 int numPageItems = endIndex - startIndex;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700547 boolean createHolographicOutlines = (numPages > 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700548
Winson Chung96785572010-10-14 13:37:13 -0700549 // If we were previously an empty page, then restart anew
550 boolean wasEmptyPage = false;
551 if (curNumPageItems == 1) {
Michael Jurka8245a862011-02-01 17:53:59 -0800552 View icon = layout.getChildOnPageAt(0);
Winson Chung96785572010-10-14 13:37:13 -0700553 if (icon.getTag() == null) {
554 wasEmptyPage = true;
555 }
556 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700557
Winson Chung96785572010-10-14 13:37:13 -0700558 if (wasEmptyPage) {
559 // Remove all the previous items
560 curNumPageItems = 0;
Michael Jurka8245a862011-02-01 17:53:59 -0800561 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700562 } else {
563 // Remove any extra items
564 int extraPageItemsDiff = curNumPageItems - numPageItems;
565 for (int i = 0; i < extraPageItemsDiff; ++i) {
Michael Jurka8245a862011-02-01 17:53:59 -0800566 layout.removeViewOnPageAt(numPageItems);
Winson Chung96785572010-10-14 13:37:13 -0700567 }
568 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700569
Winson Chung96785572010-10-14 13:37:13 -0700570 // Add any necessary items
571 for (int i = curNumPageItems; i < numPageItems; ++i) {
572 TextView text = (TextView) mInflater.inflate(
573 R.layout.all_apps_paged_view_application, layout, false);
574 text.setOnClickListener(this);
575 text.setOnLongClickListener(this);
Michael Jurka72b079e2010-12-10 01:03:53 -0800576 text.setOnTouchListener(this);
Winson Chung321e9ee2010-08-09 13:37:56 -0700577
Winson Chung96785572010-10-14 13:37:13 -0700578 layout.addViewToCellLayout(text, -1, i,
Winson Chung6a70e9f2011-05-17 16:24:49 -0700579 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
Winson Chung96785572010-10-14 13:37:13 -0700580 }
581
582 // Actually reapply to the existing text views
583 for (int i = startIndex; i < endIndex; ++i) {
584 final int index = i - startIndex;
585 final ApplicationInfo info = mFilteredApps.get(i);
Michael Jurka8245a862011-02-01 17:53:59 -0800586 PagedViewIcon icon = (PagedViewIcon) layout.getChildOnPageAt(index);
Michael Jurkab9b8ce92011-05-05 15:05:07 -0700587 icon.applyFromApplicationInfo(
Winson Chung63257c12011-05-05 17:06:13 -0700588 info, mPageViewIconCache, true, createHolographicOutlines);
Winson Chung96785572010-10-14 13:37:13 -0700589
590 PagedViewCellLayout.LayoutParams params =
591 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
592 params.cellX = index % mCellCountX;
593 params.cellY = index / mCellCountX;
594 }
595
Winson Chung6a70e9f2011-05-17 16:24:49 -0700596 // We should try and sync all the holographic icons after adding/removing new items
597 layout.reloadHolographicIcons(createHolographicOutlines);
598
Winson Chung96785572010-10-14 13:37:13 -0700599 // Default to left-aligned icons
600 layout.enableCenteredContent(false);
601 } else {
602 // There are no items, so show the user a small message
603 TextView icon = (TextView) mInflater.inflate(
604 R.layout.all_apps_no_items_placeholder, layout, false);
605 switch (mAppFilter) {
Winson Chung96785572010-10-14 13:37:13 -0700606 case ApplicationInfo.DOWNLOADED_FLAG:
607 icon.setText(mContext.getString(R.string.all_apps_no_downloads));
608 break;
609 default: break;
610 }
611
612 // Center-align the message
Winson Chungdd259022011-05-10 15:59:42 -0700613 final boolean createHolographicOutlines = (numPages > 1);
Winson Chung96785572010-10-14 13:37:13 -0700614 layout.enableCenteredContent(true);
Michael Jurka8245a862011-02-01 17:53:59 -0800615 layout.removeAllViewsOnPage();
Winson Chung96785572010-10-14 13:37:13 -0700616 layout.addViewToCellLayout(icon, -1, 0,
Winson Chung6a70e9f2011-05-17 16:24:49 -0700617 new PagedViewCellLayout.LayoutParams(0, 0, 4, 1));
Winson Chung321e9ee2010-08-09 13:37:56 -0700618 }
Michael Jurkac5e49022011-02-16 12:04:02 -0800619 layout.createHardwareLayers();
Winson Chung321e9ee2010-08-09 13:37:56 -0700620 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700621
622 /*
623 * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop
624 * to the workspace.
625 */
626 @Override
627 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
628 DragView dragView, Object dragInfo) {
629 return false;
630 }
631 @Override
Winson Chung5f2aa4e2010-08-20 14:49:25 -0700632 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
633 int yOffset, DragView dragView, Object dragInfo) {
634 return null;
635 }
636 @Override
637 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
638 DragView dragView, Object dragInfo) {}
639 @Override
640 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
641 DragView dragView, Object dragInfo) {}
642 @Override
643 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
644 DragView dragView, Object dragInfo) {}
645 @Override
646 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
647 DragView dragView, Object dragInfo) {}
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648
649 public boolean isDropEnabled() {
650 return true;
651 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700652}