blob: 2f2cb24ab8a281672b09e1a6b044c9e586e4e840 [file] [log] [blame]
Winson Chung80baf5a2010-08-09 16:03:15 -07001/*
2 * Copyright (C) 2010 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
Michael Jurka72b079e2010-12-10 01:03:53 -080019import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070020
Adam Cohen7b9d3a62010-12-07 21:49:34 -080021import org.xmlpull.v1.XmlPullParser;
22
Adam Cohen120980b2010-12-08 11:05:37 -080023import android.animation.Animator;
Patrick Dubroy047379a2010-12-19 22:02:04 -080024import android.animation.AnimatorListenerAdapter;
Adam Cohen120980b2010-12-08 11:05:37 -080025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
27import android.animation.TimeInterpolator;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080028import android.app.WallpaperManager;
Winson Chung80baf5a2010-08-09 16:03:15 -070029import android.appwidget.AppWidgetManager;
30import android.appwidget.AppWidgetProviderInfo;
31import android.content.ComponentName;
32import android.content.Context;
33import android.content.Intent;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080034import android.content.pm.ActivityInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
37import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070038import android.content.res.TypedArray;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080039import android.content.res.XmlResourceParser;
Winson Chung80baf5a2010-08-09 16:03:15 -070040import android.graphics.Bitmap;
Winson Chungcd4bc492010-12-09 18:52:32 -080041import android.graphics.Canvas;
Patrick Dubroy5f445422011-02-18 14:35:21 -080042import android.graphics.Rect;
43import android.graphics.Bitmap.Config;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070045import android.util.AttributeSet;
46import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080047import android.util.Slog;
48import android.util.TypedValue;
49import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070050import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070051import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070052import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070053import android.view.Menu;
54import android.view.MenuItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070055import android.view.View;
Adam Cohen120980b2010-12-08 11:05:37 -080056import android.view.animation.DecelerateInterpolator;
Patrick Dubroy5f445422011-02-18 14:35:21 -080057import android.view.animation.LinearInterpolator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080058import android.widget.Checkable;
Winson Chunge3193b92010-09-10 11:44:42 -070059import android.widget.ImageView;
60import android.widget.LinearLayout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080061import android.widget.TextView;
Winson Chung80baf5a2010-08-09 16:03:15 -070062
Michael Jurka72b079e2010-12-10 01:03:53 -080063import java.util.ArrayList;
64import java.util.Collections;
65import java.util.Comparator;
66import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070067
Adam Cohen7b9d3a62010-12-07 21:49:34 -080068
Michael Jurka72b079e2010-12-10 01:03:53 -080069public class CustomizePagedView extends PagedViewWithDraggableItems
70 implements View.OnClickListener, DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070071
72 public enum CustomizationType {
73 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070074 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070075 WallpaperCustomization,
76 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070077 }
78
79 private static final String TAG = "CustomizeWorkspace";
Winson Chung80baf5a2010-08-09 16:03:15 -070080
81 private Launcher mLauncher;
82 private DragController mDragController;
83 private PackageManager mPackageManager;
84
85 private CustomizationType mCustomizationType;
86
Winson Chunge3193b92010-09-10 11:44:42 -070087 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
88 private PagedViewCellLayout mWorkspaceWidgetLayout;
89
90 // The mapping between the pages and the widgets that will be laid out on them
91 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
92
Winson Chung45e1d6e2010-11-09 17:19:49 -080093 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070094 private int mMaxWidgetWidth;
95
Winson Chung45e1d6e2010-11-09 17:19:49 -080096 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -070097 private int mMaxWidgetsCellHSpan;
98
Winson Chung45e1d6e2010-11-09 17:19:49 -080099 // The size of the items on the wallpaper tab
100 private int mWallpaperCellHSpan;
101
Winson Chung78bd53c2010-12-09 13:50:24 -0800102 // The max number of wallpaper cells to take a "page" of wallpaper items
103 private int mMaxWallpaperCellHSpan;
104
Winson Chunge3193b92010-09-10 11:44:42 -0700105 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700106 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700107 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700108 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700109 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700110
Winson Chunge3193b92010-09-10 11:44:42 -0700111 private static final int sMinWidgetCellHSpan = 2;
112 private static final int sMaxWidgetCellHSpan = 4;
113
Michael Jurka3125d9d2010-09-27 11:30:20 -0700114 private int mChoiceModeTitleText;
115
Winson Chunge3193b92010-09-10 11:44:42 -0700116 // The scale factor for widget previews inside the widget drawer
117 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700118
119 private final Canvas mCanvas = new Canvas();
120 private final LayoutInflater mInflater;
121
Adam Cohen120980b2010-12-08 11:05:37 -0800122 private final float mTmpFloatPos[] = new float[2];
123 private final float ANIMATION_SCALE = 0.5f;
Patrick Dubroy5f445422011-02-18 14:35:21 -0800124
125 // The duration of the translation animation that occurs during you drag and drop
126 private final int TRANSLATE_ANIM_DURATION = 400;
127
128 // The duration of the scale & alpha animation that occurs during drag and drop
129 private final int DROP_ANIM_DURATION = 200;
130
Adam Cohen120980b2010-12-08 11:05:37 -0800131 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800132
133 // The Bitmap used to generate the drag view
134 private Bitmap mDragBitmap;
135
136 private int[] mDragViewOrigin = new int[2];
Adam Cohen120980b2010-12-08 11:05:37 -0800137
Winson Chung80baf5a2010-08-09 16:03:15 -0700138 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700139 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700140 }
141
142 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700143 this(context, attrs, 0);
144 }
145
146 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
147 super(context, attrs, defStyle);
148
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700149 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800150 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
151 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800152 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700153 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
154 a.recycle();
155 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
156 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
157 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700158 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800159
Winson Chung80baf5a2010-08-09 16:03:15 -0700160 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700161 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
162 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700163 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700164
Michael Jurka7426c422010-11-11 15:23:47 -0800165 final Resources r = context.getResources();
Michael Jurka72b079e2010-12-10 01:03:53 -0800166 setDragSlopeThreshold(
167 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7426c422010-11-11 15:23:47 -0800168
Winson Chung80baf5a2010-08-09 16:03:15 -0700169 setVisibility(View.GONE);
170 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700171 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700172 }
173
Winson Chung7da10252010-10-28 16:07:04 -0700174 @Override
175 protected void init() {
176 super.init();
177 mCenterPagesVertically = false;
178 }
179
Winson Chung80baf5a2010-08-09 16:03:15 -0700180 public void setLauncher(Launcher launcher) {
181 Context context = getContext();
182 mLauncher = launcher;
183 mPackageManager = context.getPackageManager();
184 }
185
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700186 /**
187 * Sets the list of applications that launcher has loaded.
188 */
189 public void setApps(ArrayList<ApplicationInfo> list) {
190 mApps = list;
191 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700192
193 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800194 mPageViewIconCache.retainAllApps(list);
195 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700196 }
197
198 /**
199 * Convenience function to add new items to the set of applications that were previously loaded.
200 * Called by both updateApps() and setApps().
201 */
202 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
203 // we add it in place, in alphabetical order
204 final int count = list.size();
205 for (int i = 0; i < count; ++i) {
206 final ApplicationInfo info = list.get(i);
207 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
208 if (index < 0) {
209 mApps.add(-(index + 1), info);
210 }
211 }
212 }
213
214 /**
215 * Adds new applications to the loaded list, and notifies the paged view to update itself.
216 */
217 public void addApps(ArrayList<ApplicationInfo> list) {
218 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700219
220 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800221 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700222 }
223
224 /**
225 * Convenience function to remove items to the set of applications that were previously loaded.
226 * Called by both updateApps() and removeApps().
227 */
228 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
229 // loop through all the apps and remove apps that have the same component
230 final int length = list.size();
231 for (int i = 0; i < length; ++i) {
232 final ApplicationInfo info = list.get(i);
233 int removeIndex = findAppByComponent(mApps, info);
234 if (removeIndex > -1) {
235 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800236 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700237 }
238 }
239 }
240
241 /**
242 * Removes applications from the loaded list, and notifies the paged view to update itself.
243 */
244 public void removeApps(ArrayList<ApplicationInfo> list) {
245 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700246
247 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800248 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700249 }
250
251 /**
252 * Updates a set of applications from the loaded list, and notifies the paged view to update
253 * itself.
254 */
255 public void updateApps(ArrayList<ApplicationInfo> list) {
256 // We remove and re-add the updated applications list because it's properties may have
257 // changed (ie. the title), and this will ensure that the items will be in their proper
258 // place in the list.
259 removeAppsWithoutInvalidate(list);
260 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700261
262 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800263 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700264 }
265
266 /**
267 * Convenience function to find matching ApplicationInfos for removal.
268 */
269 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
270 ComponentName removeComponent = item.intent.getComponent();
271 final int length = list.size();
272 for (int i = 0; i < length; ++i) {
273 ApplicationInfo info = list.get(i);
274 if (info.intent.getComponent().equals(removeComponent)) {
275 return i;
276 }
277 }
278 return -1;
279 }
280
Winson Chung80baf5a2010-08-09 16:03:15 -0700281 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700282 // get the list of widgets
283 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
284 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
285 @Override
286 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
287 return object1.label.compareTo(object2.label);
288 }
289 });
290
291 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
292 @Override
293 public int compare(ResolveInfo object1, ResolveInfo object2) {
294 return object1.loadLabel(mPackageManager).toString().compareTo(
295 object2.loadLabel(mPackageManager).toString());
296 }
297 };
298
Winson Chung80baf5a2010-08-09 16:03:15 -0700299 // get the list of shortcuts
300 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
301 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
302 Collections.sort(mShortcutList, resolveInfoComparator);
303
Winson Chunge8878e32010-09-15 20:37:09 -0700304 // get the list of wallpapers
305 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800306 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
307 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700308 Collections.sort(mWallpaperList, resolveInfoComparator);
309
Winson Chung04998342011-01-05 13:54:43 -0800310 ArrayList<ResolveInfo> retainShortcutList = new ArrayList<ResolveInfo>(mShortcutList);
311 retainShortcutList.addAll(mWallpaperList);
312 mPageViewIconCache.retainAllShortcuts(retainShortcutList);
313 mPageViewIconCache.retainAllAppWidgets(mWidgetList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700314 invalidatePageData();
315 }
316
317 public void setDragController(DragController dragger) {
318 mDragController = dragger;
319 }
320
321 public void setCustomizationFilter(CustomizationType filterType) {
Winson Chung7d1fcbc2011-01-04 10:22:20 -0800322 cancelDragging();
Winson Chung94569f42011-01-17 14:09:17 -0800323 mCustomizationType = filterType;
Winson Chunga12a2502010-12-20 14:41:35 -0800324 if (getChildCount() > 0) {
325 setCurrentPage(0);
326 updateCurrentPageScroll();
327 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700328
Winson Chunga12a2502010-12-20 14:41:35 -0800329 // End the current choice mode so that we don't carry selections across tabs
330 endChoiceMode();
331 }
332 }
333
334 public CustomizationType getCustomizationFilter() {
335 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700336 }
337
Patrick Dubroy5f445422011-02-18 14:35:21 -0800338 /**
339 * Similar to resetCheckedGrandchildren, but allows us to specify that it's not animated.
340 * NOTE: This assumes that only a single item can be checked.
341 */
342 private void resetCheckedItem(boolean animated) {
343 Checkable checkable = getCheckedGrandchildren().get(0);
344 if (checkable instanceof PagedViewWidget) {
345 ((PagedViewWidget) checkable).setChecked(false, animated);
346 } else {
347 ((PagedViewIcon) checkable).setChecked(false, animated);
348 }
349 }
350
351 public void onDropCompleted(View target, Object dragInfo, boolean success) {
352 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
353
354 // Create a view, identical to the drag view, that is only used for animating the
355 // item onto the home screen (or back to its original position, if the drop failed).
356 final int[] pos = new int[2];
357 mDragController.getDragView().getLocationOnScreen(pos);
358 final View animView = dragLayer.createDragView(mDragBitmap, pos[0], pos[1]);
359 animView.setVisibility(View.VISIBLE);
360
361 if (success) {
362 resetCheckedItem(true);
363 animateDropOntoScreen(animView, (ItemInfo) dragInfo, DROP_ANIM_DURATION, 0);
364 } else {
365 // Animate the icon/widget back to its original position
366 animateIntoPosition(animView, mDragViewOrigin[0], mDragViewOrigin[1], new Runnable() {
367 public void run() {
368 resetCheckedItem(false);
369 dragLayer.removeView(animView);
370 }
371 });
372 }
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800373 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800374 mLauncher.unlockScreenOrientation();
Patrick Dubroy5f445422011-02-18 14:35:21 -0800375 mDragBitmap = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700376 }
377
378 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800379 public void onDragViewVisible() {
380 }
381
Patrick Dubroy5f445422011-02-18 14:35:21 -0800382 /**
383 * Animates the given item onto the center of a home screen, and then scales the item to
384 * look as though it's disappearing onto that screen.
385 */
Adam Cohen120980b2010-12-08 11:05:37 -0800386 private void animateItemOntoScreen(View dragView,
387 final CellLayout layout, final ItemInfo info) {
388 mTmpFloatPos[0] = layout.getWidth() / 2;
389 mTmpFloatPos[1] = layout.getHeight() / 2;
390 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
391
Adam Cohen120980b2010-12-08 11:05:37 -0800392 int dragViewWidth = dragView.getMeasuredWidth();
393 int dragViewHeight = dragView.getMeasuredHeight();
394 float heightOffset = 0;
395 float widthOffset = 0;
396
397 if (dragView instanceof ImageView) {
398 Drawable d = ((ImageView) dragView).getDrawable();
399 int width = d.getIntrinsicWidth();
400 int height = d.getIntrinsicHeight();
401
402 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
403 float f = (dragViewWidth / (width * 1.0f));
404 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
405 } else {
406 float f = (dragViewHeight / (height * 1.0f));
407 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
408 }
409 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800410 final float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
411 final float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
Adam Cohen120980b2010-12-08 11:05:37 -0800412
Patrick Dubroy5f445422011-02-18 14:35:21 -0800413 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
414 final View dragCopy = dragLayer.createDragView(dragView);
415 dragCopy.setAlpha(1.0f);
Adam Cohen120980b2010-12-08 11:05:37 -0800416
Patrick Dubroy5f445422011-02-18 14:35:21 -0800417 // Translate the item to the center of the appropriate home screen
418 animateIntoPosition(dragCopy, toX, toY, null);
Adam Cohen120980b2010-12-08 11:05:37 -0800419
Patrick Dubroy5f445422011-02-18 14:35:21 -0800420 // The drop-onto-screen animation begins a bit later, but ends at the same time.
421 final int startDelay = TRANSLATE_ANIM_DURATION - DROP_ANIM_DURATION;
422
423 // Scale down the icon and fade out the alpha
424 animateDropOntoScreen(dragCopy, info, DROP_ANIM_DURATION, startDelay);
425 }
Adam Cohen120980b2010-12-08 11:05:37 -0800426
Patrick Dubroy5f445422011-02-18 14:35:21 -0800427 /**
428 * Animation which scales the view down and animates its alpha, making it appear to disappear
429 * onto a home screen.
430 */
431 private void animateDropOntoScreen(
432 final View view, final ItemInfo info, int duration, int delay) {
433 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
434 final CellLayout layout = mLauncher.getWorkspace().getCurrentDropLayout();
435
436 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
Adam Cohen120980b2010-12-08 11:05:37 -0800437 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
438 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
439 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
Patrick Dubroy5f445422011-02-18 14:35:21 -0800440 anim.setInterpolator(new LinearInterpolator());
441 if (delay > 0) {
442 anim.setStartDelay(delay);
443 }
444 anim.setDuration(duration);
445 anim.addListener(new AnimatorListenerAdapter() {
446 public void onAnimationEnd(Animator animation) {
447 dragLayer.removeView(view);
448 mLauncher.addExternalItemToScreen(info, layout);
449 }
450 });
451 anim.start();
452 }
Adam Cohen120980b2010-12-08 11:05:37 -0800453
Patrick Dubroy5f445422011-02-18 14:35:21 -0800454 /**
455 * Animates the x,y position of the view, and optionally execute a Runnable on animation end.
456 */
457 private void animateIntoPosition(
458 View view, float toX, float toY, final Runnable endRunnable) {
459 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,
460 PropertyValuesHolder.ofFloat("x", toX),
461 PropertyValuesHolder.ofFloat("y", toY));
462 anim.setInterpolator(mQuintEaseOutInterpolator);
463 anim.setDuration(TRANSLATE_ANIM_DURATION);
464 if (endRunnable != null) {
465 anim.addListener(new AnimatorListenerAdapter() {
466 @Override
467 public void onAnimationEnd(Animator animation) {
468 endRunnable.run();
469 }
470 });
471 }
472 anim.start();
Adam Cohen120980b2010-12-08 11:05:37 -0800473 }
474
Patrick Dubroya669d792010-11-23 14:40:33 -0800475 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800476 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800477 // Return early if this is not initiated from a touch
478 if (!v.isInTouchMode()) return;
479 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800480 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700481
Winson Chungd0d43012010-09-26 17:26:45 -0700482 // On certain pages, we allow single tap to mark items as selected so that they can be
483 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700484 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700485 switch (mCustomizationType) {
486 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700487 mChoiceModeTitleText = R.string.cab_widget_selection_text;
488 enterChoiceMode = true;
489 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700490 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700491 mChoiceModeTitleText = R.string.cab_app_selection_text;
492 enterChoiceMode = true;
493 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700494 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700495 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
496 enterChoiceMode = true;
497 break;
498 default:
499 break;
500 }
Winson Chungd0d43012010-09-26 17:26:45 -0700501
Michael Jurka3125d9d2010-09-27 11:30:20 -0700502 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700503 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700504
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700505 Workspace w = mLauncher.getWorkspace();
506 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
507 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800508 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700509
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700510 animateClickFeedback(v, new Runnable() {
511 @Override
512 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800513 cl.calculateSpans(itemInfo);
514 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
515 animateItemOntoScreen(dragView, cl, itemInfo);
516 } else {
517 mLauncher.showOutOfSpaceMessage();
518 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700519 }
520 });
Winson Chungd0d43012010-09-26 17:26:45 -0700521 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700522 }
523
524 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700525 switch (mCustomizationType) {
526 case WallpaperCustomization:
527 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700528 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700529 animateClickFeedback(v, new Runnable() {
530 @Override
531 public void run() {
532 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700533 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700534 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
535 ComponentName name = new ComponentName(info.activityInfo.packageName,
536 info.activityInfo.name);
537 createWallpapersIntent.setComponent(name);
538 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700539 }
540 });
Winson Chungd0d43012010-09-26 17:26:45 -0700541 break;
542 default:
543 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700544 }
545 }
546
Patrick Dubroy5f445422011-02-18 14:35:21 -0800547 private Bitmap drawableToBitmap(Drawable d) {
548 final Rect bounds = d.getBounds();
549 final int w = bounds.width();
550 final int h = bounds.height();
Michael Jurkac4e772e2011-02-10 13:32:01 -0800551 Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
552 renderDrawableToBitmap(d, b, 0, 0, w, h);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800553 return b;
554 }
555
Adam Cohen120980b2010-12-08 11:05:37 -0800556 private View getDragView(View v) {
557 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
558 v.findViewById(R.id.widget_preview) : v;
559 }
560
Michael Jurka72b079e2010-12-10 01:03:53 -0800561 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800562 if (!v.isInTouchMode()) return false;
563 if (!super.beginDragging(v)) return false;
564
Winson Chungd0d43012010-09-26 17:26:45 -0700565 // End the current choice mode before we start dragging anything
566 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
567 endChoiceMode();
568 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800569 final Workspace workspace = mLauncher.getWorkspace();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800570 boolean result = false;
Winson Chung400438b2011-01-16 17:53:48 -0800571 mLauncher.lockScreenOrientation();
Winson Chung80baf5a2010-08-09 16:03:15 -0700572 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800573 case WidgetCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800574 if (v instanceof PagedViewWidget) {
575 // Get the widget preview as the drag representation
576 final LinearLayout l = (LinearLayout) v;
577 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800578 mDragBitmap = drawableToBitmap(i.getDrawable());
579 i.getLocationOnScreen(mDragViewOrigin);
Winson Chung94569f42011-01-17 14:09:17 -0800580 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700581
Patrick Dubroy5f445422011-02-18 14:35:21 -0800582 int[] spanXY = CellLayout.rectToCell(getResources(),
583 createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
Winson Chung94569f42011-01-17 14:09:17 -0800584 createWidgetInfo.spanX = spanXY[0];
585 createWidgetInfo.spanY = spanXY[1];
Patrick Dubroy5f445422011-02-18 14:35:21 -0800586 workspace.onDragStartedWithItemSpans(spanXY[0], spanXY[1], mDragBitmap);
587 mDragController.startDrag(i, mDragBitmap, this, createWidgetInfo,
588 DragController.DRAG_ACTION_COPY, null);
Winson Chung94569f42011-01-17 14:09:17 -0800589 result = true;
590 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800591 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800592 }
Patrick Dubroy5f445422011-02-18 14:35:21 -0800593 case ShortcutCustomization:
Michael Jurkad3ef3062010-11-23 16:23:58 -0800594 case ApplicationCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800595 if (v instanceof PagedViewIcon) {
Winson Chung94569f42011-01-17 14:09:17 -0800596 // get icon (top compound drawable, index is 1)
597 final TextView tv = (TextView) v;
598 final Drawable icon = tv.getCompoundDrawables()[1];
Patrick Dubroy5f445422011-02-18 14:35:21 -0800599 mDragBitmap = drawableToBitmap(icon);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700600
Patrick Dubroy5f445422011-02-18 14:35:21 -0800601 Object dragInfo = v.getTag();
602 if (mCustomizationType == CustomizationType.ApplicationCustomization) {
603 // TODO: Not sure why we have to copy this
604 dragInfo = new ApplicationInfo((ApplicationInfo) dragInfo);
605 }
606 workspace.onDragStartedWithItemSpans(1, 1, mDragBitmap);
607
608 // Calculate where to place the drag view in order to align the icon pixels with
609 // the original view.
610 v.getLocationOnScreen(mDragViewOrigin);
611 mDragViewOrigin[0] += (v.getWidth() - icon.getIntrinsicWidth()) / 2;
612 mDragViewOrigin[1] += v.getPaddingTop();
613
614 mDragController.startDrag(mDragBitmap, mDragViewOrigin[0], mDragViewOrigin[1],
615 this, dragInfo, DragController.DRAG_ACTION_COPY);
Winson Chung94569f42011-01-17 14:09:17 -0800616 result = true;
617 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800618 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700619 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800620 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800621
622 // We toggle the checked state _after_ we create the view for the drag in case toggling the
623 // checked state changes the view's look
Winson Chung94569f42011-01-17 14:09:17 -0800624 if (result && (v instanceof Checkable)) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800625 // In preparation for drag, we always reset the checked grand children regardless of
626 // what choice mode we are in
627 resetCheckedGrandchildren();
628
629 // Toggle the selection on the dragged app
630 Checkable checkable = (Checkable) v;
631
632 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
633 // of the drag of the item. (The fade-in will occur when all checked states are
634 // disabled when dragging ends)
635 checkable.toggle();
636 }
637
638 return result;
Winson Chung80baf5a2010-08-09 16:03:15 -0700639 }
640
Winson Chunge3193b92010-09-10 11:44:42 -0700641 /**
642 * Pre-processes the layout of the different widget pages.
643 * @return the number of pages of widgets that we have
644 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700645 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700646 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700647
Winson Chunge3193b92010-09-10 11:44:42 -0700648 // create a new page for the first set of widgets
649 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700650 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700651 mWidgetPages.add(newPage);
652
653 // do this until we have no more widgets to lay out
654 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
655 final int widgetCount = mWidgetList.size();
656 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700657 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700658 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700659
Winson Chunge3193b92010-09-10 11:44:42 -0700660 // determine the size of the current widget
661 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
662 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700663
Winson Chunge3193b92010-09-10 11:44:42 -0700664 // create a new page if necessary
665 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
666 numCellsInRow = 0;
667 newPage = new ArrayList<AppWidgetProviderInfo>();
668 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700669 }
670
Winson Chunge3193b92010-09-10 11:44:42 -0700671 // add the item to the current page
672 newPage.add(info);
673 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700674 }
Winson Chunge3193b92010-09-10 11:44:42 -0700675
Winson Chung80baf5a2010-08-09 16:03:15 -0700676 return mWidgetPages.size();
677 }
678
Winson Chunge3193b92010-09-10 11:44:42 -0700679 /**
Winson Chung7da10252010-10-28 16:07:04 -0700680 * Helper function to draw a drawable to the specified canvas with the specified bounds.
681 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800682 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700683 if (bitmap != null) mCanvas.setBitmap(bitmap);
684 mCanvas.save();
Patrick Dubroy5f445422011-02-18 14:35:21 -0800685 final Rect oldBounds = d.copyBounds();
686 d.setBounds(x, y, x + w, y + h);
Winson Chung7da10252010-10-28 16:07:04 -0700687 d.draw(mCanvas);
Patrick Dubroy5f445422011-02-18 14:35:21 -0800688 d.setBounds(oldBounds); // Restore the bounds
Winson Chung7da10252010-10-28 16:07:04 -0700689 mCanvas.restore();
690 }
691
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800692 /*
693 * This method fetches an xml file specified in the manifest identified by
694 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
695 * an image which will be used as the wallpaper preview for an activity
696 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
697 * in the customize drawer.
698 */
699 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800700 ActivityInfo activityInfo = ri.activityInfo;
701 XmlResourceParser parser = null;
702 try {
703 parser = activityInfo.loadXmlMetaData(mPackageManager,
704 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
705 if (parser == null) {
706 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
707 + "wallpaper provider '" + component + '\'');
708 return null;
709 }
710
711 AttributeSet attrs = Xml.asAttributeSet(parser);
712
713 int type;
714 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
715 && type != XmlPullParser.START_TAG) {
716 // drain whitespace, comments, etc.
717 }
718
719 String nodeName = parser.getName();
720 if (!"wallpaper-preview".equals(nodeName)) {
721 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
722 + "wallpaper provider '" + component + '\'');
723 return null;
724 }
725
726 // If metaData was null, we would have returned earlier when getting
727 // the parser No need to do the check here
728 Resources res = mPackageManager.getResourcesForApplication(
729 activityInfo.applicationInfo);
730
731 TypedArray sa = res.obtainAttributes(attrs,
732 com.android.internal.R.styleable.WallpaperPreviewInfo);
733
734 TypedValue value = sa.peekValue(
735 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
736 if (value == null) return null;
737
738 return res.getDrawable(value.resourceId);
739 } catch (Exception e) {
740 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
741 return null;
742 } finally {
743 if (parser != null) parser.close();
744 }
745 }
746
Winson Chung7da10252010-10-28 16:07:04 -0700747 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800748 * This method will extract the preview image specified by the wallpaper source provider (if it
749 * exists) otherwise, it will try to generate a default image preview.
750 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800751 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800752 // To be implemented later: resolving the up-to-date wallpaper thumbnail
753
754 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
755 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
756 Resources resources = mLauncher.getResources();
757
758 // Create a new bitmap to hold the widget preview
759 int width = (int) (dim * sScaleFactor);
760 int height = (int) (dim * sScaleFactor);
761 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800762
763 Drawable background = parseWallpaperPreviewXml(
764 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
765 boolean foundCustomDrawable = background != null;
766
767 if (!foundCustomDrawable) {
768 background = resources.getDrawable(R.drawable.default_widget_preview);
769 }
770
Winson Chung45e1d6e2010-11-09 17:19:49 -0800771 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
772
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800773 // If we don't have a custom icon, we use the app icon on the default background
774 if (!foundCustomDrawable) {
775 try {
776 final IconCache iconCache =
777 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
778 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
779 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800780
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800781 final int iconSize = minDim / 2;
782 final int offset = iconSize / 4;
783 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
784 } catch (Resources.NotFoundException e) {
785 // if we can't find the icon, then just don't draw it
786 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800787 }
788
Winson Chung29d6fea2010-12-01 15:47:31 -0800789 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800790 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
791 return drawable;
792 }
793
794 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700795 * This method will extract the preview image specified by the widget developer (if it exists),
796 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800797 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700798 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800799 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800800 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700801 String packageName = info.provider.getPackageName();
802 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800803 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700804 if (info.previewImage != 0) {
805 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
806 if (drawable == null) {
807 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
808 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700809 }
810 }
811
812 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700813 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
814 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700815 if (drawable == null) {
816 Resources resources = mLauncher.getResources();
817
Winson Chung80baf5a2010-08-09 16:03:15 -0700818 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700819 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
820 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700821 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
822 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
823 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700824
Winson Chung45e1d6e2010-11-09 17:19:49 -0800825 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700826 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700827 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700828 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700829 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700830 }
831 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700832 icon = resources.getDrawable(R.drawable.ic_launcher_application);
833 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700834
Winson Chunge3193b92010-09-10 11:44:42 -0700835 final int iconSize = minDim / 2;
836 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800837 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700838 } catch (Resources.NotFoundException e) {
839 // if we can't find the icon, then just don't draw it
840 }
841
Winson Chung29d6fea2010-12-01 15:47:31 -0800842 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700843 } else {
844 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800845 final float imageWidth = drawable.getIntrinsicWidth();
846 final float imageHeight = drawable.getIntrinsicHeight();
847 final float aspect = (float) imageWidth / imageHeight;
848 final int scaledWidth =
849 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
850 final int scaledHeight =
851 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700852 int width;
853 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800854 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700855 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800856 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700857 } else {
858 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800859 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700860 }
861
862 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
863 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
864
Winson Chung29d6fea2010-12-01 15:47:31 -0800865 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700866 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800867 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
868 newDrawable.getIntrinsicHeight());
869 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700870 }
871
872 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700873 layout.setCellCount(mCellCountX, mCellCountY);
874 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
875 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700876 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700877 }
878
Winson Chunge3193b92010-09-10 11:44:42 -0700879 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700880 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700881 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
882
883 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
884 }
885
Winson Chung80baf5a2010-08-09 16:03:15 -0700886 private void syncWidgetPages() {
887 if (mWidgetList == null) return;
888
Winson Chunge3193b92010-09-10 11:44:42 -0700889 // we need to repopulate with the LinearLayout layout for the widget pages
890 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700891 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700892 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800893 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700894 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700895 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
896 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700897
Winson Chunge22a8e92010-11-12 13:40:58 -0800898 addView(layout, new LinearLayout.LayoutParams(
899 LinearLayout.LayoutParams.WRAP_CONTENT,
900 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700901 }
902 }
903
904 private void syncWidgetPageItems(int page) {
905 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700906 LinearLayout layout = (LinearLayout) getChildAt(page);
907 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700908 final int count = list.size();
Winson Chung04998342011-01-05 13:54:43 -0800909 final int numPages = getPageCount();
Winson Chung80baf5a2010-08-09 16:03:15 -0700910 layout.removeAllViews();
911 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700912 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
913 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800914 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
915 info.minHeight, null);
916 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700917
Winson Chung29d6fea2010-12-01 15:47:31 -0800918 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700919 R.layout.customize_paged_view_widget, layout, false);
Winson Chung04998342011-01-05 13:54:43 -0800920 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans,
921 mPageViewIconCache, (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -0700922 l.setTag(createItemInfo);
923 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800924 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700925 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700926
Winson Chunge3193b92010-09-10 11:44:42 -0700927 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700928 }
929 }
930
Winson Chung45e1d6e2010-11-09 17:19:49 -0800931 private void syncWallpaperPages() {
932 if (mWallpaperList == null) return;
933
934 // We need to repopulate the LinearLayout for the wallpaper pages
935 removeAllViews();
936 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800937 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800938 for (int i = 0; i < numPages; ++i) {
939 LinearLayout layout = new PagedViewExtendedLayout(getContext());
940 layout.setGravity(Gravity.CENTER_HORIZONTAL);
941 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
942 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
943
Winson Chunge22a8e92010-11-12 13:40:58 -0800944 addView(layout, new LinearLayout.LayoutParams(
945 LinearLayout.LayoutParams.WRAP_CONTENT,
946 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800947 }
948 }
949
950 private void syncWallpaperPageItems(int page) {
951 // Load the items on to the pages
952 LinearLayout layout = (LinearLayout) getChildAt(page);
953 layout.removeAllViews();
954 final int count = mWallpaperList.size();
Winson Chung04998342011-01-05 13:54:43 -0800955 final int numPages = getPageCount();
Winson Chung78bd53c2010-12-09 13:50:24 -0800956 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -0800957 final int startIndex = page * numItemsPerPage;
958 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
959 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800960 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800961 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800962
Winson Chung29d6fea2010-12-01 15:47:31 -0800963 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800964 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung04998342011-01-05 13:54:43 -0800965 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth,
966 mPageViewIconCache, (numPages > 1));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800967 l.setTag(info);
968 l.setOnClickListener(this);
969
Winson Chung45e1d6e2010-11-09 17:19:49 -0800970 layout.addView(l);
971 }
972 }
973
Winson Chung80baf5a2010-08-09 16:03:15 -0700974 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700975 // we need to repopulate with PagedViewCellLayouts
976 removeAllViews();
977
Winson Chung80baf5a2010-08-09 16:03:15 -0700978 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700979 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700980 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700981 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
982 setupPage(layout);
983 addView(layout);
984 }
985 }
986
987 private void syncListPageItems(int page, List<ResolveInfo> list) {
988 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -0800989 final int numPages = getPageCount();
990 final int numCells = mCellCountX * mCellCountY;
991 final int startIndex = page * numCells;
992 final int endIndex = Math.min(startIndex + numCells, list.size());
993 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700994 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -0800995 layout.removeAllViewsOnPage();
Winson Chung80baf5a2010-08-09 16:03:15 -0700996 for (int i = startIndex; i < endIndex; ++i) {
997 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700998 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
999
Winson Chung241c3b42010-08-25 16:53:03 -07001000 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1001 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -07001002 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Winson Chung04998342011-01-05 13:54:43 -08001003 ((LauncherApplication) mLauncher.getApplication()).getIconCache(),
1004 (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -07001005 switch (mCustomizationType) {
1006 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -07001007 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001008 break;
Winson Chungd0d43012010-09-26 17:26:45 -07001009 case ShortcutCustomization:
1010 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1011 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1012 info.activityInfo.name);
1013 icon.setTag(createItemInfo);
1014 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001015 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001016 icon.setOnLongClickListener(this);
1017 break;
1018 default:
1019 break;
Winson Chunge8878e32010-09-15 20:37:09 -07001020 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001021
1022 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001023 final int x = index % mCellCountX;
1024 final int y = index / mCellCountX;
1025 setupPage(layout);
1026 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
1027 }
1028 }
1029
1030 private void syncAppPages() {
1031 if (mApps == null) return;
1032
1033 // We need to repopulate with PagedViewCellLayouts
1034 removeAllViews();
1035
1036 // Ensure that we have the right number of pages
1037 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
1038 for (int i = 0; i < numPages; ++i) {
1039 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1040 setupPage(layout);
1041 addView(layout);
1042 }
1043 }
1044
1045 private void syncAppPageItems(int page) {
1046 if (mApps == null) return;
1047
1048 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -08001049 final int numPages = getPageCount();
1050 final int numCells = mCellCountX * mCellCountY;
1051 final int startIndex = page * numCells;
1052 final int endIndex = Math.min(startIndex + numCells, mApps.size());
1053 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001054 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -08001055 layout.removeAllViewsOnPage();
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001056 for (int i = startIndex; i < endIndex; ++i) {
1057 final ApplicationInfo info = mApps.get(i);
1058 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1059 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung04998342011-01-05 13:54:43 -08001060 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -07001061 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001062 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001063 icon.setOnLongClickListener(this);
1064
1065 final int index = i - startIndex;
1066 final int x = index % mCellCountX;
1067 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001068 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -07001069 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001070 }
1071 }
1072
Winson Chung80baf5a2010-08-09 16:03:15 -07001073 @Override
1074 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -07001075 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001076 switch (mCustomizationType) {
1077 case WidgetCustomization:
1078 syncWidgetPages();
1079 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001080 case ShortcutCustomization:
1081 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001082 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001083 break;
1084 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001085 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -07001086 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001087 case ApplicationCustomization:
1088 syncAppPages();
1089 centerPagedViewCellLayouts = false;
1090 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001091 default:
1092 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001093 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001094 break;
1095 }
1096
1097 // only try and center the page if there is one page
1098 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001099 if (centerPagedViewCellLayouts) {
1100 if (childCount == 1) {
1101 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1102 layout.enableCenteredContent(true);
1103 } else {
1104 for (int i = 0; i < childCount; ++i) {
1105 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1106 layout.enableCenteredContent(false);
1107 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001108 }
1109 }
1110
1111 // bound the current page
Winson Chung03929772011-02-23 17:07:10 -08001112 requestLayout();
1113 post(new Runnable() {
1114 @Override
1115 public void run() {
1116 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
1117 forceUpdateAdjacentPagesAlpha();
1118 }
1119 });
Winson Chung80baf5a2010-08-09 16:03:15 -07001120 }
1121
1122 @Override
1123 public void syncPageItems(int page) {
1124 switch (mCustomizationType) {
1125 case WidgetCustomization:
1126 syncWidgetPageItems(page);
1127 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001128 case ShortcutCustomization:
1129 syncListPageItems(page, mShortcutList);
1130 break;
1131 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001132 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001133 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001134 case ApplicationCustomization:
1135 syncAppPageItems(page);
1136 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001137 }
1138 }
Winson Chunge3193b92010-09-10 11:44:42 -07001139
Winson Chungd0d43012010-09-26 17:26:45 -07001140 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001141 protected int getAssociatedLowerPageBound(int page) {
1142 return 0;
1143 }
Winson Chungd0d43012010-09-26 17:26:45 -07001144 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001145 protected int getAssociatedUpperPageBound(int page) {
1146 return getChildCount();
1147 }
Winson Chungd0d43012010-09-26 17:26:45 -07001148
1149 @Override
1150 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001151 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001152 return true;
1153 }
1154
1155 @Override
1156 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1157 return true;
1158 }
1159
1160 @Override
1161 public void onDestroyActionMode(ActionMode mode) {
1162 endChoiceMode();
1163 }
1164
1165 @Override
1166 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1167 return false;
1168 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001169}