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