blob: e5fe7bd2654450df7a8847f92cbae18c9a1cf72e [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;
36import android.graphics.Rect;
37import android.graphics.drawable.Drawable;
Winson Chungb44b5242011-06-13 11:32:14 -070038import android.os.AsyncTask;
39import android.os.Process;
Winson Chung785d2eb2011-04-14 16:08:02 -070040import android.util.AttributeSet;
41import android.util.Log;
Winson Chung72d8b392011-07-29 13:56:44 -070042import android.view.Gravity;
Winson Chung785d2eb2011-04-14 16:08:02 -070043import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070044import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070045import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070046import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070047import android.view.animation.AccelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070048import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070049import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070050import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070051
52import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053import com.android.launcher2.DropTarget.DragObject;
54
55import java.util.ArrayList;
56import java.util.Collections;
57import java.util.Iterator;
58import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070059
Winson Chungb44b5242011-06-13 11:32:14 -070060/**
61 * A simple callback interface which also provides the results of the task.
62 */
63interface AsyncTaskCallback {
64 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
65}
Winson Chung4e076542011-06-23 13:04:10 -070066
Winson Chungb44b5242011-06-13 11:32:14 -070067/**
68 * The data needed to perform either of the custom AsyncTasks.
69 */
70class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070071 enum Type {
72 LoadWidgetPreviewData,
73 LoadHolographicIconsData
74 }
75
Winson Chungb44b5242011-06-13 11:32:14 -070076 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
77 AsyncTaskCallback postR) {
78 page = p;
79 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070080 sourceImages = si;
81 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070082 cellWidth = cellHeight = -1;
83 doInBackgroundCallback = bgR;
84 postExecuteCallback = postR;
85 }
Winson Chungd2945262011-06-24 15:22:14 -070086 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, int ccx, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070087 AsyncTaskCallback postR) {
88 page = p;
89 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070090 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070091 cellWidth = cw;
92 cellHeight = ch;
Winson Chungd2945262011-06-24 15:22:14 -070093 cellCountX = ccx;
Winson Chungb44b5242011-06-13 11:32:14 -070094 doInBackgroundCallback = bgR;
95 postExecuteCallback = postR;
96 }
97 int page;
98 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070099 ArrayList<Bitmap> sourceImages;
100 ArrayList<Bitmap> generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700101 int cellWidth;
102 int cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700103 int cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700104 AsyncTaskCallback doInBackgroundCallback;
105 AsyncTaskCallback postExecuteCallback;
106}
Winson Chung4e076542011-06-23 13:04:10 -0700107
Winson Chungb44b5242011-06-13 11:32:14 -0700108/**
109 * A generic template for an async task used in AppsCustomize.
110 */
111class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Winson Chung875de7e2011-06-28 14:25:17 -0700112 AppsCustomizeAsyncTask(int p, AppsCustomizePagedView.ContentType t, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700113 page = p;
114 pageContentType = t;
115 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700116 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700117 }
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 Chung875de7e2011-06-28 14:25:17 -0700139 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700140 int page;
141 AppsCustomizePagedView.ContentType pageContentType;
142 int threadPriority;
143}
Winson Chungb44b5242011-06-13 11:32:14 -0700144
145/**
146 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
147 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700148public 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 Chung6a26e5b2011-05-26 14:36:06 -0700157 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700158 }
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 Chungd2945262011-06-24 15:22:14 -0700169 private ArrayList<Object> mWidgets;
Winson Chung1ed747a2011-05-03 16:18:34 -0700170
171 // Caching
Winson Chungb44b5242011-06-13 11:32:14 -0700172 private Canvas mCanvas;
Winson Chung1ed747a2011-05-03 16:18:34 -0700173 private Drawable mDefaultWidgetBackground;
Winson Chung4dbea792011-05-05 14:21:32 -0700174 private IconCache mIconCache;
Winson Chung785d2eb2011-04-14 16:08:02 -0700175
176 // Dimens
177 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700178 private int mAppIconSize;
Winson Chung4b576be2011-04-27 17:40:20 -0700179 private int mMaxWidgetSpan, mMinWidgetSpan;
Winson Chung4b576be2011-04-27 17:40:20 -0700180 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700181 private int mWidgetWidthGap, mWidgetHeightGap;
Winson Chung1ed747a2011-05-03 16:18:34 -0700182 private final int mWidgetPreviewIconPaddedDimension;
183 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700184 private PagedViewCellLayout mWidgetSpacingLayout;
185
Winson Chungb44b5242011-06-13 11:32:14 -0700186 // Previews & outlines
187 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
188 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chung4b576be2011-04-27 17:40:20 -0700189
Winson Chung785d2eb2011-04-14 16:08:02 -0700190 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 Chung1ed747a2011-05-03 16:18:34 -0700196 mWidgets = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700197 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700198 mHolographicOutlineHelper = new HolographicOutlineHelper();
199 mCanvas = new Canvas();
200 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700201
202 // Save the default widget preview background
203 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700204 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chungd2945262011-06-24 15:22:14 -0700205 mAppIconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700206
207 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700208 // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after
Winson Chung785d2eb2011-04-14 16:08:02 -0700209 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
210 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
211 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700212 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700213 mWidgetWidthGap =
214 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
215 mWidgetHeightGap =
216 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700217 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
218 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
219 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700220 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700221
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 Chung1ed747a2011-05-03 16:18:34 -0700226
227 // The padding on the non-matched dimension for the default widget preview icons
228 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700229 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700230 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung785d2eb2011-04-14 16:08:02 -0700231 }
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 Chungf0ea4d32011-06-06 14:27:16 -0700243 @Override
Winson Chungde1af762011-07-21 16:44:07 -0700244 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 Chungf0ea4d32011-06-06 14:27:16 -0700250 }
251
252 /**
253 * This differs from isDataReady as this is the test done if isDataReady is not set.
254 */
255 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700256 // 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 Chungf0ea4d32011-06-06 14:27:16 -0700259 }
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 Chung5a808352011-06-27 19:08:49 -0700284
Winson Chungdb1138b2011-06-30 14:39:35 -0700285 // 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 Chungf0ea4d32011-06-06 14:27:16 -0700289 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700290 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 Chung34efdaf2011-05-24 14:19:56 -0700308 /** 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 Chung785d2eb2011-04-14 16:08:02 -0700324 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700325 // Get the list of widgets and shortcuts
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700326 boolean wasEmpty = mWidgets.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700327 mWidgets.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700328 List<AppWidgetProviderInfo> widgets =
329 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700330 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
331 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700332 mWidgets.addAll(widgets);
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700333 mWidgets.addAll(shortcuts);
334 Collections.sort(mWidgets,
335 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chung785d2eb2011-04-14 16:08:02 -0700336
Winson Chung875de7e2011-06-28 14:25:17 -0700337 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 Chung4b576be2011-04-27 17:40:20 -0700344 }
345
346 @Override
347 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700348 // When we have exited all apps or are in transition, disregard clicks
349 if (!mLauncher.isAllAppsCustomizeOpen() ||
350 mLauncher.getWorkspace().isSwitchingState()) return;
351
Winson Chung4b576be2011-04-27 17:40:20 -0700352 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 Chungd2e87b32011-06-02 10:53:07 -0700362 // 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 Chung46af2e82011-05-09 16:00:53 -0700365
Winson Chungd2e87b32011-06-02 10:53:07 -0700366 // 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 Chung4b576be2011-04-27 17:40:20 -0700377 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700378 }
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 Cohenac8c8762011-07-13 11:15:27 -0700387
Winson Chung4b576be2011-04-27 17:40:20 -0700388 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700389 mLauncher.getWorkspace().onDragStartedWithItem(v);
390 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700391 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700392
Winson Chung4b576be2011-04-27 17:40:20 -0700393 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 Chung1ed747a2011-05-03 16:18:34 -0700396 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700397
398 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700399 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700400 Drawable preview = image.getDrawable();
401 int w = preview.getIntrinsicWidth();
402 int h = preview.getIntrinsicHeight();
Winson Chung1ed747a2011-05-03 16:18:34 -0700403 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 Chungb44b5242011-06-13 11:32:14 -0700418 mCanvas.setBitmap(b);
419 mCanvas.save();
420 preview.draw(mCanvas);
421 mCanvas.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700422 mCanvas.setBitmap(null);
Winson Chung1ed747a2011-05-03 16:18:34 -0700423 createItemInfo.spanX = createItemInfo.spanY = 1;
424 }
Winson Chung4b576be2011-04-27 17:40:20 -0700425
426 // Start the drag
Winson Chung4b576be2011-04-27 17:40:20 -0700427 mLauncher.lockScreenOrientation();
Winson Chung1ed747a2011-05-03 16:18:34 -0700428 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
429 createItemInfo.spanY, b);
430 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700431 DragController.DRAG_ACTION_COPY, null);
432 b.recycle();
433 }
434 @Override
435 protected boolean beginDragging(View v) {
436 if (!super.beginDragging(v)) return false;
437
Winson Chungc07918d2011-07-01 15:35:26 -0700438 // Go into spring loaded mode (must happen before we startDrag())
439 int currentPageIndex = mLauncher.getWorkspace().getCurrentPage();
440 CellLayout currentPage = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPageIndex);
441 mLauncher.enterSpringLoadedDragMode(currentPage);
Winson Chungfc79c802011-05-02 13:35:34 -0700442
Winson Chung4b576be2011-04-27 17:40:20 -0700443 if (v instanceof PagedViewIcon) {
444 beginDraggingApplication(v);
445 } else if (v instanceof PagedViewWidget) {
446 beginDraggingWidget(v);
447 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700448 return true;
449 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700450 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700451 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700452 if (!success || (target != mLauncher.getWorkspace() &&
453 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700454 // Exit spring loaded mode if we have not successfully dropped or have not handled the
455 // drop in Workspace
456 mLauncher.exitSpringLoadedDragMode();
457 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700458 mLauncher.unlockScreenOrientation();
Winson Chungb26f3d62011-06-02 10:49:29 -0700459
Winson Chung785d2eb2011-04-14 16:08:02 -0700460 }
461
Winson Chung785d2eb2011-04-14 16:08:02 -0700462 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700463 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700464 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700465
466 // Display an error message if the drag failed due to there not being enough space on the
467 // target layout we were dropping on.
468 if (!success) {
469 boolean showOutOfSpaceMessage = false;
470 if (target instanceof Workspace) {
471 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
472 Workspace workspace = (Workspace) target;
473 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700474 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700475 if (layout != null) {
476 layout.calculateSpans(itemInfo);
477 showOutOfSpaceMessage =
478 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
479 }
480 }
481 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
482 if (showOutOfSpaceMessage) {
483 mLauncher.showOutOfSpaceMessage();
484 }
485 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700486 }
487
488 public void setContentType(ContentType type) {
489 mContentType = type;
Winson Chung5a808352011-06-27 19:08:49 -0700490 invalidatePageData(0);
Winson Chung785d2eb2011-04-14 16:08:02 -0700491 }
492
Winson Chungb44b5242011-06-13 11:32:14 -0700493 public boolean isContentType(ContentType type) {
494 return (mContentType == type);
495 }
496
Winson Chung5a808352011-06-27 19:08:49 -0700497 public void setCurrentPageToWidgets() {
498 invalidatePageData(0);
499 }
Winson Chung5a808352011-06-27 19:08:49 -0700500
Winson Chung785d2eb2011-04-14 16:08:02 -0700501 /*
502 * Apps PagedView implementation
503 */
Winson Chung63257c12011-05-05 17:06:13 -0700504 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
505 int childCount = layout.getChildCount();
506 for (int i = 0; i < childCount; ++i) {
507 layout.getChildAt(i).setVisibility(visibility);
508 }
509 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700510 private void setupPage(PagedViewCellLayout layout) {
511 layout.setCellCount(mCellCountX, mCellCountY);
512 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
513 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
514 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
515
Winson Chung63257c12011-05-05 17:06:13 -0700516 // Note: We force a measure here to get around the fact that when we do layout calculations
517 // immediately after syncing, we don't have a proper width. That said, we already know the
518 // expected page width, so we can actually optimize by hiding all the TextView-based
519 // children that are expensive to measure, and let that happen naturally later.
520 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700521 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700522 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700523 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700524 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700525 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700526 }
527 public void syncAppsPages() {
528 // Ensure that we have the right number of pages
529 Context context = getContext();
530 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
531 for (int i = 0; i < numPages; ++i) {
532 PagedViewCellLayout layout = new PagedViewCellLayout(context);
533 setupPage(layout);
534 addView(layout);
535 }
536 }
537 public void syncAppsPageItems(int page) {
538 // ensure that we have the right number of items on the pages
539 int numPages = getPageCount();
540 int numCells = mCellCountX * mCellCountY;
541 int startIndex = page * numCells;
542 int endIndex = Math.min(startIndex + numCells, mApps.size());
543 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700544
Winson Chung785d2eb2011-04-14 16:08:02 -0700545 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700546 ArrayList<Object> items = new ArrayList<Object>();
547 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700548 for (int i = startIndex; i < endIndex; ++i) {
549 ApplicationInfo info = mApps.get(i);
550 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
551 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700552 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700553 icon.setOnClickListener(this);
554 icon.setOnLongClickListener(this);
555 icon.setOnTouchListener(this);
556
557 int index = i - startIndex;
558 int x = index % mCellCountX;
559 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700560 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700561
562 items.add(info);
563 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700564 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700565
566 // Create the hardware layers
567 layout.allowHardwareLayerCreation();
568 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700569
570 prepareGenerateHoloOutlinesTask(page, items, images);
Winson Chung785d2eb2011-04-14 16:08:02 -0700571 }
Winson Chungb44b5242011-06-13 11:32:14 -0700572
573 /**
574 * Return the appropriate thread priority for loading for a given page (we give the current
575 * page much higher priority)
576 */
577 private int getThreadPriorityForPage(int page) {
578 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
579 int pageDiff = Math.abs(page - mCurrentPage);
580 if (pageDiff <= 0) {
581 // return Process.THREAD_PRIORITY_DEFAULT;
582 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
583 } else if (pageDiff <= 1) {
584 // return Process.THREAD_PRIORITY_BACKGROUND;
585 return Process.THREAD_PRIORITY_DEFAULT;
586 } else {
587 // return Process.THREAD_PRIORITY_LOWEST;
588 return Process.THREAD_PRIORITY_DEFAULT;
589 }
590 }
591 /**
592 * Creates and executes a new AsyncTask to load a page of widget previews.
593 */
594 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700595 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700596 // Prune all tasks that are no longer needed
597 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
598 while (iter.hasNext()) {
599 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
600 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700601 if ((taskPage == page) ||
602 taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
Winson Chung4e076542011-06-23 13:04:10 -0700603 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700604 task.cancel(false);
605 iter.remove();
606 } else {
607 task.setThreadPriority(getThreadPriorityForPage(taskPage));
608 }
609 }
610
611 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700612 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700613 @Override
614 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
615 // Ensure that this task starts running at the correct priority
616 task.syncThreadPriority();
617
618 // Load each of the widget/shortcut previews
619 ArrayList<Object> items = data.items;
Winson Chung4e076542011-06-23 13:04:10 -0700620 ArrayList<Bitmap> images = data.generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700621 int count = items.size();
622 int cellWidth = data.cellWidth;
623 int cellHeight = data.cellHeight;
624 for (int i = 0; i < count && !task.isCancelled(); ++i) {
625 // Before work on each item, ensure that this task is running at the correct
626 // priority
627 task.syncThreadPriority();
628
629 Object rawInfo = items.get(i);
630 if (rawInfo instanceof AppWidgetProviderInfo) {
631 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
632 int[] cellSpans = CellLayout.rectToCell(getResources(),
633 info.minWidth, info.minHeight, null);
Winson Chung4e076542011-06-23 13:04:10 -0700634 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
Winson Chungb44b5242011-06-13 11:32:14 -0700635 cellWidth, cellHeight));
636 } else if (rawInfo instanceof ResolveInfo) {
637 // Fill in the shortcuts information
638 ResolveInfo info = (ResolveInfo) rawInfo;
Winson Chung4e076542011-06-23 13:04:10 -0700639 images.add(getShortcutPreview(info, cellWidth, cellHeight));
Winson Chungb44b5242011-06-13 11:32:14 -0700640 }
641 }
642 }
643 },
644 new AsyncTaskCallback() {
645 @Override
646 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
647 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700648 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700649 if (task.page > getPageCount()) return;
650 if (task.pageContentType != mContentType) return;
651 onSyncWidgetPageItems(data);
652 }
653 });
654
655 // Ensure that the task is appropriately prioritized and runs in parallel
Winson Chung875de7e2011-06-28 14:25:17 -0700656 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType,
657 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700658 t.setThreadPriority(getThreadPriorityForPage(page));
659 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
660 mRunningTasks.add(t);
661 }
662 /**
663 * Creates and executes a new AsyncTask to load the outlines for a page of content.
664 */
665 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
666 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700667 // Prune old tasks for this page
668 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
669 while (iter.hasNext()) {
670 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
671 int taskPage = task.page;
672 if ((taskPage == page) &&
673 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
674 task.cancel(false);
675 iter.remove();
676 }
677 }
678
Winson Chungb44b5242011-06-13 11:32:14 -0700679 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
680 new AsyncTaskCallback() {
681 @Override
682 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
683 // Ensure that this task starts running at the correct priority
684 task.syncThreadPriority();
685
Winson Chung4e076542011-06-23 13:04:10 -0700686 ArrayList<Bitmap> images = data.generatedImages;
687 ArrayList<Bitmap> srcImages = data.sourceImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700688 int count = srcImages.size();
689 Canvas c = new Canvas();
690 for (int i = 0; i < count && !task.isCancelled(); ++i) {
691 // Before work on each item, ensure that this task is running at the correct
692 // priority
693 task.syncThreadPriority();
694
695 Bitmap b = srcImages.get(i);
696 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
697 Bitmap.Config.ARGB_8888);
698
699 c.setBitmap(outline);
700 c.save();
701 c.drawBitmap(b, 0, 0, null);
702 c.restore();
Adam Cohenaaf473c2011-08-03 12:02:47 -0700703 c.setBitmap(null);
Winson Chungb44b5242011-06-13 11:32:14 -0700704
705 images.add(outline);
706 }
707 }
708 },
709 new AsyncTaskCallback() {
710 @Override
711 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
712 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700713 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700714 if (task.page > getPageCount()) return;
715 if (task.pageContentType != mContentType) return;
716 onHolographicPageItemsLoaded(data);
717 }
718 });
719
720 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700721 AppsCustomizeAsyncTask t =
Winson Chung875de7e2011-06-28 14:25:17 -0700722 new AppsCustomizeAsyncTask(page, mContentType,
723 AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700724 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
725 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
726 mRunningTasks.add(t);
727 }
728
Winson Chung785d2eb2011-04-14 16:08:02 -0700729 /*
730 * Widgets PagedView implementation
731 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700732 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700733 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
734 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700735
736 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700737 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700738 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
739 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700740 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700741 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700742 }
Michael Jurkabe69cc82011-07-07 16:05:33 -0700743 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung785d2eb2011-04-14 16:08:02 -0700744 float scaleX, float scaleY) {
Winson Chung201bc822011-06-20 15:41:53 -0700745 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700746 Canvas c = new Canvas(bitmap);
Winson Chung201bc822011-06-20 15:41:53 -0700747 c.scale(scaleX, scaleY);
748 Rect oldBounds = d.copyBounds();
749 d.setBounds(x, y, x + w, y + h);
750 d.draw(c);
751 d.setBounds(oldBounds); // Restore the bounds
Adam Cohenaaf473c2011-08-03 12:02:47 -0700752 c.setBitmap(null);
Winson Chung201bc822011-06-20 15:41:53 -0700753 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700754 }
Winson Chungd2945262011-06-24 15:22:14 -0700755 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700756 // Render the icon
Winson Chungd2945262011-06-24 15:22:14 -0700757 Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700758 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chungd2945262011-06-24 15:22:14 -0700759 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chungb44b5242011-06-13 11:32:14 -0700760 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700761 }
Winson Chung4e076542011-06-23 13:04:10 -0700762 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700763 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700764
Winson Chung4b576be2011-04-27 17:40:20 -0700765 // Calculate the size of the drawable
766 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
767 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
768 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
769 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
770
771 // Scale down the bitmap to fit the space
772 float widgetPreviewScale = (float) cellWidth / expectedWidth;
773 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
774 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
775
776 // Load the preview image if possible
777 String packageName = info.provider.getPackageName();
778 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700779 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700780 if (info.previewImage != 0) {
781 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
782 if (drawable == null) {
783 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
784 + " for provider: " + info.provider);
785 } else {
786 // Scale down the preview to the dimensions we want
787 int imageWidth = drawable.getIntrinsicWidth();
788 int imageHeight = drawable.getIntrinsicHeight();
789 float aspect = (float) imageWidth / imageHeight;
790 int newWidth = imageWidth;
791 int newHeight = imageHeight;
792 if (aspect > 1f) {
793 newWidth = expectedWidth;
794 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
795 } else {
796 newHeight = expectedHeight;
797 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
798 }
799
Winson Chungb44b5242011-06-13 11:32:14 -0700800 preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
Winson Chung4b576be2011-04-27 17:40:20 -0700801 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700802 }
803 }
804
805 // Generate a preview image if we couldn't load one
806 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700807 Resources resources = mLauncher.getResources();
Winson Chung273c1022011-07-11 13:40:52 -0700808 int bitmapWidth;
809 int bitmapHeight;
Winson Chung1ed747a2011-05-03 16:18:34 -0700810
Winson Chung273c1022011-07-11 13:40:52 -0700811 // Specify the dimensions of the bitmap (since we are using a default preview bg with
812 // the full icon, we only imply the aspect ratio of the widget)
813 if (cellHSpan == cellVSpan) {
814 bitmapWidth = bitmapHeight = cellWidth;
815 expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension;
816 } else if (cellHSpan >= cellVSpan) {
817 bitmapWidth = expectedWidth = cellWidth;
818 bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension;
Winson Chung1ed747a2011-05-03 16:18:34 -0700819 } else {
820 // Note that in vertical widgets, we might not have enough space due to the text
821 // label, so be conservative and use the width as a height bound
Winson Chung273c1022011-07-11 13:40:52 -0700822 bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension;
823 bitmapHeight = expectedHeight = cellWidth;
Winson Chung1ed747a2011-05-03 16:18:34 -0700824 }
Winson Chung4b576be2011-04-27 17:40:20 -0700825
Winson Chung273c1022011-07-11 13:40:52 -0700826 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700827 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
828 expectedHeight, 1f,1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700829
830 // Draw the icon in the top left corner
831 try {
832 Drawable icon = null;
833 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
834 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
835
Winson Chungd2945262011-06-24 15:22:14 -0700836 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
837 renderDrawableToBitmap(icon, preview, offset, offset,
838 mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700839 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -0700840 }
Winson Chungb44b5242011-06-13 11:32:14 -0700841 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -0700842 }
843 public void syncWidgetPages() {
844 // Ensure that we have the right number of pages
845 Context context = getContext();
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700846 int numPages = (int) Math.ceil(mWidgets.size() /
847 (float) (mWidgetCountX * mWidgetCountY));
848 for (int j = 0; j < numPages; ++j) {
849 PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX,
850 mWidgetCountY);
851 setupPage(layout);
852 addView(layout);
Winson Chung785d2eb2011-04-14 16:08:02 -0700853 }
854 }
855 public void syncWidgetPageItems(int page) {
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700856 int numItemsPerPage = mWidgetCountX * mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700857 int contentWidth = mWidgetSpacingLayout.getContentWidth();
858 int contentHeight = mWidgetSpacingLayout.getContentHeight();
Winson Chungb44b5242011-06-13 11:32:14 -0700859
Winson Chungd2945262011-06-24 15:22:14 -0700860 // Calculate the dimensions of each cell we are giving to each widget
Winson Chungd2945262011-06-24 15:22:14 -0700861 ArrayList<Object> items = new ArrayList<Object>();
862 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700863 - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX);
Winson Chungd2945262011-06-24 15:22:14 -0700864 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700865 - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY);
Winson Chungd2945262011-06-24 15:22:14 -0700866
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700867 int offset = page * numItemsPerPage;
868 for (int i = offset; i < Math.min(offset + numItemsPerPage, mWidgets.size()); ++i) {
869 items.add(mWidgets.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700870 }
871
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700872 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, mWidgetCountX);
Winson Chungb44b5242011-06-13 11:32:14 -0700873 }
874 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
875 int page = data.page;
Winson Chung4e6a9762011-05-09 11:56:34 -0700876 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page);
Winson Chungd52f3d82011-07-12 14:29:11 -0700877 // Only set the column count once we have items
878 layout.setColumnCount(layout.getCellCountX());
Winson Chung785d2eb2011-04-14 16:08:02 -0700879
Winson Chungb44b5242011-06-13 11:32:14 -0700880 ArrayList<Object> items = data.items;
881 int count = items.size();
882 int cellWidth = data.cellWidth;
883 int cellHeight = data.cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700884 int cellCountX = data.cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700885 for (int i = 0; i < count; ++i) {
886 Object rawInfo = items.get(i);
Winson Chung1ed747a2011-05-03 16:18:34 -0700887 PendingAddItemInfo createItemInfo = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700888 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
889 R.layout.apps_customize_widget, layout, false);
Winson Chung1ed747a2011-05-03 16:18:34 -0700890 if (rawInfo instanceof AppWidgetProviderInfo) {
891 // Fill in the widget information
892 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
893 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chungb44b5242011-06-13 11:32:14 -0700894 int[] cellSpans = CellLayout.rectToCell(getResources(),
895 info.minWidth, info.minHeight, null);
Winson Chung4e076542011-06-23 13:04:10 -0700896 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chung9f4e0fd2011-06-02 18:59:36 -0700897 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans,
Winson Chungb44b5242011-06-13 11:32:14 -0700898 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -0700899 widget.setTag(createItemInfo);
900 } else if (rawInfo instanceof ResolveInfo) {
901 // Fill in the shortcuts information
902 ResolveInfo info = (ResolveInfo) rawInfo;
903 createItemInfo = new PendingAddItemInfo();
904 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
905 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
906 info.activityInfo.name);
Winson Chung4e076542011-06-23 13:04:10 -0700907 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700908 widget.applyFromResolveInfo(mPackageManager, info, preview,
909 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -0700910 widget.setTag(createItemInfo);
911 }
Winson Chung4b576be2011-04-27 17:40:20 -0700912 widget.setOnClickListener(this);
913 widget.setOnLongClickListener(this);
914 widget.setOnTouchListener(this);
915
916 // Layout each widget
Winson Chungd2945262011-06-24 15:22:14 -0700917 int ix = i % cellCountX;
918 int iy = i / cellCountX;
Winson Chungfd3385f2011-06-15 19:51:24 -0700919 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Winson Chung72d8b392011-07-29 13:56:44 -0700920 GridLayout.spec(iy, GridLayout.LEFT),
921 GridLayout.spec(ix, GridLayout.TOP));
Winson Chungfd3385f2011-06-15 19:51:24 -0700922 lp.width = cellWidth;
923 lp.height = cellHeight;
Winson Chung72d8b392011-07-29 13:56:44 -0700924 lp.setGravity(Gravity.TOP | Gravity.LEFT);
Winson Chungfd3385f2011-06-15 19:51:24 -0700925 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
926 if (iy > 0) lp.topMargin = mWidgetHeightGap;
Winson Chung4e6a9762011-05-09 11:56:34 -0700927 layout.addView(widget, lp);
Winson Chung785d2eb2011-04-14 16:08:02 -0700928 }
Winson Chungb44b5242011-06-13 11:32:14 -0700929
930 invalidate();
931 forceUpdateAdjacentPagesAlpha();
Winson Chung4e076542011-06-23 13:04:10 -0700932 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
Winson Chungb44b5242011-06-13 11:32:14 -0700933 }
934 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
935 // Invalidate early to short-circuit children invalidates
936 invalidate();
937
938 int page = data.page;
939 ViewGroup layout = (ViewGroup) getChildAt(page);
940 if (layout instanceof PagedViewCellLayout) {
941 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
942 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -0700943 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700944 for (int i = 0; i < count; ++i) {
945 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -0700946 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700947 }
948 } else {
949 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -0700950 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700951 for (int i = 0; i < count; ++i) {
952 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -0700953 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700954 }
955 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700956 }
Winson Chung46af2e82011-05-09 16:00:53 -0700957
Winson Chung785d2eb2011-04-14 16:08:02 -0700958 @Override
959 public void syncPages() {
960 removeAllViews();
Winson Chung875de7e2011-06-28 14:25:17 -0700961
962 // Remove all background asyc tasks if we are loading content anew
963 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
964 while (iter.hasNext()) {
965 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
966 task.cancel(false);
967 iter.remove();
968 }
969
Winson Chung785d2eb2011-04-14 16:08:02 -0700970 switch (mContentType) {
971 case Applications:
972 syncAppsPages();
973 break;
974 case Widgets:
975 syncWidgetPages();
976 break;
977 }
978 }
979 @Override
980 public void syncPageItems(int page) {
981 switch (mContentType) {
982 case Applications:
983 syncAppsPageItems(page);
984 break;
985 case Widgets:
986 syncWidgetPageItems(page);
987 break;
988 }
989 }
990
991 /**
992 * Used by the parent to get the content width to set the tab bar to
993 * @return
994 */
995 public int getPageContentWidth() {
996 return mContentWidth;
997 }
998
Winson Chungb26f3d62011-06-02 10:49:29 -0700999 @Override
1000 protected void onPageBeginMoving() {
1001 /* TO BE ENABLED LATER
1002 setChildrenDrawnWithCacheEnabled(true);
1003 for (int i = 0; i < getChildCount(); ++i) {
1004 View v = getChildAt(i);
1005 if (v instanceof PagedViewCellLayout) {
1006 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true);
1007 }
1008 }
1009 */
1010 super.onPageBeginMoving();
1011 }
1012
1013 @Override
1014 protected void onPageEndMoving() {
1015 /* TO BE ENABLED LATER
1016 for (int i = 0; i < getChildCount(); ++i) {
1017 View v = getChildAt(i);
1018 if (v instanceof PagedViewCellLayout) {
1019 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false);
1020 }
1021 }
1022 setChildrenDrawnWithCacheEnabled(false);
1023 */
1024 super.onPageEndMoving();
1025 }
1026
Winson Chung785d2eb2011-04-14 16:08:02 -07001027 /*
1028 * AllAppsView implementation
1029 */
1030 @Override
1031 public void setup(Launcher launcher, DragController dragController) {
1032 mLauncher = launcher;
1033 mDragController = dragController;
1034 }
1035 @Override
1036 public void zoom(float zoom, boolean animate) {
1037 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1038 }
1039 @Override
1040 public boolean isVisible() {
1041 return (getVisibility() == VISIBLE);
1042 }
1043 @Override
1044 public boolean isAnimating() {
1045 return false;
1046 }
1047 @Override
1048 public void setApps(ArrayList<ApplicationInfo> list) {
1049 mApps = list;
1050 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001051
Winson Chung875de7e2011-06-28 14:25:17 -07001052 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1053 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001054 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001055 }
1056 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1057 // We add it in place, in alphabetical order
1058 int count = list.size();
1059 for (int i = 0; i < count; ++i) {
1060 ApplicationInfo info = list.get(i);
1061 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1062 if (index < 0) {
1063 mApps.add(-(index + 1), info);
1064 }
1065 }
1066 }
1067 @Override
1068 public void addApps(ArrayList<ApplicationInfo> list) {
1069 addAppsWithoutInvalidate(list);
1070 invalidatePageData();
1071 }
1072 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1073 ComponentName removeComponent = item.intent.getComponent();
1074 int length = list.size();
1075 for (int i = 0; i < length; ++i) {
1076 ApplicationInfo info = list.get(i);
1077 if (info.intent.getComponent().equals(removeComponent)) {
1078 return i;
1079 }
1080 }
1081 return -1;
1082 }
1083 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1084 // loop through all the apps and remove apps that have the same component
1085 int length = list.size();
1086 for (int i = 0; i < length; ++i) {
1087 ApplicationInfo info = list.get(i);
1088 int removeIndex = findAppByComponent(mApps, info);
1089 if (removeIndex > -1) {
1090 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001091 }
1092 }
1093 }
1094 @Override
1095 public void removeApps(ArrayList<ApplicationInfo> list) {
1096 removeAppsWithoutInvalidate(list);
1097 invalidatePageData();
1098 }
1099 @Override
1100 public void updateApps(ArrayList<ApplicationInfo> list) {
1101 // We remove and re-add the updated applications list because it's properties may have
1102 // changed (ie. the title), and this will ensure that the items will be in their proper
1103 // place in the list.
1104 removeAppsWithoutInvalidate(list);
1105 addAppsWithoutInvalidate(list);
1106 invalidatePageData();
1107 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001108
Winson Chung785d2eb2011-04-14 16:08:02 -07001109 @Override
1110 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001111 if (mContentType != ContentType.Applications) {
1112 // Reset to the first page of the Apps pane
1113 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1114 mLauncher.findViewById(R.id.apps_customize_pane);
Winson Chung5a808352011-06-27 19:08:49 -07001115 tabs.selectAppsTab();
Michael Jurka35aa14d2011-07-07 17:01:08 -07001116 } else if (getCurrentPage() != 0) {
Winson Chung5a808352011-06-27 19:08:49 -07001117 invalidatePageData(0);
Winson Chungfc79c802011-05-02 13:35:34 -07001118 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001119 }
1120 @Override
1121 public void dumpState() {
1122 // TODO: Dump information related to current list of Applications, Widgets, etc.
1123 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1124 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
1125 }
1126 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001127 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001128 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001129 for (Object i: list) {
1130 if (i instanceof AppWidgetProviderInfo) {
1131 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1132 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1133 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1134 + " initialLayout=" + info.initialLayout
1135 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1136 } else if (i instanceof ResolveInfo) {
1137 ResolveInfo info = (ResolveInfo) i;
1138 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1139 + info.icon);
1140 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001141 }
1142 }
1143 @Override
1144 public void surrender() {
1145 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1146 // should stop this now.
1147 }
Winson Chung007c6982011-06-14 13:27:53 -07001148
Winson Chungb44b5242011-06-13 11:32:14 -07001149 /*
1150 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1151 * widget previews in the background with the AsyncTasks.
1152 */
1153 protected int getAssociatedLowerPageBound(int page) {
1154 return Math.max(0, page - 2);
1155 }
1156 protected int getAssociatedUpperPageBound(int page) {
1157 final int count = getChildCount();
1158 return Math.min(page + 2, count - 1);
1159 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001160
1161 @Override
1162 protected String getCurrentPageDescription() {
1163 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1164 int stringId = R.string.default_scroll_format;
1165 switch (mContentType) {
1166 case Applications:
1167 stringId = R.string.apps_customize_apps_scroll_format;
1168 break;
1169 case Widgets:
1170 stringId = R.string.apps_customize_widgets_scroll_format;
1171 break;
1172 }
1173 return String.format(mContext.getString(stringId), page + 1, getChildCount());
1174 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001175}