Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 19 | import android.animation.AnimatorSet; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 20 | import android.animation.ObjectAnimator; |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 22 | import android.appwidget.AppWidgetManager; |
| 23 | import android.appwidget.AppWidgetProviderInfo; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 27 | import android.content.pm.ActivityInfo; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.ResolveInfo; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 30 | import android.content.res.Configuration; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 31 | import android.content.res.Resources; |
| 32 | import android.content.res.TypedArray; |
| 33 | import android.graphics.Bitmap; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 34 | import android.graphics.Bitmap.Config; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 35 | import android.graphics.Canvas; |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 36 | import android.graphics.PorterDuff; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 37 | import android.graphics.Rect; |
| 38 | import android.graphics.drawable.Drawable; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 39 | import android.os.AsyncTask; |
| 40 | import android.os.Process; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 41 | import android.util.AttributeSet; |
| 42 | import android.util.Log; |
Winson Chung | 72d8b39 | 2011-07-29 13:56:44 -0700 | [diff] [blame] | 43 | import android.view.Gravity; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 44 | import android.view.LayoutInflater; |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 45 | import android.view.MotionEvent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 46 | import android.view.View; |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 47 | import android.view.ViewGroup; |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 48 | import android.view.animation.AccelerateInterpolator; |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 49 | import android.widget.GridLayout; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 50 | import android.widget.ImageView; |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 51 | import android.widget.Toast; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 52 | |
| 53 | import com.android.launcher.R; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 54 | import com.android.launcher2.DropTarget.DragObject; |
| 55 | |
| 56 | import java.util.ArrayList; |
| 57 | import java.util.Collections; |
| 58 | import java.util.Iterator; |
| 59 | import java.util.List; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 60 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 61 | /** |
| 62 | * A simple callback interface which also provides the results of the task. |
| 63 | */ |
| 64 | interface AsyncTaskCallback { |
| 65 | void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data); |
| 66 | } |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 67 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 68 | /** |
| 69 | * The data needed to perform either of the custom AsyncTasks. |
| 70 | */ |
| 71 | class AsyncTaskPageData { |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 72 | enum Type { |
| 73 | LoadWidgetPreviewData, |
| 74 | LoadHolographicIconsData |
| 75 | } |
| 76 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 77 | AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR, |
| 78 | AsyncTaskCallback postR) { |
| 79 | page = p; |
| 80 | items = l; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 81 | sourceImages = si; |
| 82 | generatedImages = new ArrayList<Bitmap>(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 83 | cellWidth = cellHeight = -1; |
| 84 | doInBackgroundCallback = bgR; |
| 85 | postExecuteCallback = postR; |
| 86 | } |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 87 | AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, int ccx, AsyncTaskCallback bgR, |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 88 | AsyncTaskCallback postR) { |
| 89 | page = p; |
| 90 | items = l; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 91 | generatedImages = new ArrayList<Bitmap>(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 92 | cellWidth = cw; |
| 93 | cellHeight = ch; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 94 | cellCountX = ccx; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 95 | doInBackgroundCallback = bgR; |
| 96 | postExecuteCallback = postR; |
| 97 | } |
| 98 | int page; |
| 99 | ArrayList<Object> items; |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 100 | ArrayList<Bitmap> sourceImages; |
| 101 | ArrayList<Bitmap> generatedImages; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 102 | int cellWidth; |
| 103 | int cellHeight; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 104 | int cellCountX; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 105 | AsyncTaskCallback doInBackgroundCallback; |
| 106 | AsyncTaskCallback postExecuteCallback; |
| 107 | } |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 108 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 109 | /** |
| 110 | * A generic template for an async task used in AppsCustomize. |
| 111 | */ |
| 112 | class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> { |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 113 | AppsCustomizeAsyncTask(int p, AppsCustomizePagedView.ContentType t, AsyncTaskPageData.Type ty) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 114 | page = p; |
| 115 | pageContentType = t; |
| 116 | threadPriority = Process.THREAD_PRIORITY_DEFAULT; |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 117 | dataType = ty; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 118 | } |
| 119 | @Override |
| 120 | protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) { |
| 121 | if (params.length != 1) return null; |
| 122 | // Load each of the widget previews in the background |
| 123 | params[0].doInBackgroundCallback.run(this, params[0]); |
| 124 | return params[0]; |
| 125 | } |
| 126 | @Override |
| 127 | protected void onPostExecute(AsyncTaskPageData result) { |
| 128 | // All the widget previews are loaded, so we can just callback to inflate the page |
| 129 | result.postExecuteCallback.run(this, result); |
| 130 | } |
| 131 | |
| 132 | void setThreadPriority(int p) { |
| 133 | threadPriority = p; |
| 134 | } |
| 135 | void syncThreadPriority() { |
| 136 | Process.setThreadPriority(threadPriority); |
| 137 | } |
| 138 | |
| 139 | // The page that this async task is associated with |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 140 | AsyncTaskPageData.Type dataType; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 141 | int page; |
| 142 | AppsCustomizePagedView.ContentType pageContentType; |
| 143 | int threadPriority; |
| 144 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * The Apps/Customize page that displays all the applications, widgets, and shortcuts. |
| 148 | */ |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 149 | public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements |
| 150 | AllAppsView, View.OnClickListener, DragSource { |
| 151 | static final String LOG_TAG = "AppsCustomizePagedView"; |
| 152 | |
| 153 | /** |
| 154 | * The different content types that this paged view can show. |
| 155 | */ |
| 156 | public enum ContentType { |
| 157 | Applications, |
Winson Chung | 6a26e5b | 2011-05-26 14:36:06 -0700 | [diff] [blame] | 158 | Widgets |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Refs |
| 162 | private Launcher mLauncher; |
| 163 | private DragController mDragController; |
| 164 | private final LayoutInflater mLayoutInflater; |
| 165 | private final PackageManager mPackageManager; |
| 166 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 167 | // Save and Restore |
| 168 | private int mSaveInstanceStateItemIndex = -1; |
| 169 | private int mRestorePage = -1; |
| 170 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 171 | // Content |
| 172 | private ContentType mContentType; |
| 173 | private ArrayList<ApplicationInfo> mApps; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 174 | private ArrayList<Object> mWidgets; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 175 | |
| 176 | // Caching |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 177 | private Canvas mCanvas; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 178 | private Drawable mDefaultWidgetBackground; |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 179 | private IconCache mIconCache; |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 180 | private int mDragViewMultiplyColor; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 181 | |
| 182 | // Dimens |
| 183 | private int mContentWidth; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 184 | private int mAppIconSize; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 185 | private int mMaxWidgetSpan, mMinWidgetSpan; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 186 | private int mWidgetCountX, mWidgetCountY; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 187 | private int mWidgetWidthGap, mWidgetHeightGap; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 188 | private final int mWidgetPreviewIconPaddedDimension; |
| 189 | private final float sWidgetPreviewIconPaddingPercentage = 0.25f; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 190 | private PagedViewCellLayout mWidgetSpacingLayout; |
| 191 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 192 | // Previews & outlines |
| 193 | ArrayList<AppsCustomizeAsyncTask> mRunningTasks; |
| 194 | private HolographicOutlineHelper mHolographicOutlineHelper; |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 195 | private static final int sPageSleepDelay = 200; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 196 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 197 | public AppsCustomizePagedView(Context context, AttributeSet attrs) { |
| 198 | super(context, attrs); |
| 199 | mLayoutInflater = LayoutInflater.from(context); |
| 200 | mPackageManager = context.getPackageManager(); |
| 201 | mContentType = ContentType.Applications; |
| 202 | mApps = new ArrayList<ApplicationInfo>(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 203 | mWidgets = new ArrayList<Object>(); |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 204 | mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 205 | mHolographicOutlineHelper = new HolographicOutlineHelper(); |
| 206 | mCanvas = new Canvas(); |
| 207 | mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 208 | |
| 209 | // Save the default widget preview background |
| 210 | Resources resources = context.getResources(); |
Winson Chung | 967289b | 2011-06-30 18:09:30 -0700 | [diff] [blame] | 211 | mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo); |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 212 | mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size); |
| 213 | mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 214 | |
| 215 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 216 | // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 217 | mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6); |
| 218 | mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4); |
| 219 | a.recycle(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 220 | a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 221 | mWidgetWidthGap = |
| 222 | a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0); |
| 223 | mWidgetHeightGap = |
| 224 | a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 225 | mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2); |
| 226 | mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2); |
| 227 | a.recycle(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 228 | mWidgetSpacingLayout = new PagedViewCellLayout(getContext()); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 229 | |
| 230 | // The max widget span is the length N, such that NxN is the largest bounds that the widget |
| 231 | // preview can be before applying the widget scaling |
| 232 | mMinWidgetSpan = 1; |
| 233 | mMaxWidgetSpan = 3; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 234 | |
| 235 | // The padding on the non-matched dimension for the default widget preview icons |
| 236 | // (top + bottom) |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 237 | mWidgetPreviewIconPaddedDimension = |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 238 | (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage))); |
Winson Chung | 54c725c | 2011-08-03 12:03:40 -0700 | [diff] [blame] | 239 | mFadeInAdjacentScreens = LauncherApplication.isScreenLarge(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | protected void init() { |
| 244 | super.init(); |
| 245 | mCenterPagesVertically = false; |
| 246 | |
| 247 | Context context = getContext(); |
| 248 | Resources r = context.getResources(); |
| 249 | setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f); |
| 250 | } |
| 251 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 252 | @Override |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 253 | protected void onUnhandledTap(MotionEvent ev) { |
| 254 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | de1af76 | 2011-07-21 16:44:07 -0700 | [diff] [blame] | 255 | // Dismiss AppsCustomize if we tap |
| 256 | mLauncher.showWorkspace(true); |
| 257 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 260 | /** Returns the item index of the center item on this page so that we can restore to this |
| 261 | * item index when we rotate. */ |
| 262 | private int getMiddleComponentIndexOnCurrentPage() { |
| 263 | int i = -1; |
| 264 | if (getPageCount() > 0) { |
| 265 | int currentPage = getCurrentPage(); |
| 266 | switch (mContentType) { |
| 267 | case Applications: { |
| 268 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(currentPage); |
| 269 | PagedViewCellLayoutChildren childrenLayout = layout.getChildrenLayout(); |
| 270 | int numItemsPerPage = mCellCountX * mCellCountY; |
| 271 | int childCount = childrenLayout.getChildCount(); |
| 272 | if (childCount > 0) { |
| 273 | i = (currentPage * numItemsPerPage) + (childCount / 2); |
| 274 | }} |
| 275 | break; |
| 276 | case Widgets: { |
| 277 | PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(currentPage); |
| 278 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
| 279 | int childCount = layout.getChildCount(); |
| 280 | if (childCount > 0) { |
| 281 | i = (currentPage * numItemsPerPage) + (childCount / 2); |
| 282 | }} |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | return i; |
| 287 | } |
| 288 | |
| 289 | /** Get the index of the item to restore to if we need to restore the current page. */ |
| 290 | int getSaveInstanceStateIndex() { |
| 291 | if (mSaveInstanceStateItemIndex == -1) { |
| 292 | mSaveInstanceStateItemIndex = getMiddleComponentIndexOnCurrentPage(); |
| 293 | } |
| 294 | return mSaveInstanceStateItemIndex; |
| 295 | } |
| 296 | |
| 297 | /** Returns the page in the current orientation which is expected to contain the specified |
| 298 | * item index. */ |
| 299 | int getPageForComponent(int index) { |
| 300 | switch (mContentType) { |
| 301 | case Applications: { |
| 302 | int numItemsPerPage = mCellCountX * mCellCountY; |
| 303 | return (index / numItemsPerPage); |
| 304 | } |
| 305 | case Widgets: { |
| 306 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
| 307 | return (index / numItemsPerPage); |
| 308 | }} |
| 309 | return -1; |
| 310 | } |
| 311 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 312 | /** |
| 313 | * This differs from isDataReady as this is the test done if isDataReady is not set. |
| 314 | */ |
| 315 | private boolean testDataReady() { |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 316 | // We only do this test once, and we default to the Applications page, so we only really |
| 317 | // have to wait for there to be apps. |
Winson Chung | 87acb48 | 2011-08-23 17:28:05 -0700 | [diff] [blame] | 318 | if (mContentType == AppsCustomizePagedView.ContentType.Widgets) { |
| 319 | return !mApps.isEmpty() && !mWidgets.isEmpty(); |
| 320 | } else { |
| 321 | return !mApps.isEmpty(); |
| 322 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 325 | /** Restores the page for an item at the specified index */ |
| 326 | void restorePageForIndex(int index) { |
| 327 | if (index < 0) return; |
| 328 | |
| 329 | int page = getPageForComponent(index); |
| 330 | if (page > -1) { |
| 331 | if (getChildCount() > 0) { |
| 332 | invalidatePageData(page); |
| 333 | } else { |
| 334 | mRestorePage = page; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 339 | protected void onDataReady(int width, int height) { |
| 340 | // Note that we transpose the counts in portrait so that we get a similar layout |
| 341 | boolean isLandscape = getResources().getConfiguration().orientation == |
| 342 | Configuration.ORIENTATION_LANDSCAPE; |
| 343 | int maxCellCountX = Integer.MAX_VALUE; |
| 344 | int maxCellCountY = Integer.MAX_VALUE; |
| 345 | if (LauncherApplication.isScreenLarge()) { |
| 346 | maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() : |
| 347 | LauncherModel.getCellCountY()); |
| 348 | maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() : |
| 349 | LauncherModel.getCellCountX()); |
| 350 | } |
| 351 | |
| 352 | // Now that the data is ready, we can calculate the content width, the number of cells to |
| 353 | // use for each page |
| 354 | mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
| 355 | mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 356 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 357 | mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY); |
| 358 | mCellCountX = mWidgetSpacingLayout.getCellCountX(); |
| 359 | mCellCountY = mWidgetSpacingLayout.getCellCountY(); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 360 | |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 361 | // Force a measure to update recalculate the gaps |
| 362 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
| 363 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
| 364 | mWidgetSpacingLayout.measure(widthSpec, heightSpec); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 365 | mContentWidth = mWidgetSpacingLayout.getContentWidth(); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 366 | invalidatePageData(Math.max(0, mRestorePage)); |
| 367 | mRestorePage = -1; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | @Override |
| 371 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 372 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 373 | int height = MeasureSpec.getSize(heightMeasureSpec); |
| 374 | if (!isDataReady()) { |
| 375 | if (testDataReady()) { |
| 376 | setDataIsReady(); |
| 377 | setMeasuredDimension(width, height); |
| 378 | onDataReady(width, height); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 383 | } |
| 384 | |
Winson Chung | 34efdaf | 2011-05-24 14:19:56 -0700 | [diff] [blame] | 385 | /** Removes and returns the ResolveInfo with the specified ComponentName */ |
| 386 | private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list, |
| 387 | ComponentName cn) { |
| 388 | Iterator<ResolveInfo> iter = list.iterator(); |
| 389 | while (iter.hasNext()) { |
| 390 | ResolveInfo rinfo = iter.next(); |
| 391 | ActivityInfo info = rinfo.activityInfo; |
| 392 | ComponentName c = new ComponentName(info.packageName, info.name); |
| 393 | if (c.equals(cn)) { |
| 394 | iter.remove(); |
| 395 | return rinfo; |
| 396 | } |
| 397 | } |
| 398 | return null; |
| 399 | } |
| 400 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 401 | public void onPackagesUpdated() { |
Adam Cohen | b0fa352 | 2011-08-17 16:10:46 -0700 | [diff] [blame] | 402 | // TODO: this isn't ideal, but we actually need to delay here. This call is triggered |
| 403 | // by a broadcast receiver, and in order for it to work correctly, we need to know that |
| 404 | // the AppWidgetService has already received and processed the same broadcast. Since there |
| 405 | // is no guarantee about ordering of broadcast receipt, we just delay here. Ideally, |
| 406 | // we should have a more precise way of ensuring the AppWidgetService is up to date. |
| 407 | postDelayed(new Runnable() { |
| 408 | public void run() { |
| 409 | updatePackages(); |
| 410 | } |
| 411 | }, 500); |
| 412 | } |
| 413 | |
| 414 | public void updatePackages() { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 415 | // Get the list of widgets and shortcuts |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 416 | boolean wasEmpty = mWidgets.isEmpty(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 417 | mWidgets.clear(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 418 | List<AppWidgetProviderInfo> widgets = |
| 419 | AppWidgetManager.getInstance(mLauncher).getInstalledProviders(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 420 | Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); |
| 421 | List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 422 | mWidgets.addAll(widgets); |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 423 | mWidgets.addAll(shortcuts); |
| 424 | Collections.sort(mWidgets, |
| 425 | new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager)); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 426 | |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 427 | if (wasEmpty) { |
| 428 | // The next layout pass will trigger data-ready if both widgets and apps are set, so request |
| 429 | // a layout to do this test and invalidate the page data when ready. |
| 430 | if (testDataReady()) requestLayout(); |
| 431 | } else { |
| 432 | invalidatePageData(); |
| 433 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public void onClick(View v) { |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 438 | // When we have exited all apps or are in transition, disregard clicks |
| 439 | if (!mLauncher.isAllAppsCustomizeOpen() || |
| 440 | mLauncher.getWorkspace().isSwitchingState()) return; |
| 441 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 442 | if (v instanceof PagedViewIcon) { |
| 443 | // Animate some feedback to the click |
| 444 | final ApplicationInfo appInfo = (ApplicationInfo) v.getTag(); |
| 445 | animateClickFeedback(v, new Runnable() { |
| 446 | @Override |
| 447 | public void run() { |
| 448 | mLauncher.startActivitySafely(appInfo.intent, appInfo); |
| 449 | } |
| 450 | }); |
| 451 | } else if (v instanceof PagedViewWidget) { |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 452 | // Let the user know that they have to long press to add a widget |
| 453 | Toast.makeText(getContext(), R.string.long_press_widget_to_add, |
| 454 | Toast.LENGTH_SHORT).show(); |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 455 | |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 456 | // Create a little animation to show that the widget can move |
| 457 | float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); |
| 458 | final ImageView p = (ImageView) v.findViewById(R.id.widget_preview); |
| 459 | AnimatorSet bounce = new AnimatorSet(); |
| 460 | ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY); |
| 461 | tyuAnim.setDuration(125); |
| 462 | ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f); |
| 463 | tydAnim.setDuration(100); |
| 464 | bounce.play(tyuAnim).before(tydAnim); |
| 465 | bounce.setInterpolator(new AccelerateInterpolator()); |
| 466 | bounce.start(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 467 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* |
| 471 | * PagedViewWithDraggableItems implementation |
| 472 | */ |
| 473 | @Override |
| 474 | protected void determineDraggingStart(android.view.MotionEvent ev) { |
| 475 | // Disable dragging by pulling an app down for now. |
| 476 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 477 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 478 | private void beginDraggingApplication(View v) { |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 479 | mLauncher.getWorkspace().onDragStartedWithItem(v); |
| 480 | mLauncher.getWorkspace().beginDragShared(v, this); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 481 | } |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 482 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 483 | private void beginDraggingWidget(View v) { |
| 484 | // Get the widget preview as the drag representation |
| 485 | ImageView image = (ImageView) v.findViewById(R.id.widget_preview); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 486 | PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 487 | |
| 488 | // Compose the drag image |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 489 | Bitmap b; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 490 | Drawable preview = image.getDrawable(); |
| 491 | int w = preview.getIntrinsicWidth(); |
| 492 | int h = preview.getIntrinsicHeight(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 493 | if (createItemInfo instanceof PendingAddWidgetInfo) { |
| 494 | PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo; |
Adam Cohen | f814aa0 | 2011-09-01 13:48:05 -0700 | [diff] [blame^] | 495 | int[] spanXY = mLauncher.getSpanForWidget(createWidgetInfo, null); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 496 | createItemInfo.spanX = spanXY[0]; |
| 497 | createItemInfo.spanY = spanXY[1]; |
| 498 | |
| 499 | b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 500 | renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1, mDragViewMultiplyColor); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 501 | } else { |
| 502 | // Workaround for the fact that we don't keep the original ResolveInfo associated with |
| 503 | // the shortcut around. To get the icon, we just render the preview image (which has |
| 504 | // the shortcut icon) to a new drag bitmap that clips the non-icon space. |
| 505 | b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension, |
| 506 | mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 507 | mCanvas.setBitmap(b); |
| 508 | mCanvas.save(); |
| 509 | preview.draw(mCanvas); |
| 510 | mCanvas.restore(); |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 511 | mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 512 | mCanvas.setBitmap(null); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 513 | createItemInfo.spanX = createItemInfo.spanY = 1; |
| 514 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 515 | |
| 516 | // Start the drag |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 517 | mLauncher.lockScreenOrientation(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 518 | mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX, |
| 519 | createItemInfo.spanY, b); |
| 520 | mDragController.startDrag(image, b, this, createItemInfo, |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 521 | DragController.DRAG_ACTION_COPY, null); |
| 522 | b.recycle(); |
| 523 | } |
| 524 | @Override |
| 525 | protected boolean beginDragging(View v) { |
| 526 | if (!super.beginDragging(v)) return false; |
| 527 | |
Winson Chung | c07918d | 2011-07-01 15:35:26 -0700 | [diff] [blame] | 528 | // Go into spring loaded mode (must happen before we startDrag()) |
| 529 | int currentPageIndex = mLauncher.getWorkspace().getCurrentPage(); |
| 530 | CellLayout currentPage = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPageIndex); |
Adam Cohen | 7777d96 | 2011-08-18 18:58:38 -0700 | [diff] [blame] | 531 | mLauncher.enterSpringLoadedDragMode(); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 532 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 533 | if (v instanceof PagedViewIcon) { |
| 534 | beginDraggingApplication(v); |
| 535 | } else if (v instanceof PagedViewWidget) { |
| 536 | beginDraggingWidget(v); |
| 537 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 538 | return true; |
| 539 | } |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 540 | private void endDragging(View target, boolean success) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 541 | mLauncher.getWorkspace().onDragStopped(success); |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 542 | if (!success || (target != mLauncher.getWorkspace() && |
| 543 | !(target instanceof DeleteDropTarget))) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 544 | // Exit spring loaded mode if we have not successfully dropped or have not handled the |
| 545 | // drop in Workspace |
| 546 | mLauncher.exitSpringLoadedDragMode(); |
| 547 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 548 | mLauncher.unlockScreenOrientation(); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 549 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 552 | @Override |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 553 | public void onDropCompleted(View target, DragObject d, boolean success) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 554 | endDragging(target, success); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 555 | |
| 556 | // Display an error message if the drag failed due to there not being enough space on the |
| 557 | // target layout we were dropping on. |
| 558 | if (!success) { |
| 559 | boolean showOutOfSpaceMessage = false; |
| 560 | if (target instanceof Workspace) { |
| 561 | int currentScreen = mLauncher.getCurrentWorkspaceScreen(); |
| 562 | Workspace workspace = (Workspace) target; |
| 563 | CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 564 | ItemInfo itemInfo = (ItemInfo) d.dragInfo; |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 565 | if (layout != null) { |
| 566 | layout.calculateSpans(itemInfo); |
| 567 | showOutOfSpaceMessage = |
| 568 | !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY); |
| 569 | } |
| 570 | } |
| 571 | // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later. |
| 572 | if (showOutOfSpaceMessage) { |
| 573 | mLauncher.showOutOfSpaceMessage(); |
| 574 | } |
| 575 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | public void setContentType(ContentType type) { |
| 579 | mContentType = type; |
Winson Chung | 87acb48 | 2011-08-23 17:28:05 -0700 | [diff] [blame] | 580 | invalidatePageData(0, (type != ContentType.Applications)); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 583 | public boolean isContentType(ContentType type) { |
| 584 | return (mContentType == type); |
| 585 | } |
| 586 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 587 | public void setCurrentPageToWidgets() { |
| 588 | invalidatePageData(0); |
| 589 | } |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 590 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 591 | /* |
| 592 | * Apps PagedView implementation |
| 593 | */ |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 594 | private void setVisibilityOnChildren(ViewGroup layout, int visibility) { |
| 595 | int childCount = layout.getChildCount(); |
| 596 | for (int i = 0; i < childCount; ++i) { |
| 597 | layout.getChildAt(i).setVisibility(visibility); |
| 598 | } |
| 599 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 600 | private void setupPage(PagedViewCellLayout layout) { |
| 601 | layout.setCellCount(mCellCountX, mCellCountY); |
| 602 | layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
| 603 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 604 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 605 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 606 | // Note: We force a measure here to get around the fact that when we do layout calculations |
| 607 | // immediately after syncing, we don't have a proper width. That said, we already know the |
| 608 | // expected page width, so we can actually optimize by hiding all the TextView-based |
| 609 | // children that are expensive to measure, and let that happen naturally later. |
| 610 | setVisibilityOnChildren(layout, View.GONE); |
Winson Chung | db1138b | 2011-06-30 14:39:35 -0700 | [diff] [blame] | 611 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 612 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 613 | layout.setMinimumWidth(getPageContentWidth()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 614 | layout.measure(widthSpec, heightSpec); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 615 | setVisibilityOnChildren(layout, View.VISIBLE); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 616 | } |
| 617 | public void syncAppsPages() { |
| 618 | // Ensure that we have the right number of pages |
| 619 | Context context = getContext(); |
| 620 | int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); |
| 621 | for (int i = 0; i < numPages; ++i) { |
| 622 | PagedViewCellLayout layout = new PagedViewCellLayout(context); |
| 623 | setupPage(layout); |
| 624 | addView(layout); |
| 625 | } |
| 626 | } |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 627 | public void syncAppsPageItems(int page, boolean immediate) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 628 | // ensure that we have the right number of items on the pages |
| 629 | int numPages = getPageCount(); |
| 630 | int numCells = mCellCountX * mCellCountY; |
| 631 | int startIndex = page * numCells; |
| 632 | int endIndex = Math.min(startIndex + numCells, mApps.size()); |
| 633 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 634 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 635 | layout.removeAllViewsOnPage(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 636 | ArrayList<Object> items = new ArrayList<Object>(); |
| 637 | ArrayList<Bitmap> images = new ArrayList<Bitmap>(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 638 | for (int i = startIndex; i < endIndex; ++i) { |
| 639 | ApplicationInfo info = mApps.get(i); |
| 640 | PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate( |
| 641 | R.layout.apps_customize_application, layout, false); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 642 | icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 643 | icon.setOnClickListener(this); |
| 644 | icon.setOnLongClickListener(this); |
| 645 | icon.setOnTouchListener(this); |
| 646 | |
| 647 | int index = i - startIndex; |
| 648 | int x = index % mCellCountX; |
| 649 | int y = index / mCellCountX; |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 650 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 651 | |
| 652 | items.add(info); |
| 653 | images.add(info.iconBitmap); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 654 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 655 | |
| 656 | // Create the hardware layers |
| 657 | layout.allowHardwareLayerCreation(); |
| 658 | layout.createHardwareLayers(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 659 | |
Winson Chung | 54c725c | 2011-08-03 12:03:40 -0700 | [diff] [blame] | 660 | if (mFadeInAdjacentScreens) { |
| 661 | prepareGenerateHoloOutlinesTask(page, items, images); |
| 662 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 663 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 664 | |
| 665 | /** |
| 666 | * Return the appropriate thread priority for loading for a given page (we give the current |
| 667 | * page much higher priority) |
| 668 | */ |
| 669 | private int getThreadPriorityForPage(int page) { |
| 670 | // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below |
| 671 | int pageDiff = Math.abs(page - mCurrentPage); |
| 672 | if (pageDiff <= 0) { |
| 673 | // return Process.THREAD_PRIORITY_DEFAULT; |
| 674 | return Process.THREAD_PRIORITY_MORE_FAVORABLE; |
| 675 | } else if (pageDiff <= 1) { |
| 676 | // return Process.THREAD_PRIORITY_BACKGROUND; |
| 677 | return Process.THREAD_PRIORITY_DEFAULT; |
| 678 | } else { |
| 679 | // return Process.THREAD_PRIORITY_LOWEST; |
| 680 | return Process.THREAD_PRIORITY_DEFAULT; |
| 681 | } |
| 682 | } |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 683 | private int getSleepForPage(int page) { |
| 684 | int pageDiff = Math.abs(page - mCurrentPage) - 1; |
| 685 | return Math.max(0, pageDiff * sPageSleepDelay); |
| 686 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 687 | /** |
| 688 | * Creates and executes a new AsyncTask to load a page of widget previews. |
| 689 | */ |
| 690 | private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 691 | int cellWidth, int cellHeight, int cellCountX) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 692 | // Prune all tasks that are no longer needed |
| 693 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 694 | while (iter.hasNext()) { |
| 695 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 696 | int taskPage = task.page; |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 697 | if ((taskPage == page) || |
| 698 | taskPage < getAssociatedLowerPageBound(mCurrentPage) || |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 699 | taskPage > getAssociatedUpperPageBound(mCurrentPage)) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 700 | task.cancel(false); |
| 701 | iter.remove(); |
| 702 | } else { |
| 703 | task.setThreadPriority(getThreadPriorityForPage(taskPage)); |
| 704 | } |
| 705 | } |
| 706 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 707 | // We introduce a slight delay to order the loading of side pages so that we don't thrash |
| 708 | final int sleepMs = getSleepForPage(page); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 709 | AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 710 | cellCountX, new AsyncTaskCallback() { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 711 | @Override |
| 712 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 713 | try { |
| 714 | Thread.sleep(sleepMs); |
| 715 | } catch (Exception e) {} |
| 716 | loadWidgetPreviewsInBackground(task, data); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 717 | } |
| 718 | }, |
| 719 | new AsyncTaskCallback() { |
| 720 | @Override |
| 721 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
| 722 | mRunningTasks.remove(task); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 723 | if (task.isCancelled()) return; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 724 | if (task.page > getPageCount()) return; |
| 725 | if (task.pageContentType != mContentType) return; |
| 726 | onSyncWidgetPageItems(data); |
| 727 | } |
| 728 | }); |
| 729 | |
| 730 | // Ensure that the task is appropriately prioritized and runs in parallel |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 731 | AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType, |
| 732 | AsyncTaskPageData.Type.LoadWidgetPreviewData); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 733 | t.setThreadPriority(getThreadPriorityForPage(page)); |
| 734 | t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData); |
| 735 | mRunningTasks.add(t); |
| 736 | } |
| 737 | /** |
| 738 | * Creates and executes a new AsyncTask to load the outlines for a page of content. |
| 739 | */ |
| 740 | private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items, |
| 741 | ArrayList<Bitmap> images) { |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 742 | // Prune old tasks for this page |
| 743 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 744 | while (iter.hasNext()) { |
| 745 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 746 | int taskPage = task.page; |
| 747 | if ((taskPage == page) && |
| 748 | (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) { |
| 749 | task.cancel(false); |
| 750 | iter.remove(); |
| 751 | } |
| 752 | } |
| 753 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 754 | AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images, |
| 755 | new AsyncTaskCallback() { |
| 756 | @Override |
| 757 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
| 758 | // Ensure that this task starts running at the correct priority |
| 759 | task.syncThreadPriority(); |
| 760 | |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 761 | ArrayList<Bitmap> images = data.generatedImages; |
| 762 | ArrayList<Bitmap> srcImages = data.sourceImages; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 763 | int count = srcImages.size(); |
| 764 | Canvas c = new Canvas(); |
| 765 | for (int i = 0; i < count && !task.isCancelled(); ++i) { |
| 766 | // Before work on each item, ensure that this task is running at the correct |
| 767 | // priority |
| 768 | task.syncThreadPriority(); |
| 769 | |
| 770 | Bitmap b = srcImages.get(i); |
| 771 | Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(), |
| 772 | Bitmap.Config.ARGB_8888); |
| 773 | |
| 774 | c.setBitmap(outline); |
| 775 | c.save(); |
| 776 | c.drawBitmap(b, 0, 0, null); |
| 777 | c.restore(); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 778 | c.setBitmap(null); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 779 | |
| 780 | images.add(outline); |
| 781 | } |
| 782 | } |
| 783 | }, |
| 784 | new AsyncTaskCallback() { |
| 785 | @Override |
| 786 | public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) { |
| 787 | mRunningTasks.remove(task); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 788 | if (task.isCancelled()) return; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 789 | if (task.page > getPageCount()) return; |
| 790 | if (task.pageContentType != mContentType) return; |
| 791 | onHolographicPageItemsLoaded(data); |
| 792 | } |
| 793 | }); |
| 794 | |
| 795 | // Ensure that the outline task always runs in the background, serially |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 796 | AppsCustomizeAsyncTask t = |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 797 | new AppsCustomizeAsyncTask(page, mContentType, |
| 798 | AsyncTaskPageData.Type.LoadHolographicIconsData); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 799 | t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 800 | t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData); |
| 801 | mRunningTasks.add(t); |
| 802 | } |
| 803 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 804 | /* |
| 805 | * Widgets PagedView implementation |
| 806 | */ |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 807 | private void setupPage(PagedViewGridLayout layout) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 808 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 809 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 810 | |
| 811 | // Note: We force a measure here to get around the fact that when we do layout calculations |
Winson Chung | d52f3d8 | 2011-07-12 14:29:11 -0700 | [diff] [blame] | 812 | // immediately after syncing, we don't have a proper width. |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 813 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
| 814 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 815 | layout.setMinimumWidth(getPageContentWidth()); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 816 | layout.measure(widthSpec, heightSpec); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 817 | } |
Michael Jurka | be69cc8 | 2011-07-07 16:05:33 -0700 | [diff] [blame] | 818 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 819 | float scaleX, float scaleY) { |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 820 | renderDrawableToBitmap(d, bitmap, x, y, w, h, scaleX, scaleY, 0xFFFFFFFF); |
| 821 | } |
| 822 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, |
| 823 | float scaleX, float scaleY, int multiplyColor) { |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 824 | if (bitmap != null) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 825 | Canvas c = new Canvas(bitmap); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 826 | c.scale(scaleX, scaleY); |
| 827 | Rect oldBounds = d.copyBounds(); |
| 828 | d.setBounds(x, y, x + w, y + h); |
| 829 | d.draw(c); |
| 830 | d.setBounds(oldBounds); // Restore the bounds |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 831 | if (multiplyColor != 0xFFFFFFFF) { |
| 832 | c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY); |
| 833 | } |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 834 | c.setBitmap(null); |
Winson Chung | 201bc82 | 2011-06-20 15:41:53 -0700 | [diff] [blame] | 835 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 836 | } |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 837 | private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 838 | // Render the icon |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 839 | Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888); |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 840 | Drawable icon = mIconCache.getFullResIcon(info, mPackageManager); |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 841 | renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 842 | return preview; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 843 | } |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 844 | private Bitmap getWidgetPreview(AppWidgetProviderInfo info, |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 845 | int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 846 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 847 | // Calculate the size of the drawable |
| 848 | cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan)); |
| 849 | cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan)); |
| 850 | int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan); |
| 851 | int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan); |
| 852 | |
| 853 | // Scale down the bitmap to fit the space |
| 854 | float widgetPreviewScale = (float) cellWidth / expectedWidth; |
| 855 | expectedWidth = (int) (widgetPreviewScale * expectedWidth); |
| 856 | expectedHeight = (int) (widgetPreviewScale * expectedHeight); |
| 857 | |
| 858 | // Load the preview image if possible |
| 859 | String packageName = info.provider.getPackageName(); |
| 860 | Drawable drawable = null; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 861 | Bitmap preview = null; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 862 | if (info.previewImage != 0) { |
| 863 | drawable = mPackageManager.getDrawable(packageName, info.previewImage, null); |
| 864 | if (drawable == null) { |
| 865 | Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon) |
| 866 | + " for provider: " + info.provider); |
| 867 | } else { |
| 868 | // Scale down the preview to the dimensions we want |
| 869 | int imageWidth = drawable.getIntrinsicWidth(); |
| 870 | int imageHeight = drawable.getIntrinsicHeight(); |
| 871 | float aspect = (float) imageWidth / imageHeight; |
| 872 | int newWidth = imageWidth; |
| 873 | int newHeight = imageHeight; |
| 874 | if (aspect > 1f) { |
| 875 | newWidth = expectedWidth; |
| 876 | newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth)); |
| 877 | } else { |
| 878 | newHeight = expectedHeight; |
| 879 | newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight)); |
| 880 | } |
| 881 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 882 | preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 883 | renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
| 887 | // Generate a preview image if we couldn't load one |
| 888 | if (drawable == null) { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 889 | Resources resources = mLauncher.getResources(); |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 890 | int bitmapWidth; |
| 891 | int bitmapHeight; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 892 | |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 893 | // Specify the dimensions of the bitmap (since we are using a default preview bg with |
| 894 | // the full icon, we only imply the aspect ratio of the widget) |
| 895 | if (cellHSpan == cellVSpan) { |
| 896 | bitmapWidth = bitmapHeight = cellWidth; |
| 897 | expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension; |
| 898 | } else if (cellHSpan >= cellVSpan) { |
| 899 | bitmapWidth = expectedWidth = cellWidth; |
| 900 | bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 901 | } else { |
| 902 | // Note that in vertical widgets, we might not have enough space due to the text |
| 903 | // label, so be conservative and use the width as a height bound |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 904 | bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension; |
| 905 | bitmapHeight = expectedHeight = cellWidth; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 906 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 907 | |
Winson Chung | 273c102 | 2011-07-11 13:40:52 -0700 | [diff] [blame] | 908 | preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 909 | renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth, |
Winson Chung | 70fc438 | 2011-08-08 15:31:33 -0700 | [diff] [blame] | 910 | expectedHeight, 1f, 1f); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 911 | |
| 912 | // Draw the icon in the top left corner |
| 913 | try { |
| 914 | Drawable icon = null; |
| 915 | if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null); |
| 916 | if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application); |
| 917 | |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 918 | int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage); |
| 919 | renderDrawableToBitmap(icon, preview, offset, offset, |
| 920 | mAppIconSize, mAppIconSize, 1f, 1f); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 921 | } catch (Resources.NotFoundException e) {} |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 922 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 923 | return preview; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 924 | } |
| 925 | public void syncWidgetPages() { |
| 926 | // Ensure that we have the right number of pages |
| 927 | Context context = getContext(); |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 928 | int numPages = (int) Math.ceil(mWidgets.size() / |
| 929 | (float) (mWidgetCountX * mWidgetCountY)); |
| 930 | for (int j = 0; j < numPages; ++j) { |
| 931 | PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX, |
| 932 | mWidgetCountY); |
| 933 | setupPage(layout); |
| 934 | addView(layout); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 937 | public void syncWidgetPageItems(int page, boolean immediate) { |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 938 | int numItemsPerPage = mWidgetCountX * mWidgetCountY; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 939 | int contentWidth = mWidgetSpacingLayout.getContentWidth(); |
| 940 | int contentHeight = mWidgetSpacingLayout.getContentHeight(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 941 | |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 942 | // Calculate the dimensions of each cell we are giving to each widget |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 943 | ArrayList<Object> items = new ArrayList<Object>(); |
| 944 | int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 945 | - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX); |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 946 | int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 947 | - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY); |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 948 | |
Winson Chung | 6a3fd3f | 2011-08-02 14:03:26 -0700 | [diff] [blame] | 949 | int offset = page * numItemsPerPage; |
| 950 | for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) { |
| 951 | items.add(mWidgets.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 954 | if (immediate) { |
| 955 | AsyncTaskPageData data = new AsyncTaskPageData(page, items, cellWidth, cellHeight, |
| 956 | mWidgetCountX, null, null); |
| 957 | loadWidgetPreviewsInBackground(null, data); |
| 958 | onSyncWidgetPageItems(data); |
| 959 | } else { |
| 960 | prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX); |
| 961 | } |
| 962 | } |
| 963 | private void loadWidgetPreviewsInBackground(AppsCustomizeAsyncTask task, |
| 964 | AsyncTaskPageData data) { |
| 965 | if (task != null) { |
| 966 | // Ensure that this task starts running at the correct priority |
| 967 | task.syncThreadPriority(); |
| 968 | } |
| 969 | |
| 970 | // Load each of the widget/shortcut previews |
| 971 | ArrayList<Object> items = data.items; |
| 972 | ArrayList<Bitmap> images = data.generatedImages; |
| 973 | int count = items.size(); |
| 974 | int cellWidth = data.cellWidth; |
| 975 | int cellHeight = data.cellHeight; |
| 976 | for (int i = 0; i < count; ++i) { |
| 977 | if (task != null) { |
| 978 | // Ensure we haven't been cancelled yet |
| 979 | if (task.isCancelled()) break; |
| 980 | // Before work on each item, ensure that this task is running at the correct |
| 981 | // priority |
| 982 | task.syncThreadPriority(); |
| 983 | } |
| 984 | |
| 985 | Object rawInfo = items.get(i); |
| 986 | if (rawInfo instanceof AppWidgetProviderInfo) { |
| 987 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; |
Adam Cohen | f814aa0 | 2011-09-01 13:48:05 -0700 | [diff] [blame^] | 988 | int[] cellSpans = mLauncher.getSpanForWidget(info, null); |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 989 | images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1], |
| 990 | cellWidth, cellHeight)); |
| 991 | } else if (rawInfo instanceof ResolveInfo) { |
| 992 | // Fill in the shortcuts information |
| 993 | ResolveInfo info = (ResolveInfo) rawInfo; |
| 994 | images.add(getShortcutPreview(info, cellWidth, cellHeight)); |
| 995 | } |
| 996 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 997 | } |
| 998 | private void onSyncWidgetPageItems(AsyncTaskPageData data) { |
| 999 | int page = data.page; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 1000 | PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page); |
Winson Chung | d52f3d8 | 2011-07-12 14:29:11 -0700 | [diff] [blame] | 1001 | // Only set the column count once we have items |
| 1002 | layout.setColumnCount(layout.getCellCountX()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1003 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1004 | ArrayList<Object> items = data.items; |
| 1005 | int count = items.size(); |
| 1006 | int cellWidth = data.cellWidth; |
| 1007 | int cellHeight = data.cellHeight; |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1008 | int cellCountX = data.cellCountX; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1009 | for (int i = 0; i < count; ++i) { |
| 1010 | Object rawInfo = items.get(i); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1011 | PendingAddItemInfo createItemInfo = null; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1012 | PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate( |
| 1013 | R.layout.apps_customize_widget, layout, false); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1014 | if (rawInfo instanceof AppWidgetProviderInfo) { |
| 1015 | // Fill in the widget information |
| 1016 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; |
Michael Jurka | b60fd0e | 2011-08-29 14:02:47 -0700 | [diff] [blame] | 1017 | createItemInfo = new PendingAddWidgetInfo(info, null, null, "13"); |
Adam Cohen | f814aa0 | 2011-09-01 13:48:05 -0700 | [diff] [blame^] | 1018 | int[] cellSpans = mLauncher.getSpanForWidget(info, null); |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1019 | FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i)); |
Winson Chung | 9f4e0fd | 2011-06-02 18:59:36 -0700 | [diff] [blame] | 1020 | widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans, |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1021 | mHolographicOutlineHelper); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1022 | widget.setTag(createItemInfo); |
| 1023 | } else if (rawInfo instanceof ResolveInfo) { |
| 1024 | // Fill in the shortcuts information |
| 1025 | ResolveInfo info = (ResolveInfo) rawInfo; |
Michael Jurka | b60fd0e | 2011-08-29 14:02:47 -0700 | [diff] [blame] | 1026 | createItemInfo = new PendingAddItemInfo("14"); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1027 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
| 1028 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 1029 | info.activityInfo.name); |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1030 | FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1031 | widget.applyFromResolveInfo(mPackageManager, info, preview, |
| 1032 | mHolographicOutlineHelper); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1033 | widget.setTag(createItemInfo); |
| 1034 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 1035 | widget.setOnClickListener(this); |
| 1036 | widget.setOnLongClickListener(this); |
| 1037 | widget.setOnTouchListener(this); |
| 1038 | |
| 1039 | // Layout each widget |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1040 | int ix = i % cellCountX; |
| 1041 | int iy = i / cellCountX; |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 1042 | GridLayout.LayoutParams lp = new GridLayout.LayoutParams( |
Winson Chung | 72d8b39 | 2011-07-29 13:56:44 -0700 | [diff] [blame] | 1043 | GridLayout.spec(iy, GridLayout.LEFT), |
| 1044 | GridLayout.spec(ix, GridLayout.TOP)); |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 1045 | lp.width = cellWidth; |
| 1046 | lp.height = cellHeight; |
Winson Chung | 72d8b39 | 2011-07-29 13:56:44 -0700 | [diff] [blame] | 1047 | lp.setGravity(Gravity.TOP | Gravity.LEFT); |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 1048 | if (ix > 0) lp.leftMargin = mWidgetWidthGap; |
| 1049 | if (iy > 0) lp.topMargin = mWidgetHeightGap; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 1050 | layout.addView(widget, lp); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1051 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1052 | |
| 1053 | invalidate(); |
| 1054 | forceUpdateAdjacentPagesAlpha(); |
Winson Chung | 54c725c | 2011-08-03 12:03:40 -0700 | [diff] [blame] | 1055 | |
| 1056 | if (mFadeInAdjacentScreens) { |
| 1057 | prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages); |
| 1058 | } |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1059 | } |
| 1060 | private void onHolographicPageItemsLoaded(AsyncTaskPageData data) { |
| 1061 | // Invalidate early to short-circuit children invalidates |
| 1062 | invalidate(); |
| 1063 | |
| 1064 | int page = data.page; |
| 1065 | ViewGroup layout = (ViewGroup) getChildAt(page); |
| 1066 | if (layout instanceof PagedViewCellLayout) { |
| 1067 | PagedViewCellLayout cl = (PagedViewCellLayout) layout; |
| 1068 | int count = cl.getPageChildCount(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1069 | if (count != data.generatedImages.size()) return; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1070 | for (int i = 0; i < count; ++i) { |
| 1071 | PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i); |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1072 | icon.setHolographicOutline(data.generatedImages.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1073 | } |
| 1074 | } else { |
| 1075 | int count = layout.getChildCount(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1076 | if (count != data.generatedImages.size()) return; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1077 | for (int i = 0; i < count; ++i) { |
| 1078 | View v = layout.getChildAt(i); |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1079 | ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i)); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1080 | } |
| 1081 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1082 | } |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 1083 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1084 | @Override |
| 1085 | public void syncPages() { |
| 1086 | removeAllViews(); |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1087 | |
| 1088 | // Remove all background asyc tasks if we are loading content anew |
| 1089 | Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator(); |
| 1090 | while (iter.hasNext()) { |
| 1091 | AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next(); |
| 1092 | task.cancel(false); |
| 1093 | iter.remove(); |
| 1094 | } |
| 1095 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1096 | switch (mContentType) { |
| 1097 | case Applications: |
| 1098 | syncAppsPages(); |
| 1099 | break; |
| 1100 | case Widgets: |
| 1101 | syncWidgetPages(); |
| 1102 | break; |
| 1103 | } |
| 1104 | } |
| 1105 | @Override |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1106 | public void syncPageItems(int page, boolean immediate) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1107 | switch (mContentType) { |
| 1108 | case Applications: |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1109 | syncAppsPageItems(page, immediate); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1110 | break; |
| 1111 | case Widgets: |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1112 | syncWidgetPageItems(page, immediate); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1113 | break; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * Used by the parent to get the content width to set the tab bar to |
| 1119 | * @return |
| 1120 | */ |
| 1121 | public int getPageContentWidth() { |
| 1122 | return mContentWidth; |
| 1123 | } |
| 1124 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1125 | @Override |
| 1126 | protected void onPageBeginMoving() { |
| 1127 | /* TO BE ENABLED LATER |
| 1128 | setChildrenDrawnWithCacheEnabled(true); |
| 1129 | for (int i = 0; i < getChildCount(); ++i) { |
| 1130 | View v = getChildAt(i); |
| 1131 | if (v instanceof PagedViewCellLayout) { |
| 1132 | ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true); |
| 1133 | } |
| 1134 | } |
| 1135 | */ |
| 1136 | super.onPageBeginMoving(); |
| 1137 | } |
| 1138 | |
| 1139 | @Override |
| 1140 | protected void onPageEndMoving() { |
| 1141 | /* TO BE ENABLED LATER |
| 1142 | for (int i = 0; i < getChildCount(); ++i) { |
| 1143 | View v = getChildAt(i); |
| 1144 | if (v instanceof PagedViewCellLayout) { |
| 1145 | ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false); |
| 1146 | } |
| 1147 | } |
| 1148 | setChildrenDrawnWithCacheEnabled(false); |
| 1149 | */ |
| 1150 | super.onPageEndMoving(); |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 1151 | |
| 1152 | // We reset the save index when we change pages so that it will be recalculated on next |
| 1153 | // rotation |
| 1154 | mSaveInstanceStateItemIndex = -1; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1157 | /* |
| 1158 | * AllAppsView implementation |
| 1159 | */ |
| 1160 | @Override |
| 1161 | public void setup(Launcher launcher, DragController dragController) { |
| 1162 | mLauncher = launcher; |
| 1163 | mDragController = dragController; |
| 1164 | } |
| 1165 | @Override |
| 1166 | public void zoom(float zoom, boolean animate) { |
| 1167 | // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed() |
| 1168 | } |
| 1169 | @Override |
| 1170 | public boolean isVisible() { |
| 1171 | return (getVisibility() == VISIBLE); |
| 1172 | } |
| 1173 | @Override |
| 1174 | public boolean isAnimating() { |
| 1175 | return false; |
| 1176 | } |
| 1177 | @Override |
| 1178 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 1179 | mApps = list; |
| 1180 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1181 | |
Winson Chung | 875de7e | 2011-06-28 14:25:17 -0700 | [diff] [blame] | 1182 | // The next layout pass will trigger data-ready if both widgets and apps are set, so |
| 1183 | // request a layout to do this test and invalidate the page data when ready. |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1184 | if (testDataReady()) requestLayout(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1185 | } |
| 1186 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 1187 | // We add it in place, in alphabetical order |
| 1188 | int count = list.size(); |
| 1189 | for (int i = 0; i < count; ++i) { |
| 1190 | ApplicationInfo info = list.get(i); |
| 1191 | int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 1192 | if (index < 0) { |
| 1193 | mApps.add(-(index + 1), info); |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | @Override |
| 1198 | public void addApps(ArrayList<ApplicationInfo> list) { |
| 1199 | addAppsWithoutInvalidate(list); |
| 1200 | invalidatePageData(); |
| 1201 | } |
| 1202 | private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) { |
| 1203 | ComponentName removeComponent = item.intent.getComponent(); |
| 1204 | int length = list.size(); |
| 1205 | for (int i = 0; i < length; ++i) { |
| 1206 | ApplicationInfo info = list.get(i); |
| 1207 | if (info.intent.getComponent().equals(removeComponent)) { |
| 1208 | return i; |
| 1209 | } |
| 1210 | } |
| 1211 | return -1; |
| 1212 | } |
| 1213 | private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 1214 | // loop through all the apps and remove apps that have the same component |
| 1215 | int length = list.size(); |
| 1216 | for (int i = 0; i < length; ++i) { |
| 1217 | ApplicationInfo info = list.get(i); |
| 1218 | int removeIndex = findAppByComponent(mApps, info); |
| 1219 | if (removeIndex > -1) { |
| 1220 | mApps.remove(removeIndex); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | @Override |
| 1225 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 1226 | removeAppsWithoutInvalidate(list); |
| 1227 | invalidatePageData(); |
| 1228 | } |
| 1229 | @Override |
| 1230 | public void updateApps(ArrayList<ApplicationInfo> list) { |
| 1231 | // We remove and re-add the updated applications list because it's properties may have |
| 1232 | // changed (ie. the title), and this will ensure that the items will be in their proper |
| 1233 | // place in the list. |
| 1234 | removeAppsWithoutInvalidate(list); |
| 1235 | addAppsWithoutInvalidate(list); |
| 1236 | invalidatePageData(); |
| 1237 | } |
Michael Jurka | 35aa14d | 2011-07-07 17:01:08 -0700 | [diff] [blame] | 1238 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1239 | @Override |
| 1240 | public void reset() { |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 1241 | if (mContentType != ContentType.Applications) { |
| 1242 | // Reset to the first page of the Apps pane |
| 1243 | AppsCustomizeTabHost tabs = (AppsCustomizeTabHost) |
| 1244 | mLauncher.findViewById(R.id.apps_customize_pane); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1245 | tabs.selectAppsTab(); |
Michael Jurka | 35aa14d | 2011-07-07 17:01:08 -0700 | [diff] [blame] | 1246 | } else if (getCurrentPage() != 0) { |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1247 | invalidatePageData(0); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 1248 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1249 | } |
| 1250 | @Override |
| 1251 | public void dumpState() { |
| 1252 | // TODO: Dump information related to current list of Applications, Widgets, etc. |
| 1253 | ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps); |
| 1254 | dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets); |
| 1255 | } |
| 1256 | private void dumpAppWidgetProviderInfoList(String tag, String label, |
Winson Chung | d294526 | 2011-06-24 15:22:14 -0700 | [diff] [blame] | 1257 | ArrayList<Object> list) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1258 | Log.d(tag, label + " size=" + list.size()); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 1259 | for (Object i: list) { |
| 1260 | if (i instanceof AppWidgetProviderInfo) { |
| 1261 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) i; |
| 1262 | Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage |
| 1263 | + " resizeMode=" + info.resizeMode + " configure=" + info.configure |
| 1264 | + " initialLayout=" + info.initialLayout |
| 1265 | + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight); |
| 1266 | } else if (i instanceof ResolveInfo) { |
| 1267 | ResolveInfo info = (ResolveInfo) i; |
| 1268 | Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon=" |
| 1269 | + info.icon); |
| 1270 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | @Override |
| 1274 | public void surrender() { |
| 1275 | // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we |
| 1276 | // should stop this now. |
| 1277 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1278 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1279 | /* |
| 1280 | * We load an extra page on each side to prevent flashes from scrolling and loading of the |
| 1281 | * widget previews in the background with the AsyncTasks. |
| 1282 | */ |
| 1283 | protected int getAssociatedLowerPageBound(int page) { |
| 1284 | return Math.max(0, page - 2); |
| 1285 | } |
| 1286 | protected int getAssociatedUpperPageBound(int page) { |
| 1287 | final int count = getChildCount(); |
| 1288 | return Math.min(page + 2, count - 1); |
| 1289 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1290 | |
| 1291 | @Override |
| 1292 | protected String getCurrentPageDescription() { |
| 1293 | int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 1294 | int stringId = R.string.default_scroll_format; |
| 1295 | switch (mContentType) { |
| 1296 | case Applications: |
| 1297 | stringId = R.string.apps_customize_apps_scroll_format; |
| 1298 | break; |
| 1299 | case Widgets: |
| 1300 | stringId = R.string.apps_customize_widgets_scroll_format; |
| 1301 | break; |
| 1302 | } |
| 1303 | return String.format(mContext.getString(stringId), page + 1, getChildCount()); |
| 1304 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1305 | } |