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; |
| 27 | import android.graphics.drawable.Drawable; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 28 | import android.util.AttributeSet; |
| 29 | import android.view.LayoutInflater; |
| 30 | import android.view.View; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 31 | import android.view.animation.AnimationUtils; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 32 | import android.widget.Checkable; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 33 | import android.widget.TextView; |
| 34 | |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.Collections; |
| 37 | import java.util.HashSet; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * An implementation of PagedView that populates the pages of the workspace |
| 41 | * with all of the user's applications. |
| 42 | */ |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 43 | public class AllAppsPagedView extends PagedViewWithDraggableItems implements AllAppsView, |
| 44 | View.OnClickListener, DragSource, DropTarget { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 45 | |
| 46 | private static final String TAG = "AllAppsPagedView"; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 47 | |
| 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 Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 65 | private final LayoutInflater mInflater; |
| 66 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 67 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 68 | public AllAppsPagedView(Context context) { |
| 69 | this(context, null); |
| 70 | } |
| 71 | |
| 72 | public AllAppsPagedView(Context context, AttributeSet attrs) { |
| 73 | this(context, attrs, 0); |
| 74 | } |
| 75 | |
| 76 | public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) { |
| 77 | super(context, attrs, defStyle); |
| 78 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0); |
| 79 | mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6); |
| 80 | mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4); |
| 81 | mInflater = LayoutInflater.from(context); |
| 82 | a.recycle(); |
| 83 | setSoundEffectsEnabled(false); |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 84 | |
| 85 | Resources r = context.getResources(); |
| 86 | setDragSlopeThreshold( |
| 87 | r.getInteger(R.integer.config_allAppsDrawerDragSlopeThreshold) / 100.0f); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 91 | protected void init() { |
| 92 | super.init(); |
| 93 | mCenterPagesVertically = false; |
| 94 | } |
| 95 | |
| 96 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 97 | public void setLauncher(Launcher launcher) { |
| 98 | mLauncher = launcher; |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 99 | mLauncher.setAllAppsPagedView(this); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void setDragController(DragController dragger) { |
| 104 | mDragController = dragger; |
| 105 | } |
| 106 | |
| 107 | public void setAppFilter(int filterType) { |
| 108 | mAppFilter = filterType; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 109 | if (mApps != null) { |
| 110 | mFilteredApps = rebuildFilteredApps(mApps); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 111 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 112 | invalidatePageData(); |
| 113 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void zoom(float zoom, boolean animate) { |
| 118 | mZoom = zoom; |
| 119 | cancelLongPress(); |
| 120 | |
| 121 | if (isVisible()) { |
| 122 | getParent().bringChildToFront(this); |
| 123 | setVisibility(View.VISIBLE); |
| 124 | if (animate) { |
| 125 | startAnimation(AnimationUtils.loadAnimation(getContext(), |
| 126 | R.anim.all_apps_2d_fade_in)); |
| 127 | } else { |
| 128 | onAnimationEnd(); |
| 129 | } |
| 130 | } else { |
| 131 | if (animate) { |
| 132 | startAnimation(AnimationUtils.loadAnimation(getContext(), |
| 133 | R.anim.all_apps_2d_fade_out)); |
| 134 | } else { |
| 135 | onAnimationEnd(); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | protected void onAnimationEnd() { |
| 141 | if (!isVisible()) { |
| 142 | setVisibility(View.GONE); |
| 143 | mZoom = 0.0f; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 144 | |
| 145 | endChoiceMode(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 146 | } else { |
| 147 | mZoom = 1.0f; |
| 148 | } |
| 149 | |
| 150 | if (mLauncher != null) |
| 151 | mLauncher.zoomed(mZoom); |
| 152 | } |
| 153 | |
| 154 | private int getChildIndexForGrandChild(View v) { |
| 155 | final int childCount = getChildCount(); |
| 156 | for (int i = 0; i < childCount; ++i) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 157 | final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 158 | if (layout.indexOfChild(v) > -1) { |
| 159 | return i; |
| 160 | } |
| 161 | } |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void onClick(View v) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 167 | // if we are already in a choice mode, then just change the selection |
| 168 | if (v instanceof Checkable) { |
| 169 | if (!isChoiceMode(CHOICE_MODE_NONE)) { |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 170 | Checkable c = (Checkable) v; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 171 | if (isChoiceMode(CHOICE_MODE_SINGLE)) { |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 172 | // Uncheck all the other grandchildren, and toggle the clicked one |
| 173 | boolean wasChecked = c.isChecked(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 174 | resetCheckedGrandchildren(); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 175 | c.setChecked(!wasChecked); |
| 176 | } else { |
| 177 | c.toggle(); |
| 178 | } |
| 179 | if (getCheckedGrandchildren().size() == 0) { |
| 180 | endChoiceMode(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // otherwise continue and launch the application |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 188 | int childIndex = getChildIndexForGrandChild(v); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 189 | if (childIndex == getCurrentPage()) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 190 | final ApplicationInfo app = (ApplicationInfo) v.getTag(); |
| 191 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 192 | // animate some feedback to the click |
| 193 | animateClickFeedback(v, new Runnable() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 194 | @Override |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 195 | public void run() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 196 | mLauncher.startActivitySafely(app.intent, app); |
| 197 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 198 | }); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 199 | |
| 200 | endChoiceMode(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 204 | private void setupDragMode() { |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 205 | mLauncher.getWorkspace().shrink(Workspace.ShrinkState.BOTTOM_VISIBLE); |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 206 | DeleteZone allAppsDeleteZone = (DeleteZone) |
| 207 | mLauncher.findViewById(R.id.all_apps_delete_zone); |
| 208 | allAppsDeleteZone.setDragAndDropEnabled(true); |
| 209 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 210 | ApplicationInfoDropTarget allAppsInfoButton = |
| 211 | (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target); |
| 212 | allAppsInfoButton.setDragAndDropEnabled(true); |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 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() { |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 221 | DeleteZone allAppsDeleteZone = |
| 222 | (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone); |
Michael Jurka | c31820d | 2011-01-16 16:57:05 -0800 | [diff] [blame] | 223 | // if onDestroy was called on Launcher, we might have already deleted the |
| 224 | // all apps delete zone / info button, so check if they are null |
| 225 | if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false); |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 226 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 227 | ApplicationInfoDropTarget allAppsInfoButton = |
| 228 | (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target); |
Michael Jurka | c31820d | 2011-01-16 16:57:05 -0800 | [diff] [blame] | 229 | if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false); |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 230 | } |
| 231 | }); |
| 232 | resetCheckedGrandchildren(); |
| 233 | mDragController.removeDropTarget(this); |
| 234 | } |
| 235 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 236 | @Override |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 237 | protected boolean beginDragging(View v) { |
Winson Chung | 304dcde | 2011-01-07 11:17:23 -0800 | [diff] [blame] | 238 | if (!v.isInTouchMode()) return false; |
| 239 | if (!super.beginDragging(v)) return false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 240 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 241 | // Start drag mode after the item is selected |
| 242 | setupDragMode(); |
Patrick Dubroy | 430c53b | 2010-09-08 16:01:19 -0700 | [diff] [blame] | 243 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 244 | ApplicationInfo app = (ApplicationInfo) v.getTag(); |
| 245 | app = new ApplicationInfo(app); |
| 246 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 247 | // get icon (top compound drawable, index is 1) |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 248 | final TextView tv = (TextView) v; |
| 249 | final Drawable icon = tv.getCompoundDrawables()[1]; |
| 250 | Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 251 | Bitmap.Config.ARGB_8888); |
| 252 | Canvas c = new Canvas(b); |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 253 | c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop()); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 254 | icon.draw(c); |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 255 | |
| 256 | // We toggle the checked state _after_ we create the view for the drag in case toggling the |
| 257 | // checked state changes the view's look |
| 258 | if (v instanceof Checkable) { |
| 259 | // In preparation for drag, we always reset the checked grand children regardless of |
| 260 | // what choice mode we are in |
| 261 | resetCheckedGrandchildren(); |
| 262 | |
| 263 | // Toggle the selection on the dragged app |
| 264 | Checkable checkable = (Checkable) v; |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 265 | |
| 266 | // Note: we toggle the checkable state to actually cause an alpha fade for the duration |
| 267 | // of the drag of the item. (The fade-in will occur when all checked states are |
| 268 | // disabled when dragging ends) |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 269 | checkable.toggle(); |
| 270 | } |
| 271 | |
| 272 | // Start the drag |
Winson Chung | 400438b | 2011-01-16 17:53:48 -0800 | [diff] [blame] | 273 | mLauncher.lockScreenOrientation(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 274 | mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b); |
| 275 | mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null); |
Winson Chung | cd4bc49 | 2010-12-09 18:52:32 -0800 | [diff] [blame] | 276 | b.recycle(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 277 | return true; |
| 278 | } |
| 279 | |
| 280 | @Override |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 281 | public void onDragViewVisible() { |
| 282 | } |
| 283 | |
| 284 | @Override |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 285 | public void onDropCompleted(View target, boolean success) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 286 | // close the choice action mode if we have a proper drop |
| 287 | if (target != this) { |
| 288 | endChoiceMode(); |
| 289 | } |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 290 | tearDownDragMode(); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 291 | mLauncher.getWorkspace().onDragStopped(); |
Winson Chung | 400438b | 2011-01-16 17:53:48 -0800 | [diff] [blame] | 292 | mLauncher.unlockScreenOrientation(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public boolean isVisible() { |
| 297 | return mZoom > 0.001f; |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public boolean isAnimating() { |
| 302 | return (getAnimation() != null); |
| 303 | } |
| 304 | |
| 305 | private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) { |
| 306 | ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>(); |
| 307 | if (mAppFilter == ALL_APPS_FLAG) { |
| 308 | return apps; |
| 309 | } else { |
| 310 | final int length = apps.size(); |
| 311 | for (int i = 0; i < length; ++i) { |
| 312 | ApplicationInfo info = apps.get(i); |
| 313 | if ((info.flags & mAppFilter) > 0) { |
| 314 | filteredApps.add(info); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | return filteredApps; |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 323 | mApps = list; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 324 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 325 | mFilteredApps = rebuildFilteredApps(mApps); |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 326 | mPageViewIconCache.retainAllApps(list); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 327 | invalidatePageData(); |
| 328 | } |
| 329 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 330 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 331 | // we add it in place, in alphabetical order |
| 332 | final int count = list.size(); |
| 333 | for (int i = 0; i < count; ++i) { |
| 334 | final ApplicationInfo info = list.get(i); |
| 335 | final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 336 | if (index < 0) { |
| 337 | mApps.add(-(index + 1), info); |
Winson Chung | 452821f | 2011-01-14 16:39:47 -0800 | [diff] [blame] | 338 | } else { |
| 339 | mApps.add(index, info); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 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 | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 373 | mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(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 |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 484 | final int numPages = getPageCount(); |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 485 | for (int i = startIndex; i < endIndex; ++i) { |
| 486 | final int index = i - startIndex; |
| 487 | final ApplicationInfo info = mFilteredApps.get(i); |
| 488 | PagedViewIcon icon = (PagedViewIcon) layout.getChildAt(index); |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 489 | icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1)); |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 490 | |
| 491 | PagedViewCellLayout.LayoutParams params = |
| 492 | (PagedViewCellLayout.LayoutParams) icon.getLayoutParams(); |
| 493 | params.cellX = index % mCellCountX; |
| 494 | params.cellY = index / mCellCountX; |
| 495 | } |
| 496 | |
| 497 | // Default to left-aligned icons |
| 498 | layout.enableCenteredContent(false); |
| 499 | } else { |
| 500 | // There are no items, so show the user a small message |
| 501 | TextView icon = (TextView) mInflater.inflate( |
| 502 | R.layout.all_apps_no_items_placeholder, layout, false); |
| 503 | switch (mAppFilter) { |
Winson Chung | 9678557 | 2010-10-14 13:37:13 -0700 | [diff] [blame] | 504 | case ApplicationInfo.DOWNLOADED_FLAG: |
| 505 | icon.setText(mContext.getString(R.string.all_apps_no_downloads)); |
| 506 | break; |
| 507 | default: break; |
| 508 | } |
| 509 | |
| 510 | // Center-align the message |
| 511 | layout.enableCenteredContent(true); |
| 512 | layout.removeAllViews(); |
| 513 | layout.addViewToCellLayout(icon, -1, 0, |
| 514 | new PagedViewCellLayout.LayoutParams(0, 0, 2, 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop |
| 520 | * to the workspace. |
| 521 | */ |
| 522 | @Override |
| 523 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 524 | DragView dragView, Object dragInfo) { |
| 525 | return false; |
| 526 | } |
| 527 | @Override |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 528 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, |
| 529 | int yOffset, DragView dragView, Object dragInfo) { |
| 530 | return null; |
| 531 | } |
| 532 | @Override |
| 533 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 534 | DragView dragView, Object dragInfo) {} |
| 535 | @Override |
| 536 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 537 | DragView dragView, Object dragInfo) {} |
| 538 | @Override |
| 539 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 540 | DragView dragView, Object dragInfo) {} |
| 541 | @Override |
| 542 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 543 | DragView dragView, Object dragInfo) {} |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 544 | |
| 545 | public boolean isDropEnabled() { |
| 546 | return true; |
| 547 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 548 | } |