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