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