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