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