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