blob: cb60034ac7d6fd63c9f72a35cf688a12560853d5 [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;
170 private ArrayList<Object> mShortcuts;
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 Chung785d2eb2011-04-14 16:08:02 -0700176
177 // Dimens
178 private int mContentWidth;
Winson Chungd2945262011-06-24 15:22:14 -0700179 private int mAppIconSize;
Winson Chung4b576be2011-04-27 17:40:20 -0700180 private int mMaxWidgetSpan, mMinWidgetSpan;
Winson Chung4b576be2011-04-27 17:40:20 -0700181 private int mWidgetCountX, mWidgetCountY;
Winson Chungd2945262011-06-24 15:22:14 -0700182 private int mWidgetWidthGap, mWidgetHeightGap;
183 private int mShortcutCountX, mShortcutCountY;
184 private int mShortcutWidthGap, mShortcutHeightGap;
Winson Chung5a808352011-06-27 19:08:49 -0700185 private int mNumWidgetPages, mNumShortcutPages;
Winson Chung1ed747a2011-05-03 16:18:34 -0700186 private final int mWidgetPreviewIconPaddedDimension;
187 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700188 private PagedViewCellLayout mWidgetSpacingLayout;
189
Winson Chungb44b5242011-06-13 11:32:14 -0700190 // Previews & outlines
191 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
192 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chung4b576be2011-04-27 17:40:20 -0700193
Winson Chung785d2eb2011-04-14 16:08:02 -0700194 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
195 super(context, attrs);
196 mLayoutInflater = LayoutInflater.from(context);
197 mPackageManager = context.getPackageManager();
198 mContentType = ContentType.Applications;
199 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700200 mWidgets = new ArrayList<Object>();
Winson Chungd2945262011-06-24 15:22:14 -0700201 mShortcuts = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700202 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700203 mHolographicOutlineHelper = new HolographicOutlineHelper();
204 mCanvas = new Canvas();
205 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700206
207 // Save the default widget preview background
208 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700209 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chungd2945262011-06-24 15:22:14 -0700210 mAppIconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700211
212 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700213 // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after
Winson Chung785d2eb2011-04-14 16:08:02 -0700214 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
215 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
216 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700217 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700218 mWidgetWidthGap =
219 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
220 mWidgetHeightGap =
221 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700222 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
223 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
224 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700225 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700226
227 // The max widget span is the length N, such that NxN is the largest bounds that the widget
228 // preview can be before applying the widget scaling
229 mMinWidgetSpan = 1;
230 mMaxWidgetSpan = 3;
Winson Chung1ed747a2011-05-03 16:18:34 -0700231
232 // The padding on the non-matched dimension for the default widget preview icons
233 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700234 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700235 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung785d2eb2011-04-14 16:08:02 -0700236 }
237
238 @Override
239 protected void init() {
240 super.init();
241 mCenterPagesVertically = false;
242
243 Context context = getContext();
244 Resources r = context.getResources();
245 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
246 }
247
Winson Chungf0ea4d32011-06-06 14:27:16 -0700248 @Override
Winson Chungde1af762011-07-21 16:44:07 -0700249 protected void onWallpaperTap(MotionEvent ev) {
250 int action = ev.getAction();
251 if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
252 // Dismiss AppsCustomize if we tap
253 mLauncher.showWorkspace(true);
254 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700255 }
256
257 /**
258 * This differs from isDataReady as this is the test done if isDataReady is not set.
259 */
260 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700261 // We only do this test once, and we default to the Applications page, so we only really
262 // have to wait for there to be apps.
263 return !mApps.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700264 }
265
266 protected void onDataReady(int width, int height) {
267 // Note that we transpose the counts in portrait so that we get a similar layout
268 boolean isLandscape = getResources().getConfiguration().orientation ==
269 Configuration.ORIENTATION_LANDSCAPE;
270 int maxCellCountX = Integer.MAX_VALUE;
271 int maxCellCountY = Integer.MAX_VALUE;
272 if (LauncherApplication.isScreenLarge()) {
273 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
274 LauncherModel.getCellCountY());
275 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
276 LauncherModel.getCellCountX());
277 }
278
279 // Now that the data is ready, we can calculate the content width, the number of cells to
280 // use for each page
281 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
282 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
283 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
284 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
285 mCellCountX = mWidgetSpacingLayout.getCellCountX();
286 mCellCountY = mWidgetSpacingLayout.getCellCountY();
287 mWidgetCountX = Math.max(1, (int) Math.round(mCellCountX / 2f));
288 mWidgetCountY = Math.max(1, (int) Math.round(mCellCountY / 3f));
Winson Chungd2945262011-06-24 15:22:14 -0700289 mShortcutCountX = Math.max(1, (int) Math.round(mCellCountX / 2f));
290 mShortcutCountY = Math.max(1, (int) Math.round(mCellCountY / 2f));
291
Winson Chung5a808352011-06-27 19:08:49 -0700292 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
293 (float) (mWidgetCountX * mWidgetCountY));
294 mNumShortcutPages = (int) Math.ceil(mShortcuts.size() /
295 (float) (mShortcutCountX * mShortcutCountY));
296
Winson Chungdb1138b2011-06-30 14:39:35 -0700297 // Force a measure to update recalculate the gaps
298 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
299 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
300 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700301 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700302 invalidatePageData();
303 }
304
305 @Override
306 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
307 int width = MeasureSpec.getSize(widthMeasureSpec);
308 int height = MeasureSpec.getSize(heightMeasureSpec);
309 if (!isDataReady()) {
310 if (testDataReady()) {
311 setDataIsReady();
312 setMeasuredDimension(width, height);
313 onDataReady(width, height);
314 }
315 }
316
317 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
318 }
319
Winson Chung34efdaf2011-05-24 14:19:56 -0700320 /** Removes and returns the ResolveInfo with the specified ComponentName */
321 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
322 ComponentName cn) {
323 Iterator<ResolveInfo> iter = list.iterator();
324 while (iter.hasNext()) {
325 ResolveInfo rinfo = iter.next();
326 ActivityInfo info = rinfo.activityInfo;
327 ComponentName c = new ComponentName(info.packageName, info.name);
328 if (c.equals(cn)) {
329 iter.remove();
330 return rinfo;
331 }
332 }
333 return null;
334 }
335
Winson Chung785d2eb2011-04-14 16:08:02 -0700336 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700337 // Get the list of widgets and shortcuts
Winson Chung875de7e2011-06-28 14:25:17 -0700338 boolean wasEmpty = mWidgets.isEmpty() && mShortcuts.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700339 mWidgets.clear();
Winson Chungd2945262011-06-24 15:22:14 -0700340 mShortcuts.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700341 List<AppWidgetProviderInfo> widgets =
342 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
343 Collections.sort(widgets,
Winson Chung1ed747a2011-05-03 16:18:34 -0700344 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700345 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
346 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
347 Collections.sort(shortcuts,
348 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
349 mWidgets.addAll(widgets);
Winson Chungd2945262011-06-24 15:22:14 -0700350 mShortcuts.addAll(shortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -0700351
Winson Chung875de7e2011-06-28 14:25:17 -0700352 if (wasEmpty) {
353 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
354 // a layout to do this test and invalidate the page data when ready.
355 if (testDataReady()) requestLayout();
356 } else {
357 invalidatePageData();
358 }
Winson Chung4b576be2011-04-27 17:40:20 -0700359 }
360
361 @Override
362 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700363 // When we have exited all apps or are in transition, disregard clicks
364 if (!mLauncher.isAllAppsCustomizeOpen() ||
365 mLauncher.getWorkspace().isSwitchingState()) return;
366
Winson Chung4b576be2011-04-27 17:40:20 -0700367 if (v instanceof PagedViewIcon) {
368 // Animate some feedback to the click
369 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
370 animateClickFeedback(v, new Runnable() {
371 @Override
372 public void run() {
373 mLauncher.startActivitySafely(appInfo.intent, appInfo);
374 }
375 });
376 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700377 // Let the user know that they have to long press to add a widget
378 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
379 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700380
Winson Chungd2e87b32011-06-02 10:53:07 -0700381 // Create a little animation to show that the widget can move
382 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
383 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
384 AnimatorSet bounce = new AnimatorSet();
385 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
386 tyuAnim.setDuration(125);
387 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
388 tydAnim.setDuration(100);
389 bounce.play(tyuAnim).before(tydAnim);
390 bounce.setInterpolator(new AccelerateInterpolator());
391 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700392 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700393 }
394
395 /*
396 * PagedViewWithDraggableItems implementation
397 */
398 @Override
399 protected void determineDraggingStart(android.view.MotionEvent ev) {
400 // Disable dragging by pulling an app down for now.
401 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700402
Winson Chung4b576be2011-04-27 17:40:20 -0700403 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700404 mLauncher.getWorkspace().onDragStartedWithItem(v);
405 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700406 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700407
Winson Chung4b576be2011-04-27 17:40:20 -0700408 private void beginDraggingWidget(View v) {
409 // Get the widget preview as the drag representation
410 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700411 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700412
413 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700414 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700415 Drawable preview = image.getDrawable();
416 int w = preview.getIntrinsicWidth();
417 int h = preview.getIntrinsicHeight();
Winson Chung1ed747a2011-05-03 16:18:34 -0700418 if (createItemInfo instanceof PendingAddWidgetInfo) {
419 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
420 int[] spanXY = CellLayout.rectToCell(getResources(),
421 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
422 createItemInfo.spanX = spanXY[0];
423 createItemInfo.spanY = spanXY[1];
424
425 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
426 renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1);
427 } else {
428 // Workaround for the fact that we don't keep the original ResolveInfo associated with
429 // the shortcut around. To get the icon, we just render the preview image (which has
430 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
431 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
432 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700433 mCanvas.setBitmap(b);
434 mCanvas.save();
435 preview.draw(mCanvas);
436 mCanvas.restore();
Winson Chung1ed747a2011-05-03 16:18:34 -0700437 createItemInfo.spanX = createItemInfo.spanY = 1;
438 }
Winson Chung4b576be2011-04-27 17:40:20 -0700439
440 // Start the drag
Winson Chung4b576be2011-04-27 17:40:20 -0700441 mLauncher.lockScreenOrientation();
Winson Chung1ed747a2011-05-03 16:18:34 -0700442 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
443 createItemInfo.spanY, b);
444 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700445 DragController.DRAG_ACTION_COPY, null);
446 b.recycle();
447 }
448 @Override
449 protected boolean beginDragging(View v) {
450 if (!super.beginDragging(v)) return false;
451
Winson Chungc07918d2011-07-01 15:35:26 -0700452 // Go into spring loaded mode (must happen before we startDrag())
453 int currentPageIndex = mLauncher.getWorkspace().getCurrentPage();
454 CellLayout currentPage = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPageIndex);
455 mLauncher.enterSpringLoadedDragMode(currentPage);
Winson Chungfc79c802011-05-02 13:35:34 -0700456
Winson Chung4b576be2011-04-27 17:40:20 -0700457 if (v instanceof PagedViewIcon) {
458 beginDraggingApplication(v);
459 } else if (v instanceof PagedViewWidget) {
460 beginDraggingWidget(v);
461 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700462 return true;
463 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700464 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700465 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700466 if (!success || (target != mLauncher.getWorkspace() &&
467 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700468 // Exit spring loaded mode if we have not successfully dropped or have not handled the
469 // drop in Workspace
470 mLauncher.exitSpringLoadedDragMode();
471 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700472 mLauncher.unlockScreenOrientation();
Winson Chungb26f3d62011-06-02 10:49:29 -0700473
Winson Chung785d2eb2011-04-14 16:08:02 -0700474 }
475
Winson Chung785d2eb2011-04-14 16:08:02 -0700476 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700477 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700478 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700479
480 // Display an error message if the drag failed due to there not being enough space on the
481 // target layout we were dropping on.
482 if (!success) {
483 boolean showOutOfSpaceMessage = false;
484 if (target instanceof Workspace) {
485 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
486 Workspace workspace = (Workspace) target;
487 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700488 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700489 if (layout != null) {
490 layout.calculateSpans(itemInfo);
491 showOutOfSpaceMessage =
492 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
493 }
494 }
495 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
496 if (showOutOfSpaceMessage) {
497 mLauncher.showOutOfSpaceMessage();
498 }
499 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700500 }
501
502 public void setContentType(ContentType type) {
503 mContentType = type;
Winson Chung5a808352011-06-27 19:08:49 -0700504 invalidatePageData(0);
Winson Chung785d2eb2011-04-14 16:08:02 -0700505 }
506
Winson Chungb44b5242011-06-13 11:32:14 -0700507 public boolean isContentType(ContentType type) {
508 return (mContentType == type);
509 }
510
Winson Chung5a808352011-06-27 19:08:49 -0700511 public void setCurrentPageToWidgets() {
512 invalidatePageData(0);
513 }
514 public void setCurrentPageToShortcuts() {
515 invalidatePageData(mNumWidgetPages);
516 }
517
Winson Chung785d2eb2011-04-14 16:08:02 -0700518 /*
519 * Apps PagedView implementation
520 */
Winson Chung63257c12011-05-05 17:06:13 -0700521 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
522 int childCount = layout.getChildCount();
523 for (int i = 0; i < childCount; ++i) {
524 layout.getChildAt(i).setVisibility(visibility);
525 }
526 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700527 private void setupPage(PagedViewCellLayout layout) {
528 layout.setCellCount(mCellCountX, mCellCountY);
529 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
530 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
531 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
532
Winson Chung63257c12011-05-05 17:06:13 -0700533 // Note: We force a measure here to get around the fact that when we do layout calculations
534 // immediately after syncing, we don't have a proper width. That said, we already know the
535 // expected page width, so we can actually optimize by hiding all the TextView-based
536 // children that are expensive to measure, and let that happen naturally later.
537 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700538 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700539 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700540 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700541 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700542 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700543 }
544 public void syncAppsPages() {
545 // Ensure that we have the right number of pages
546 Context context = getContext();
547 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
548 for (int i = 0; i < numPages; ++i) {
549 PagedViewCellLayout layout = new PagedViewCellLayout(context);
550 setupPage(layout);
551 addView(layout);
552 }
553 }
554 public void syncAppsPageItems(int page) {
555 // ensure that we have the right number of items on the pages
556 int numPages = getPageCount();
557 int numCells = mCellCountX * mCellCountY;
558 int startIndex = page * numCells;
559 int endIndex = Math.min(startIndex + numCells, mApps.size());
560 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700561
Winson Chung785d2eb2011-04-14 16:08:02 -0700562 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700563 ArrayList<Object> items = new ArrayList<Object>();
564 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700565 for (int i = startIndex; i < endIndex; ++i) {
566 ApplicationInfo info = mApps.get(i);
567 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
568 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700569 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700570 icon.setOnClickListener(this);
571 icon.setOnLongClickListener(this);
572 icon.setOnTouchListener(this);
573
574 int index = i - startIndex;
575 int x = index % mCellCountX;
576 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700577 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700578
579 items.add(info);
580 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700581 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700582
583 // Create the hardware layers
584 layout.allowHardwareLayerCreation();
585 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700586
587 prepareGenerateHoloOutlinesTask(page, items, images);
Winson Chung785d2eb2011-04-14 16:08:02 -0700588 }
Winson Chungb44b5242011-06-13 11:32:14 -0700589
590 /**
591 * Return the appropriate thread priority for loading for a given page (we give the current
592 * page much higher priority)
593 */
594 private int getThreadPriorityForPage(int page) {
595 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
596 int pageDiff = Math.abs(page - mCurrentPage);
597 if (pageDiff <= 0) {
598 // return Process.THREAD_PRIORITY_DEFAULT;
599 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
600 } else if (pageDiff <= 1) {
601 // return Process.THREAD_PRIORITY_BACKGROUND;
602 return Process.THREAD_PRIORITY_DEFAULT;
603 } else {
604 // return Process.THREAD_PRIORITY_LOWEST;
605 return Process.THREAD_PRIORITY_DEFAULT;
606 }
607 }
608 /**
609 * Creates and executes a new AsyncTask to load a page of widget previews.
610 */
611 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700612 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700613 // Prune all tasks that are no longer needed
614 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
615 while (iter.hasNext()) {
616 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
617 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700618 if ((taskPage == page) ||
619 taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
Winson Chung4e076542011-06-23 13:04:10 -0700620 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700621 task.cancel(false);
622 iter.remove();
623 } else {
624 task.setThreadPriority(getThreadPriorityForPage(taskPage));
625 }
626 }
627
628 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700629 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700630 @Override
631 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
632 // Ensure that this task starts running at the correct priority
633 task.syncThreadPriority();
634
635 // Load each of the widget/shortcut previews
636 ArrayList<Object> items = data.items;
Winson Chung4e076542011-06-23 13:04:10 -0700637 ArrayList<Bitmap> images = data.generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700638 int count = items.size();
639 int cellWidth = data.cellWidth;
640 int cellHeight = data.cellHeight;
641 for (int i = 0; i < count && !task.isCancelled(); ++i) {
642 // Before work on each item, ensure that this task is running at the correct
643 // priority
644 task.syncThreadPriority();
645
646 Object rawInfo = items.get(i);
647 if (rawInfo instanceof AppWidgetProviderInfo) {
648 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
649 int[] cellSpans = CellLayout.rectToCell(getResources(),
650 info.minWidth, info.minHeight, null);
Winson Chung4e076542011-06-23 13:04:10 -0700651 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
Winson Chungb44b5242011-06-13 11:32:14 -0700652 cellWidth, cellHeight));
653 } else if (rawInfo instanceof ResolveInfo) {
654 // Fill in the shortcuts information
655 ResolveInfo info = (ResolveInfo) rawInfo;
Winson Chung4e076542011-06-23 13:04:10 -0700656 images.add(getShortcutPreview(info, cellWidth, cellHeight));
Winson Chungb44b5242011-06-13 11:32:14 -0700657 }
658 }
659 }
660 },
661 new AsyncTaskCallback() {
662 @Override
663 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
664 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700665 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700666 if (task.page > getPageCount()) return;
667 if (task.pageContentType != mContentType) return;
668 onSyncWidgetPageItems(data);
669 }
670 });
671
672 // Ensure that the task is appropriately prioritized and runs in parallel
Winson Chung875de7e2011-06-28 14:25:17 -0700673 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType,
674 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700675 t.setThreadPriority(getThreadPriorityForPage(page));
676 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
677 mRunningTasks.add(t);
678 }
679 /**
680 * Creates and executes a new AsyncTask to load the outlines for a page of content.
681 */
682 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
683 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700684 // Prune old tasks for this page
685 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
686 while (iter.hasNext()) {
687 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
688 int taskPage = task.page;
689 if ((taskPage == page) &&
690 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
691 task.cancel(false);
692 iter.remove();
693 }
694 }
695
Winson Chungb44b5242011-06-13 11:32:14 -0700696 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
697 new AsyncTaskCallback() {
698 @Override
699 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
700 // Ensure that this task starts running at the correct priority
701 task.syncThreadPriority();
702
Winson Chung4e076542011-06-23 13:04:10 -0700703 ArrayList<Bitmap> images = data.generatedImages;
704 ArrayList<Bitmap> srcImages = data.sourceImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700705 int count = srcImages.size();
706 Canvas c = new Canvas();
707 for (int i = 0; i < count && !task.isCancelled(); ++i) {
708 // Before work on each item, ensure that this task is running at the correct
709 // priority
710 task.syncThreadPriority();
711
712 Bitmap b = srcImages.get(i);
713 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
714 Bitmap.Config.ARGB_8888);
715
716 c.setBitmap(outline);
717 c.save();
718 c.drawBitmap(b, 0, 0, null);
719 c.restore();
720
721 images.add(outline);
722 }
723 }
724 },
725 new AsyncTaskCallback() {
726 @Override
727 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
728 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700729 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700730 if (task.page > getPageCount()) return;
731 if (task.pageContentType != mContentType) return;
732 onHolographicPageItemsLoaded(data);
733 }
734 });
735
736 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700737 AppsCustomizeAsyncTask t =
Winson Chung875de7e2011-06-28 14:25:17 -0700738 new AppsCustomizeAsyncTask(page, mContentType,
739 AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700740 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
741 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
742 mRunningTasks.add(t);
743 }
744
Winson Chung785d2eb2011-04-14 16:08:02 -0700745 /*
746 * Widgets PagedView implementation
747 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700748 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700749 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
750 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700751
752 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700753 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700754 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
755 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700756 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700757 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700758 }
Michael Jurkabe69cc82011-07-07 16:05:33 -0700759 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung785d2eb2011-04-14 16:08:02 -0700760 float scaleX, float scaleY) {
Winson Chung201bc822011-06-20 15:41:53 -0700761 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700762 Canvas c = new Canvas(bitmap);
Winson Chung201bc822011-06-20 15:41:53 -0700763 c.scale(scaleX, scaleY);
764 Rect oldBounds = d.copyBounds();
765 d.setBounds(x, y, x + w, y + h);
766 d.draw(c);
767 d.setBounds(oldBounds); // Restore the bounds
Winson Chung201bc822011-06-20 15:41:53 -0700768 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700769 }
Winson Chungd2945262011-06-24 15:22:14 -0700770 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700771 // Render the icon
Winson Chungd2945262011-06-24 15:22:14 -0700772 Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700773 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chungd2945262011-06-24 15:22:14 -0700774 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chungb44b5242011-06-13 11:32:14 -0700775 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700776 }
Winson Chung4e076542011-06-23 13:04:10 -0700777 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700778 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700779
Winson Chung4b576be2011-04-27 17:40:20 -0700780 // Calculate the size of the drawable
781 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
782 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
783 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
784 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
785
786 // Scale down the bitmap to fit the space
787 float widgetPreviewScale = (float) cellWidth / expectedWidth;
788 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
789 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
790
791 // Load the preview image if possible
792 String packageName = info.provider.getPackageName();
793 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700794 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700795 if (info.previewImage != 0) {
796 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
797 if (drawable == null) {
798 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
799 + " for provider: " + info.provider);
800 } else {
801 // Scale down the preview to the dimensions we want
802 int imageWidth = drawable.getIntrinsicWidth();
803 int imageHeight = drawable.getIntrinsicHeight();
804 float aspect = (float) imageWidth / imageHeight;
805 int newWidth = imageWidth;
806 int newHeight = imageHeight;
807 if (aspect > 1f) {
808 newWidth = expectedWidth;
809 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
810 } else {
811 newHeight = expectedHeight;
812 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
813 }
814
Winson Chungb44b5242011-06-13 11:32:14 -0700815 preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
Winson Chung4b576be2011-04-27 17:40:20 -0700816 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700817 }
818 }
819
820 // Generate a preview image if we couldn't load one
821 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700822 Resources resources = mLauncher.getResources();
Winson Chung273c1022011-07-11 13:40:52 -0700823 int bitmapWidth;
824 int bitmapHeight;
Winson Chung1ed747a2011-05-03 16:18:34 -0700825
Winson Chung273c1022011-07-11 13:40:52 -0700826 // Specify the dimensions of the bitmap (since we are using a default preview bg with
827 // the full icon, we only imply the aspect ratio of the widget)
828 if (cellHSpan == cellVSpan) {
829 bitmapWidth = bitmapHeight = cellWidth;
830 expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension;
831 } else if (cellHSpan >= cellVSpan) {
832 bitmapWidth = expectedWidth = cellWidth;
833 bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension;
Winson Chung1ed747a2011-05-03 16:18:34 -0700834 } else {
835 // Note that in vertical widgets, we might not have enough space due to the text
836 // label, so be conservative and use the width as a height bound
Winson Chung273c1022011-07-11 13:40:52 -0700837 bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension;
838 bitmapHeight = expectedHeight = cellWidth;
Winson Chung1ed747a2011-05-03 16:18:34 -0700839 }
Winson Chung4b576be2011-04-27 17:40:20 -0700840
Winson Chung273c1022011-07-11 13:40:52 -0700841 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700842 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
843 expectedHeight, 1f,1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700844
845 // Draw the icon in the top left corner
846 try {
847 Drawable icon = null;
848 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
849 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
850
Winson Chungd2945262011-06-24 15:22:14 -0700851 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
852 renderDrawableToBitmap(icon, preview, offset, offset,
853 mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700854 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -0700855 }
Winson Chungb44b5242011-06-13 11:32:14 -0700856 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -0700857 }
858 public void syncWidgetPages() {
859 // Ensure that we have the right number of pages
860 Context context = getContext();
Winson Chungd2945262011-06-24 15:22:14 -0700861 int[] countX = { mWidgetCountX, mShortcutCountX };
862 int[] countY = { mWidgetCountY, mShortcutCountY };
Winson Chung5a808352011-06-27 19:08:49 -0700863 int[] numPages = { mNumWidgetPages, mNumShortcutPages };
Winson Chungd2945262011-06-24 15:22:14 -0700864 for (int i = 0; i < 2; ++i) {
Winson Chung5a808352011-06-27 19:08:49 -0700865 for (int j = 0; j < numPages[i]; ++j) {
866 PagedViewGridLayout layout = new PagedViewGridLayout(context, countX[i], countY[i]);
Winson Chungd2945262011-06-24 15:22:14 -0700867 setupPage(layout);
868 addView(layout);
869 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700870 }
871 }
872 public void syncWidgetPageItems(int page) {
Winson Chungd2945262011-06-24 15:22:14 -0700873 int[] countX = { mWidgetCountX, mShortcutCountX };
874 int[] countY = { mWidgetCountY, mShortcutCountY };
875 int[] widthGap = { mWidgetWidthGap, mWidgetWidthGap };
876 int[] heightGap = { mWidgetHeightGap, mWidgetHeightGap };
877 int[] numItemsPerPage = { mWidgetCountX * mWidgetCountY,
878 mShortcutCountX * mShortcutCountY };
879 Object[] collection = { mWidgets, mShortcuts };
880 int contentWidth = mWidgetSpacingLayout.getContentWidth();
881 int contentHeight = mWidgetSpacingLayout.getContentHeight();
882 int numWidgetPages = (int) Math.ceil(mWidgets.size() / (float) numItemsPerPage[0]);
883 int[] offsets = { page * numItemsPerPage[0], (page - numWidgetPages) * numItemsPerPage[1] };
884 int index = (page < numWidgetPages ? 0 : 1);
Winson Chungb44b5242011-06-13 11:32:14 -0700885
Winson Chungd2945262011-06-24 15:22:14 -0700886 // Calculate the dimensions of each cell we are giving to each widget
887 ArrayList<Object> list = (ArrayList<Object>) collection[index];
888 ArrayList<Object> items = new ArrayList<Object>();
889 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
890 - ((countX[index] - 1) * widthGap[index])) / countX[index]);
891 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
892 - ((countY[index] - 1) * heightGap[index])) / countY[index]);
893
894 int offset = offsets[index];
895 for (int i = offset; i < Math.min(offset + numItemsPerPage[index], list.size()); ++i) {
896 items.add(list.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700897 }
898
Winson Chungd2945262011-06-24 15:22:14 -0700899 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, countX[index]);
Winson Chungb44b5242011-06-13 11:32:14 -0700900 }
901 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
902 int page = data.page;
Winson Chung4e6a9762011-05-09 11:56:34 -0700903 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page);
Winson Chungd52f3d82011-07-12 14:29:11 -0700904 // Only set the column count once we have items
905 layout.setColumnCount(layout.getCellCountX());
Winson Chung785d2eb2011-04-14 16:08:02 -0700906
Winson Chungb44b5242011-06-13 11:32:14 -0700907 ArrayList<Object> items = data.items;
908 int count = items.size();
909 int cellWidth = data.cellWidth;
910 int cellHeight = data.cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700911 int cellCountX = data.cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700912 for (int i = 0; i < count; ++i) {
913 Object rawInfo = items.get(i);
Winson Chung1ed747a2011-05-03 16:18:34 -0700914 PendingAddItemInfo createItemInfo = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700915 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
916 R.layout.apps_customize_widget, layout, false);
Winson Chung1ed747a2011-05-03 16:18:34 -0700917 if (rawInfo instanceof AppWidgetProviderInfo) {
918 // Fill in the widget information
919 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
920 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chungb44b5242011-06-13 11:32:14 -0700921 int[] cellSpans = CellLayout.rectToCell(getResources(),
922 info.minWidth, info.minHeight, null);
Winson Chung4e076542011-06-23 13:04:10 -0700923 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chung9f4e0fd2011-06-02 18:59:36 -0700924 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans,
Winson Chungb44b5242011-06-13 11:32:14 -0700925 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -0700926 widget.setTag(createItemInfo);
927 } else if (rawInfo instanceof ResolveInfo) {
928 // Fill in the shortcuts information
929 ResolveInfo info = (ResolveInfo) rawInfo;
930 createItemInfo = new PendingAddItemInfo();
931 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
932 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
933 info.activityInfo.name);
Winson Chung4e076542011-06-23 13:04:10 -0700934 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700935 widget.applyFromResolveInfo(mPackageManager, info, preview,
936 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -0700937 widget.setTag(createItemInfo);
938 }
Winson Chung4b576be2011-04-27 17:40:20 -0700939 widget.setOnClickListener(this);
940 widget.setOnLongClickListener(this);
941 widget.setOnTouchListener(this);
942
943 // Layout each widget
Winson Chungd2945262011-06-24 15:22:14 -0700944 int ix = i % cellCountX;
945 int iy = i / cellCountX;
Winson Chungfd3385f2011-06-15 19:51:24 -0700946 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Winson Chung72d8b392011-07-29 13:56:44 -0700947 GridLayout.spec(iy, GridLayout.LEFT),
948 GridLayout.spec(ix, GridLayout.TOP));
Winson Chungfd3385f2011-06-15 19:51:24 -0700949 lp.width = cellWidth;
950 lp.height = cellHeight;
Winson Chung72d8b392011-07-29 13:56:44 -0700951 lp.setGravity(Gravity.TOP | Gravity.LEFT);
Winson Chungfd3385f2011-06-15 19:51:24 -0700952 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
953 if (iy > 0) lp.topMargin = mWidgetHeightGap;
Winson Chung4e6a9762011-05-09 11:56:34 -0700954 layout.addView(widget, lp);
Winson Chung785d2eb2011-04-14 16:08:02 -0700955 }
Winson Chungb44b5242011-06-13 11:32:14 -0700956
957 invalidate();
958 forceUpdateAdjacentPagesAlpha();
Winson Chung4e076542011-06-23 13:04:10 -0700959 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
Winson Chungb44b5242011-06-13 11:32:14 -0700960 }
961 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
962 // Invalidate early to short-circuit children invalidates
963 invalidate();
964
965 int page = data.page;
966 ViewGroup layout = (ViewGroup) getChildAt(page);
967 if (layout instanceof PagedViewCellLayout) {
968 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
969 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -0700970 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700971 for (int i = 0; i < count; ++i) {
972 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -0700973 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700974 }
975 } else {
976 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -0700977 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700978 for (int i = 0; i < count; ++i) {
979 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -0700980 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700981 }
982 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700983 }
Winson Chung46af2e82011-05-09 16:00:53 -0700984
Winson Chung785d2eb2011-04-14 16:08:02 -0700985 @Override
986 public void syncPages() {
987 removeAllViews();
Winson Chung875de7e2011-06-28 14:25:17 -0700988
989 // Remove all background asyc tasks if we are loading content anew
990 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
991 while (iter.hasNext()) {
992 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
993 task.cancel(false);
994 iter.remove();
995 }
996
Winson Chung785d2eb2011-04-14 16:08:02 -0700997 switch (mContentType) {
998 case Applications:
999 syncAppsPages();
1000 break;
1001 case Widgets:
1002 syncWidgetPages();
1003 break;
1004 }
1005 }
1006 @Override
1007 public void syncPageItems(int page) {
1008 switch (mContentType) {
1009 case Applications:
1010 syncAppsPageItems(page);
1011 break;
1012 case Widgets:
1013 syncWidgetPageItems(page);
1014 break;
1015 }
1016 }
1017
1018 /**
1019 * Used by the parent to get the content width to set the tab bar to
1020 * @return
1021 */
1022 public int getPageContentWidth() {
1023 return mContentWidth;
1024 }
1025
Winson Chungb26f3d62011-06-02 10:49:29 -07001026 @Override
1027 protected void onPageBeginMoving() {
1028 /* TO BE ENABLED LATER
1029 setChildrenDrawnWithCacheEnabled(true);
1030 for (int i = 0; i < getChildCount(); ++i) {
1031 View v = getChildAt(i);
1032 if (v instanceof PagedViewCellLayout) {
1033 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true);
1034 }
1035 }
1036 */
1037 super.onPageBeginMoving();
1038 }
1039
1040 @Override
1041 protected void onPageEndMoving() {
1042 /* TO BE ENABLED LATER
1043 for (int i = 0; i < getChildCount(); ++i) {
1044 View v = getChildAt(i);
1045 if (v instanceof PagedViewCellLayout) {
1046 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false);
1047 }
1048 }
1049 setChildrenDrawnWithCacheEnabled(false);
1050 */
1051 super.onPageEndMoving();
1052 }
1053
Winson Chung785d2eb2011-04-14 16:08:02 -07001054 /*
1055 * AllAppsView implementation
1056 */
1057 @Override
1058 public void setup(Launcher launcher, DragController dragController) {
1059 mLauncher = launcher;
1060 mDragController = dragController;
1061 }
1062 @Override
1063 public void zoom(float zoom, boolean animate) {
1064 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1065 }
1066 @Override
1067 public boolean isVisible() {
1068 return (getVisibility() == VISIBLE);
1069 }
1070 @Override
1071 public boolean isAnimating() {
1072 return false;
1073 }
1074 @Override
1075 public void setApps(ArrayList<ApplicationInfo> list) {
1076 mApps = list;
1077 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001078
Winson Chung875de7e2011-06-28 14:25:17 -07001079 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1080 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001081 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001082 }
1083 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1084 // We add it in place, in alphabetical order
1085 int count = list.size();
1086 for (int i = 0; i < count; ++i) {
1087 ApplicationInfo info = list.get(i);
1088 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1089 if (index < 0) {
1090 mApps.add(-(index + 1), info);
1091 }
1092 }
1093 }
1094 @Override
1095 public void addApps(ArrayList<ApplicationInfo> list) {
1096 addAppsWithoutInvalidate(list);
1097 invalidatePageData();
1098 }
1099 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1100 ComponentName removeComponent = item.intent.getComponent();
1101 int length = list.size();
1102 for (int i = 0; i < length; ++i) {
1103 ApplicationInfo info = list.get(i);
1104 if (info.intent.getComponent().equals(removeComponent)) {
1105 return i;
1106 }
1107 }
1108 return -1;
1109 }
1110 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1111 // loop through all the apps and remove apps that have the same component
1112 int length = list.size();
1113 for (int i = 0; i < length; ++i) {
1114 ApplicationInfo info = list.get(i);
1115 int removeIndex = findAppByComponent(mApps, info);
1116 if (removeIndex > -1) {
1117 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001118 }
1119 }
1120 }
1121 @Override
1122 public void removeApps(ArrayList<ApplicationInfo> list) {
1123 removeAppsWithoutInvalidate(list);
1124 invalidatePageData();
1125 }
1126 @Override
1127 public void updateApps(ArrayList<ApplicationInfo> list) {
1128 // We remove and re-add the updated applications list because it's properties may have
1129 // changed (ie. the title), and this will ensure that the items will be in their proper
1130 // place in the list.
1131 removeAppsWithoutInvalidate(list);
1132 addAppsWithoutInvalidate(list);
1133 invalidatePageData();
1134 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001135
Winson Chung785d2eb2011-04-14 16:08:02 -07001136 @Override
1137 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001138 if (mContentType != ContentType.Applications) {
1139 // Reset to the first page of the Apps pane
1140 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1141 mLauncher.findViewById(R.id.apps_customize_pane);
Winson Chung5a808352011-06-27 19:08:49 -07001142 tabs.selectAppsTab();
Michael Jurka35aa14d2011-07-07 17:01:08 -07001143 } else if (getCurrentPage() != 0) {
Winson Chung5a808352011-06-27 19:08:49 -07001144 invalidatePageData(0);
Winson Chungfc79c802011-05-02 13:35:34 -07001145 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001146 }
1147 @Override
1148 public void dumpState() {
1149 // TODO: Dump information related to current list of Applications, Widgets, etc.
1150 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1151 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
Winson Chungd2945262011-06-24 15:22:14 -07001152 dumpAppWidgetProviderInfoList(LOG_TAG, "mShortcuts", mShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07001153 }
1154 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001155 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001156 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001157 for (Object i: list) {
1158 if (i instanceof AppWidgetProviderInfo) {
1159 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1160 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1161 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1162 + " initialLayout=" + info.initialLayout
1163 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1164 } else if (i instanceof ResolveInfo) {
1165 ResolveInfo info = (ResolveInfo) i;
1166 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1167 + info.icon);
1168 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001169 }
1170 }
1171 @Override
1172 public void surrender() {
1173 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1174 // should stop this now.
1175 }
Winson Chung007c6982011-06-14 13:27:53 -07001176
Winson Chungb44b5242011-06-13 11:32:14 -07001177 /*
1178 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1179 * widget previews in the background with the AsyncTasks.
1180 */
1181 protected int getAssociatedLowerPageBound(int page) {
1182 return Math.max(0, page - 2);
1183 }
1184 protected int getAssociatedUpperPageBound(int page) {
1185 final int count = getChildCount();
1186 return Math.min(page + 2, count - 1);
1187 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001188
1189 @Override
1190 protected String getCurrentPageDescription() {
1191 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1192 int stringId = R.string.default_scroll_format;
1193 switch (mContentType) {
1194 case Applications:
1195 stringId = R.string.apps_customize_apps_scroll_format;
1196 break;
1197 case Widgets:
1198 stringId = R.string.apps_customize_widgets_scroll_format;
1199 break;
1200 }
1201 return String.format(mContext.getString(stringId), page + 1, getChildCount());
1202 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001203}