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