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; |
| 71 | |
| 72 | private final LayoutInflater mInflater; |
| 73 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame^] | 74 | private ViewGroup mOrigInfoButtonParent; |
| 75 | private LayoutParams mOrigInfoButtonLayoutParams; |
| 76 | |
| 77 | private ViewGroup mOrigDeleteZoneParent; |
| 78 | private LayoutParams mOrigDeleteZoneLayoutParams; |
| 79 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 80 | public AllAppsPagedView(Context context) { |
| 81 | this(context, null); |
| 82 | } |
| 83 | |
| 84 | public AllAppsPagedView(Context context, AttributeSet attrs) { |
| 85 | this(context, attrs, 0); |
| 86 | } |
| 87 | |
| 88 | public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) { |
| 89 | super(context, attrs, defStyle); |
| 90 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0); |
| 91 | mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6); |
| 92 | mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4); |
| 93 | mInflater = LayoutInflater.from(context); |
| 94 | a.recycle(); |
| 95 | setSoundEffectsEnabled(false); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 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) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 159 | final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 160 | if (layout.indexOfChild(v) > -1) { |
| 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 | |
| 206 | @Override |
| 207 | public boolean onLongClick(View v) { |
| 208 | if (!v.isInTouchMode()) { |
| 209 | return false; |
| 210 | } |
| 211 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 212 | if (v instanceof Checkable) { |
| 213 | // In preparation for drag, we always reset the checked grand children regardless of |
| 214 | // what choice mode we are in |
| 215 | resetCheckedGrandchildren(); |
| 216 | |
| 217 | // Toggle the selection on the dragged app |
| 218 | Checkable c = (Checkable) v; |
| 219 | c.toggle(); |
| 220 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 221 | |
Patrick Dubroy | 430c53b | 2010-09-08 16:01:19 -0700 | [diff] [blame] | 222 | // Start choice mode AFTER the item is selected |
| 223 | if (isChoiceMode(CHOICE_MODE_NONE)) { |
| 224 | startChoiceMode(CHOICE_MODE_SINGLE, this); |
| 225 | } |
| 226 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 227 | ApplicationInfo app = (ApplicationInfo) v.getTag(); |
| 228 | app = new ApplicationInfo(app); |
| 229 | |
| 230 | mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void onDropCompleted(View target, boolean success) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 236 | // close the choice action mode if we have a proper drop |
| 237 | if (target != this) { |
| 238 | endChoiceMode(); |
| 239 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public boolean isVisible() { |
| 244 | return mZoom > 0.001f; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public boolean isAnimating() { |
| 249 | return (getAnimation() != null); |
| 250 | } |
| 251 | |
| 252 | private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) { |
| 253 | ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>(); |
| 254 | if (mAppFilter == ALL_APPS_FLAG) { |
| 255 | return apps; |
| 256 | } else { |
| 257 | final int length = apps.size(); |
| 258 | for (int i = 0; i < length; ++i) { |
| 259 | ApplicationInfo info = apps.get(i); |
| 260 | if ((info.flags & mAppFilter) > 0) { |
| 261 | filteredApps.add(info); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | return filteredApps; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 270 | mApps = list; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 271 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 272 | mFilteredApps = rebuildFilteredApps(mApps); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 273 | mPageViewIconCache.clear(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 274 | invalidatePageData(); |
| 275 | } |
| 276 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 277 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 278 | // we add it in place, in alphabetical order |
| 279 | final int count = list.size(); |
| 280 | for (int i = 0; i < count; ++i) { |
| 281 | final ApplicationInfo info = list.get(i); |
| 282 | final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 283 | if (index < 0) { |
| 284 | mApps.add(-(index + 1), info); |
| 285 | } |
| 286 | } |
| 287 | mFilteredApps = rebuildFilteredApps(mApps); |
| 288 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 289 | @Override |
| 290 | public void addApps(ArrayList<ApplicationInfo> list) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 291 | addAppsWithoutInvalidate(list); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 292 | invalidatePageData(); |
| 293 | } |
| 294 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 295 | private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 296 | // loop through all the apps and remove apps that have the same component |
| 297 | final int length = list.size(); |
| 298 | for (int i = 0; i < length; ++i) { |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 299 | final ApplicationInfo info = list.get(i); |
| 300 | int removeIndex = findAppByComponent(mApps, info); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 301 | if (removeIndex > -1) { |
| 302 | mApps.remove(removeIndex); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 303 | mPageViewIconCache.removeOutline(info); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 306 | mFilteredApps = rebuildFilteredApps(mApps); |
| 307 | } |
| 308 | @Override |
| 309 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 310 | removeAppsWithoutInvalidate(list); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 311 | invalidatePageData(); |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public void updateApps(ArrayList<ApplicationInfo> list) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 316 | removeAppsWithoutInvalidate(list); |
| 317 | addAppsWithoutInvalidate(list); |
| 318 | invalidatePageData(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) { |
| 322 | ComponentName removeComponent = item.intent.getComponent(); |
| 323 | final int length = list.size(); |
| 324 | for (int i = 0; i < length; ++i) { |
| 325 | ApplicationInfo info = list.get(i); |
| 326 | if (info.intent.getComponent().equals(removeComponent)) { |
| 327 | return i; |
| 328 | } |
| 329 | } |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void dumpState() { |
| 335 | ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps); |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void surrender() { |
| 340 | // do nothing? |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void syncPages() { |
| 345 | // ensure that we have the right number of pages |
| 346 | int numPages = (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)); |
| 347 | int curNumPages = getChildCount(); |
| 348 | // remove any extra pages after the "last" page |
| 349 | int extraPageDiff = curNumPages - numPages; |
| 350 | for (int i = 0; i < extraPageDiff; ++i) { |
| 351 | removeViewAt(numPages); |
| 352 | } |
| 353 | // add any necessary pages |
| 354 | for (int i = curNumPages; i < numPages; ++i) { |
| 355 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 356 | layout.setCellCount(mCellCountX, mCellCountY); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 357 | addView(layout); |
| 358 | } |
| 359 | |
| 360 | // bound the current page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 361 | setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage()))); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public void syncPageItems(int page) { |
| 366 | // ensure that we have the right number of items on the pages |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 367 | final int cellsPerPage = mCellCountX * mCellCountY; |
| 368 | final int startIndex = page * cellsPerPage; |
| 369 | final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size()); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 370 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 371 | |
| 372 | final int curNumPageItems = layout.getChildCount(); |
| 373 | final int numPageItems = endIndex - startIndex; |
| 374 | |
| 375 | // remove any extra items |
| 376 | int extraPageItemsDiff = curNumPageItems - numPageItems; |
| 377 | for (int i = 0; i < extraPageItemsDiff; ++i) { |
| 378 | layout.removeViewAt(numPageItems); |
| 379 | } |
| 380 | // add any necessary items |
| 381 | for (int i = curNumPageItems; i < numPageItems; ++i) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 382 | 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] | 383 | text.setOnClickListener(this); |
| 384 | text.setOnLongClickListener(this); |
| 385 | |
| 386 | layout.addViewToCellLayout(text, -1, i, |
| 387 | new PagedViewCellLayout.LayoutParams(0, 0, 1, 1)); |
| 388 | } |
| 389 | |
| 390 | // actually reapply to the existing text views |
| 391 | for (int i = startIndex; i < endIndex; ++i) { |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 392 | final int index = i - startIndex; |
| 393 | final ApplicationInfo info = mFilteredApps.get(i); |
| 394 | PagedViewIcon icon = (PagedViewIcon) layout.getChildAt(index); |
| 395 | icon.applyFromApplicationInfo(info, mPageViewIconCache); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 396 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 397 | PagedViewCellLayout.LayoutParams params = |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 398 | (PagedViewCellLayout.LayoutParams) icon.getLayoutParams(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 399 | params.cellX = index % mCellCountX; |
| 400 | params.cellY = index / mCellCountX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 403 | |
| 404 | @Override |
| 405 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 406 | mode.setTitle(R.string.cab_selection_text); |
Patrick Dubroy | 430c53b | 2010-09-08 16:01:19 -0700 | [diff] [blame] | 407 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame^] | 408 | // Until the workspace has a selection mode and the CAB supports drag-and-drop, we |
| 409 | // take a hybrid approach: grab the views from the workspace and stuff them into the CAB. |
| 410 | // When the action mode is done, restore the views to their original place in the toolbar. |
| 411 | |
| 412 | ApplicationInfoDropTarget infoButton = |
| 413 | (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.info_button); |
| 414 | mOrigInfoButtonParent = (ViewGroup) infoButton.getParent(); |
| 415 | mOrigInfoButtonLayoutParams = infoButton.getLayoutParams(); |
| 416 | mOrigInfoButtonParent.removeView(infoButton); |
| 417 | infoButton.setManageVisibility(false); |
| 418 | infoButton.setVisibility(View.VISIBLE); |
| 419 | |
| 420 | DeleteZone deleteZone = (DeleteZone) mLauncher.findViewById(R.id.delete_zone); |
| 421 | mOrigDeleteZoneParent = (ViewGroup) deleteZone.getParent(); |
| 422 | mOrigDeleteZoneLayoutParams = deleteZone.getLayoutParams(); |
| 423 | mOrigDeleteZoneParent.removeView(deleteZone); |
| 424 | deleteZone.setManageVisibility(false); |
| 425 | deleteZone.setVisibility(View.VISIBLE); |
| 426 | |
| 427 | menu.add(0, MENU_APP_INFO, 0, R.string.cab_menu_app_info).setActionView(infoButton); |
| 428 | menu.add(0, MENU_DELETE_APP, 0, R.string.cab_menu_delete_app).setActionView(deleteZone); |
| 429 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 430 | return true; |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 435 | mDragController.addDropTarget(this); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public void onDestroyActionMode(ActionMode mode) { |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame^] | 441 | // Re-parent the drop targets into the toolbar, and restore their layout params |
| 442 | ApplicationInfoDropTarget infoButton = |
| 443 | (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.info_button); |
| 444 | ((ViewGroup) infoButton.getParent()).removeView(infoButton); |
| 445 | mOrigInfoButtonParent.addView(infoButton, mOrigInfoButtonLayoutParams); |
| 446 | infoButton.setVisibility(View.GONE); |
| 447 | infoButton.setManageVisibility(true); |
| 448 | |
| 449 | DeleteZone deleteZone = (DeleteZone) mLauncher.findViewById(R.id.delete_zone); |
| 450 | ((ViewGroup) deleteZone.getParent()).removeView(deleteZone); |
| 451 | mOrigDeleteZoneParent.addView(deleteZone, mOrigDeleteZoneLayoutParams); |
| 452 | deleteZone.setVisibility(View.GONE); |
| 453 | deleteZone.setManageVisibility(true); |
| 454 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 455 | mDragController.removeDropTarget(this); |
| 456 | endChoiceMode(); |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 461 | final int id = item.getItemId(); |
| 462 | |
| 463 | // Assumes that we are in CHOICE_MODE_SINGLE |
Patrick Dubroy | 430c53b | 2010-09-08 16:01:19 -0700 | [diff] [blame] | 464 | final ApplicationInfo appInfo = (ApplicationInfo) getChosenItem(); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 465 | |
| 466 | if (id == MENU_APP_INFO) { |
| 467 | mLauncher.startApplicationDetailsActivity(appInfo.componentName); |
| 468 | } else if (id == MENU_DELETE_APP) { |
Patrick Dubroy | 5539af7 | 2010-09-07 15:22:01 -0700 | [diff] [blame] | 469 | mLauncher.startApplicationUninstallActivity(appInfo); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 470 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 471 | return false; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * We don't actually use AllAppsPagedView as a drop target... it's only used to intercept a drop |
| 476 | * to the workspace. |
| 477 | */ |
| 478 | @Override |
| 479 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 480 | DragView dragView, Object dragInfo) { |
| 481 | return false; |
| 482 | } |
| 483 | @Override |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 484 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, |
| 485 | int yOffset, DragView dragView, Object dragInfo) { |
| 486 | return null; |
| 487 | } |
| 488 | @Override |
| 489 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 490 | DragView dragView, Object dragInfo) {} |
| 491 | @Override |
| 492 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 493 | DragView dragView, Object dragInfo) {} |
| 494 | @Override |
| 495 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 496 | DragView dragView, Object dragInfo) {} |
| 497 | @Override |
| 498 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 499 | DragView dragView, Object dragInfo) {} |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 500 | } |