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