Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame^] | 19 | import com.android.launcher.R; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 20 | |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.res.TypedArray; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
| 25 | import android.view.LayoutInflater; |
| 26 | import android.view.View; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 27 | import android.view.animation.AnimationUtils; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 28 | import android.widget.Checkable; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 29 | import android.widget.TextView; |
| 30 | |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame^] | 31 | import java.util.ArrayList; |
| 32 | import java.util.Collections; |
| 33 | import java.util.HashSet; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * An implementation of PagedView that populates the pages of the workspace |
| 37 | * with all of the user's applications. |
| 38 | */ |
| 39 | public class AllAppsPagedView extends PagedView |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 40 | implements AllAppsView, View.OnClickListener, View.OnLongClickListener, DragSource, |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 41 | DropTarget { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 42 | |
| 43 | private static final String TAG = "AllAppsPagedView"; |
| 44 | private static final boolean DEBUG = false; |
| 45 | |
| 46 | private Launcher mLauncher; |
| 47 | private DragController mDragController; |
| 48 | |
| 49 | // preserve compatibility with 3D all apps: |
| 50 | // 0.0 -> hidden |
| 51 | // 1.0 -> shown and opaque |
| 52 | // intermediate values -> partially shown & partially opaque |
| 53 | private float mZoom; |
| 54 | |
| 55 | // set of all applications |
| 56 | private ArrayList<ApplicationInfo> mApps; |
| 57 | private ArrayList<ApplicationInfo> mFilteredApps; |
| 58 | |
| 59 | // the types of applications to filter |
| 60 | static final int ALL_APPS_FLAG = -1; |
| 61 | private int mAppFilter = ALL_APPS_FLAG; |
| 62 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 63 | private final LayoutInflater mInflater; |
| 64 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 65 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 66 | public AllAppsPagedView(Context context) { |
| 67 | this(context, null); |
| 68 | } |
| 69 | |
| 70 | public AllAppsPagedView(Context context, AttributeSet attrs) { |
| 71 | this(context, attrs, 0); |
| 72 | } |
| 73 | |
| 74 | public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) { |
| 75 | super(context, attrs, defStyle); |
| 76 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0); |
| 77 | mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6); |
| 78 | mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4); |
| 79 | mInflater = LayoutInflater.from(context); |
| 80 | a.recycle(); |
| 81 | setSoundEffectsEnabled(false); |
| 82 | } |
| 83 | |
| 84 | @Override |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 85 | protected void init() { |
| 86 | super.init(); |
| 87 | mCenterPagesVertically = false; |
| 88 | } |
| 89 | |
| 90 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 91 | public void setLauncher(Launcher launcher) { |
| 92 | mLauncher = launcher; |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 93 | mLauncher.setAllAppsPagedView(this); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void setDragController(DragController dragger) { |
| 98 | mDragController = dragger; |
| 99 | } |
| 100 | |
| 101 | public void setAppFilter(int filterType) { |
| 102 | mAppFilter = filterType; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 103 | if (mApps != null) { |
| 104 | mFilteredApps = rebuildFilteredApps(mApps); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 105 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 106 | invalidatePageData(); |
| 107 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void zoom(float zoom, boolean animate) { |
| 112 | mZoom = zoom; |
| 113 | cancelLongPress(); |
| 114 | |
| 115 | if (isVisible()) { |
| 116 | getParent().bringChildToFront(this); |
| 117 | setVisibility(View.VISIBLE); |
| 118 | if (animate) { |
| 119 | startAnimation(AnimationUtils.loadAnimation(getContext(), |
| 120 | R.anim.all_apps_2d_fade_in)); |
| 121 | } else { |
| 122 | onAnimationEnd(); |
| 123 | } |
| 124 | } else { |
| 125 | if (animate) { |
| 126 | startAnimation(AnimationUtils.loadAnimation(getContext(), |
| 127 | R.anim.all_apps_2d_fade_out)); |
| 128 | } else { |
| 129 | onAnimationEnd(); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | protected void onAnimationEnd() { |
| 135 | if (!isVisible()) { |
| 136 | setVisibility(View.GONE); |
| 137 | mZoom = 0.0f; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 138 | |
| 139 | endChoiceMode(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 140 | } else { |
| 141 | mZoom = 1.0f; |
| 142 | } |
| 143 | |
| 144 | if (mLauncher != null) |
| 145 | mLauncher.zoomed(mZoom); |
| 146 | } |
| 147 | |
| 148 | private int getChildIndexForGrandChild(View v) { |
| 149 | final int childCount = getChildCount(); |
| 150 | for (int i = 0; i < childCount; ++i) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 151 | final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 152 | if (layout.indexOfChild(v) > -1) { |
| 153 | return i; |
| 154 | } |
| 155 | } |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onClick(View v) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 161 | // if we are already in a choice mode, then just change the selection |
| 162 | if (v instanceof Checkable) { |
| 163 | if (!isChoiceMode(CHOICE_MODE_NONE)) { |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 164 | Checkable c = (Checkable) v; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 165 | if (isChoiceMode(CHOICE_MODE_SINGLE)) { |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 166 | // Uncheck all the other grandchildren, and toggle the clicked one |
| 167 | boolean wasChecked = c.isChecked(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 168 | resetCheckedGrandchildren(); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 169 | c.setChecked(!wasChecked); |
| 170 | } else { |
| 171 | c.toggle(); |
| 172 | } |
| 173 | if (getCheckedGrandchildren().size() == 0) { |
| 174 | endChoiceMode(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // otherwise continue and launch the application |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 182 | int childIndex = getChildIndexForGrandChild(v); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 183 | if (childIndex == getCurrentPage()) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 184 | final ApplicationInfo app = (ApplicationInfo) v.getTag(); |
| 185 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 186 | // animate some feedback to the click |
| 187 | animateClickFeedback(v, new Runnable() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 188 | @Override |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 189 | public void run() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 190 | mLauncher.startActivitySafely(app.intent, app); |
| 191 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 192 | }); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 193 | |
| 194 | endChoiceMode(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 198 | private void setupDragMode() { |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame^] | 199 | mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE); |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 200 | |
| 201 | ApplicationInfoDropTarget infoButton = |
| 202 | (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.info_button); |
| 203 | infoButton.setDragAndDropEnabled(false); |
| 204 | DeleteZone deleteZone = (DeleteZone) mLauncher.findViewById(R.id.delete_zone); |
| 205 | deleteZone.setDragAndDropEnabled(false); |
| 206 | |
| 207 | ApplicationInfoDropTarget allAppsInfoButton = |
| 208 | (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target); |
| 209 | allAppsInfoButton.setDragAndDropEnabled(true); |
| 210 | DeleteZone allAppsDeleteZone = (DeleteZone) |
| 211 | mLauncher.findViewById(R.id.all_apps_delete_zone); |
| 212 | allAppsDeleteZone.setDragAndDropEnabled(true); |
| 213 | } |
| 214 | |
| 215 | private void tearDownDragMode() { |
| 216 | post(new Runnable() { |
| 217 | // Once the drag operation has fully completed, hence the post, we want to disable the |
| 218 | // deleteZone and the appInfoButton in all apps, and re-enable the instance which |
| 219 | // live in the workspace |
| 220 | public void run() { |
| 221 | ApplicationInfoDropTarget infoButton = |
| 222 | (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.info_button); |
| 223 | infoButton.setDragAndDropEnabled(true); |
| 224 | DeleteZone deleteZone = (DeleteZone) mLauncher.findViewById(R.id.delete_zone); |
| 225 | deleteZone.setDragAndDropEnabled(true); |
| 226 | |
| 227 | ApplicationInfoDropTarget allAppsInfoButton = |
| 228 | (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target); |
| 229 | allAppsInfoButton.setDragAndDropEnabled(false); |
| 230 | DeleteZone allAppsDeleteZone = |
| 231 | (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone); |
| 232 | allAppsDeleteZone.setDragAndDropEnabled(false); |
| 233 | } |
| 234 | }); |
| 235 | resetCheckedGrandchildren(); |
| 236 | mDragController.removeDropTarget(this); |
| 237 | } |
| 238 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 239 | @Override |
| 240 | public boolean onLongClick(View v) { |
| 241 | if (!v.isInTouchMode()) { |
| 242 | return false; |
| 243 | } |
| 244 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 245 | if (v instanceof Checkable) { |
| 246 | // In preparation for drag, we always reset the checked grand children regardless of |
| 247 | // what choice mode we are in |
| 248 | resetCheckedGrandchildren(); |
| 249 | |
| 250 | // Toggle the selection on the dragged app |
| 251 | Checkable c = (Checkable) v; |
| 252 | c.toggle(); |
| 253 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 254 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 255 | // Start drag mode after the item is selected |
| 256 | setupDragMode(); |
Patrick Dubroy | 430c53b | 2010-09-08 16:01:19 -0700 | [diff] [blame] | 257 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 258 | ApplicationInfo app = (ApplicationInfo) v.getTag(); |
| 259 | app = new ApplicationInfo(app); |
| 260 | |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 261 | mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 262 | mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | |
| 266 | @Override |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 267 | public void onDragViewVisible() { |
| 268 | } |
| 269 | |
| 270 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 271 | public void onDropCompleted(View target, boolean success) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 272 | // close the choice action mode if we have a proper drop |
| 273 | if (target != this) { |
| 274 | endChoiceMode(); |
| 275 | } |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 276 | tearDownDragMode(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 277 | mLauncher.getWorkspace().onDragStopped(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public boolean isVisible() { |
| 282 | return mZoom > 0.001f; |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public boolean isAnimating() { |
| 287 | return (getAnimation() != null); |
| 288 | } |
| 289 | |
| 290 | private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) { |
| 291 | ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>(); |
| 292 | if (mAppFilter == ALL_APPS_FLAG) { |
| 293 | return apps; |
| 294 | } else { |
| 295 | final int length = apps.size(); |
| 296 | for (int i = 0; i < length; ++i) { |
| 297 | ApplicationInfo info = apps.get(i); |
| 298 | if ((info.flags & mAppFilter) > 0) { |
| 299 | filteredApps.add(info); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | return filteredApps; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 308 | mApps = list; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 309 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 310 | mFilteredApps = rebuildFilteredApps(mApps); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 311 | mPageViewIconCache.clear(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 312 | invalidatePageData(); |
| 313 | } |
| 314 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 315 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 316 | // we add it in place, in alphabetical order |
| 317 | final int count = list.size(); |
| 318 | for (int i = 0; i < count; ++i) { |
| 319 | final ApplicationInfo info = list.get(i); |
| 320 | final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 321 | if (index < 0) { |
| 322 | mApps.add(-(index + 1), info); |
| 323 | } |
| 324 | } |
| 325 | mFilteredApps = rebuildFilteredApps(mApps); |
| 326 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 327 | @Override |
| 328 | public void addApps(ArrayList<ApplicationInfo> list) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 329 | addAppsWithoutInvalidate(list); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 330 | invalidatePageData(); |
| 331 | } |
| 332 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 333 | private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
Winson Chung | 10fefb1 | 2010-11-01 11:57:06 -0700 | [diff] [blame] | 334 | // End the choice mode if any of the items in the list that are being removed are |
| 335 | // currently selected |
| 336 | ArrayList<Checkable> checkedList = getCheckedGrandchildren(); |
| 337 | HashSet<ApplicationInfo> checkedAppInfos = new HashSet<ApplicationInfo>(); |
| 338 | for (Checkable checked : checkedList) { |
| 339 | PagedViewIcon icon = (PagedViewIcon) checked; |
| 340 | checkedAppInfos.add((ApplicationInfo) icon.getTag()); |
| 341 | } |
| 342 | for (ApplicationInfo info : list) { |
| 343 | if (checkedAppInfos.contains(info)) { |
| 344 | endChoiceMode(); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // Loop through all the apps and remove apps that have the same component |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 350 | final int length = list.size(); |
| 351 | for (int i = 0; i < length; ++i) { |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 352 | final ApplicationInfo info = list.get(i); |
| 353 | int removeIndex = findAppByComponent(mApps, info); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 354 | if (removeIndex > -1) { |
| 355 | mApps.remove(removeIndex); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 356 | mPageViewIconCache.removeOutline(info); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 359 | mFilteredApps = rebuildFilteredApps(mApps); |
| 360 | } |
| 361 | @Override |
| 362 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 363 | removeAppsWithoutInvalidate(list); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 364 | invalidatePageData(); |
| 365 | } |
| 366 | |
| 367 | @Override |
| 368 | public void updateApps(ArrayList<ApplicationInfo> list) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 369 | removeAppsWithoutInvalidate(list); |
| 370 | addAppsWithoutInvalidate(list); |
| 371 | invalidatePageData(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) { |
| 375 | ComponentName removeComponent = item.intent.getComponent(); |
| 376 | final int length = list.size(); |
| 377 | for (int i = 0; i < length; ++i) { |
| 378 | ApplicationInfo info = list.get(i); |
| 379 | if (info.intent.getComponent().equals(removeComponent)) { |
| 380 | return i; |
| 381 | } |
| 382 | } |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public void dumpState() { |
| 388 | ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps); |
| 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public void surrender() { |
| 393 | // do nothing? |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public void syncPages() { |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 398 | // ensure that we have the right number of pages (min of 1, since we have placeholders) |
| 399 | int numPages = Math.max(1, |
| 400 | (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY))); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 401 | int curNumPages = getChildCount(); |
| 402 | // remove any extra pages after the "last" page |
| 403 | int extraPageDiff = curNumPages - numPages; |
| 404 | for (int i = 0; i < extraPageDiff; ++i) { |
| 405 | removeViewAt(numPages); |
| 406 | } |
| 407 | // add any necessary pages |
| 408 | for (int i = curNumPages; i < numPages; ++i) { |
| 409 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 410 | layout.setCellCount(mCellCountX, mCellCountY); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 411 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 412 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 413 | layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 414 | addView(layout); |
| 415 | } |
| 416 | |
| 417 | // bound the current page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 418 | setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage()))); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public void syncPageItems(int page) { |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 423 | // Ensure that we have the right number of items on the pages |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 424 | final int cellsPerPage = mCellCountX * mCellCountY; |
| 425 | final int startIndex = page * cellsPerPage; |
| 426 | final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size()); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 427 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 428 | |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 429 | if (!mFilteredApps.isEmpty()) { |
| 430 | int curNumPageItems = layout.getChildCount(); |
| 431 | int numPageItems = endIndex - startIndex; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 432 | |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 433 | // If we were previously an empty page, then restart anew |
| 434 | boolean wasEmptyPage = false; |
| 435 | if (curNumPageItems == 1) { |
| 436 | View icon = layout.getChildAt(0); |
| 437 | if (icon.getTag() == null) { |
| 438 | wasEmptyPage = true; |
| 439 | } |
| 440 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 441 | |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 442 | if (wasEmptyPage) { |
| 443 | // Remove all the previous items |
| 444 | curNumPageItems = 0; |
| 445 | layout.removeAllViews(); |
| 446 | } else { |
| 447 | // Remove any extra items |
| 448 | int extraPageItemsDiff = curNumPageItems - numPageItems; |
| 449 | for (int i = 0; i < extraPageItemsDiff; ++i) { |
| 450 | layout.removeViewAt(numPageItems); |
| 451 | } |
| 452 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 453 | |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 454 | // Add any necessary items |
| 455 | for (int i = curNumPageItems; i < numPageItems; ++i) { |
| 456 | TextView text = (TextView) mInflater.inflate( |
| 457 | R.layout.all_apps_paged_view_application, layout, false); |
| 458 | text.setOnClickListener(this); |
| 459 | text.setOnLongClickListener(this); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 460 | |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 461 | layout.addViewToCellLayout(text, -1, i, |
| 462 | new PagedViewCellLayout.LayoutParams(0, 0, 1, 1)); |
| 463 | } |
| 464 | |
| 465 | // Actually reapply to the existing text views |
| 466 | for (int i = startIndex; i < endIndex; ++i) { |
| 467 | final int index = i - startIndex; |
| 468 | final ApplicationInfo info = mFilteredApps.get(i); |
| 469 | PagedViewIcon icon = (PagedViewIcon) layout.getChildAt(index); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 470 | icon.applyFromApplicationInfo(info, mPageViewIconCache, true); |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 471 | |
| 472 | PagedViewCellLayout.LayoutParams params = |
| 473 | (PagedViewCellLayout.LayoutParams) icon.getLayoutParams(); |
| 474 | params.cellX = index % mCellCountX; |
| 475 | params.cellY = index / mCellCountX; |
| 476 | } |
| 477 | |
| 478 | // Default to left-aligned icons |
| 479 | layout.enableCenteredContent(false); |
| 480 | } else { |
| 481 | // There are no items, so show the user a small message |
| 482 | TextView icon = (TextView) mInflater.inflate( |
| 483 | R.layout.all_apps_no_items_placeholder, layout, false); |
| 484 | switch (mAppFilter) { |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 485 | case ApplicationInfo.DOWNLOADED_FLAG: |
| 486 | icon.setText(mContext.getString(R.string.all_apps_no_downloads)); |
| 487 | break; |
| 488 | default: break; |
| 489 | } |
| 490 | |
| 491 | // Center-align the message |
| 492 | layout.enableCenteredContent(true); |
| 493 | layout.removeAllViews(); |
| 494 | layout.addViewToCellLayout(icon, -1, 0, |
| 495 | new PagedViewCellLayout.LayoutParams(0, 0, 2, 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 498 | |
| 499 | /* |
| 500 | * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop |
| 501 | * to the workspace. |
| 502 | */ |
| 503 | @Override |
| 504 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 505 | DragView dragView, Object dragInfo) { |
| 506 | return false; |
| 507 | } |
| 508 | @Override |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 509 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, |
| 510 | int yOffset, DragView dragView, Object dragInfo) { |
| 511 | return null; |
| 512 | } |
| 513 | @Override |
| 514 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 515 | DragView dragView, Object dragInfo) {} |
| 516 | @Override |
| 517 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 518 | DragView dragView, Object dragInfo) {} |
| 519 | @Override |
| 520 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 521 | DragView dragView, Object dragInfo) {} |
| 522 | @Override |
| 523 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 524 | DragView dragView, Object dragInfo) {} |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 525 | |
| 526 | public boolean isDropEnabled() { |
| 527 | return true; |
| 528 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 529 | } |