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