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