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 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collections; |
| 21 | import java.util.Comparator; |
| 22 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 23 | import android.animation.Animatable; |
| 24 | import android.animation.AnimatableListenerAdapter; |
| 25 | import android.animation.Animator; |
| 26 | import android.animation.PropertyAnimator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
| 29 | import android.content.res.TypedArray; |
| 30 | import android.graphics.drawable.BitmapDrawable; |
| 31 | import android.util.AttributeSet; |
| 32 | import android.view.LayoutInflater; |
| 33 | import android.view.View; |
| 34 | import android.view.animation.AlphaAnimation; |
| 35 | import android.view.animation.Animation; |
| 36 | import android.view.animation.AnimationUtils; |
| 37 | import android.view.animation.Animation.AnimationListener; |
| 38 | import android.widget.TextView; |
| 39 | |
| 40 | import com.android.launcher.R; |
| 41 | |
| 42 | /** |
| 43 | * An implementation of PagedView that populates the pages of the workspace |
| 44 | * with all of the user's applications. |
| 45 | */ |
| 46 | public class AllAppsPagedView extends PagedView |
| 47 | implements AllAppsView, View.OnClickListener, View.OnLongClickListener, DragSource, |
| 48 | PagedViewCellLayout.DimmedBitmapSetupListener { |
| 49 | |
| 50 | private static final String TAG = "AllAppsPagedView"; |
| 51 | private static final boolean DEBUG = false; |
| 52 | |
| 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 | |
| 70 | private int mCellCountX; |
| 71 | private int mCellCountY; |
| 72 | |
| 73 | private final LayoutInflater mInflater; |
| 74 | |
| 75 | public AllAppsPagedView(Context context) { |
| 76 | this(context, null); |
| 77 | } |
| 78 | |
| 79 | public AllAppsPagedView(Context context, AttributeSet attrs) { |
| 80 | this(context, attrs, 0); |
| 81 | } |
| 82 | |
| 83 | public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) { |
| 84 | super(context, attrs, defStyle); |
| 85 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0); |
| 86 | mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6); |
| 87 | mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4); |
| 88 | mInflater = LayoutInflater.from(context); |
| 89 | a.recycle(); |
| 90 | setSoundEffectsEnabled(false); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void setLauncher(Launcher launcher) { |
| 95 | mLauncher = launcher; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void setDragController(DragController dragger) { |
| 100 | mDragController = dragger; |
| 101 | } |
| 102 | |
| 103 | public void setAppFilter(int filterType) { |
| 104 | mAppFilter = filterType; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 105 | if (mApps != null) { |
| 106 | mFilteredApps = rebuildFilteredApps(mApps); |
| 107 | setCurrentScreen(0); |
| 108 | invalidatePageData(); |
| 109 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void zoom(float zoom, boolean animate) { |
| 114 | mZoom = zoom; |
| 115 | cancelLongPress(); |
| 116 | |
| 117 | if (isVisible()) { |
| 118 | getParent().bringChildToFront(this); |
| 119 | setVisibility(View.VISIBLE); |
| 120 | if (animate) { |
| 121 | startAnimation(AnimationUtils.loadAnimation(getContext(), |
| 122 | R.anim.all_apps_2d_fade_in)); |
| 123 | } else { |
| 124 | onAnimationEnd(); |
| 125 | } |
| 126 | } else { |
| 127 | if (animate) { |
| 128 | startAnimation(AnimationUtils.loadAnimation(getContext(), |
| 129 | R.anim.all_apps_2d_fade_out)); |
| 130 | } else { |
| 131 | onAnimationEnd(); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | protected void onAnimationEnd() { |
| 137 | if (!isVisible()) { |
| 138 | setVisibility(View.GONE); |
| 139 | mZoom = 0.0f; |
| 140 | } else { |
| 141 | mZoom = 1.0f; |
| 142 | } |
| 143 | |
| 144 | if (mLauncher != null) |
| 145 | mLauncher.zoomed(mZoom); |
| 146 | } |
| 147 | |
| 148 | private int getChildIndexForGrandChild(View v) { |
| 149 | final int childCount = getChildCount(); |
| 150 | for (int i = 0; i < childCount; ++i) { |
| 151 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
| 152 | if (layout.indexOfChild(v) > -1) { |
| 153 | return i; |
| 154 | } |
| 155 | } |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onClick(View v) { |
| 161 | int childIndex = getChildIndexForGrandChild(v); |
| 162 | if (childIndex == getCurrentScreen()) { |
| 163 | final ApplicationInfo app = (ApplicationInfo) v.getTag(); |
| 164 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 165 | // animate some feedback to the click |
| 166 | animateClickFeedback(v, new Runnable() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 167 | @Override |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 168 | public void run() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 169 | mLauncher.startActivitySafely(app.intent, app); |
| 170 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 171 | }); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public boolean onLongClick(View v) { |
| 177 | if (!v.isInTouchMode()) { |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | ApplicationInfo app = (ApplicationInfo) v.getTag(); |
| 182 | app = new ApplicationInfo(app); |
| 183 | |
| 184 | mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY); |
| 185 | mLauncher.closeAllApps(true); |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void onDropCompleted(View target, boolean success) { |
| 191 | // do nothing |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public boolean isVisible() { |
| 196 | return mZoom > 0.001f; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public boolean isAnimating() { |
| 201 | return (getAnimation() != null); |
| 202 | } |
| 203 | |
| 204 | private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) { |
| 205 | ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>(); |
| 206 | if (mAppFilter == ALL_APPS_FLAG) { |
| 207 | return apps; |
| 208 | } else { |
| 209 | final int length = apps.size(); |
| 210 | for (int i = 0; i < length; ++i) { |
| 211 | ApplicationInfo info = apps.get(i); |
| 212 | if ((info.flags & mAppFilter) > 0) { |
| 213 | filteredApps.add(info); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | return filteredApps; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 222 | mApps = list; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 223 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 224 | mFilteredApps = rebuildFilteredApps(mApps); |
| 225 | invalidatePageData(); |
| 226 | } |
| 227 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 228 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 229 | // we add it in place, in alphabetical order |
| 230 | final int count = list.size(); |
| 231 | for (int i = 0; i < count; ++i) { |
| 232 | final ApplicationInfo info = list.get(i); |
| 233 | final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 234 | if (index < 0) { |
| 235 | mApps.add(-(index + 1), info); |
| 236 | } |
| 237 | } |
| 238 | mFilteredApps = rebuildFilteredApps(mApps); |
| 239 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 240 | @Override |
| 241 | public void addApps(ArrayList<ApplicationInfo> list) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 242 | addAppsWithoutInvalidate(list); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 243 | invalidatePageData(); |
| 244 | } |
| 245 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 246 | private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 247 | // loop through all the apps and remove apps that have the same component |
| 248 | final int length = list.size(); |
| 249 | for (int i = 0; i < length; ++i) { |
| 250 | int removeIndex = findAppByComponent(mApps, list.get(i)); |
| 251 | if (removeIndex > -1) { |
| 252 | mApps.remove(removeIndex); |
| 253 | } |
| 254 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 255 | mFilteredApps = rebuildFilteredApps(mApps); |
| 256 | } |
| 257 | @Override |
| 258 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 259 | removeAppsWithoutInvalidate(list); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 260 | invalidatePageData(); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void updateApps(ArrayList<ApplicationInfo> list) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 265 | removeAppsWithoutInvalidate(list); |
| 266 | addAppsWithoutInvalidate(list); |
| 267 | invalidatePageData(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) { |
| 271 | ComponentName removeComponent = item.intent.getComponent(); |
| 272 | final int length = list.size(); |
| 273 | for (int i = 0; i < length; ++i) { |
| 274 | ApplicationInfo info = list.get(i); |
| 275 | if (info.intent.getComponent().equals(removeComponent)) { |
| 276 | return i; |
| 277 | } |
| 278 | } |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void dumpState() { |
| 284 | ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void surrender() { |
| 289 | // do nothing? |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public void syncPages() { |
| 294 | // ensure that we have the right number of pages |
| 295 | int numPages = (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY)); |
| 296 | int curNumPages = getChildCount(); |
| 297 | // remove any extra pages after the "last" page |
| 298 | int extraPageDiff = curNumPages - numPages; |
| 299 | for (int i = 0; i < extraPageDiff; ++i) { |
| 300 | removeViewAt(numPages); |
| 301 | } |
| 302 | // add any necessary pages |
| 303 | for (int i = curNumPages; i < numPages; ++i) { |
| 304 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 305 | layout.setCellCount(mCellCountX, mCellCountY); |
| 306 | layout.setDimmedBitmapSetupListener(this); |
| 307 | addView(layout); |
| 308 | } |
| 309 | |
| 310 | // bound the current page |
| 311 | setCurrentScreen(Math.max(0, Math.min(numPages - 1, getCurrentScreen()))); |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public void syncPageItems(int page) { |
| 316 | // ensure that we have the right number of items on the pages |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 317 | final int cellsPerPage = mCellCountX * mCellCountY; |
| 318 | final int startIndex = page * cellsPerPage; |
| 319 | final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size()); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 320 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 321 | |
| 322 | final int curNumPageItems = layout.getChildCount(); |
| 323 | final int numPageItems = endIndex - startIndex; |
| 324 | |
| 325 | // remove any extra items |
| 326 | int extraPageItemsDiff = curNumPageItems - numPageItems; |
| 327 | for (int i = 0; i < extraPageItemsDiff; ++i) { |
| 328 | layout.removeViewAt(numPageItems); |
| 329 | } |
| 330 | // add any necessary items |
| 331 | for (int i = curNumPageItems; i < numPageItems; ++i) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 332 | 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^] | 333 | text.setOnClickListener(this); |
| 334 | text.setOnLongClickListener(this); |
| 335 | |
| 336 | layout.addViewToCellLayout(text, -1, i, |
| 337 | new PagedViewCellLayout.LayoutParams(0, 0, 1, 1)); |
| 338 | } |
| 339 | |
| 340 | // actually reapply to the existing text views |
| 341 | for (int i = startIndex; i < endIndex; ++i) { |
| 342 | int index = i - startIndex; |
| 343 | ApplicationInfo info = mFilteredApps.get(i); |
| 344 | TextView text = (TextView) layout.getChildAt(index); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 345 | text.setCompoundDrawablesWithIntrinsicBounds(null, |
| 346 | new BitmapDrawable(info.iconBitmap), null, null); |
| 347 | text.setText(info.title); |
| 348 | text.setTag(info); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 349 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame^] | 350 | PagedViewCellLayout.LayoutParams params = |
| 351 | (PagedViewCellLayout.LayoutParams) text.getLayoutParams(); |
| 352 | params.cellX = index % mCellCountX; |
| 353 | params.cellY = index / mCellCountX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public void onPreUpdateDimmedBitmap(PagedViewCellLayout layout) { |
| 359 | // disable all children text for now |
| 360 | final int childCount = layout.getChildCount(); |
| 361 | for (int i = 0; i < childCount; ++i) { |
| 362 | TextView text = (TextView) layout.getChildAt(i); |
| 363 | text.setText(""); |
| 364 | } |
| 365 | } |
| 366 | @Override |
| 367 | public void onPostUpdateDimmedBitmap(PagedViewCellLayout layout) { |
| 368 | // re-enable all children text |
| 369 | final int childCount = layout.getChildCount(); |
| 370 | for (int i = 0; i < childCount; ++i) { |
| 371 | TextView text = (TextView) layout.getChildAt(i); |
| 372 | final ApplicationInfo info = (ApplicationInfo) text.getTag(); |
| 373 | text.setText(info.title); |
| 374 | } |
| 375 | } |
| 376 | } |