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