blob: 16dad1be91b87d04a3e2160a0900c63de322112f [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
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
17package com.android.launcher2;
18
Winson Chung55b65502011-05-26 12:03:43 -070019import android.animation.AnimatorSet;
Winson Chung4b576be2011-04-27 17:40:20 -070020import android.animation.ObjectAnimator;
Winson Chungd2e87b32011-06-02 10:53:07 -070021import android.animation.ValueAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
Winson Chung46af2e82011-05-09 16:00:53 -070027import android.content.pm.ActivityInfo;
Winson Chung785d2eb2011-04-14 16:08:02 -070028import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Winson Chungf0ea4d32011-06-06 14:27:16 -070030import android.content.res.Configuration;
Winson Chung785d2eb2011-04-14 16:08:02 -070031import android.content.res.Resources;
32import android.content.res.TypedArray;
33import android.graphics.Bitmap;
Winson Chungf0ea4d32011-06-06 14:27:16 -070034import android.graphics.Bitmap.Config;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.graphics.Canvas;
Winson Chung70fc4382011-08-08 15:31:33 -070036import android.graphics.PorterDuff;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.graphics.Rect;
38import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070039import android.os.AsyncTask;
40import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070041import android.util.AttributeSet;
42import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070043import android.view.Gravity;
Winson Chung785d2eb2011-04-14 16:08:02 -070044import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070045import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070046import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070047import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070048import android.view.animation.AccelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070049import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070050import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070051import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070052
53import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070054import com.android.launcher2.DropTarget.DragObject;
55
56import java.util.ArrayList;
57import java.util.Collections;
58import java.util.Iterator;
59import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070060
Winson Chungb44b5242011-06-13 11:32:14 -070061/**
62 * A simple callback interface which also provides the results of the task.
63 */
64interface AsyncTaskCallback {
65 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
66}
Winson Chung4e076542011-06-23 13:04:10 -070067
Winson Chungb44b5242011-06-13 11:32:14 -070068/**
69 * The data needed to perform either of the custom AsyncTasks.
70 */
71class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070072 enum Type {
73 LoadWidgetPreviewData,
74 LoadHolographicIconsData
75 }
76
Winson Chungb44b5242011-06-13 11:32:14 -070077 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
78 AsyncTaskCallback postR) {
79 page = p;
80 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070081 sourceImages = si;
82 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070083 cellWidth = cellHeight = -1;
84 doInBackgroundCallback = bgR;
85 postExecuteCallback = postR;
86 }
Winson Chungd2945262011-06-24 15:22:14 -070087 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, int ccx, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070088 AsyncTaskCallback postR) {
89 page = p;
90 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070091 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070092 cellWidth = cw;
93 cellHeight = ch;
Winson Chungd2945262011-06-24 15:22:14 -070094 cellCountX = ccx;
Winson Chungb44b5242011-06-13 11:32:14 -070095 doInBackgroundCallback = bgR;
96 postExecuteCallback = postR;
97 }
98 int page;
99 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -0700100 ArrayList<Bitmap> sourceImages;
101 ArrayList<Bitmap> generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700102 int cellWidth;
103 int cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700104 int cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700105 AsyncTaskCallback doInBackgroundCallback;
106 AsyncTaskCallback postExecuteCallback;
107}
Winson Chung4e076542011-06-23 13:04:10 -0700108
Winson Chungb44b5242011-06-13 11:32:14 -0700109/**
110 * A generic template for an async task used in AppsCustomize.
111 */
112class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Winson Chung875de7e2011-06-28 14:25:17 -0700113 AppsCustomizeAsyncTask(int p, AppsCustomizePagedView.ContentType t, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700114 page = p;
115 pageContentType = t;
116 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700117 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700118 }
119 @Override
120 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
121 if (params.length != 1) return null;
122 // Load each of the widget previews in the background
123 params[0].doInBackgroundCallback.run(this, params[0]);
124 return params[0];
125 }
126 @Override
127 protected void onPostExecute(AsyncTaskPageData result) {
128 // All the widget previews are loaded, so we can just callback to inflate the page
129 result.postExecuteCallback.run(this, result);
130 }
131
132 void setThreadPriority(int p) {
133 threadPriority = p;
134 }
135 void syncThreadPriority() {
136 Process.setThreadPriority(threadPriority);
137 }
138
139 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700140 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700141 int page;
142 AppsCustomizePagedView.ContentType pageContentType;
143 int threadPriority;
144}
Winson Chungb44b5242011-06-13 11:32:14 -0700145
146/**
147 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
148 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700149public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
150 AllAppsView, View.OnClickListener, DragSource {
151 static final String LOG_TAG = "AppsCustomizePagedView";
152
153 /**
154 * The different content types that this paged view can show.
155 */
156 public enum ContentType {
157 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700158 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700159 }
160
161 // Refs
162 private Launcher mLauncher;
163 private DragController mDragController;
164 private final LayoutInflater mLayoutInflater;
165 private final PackageManager mPackageManager;
166
167 // Content
168 private ContentType mContentType;
169 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700170 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700171
172 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700173 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700174 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700175 private IconCache mIconCache;
Winson Chung70fc4382011-08-08 15:31:33 -0700176 private int mDragViewMultiplyColor;
Winson Chung785d2eb2011-04-14 16:08:02 -0700177
178 // Dimens
179 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700180 private int mAppIconSize;
Winson Chung4b576be2011-04-27 17:40:20 -0700181 private int mMaxWidgetSpan, mMinWidgetSpan;
Winson Chung4b576be2011-04-27 17:40:20 -0700182 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700183 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700184 private final int mWidgetPreviewIconPaddedDimension;
185 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700186 private PagedViewCellLayout mWidgetSpacingLayout;
187
Winson Chungb44b5242011-06-13 11:32:14 -0700188 // Previews & outlines
189 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
190 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chung4b576be2011-04-27 17:40:20 -0700191
Winson Chung785d2eb2011-04-14 16:08:02 -0700192 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
193 super(context, attrs);
194 mLayoutInflater = LayoutInflater.from(context);
195 mPackageManager = context.getPackageManager();
196 mContentType = ContentType.Applications;
197 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700198 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700199 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700200 mHolographicOutlineHelper = new HolographicOutlineHelper();
201 mCanvas = new Canvas();
202 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700203
204 // Save the default widget preview background
205 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700206 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chung70fc4382011-08-08 15:31:33 -0700207 mAppIconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size);
208 mDragViewMultiplyColor = resources.getColor(R.color.drag_view_multiply_color);
Winson Chung785d2eb2011-04-14 16:08:02 -0700209
210 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700211 // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after
Winson Chung785d2eb2011-04-14 16:08:02 -0700212 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
213 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
214 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700215 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700216 mWidgetWidthGap =
217 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
218 mWidgetHeightGap =
219 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700220 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
221 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
222 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700223 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700224
225 // The max widget span is the length N, such that NxN is the largest bounds that the widget
226 // preview can be before applying the widget scaling
227 mMinWidgetSpan = 1;
228 mMaxWidgetSpan = 3;
Winson Chung1ed747a2011-05-03 16:18:34 -0700229
230 // The padding on the non-matched dimension for the default widget preview icons
231 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700232 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700233 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung54c725c2011-08-03 12:03:40 -0700234 mFadeInAdjacentScreens = LauncherApplication.isScreenLarge();
Winson Chung785d2eb2011-04-14 16:08:02 -0700235 }
236
237 @Override
238 protected void init() {
239 super.init();
240 mCenterPagesVertically = false;
241
242 Context context = getContext();
243 Resources r = context.getResources();
244 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
245 }
246
Winson Chungf0ea4d32011-06-06 14:27:16 -0700247 @Override
Michael Jurkad771c962011-08-09 15:00:48 -0700248 protected void onUnhandledTap(MotionEvent ev) {
249 if (LauncherApplication.isScreenLarge()) {
Winson Chungde1af762011-07-21 16:44:07 -0700250 // Dismiss AppsCustomize if we tap
251 mLauncher.showWorkspace(true);
252 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700253 }
254
255 /**
256 * This differs from isDataReady as this is the test done if isDataReady is not set.
257 */
258 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700259 // We only do this test once, and we default to the Applications page, so we only really
260 // have to wait for there to be apps.
261 return !mApps.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700262 }
263
264 protected void onDataReady(int width, int height) {
265 // Note that we transpose the counts in portrait so that we get a similar layout
266 boolean isLandscape = getResources().getConfiguration().orientation ==
267 Configuration.ORIENTATION_LANDSCAPE;
268 int maxCellCountX = Integer.MAX_VALUE;
269 int maxCellCountY = Integer.MAX_VALUE;
270 if (LauncherApplication.isScreenLarge()) {
271 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
272 LauncherModel.getCellCountY());
273 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
274 LauncherModel.getCellCountX());
275 }
276
277 // Now that the data is ready, we can calculate the content width, the number of cells to
278 // use for each page
279 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
280 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
281 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
282 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
283 mCellCountX = mWidgetSpacingLayout.getCellCountX();
284 mCellCountY = mWidgetSpacingLayout.getCellCountY();
285 mWidgetCountX = Math.max(1, (int) Math.round(mCellCountX / 2f));
286 mWidgetCountY = Math.max(1, (int) Math.round(mCellCountY / 3f));
Winson Chung5a808352011-06-27 19:08:49 -0700287
Winson Chungdb1138b2011-06-30 14:39:35 -0700288 // Force a measure to update recalculate the gaps
289 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
290 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
291 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700292 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700293 invalidatePageData();
294 }
295
296 @Override
297 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
298 int width = MeasureSpec.getSize(widthMeasureSpec);
299 int height = MeasureSpec.getSize(heightMeasureSpec);
300 if (!isDataReady()) {
301 if (testDataReady()) {
302 setDataIsReady();
303 setMeasuredDimension(width, height);
304 onDataReady(width, height);
305 }
306 }
307
308 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
309 }
310
Winson Chung34efdaf2011-05-24 14:19:56 -0700311 /** Removes and returns the ResolveInfo with the specified ComponentName */
312 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
313 ComponentName cn) {
314 Iterator<ResolveInfo> iter = list.iterator();
315 while (iter.hasNext()) {
316 ResolveInfo rinfo = iter.next();
317 ActivityInfo info = rinfo.activityInfo;
318 ComponentName c = new ComponentName(info.packageName, info.name);
319 if (c.equals(cn)) {
320 iter.remove();
321 return rinfo;
322 }
323 }
324 return null;
325 }
326
Winson Chung785d2eb2011-04-14 16:08:02 -0700327 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700328 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700329 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700330 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700331 List<AppWidgetProviderInfo> widgets =
332 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700333 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
334 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700335 mWidgets.addAll(widgets);
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700336 mWidgets.addAll(shortcuts);
337 Collections.sort(mWidgets,
338 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung785d2eb2011-04-14 16:08:02 -0700339
Winson Chung875de7e2011-06-28 14:25:17 -0700340 if (wasEmpty) {
341 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
342 // a layout to do this test and invalidate the page data when ready.
343 if (testDataReady()) requestLayout();
344 } else {
345 invalidatePageData();
346 }
Winson Chung4b576be2011-04-27 17:40:20 -0700347 }
348
349 @Override
350 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700351 // When we have exited all apps or are in transition, disregard clicks
352 if (!mLauncher.isAllAppsCustomizeOpen() ||
353 mLauncher.getWorkspace().isSwitchingState()) return;
354
Winson Chung4b576be2011-04-27 17:40:20 -0700355 if (v instanceof PagedViewIcon) {
356 // Animate some feedback to the click
357 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
358 animateClickFeedback(v, new Runnable() {
359 @Override
360 public void run() {
361 mLauncher.startActivitySafely(appInfo.intent, appInfo);
362 }
363 });
364 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700365 // Let the user know that they have to long press to add a widget
366 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
367 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700368
Winson Chungd2e87b32011-06-02 10:53:07 -0700369 // Create a little animation to show that the widget can move
370 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
371 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
372 AnimatorSet bounce = new AnimatorSet();
373 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
374 tyuAnim.setDuration(125);
375 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
376 tydAnim.setDuration(100);
377 bounce.play(tyuAnim).before(tydAnim);
378 bounce.setInterpolator(new AccelerateInterpolator());
379 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700380 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700381 }
382
383 /*
384 * PagedViewWithDraggableItems implementation
385 */
386 @Override
387 protected void determineDraggingStart(android.view.MotionEvent ev) {
388 // Disable dragging by pulling an app down for now.
389 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700390
Winson Chung4b576be2011-04-27 17:40:20 -0700391 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700392 mLauncher.getWorkspace().onDragStartedWithItem(v);
393 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700394 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700395
Winson Chung4b576be2011-04-27 17:40:20 -0700396 private void beginDraggingWidget(View v) {
397 // Get the widget preview as the drag representation
398 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700399 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700400
401 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700402 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700403 Drawable preview = image.getDrawable();
404 int w = preview.getIntrinsicWidth();
405 int h = preview.getIntrinsicHeight();
Winson Chung1ed747a2011-05-03 16:18:34 -0700406 if (createItemInfo instanceof PendingAddWidgetInfo) {
407 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
408 int[] spanXY = CellLayout.rectToCell(getResources(),
409 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
410 createItemInfo.spanX = spanXY[0];
411 createItemInfo.spanY = spanXY[1];
412
413 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Winson Chung70fc4382011-08-08 15:31:33 -0700414 renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1, mDragViewMultiplyColor);
Winson Chung1ed747a2011-05-03 16:18:34 -0700415 } else {
416 // Workaround for the fact that we don't keep the original ResolveInfo associated with
417 // the shortcut around. To get the icon, we just render the preview image (which has
418 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
419 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
420 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700421 mCanvas.setBitmap(b);
422 mCanvas.save();
423 preview.draw(mCanvas);
424 mCanvas.restore();
Winson Chung70fc4382011-08-08 15:31:33 -0700425 mCanvas.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700426 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700427 createItemInfo.spanX = createItemInfo.spanY = 1;
428 }
Winson Chung4b576be2011-04-27 17:40:20 -0700429
430 // Start the drag
Winson Chung4b576be2011-04-27 17:40:20 -0700431 mLauncher.lockScreenOrientation();
Winson Chung1ed747a2011-05-03 16:18:34 -0700432 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
433 createItemInfo.spanY, b);
434 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700435 DragController.DRAG_ACTION_COPY, null);
436 b.recycle();
437 }
438 @Override
439 protected boolean beginDragging(View v) {
440 if (!super.beginDragging(v)) return false;
441
Winson Chungc07918d2011-07-01 15:35:26 -0700442 // Go into spring loaded mode (must happen before we startDrag())
443 int currentPageIndex = mLauncher.getWorkspace().getCurrentPage();
444 CellLayout currentPage = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPageIndex);
445 mLauncher.enterSpringLoadedDragMode(currentPage);
Winson Chungfc79c802011-05-02 13:35:34 -0700446
Winson Chung4b576be2011-04-27 17:40:20 -0700447 if (v instanceof PagedViewIcon) {
448 beginDraggingApplication(v);
449 } else if (v instanceof PagedViewWidget) {
450 beginDraggingWidget(v);
451 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700452 return true;
453 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700454 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700455 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700456 if (!success || (target != mLauncher.getWorkspace() &&
457 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700458 // Exit spring loaded mode if we have not successfully dropped or have not handled the
459 // drop in Workspace
460 mLauncher.exitSpringLoadedDragMode();
461 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700462 mLauncher.unlockScreenOrientation();
Winson Chungb26f3d62011-06-02 10:49:29 -0700463
Winson Chung785d2eb2011-04-14 16:08:02 -0700464 }
465
Winson Chung785d2eb2011-04-14 16:08:02 -0700466 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700467 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700468 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700469
470 // Display an error message if the drag failed due to there not being enough space on the
471 // target layout we were dropping on.
472 if (!success) {
473 boolean showOutOfSpaceMessage = false;
474 if (target instanceof Workspace) {
475 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
476 Workspace workspace = (Workspace) target;
477 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700478 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700479 if (layout != null) {
480 layout.calculateSpans(itemInfo);
481 showOutOfSpaceMessage =
482 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
483 }
484 }
485 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
486 if (showOutOfSpaceMessage) {
487 mLauncher.showOutOfSpaceMessage();
488 }
489 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700490 }
491
492 public void setContentType(ContentType type) {
493 mContentType = type;
Winson Chung5a808352011-06-27 19:08:49 -0700494 invalidatePageData(0);
Winson Chung785d2eb2011-04-14 16:08:02 -0700495 }
496
Winson Chungb44b5242011-06-13 11:32:14 -0700497 public boolean isContentType(ContentType type) {
498 return (mContentType == type);
499 }
500
Winson Chung5a808352011-06-27 19:08:49 -0700501 public void setCurrentPageToWidgets() {
502 invalidatePageData(0);
503 }
Winson Chung5a808352011-06-27 19:08:49 -0700504
Winson Chung785d2eb2011-04-14 16:08:02 -0700505 /*
506 * Apps PagedView implementation
507 */
Winson Chung63257c12011-05-05 17:06:13 -0700508 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
509 int childCount = layout.getChildCount();
510 for (int i = 0; i < childCount; ++i) {
511 layout.getChildAt(i).setVisibility(visibility);
512 }
513 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700514 private void setupPage(PagedViewCellLayout layout) {
515 layout.setCellCount(mCellCountX, mCellCountY);
516 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
517 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
518 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
519
Winson Chung63257c12011-05-05 17:06:13 -0700520 // Note: We force a measure here to get around the fact that when we do layout calculations
521 // immediately after syncing, we don't have a proper width. That said, we already know the
522 // expected page width, so we can actually optimize by hiding all the TextView-based
523 // children that are expensive to measure, and let that happen naturally later.
524 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700525 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700526 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700527 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700528 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700529 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700530 }
531 public void syncAppsPages() {
532 // Ensure that we have the right number of pages
533 Context context = getContext();
534 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
535 for (int i = 0; i < numPages; ++i) {
536 PagedViewCellLayout layout = new PagedViewCellLayout(context);
537 setupPage(layout);
538 addView(layout);
539 }
540 }
541 public void syncAppsPageItems(int page) {
542 // ensure that we have the right number of items on the pages
543 int numPages = getPageCount();
544 int numCells = mCellCountX * mCellCountY;
545 int startIndex = page * numCells;
546 int endIndex = Math.min(startIndex + numCells, mApps.size());
547 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700548
Winson Chung785d2eb2011-04-14 16:08:02 -0700549 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700550 ArrayList<Object> items = new ArrayList<Object>();
551 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700552 for (int i = startIndex; i < endIndex; ++i) {
553 ApplicationInfo info = mApps.get(i);
554 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
555 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700556 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700557 icon.setOnClickListener(this);
558 icon.setOnLongClickListener(this);
559 icon.setOnTouchListener(this);
560
561 int index = i - startIndex;
562 int x = index % mCellCountX;
563 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700564 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700565
566 items.add(info);
567 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700568 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700569
570 // Create the hardware layers
571 layout.allowHardwareLayerCreation();
572 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700573
Winson Chung54c725c2011-08-03 12:03:40 -0700574 if (mFadeInAdjacentScreens) {
575 prepareGenerateHoloOutlinesTask(page, items, images);
576 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700577 }
Winson Chungb44b5242011-06-13 11:32:14 -0700578
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 Chungd2945262011-06-24 15:22:14 -0700601 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700602 // 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 Chung875de7e2011-06-28 14:25:17 -0700607 if ((taskPage == page) ||
608 taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
Winson Chung4e076542011-06-23 13:04:10 -0700609 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700610 task.cancel(false);
611 iter.remove();
612 } else {
613 task.setThreadPriority(getThreadPriorityForPage(taskPage));
614 }
615 }
616
617 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700618 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700619 @Override
620 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
621 // Ensure that this task starts running at the correct priority
622 task.syncThreadPriority();
623
624 // Load each of the widget/shortcut previews
625 ArrayList<Object> items = data.items;
Winson Chung4e076542011-06-23 13:04:10 -0700626 ArrayList<Bitmap> images = data.generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700627 int count = items.size();
628 int cellWidth = data.cellWidth;
629 int cellHeight = data.cellHeight;
630 for (int i = 0; i < count && !task.isCancelled(); ++i) {
631 // Before work on each item, ensure that this task is running at the correct
632 // priority
633 task.syncThreadPriority();
634
635 Object rawInfo = items.get(i);
636 if (rawInfo instanceof AppWidgetProviderInfo) {
637 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
638 int[] cellSpans = CellLayout.rectToCell(getResources(),
639 info.minWidth, info.minHeight, null);
Winson Chung4e076542011-06-23 13:04:10 -0700640 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
Winson Chungb44b5242011-06-13 11:32:14 -0700641 cellWidth, cellHeight));
642 } else if (rawInfo instanceof ResolveInfo) {
643 // Fill in the shortcuts information
644 ResolveInfo info = (ResolveInfo) rawInfo;
Winson Chung4e076542011-06-23 13:04:10 -0700645 images.add(getShortcutPreview(info, cellWidth, cellHeight));
Winson Chungb44b5242011-06-13 11:32:14 -0700646 }
647 }
648 }
649 },
650 new AsyncTaskCallback() {
651 @Override
652 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
653 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700654 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700655 if (task.page > getPageCount()) return;
656 if (task.pageContentType != mContentType) return;
657 onSyncWidgetPageItems(data);
658 }
659 });
660
661 // Ensure that the task is appropriately prioritized and runs in parallel
Winson Chung875de7e2011-06-28 14:25:17 -0700662 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType,
663 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700664 t.setThreadPriority(getThreadPriorityForPage(page));
665 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
666 mRunningTasks.add(t);
667 }
668 /**
669 * Creates and executes a new AsyncTask to load the outlines for a page of content.
670 */
671 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
672 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700673 // Prune old tasks for this page
674 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
675 while (iter.hasNext()) {
676 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
677 int taskPage = task.page;
678 if ((taskPage == page) &&
679 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
680 task.cancel(false);
681 iter.remove();
682 }
683 }
684
Winson Chungb44b5242011-06-13 11:32:14 -0700685 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
686 new AsyncTaskCallback() {
687 @Override
688 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
689 // Ensure that this task starts running at the correct priority
690 task.syncThreadPriority();
691
Winson Chung4e076542011-06-23 13:04:10 -0700692 ArrayList<Bitmap> images = data.generatedImages;
693 ArrayList<Bitmap> srcImages = data.sourceImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700694 int count = srcImages.size();
695 Canvas c = new Canvas();
696 for (int i = 0; i < count && !task.isCancelled(); ++i) {
697 // Before work on each item, ensure that this task is running at the correct
698 // priority
699 task.syncThreadPriority();
700
701 Bitmap b = srcImages.get(i);
702 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
703 Bitmap.Config.ARGB_8888);
704
705 c.setBitmap(outline);
706 c.save();
707 c.drawBitmap(b, 0, 0, null);
708 c.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700709 c.setBitmap(null);
Winson Chungb44b5242011-06-13 11:32:14 -0700710
711 images.add(outline);
712 }
713 }
714 },
715 new AsyncTaskCallback() {
716 @Override
717 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
718 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700719 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700720 if (task.page > getPageCount()) return;
721 if (task.pageContentType != mContentType) return;
722 onHolographicPageItemsLoaded(data);
723 }
724 });
725
726 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700727 AppsCustomizeAsyncTask t =
Winson Chung875de7e2011-06-28 14:25:17 -0700728 new AppsCustomizeAsyncTask(page, mContentType,
729 AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700730 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
731 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
732 mRunningTasks.add(t);
733 }
734
Winson Chung785d2eb2011-04-14 16:08:02 -0700735 /*
736 * Widgets PagedView implementation
737 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700738 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700739 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
740 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700741
742 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700743 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700744 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
745 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700746 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700747 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700748 }
Michael Jurkabe69cc82011-07-07 16:05:33 -0700749 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung785d2eb2011-04-14 16:08:02 -0700750 float scaleX, float scaleY) {
Winson Chung70fc4382011-08-08 15:31:33 -0700751 renderDrawableToBitmap(d, bitmap, x, y, w, h, scaleX, scaleY, 0xFFFFFFFF);
752 }
753 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
754 float scaleX, float scaleY, int multiplyColor) {
Winson Chung201bc822011-06-20 15:41:53 -0700755 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700756 Canvas c = new Canvas(bitmap);
Winson Chung201bc822011-06-20 15:41:53 -0700757 c.scale(scaleX, scaleY);
758 Rect oldBounds = d.copyBounds();
759 d.setBounds(x, y, x + w, y + h);
760 d.draw(c);
761 d.setBounds(oldBounds); // Restore the bounds
Winson Chung70fc4382011-08-08 15:31:33 -0700762 if (multiplyColor != 0xFFFFFFFF) {
763 c.drawColor(mDragViewMultiplyColor, PorterDuff.Mode.MULTIPLY);
764 }
Adam Cohenaaf473c2011-08-03 12:02:47 -0700765 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700766 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700767 }
Winson Chungd2945262011-06-24 15:22:14 -0700768 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700769 // Render the icon
Winson Chungd2945262011-06-24 15:22:14 -0700770 Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700771 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chungd2945262011-06-24 15:22:14 -0700772 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chungb44b5242011-06-13 11:32:14 -0700773 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700774 }
Winson Chung4e076542011-06-23 13:04:10 -0700775 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700776 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700777
Winson Chung4b576be2011-04-27 17:40:20 -0700778 // Calculate the size of the drawable
779 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
780 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
781 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
782 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
783
784 // Scale down the bitmap to fit the space
785 float widgetPreviewScale = (float) cellWidth / expectedWidth;
786 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
787 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
788
789 // Load the preview image if possible
790 String packageName = info.provider.getPackageName();
791 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700792 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700793 if (info.previewImage != 0) {
794 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
795 if (drawable == null) {
796 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
797 + " for provider: " + info.provider);
798 } else {
799 // Scale down the preview to the dimensions we want
800 int imageWidth = drawable.getIntrinsicWidth();
801 int imageHeight = drawable.getIntrinsicHeight();
802 float aspect = (float) imageWidth / imageHeight;
803 int newWidth = imageWidth;
804 int newHeight = imageHeight;
805 if (aspect > 1f) {
806 newWidth = expectedWidth;
807 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
808 } else {
809 newHeight = expectedHeight;
810 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
811 }
812
Winson Chungb44b5242011-06-13 11:32:14 -0700813 preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
Winson Chung4b576be2011-04-27 17:40:20 -0700814 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700815 }
816 }
817
818 // Generate a preview image if we couldn't load one
819 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700820 Resources resources = mLauncher.getResources();
Winson Chung273c1022011-07-11 13:40:52 -0700821 int bitmapWidth;
822 int bitmapHeight;
Winson Chung1ed747a2011-05-03 16:18:34 -0700823
Winson Chung273c1022011-07-11 13:40:52 -0700824 // Specify the dimensions of the bitmap (since we are using a default preview bg with
825 // the full icon, we only imply the aspect ratio of the widget)
826 if (cellHSpan == cellVSpan) {
827 bitmapWidth = bitmapHeight = cellWidth;
828 expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension;
829 } else if (cellHSpan >= cellVSpan) {
830 bitmapWidth = expectedWidth = cellWidth;
831 bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension;
Winson Chung1ed747a2011-05-03 16:18:34 -0700832 } else {
833 // Note that in vertical widgets, we might not have enough space due to the text
834 // label, so be conservative and use the width as a height bound
Winson Chung273c1022011-07-11 13:40:52 -0700835 bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension;
836 bitmapHeight = expectedHeight = cellWidth;
Winson Chung1ed747a2011-05-03 16:18:34 -0700837 }
Winson Chung4b576be2011-04-27 17:40:20 -0700838
Winson Chung273c1022011-07-11 13:40:52 -0700839 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700840 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
Winson Chung70fc4382011-08-08 15:31:33 -0700841 expectedHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700842
843 // Draw the icon in the top left corner
844 try {
845 Drawable icon = null;
846 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
847 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
848
Winson Chungd2945262011-06-24 15:22:14 -0700849 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
850 renderDrawableToBitmap(icon, preview, offset, offset,
851 mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700852 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -0700853 }
Winson Chungb44b5242011-06-13 11:32:14 -0700854 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -0700855 }
856 public void syncWidgetPages() {
857 // Ensure that we have the right number of pages
858 Context context = getContext();
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700859 int numPages = (int) Math.ceil(mWidgets.size() /
860 (float) (mWidgetCountX * mWidgetCountY));
861 for (int j = 0; j < numPages; ++j) {
862 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
863 mWidgetCountY);
864 setupPage(layout);
865 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -0700866 }
867 }
868 public void syncWidgetPageItems(int page) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700869 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700870 int contentWidth = mWidgetSpacingLayout.getContentWidth();
871 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -0700872
Winson Chungd2945262011-06-24 15:22:14 -0700873 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -0700874 ArrayList<Object> items = new ArrayList<Object>();
875 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700876 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -0700877 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700878 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -0700879
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700880 int offset = page * numItemsPerPage;
881 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
882 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700883 }
884
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700885 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
Winson Chungb44b5242011-06-13 11:32:14 -0700886 }
887 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
888 int page = data.page;
Winson Chung4e6a9762011-05-09 11:56:34 -0700889 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page);
Winson Chungd52f3d82011-07-12 14:29:11 -0700890 // Only set the column count once we have items
891 layout.setColumnCount(layout.getCellCountX());
Winson Chung785d2eb2011-04-14 16:08:02 -0700892
Winson Chungb44b5242011-06-13 11:32:14 -0700893 ArrayList<Object> items = data.items;
894 int count = items.size();
895 int cellWidth = data.cellWidth;
896 int cellHeight = data.cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700897 int cellCountX = data.cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700898 for (int i = 0; i < count; ++i) {
899 Object rawInfo = items.get(i);
Winson Chung1ed747a2011-05-03 16:18:34 -0700900 PendingAddItemInfo createItemInfo = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700901 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
902 R.layout.apps_customize_widget, layout, false);
Winson Chung1ed747a2011-05-03 16:18:34 -0700903 if (rawInfo instanceof AppWidgetProviderInfo) {
904 // Fill in the widget information
905 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
906 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chungb44b5242011-06-13 11:32:14 -0700907 int[] cellSpans = CellLayout.rectToCell(getResources(),
908 info.minWidth, info.minHeight, null);
Winson Chung4e076542011-06-23 13:04:10 -0700909 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chung9f4e0fd2011-06-02 18:59:36 -0700910 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans,
Winson Chungb44b5242011-06-13 11:32:14 -0700911 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -0700912 widget.setTag(createItemInfo);
913 } else if (rawInfo instanceof ResolveInfo) {
914 // Fill in the shortcuts information
915 ResolveInfo info = (ResolveInfo) rawInfo;
916 createItemInfo = new PendingAddItemInfo();
917 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
918 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
919 info.activityInfo.name);
Winson Chung4e076542011-06-23 13:04:10 -0700920 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700921 widget.applyFromResolveInfo(mPackageManager, info, preview,
922 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -0700923 widget.setTag(createItemInfo);
924 }
Winson Chung4b576be2011-04-27 17:40:20 -0700925 widget.setOnClickListener(this);
926 widget.setOnLongClickListener(this);
927 widget.setOnTouchListener(this);
928
929 // Layout each widget
Winson Chungd2945262011-06-24 15:22:14 -0700930 int ix = i % cellCountX;
931 int iy = i / cellCountX;
Winson Chungfd3385f2011-06-15 19:51:24 -0700932 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Winson Chung72d8b392011-07-29 13:56:44 -0700933 GridLayout.spec(iy, GridLayout.LEFT),
934 GridLayout.spec(ix, GridLayout.TOP));
Winson Chungfd3385f2011-06-15 19:51:24 -0700935 lp.width = cellWidth;
936 lp.height = cellHeight;
Winson Chung72d8b392011-07-29 13:56:44 -0700937 lp.setGravity(Gravity.TOP | Gravity.LEFT);
Winson Chungfd3385f2011-06-15 19:51:24 -0700938 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
939 if (iy > 0) lp.topMargin = mWidgetHeightGap;
Winson Chung4e6a9762011-05-09 11:56:34 -0700940 layout.addView(widget, lp);
Winson Chung785d2eb2011-04-14 16:08:02 -0700941 }
Winson Chungb44b5242011-06-13 11:32:14 -0700942
943 invalidate();
944 forceUpdateAdjacentPagesAlpha();
Winson Chung54c725c2011-08-03 12:03:40 -0700945
946 if (mFadeInAdjacentScreens) {
947 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
948 }
Winson Chungb44b5242011-06-13 11:32:14 -0700949 }
950 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
951 // Invalidate early to short-circuit children invalidates
952 invalidate();
953
954 int page = data.page;
955 ViewGroup layout = (ViewGroup) getChildAt(page);
956 if (layout instanceof PagedViewCellLayout) {
957 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
958 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -0700959 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700960 for (int i = 0; i < count; ++i) {
961 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -0700962 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700963 }
964 } else {
965 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -0700966 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700967 for (int i = 0; i < count; ++i) {
968 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -0700969 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700970 }
971 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700972 }
Winson Chung46af2e82011-05-09 16:00:53 -0700973
Winson Chung785d2eb2011-04-14 16:08:02 -0700974 @Override
975 public void syncPages() {
976 removeAllViews();
Winson Chung875de7e2011-06-28 14:25:17 -0700977
978 // Remove all background asyc tasks if we are loading content anew
979 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
980 while (iter.hasNext()) {
981 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
982 task.cancel(false);
983 iter.remove();
984 }
985
Winson Chung785d2eb2011-04-14 16:08:02 -0700986 switch (mContentType) {
987 case Applications:
988 syncAppsPages();
989 break;
990 case Widgets:
991 syncWidgetPages();
992 break;
993 }
994 }
995 @Override
996 public void syncPageItems(int page) {
997 switch (mContentType) {
998 case Applications:
999 syncAppsPageItems(page);
1000 break;
1001 case Widgets:
1002 syncWidgetPageItems(page);
1003 break;
1004 }
1005 }
1006
1007 /**
1008 * Used by the parent to get the content width to set the tab bar to
1009 * @return
1010 */
1011 public int getPageContentWidth() {
1012 return mContentWidth;
1013 }
1014
Winson Chungb26f3d62011-06-02 10:49:29 -07001015 @Override
1016 protected void onPageBeginMoving() {
1017 /* TO BE ENABLED LATER
1018 setChildrenDrawnWithCacheEnabled(true);
1019 for (int i = 0; i < getChildCount(); ++i) {
1020 View v = getChildAt(i);
1021 if (v instanceof PagedViewCellLayout) {
1022 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true);
1023 }
1024 }
1025 */
1026 super.onPageBeginMoving();
1027 }
1028
1029 @Override
1030 protected void onPageEndMoving() {
1031 /* TO BE ENABLED LATER
1032 for (int i = 0; i < getChildCount(); ++i) {
1033 View v = getChildAt(i);
1034 if (v instanceof PagedViewCellLayout) {
1035 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false);
1036 }
1037 }
1038 setChildrenDrawnWithCacheEnabled(false);
1039 */
1040 super.onPageEndMoving();
1041 }
1042
Winson Chung785d2eb2011-04-14 16:08:02 -07001043 /*
1044 * AllAppsView implementation
1045 */
1046 @Override
1047 public void setup(Launcher launcher, DragController dragController) {
1048 mLauncher = launcher;
1049 mDragController = dragController;
1050 }
1051 @Override
1052 public void zoom(float zoom, boolean animate) {
1053 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1054 }
1055 @Override
1056 public boolean isVisible() {
1057 return (getVisibility() == VISIBLE);
1058 }
1059 @Override
1060 public boolean isAnimating() {
1061 return false;
1062 }
1063 @Override
1064 public void setApps(ArrayList<ApplicationInfo> list) {
1065 mApps = list;
1066 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001067
Winson Chung875de7e2011-06-28 14:25:17 -07001068 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1069 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001070 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001071 }
1072 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1073 // We add it in place, in alphabetical order
1074 int count = list.size();
1075 for (int i = 0; i < count; ++i) {
1076 ApplicationInfo info = list.get(i);
1077 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1078 if (index < 0) {
1079 mApps.add(-(index + 1), info);
1080 }
1081 }
1082 }
1083 @Override
1084 public void addApps(ArrayList<ApplicationInfo> list) {
1085 addAppsWithoutInvalidate(list);
1086 invalidatePageData();
1087 }
1088 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1089 ComponentName removeComponent = item.intent.getComponent();
1090 int length = list.size();
1091 for (int i = 0; i < length; ++i) {
1092 ApplicationInfo info = list.get(i);
1093 if (info.intent.getComponent().equals(removeComponent)) {
1094 return i;
1095 }
1096 }
1097 return -1;
1098 }
1099 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1100 // loop through all the apps and remove apps that have the same component
1101 int length = list.size();
1102 for (int i = 0; i < length; ++i) {
1103 ApplicationInfo info = list.get(i);
1104 int removeIndex = findAppByComponent(mApps, info);
1105 if (removeIndex > -1) {
1106 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001107 }
1108 }
1109 }
1110 @Override
1111 public void removeApps(ArrayList<ApplicationInfo> list) {
1112 removeAppsWithoutInvalidate(list);
1113 invalidatePageData();
1114 }
1115 @Override
1116 public void updateApps(ArrayList<ApplicationInfo> list) {
1117 // We remove and re-add the updated applications list because it's properties may have
1118 // changed (ie. the title), and this will ensure that the items will be in their proper
1119 // place in the list.
1120 removeAppsWithoutInvalidate(list);
1121 addAppsWithoutInvalidate(list);
1122 invalidatePageData();
1123 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001124
Winson Chung785d2eb2011-04-14 16:08:02 -07001125 @Override
1126 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001127 if (mContentType != ContentType.Applications) {
1128 // Reset to the first page of the Apps pane
1129 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1130 mLauncher.findViewById(R.id.apps_customize_pane);
Winson Chung5a808352011-06-27 19:08:49 -07001131 tabs.selectAppsTab();
Michael Jurka35aa14d2011-07-07 17:01:08 -07001132 } else if (getCurrentPage() != 0) {
Winson Chung5a808352011-06-27 19:08:49 -07001133 invalidatePageData(0);
Winson Chungfc79c802011-05-02 13:35:34 -07001134 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001135 }
1136 @Override
1137 public void dumpState() {
1138 // TODO: Dump information related to current list of Applications, Widgets, etc.
1139 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1140 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1141 }
1142 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001143 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001144 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001145 for (Object i: list) {
1146 if (i instanceof AppWidgetProviderInfo) {
1147 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1148 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1149 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1150 + " initialLayout=" + info.initialLayout
1151 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1152 } else if (i instanceof ResolveInfo) {
1153 ResolveInfo info = (ResolveInfo) i;
1154 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1155 + info.icon);
1156 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001157 }
1158 }
1159 @Override
1160 public void surrender() {
1161 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1162 // should stop this now.
1163 }
Winson Chung007c6982011-06-14 13:27:53 -07001164
Winson Chungb44b5242011-06-13 11:32:14 -07001165 /*
1166 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1167 * widget previews in the background with the AsyncTasks.
1168 */
1169 protected int getAssociatedLowerPageBound(int page) {
1170 return Math.max(0, page - 2);
1171 }
1172 protected int getAssociatedUpperPageBound(int page) {
1173 final int count = getChildCount();
1174 return Math.min(page + 2, count - 1);
1175 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001176
1177 @Override
1178 protected String getCurrentPageDescription() {
1179 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1180 int stringId = R.string.default_scroll_format;
1181 switch (mContentType) {
1182 case Applications:
1183 stringId = R.string.apps_customize_apps_scroll_format;
1184 break;
1185 case Widgets:
1186 stringId = R.string.apps_customize_widgets_scroll_format;
1187 break;
1188 }
1189 return String.format(mContext.getString(stringId), page + 1, getChildCount());
1190 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001191}