blob: 342974a4bf7ee5d9620714dd5e51dabefaaeef2b [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;
27import android.animation.ObjectAnimator;
28import android.animation.PropertyValuesHolder;
29import android.animation.TimeInterpolator;
30import android.animation.Animator.AnimatorListener;
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;
Winson Chung86f77532010-08-24 11:08:22 -070044import android.graphics.Canvas;
Michael Jurkaaf91de02010-11-23 16:23:58 -080045import android.graphics.Bitmap.Config;
Winson Chung80baf5a2010-08-09 16:03:15 -070046import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070047import android.util.AttributeSet;
48import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080049import android.util.Slog;
50import android.util.TypedValue;
51import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070052import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070053import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070054import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070055import android.view.Menu;
56import android.view.MenuItem;
Michael Jurka7426c422010-11-11 15:23:47 -080057import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070058import android.view.View;
Adam Cohen120980b2010-12-08 11:05:37 -080059import android.view.animation.DecelerateInterpolator;
60import android.view.animation.Interpolator;
Winson Chunge3193b92010-09-10 11:44:42 -070061import android.widget.ImageView;
62import android.widget.LinearLayout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080063import android.widget.TextView;
Winson Chung80baf5a2010-08-09 16:03:15 -070064
Adam Cohen120980b2010-12-08 11:05:37 -080065import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070066
Adam Cohen7b9d3a62010-12-07 21:49:34 -080067
Winson Chung80baf5a2010-08-09 16:03:15 -070068public class CustomizePagedView extends PagedView
Michael Jurka7426c422010-11-11 15:23:47 -080069 implements View.OnLongClickListener, View.OnClickListener, View.OnTouchListener,
Winson Chungd0d43012010-09-26 17:26:45 -070070 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";
80 private static final boolean DEBUG = false;
81
Michael Jurka7426c422010-11-11 15:23:47 -080082 private View mLastTouchedItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070083 private Launcher mLauncher;
84 private DragController mDragController;
85 private PackageManager mPackageManager;
Michael Jurka7426c422010-11-11 15:23:47 -080086 private boolean mIsDragging;
87 private float mDragSlopeThreshold;
Winson Chung80baf5a2010-08-09 16:03:15 -070088
89 private CustomizationType mCustomizationType;
90
Winson Chunge3193b92010-09-10 11:44:42 -070091 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
92 private PagedViewCellLayout mWorkspaceWidgetLayout;
93
94 // The mapping between the pages and the widgets that will be laid out on them
95 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
96
Winson Chung45e1d6e2010-11-09 17:19:49 -080097 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070098 private int mMaxWidgetWidth;
99
Winson Chung45e1d6e2010-11-09 17:19:49 -0800100 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -0700101 private int mMaxWidgetsCellHSpan;
102
Winson Chung45e1d6e2010-11-09 17:19:49 -0800103 // The size of the items on the wallpaper tab
104 private int mWallpaperCellHSpan;
105
Winson Chunge3193b92010-09-10 11:44:42 -0700106 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700107 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700108 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700109 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700110 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700111
Winson Chunge3193b92010-09-10 11:44:42 -0700112 private static final int sMinWidgetCellHSpan = 2;
113 private static final int sMaxWidgetCellHSpan = 4;
114
Michael Jurka3125d9d2010-09-27 11:30:20 -0700115 private int mChoiceModeTitleText;
116
Winson Chunge3193b92010-09-10 11:44:42 -0700117 // The scale factor for widget previews inside the widget drawer
118 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700119
120 private final Canvas mCanvas = new Canvas();
121 private final LayoutInflater mInflater;
122
Adam Cohen120980b2010-12-08 11:05:37 -0800123 private final float mTmpFloatPos[] = new float[2];
124 private final float ANIMATION_SCALE = 0.5f;
125 private final int ANIMATION_DURATION = 400;
126 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
127 private ScaleAlphaInterpolator mScaleAlphaInterpolator = new ScaleAlphaInterpolator();
128
Winson Chung80baf5a2010-08-09 16:03:15 -0700129 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700130 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700131 }
132
133 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700134 this(context, attrs, 0);
135 }
136
137 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
138 super(context, attrs, defStyle);
139
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700140 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800141 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
142 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700143 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
144 a.recycle();
145 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
146 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
147 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700148 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800149
Winson Chung80baf5a2010-08-09 16:03:15 -0700150 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700151 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
152 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700153 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700154
Michael Jurka7426c422010-11-11 15:23:47 -0800155 final Resources r = context.getResources();
156 mDragSlopeThreshold =
157 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f;
158
Winson Chung80baf5a2010-08-09 16:03:15 -0700159 setVisibility(View.GONE);
160 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700161 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700162 }
163
Winson Chung7da10252010-10-28 16:07:04 -0700164 @Override
165 protected void init() {
166 super.init();
167 mCenterPagesVertically = false;
168 }
169
Winson Chung80baf5a2010-08-09 16:03:15 -0700170 public void setLauncher(Launcher launcher) {
171 Context context = getContext();
172 mLauncher = launcher;
173 mPackageManager = context.getPackageManager();
174 }
175
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700176 /**
177 * Sets the list of applications that launcher has loaded.
178 */
179 public void setApps(ArrayList<ApplicationInfo> list) {
180 mApps = list;
181 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700182
183 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700184 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700185 }
186
187 /**
188 * Convenience function to add new items to the set of applications that were previously loaded.
189 * Called by both updateApps() and setApps().
190 */
191 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
192 // we add it in place, in alphabetical order
193 final int count = list.size();
194 for (int i = 0; i < count; ++i) {
195 final ApplicationInfo info = list.get(i);
196 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
197 if (index < 0) {
198 mApps.add(-(index + 1), info);
199 }
200 }
201 }
202
203 /**
204 * Adds new applications to the loaded list, and notifies the paged view to update itself.
205 */
206 public void addApps(ArrayList<ApplicationInfo> list) {
207 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700208
209 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700210 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700211 }
212
213 /**
214 * Convenience function to remove items to the set of applications that were previously loaded.
215 * Called by both updateApps() and removeApps().
216 */
217 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
218 // loop through all the apps and remove apps that have the same component
219 final int length = list.size();
220 for (int i = 0; i < length; ++i) {
221 final ApplicationInfo info = list.get(i);
222 int removeIndex = findAppByComponent(mApps, info);
223 if (removeIndex > -1) {
224 mApps.remove(removeIndex);
225 mPageViewIconCache.removeOutline(info);
226 }
227 }
228 }
229
230 /**
231 * Removes applications from the loaded list, and notifies the paged view to update itself.
232 */
233 public void removeApps(ArrayList<ApplicationInfo> list) {
234 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700235
236 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700237 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700238 }
239
240 /**
241 * Updates a set of applications from the loaded list, and notifies the paged view to update
242 * itself.
243 */
244 public void updateApps(ArrayList<ApplicationInfo> list) {
245 // We remove and re-add the updated applications list because it's properties may have
246 // changed (ie. the title), and this will ensure that the items will be in their proper
247 // place in the list.
248 removeAppsWithoutInvalidate(list);
249 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700250
251 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700252 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700253 }
254
255 /**
256 * Convenience function to find matching ApplicationInfos for removal.
257 */
258 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
259 ComponentName removeComponent = item.intent.getComponent();
260 final int length = list.size();
261 for (int i = 0; i < length; ++i) {
262 ApplicationInfo info = list.get(i);
263 if (info.intent.getComponent().equals(removeComponent)) {
264 return i;
265 }
266 }
267 return -1;
268 }
269
Winson Chung80baf5a2010-08-09 16:03:15 -0700270 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700271 // get the list of widgets
272 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
273 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
274 @Override
275 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
276 return object1.label.compareTo(object2.label);
277 }
278 });
279
280 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
281 @Override
282 public int compare(ResolveInfo object1, ResolveInfo object2) {
283 return object1.loadLabel(mPackageManager).toString().compareTo(
284 object2.loadLabel(mPackageManager).toString());
285 }
286 };
287
Winson Chung80baf5a2010-08-09 16:03:15 -0700288 // get the list of shortcuts
289 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
290 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
291 Collections.sort(mShortcutList, resolveInfoComparator);
292
Winson Chunge8878e32010-09-15 20:37:09 -0700293 // get the list of wallpapers
294 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800295 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
296 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700297 Collections.sort(mWallpaperList, resolveInfoComparator);
298
Winson Chung10fefb12010-11-01 11:57:06 -0700299 invalidatePageDataAndIconCache();
300 }
301
302 private void invalidatePageDataAndIconCache() {
303 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700304 mPageViewIconCache.clear();
305
Winson Chunge3193b92010-09-10 11:44:42 -0700306 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700307 invalidatePageData();
308 }
309
310 public void setDragController(DragController dragger) {
311 mDragController = dragger;
312 }
313
314 public void setCustomizationFilter(CustomizationType filterType) {
315 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700316 setCurrentPage(0);
Winson Chungbbc60d82010-11-11 16:34:41 -0800317 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700318 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700319
320 // End the current choice mode so that we don't carry selections across tabs
321 endChoiceMode();
Winson Chung649a4ca2010-11-18 10:38:13 -0800322 // Reset the touch item (if we are mid-dragging)
323 mLastTouchedItem = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700324 }
325
326 @Override
327 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700328 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700329 }
330
331 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800332 public void onDragViewVisible() {
333 }
334
Adam Cohen120980b2010-12-08 11:05:37 -0800335 class ScaleAlphaInterpolator implements Interpolator {
336 public float getInterpolation(float input) {
337 float pivot = 0.5f;
338 if (input < pivot) {
339 return 0;
340 } else {
341 return (input - pivot)/(1 - pivot);
342 }
343 }
344 }
345
346 private void animateItemOntoScreen(View dragView,
347 final CellLayout layout, final ItemInfo info) {
348 mTmpFloatPos[0] = layout.getWidth() / 2;
349 mTmpFloatPos[1] = layout.getHeight() / 2;
350 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
351
352 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
353 final View dragCopy = dragLayer.createDragView(dragView);
354 dragCopy.setAlpha(1.0f);
355
356 int dragViewWidth = dragView.getMeasuredWidth();
357 int dragViewHeight = dragView.getMeasuredHeight();
358 float heightOffset = 0;
359 float widthOffset = 0;
360
361 if (dragView instanceof ImageView) {
362 Drawable d = ((ImageView) dragView).getDrawable();
363 int width = d.getIntrinsicWidth();
364 int height = d.getIntrinsicHeight();
365
366 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
367 float f = (dragViewWidth / (width * 1.0f));
368 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
369 } else {
370 float f = (dragViewHeight / (height * 1.0f));
371 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
372 }
373 }
374
375 float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
376 float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
377
378 ObjectAnimator posAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
379 PropertyValuesHolder.ofFloat("x", toX),
380 PropertyValuesHolder.ofFloat("y", toY));
381 posAnim.setInterpolator(mQuintEaseOutInterpolator);
382 posAnim.setDuration(ANIMATION_DURATION);
383
384 posAnim.addListener(new AnimatorListener() {
385 public void onAnimationCancel(Animator animation) {}
386 public void onAnimationRepeat(Animator animation) {}
387 public void onAnimationStart(Animator animation) {}
388
389 public void onAnimationEnd(Animator animation) {
390 dragLayer.removeView(dragCopy);
391 mLauncher.addExternalItemToScreen(info, layout);
392 post(new Runnable() {
393 public void run() {
394 layout.animateDrop();
395 }
396 });
397 }
398 });
399
400 ObjectAnimator scaleAlphaAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
401 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
402 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
403 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
404 scaleAlphaAnim.setInterpolator(mScaleAlphaInterpolator);
405 scaleAlphaAnim.setDuration(ANIMATION_DURATION);
406
407 posAnim.start();
408 scaleAlphaAnim.start();
409 }
410
Patrick Dubroya669d792010-11-23 14:40:33 -0800411 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800412 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800413 // Return early if this is not initiated from a touch
414 if (!v.isInTouchMode()) return;
415 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800416 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700417
Winson Chungd0d43012010-09-26 17:26:45 -0700418 // On certain pages, we allow single tap to mark items as selected so that they can be
419 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700420 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700421 switch (mCustomizationType) {
422 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700423 mChoiceModeTitleText = R.string.cab_widget_selection_text;
424 enterChoiceMode = true;
425 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700426 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700427 mChoiceModeTitleText = R.string.cab_app_selection_text;
428 enterChoiceMode = true;
429 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700430 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700431 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
432 enterChoiceMode = true;
433 break;
434 default:
435 break;
436 }
Winson Chungd0d43012010-09-26 17:26:45 -0700437
Michael Jurka3125d9d2010-09-27 11:30:20 -0700438 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700439 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700440
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700441 Workspace w = mLauncher.getWorkspace();
442 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
443 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800444 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700445
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700446 animateClickFeedback(v, new Runnable() {
447 @Override
448 public void run() {
Adam Cohen120980b2010-12-08 11:05:37 -0800449 animateItemOntoScreen(dragView, cl, itemInfo);
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700450 }
451 });
Winson Chungd0d43012010-09-26 17:26:45 -0700452 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700453 }
454
455 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700456 switch (mCustomizationType) {
457 case WallpaperCustomization:
458 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700459 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700460 animateClickFeedback(v, new Runnable() {
461 @Override
462 public void run() {
463 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700464 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700465 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
466 ComponentName name = new ComponentName(info.activityInfo.packageName,
467 info.activityInfo.name);
468 createWallpapersIntent.setComponent(name);
469 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700470 }
471 });
Winson Chungd0d43012010-09-26 17:26:45 -0700472 break;
473 default:
474 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700475 }
476 }
477
478 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700479 public boolean onLongClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800480 // Return early if this is not initiated from a touch
481 if (!v.isInTouchMode()) return false;
482 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800483 if (mNextPage != INVALID_PAGE) return false;
Michael Jurka7426c422010-11-11 15:23:47 -0800484 return beginDragging(v);
485 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700486
Michael Jurka7426c422010-11-11 15:23:47 -0800487 @Override
488 public boolean onTouch(View v, MotionEvent event) {
489 mLastTouchedItem = v;
490 return false;
491 }
492
493 /*
494 * Determines if we should change the touch state to start scrolling after the
495 * user moves their touch point too far.
496 */
497 protected void determineScrollingStart(MotionEvent ev) {
498 if (!mIsDragging) super.determineScrollingStart(ev);
499 }
500
501 /*
502 * Determines if we should change the touch state to start dragging after the
503 * user moves their touch point far enough.
504 */
505 protected void determineDraggingStart(MotionEvent ev) {
506 /*
507 * Locally do absolute value. mLastMotionX is set to the y value
508 * of the down event.
509 */
510 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
511 final float x = ev.getX(pointerIndex);
512 final float y = ev.getY(pointerIndex);
513 final int xDiff = (int) Math.abs(x - mLastMotionX);
514 final int yDiff = (int) Math.abs(y - mLastMotionY);
515
516 final int touchSlop = mTouchSlop;
517 boolean yMoved = yDiff > touchSlop;
518 boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
519
Winson Chung649a4ca2010-11-18 10:38:13 -0800520 if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
Michael Jurka7426c422010-11-11 15:23:47 -0800521 // Drag if the user moved far enough along the Y axis
522 beginDragging(mLastTouchedItem);
523
524 // Cancel any pending longpress
525 if (mAllowLongPress) {
526 mAllowLongPress = false;
527 // Try canceling the long press. It could also have been scheduled
528 // by a distant descendant, so use the mAllowLongPress flag to block
529 // everything
530 final View currentPage = getPageAt(mCurrentPage);
531 if (currentPage != null) {
532 currentPage.cancelLongPress();
533 }
534 }
535 }
536 }
537
538 @Override
539 public boolean onInterceptTouchEvent(MotionEvent ev) {
540 final int action = ev.getAction();
541 switch (action & MotionEvent.ACTION_MASK) {
542 case MotionEvent.ACTION_DOWN:
543 mIsDragging = false;
544 break;
545 case MotionEvent.ACTION_MOVE:
546 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
547 determineDraggingStart(ev);
548 }
549 break;
550 }
551 return super.onInterceptTouchEvent(ev);
552 }
553
554 @Override
555 public boolean onTouchEvent(MotionEvent ev) {
556 final int action = ev.getAction();
557 switch (action & MotionEvent.ACTION_MASK) {
558 case MotionEvent.ACTION_DOWN:
559 mIsDragging = false;
560 break;
561 case MotionEvent.ACTION_MOVE:
562 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
563 determineDraggingStart(ev);
564 }
565 break;
566 }
567 return super.onTouchEvent(ev);
568 }
569
Michael Jurkaaf91de02010-11-23 16:23:58 -0800570 Bitmap drawableToBitmap(Drawable d) {
571 Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(),
572 Bitmap.Config.ARGB_8888);
573 Canvas c = new Canvas(b);
574 d.draw(c);
575 return b;
576 }
577
Adam Cohen120980b2010-12-08 11:05:37 -0800578 private View getDragView(View v) {
579 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
580 v.findViewById(R.id.widget_preview) : v;
581 }
582
Michael Jurka7426c422010-11-11 15:23:47 -0800583 private boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700584 // End the current choice mode before we start dragging anything
585 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
586 endChoiceMode();
587 }
Michael Jurka7426c422010-11-11 15:23:47 -0800588 mIsDragging = true;
Winson Chungd0d43012010-09-26 17:26:45 -0700589
Winson Chung80baf5a2010-08-09 16:03:15 -0700590 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800591 case WidgetCustomization: {
Adam Cohen120980b2010-12-08 11:05:37 -0800592 // Get the widget preview as the drag representation
Michael Jurkad3ef3062010-11-23 16:23:58 -0800593 final LinearLayout l = (LinearLayout) v;
594 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
595 Bitmap b = drawableToBitmap(icon);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700596 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Adam Cohen120980b2010-12-08 11:05:37 -0800597 final View dragView = v.findViewById(R.id.widget_preview);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700598
Michael Jurkad3ef3062010-11-23 16:23:58 -0800599 int[] spanXY = CellLayout.rectToCell(
600 getResources(), createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
601 createWidgetInfo.spanX = spanXY[0];
602 createWidgetInfo.spanY = spanXY[1];
603 mLauncher.getWorkspace().onDragStartedWithItemSpans(spanXY[0], spanXY[1], b);
604 mDragController.startDrag(
605 v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chunge3193b92010-09-10 11:44:42 -0700606
Winson Chung80baf5a2010-08-09 16:03:15 -0700607 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800608 }
609 case ShortcutCustomization: {
610 // get icon (top compound drawable, index is 1)
611 final Drawable icon = ((TextView) v).getCompoundDrawables()[1];
612 Bitmap b = drawableToBitmap(icon);
Adam Cohen120980b2010-12-08 11:05:37 -0800613 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800614 mDragController.startDrag(
615 v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
616 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chung80baf5a2010-08-09 16:03:15 -0700617 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800618 }
619 case ApplicationCustomization: {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700620 // Pick up the application for dropping
Michael Jurkad3ef3062010-11-23 16:23:58 -0800621 // get icon (top compound drawable, index is 1)
622 final Drawable icon = ((TextView) v).getCompoundDrawables()[1];
623 Bitmap b = drawableToBitmap(icon);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700624 ApplicationInfo app = (ApplicationInfo) v.getTag();
625 app = new ApplicationInfo(app);
626
Michael Jurkad3ef3062010-11-23 16:23:58 -0800627 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
628 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700629 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700630 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800631 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700632 return false;
633 }
634
Winson Chunge3193b92010-09-10 11:44:42 -0700635 /**
636 * Pre-processes the layout of the different widget pages.
637 * @return the number of pages of widgets that we have
638 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700639 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700640 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700641
Winson Chunge3193b92010-09-10 11:44:42 -0700642 // create a new page for the first set of widgets
643 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700644 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700645 mWidgetPages.add(newPage);
646
647 // do this until we have no more widgets to lay out
648 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
649 final int widgetCount = mWidgetList.size();
650 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700651 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700652 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700653
Winson Chunge3193b92010-09-10 11:44:42 -0700654 // determine the size of the current widget
655 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
656 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700657
Winson Chunge3193b92010-09-10 11:44:42 -0700658 // create a new page if necessary
659 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
660 numCellsInRow = 0;
661 newPage = new ArrayList<AppWidgetProviderInfo>();
662 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700663 }
664
Winson Chunge3193b92010-09-10 11:44:42 -0700665 // add the item to the current page
666 newPage.add(info);
667 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700668 }
Winson Chunge3193b92010-09-10 11:44:42 -0700669
Winson Chung80baf5a2010-08-09 16:03:15 -0700670 return mWidgetPages.size();
671 }
672
Winson Chunge3193b92010-09-10 11:44:42 -0700673 /**
Winson Chung7da10252010-10-28 16:07:04 -0700674 * Helper function to draw a drawable to the specified canvas with the specified bounds.
675 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800676 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700677 if (bitmap != null) mCanvas.setBitmap(bitmap);
678 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800679 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700680 d.draw(mCanvas);
681 mCanvas.restore();
682 }
683
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800684 /*
685 * This method fetches an xml file specified in the manifest identified by
686 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
687 * an image which will be used as the wallpaper preview for an activity
688 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
689 * in the customize drawer.
690 */
691 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
692 Drawable d = null;
693
694 ActivityInfo activityInfo = ri.activityInfo;
695 XmlResourceParser parser = null;
696 try {
697 parser = activityInfo.loadXmlMetaData(mPackageManager,
698 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
699 if (parser == null) {
700 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
701 + "wallpaper provider '" + component + '\'');
702 return null;
703 }
704
705 AttributeSet attrs = Xml.asAttributeSet(parser);
706
707 int type;
708 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
709 && type != XmlPullParser.START_TAG) {
710 // drain whitespace, comments, etc.
711 }
712
713 String nodeName = parser.getName();
714 if (!"wallpaper-preview".equals(nodeName)) {
715 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
716 + "wallpaper provider '" + component + '\'');
717 return null;
718 }
719
720 // If metaData was null, we would have returned earlier when getting
721 // the parser No need to do the check here
722 Resources res = mPackageManager.getResourcesForApplication(
723 activityInfo.applicationInfo);
724
725 TypedArray sa = res.obtainAttributes(attrs,
726 com.android.internal.R.styleable.WallpaperPreviewInfo);
727
728 TypedValue value = sa.peekValue(
729 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
730 if (value == null) return null;
731
732 return res.getDrawable(value.resourceId);
733 } catch (Exception e) {
734 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
735 return null;
736 } finally {
737 if (parser != null) parser.close();
738 }
739 }
740
Winson Chung7da10252010-10-28 16:07:04 -0700741 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800742 * This method will extract the preview image specified by the wallpaper source provider (if it
743 * exists) otherwise, it will try to generate a default image preview.
744 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800745 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800746 // To be implemented later: resolving the up-to-date wallpaper thumbnail
747
748 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
749 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
750 Resources resources = mLauncher.getResources();
751
752 // Create a new bitmap to hold the widget preview
753 int width = (int) (dim * sScaleFactor);
754 int height = (int) (dim * sScaleFactor);
755 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800756
757 Drawable background = parseWallpaperPreviewXml(
758 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
759 boolean foundCustomDrawable = background != null;
760
761 if (!foundCustomDrawable) {
762 background = resources.getDrawable(R.drawable.default_widget_preview);
763 }
764
Winson Chung45e1d6e2010-11-09 17:19:49 -0800765 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
766
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800767 // If we don't have a custom icon, we use the app icon on the default background
768 if (!foundCustomDrawable) {
769 try {
770 final IconCache iconCache =
771 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
772 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
773 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800774
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800775 final int iconSize = minDim / 2;
776 final int offset = iconSize / 4;
777 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
778 } catch (Resources.NotFoundException e) {
779 // if we can't find the icon, then just don't draw it
780 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800781 }
782
Winson Chung29d6fea2010-12-01 15:47:31 -0800783 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800784 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
785 return drawable;
786 }
787
788 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700789 * This method will extract the preview image specified by the widget developer (if it exists),
790 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800791 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700792 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800793 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800794 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700795 String packageName = info.provider.getPackageName();
796 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800797 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700798 if (info.previewImage != 0) {
799 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
800 if (drawable == null) {
801 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
802 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700803 }
804 }
805
806 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700807 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
808 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700809 if (drawable == null) {
810 Resources resources = mLauncher.getResources();
811
Winson Chung80baf5a2010-08-09 16:03:15 -0700812 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700813 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
814 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700815 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
816 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
817 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700818
Winson Chung45e1d6e2010-11-09 17:19:49 -0800819 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700820 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700821 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700822 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700823 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700824 }
825 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700826 icon = resources.getDrawable(R.drawable.ic_launcher_application);
827 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700828
Winson Chunge3193b92010-09-10 11:44:42 -0700829 final int iconSize = minDim / 2;
830 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800831 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700832 } catch (Resources.NotFoundException e) {
833 // if we can't find the icon, then just don't draw it
834 }
835
Winson Chung29d6fea2010-12-01 15:47:31 -0800836 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700837 } else {
838 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800839 final float imageWidth = drawable.getIntrinsicWidth();
840 final float imageHeight = drawable.getIntrinsicHeight();
841 final float aspect = (float) imageWidth / imageHeight;
842 final int scaledWidth =
843 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
844 final int scaledHeight =
845 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700846 int width;
847 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800848 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700849 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800850 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700851 } else {
852 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800853 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700854 }
855
856 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
857 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
858
Winson Chung29d6fea2010-12-01 15:47:31 -0800859 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700860 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800861 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
862 newDrawable.getIntrinsicHeight());
863 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700864 }
865
866 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700867 layout.setCellCount(mCellCountX, mCellCountY);
868 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
869 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700870 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700871 }
872
Winson Chunge3193b92010-09-10 11:44:42 -0700873 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700874 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700875 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
876
877 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
878 }
879
Winson Chung80baf5a2010-08-09 16:03:15 -0700880 private void syncWidgetPages() {
881 if (mWidgetList == null) return;
882
Winson Chunge3193b92010-09-10 11:44:42 -0700883 // we need to repopulate with the LinearLayout layout for the widget pages
884 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700885 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700886 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800887 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700888 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700889 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
890 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700891
Winson Chunge22a8e92010-11-12 13:40:58 -0800892 addView(layout, new LinearLayout.LayoutParams(
893 LinearLayout.LayoutParams.WRAP_CONTENT,
894 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700895 }
896 }
897
898 private void syncWidgetPageItems(int page) {
899 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700900 LinearLayout layout = (LinearLayout) getChildAt(page);
901 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700902 final int count = list.size();
903 layout.removeAllViews();
904 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700905 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
906 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800907 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
908 info.minHeight, null);
909 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700910
Winson Chung29d6fea2010-12-01 15:47:31 -0800911 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700912 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800913 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700914 l.setTag(createItemInfo);
915 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800916 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700917 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700918
Winson Chunge3193b92010-09-10 11:44:42 -0700919 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700920 }
921 }
922
Winson Chung45e1d6e2010-11-09 17:19:49 -0800923 private void syncWallpaperPages() {
924 if (mWallpaperList == null) return;
925
926 // We need to repopulate the LinearLayout for the wallpaper pages
927 removeAllViews();
928 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
929 mMaxWidgetsCellHSpan);
930 for (int i = 0; i < numPages; ++i) {
931 LinearLayout layout = new PagedViewExtendedLayout(getContext());
932 layout.setGravity(Gravity.CENTER_HORIZONTAL);
933 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
934 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
935
Winson Chunge22a8e92010-11-12 13:40:58 -0800936 addView(layout, new LinearLayout.LayoutParams(
937 LinearLayout.LayoutParams.WRAP_CONTENT,
938 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800939 }
940 }
941
942 private void syncWallpaperPageItems(int page) {
943 // Load the items on to the pages
944 LinearLayout layout = (LinearLayout) getChildAt(page);
945 layout.removeAllViews();
946 final int count = mWallpaperList.size();
Winson Chungd28ed492010-11-22 14:34:57 -0800947 final int numItemsPerPage = mMaxWidgetsCellHSpan / mWallpaperCellHSpan;
948 final int startIndex = page * numItemsPerPage;
949 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
950 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800951 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800952 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800953
Winson Chung29d6fea2010-12-01 15:47:31 -0800954 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800955 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800956 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800957 l.setTag(info);
958 l.setOnClickListener(this);
959
Winson Chung45e1d6e2010-11-09 17:19:49 -0800960 layout.addView(l);
961 }
962 }
963
Winson Chung80baf5a2010-08-09 16:03:15 -0700964 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700965 // we need to repopulate with PagedViewCellLayouts
966 removeAllViews();
967
Winson Chung80baf5a2010-08-09 16:03:15 -0700968 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700969 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700970 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700971 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
972 setupPage(layout);
973 addView(layout);
974 }
975 }
976
977 private void syncListPageItems(int page, List<ResolveInfo> list) {
978 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700979 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700980 int startIndex = page * numCells;
981 int endIndex = Math.min(startIndex + numCells, list.size());
982 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
983 // TODO: we can optimize by just re-applying to existing views
984 layout.removeAllViews();
985 for (int i = startIndex; i < endIndex; ++i) {
986 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700987 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
988
Winson Chung241c3b42010-08-25 16:53:03 -0700989 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
990 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700991 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Adam Cohen120980b2010-12-08 11:05:37 -0800992 ((LauncherApplication) mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700993 switch (mCustomizationType) {
994 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700995 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700996 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700997 case ShortcutCustomization:
998 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
999 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1000 info.activityInfo.name);
1001 icon.setTag(createItemInfo);
1002 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001003 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001004 icon.setOnLongClickListener(this);
1005 break;
1006 default:
1007 break;
Winson Chunge8878e32010-09-15 20:37:09 -07001008 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001009
1010 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001011 final int x = index % mCellCountX;
1012 final int y = index / mCellCountX;
1013 setupPage(layout);
1014 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
1015 }
1016 }
1017
1018 private void syncAppPages() {
1019 if (mApps == null) return;
1020
1021 // We need to repopulate with PagedViewCellLayouts
1022 removeAllViews();
1023
1024 // Ensure that we have the right number of pages
1025 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
1026 for (int i = 0; i < numPages; ++i) {
1027 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1028 setupPage(layout);
1029 addView(layout);
1030 }
1031 }
1032
1033 private void syncAppPageItems(int page) {
1034 if (mApps == null) return;
1035
1036 // ensure that we have the right number of items on the pages
1037 int numCells = mCellCountX * mCellCountY;
1038 int startIndex = page * numCells;
1039 int endIndex = Math.min(startIndex + numCells, mApps.size());
1040 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
1041 // TODO: we can optimize by just re-applying to existing views
1042 layout.removeAllViews();
1043 for (int i = startIndex; i < endIndex; ++i) {
1044 final ApplicationInfo info = mApps.get(i);
1045 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1046 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -07001047 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -07001048 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001049 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001050 icon.setOnLongClickListener(this);
1051
1052 final int index = i - startIndex;
1053 final int x = index % mCellCountX;
1054 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001055 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -07001056 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001057 }
1058 }
1059
Winson Chung80baf5a2010-08-09 16:03:15 -07001060 @Override
1061 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -07001062 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001063 switch (mCustomizationType) {
1064 case WidgetCustomization:
1065 syncWidgetPages();
1066 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001067 case ShortcutCustomization:
1068 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001069 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001070 break;
1071 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001072 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -07001073 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001074 case ApplicationCustomization:
1075 syncAppPages();
1076 centerPagedViewCellLayouts = false;
1077 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001078 default:
1079 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001080 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001081 break;
1082 }
1083
1084 // only try and center the page if there is one page
1085 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001086 if (centerPagedViewCellLayouts) {
1087 if (childCount == 1) {
1088 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1089 layout.enableCenteredContent(true);
1090 } else {
1091 for (int i = 0; i < childCount; ++i) {
1092 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1093 layout.enableCenteredContent(false);
1094 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001095 }
1096 }
1097
1098 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001099 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001100 }
1101
1102 @Override
1103 public void syncPageItems(int page) {
1104 switch (mCustomizationType) {
1105 case WidgetCustomization:
1106 syncWidgetPageItems(page);
1107 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001108 case ShortcutCustomization:
1109 syncListPageItems(page, mShortcutList);
1110 break;
1111 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001112 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001113 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001114 case ApplicationCustomization:
1115 syncAppPageItems(page);
1116 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001117 }
1118 }
Winson Chunge3193b92010-09-10 11:44:42 -07001119
Winson Chungd0d43012010-09-26 17:26:45 -07001120 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001121 protected int getAssociatedLowerPageBound(int page) {
1122 return 0;
1123 }
Winson Chungd0d43012010-09-26 17:26:45 -07001124 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001125 protected int getAssociatedUpperPageBound(int page) {
1126 return getChildCount();
1127 }
Winson Chungd0d43012010-09-26 17:26:45 -07001128
1129 @Override
1130 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001131 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001132 return true;
1133 }
1134
1135 @Override
1136 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1137 return true;
1138 }
1139
1140 @Override
1141 public void onDestroyActionMode(ActionMode mode) {
1142 endChoiceMode();
1143 }
1144
1145 @Override
1146 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1147 return false;
1148 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001149}