blob: 694d4d01518d1640d59a4eb3cfb21524c13deb8a [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
Adam Cohen120980b2010-12-08 11:05:37 -080019import java.util.ArrayList;
20import java.util.Collections;
21import java.util.Comparator;
22import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070023
Adam Cohen7b9d3a62010-12-07 21:49:34 -080024import org.xmlpull.v1.XmlPullParser;
25
Adam Cohen120980b2010-12-08 11:05:37 -080026import android.animation.Animator;
Winson Chungcd4bc492010-12-09 18:52:32 -080027import android.animation.Animator.AnimatorListener;
Adam Cohen120980b2010-12-08 11:05:37 -080028import android.animation.ObjectAnimator;
29import android.animation.PropertyValuesHolder;
30import android.animation.TimeInterpolator;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080031import android.app.WallpaperManager;
Winson Chung80baf5a2010-08-09 16:03:15 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
34import android.content.ComponentName;
35import android.content.Context;
36import android.content.Intent;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080037import android.content.pm.ActivityInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -070038import android.content.pm.PackageManager;
39import android.content.pm.ResolveInfo;
40import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070041import android.content.res.TypedArray;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080042import android.content.res.XmlResourceParser;
Winson Chung80baf5a2010-08-09 16:03:15 -070043import android.graphics.Bitmap;
Michael Jurkaaf91de02010-11-23 16:23:58 -080044import android.graphics.Bitmap.Config;
Winson Chungcd4bc492010-12-09 18:52:32 -080045import android.graphics.Canvas;
46import android.graphics.Color;
Winson Chung80baf5a2010-08-09 16:03:15 -070047import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070048import android.util.AttributeSet;
49import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080050import android.util.Slog;
51import android.util.TypedValue;
52import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070053import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070054import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070055import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070056import android.view.Menu;
57import android.view.MenuItem;
Michael Jurka7426c422010-11-11 15:23:47 -080058import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070059import android.view.View;
Adam Cohen120980b2010-12-08 11:05:37 -080060import android.view.animation.DecelerateInterpolator;
61import android.view.animation.Interpolator;
Winson Chunge3193b92010-09-10 11:44:42 -070062import android.widget.ImageView;
63import android.widget.LinearLayout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080064import android.widget.TextView;
Winson Chung80baf5a2010-08-09 16:03:15 -070065
Adam Cohen120980b2010-12-08 11:05:37 -080066import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070067
Adam Cohen7b9d3a62010-12-07 21:49:34 -080068
Winson Chung80baf5a2010-08-09 16:03:15 -070069public class CustomizePagedView extends PagedView
Michael Jurka7426c422010-11-11 15:23:47 -080070 implements View.OnLongClickListener, View.OnClickListener, View.OnTouchListener,
Winson Chungd0d43012010-09-26 17:26:45 -070071 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070072
73 public enum CustomizationType {
74 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070075 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070076 WallpaperCustomization,
77 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070078 }
79
80 private static final String TAG = "CustomizeWorkspace";
81 private static final boolean DEBUG = false;
82
Michael Jurka7426c422010-11-11 15:23:47 -080083 private View mLastTouchedItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070084 private Launcher mLauncher;
85 private DragController mDragController;
86 private PackageManager mPackageManager;
Michael Jurka7426c422010-11-11 15:23:47 -080087 private boolean mIsDragging;
88 private float mDragSlopeThreshold;
Winson Chung80baf5a2010-08-09 16:03:15 -070089
90 private CustomizationType mCustomizationType;
91
Winson Chunge3193b92010-09-10 11:44:42 -070092 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
93 private PagedViewCellLayout mWorkspaceWidgetLayout;
94
95 // The mapping between the pages and the widgets that will be laid out on them
96 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
97
Winson Chung45e1d6e2010-11-09 17:19:49 -080098 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070099 private int mMaxWidgetWidth;
100
Winson Chung45e1d6e2010-11-09 17:19:49 -0800101 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -0700102 private int mMaxWidgetsCellHSpan;
103
Winson Chung45e1d6e2010-11-09 17:19:49 -0800104 // The size of the items on the wallpaper tab
105 private int mWallpaperCellHSpan;
106
Winson Chung78bd53c2010-12-09 13:50:24 -0800107 // The max number of wallpaper cells to take a "page" of wallpaper items
108 private int mMaxWallpaperCellHSpan;
109
Winson Chunge3193b92010-09-10 11:44:42 -0700110 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700111 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700112 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700113 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700114 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700115
Winson Chunge3193b92010-09-10 11:44:42 -0700116 private static final int sMinWidgetCellHSpan = 2;
117 private static final int sMaxWidgetCellHSpan = 4;
118
Michael Jurka3125d9d2010-09-27 11:30:20 -0700119 private int mChoiceModeTitleText;
120
Winson Chunge3193b92010-09-10 11:44:42 -0700121 // The scale factor for widget previews inside the widget drawer
122 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700123
124 private final Canvas mCanvas = new Canvas();
125 private final LayoutInflater mInflater;
126
Adam Cohen120980b2010-12-08 11:05:37 -0800127 private final float mTmpFloatPos[] = new float[2];
128 private final float ANIMATION_SCALE = 0.5f;
129 private final int ANIMATION_DURATION = 400;
130 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
131 private ScaleAlphaInterpolator mScaleAlphaInterpolator = new ScaleAlphaInterpolator();
132
Winson Chung80baf5a2010-08-09 16:03:15 -0700133 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700134 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700135 }
136
137 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700138 this(context, attrs, 0);
139 }
140
141 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
142 super(context, attrs, defStyle);
143
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700144 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800145 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
146 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800147 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700148 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
149 a.recycle();
150 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
151 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
152 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700153 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800154
Winson Chung80baf5a2010-08-09 16:03:15 -0700155 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700156 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
157 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700158 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700159
Michael Jurka7426c422010-11-11 15:23:47 -0800160 final Resources r = context.getResources();
161 mDragSlopeThreshold =
162 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f;
163
Winson Chung80baf5a2010-08-09 16:03:15 -0700164 setVisibility(View.GONE);
165 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700166 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700167 }
168
Winson Chung7da10252010-10-28 16:07:04 -0700169 @Override
170 protected void init() {
171 super.init();
172 mCenterPagesVertically = false;
173 }
174
Winson Chung80baf5a2010-08-09 16:03:15 -0700175 public void setLauncher(Launcher launcher) {
176 Context context = getContext();
177 mLauncher = launcher;
178 mPackageManager = context.getPackageManager();
179 }
180
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700181 /**
182 * Sets the list of applications that launcher has loaded.
183 */
184 public void setApps(ArrayList<ApplicationInfo> list) {
185 mApps = list;
186 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700187
188 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700189 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700190 }
191
192 /**
193 * Convenience function to add new items to the set of applications that were previously loaded.
194 * Called by both updateApps() and setApps().
195 */
196 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
197 // we add it in place, in alphabetical order
198 final int count = list.size();
199 for (int i = 0; i < count; ++i) {
200 final ApplicationInfo info = list.get(i);
201 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
202 if (index < 0) {
203 mApps.add(-(index + 1), info);
204 }
205 }
206 }
207
208 /**
209 * Adds new applications to the loaded list, and notifies the paged view to update itself.
210 */
211 public void addApps(ArrayList<ApplicationInfo> list) {
212 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700213
214 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700215 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700216 }
217
218 /**
219 * Convenience function to remove items to the set of applications that were previously loaded.
220 * Called by both updateApps() and removeApps().
221 */
222 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
223 // loop through all the apps and remove apps that have the same component
224 final int length = list.size();
225 for (int i = 0; i < length; ++i) {
226 final ApplicationInfo info = list.get(i);
227 int removeIndex = findAppByComponent(mApps, info);
228 if (removeIndex > -1) {
229 mApps.remove(removeIndex);
230 mPageViewIconCache.removeOutline(info);
231 }
232 }
233 }
234
235 /**
236 * Removes applications from the loaded list, and notifies the paged view to update itself.
237 */
238 public void removeApps(ArrayList<ApplicationInfo> list) {
239 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700240
241 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700242 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700243 }
244
245 /**
246 * Updates a set of applications from the loaded list, and notifies the paged view to update
247 * itself.
248 */
249 public void updateApps(ArrayList<ApplicationInfo> list) {
250 // We remove and re-add the updated applications list because it's properties may have
251 // changed (ie. the title), and this will ensure that the items will be in their proper
252 // place in the list.
253 removeAppsWithoutInvalidate(list);
254 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700255
256 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700257 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700258 }
259
260 /**
261 * Convenience function to find matching ApplicationInfos for removal.
262 */
263 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
264 ComponentName removeComponent = item.intent.getComponent();
265 final int length = list.size();
266 for (int i = 0; i < length; ++i) {
267 ApplicationInfo info = list.get(i);
268 if (info.intent.getComponent().equals(removeComponent)) {
269 return i;
270 }
271 }
272 return -1;
273 }
274
Winson Chung80baf5a2010-08-09 16:03:15 -0700275 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700276 // get the list of widgets
277 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
278 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
279 @Override
280 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
281 return object1.label.compareTo(object2.label);
282 }
283 });
284
285 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
286 @Override
287 public int compare(ResolveInfo object1, ResolveInfo object2) {
288 return object1.loadLabel(mPackageManager).toString().compareTo(
289 object2.loadLabel(mPackageManager).toString());
290 }
291 };
292
Winson Chung80baf5a2010-08-09 16:03:15 -0700293 // get the list of shortcuts
294 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
295 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
296 Collections.sort(mShortcutList, resolveInfoComparator);
297
Winson Chunge8878e32010-09-15 20:37:09 -0700298 // get the list of wallpapers
299 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800300 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
301 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700302 Collections.sort(mWallpaperList, resolveInfoComparator);
303
Winson Chung10fefb12010-11-01 11:57:06 -0700304 invalidatePageDataAndIconCache();
305 }
306
307 private void invalidatePageDataAndIconCache() {
308 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700309 mPageViewIconCache.clear();
310
Winson Chunge3193b92010-09-10 11:44:42 -0700311 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700312 invalidatePageData();
313 }
314
315 public void setDragController(DragController dragger) {
316 mDragController = dragger;
317 }
318
319 public void setCustomizationFilter(CustomizationType filterType) {
320 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700321 setCurrentPage(0);
Winson Chungbbc60d82010-11-11 16:34:41 -0800322 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700323 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700324
325 // End the current choice mode so that we don't carry selections across tabs
326 endChoiceMode();
Winson Chung649a4ca2010-11-18 10:38:13 -0800327 // Reset the touch item (if we are mid-dragging)
328 mLastTouchedItem = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700329 }
330
331 @Override
332 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700333 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700334 }
335
336 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800337 public void onDragViewVisible() {
338 }
339
Adam Cohen120980b2010-12-08 11:05:37 -0800340 class ScaleAlphaInterpolator implements Interpolator {
341 public float getInterpolation(float input) {
342 float pivot = 0.5f;
343 if (input < pivot) {
344 return 0;
345 } else {
346 return (input - pivot)/(1 - pivot);
347 }
348 }
349 }
350
351 private void animateItemOntoScreen(View dragView,
352 final CellLayout layout, final ItemInfo info) {
353 mTmpFloatPos[0] = layout.getWidth() / 2;
354 mTmpFloatPos[1] = layout.getHeight() / 2;
355 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
356
357 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
358 final View dragCopy = dragLayer.createDragView(dragView);
359 dragCopy.setAlpha(1.0f);
360
361 int dragViewWidth = dragView.getMeasuredWidth();
362 int dragViewHeight = dragView.getMeasuredHeight();
363 float heightOffset = 0;
364 float widthOffset = 0;
365
366 if (dragView instanceof ImageView) {
367 Drawable d = ((ImageView) dragView).getDrawable();
368 int width = d.getIntrinsicWidth();
369 int height = d.getIntrinsicHeight();
370
371 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
372 float f = (dragViewWidth / (width * 1.0f));
373 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
374 } else {
375 float f = (dragViewHeight / (height * 1.0f));
376 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
377 }
378 }
379
380 float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
381 float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
382
383 ObjectAnimator posAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
384 PropertyValuesHolder.ofFloat("x", toX),
385 PropertyValuesHolder.ofFloat("y", toY));
386 posAnim.setInterpolator(mQuintEaseOutInterpolator);
387 posAnim.setDuration(ANIMATION_DURATION);
388
389 posAnim.addListener(new AnimatorListener() {
390 public void onAnimationCancel(Animator animation) {}
391 public void onAnimationRepeat(Animator animation) {}
392 public void onAnimationStart(Animator animation) {}
393
394 public void onAnimationEnd(Animator animation) {
395 dragLayer.removeView(dragCopy);
396 mLauncher.addExternalItemToScreen(info, layout);
397 post(new Runnable() {
398 public void run() {
399 layout.animateDrop();
400 }
401 });
402 }
403 });
404
405 ObjectAnimator scaleAlphaAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
406 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
407 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
408 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
409 scaleAlphaAnim.setInterpolator(mScaleAlphaInterpolator);
410 scaleAlphaAnim.setDuration(ANIMATION_DURATION);
411
412 posAnim.start();
413 scaleAlphaAnim.start();
414 }
415
Patrick Dubroya669d792010-11-23 14:40:33 -0800416 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800417 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800418 // Return early if this is not initiated from a touch
419 if (!v.isInTouchMode()) return;
420 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800421 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700422
Winson Chungd0d43012010-09-26 17:26:45 -0700423 // On certain pages, we allow single tap to mark items as selected so that they can be
424 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700425 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700426 switch (mCustomizationType) {
427 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700428 mChoiceModeTitleText = R.string.cab_widget_selection_text;
429 enterChoiceMode = true;
430 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700431 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700432 mChoiceModeTitleText = R.string.cab_app_selection_text;
433 enterChoiceMode = true;
434 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700435 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700436 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
437 enterChoiceMode = true;
438 break;
439 default:
440 break;
441 }
Winson Chungd0d43012010-09-26 17:26:45 -0700442
Michael Jurka3125d9d2010-09-27 11:30:20 -0700443 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700444 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700445
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700446 Workspace w = mLauncher.getWorkspace();
447 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
448 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800449 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700450
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700451 animateClickFeedback(v, new Runnable() {
452 @Override
453 public void run() {
Adam Cohen120980b2010-12-08 11:05:37 -0800454 animateItemOntoScreen(dragView, cl, itemInfo);
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700455 }
456 });
Winson Chungd0d43012010-09-26 17:26:45 -0700457 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700458 }
459
460 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700461 switch (mCustomizationType) {
462 case WallpaperCustomization:
463 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700464 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700465 animateClickFeedback(v, new Runnable() {
466 @Override
467 public void run() {
468 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700469 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700470 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
471 ComponentName name = new ComponentName(info.activityInfo.packageName,
472 info.activityInfo.name);
473 createWallpapersIntent.setComponent(name);
474 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700475 }
476 });
Winson Chungd0d43012010-09-26 17:26:45 -0700477 break;
478 default:
479 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700480 }
481 }
482
483 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700484 public boolean onLongClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800485 // Return early if this is not initiated from a touch
486 if (!v.isInTouchMode()) return false;
487 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800488 if (mNextPage != INVALID_PAGE) return false;
Michael Jurka7426c422010-11-11 15:23:47 -0800489 return beginDragging(v);
490 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700491
Michael Jurka7426c422010-11-11 15:23:47 -0800492 @Override
493 public boolean onTouch(View v, MotionEvent event) {
494 mLastTouchedItem = v;
495 return false;
496 }
497
498 /*
499 * Determines if we should change the touch state to start scrolling after the
500 * user moves their touch point too far.
501 */
502 protected void determineScrollingStart(MotionEvent ev) {
503 if (!mIsDragging) super.determineScrollingStart(ev);
504 }
505
506 /*
507 * Determines if we should change the touch state to start dragging after the
508 * user moves their touch point far enough.
509 */
510 protected void determineDraggingStart(MotionEvent ev) {
511 /*
512 * Locally do absolute value. mLastMotionX is set to the y value
513 * of the down event.
514 */
515 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
516 final float x = ev.getX(pointerIndex);
517 final float y = ev.getY(pointerIndex);
518 final int xDiff = (int) Math.abs(x - mLastMotionX);
519 final int yDiff = (int) Math.abs(y - mLastMotionY);
520
521 final int touchSlop = mTouchSlop;
522 boolean yMoved = yDiff > touchSlop;
523 boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
524
Winson Chung649a4ca2010-11-18 10:38:13 -0800525 if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
Michael Jurka7426c422010-11-11 15:23:47 -0800526 // Drag if the user moved far enough along the Y axis
527 beginDragging(mLastTouchedItem);
528
529 // Cancel any pending longpress
530 if (mAllowLongPress) {
531 mAllowLongPress = false;
532 // Try canceling the long press. It could also have been scheduled
533 // by a distant descendant, so use the mAllowLongPress flag to block
534 // everything
535 final View currentPage = getPageAt(mCurrentPage);
536 if (currentPage != null) {
537 currentPage.cancelLongPress();
538 }
539 }
540 }
541 }
542
543 @Override
544 public boolean onInterceptTouchEvent(MotionEvent ev) {
545 final int action = ev.getAction();
546 switch (action & MotionEvent.ACTION_MASK) {
547 case MotionEvent.ACTION_DOWN:
548 mIsDragging = false;
549 break;
550 case MotionEvent.ACTION_MOVE:
551 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
552 determineDraggingStart(ev);
553 }
554 break;
555 }
556 return super.onInterceptTouchEvent(ev);
557 }
558
559 @Override
560 public boolean onTouchEvent(MotionEvent ev) {
561 final int action = ev.getAction();
562 switch (action & MotionEvent.ACTION_MASK) {
563 case MotionEvent.ACTION_DOWN:
564 mIsDragging = false;
565 break;
566 case MotionEvent.ACTION_MOVE:
567 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
568 determineDraggingStart(ev);
569 }
570 break;
571 }
572 return super.onTouchEvent(ev);
573 }
574
Winson Chungcd4bc492010-12-09 18:52:32 -0800575 Bitmap drawableToBitmap(Drawable d, View v) {
576 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800577 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800578 c.translate((v.getWidth() - d.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkaaf91de02010-11-23 16:23:58 -0800579 d.draw(c);
580 return b;
581 }
582
Adam Cohen120980b2010-12-08 11:05:37 -0800583 private View getDragView(View v) {
584 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
585 v.findViewById(R.id.widget_preview) : v;
586 }
587
Michael Jurka7426c422010-11-11 15:23:47 -0800588 private boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700589 // End the current choice mode before we start dragging anything
590 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
591 endChoiceMode();
592 }
Michael Jurka7426c422010-11-11 15:23:47 -0800593 mIsDragging = true;
Winson Chungd0d43012010-09-26 17:26:45 -0700594
Winson Chung80baf5a2010-08-09 16:03:15 -0700595 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800596 case WidgetCustomization: {
Adam Cohen120980b2010-12-08 11:05:37 -0800597 // Get the widget preview as the drag representation
Michael Jurkad3ef3062010-11-23 16:23:58 -0800598 final LinearLayout l = (LinearLayout) v;
Winson Chungcd4bc492010-12-09 18:52:32 -0800599 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
600 final Drawable icon = i.getDrawable();
601 Bitmap b = drawableToBitmap(icon, i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700602 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700603
Michael Jurkad3ef3062010-11-23 16:23:58 -0800604 int[] spanXY = CellLayout.rectToCell(
605 getResources(), createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
606 createWidgetInfo.spanX = spanXY[0];
607 createWidgetInfo.spanY = spanXY[1];
608 mLauncher.getWorkspace().onDragStartedWithItemSpans(spanXY[0], spanXY[1], b);
609 mDragController.startDrag(
Winson Chungcd4bc492010-12-09 18:52:32 -0800610 i, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
611 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700612 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800613 }
614 case ShortcutCustomization: {
615 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800616 final TextView tv = (TextView) v;
617 final Drawable icon = tv.getCompoundDrawables()[1];
618 Bitmap b = drawableToBitmap(icon, tv);
Adam Cohen120980b2010-12-08 11:05:37 -0800619 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chungcd4bc492010-12-09 18:52:32 -0800620
Michael Jurkad3ef3062010-11-23 16:23:58 -0800621 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800622 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
623 null);
624 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700625 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800626 }
627 case ApplicationCustomization: {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700628 // Pick up the application for dropping
Michael Jurkad3ef3062010-11-23 16:23:58 -0800629 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800630 final TextView tv = (TextView) v;
631 final Drawable icon = tv.getCompoundDrawables()[1];
632 Bitmap b = drawableToBitmap(icon, tv);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700633 ApplicationInfo app = (ApplicationInfo) v.getTag();
634 app = new ApplicationInfo(app);
635
Michael Jurkad3ef3062010-11-23 16:23:58 -0800636 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800637 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
638 b.recycle();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700639 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700640 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800641 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700642 return false;
643 }
644
Winson Chunge3193b92010-09-10 11:44:42 -0700645 /**
646 * Pre-processes the layout of the different widget pages.
647 * @return the number of pages of widgets that we have
648 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700649 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700650 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700651
Winson Chunge3193b92010-09-10 11:44:42 -0700652 // create a new page for the first set of widgets
653 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700654 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700655 mWidgetPages.add(newPage);
656
657 // do this until we have no more widgets to lay out
658 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
659 final int widgetCount = mWidgetList.size();
660 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700661 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700662 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700663
Winson Chunge3193b92010-09-10 11:44:42 -0700664 // determine the size of the current widget
665 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
666 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700667
Winson Chunge3193b92010-09-10 11:44:42 -0700668 // create a new page if necessary
669 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
670 numCellsInRow = 0;
671 newPage = new ArrayList<AppWidgetProviderInfo>();
672 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700673 }
674
Winson Chunge3193b92010-09-10 11:44:42 -0700675 // add the item to the current page
676 newPage.add(info);
677 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700678 }
Winson Chunge3193b92010-09-10 11:44:42 -0700679
Winson Chung80baf5a2010-08-09 16:03:15 -0700680 return mWidgetPages.size();
681 }
682
Winson Chunge3193b92010-09-10 11:44:42 -0700683 /**
Winson Chung7da10252010-10-28 16:07:04 -0700684 * Helper function to draw a drawable to the specified canvas with the specified bounds.
685 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800686 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700687 if (bitmap != null) mCanvas.setBitmap(bitmap);
688 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800689 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700690 d.draw(mCanvas);
691 mCanvas.restore();
692 }
693
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800694 /*
695 * This method fetches an xml file specified in the manifest identified by
696 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
697 * an image which will be used as the wallpaper preview for an activity
698 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
699 * in the customize drawer.
700 */
701 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
702 Drawable d = null;
703
704 ActivityInfo activityInfo = ri.activityInfo;
705 XmlResourceParser parser = null;
706 try {
707 parser = activityInfo.loadXmlMetaData(mPackageManager,
708 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
709 if (parser == null) {
710 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
711 + "wallpaper provider '" + component + '\'');
712 return null;
713 }
714
715 AttributeSet attrs = Xml.asAttributeSet(parser);
716
717 int type;
718 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
719 && type != XmlPullParser.START_TAG) {
720 // drain whitespace, comments, etc.
721 }
722
723 String nodeName = parser.getName();
724 if (!"wallpaper-preview".equals(nodeName)) {
725 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
726 + "wallpaper provider '" + component + '\'');
727 return null;
728 }
729
730 // If metaData was null, we would have returned earlier when getting
731 // the parser No need to do the check here
732 Resources res = mPackageManager.getResourcesForApplication(
733 activityInfo.applicationInfo);
734
735 TypedArray sa = res.obtainAttributes(attrs,
736 com.android.internal.R.styleable.WallpaperPreviewInfo);
737
738 TypedValue value = sa.peekValue(
739 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
740 if (value == null) return null;
741
742 return res.getDrawable(value.resourceId);
743 } catch (Exception e) {
744 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
745 return null;
746 } finally {
747 if (parser != null) parser.close();
748 }
749 }
750
Winson Chung7da10252010-10-28 16:07:04 -0700751 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800752 * This method will extract the preview image specified by the wallpaper source provider (if it
753 * exists) otherwise, it will try to generate a default image preview.
754 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800755 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800756 // To be implemented later: resolving the up-to-date wallpaper thumbnail
757
758 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
759 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
760 Resources resources = mLauncher.getResources();
761
762 // Create a new bitmap to hold the widget preview
763 int width = (int) (dim * sScaleFactor);
764 int height = (int) (dim * sScaleFactor);
765 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800766
767 Drawable background = parseWallpaperPreviewXml(
768 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
769 boolean foundCustomDrawable = background != null;
770
771 if (!foundCustomDrawable) {
772 background = resources.getDrawable(R.drawable.default_widget_preview);
773 }
774
Winson Chung45e1d6e2010-11-09 17:19:49 -0800775 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
776
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800777 // If we don't have a custom icon, we use the app icon on the default background
778 if (!foundCustomDrawable) {
779 try {
780 final IconCache iconCache =
781 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
782 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
783 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800784
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800785 final int iconSize = minDim / 2;
786 final int offset = iconSize / 4;
787 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
788 } catch (Resources.NotFoundException e) {
789 // if we can't find the icon, then just don't draw it
790 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800791 }
792
Winson Chung29d6fea2010-12-01 15:47:31 -0800793 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800794 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
795 return drawable;
796 }
797
798 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700799 * This method will extract the preview image specified by the widget developer (if it exists),
800 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800801 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700802 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800803 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800804 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700805 String packageName = info.provider.getPackageName();
806 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800807 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700808 if (info.previewImage != 0) {
809 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
810 if (drawable == null) {
811 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
812 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700813 }
814 }
815
816 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700817 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
818 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700819 if (drawable == null) {
820 Resources resources = mLauncher.getResources();
821
Winson Chung80baf5a2010-08-09 16:03:15 -0700822 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700823 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
824 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700825 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
826 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
827 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700828
Winson Chung45e1d6e2010-11-09 17:19:49 -0800829 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700830 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700831 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700832 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700833 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700834 }
835 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700836 icon = resources.getDrawable(R.drawable.ic_launcher_application);
837 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700838
Winson Chunge3193b92010-09-10 11:44:42 -0700839 final int iconSize = minDim / 2;
840 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800841 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700842 } catch (Resources.NotFoundException e) {
843 // if we can't find the icon, then just don't draw it
844 }
845
Winson Chung29d6fea2010-12-01 15:47:31 -0800846 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700847 } else {
848 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800849 final float imageWidth = drawable.getIntrinsicWidth();
850 final float imageHeight = drawable.getIntrinsicHeight();
851 final float aspect = (float) imageWidth / imageHeight;
852 final int scaledWidth =
853 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
854 final int scaledHeight =
855 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700856 int width;
857 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800858 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700859 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800860 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700861 } else {
862 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800863 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700864 }
865
866 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
867 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
868
Winson Chung29d6fea2010-12-01 15:47:31 -0800869 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700870 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800871 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
872 newDrawable.getIntrinsicHeight());
873 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700874 }
875
876 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700877 layout.setCellCount(mCellCountX, mCellCountY);
878 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
879 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700880 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700881 }
882
Winson Chunge3193b92010-09-10 11:44:42 -0700883 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700884 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700885 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
886
887 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
888 }
889
Winson Chung80baf5a2010-08-09 16:03:15 -0700890 private void syncWidgetPages() {
891 if (mWidgetList == null) return;
892
Winson Chunge3193b92010-09-10 11:44:42 -0700893 // we need to repopulate with the LinearLayout layout for the widget pages
894 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700895 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700896 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800897 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700898 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700899 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
900 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700901
Winson Chunge22a8e92010-11-12 13:40:58 -0800902 addView(layout, new LinearLayout.LayoutParams(
903 LinearLayout.LayoutParams.WRAP_CONTENT,
904 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700905 }
906 }
907
908 private void syncWidgetPageItems(int page) {
909 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700910 LinearLayout layout = (LinearLayout) getChildAt(page);
911 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700912 final int count = list.size();
913 layout.removeAllViews();
914 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700915 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
916 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800917 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
918 info.minHeight, null);
919 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700920
Winson Chung29d6fea2010-12-01 15:47:31 -0800921 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700922 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800923 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700924 l.setTag(createItemInfo);
925 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800926 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700927 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700928
Winson Chunge3193b92010-09-10 11:44:42 -0700929 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700930 }
931 }
932
Winson Chung45e1d6e2010-11-09 17:19:49 -0800933 private void syncWallpaperPages() {
934 if (mWallpaperList == null) return;
935
936 // We need to repopulate the LinearLayout for the wallpaper pages
937 removeAllViews();
938 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800939 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800940 for (int i = 0; i < numPages; ++i) {
941 LinearLayout layout = new PagedViewExtendedLayout(getContext());
942 layout.setGravity(Gravity.CENTER_HORIZONTAL);
943 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
944 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
945
Winson Chunge22a8e92010-11-12 13:40:58 -0800946 addView(layout, new LinearLayout.LayoutParams(
947 LinearLayout.LayoutParams.WRAP_CONTENT,
948 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800949 }
950 }
951
952 private void syncWallpaperPageItems(int page) {
953 // Load the items on to the pages
954 LinearLayout layout = (LinearLayout) getChildAt(page);
955 layout.removeAllViews();
956 final int count = mWallpaperList.size();
Winson Chung78bd53c2010-12-09 13:50:24 -0800957 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -0800958 final int startIndex = page * numItemsPerPage;
959 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
960 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800961 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800962 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800963
Winson Chung29d6fea2010-12-01 15:47:31 -0800964 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800965 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800966 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
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 Chung5ffd8ea2010-09-23 18:40:29 -0700989 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700990 int startIndex = page * numCells;
991 int endIndex = Math.min(startIndex + numCells, list.size());
992 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
993 // TODO: we can optimize by just re-applying to existing views
994 layout.removeAllViews();
995 for (int i = startIndex; i < endIndex; ++i) {
996 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700997 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
998
Winson Chung241c3b42010-08-25 16:53:03 -0700999 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1000 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -07001001 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Adam Cohen120980b2010-12-08 11:05:37 -08001002 ((LauncherApplication) mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -07001003 switch (mCustomizationType) {
1004 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -07001005 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001006 break;
Winson Chungd0d43012010-09-26 17:26:45 -07001007 case ShortcutCustomization:
1008 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1009 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1010 info.activityInfo.name);
1011 icon.setTag(createItemInfo);
1012 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001013 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001014 icon.setOnLongClickListener(this);
1015 break;
1016 default:
1017 break;
Winson Chunge8878e32010-09-15 20:37:09 -07001018 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001019
1020 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001021 final int x = index % mCellCountX;
1022 final int y = index / mCellCountX;
1023 setupPage(layout);
1024 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
1025 }
1026 }
1027
1028 private void syncAppPages() {
1029 if (mApps == null) return;
1030
1031 // We need to repopulate with PagedViewCellLayouts
1032 removeAllViews();
1033
1034 // Ensure that we have the right number of pages
1035 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
1036 for (int i = 0; i < numPages; ++i) {
1037 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1038 setupPage(layout);
1039 addView(layout);
1040 }
1041 }
1042
1043 private void syncAppPageItems(int page) {
1044 if (mApps == null) return;
1045
1046 // ensure that we have the right number of items on the pages
1047 int numCells = mCellCountX * mCellCountY;
1048 int startIndex = page * numCells;
1049 int endIndex = Math.min(startIndex + numCells, mApps.size());
1050 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
1051 // TODO: we can optimize by just re-applying to existing views
1052 layout.removeAllViews();
1053 for (int i = startIndex; i < endIndex; ++i) {
1054 final ApplicationInfo info = mApps.get(i);
1055 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1056 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -07001057 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -07001058 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001059 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001060 icon.setOnLongClickListener(this);
1061
1062 final int index = i - startIndex;
1063 final int x = index % mCellCountX;
1064 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001065 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -07001066 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001067 }
1068 }
1069
Winson Chung80baf5a2010-08-09 16:03:15 -07001070 @Override
1071 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -07001072 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001073 switch (mCustomizationType) {
1074 case WidgetCustomization:
1075 syncWidgetPages();
1076 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001077 case ShortcutCustomization:
1078 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001079 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001080 break;
1081 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001082 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -07001083 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001084 case ApplicationCustomization:
1085 syncAppPages();
1086 centerPagedViewCellLayouts = false;
1087 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001088 default:
1089 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001090 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001091 break;
1092 }
1093
1094 // only try and center the page if there is one page
1095 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001096 if (centerPagedViewCellLayouts) {
1097 if (childCount == 1) {
1098 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1099 layout.enableCenteredContent(true);
1100 } else {
1101 for (int i = 0; i < childCount; ++i) {
1102 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1103 layout.enableCenteredContent(false);
1104 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001105 }
1106 }
1107
1108 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001109 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001110 }
1111
1112 @Override
1113 public void syncPageItems(int page) {
1114 switch (mCustomizationType) {
1115 case WidgetCustomization:
1116 syncWidgetPageItems(page);
1117 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001118 case ShortcutCustomization:
1119 syncListPageItems(page, mShortcutList);
1120 break;
1121 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001122 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001123 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001124 case ApplicationCustomization:
1125 syncAppPageItems(page);
1126 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001127 }
1128 }
Winson Chunge3193b92010-09-10 11:44:42 -07001129
Winson Chungd0d43012010-09-26 17:26:45 -07001130 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001131 protected int getAssociatedLowerPageBound(int page) {
1132 return 0;
1133 }
Winson Chungd0d43012010-09-26 17:26:45 -07001134 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001135 protected int getAssociatedUpperPageBound(int page) {
1136 return getChildCount();
1137 }
Winson Chungd0d43012010-09-26 17:26:45 -07001138
1139 @Override
1140 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001141 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001142 return true;
1143 }
1144
1145 @Override
1146 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1147 return true;
1148 }
1149
1150 @Override
1151 public void onDestroyActionMode(ActionMode mode) {
1152 endChoiceMode();
1153 }
1154
1155 @Override
1156 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1157 return false;
1158 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001159}