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