blob: d07b321df6f2074007b451641b0a371969646c73 [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 Chung785d2eb2011-04-14 16:08:02 -070042import android.view.LayoutInflater;
Winson Chungde1af762011-07-21 16:44:07 -070043import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070044import android.view.View;
Winson Chung63257c12011-05-05 17:06:13 -070045import android.view.ViewGroup;
Winson Chung55b65502011-05-26 12:03:43 -070046import android.view.animation.AccelerateInterpolator;
Winson Chungfd3385f2011-06-15 19:51:24 -070047import android.widget.GridLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070048import android.widget.ImageView;
Winson Chung55b65502011-05-26 12:03:43 -070049import android.widget.Toast;
Winson Chung785d2eb2011-04-14 16:08:02 -070050
51import com.android.launcher.R;
Adam Cohenc0dcf592011-06-01 15:30:43 -070052import com.android.launcher2.DropTarget.DragObject;
53
54import java.util.ArrayList;
55import java.util.Collections;
56import java.util.Iterator;
57import java.util.List;
Winson Chung785d2eb2011-04-14 16:08:02 -070058
Winson Chungb44b5242011-06-13 11:32:14 -070059/**
60 * A simple callback interface which also provides the results of the task.
61 */
62interface AsyncTaskCallback {
63 void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data);
64}
Winson Chung4e076542011-06-23 13:04:10 -070065
Winson Chungb44b5242011-06-13 11:32:14 -070066/**
67 * The data needed to perform either of the custom AsyncTasks.
68 */
69class AsyncTaskPageData {
Winson Chung875de7e2011-06-28 14:25:17 -070070 enum Type {
71 LoadWidgetPreviewData,
72 LoadHolographicIconsData
73 }
74
Winson Chungb44b5242011-06-13 11:32:14 -070075 AsyncTaskPageData(int p, ArrayList<Object> l, ArrayList<Bitmap> si, AsyncTaskCallback bgR,
76 AsyncTaskCallback postR) {
77 page = p;
78 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070079 sourceImages = si;
80 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070081 cellWidth = cellHeight = -1;
82 doInBackgroundCallback = bgR;
83 postExecuteCallback = postR;
84 }
Winson Chungd2945262011-06-24 15:22:14 -070085 AsyncTaskPageData(int p, ArrayList<Object> l, int cw, int ch, int ccx, AsyncTaskCallback bgR,
Winson Chungb44b5242011-06-13 11:32:14 -070086 AsyncTaskCallback postR) {
87 page = p;
88 items = l;
Winson Chung4e076542011-06-23 13:04:10 -070089 generatedImages = new ArrayList<Bitmap>();
Winson Chungb44b5242011-06-13 11:32:14 -070090 cellWidth = cw;
91 cellHeight = ch;
Winson Chungd2945262011-06-24 15:22:14 -070092 cellCountX = ccx;
Winson Chungb44b5242011-06-13 11:32:14 -070093 doInBackgroundCallback = bgR;
94 postExecuteCallback = postR;
95 }
96 int page;
97 ArrayList<Object> items;
Winson Chung4e076542011-06-23 13:04:10 -070098 ArrayList<Bitmap> sourceImages;
99 ArrayList<Bitmap> generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700100 int cellWidth;
101 int cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700102 int cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700103 AsyncTaskCallback doInBackgroundCallback;
104 AsyncTaskCallback postExecuteCallback;
105}
Winson Chung4e076542011-06-23 13:04:10 -0700106
Winson Chungb44b5242011-06-13 11:32:14 -0700107/**
108 * A generic template for an async task used in AppsCustomize.
109 */
110class AppsCustomizeAsyncTask extends AsyncTask<AsyncTaskPageData, Void, AsyncTaskPageData> {
Winson Chung875de7e2011-06-28 14:25:17 -0700111 AppsCustomizeAsyncTask(int p, AppsCustomizePagedView.ContentType t, AsyncTaskPageData.Type ty) {
Winson Chungb44b5242011-06-13 11:32:14 -0700112 page = p;
113 pageContentType = t;
114 threadPriority = Process.THREAD_PRIORITY_DEFAULT;
Winson Chung875de7e2011-06-28 14:25:17 -0700115 dataType = ty;
Winson Chungb44b5242011-06-13 11:32:14 -0700116 }
117 @Override
118 protected AsyncTaskPageData doInBackground(AsyncTaskPageData... params) {
119 if (params.length != 1) return null;
120 // Load each of the widget previews in the background
121 params[0].doInBackgroundCallback.run(this, params[0]);
122 return params[0];
123 }
124 @Override
125 protected void onPostExecute(AsyncTaskPageData result) {
126 // All the widget previews are loaded, so we can just callback to inflate the page
127 result.postExecuteCallback.run(this, result);
128 }
129
130 void setThreadPriority(int p) {
131 threadPriority = p;
132 }
133 void syncThreadPriority() {
134 Process.setThreadPriority(threadPriority);
135 }
136
137 // The page that this async task is associated with
Winson Chung875de7e2011-06-28 14:25:17 -0700138 AsyncTaskPageData.Type dataType;
Winson Chungb44b5242011-06-13 11:32:14 -0700139 int page;
140 AppsCustomizePagedView.ContentType pageContentType;
141 int threadPriority;
142}
Winson Chungb44b5242011-06-13 11:32:14 -0700143
144/**
145 * The Apps/Customize page that displays all the applications, widgets, and shortcuts.
146 */
Winson Chung785d2eb2011-04-14 16:08:02 -0700147public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements
148 AllAppsView, View.OnClickListener, DragSource {
149 static final String LOG_TAG = "AppsCustomizePagedView";
150
151 /**
152 * The different content types that this paged view can show.
153 */
154 public enum ContentType {
155 Applications,
Winson Chung6a26e5b2011-05-26 14:36:06 -0700156 Widgets
Winson Chung785d2eb2011-04-14 16:08:02 -0700157 }
158
159 // Refs
160 private Launcher mLauncher;
161 private DragController mDragController;
162 private final LayoutInflater mLayoutInflater;
163 private final PackageManager mPackageManager;
164
165 // Content
166 private ContentType mContentType;
167 private ArrayList<ApplicationInfo> mApps;
Winson Chungd2945262011-06-24 15:22:14 -0700168 private ArrayList<Object> mWidgets;
169 private ArrayList<Object> mShortcuts;
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;
182 private int mShortcutCountX, mShortcutCountY;
183 private int mShortcutWidthGap, mShortcutHeightGap;
Winson Chung5a808352011-06-27 19:08:49 -0700184 private int mNumWidgetPages, mNumShortcutPages;
Winson Chung1ed747a2011-05-03 16:18:34 -0700185 private final int mWidgetPreviewIconPaddedDimension;
186 private final float sWidgetPreviewIconPaddingPercentage = 0.25f;
Winson Chung785d2eb2011-04-14 16:08:02 -0700187 private PagedViewCellLayout mWidgetSpacingLayout;
188
Winson Chungb44b5242011-06-13 11:32:14 -0700189 // Previews & outlines
190 ArrayList<AppsCustomizeAsyncTask> mRunningTasks;
191 private HolographicOutlineHelper mHolographicOutlineHelper;
Winson Chung4b576be2011-04-27 17:40:20 -0700192
Winson Chung785d2eb2011-04-14 16:08:02 -0700193 public AppsCustomizePagedView(Context context, AttributeSet attrs) {
194 super(context, attrs);
195 mLayoutInflater = LayoutInflater.from(context);
196 mPackageManager = context.getPackageManager();
197 mContentType = ContentType.Applications;
198 mApps = new ArrayList<ApplicationInfo>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700199 mWidgets = new ArrayList<Object>();
Winson Chungd2945262011-06-24 15:22:14 -0700200 mShortcuts = new ArrayList<Object>();
Winson Chung4dbea792011-05-05 14:21:32 -0700201 mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache();
Winson Chungb44b5242011-06-13 11:32:14 -0700202 mHolographicOutlineHelper = new HolographicOutlineHelper();
203 mCanvas = new Canvas();
204 mRunningTasks = new ArrayList<AppsCustomizeAsyncTask>();
Winson Chung1ed747a2011-05-03 16:18:34 -0700205
206 // Save the default widget preview background
207 Resources resources = context.getResources();
Winson Chung967289b2011-06-30 18:09:30 -0700208 mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview_holo);
Winson Chungd2945262011-06-24 15:22:14 -0700209 mAppIconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung785d2eb2011-04-14 16:08:02 -0700210
211 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700212 // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after
Winson Chung785d2eb2011-04-14 16:08:02 -0700213 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
214 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
215 a.recycle();
Winson Chung4b576be2011-04-27 17:40:20 -0700216 a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700217 mWidgetWidthGap =
218 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0);
219 mWidgetHeightGap =
220 a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0);
Winson Chung4b576be2011-04-27 17:40:20 -0700221 mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2);
222 mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2);
223 a.recycle();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700224 mWidgetSpacingLayout = new PagedViewCellLayout(getContext());
Winson Chung4b576be2011-04-27 17:40:20 -0700225
226 // The max widget span is the length N, such that NxN is the largest bounds that the widget
227 // preview can be before applying the widget scaling
228 mMinWidgetSpan = 1;
229 mMaxWidgetSpan = 3;
Winson Chung1ed747a2011-05-03 16:18:34 -0700230
231 // The padding on the non-matched dimension for the default widget preview icons
232 // (top + bottom)
Winson Chung1ed747a2011-05-03 16:18:34 -0700233 mWidgetPreviewIconPaddedDimension =
Winson Chungd2945262011-06-24 15:22:14 -0700234 (int) (mAppIconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage)));
Winson Chung785d2eb2011-04-14 16:08:02 -0700235 }
236
237 @Override
238 protected void init() {
239 super.init();
240 mCenterPagesVertically = false;
241
242 Context context = getContext();
243 Resources r = context.getResources();
244 setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f);
245 }
246
Winson Chungf0ea4d32011-06-06 14:27:16 -0700247 @Override
Winson Chungde1af762011-07-21 16:44:07 -0700248 protected void onWallpaperTap(MotionEvent ev) {
249 int action = ev.getAction();
250 if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
251 // Dismiss AppsCustomize if we tap
252 mLauncher.showWorkspace(true);
253 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700254 }
255
256 /**
257 * This differs from isDataReady as this is the test done if isDataReady is not set.
258 */
259 private boolean testDataReady() {
Winson Chungfd3385f2011-06-15 19:51:24 -0700260 // We only do this test once, and we default to the Applications page, so we only really
261 // have to wait for there to be apps.
262 return !mApps.isEmpty();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700263 }
264
265 protected void onDataReady(int width, int height) {
266 // Note that we transpose the counts in portrait so that we get a similar layout
267 boolean isLandscape = getResources().getConfiguration().orientation ==
268 Configuration.ORIENTATION_LANDSCAPE;
269 int maxCellCountX = Integer.MAX_VALUE;
270 int maxCellCountY = Integer.MAX_VALUE;
271 if (LauncherApplication.isScreenLarge()) {
272 maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() :
273 LauncherModel.getCellCountY());
274 maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() :
275 LauncherModel.getCellCountX());
276 }
277
278 // Now that the data is ready, we can calculate the content width, the number of cells to
279 // use for each page
280 mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
281 mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
282 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
283 mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY);
284 mCellCountX = mWidgetSpacingLayout.getCellCountX();
285 mCellCountY = mWidgetSpacingLayout.getCellCountY();
286 mWidgetCountX = Math.max(1, (int) Math.round(mCellCountX / 2f));
287 mWidgetCountY = Math.max(1, (int) Math.round(mCellCountY / 3f));
Winson Chungd2945262011-06-24 15:22:14 -0700288 mShortcutCountX = Math.max(1, (int) Math.round(mCellCountX / 2f));
289 mShortcutCountY = Math.max(1, (int) Math.round(mCellCountY / 2f));
290
Winson Chung5a808352011-06-27 19:08:49 -0700291 mNumWidgetPages = (int) Math.ceil(mWidgets.size() /
292 (float) (mWidgetCountX * mWidgetCountY));
293 mNumShortcutPages = (int) Math.ceil(mShortcuts.size() /
294 (float) (mShortcutCountX * mShortcutCountY));
295
Winson Chungdb1138b2011-06-30 14:39:35 -0700296 // Force a measure to update recalculate the gaps
297 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
298 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
299 mWidgetSpacingLayout.measure(widthSpec, heightSpec);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700300 mContentWidth = mWidgetSpacingLayout.getContentWidth();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700301 invalidatePageData();
302 }
303
304 @Override
305 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
306 int width = MeasureSpec.getSize(widthMeasureSpec);
307 int height = MeasureSpec.getSize(heightMeasureSpec);
308 if (!isDataReady()) {
309 if (testDataReady()) {
310 setDataIsReady();
311 setMeasuredDimension(width, height);
312 onDataReady(width, height);
313 }
314 }
315
316 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
317 }
318
Winson Chung34efdaf2011-05-24 14:19:56 -0700319 /** Removes and returns the ResolveInfo with the specified ComponentName */
320 private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list,
321 ComponentName cn) {
322 Iterator<ResolveInfo> iter = list.iterator();
323 while (iter.hasNext()) {
324 ResolveInfo rinfo = iter.next();
325 ActivityInfo info = rinfo.activityInfo;
326 ComponentName c = new ComponentName(info.packageName, info.name);
327 if (c.equals(cn)) {
328 iter.remove();
329 return rinfo;
330 }
331 }
332 return null;
333 }
334
Winson Chung785d2eb2011-04-14 16:08:02 -0700335 public void onPackagesUpdated() {
Winson Chung1ed747a2011-05-03 16:18:34 -0700336 // Get the list of widgets and shortcuts
Winson Chung875de7e2011-06-28 14:25:17 -0700337 boolean wasEmpty = mWidgets.isEmpty() && mShortcuts.isEmpty();
Winson Chung1ed747a2011-05-03 16:18:34 -0700338 mWidgets.clear();
Winson Chungd2945262011-06-24 15:22:14 -0700339 mShortcuts.clear();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700340 List<AppWidgetProviderInfo> widgets =
341 AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
342 Collections.sort(widgets,
Winson Chung1ed747a2011-05-03 16:18:34 -0700343 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700344 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
345 List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
346 Collections.sort(shortcuts,
347 new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager));
348 mWidgets.addAll(widgets);
Winson Chungd2945262011-06-24 15:22:14 -0700349 mShortcuts.addAll(shortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -0700350
Winson Chung875de7e2011-06-28 14:25:17 -0700351 if (wasEmpty) {
352 // The next layout pass will trigger data-ready if both widgets and apps are set, so request
353 // a layout to do this test and invalidate the page data when ready.
354 if (testDataReady()) requestLayout();
355 } else {
356 invalidatePageData();
357 }
Winson Chung4b576be2011-04-27 17:40:20 -0700358 }
359
360 @Override
361 public void onClick(View v) {
Adam Cohenfc53cd22011-07-20 15:45:11 -0700362 // When we have exited all apps or are in transition, disregard clicks
363 if (!mLauncher.isAllAppsCustomizeOpen() ||
364 mLauncher.getWorkspace().isSwitchingState()) return;
365
Winson Chung4b576be2011-04-27 17:40:20 -0700366 if (v instanceof PagedViewIcon) {
367 // Animate some feedback to the click
368 final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
369 animateClickFeedback(v, new Runnable() {
370 @Override
371 public void run() {
372 mLauncher.startActivitySafely(appInfo.intent, appInfo);
373 }
374 });
375 } else if (v instanceof PagedViewWidget) {
Winson Chungd2e87b32011-06-02 10:53:07 -0700376 // Let the user know that they have to long press to add a widget
377 Toast.makeText(getContext(), R.string.long_press_widget_to_add,
378 Toast.LENGTH_SHORT).show();
Winson Chung46af2e82011-05-09 16:00:53 -0700379
Winson Chungd2e87b32011-06-02 10:53:07 -0700380 // Create a little animation to show that the widget can move
381 float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
382 final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
383 AnimatorSet bounce = new AnimatorSet();
384 ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY);
385 tyuAnim.setDuration(125);
386 ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f);
387 tydAnim.setDuration(100);
388 bounce.play(tyuAnim).before(tydAnim);
389 bounce.setInterpolator(new AccelerateInterpolator());
390 bounce.start();
Winson Chung4b576be2011-04-27 17:40:20 -0700391 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700392 }
393
394 /*
395 * PagedViewWithDraggableItems implementation
396 */
397 @Override
398 protected void determineDraggingStart(android.view.MotionEvent ev) {
399 // Disable dragging by pulling an app down for now.
400 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700401
Winson Chung4b576be2011-04-27 17:40:20 -0700402 private void beginDraggingApplication(View v) {
Adam Cohenac8c8762011-07-13 11:15:27 -0700403 mLauncher.getWorkspace().onDragStartedWithItem(v);
404 mLauncher.getWorkspace().beginDragShared(v, this);
Winson Chung4b576be2011-04-27 17:40:20 -0700405 }
Adam Cohenac8c8762011-07-13 11:15:27 -0700406
Winson Chung4b576be2011-04-27 17:40:20 -0700407 private void beginDraggingWidget(View v) {
408 // Get the widget preview as the drag representation
409 ImageView image = (ImageView) v.findViewById(R.id.widget_preview);
Winson Chung1ed747a2011-05-03 16:18:34 -0700410 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chung4b576be2011-04-27 17:40:20 -0700411
412 // Compose the drag image
Winson Chung1ed747a2011-05-03 16:18:34 -0700413 Bitmap b;
Winson Chung4b576be2011-04-27 17:40:20 -0700414 Drawable preview = image.getDrawable();
415 int w = preview.getIntrinsicWidth();
416 int h = preview.getIntrinsicHeight();
Winson Chung1ed747a2011-05-03 16:18:34 -0700417 if (createItemInfo instanceof PendingAddWidgetInfo) {
418 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo;
419 int[] spanXY = CellLayout.rectToCell(getResources(),
420 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
421 createItemInfo.spanX = spanXY[0];
422 createItemInfo.spanY = spanXY[1];
423
424 b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
425 renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1);
426 } else {
427 // Workaround for the fact that we don't keep the original ResolveInfo associated with
428 // the shortcut around. To get the icon, we just render the preview image (which has
429 // the shortcut icon) to a new drag bitmap that clips the non-icon space.
430 b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension,
431 mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888);
Winson Chungb44b5242011-06-13 11:32:14 -0700432 mCanvas.setBitmap(b);
433 mCanvas.save();
434 preview.draw(mCanvas);
435 mCanvas.restore();
Winson Chung1ed747a2011-05-03 16:18:34 -0700436 createItemInfo.spanX = createItemInfo.spanY = 1;
437 }
Winson Chung4b576be2011-04-27 17:40:20 -0700438
439 // Start the drag
Winson Chung4b576be2011-04-27 17:40:20 -0700440 mLauncher.lockScreenOrientation();
Winson Chung1ed747a2011-05-03 16:18:34 -0700441 mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX,
442 createItemInfo.spanY, b);
443 mDragController.startDrag(image, b, this, createItemInfo,
Winson Chung4b576be2011-04-27 17:40:20 -0700444 DragController.DRAG_ACTION_COPY, null);
445 b.recycle();
446 }
447 @Override
448 protected boolean beginDragging(View v) {
449 if (!super.beginDragging(v)) return false;
450
Winson Chungc07918d2011-07-01 15:35:26 -0700451 // Go into spring loaded mode (must happen before we startDrag())
452 int currentPageIndex = mLauncher.getWorkspace().getCurrentPage();
453 CellLayout currentPage = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPageIndex);
454 mLauncher.enterSpringLoadedDragMode(currentPage);
Winson Chungfc79c802011-05-02 13:35:34 -0700455
Winson Chung4b576be2011-04-27 17:40:20 -0700456 if (v instanceof PagedViewIcon) {
457 beginDraggingApplication(v);
458 } else if (v instanceof PagedViewWidget) {
459 beginDraggingWidget(v);
460 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700461 return true;
462 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700463 private void endDragging(View target, boolean success) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700464 mLauncher.getWorkspace().onDragStopped(success);
Adam Cohend4d7aa52011-07-19 21:47:37 -0700465 if (!success || (target != mLauncher.getWorkspace() &&
466 !(target instanceof DeleteDropTarget))) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700467 // Exit spring loaded mode if we have not successfully dropped or have not handled the
468 // drop in Workspace
469 mLauncher.exitSpringLoadedDragMode();
470 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700471 mLauncher.unlockScreenOrientation();
Winson Chungb26f3d62011-06-02 10:49:29 -0700472
Winson Chung785d2eb2011-04-14 16:08:02 -0700473 }
474
Winson Chung785d2eb2011-04-14 16:08:02 -0700475 @Override
Adam Cohenc0dcf592011-06-01 15:30:43 -0700476 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700477 endDragging(target, success);
Winson Chungfc79c802011-05-02 13:35:34 -0700478
479 // Display an error message if the drag failed due to there not being enough space on the
480 // target layout we were dropping on.
481 if (!success) {
482 boolean showOutOfSpaceMessage = false;
483 if (target instanceof Workspace) {
484 int currentScreen = mLauncher.getCurrentWorkspaceScreen();
485 Workspace workspace = (Workspace) target;
486 CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen);
Adam Cohenc0dcf592011-06-01 15:30:43 -0700487 ItemInfo itemInfo = (ItemInfo) d.dragInfo;
Winson Chungfc79c802011-05-02 13:35:34 -0700488 if (layout != null) {
489 layout.calculateSpans(itemInfo);
490 showOutOfSpaceMessage =
491 !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY);
492 }
493 }
494 // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later.
495 if (showOutOfSpaceMessage) {
496 mLauncher.showOutOfSpaceMessage();
497 }
498 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700499 }
500
501 public void setContentType(ContentType type) {
502 mContentType = type;
Winson Chung5a808352011-06-27 19:08:49 -0700503 invalidatePageData(0);
Winson Chung785d2eb2011-04-14 16:08:02 -0700504 }
505
Winson Chungb44b5242011-06-13 11:32:14 -0700506 public boolean isContentType(ContentType type) {
507 return (mContentType == type);
508 }
509
Winson Chung5a808352011-06-27 19:08:49 -0700510 public void setCurrentPageToWidgets() {
511 invalidatePageData(0);
512 }
513 public void setCurrentPageToShortcuts() {
514 invalidatePageData(mNumWidgetPages);
515 }
516
Winson Chung785d2eb2011-04-14 16:08:02 -0700517 /*
518 * Apps PagedView implementation
519 */
Winson Chung63257c12011-05-05 17:06:13 -0700520 private void setVisibilityOnChildren(ViewGroup layout, int visibility) {
521 int childCount = layout.getChildCount();
522 for (int i = 0; i < childCount; ++i) {
523 layout.getChildAt(i).setVisibility(visibility);
524 }
525 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700526 private void setupPage(PagedViewCellLayout layout) {
527 layout.setCellCount(mCellCountX, mCellCountY);
528 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
529 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
530 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
531
Winson Chung63257c12011-05-05 17:06:13 -0700532 // Note: We force a measure here to get around the fact that when we do layout calculations
533 // immediately after syncing, we don't have a proper width. That said, we already know the
534 // expected page width, so we can actually optimize by hiding all the TextView-based
535 // children that are expensive to measure, and let that happen naturally later.
536 setVisibilityOnChildren(layout, View.GONE);
Winson Chungdb1138b2011-06-30 14:39:35 -0700537 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700538 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung63257c12011-05-05 17:06:13 -0700539 layout.setMinimumWidth(getPageContentWidth());
Winson Chung785d2eb2011-04-14 16:08:02 -0700540 layout.measure(widthSpec, heightSpec);
Winson Chung63257c12011-05-05 17:06:13 -0700541 setVisibilityOnChildren(layout, View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -0700542 }
543 public void syncAppsPages() {
544 // Ensure that we have the right number of pages
545 Context context = getContext();
546 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
547 for (int i = 0; i < numPages; ++i) {
548 PagedViewCellLayout layout = new PagedViewCellLayout(context);
549 setupPage(layout);
550 addView(layout);
551 }
552 }
553 public void syncAppsPageItems(int page) {
554 // ensure that we have the right number of items on the pages
555 int numPages = getPageCount();
556 int numCells = mCellCountX * mCellCountY;
557 int startIndex = page * numCells;
558 int endIndex = Math.min(startIndex + numCells, mApps.size());
559 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung875de7e2011-06-28 14:25:17 -0700560
Winson Chung785d2eb2011-04-14 16:08:02 -0700561 layout.removeAllViewsOnPage();
Winson Chungb44b5242011-06-13 11:32:14 -0700562 ArrayList<Object> items = new ArrayList<Object>();
563 ArrayList<Bitmap> images = new ArrayList<Bitmap>();
Winson Chung785d2eb2011-04-14 16:08:02 -0700564 for (int i = startIndex; i < endIndex; ++i) {
565 ApplicationInfo info = mApps.get(i);
566 PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate(
567 R.layout.apps_customize_application, layout, false);
Winson Chungb44b5242011-06-13 11:32:14 -0700568 icon.applyFromApplicationInfo(info, true, mHolographicOutlineHelper);
Winson Chung785d2eb2011-04-14 16:08:02 -0700569 icon.setOnClickListener(this);
570 icon.setOnLongClickListener(this);
571 icon.setOnTouchListener(this);
572
573 int index = i - startIndex;
574 int x = index % mCellCountX;
575 int y = index / mCellCountX;
Winson Chung6a70e9f2011-05-17 16:24:49 -0700576 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chungb44b5242011-06-13 11:32:14 -0700577
578 items.add(info);
579 images.add(info.iconBitmap);
Winson Chung785d2eb2011-04-14 16:08:02 -0700580 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700581
582 // Create the hardware layers
583 layout.allowHardwareLayerCreation();
584 layout.createHardwareLayers();
Winson Chungb44b5242011-06-13 11:32:14 -0700585
586 prepareGenerateHoloOutlinesTask(page, items, images);
Winson Chung785d2eb2011-04-14 16:08:02 -0700587 }
Winson Chungb44b5242011-06-13 11:32:14 -0700588
589 /**
590 * Return the appropriate thread priority for loading for a given page (we give the current
591 * page much higher priority)
592 */
593 private int getThreadPriorityForPage(int page) {
594 // TODO-APPS_CUSTOMIZE: detect number of cores and set thread priorities accordingly below
595 int pageDiff = Math.abs(page - mCurrentPage);
596 if (pageDiff <= 0) {
597 // return Process.THREAD_PRIORITY_DEFAULT;
598 return Process.THREAD_PRIORITY_MORE_FAVORABLE;
599 } else if (pageDiff <= 1) {
600 // return Process.THREAD_PRIORITY_BACKGROUND;
601 return Process.THREAD_PRIORITY_DEFAULT;
602 } else {
603 // return Process.THREAD_PRIORITY_LOWEST;
604 return Process.THREAD_PRIORITY_DEFAULT;
605 }
606 }
607 /**
608 * Creates and executes a new AsyncTask to load a page of widget previews.
609 */
610 private void prepareLoadWidgetPreviewsTask(int page, ArrayList<Object> widgets,
Winson Chungd2945262011-06-24 15:22:14 -0700611 int cellWidth, int cellHeight, int cellCountX) {
Winson Chungb44b5242011-06-13 11:32:14 -0700612 // Prune all tasks that are no longer needed
613 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
614 while (iter.hasNext()) {
615 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
616 int taskPage = task.page;
Winson Chung875de7e2011-06-28 14:25:17 -0700617 if ((taskPage == page) ||
618 taskPage < getAssociatedLowerPageBound(mCurrentPage) ||
Winson Chung4e076542011-06-23 13:04:10 -0700619 taskPage > getAssociatedUpperPageBound(mCurrentPage)) {
Winson Chungb44b5242011-06-13 11:32:14 -0700620 task.cancel(false);
621 iter.remove();
622 } else {
623 task.setThreadPriority(getThreadPriorityForPage(taskPage));
624 }
625 }
626
627 AsyncTaskPageData pageData = new AsyncTaskPageData(page, widgets, cellWidth, cellHeight,
Winson Chungd2945262011-06-24 15:22:14 -0700628 cellCountX, new AsyncTaskCallback() {
Winson Chungb44b5242011-06-13 11:32:14 -0700629 @Override
630 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
631 // Ensure that this task starts running at the correct priority
632 task.syncThreadPriority();
633
634 // Load each of the widget/shortcut previews
635 ArrayList<Object> items = data.items;
Winson Chung4e076542011-06-23 13:04:10 -0700636 ArrayList<Bitmap> images = data.generatedImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700637 int count = items.size();
638 int cellWidth = data.cellWidth;
639 int cellHeight = data.cellHeight;
640 for (int i = 0; i < count && !task.isCancelled(); ++i) {
641 // Before work on each item, ensure that this task is running at the correct
642 // priority
643 task.syncThreadPriority();
644
645 Object rawInfo = items.get(i);
646 if (rawInfo instanceof AppWidgetProviderInfo) {
647 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
648 int[] cellSpans = CellLayout.rectToCell(getResources(),
649 info.minWidth, info.minHeight, null);
Winson Chung4e076542011-06-23 13:04:10 -0700650 images.add(getWidgetPreview(info, cellSpans[0],cellSpans[1],
Winson Chungb44b5242011-06-13 11:32:14 -0700651 cellWidth, cellHeight));
652 } else if (rawInfo instanceof ResolveInfo) {
653 // Fill in the shortcuts information
654 ResolveInfo info = (ResolveInfo) rawInfo;
Winson Chung4e076542011-06-23 13:04:10 -0700655 images.add(getShortcutPreview(info, cellWidth, cellHeight));
Winson Chungb44b5242011-06-13 11:32:14 -0700656 }
657 }
658 }
659 },
660 new AsyncTaskCallback() {
661 @Override
662 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
663 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700664 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700665 if (task.page > getPageCount()) return;
666 if (task.pageContentType != mContentType) return;
667 onSyncWidgetPageItems(data);
668 }
669 });
670
671 // Ensure that the task is appropriately prioritized and runs in parallel
Winson Chung875de7e2011-06-28 14:25:17 -0700672 AppsCustomizeAsyncTask t = new AppsCustomizeAsyncTask(page, mContentType,
673 AsyncTaskPageData.Type.LoadWidgetPreviewData);
Winson Chungb44b5242011-06-13 11:32:14 -0700674 t.setThreadPriority(getThreadPriorityForPage(page));
675 t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, pageData);
676 mRunningTasks.add(t);
677 }
678 /**
679 * Creates and executes a new AsyncTask to load the outlines for a page of content.
680 */
681 private void prepareGenerateHoloOutlinesTask(int page, ArrayList<Object> items,
682 ArrayList<Bitmap> images) {
Winson Chung875de7e2011-06-28 14:25:17 -0700683 // Prune old tasks for this page
684 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
685 while (iter.hasNext()) {
686 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
687 int taskPage = task.page;
688 if ((taskPage == page) &&
689 (task.dataType == AsyncTaskPageData.Type.LoadHolographicIconsData)) {
690 task.cancel(false);
691 iter.remove();
692 }
693 }
694
Winson Chungb44b5242011-06-13 11:32:14 -0700695 AsyncTaskPageData pageData = new AsyncTaskPageData(page, items, images,
696 new AsyncTaskCallback() {
697 @Override
698 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
699 // Ensure that this task starts running at the correct priority
700 task.syncThreadPriority();
701
Winson Chung4e076542011-06-23 13:04:10 -0700702 ArrayList<Bitmap> images = data.generatedImages;
703 ArrayList<Bitmap> srcImages = data.sourceImages;
Winson Chungb44b5242011-06-13 11:32:14 -0700704 int count = srcImages.size();
705 Canvas c = new Canvas();
706 for (int i = 0; i < count && !task.isCancelled(); ++i) {
707 // Before work on each item, ensure that this task is running at the correct
708 // priority
709 task.syncThreadPriority();
710
711 Bitmap b = srcImages.get(i);
712 Bitmap outline = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
713 Bitmap.Config.ARGB_8888);
714
715 c.setBitmap(outline);
716 c.save();
717 c.drawBitmap(b, 0, 0, null);
718 c.restore();
719
720 images.add(outline);
721 }
722 }
723 },
724 new AsyncTaskCallback() {
725 @Override
726 public void run(AppsCustomizeAsyncTask task, AsyncTaskPageData data) {
727 mRunningTasks.remove(task);
Winson Chung875de7e2011-06-28 14:25:17 -0700728 if (task.isCancelled()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700729 if (task.page > getPageCount()) return;
730 if (task.pageContentType != mContentType) return;
731 onHolographicPageItemsLoaded(data);
732 }
733 });
734
735 // Ensure that the outline task always runs in the background, serially
Winson Chung4e076542011-06-23 13:04:10 -0700736 AppsCustomizeAsyncTask t =
Winson Chung875de7e2011-06-28 14:25:17 -0700737 new AppsCustomizeAsyncTask(page, mContentType,
738 AsyncTaskPageData.Type.LoadHolographicIconsData);
Winson Chungb44b5242011-06-13 11:32:14 -0700739 t.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
740 t.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, pageData);
741 mRunningTasks.add(t);
742 }
743
Winson Chung785d2eb2011-04-14 16:08:02 -0700744 /*
745 * Widgets PagedView implementation
746 */
Winson Chung4e6a9762011-05-09 11:56:34 -0700747 private void setupPage(PagedViewGridLayout layout) {
Winson Chung785d2eb2011-04-14 16:08:02 -0700748 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
749 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chung63257c12011-05-05 17:06:13 -0700750
751 // Note: We force a measure here to get around the fact that when we do layout calculations
Winson Chungd52f3d82011-07-12 14:29:11 -0700752 // immediately after syncing, we don't have a proper width.
Winson Chung63257c12011-05-05 17:06:13 -0700753 int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST);
754 int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST);
Winson Chung785d2eb2011-04-14 16:08:02 -0700755 layout.setMinimumWidth(getPageContentWidth());
Winson Chung63257c12011-05-05 17:06:13 -0700756 layout.measure(widthSpec, heightSpec);
Winson Chung785d2eb2011-04-14 16:08:02 -0700757 }
Michael Jurkabe69cc82011-07-07 16:05:33 -0700758 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h,
Winson Chung785d2eb2011-04-14 16:08:02 -0700759 float scaleX, float scaleY) {
Winson Chung201bc822011-06-20 15:41:53 -0700760 if (bitmap != null) {
Winson Chungb44b5242011-06-13 11:32:14 -0700761 Canvas c = new Canvas(bitmap);
Winson Chung201bc822011-06-20 15:41:53 -0700762 c.scale(scaleX, scaleY);
763 Rect oldBounds = d.copyBounds();
764 d.setBounds(x, y, x + w, y + h);
765 d.draw(c);
766 d.setBounds(oldBounds); // Restore the bounds
Winson Chung201bc822011-06-20 15:41:53 -0700767 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700768 }
Winson Chungd2945262011-06-24 15:22:14 -0700769 private Bitmap getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700770 // Render the icon
Winson Chungd2945262011-06-24 15:22:14 -0700771 Bitmap preview = Bitmap.createBitmap(cellWidth, mAppIconSize, Config.ARGB_8888);
Winson Chung4dbea792011-05-05 14:21:32 -0700772 Drawable icon = mIconCache.getFullResIcon(info, mPackageManager);
Winson Chungd2945262011-06-24 15:22:14 -0700773 renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chungb44b5242011-06-13 11:32:14 -0700774 return preview;
Winson Chung1ed747a2011-05-03 16:18:34 -0700775 }
Winson Chung4e076542011-06-23 13:04:10 -0700776 private Bitmap getWidgetPreview(AppWidgetProviderInfo info,
Winson Chungb44b5242011-06-13 11:32:14 -0700777 int cellHSpan, int cellVSpan, int cellWidth, int cellHeight) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700778
Winson Chung4b576be2011-04-27 17:40:20 -0700779 // Calculate the size of the drawable
780 cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan));
781 cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan));
782 int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan);
783 int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan);
784
785 // Scale down the bitmap to fit the space
786 float widgetPreviewScale = (float) cellWidth / expectedWidth;
787 expectedWidth = (int) (widgetPreviewScale * expectedWidth);
788 expectedHeight = (int) (widgetPreviewScale * expectedHeight);
789
790 // Load the preview image if possible
791 String packageName = info.provider.getPackageName();
792 Drawable drawable = null;
Winson Chungb44b5242011-06-13 11:32:14 -0700793 Bitmap preview = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700794 if (info.previewImage != 0) {
795 drawable = mPackageManager.getDrawable(packageName, info.previewImage, null);
796 if (drawable == null) {
797 Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
798 + " for provider: " + info.provider);
799 } else {
800 // Scale down the preview to the dimensions we want
801 int imageWidth = drawable.getIntrinsicWidth();
802 int imageHeight = drawable.getIntrinsicHeight();
803 float aspect = (float) imageWidth / imageHeight;
804 int newWidth = imageWidth;
805 int newHeight = imageHeight;
806 if (aspect > 1f) {
807 newWidth = expectedWidth;
808 newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth));
809 } else {
810 newHeight = expectedHeight;
811 newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight));
812 }
813
Winson Chungb44b5242011-06-13 11:32:14 -0700814 preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
Winson Chung4b576be2011-04-27 17:40:20 -0700815 renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700816 }
817 }
818
819 // Generate a preview image if we couldn't load one
820 if (drawable == null) {
Winson Chung1ed747a2011-05-03 16:18:34 -0700821 Resources resources = mLauncher.getResources();
Winson Chung273c1022011-07-11 13:40:52 -0700822 int bitmapWidth;
823 int bitmapHeight;
Winson Chung1ed747a2011-05-03 16:18:34 -0700824
Winson Chung273c1022011-07-11 13:40:52 -0700825 // Specify the dimensions of the bitmap (since we are using a default preview bg with
826 // the full icon, we only imply the aspect ratio of the widget)
827 if (cellHSpan == cellVSpan) {
828 bitmapWidth = bitmapHeight = cellWidth;
829 expectedWidth = expectedHeight = mWidgetPreviewIconPaddedDimension;
830 } else if (cellHSpan >= cellVSpan) {
831 bitmapWidth = expectedWidth = cellWidth;
832 bitmapHeight = expectedHeight = mWidgetPreviewIconPaddedDimension;
Winson Chung1ed747a2011-05-03 16:18:34 -0700833 } else {
834 // Note that in vertical widgets, we might not have enough space due to the text
835 // label, so be conservative and use the width as a height bound
Winson Chung273c1022011-07-11 13:40:52 -0700836 bitmapWidth = expectedWidth = mWidgetPreviewIconPaddedDimension;
837 bitmapHeight = expectedHeight = cellWidth;
Winson Chung1ed747a2011-05-03 16:18:34 -0700838 }
Winson Chung4b576be2011-04-27 17:40:20 -0700839
Winson Chung273c1022011-07-11 13:40:52 -0700840 preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
Winson Chung1ed747a2011-05-03 16:18:34 -0700841 renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth,
842 expectedHeight, 1f,1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700843
844 // Draw the icon in the top left corner
845 try {
846 Drawable icon = null;
847 if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null);
848 if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application);
849
Winson Chungd2945262011-06-24 15:22:14 -0700850 int offset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
851 renderDrawableToBitmap(icon, preview, offset, offset,
852 mAppIconSize, mAppIconSize, 1f, 1f);
Winson Chung4b576be2011-04-27 17:40:20 -0700853 } catch (Resources.NotFoundException e) {}
Winson Chung4b576be2011-04-27 17:40:20 -0700854 }
Winson Chungb44b5242011-06-13 11:32:14 -0700855 return preview;
Winson Chung785d2eb2011-04-14 16:08:02 -0700856 }
857 public void syncWidgetPages() {
858 // Ensure that we have the right number of pages
859 Context context = getContext();
Winson Chungd2945262011-06-24 15:22:14 -0700860 int[] countX = { mWidgetCountX, mShortcutCountX };
861 int[] countY = { mWidgetCountY, mShortcutCountY };
Winson Chung5a808352011-06-27 19:08:49 -0700862 int[] numPages = { mNumWidgetPages, mNumShortcutPages };
Winson Chungd2945262011-06-24 15:22:14 -0700863 for (int i = 0; i < 2; ++i) {
Winson Chung5a808352011-06-27 19:08:49 -0700864 for (int j = 0; j < numPages[i]; ++j) {
865 PagedViewGridLayout layout = new PagedViewGridLayout(context, countX[i], countY[i]);
Winson Chungd2945262011-06-24 15:22:14 -0700866 setupPage(layout);
867 addView(layout);
868 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700869 }
870 }
871 public void syncWidgetPageItems(int page) {
Winson Chungd2945262011-06-24 15:22:14 -0700872 int[] countX = { mWidgetCountX, mShortcutCountX };
873 int[] countY = { mWidgetCountY, mShortcutCountY };
874 int[] widthGap = { mWidgetWidthGap, mWidgetWidthGap };
875 int[] heightGap = { mWidgetHeightGap, mWidgetHeightGap };
876 int[] numItemsPerPage = { mWidgetCountX * mWidgetCountY,
877 mShortcutCountX * mShortcutCountY };
878 Object[] collection = { mWidgets, mShortcuts };
879 int contentWidth = mWidgetSpacingLayout.getContentWidth();
880 int contentHeight = mWidgetSpacingLayout.getContentHeight();
881 int numWidgetPages = (int) Math.ceil(mWidgets.size() / (float) numItemsPerPage[0]);
882 int[] offsets = { page * numItemsPerPage[0], (page - numWidgetPages) * numItemsPerPage[1] };
883 int index = (page < numWidgetPages ? 0 : 1);
Winson Chungb44b5242011-06-13 11:32:14 -0700884
Winson Chungd2945262011-06-24 15:22:14 -0700885 // Calculate the dimensions of each cell we are giving to each widget
886 ArrayList<Object> list = (ArrayList<Object>) collection[index];
887 ArrayList<Object> items = new ArrayList<Object>();
888 int cellWidth = ((contentWidth - mPageLayoutPaddingLeft - mPageLayoutPaddingRight
889 - ((countX[index] - 1) * widthGap[index])) / countX[index]);
890 int cellHeight = ((contentHeight - mPageLayoutPaddingTop - mPageLayoutPaddingBottom
891 - ((countY[index] - 1) * heightGap[index])) / countY[index]);
892
893 int offset = offsets[index];
894 for (int i = offset; i < Math.min(offset + numItemsPerPage[index], list.size()); ++i) {
895 items.add(list.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700896 }
897
Winson Chungd2945262011-06-24 15:22:14 -0700898 prepareLoadWidgetPreviewsTask(page, items, cellWidth, cellHeight, countX[index]);
Winson Chungb44b5242011-06-13 11:32:14 -0700899 }
900 private void onSyncWidgetPageItems(AsyncTaskPageData data) {
901 int page = data.page;
Winson Chung4e6a9762011-05-09 11:56:34 -0700902 PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page);
Winson Chungd52f3d82011-07-12 14:29:11 -0700903 // Only set the column count once we have items
904 layout.setColumnCount(layout.getCellCountX());
Winson Chung785d2eb2011-04-14 16:08:02 -0700905
Winson Chungb44b5242011-06-13 11:32:14 -0700906 ArrayList<Object> items = data.items;
907 int count = items.size();
908 int cellWidth = data.cellWidth;
909 int cellHeight = data.cellHeight;
Winson Chungd2945262011-06-24 15:22:14 -0700910 int cellCountX = data.cellCountX;
Winson Chungb44b5242011-06-13 11:32:14 -0700911 for (int i = 0; i < count; ++i) {
912 Object rawInfo = items.get(i);
Winson Chung1ed747a2011-05-03 16:18:34 -0700913 PendingAddItemInfo createItemInfo = null;
Winson Chung4b576be2011-04-27 17:40:20 -0700914 PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate(
915 R.layout.apps_customize_widget, layout, false);
Winson Chung1ed747a2011-05-03 16:18:34 -0700916 if (rawInfo instanceof AppWidgetProviderInfo) {
917 // Fill in the widget information
918 AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo;
919 createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chungb44b5242011-06-13 11:32:14 -0700920 int[] cellSpans = CellLayout.rectToCell(getResources(),
921 info.minWidth, info.minHeight, null);
Winson Chung4e076542011-06-23 13:04:10 -0700922 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chung9f4e0fd2011-06-02 18:59:36 -0700923 widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans,
Winson Chungb44b5242011-06-13 11:32:14 -0700924 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -0700925 widget.setTag(createItemInfo);
926 } else if (rawInfo instanceof ResolveInfo) {
927 // Fill in the shortcuts information
928 ResolveInfo info = (ResolveInfo) rawInfo;
929 createItemInfo = new PendingAddItemInfo();
930 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
931 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
932 info.activityInfo.name);
Winson Chung4e076542011-06-23 13:04:10 -0700933 FastBitmapDrawable preview = new FastBitmapDrawable(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700934 widget.applyFromResolveInfo(mPackageManager, info, preview,
935 mHolographicOutlineHelper);
Winson Chung1ed747a2011-05-03 16:18:34 -0700936 widget.setTag(createItemInfo);
937 }
Winson Chung4b576be2011-04-27 17:40:20 -0700938 widget.setOnClickListener(this);
939 widget.setOnLongClickListener(this);
940 widget.setOnTouchListener(this);
941
942 // Layout each widget
Winson Chungd2945262011-06-24 15:22:14 -0700943 int ix = i % cellCountX;
944 int iy = i / cellCountX;
Winson Chungfd3385f2011-06-15 19:51:24 -0700945 GridLayout.LayoutParams lp = new GridLayout.LayoutParams(
Winson Chungd52f3d82011-07-12 14:29:11 -0700946 GridLayout.spec(iy, GridLayout.LEFT, GridLayout.CAN_STRETCH),
947 GridLayout.spec(ix, GridLayout.TOP, GridLayout.CAN_STRETCH));
Winson Chungfd3385f2011-06-15 19:51:24 -0700948 lp.width = cellWidth;
949 lp.height = cellHeight;
950 if (ix > 0) lp.leftMargin = mWidgetWidthGap;
951 if (iy > 0) lp.topMargin = mWidgetHeightGap;
Winson Chung4e6a9762011-05-09 11:56:34 -0700952 layout.addView(widget, lp);
Winson Chung785d2eb2011-04-14 16:08:02 -0700953 }
Winson Chungb44b5242011-06-13 11:32:14 -0700954
955 invalidate();
956 forceUpdateAdjacentPagesAlpha();
Winson Chung4e076542011-06-23 13:04:10 -0700957 prepareGenerateHoloOutlinesTask(data.page, data.items, data.generatedImages);
Winson Chungb44b5242011-06-13 11:32:14 -0700958 }
959 private void onHolographicPageItemsLoaded(AsyncTaskPageData data) {
960 // Invalidate early to short-circuit children invalidates
961 invalidate();
962
963 int page = data.page;
964 ViewGroup layout = (ViewGroup) getChildAt(page);
965 if (layout instanceof PagedViewCellLayout) {
966 PagedViewCellLayout cl = (PagedViewCellLayout) layout;
967 int count = cl.getPageChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -0700968 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700969 for (int i = 0; i < count; ++i) {
970 PagedViewIcon icon = (PagedViewIcon) cl.getChildOnPageAt(i);
Winson Chung4e076542011-06-23 13:04:10 -0700971 icon.setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700972 }
973 } else {
974 int count = layout.getChildCount();
Winson Chung875de7e2011-06-28 14:25:17 -0700975 if (count != data.generatedImages.size()) return;
Winson Chungb44b5242011-06-13 11:32:14 -0700976 for (int i = 0; i < count; ++i) {
977 View v = layout.getChildAt(i);
Winson Chung4e076542011-06-23 13:04:10 -0700978 ((PagedViewWidget) v).setHolographicOutline(data.generatedImages.get(i));
Winson Chungb44b5242011-06-13 11:32:14 -0700979 }
980 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700981 }
Winson Chung46af2e82011-05-09 16:00:53 -0700982
Winson Chung785d2eb2011-04-14 16:08:02 -0700983 @Override
984 public void syncPages() {
985 removeAllViews();
Winson Chung875de7e2011-06-28 14:25:17 -0700986
987 // Remove all background asyc tasks if we are loading content anew
988 Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
989 while (iter.hasNext()) {
990 AppsCustomizeAsyncTask task = (AppsCustomizeAsyncTask) iter.next();
991 task.cancel(false);
992 iter.remove();
993 }
994
Winson Chung785d2eb2011-04-14 16:08:02 -0700995 switch (mContentType) {
996 case Applications:
997 syncAppsPages();
998 break;
999 case Widgets:
1000 syncWidgetPages();
1001 break;
1002 }
1003 }
1004 @Override
1005 public void syncPageItems(int page) {
1006 switch (mContentType) {
1007 case Applications:
1008 syncAppsPageItems(page);
1009 break;
1010 case Widgets:
1011 syncWidgetPageItems(page);
1012 break;
1013 }
1014 }
1015
1016 /**
1017 * Used by the parent to get the content width to set the tab bar to
1018 * @return
1019 */
1020 public int getPageContentWidth() {
1021 return mContentWidth;
1022 }
1023
Winson Chungb26f3d62011-06-02 10:49:29 -07001024 @Override
1025 protected void onPageBeginMoving() {
1026 /* TO BE ENABLED LATER
1027 setChildrenDrawnWithCacheEnabled(true);
1028 for (int i = 0; i < getChildCount(); ++i) {
1029 View v = getChildAt(i);
1030 if (v instanceof PagedViewCellLayout) {
1031 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true);
1032 }
1033 }
1034 */
1035 super.onPageBeginMoving();
1036 }
1037
1038 @Override
1039 protected void onPageEndMoving() {
1040 /* TO BE ENABLED LATER
1041 for (int i = 0; i < getChildCount(); ++i) {
1042 View v = getChildAt(i);
1043 if (v instanceof PagedViewCellLayout) {
1044 ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false);
1045 }
1046 }
1047 setChildrenDrawnWithCacheEnabled(false);
1048 */
1049 super.onPageEndMoving();
1050 }
1051
Winson Chung785d2eb2011-04-14 16:08:02 -07001052 /*
1053 * AllAppsView implementation
1054 */
1055 @Override
1056 public void setup(Launcher launcher, DragController dragController) {
1057 mLauncher = launcher;
1058 mDragController = dragController;
1059 }
1060 @Override
1061 public void zoom(float zoom, boolean animate) {
1062 // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed()
1063 }
1064 @Override
1065 public boolean isVisible() {
1066 return (getVisibility() == VISIBLE);
1067 }
1068 @Override
1069 public boolean isAnimating() {
1070 return false;
1071 }
1072 @Override
1073 public void setApps(ArrayList<ApplicationInfo> list) {
1074 mApps = list;
1075 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001076
Winson Chung875de7e2011-06-28 14:25:17 -07001077 // The next layout pass will trigger data-ready if both widgets and apps are set, so
1078 // request a layout to do this test and invalidate the page data when ready.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001079 if (testDataReady()) requestLayout();
Winson Chung785d2eb2011-04-14 16:08:02 -07001080 }
1081 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1082 // We add it in place, in alphabetical order
1083 int count = list.size();
1084 for (int i = 0; i < count; ++i) {
1085 ApplicationInfo info = list.get(i);
1086 int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
1087 if (index < 0) {
1088 mApps.add(-(index + 1), info);
1089 }
1090 }
1091 }
1092 @Override
1093 public void addApps(ArrayList<ApplicationInfo> list) {
1094 addAppsWithoutInvalidate(list);
1095 invalidatePageData();
1096 }
1097 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
1098 ComponentName removeComponent = item.intent.getComponent();
1099 int length = list.size();
1100 for (int i = 0; i < length; ++i) {
1101 ApplicationInfo info = list.get(i);
1102 if (info.intent.getComponent().equals(removeComponent)) {
1103 return i;
1104 }
1105 }
1106 return -1;
1107 }
1108 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
1109 // loop through all the apps and remove apps that have the same component
1110 int length = list.size();
1111 for (int i = 0; i < length; ++i) {
1112 ApplicationInfo info = list.get(i);
1113 int removeIndex = findAppByComponent(mApps, info);
1114 if (removeIndex > -1) {
1115 mApps.remove(removeIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001116 }
1117 }
1118 }
1119 @Override
1120 public void removeApps(ArrayList<ApplicationInfo> list) {
1121 removeAppsWithoutInvalidate(list);
1122 invalidatePageData();
1123 }
1124 @Override
1125 public void updateApps(ArrayList<ApplicationInfo> list) {
1126 // We remove and re-add the updated applications list because it's properties may have
1127 // changed (ie. the title), and this will ensure that the items will be in their proper
1128 // place in the list.
1129 removeAppsWithoutInvalidate(list);
1130 addAppsWithoutInvalidate(list);
1131 invalidatePageData();
1132 }
Michael Jurka35aa14d2011-07-07 17:01:08 -07001133
Winson Chung785d2eb2011-04-14 16:08:02 -07001134 @Override
1135 public void reset() {
Winson Chungfc79c802011-05-02 13:35:34 -07001136 if (mContentType != ContentType.Applications) {
1137 // Reset to the first page of the Apps pane
1138 AppsCustomizeTabHost tabs = (AppsCustomizeTabHost)
1139 mLauncher.findViewById(R.id.apps_customize_pane);
Winson Chung5a808352011-06-27 19:08:49 -07001140 tabs.selectAppsTab();
Michael Jurka35aa14d2011-07-07 17:01:08 -07001141 } else if (getCurrentPage() != 0) {
Winson Chung5a808352011-06-27 19:08:49 -07001142 invalidatePageData(0);
Winson Chungfc79c802011-05-02 13:35:34 -07001143 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001144 }
1145 @Override
1146 public void dumpState() {
1147 // TODO: Dump information related to current list of Applications, Widgets, etc.
1148 ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps);
1149 dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets);
Winson Chungd2945262011-06-24 15:22:14 -07001150 dumpAppWidgetProviderInfoList(LOG_TAG, "mShortcuts", mShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07001151 }
1152 private void dumpAppWidgetProviderInfoList(String tag, String label,
Winson Chungd2945262011-06-24 15:22:14 -07001153 ArrayList<Object> list) {
Winson Chung785d2eb2011-04-14 16:08:02 -07001154 Log.d(tag, label + " size=" + list.size());
Winson Chung1ed747a2011-05-03 16:18:34 -07001155 for (Object i: list) {
1156 if (i instanceof AppWidgetProviderInfo) {
1157 AppWidgetProviderInfo info = (AppWidgetProviderInfo) i;
1158 Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage
1159 + " resizeMode=" + info.resizeMode + " configure=" + info.configure
1160 + " initialLayout=" + info.initialLayout
1161 + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight);
1162 } else if (i instanceof ResolveInfo) {
1163 ResolveInfo info = (ResolveInfo) i;
1164 Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon="
1165 + info.icon);
1166 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001167 }
1168 }
1169 @Override
1170 public void surrender() {
1171 // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we
1172 // should stop this now.
1173 }
Winson Chung007c6982011-06-14 13:27:53 -07001174
Winson Chungb44b5242011-06-13 11:32:14 -07001175 /*
1176 * We load an extra page on each side to prevent flashes from scrolling and loading of the
1177 * widget previews in the background with the AsyncTasks.
1178 */
1179 protected int getAssociatedLowerPageBound(int page) {
1180 return Math.max(0, page - 2);
1181 }
1182 protected int getAssociatedUpperPageBound(int page) {
1183 final int count = getChildCount();
1184 return Math.min(page + 2, count - 1);
1185 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07001186
1187 @Override
1188 protected String getCurrentPageDescription() {
1189 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
1190 int stringId = R.string.default_scroll_format;
1191 switch (mContentType) {
1192 case Applications:
1193 stringId = R.string.apps_customize_apps_scroll_format;
1194 break;
1195 case Widgets:
1196 stringId = R.string.apps_customize_widgets_scroll_format;
1197 break;
1198 }
1199 return String.format(mContext.getString(stringId), page + 1, getChildCount());
1200 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001201}