blob: 62dcf4a0dffd574a9e8dbe2da8abfebb830868b7 [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;
Winson Chung80baf5a2010-08-09 16:03:15 -070063
Adam Cohen120980b2010-12-08 11:05:37 -080064import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070065
Adam Cohen7b9d3a62010-12-07 21:49:34 -080066
Winson Chung80baf5a2010-08-09 16:03:15 -070067public class CustomizePagedView extends PagedView
Michael Jurka7426c422010-11-11 15:23:47 -080068 implements View.OnLongClickListener, View.OnClickListener, View.OnTouchListener,
Winson Chungd0d43012010-09-26 17:26:45 -070069 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070070
71 public enum CustomizationType {
72 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070073 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070074 WallpaperCustomization,
75 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070076 }
77
78 private static final String TAG = "CustomizeWorkspace";
79 private static final boolean DEBUG = false;
80
Michael Jurka7426c422010-11-11 15:23:47 -080081 private View mLastTouchedItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070082 private Launcher mLauncher;
83 private DragController mDragController;
84 private PackageManager mPackageManager;
Michael Jurka7426c422010-11-11 15:23:47 -080085 private boolean mIsDragging;
86 private float mDragSlopeThreshold;
Winson Chung80baf5a2010-08-09 16:03:15 -070087
88 private CustomizationType mCustomizationType;
89
Winson Chunge3193b92010-09-10 11:44:42 -070090 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
91 private PagedViewCellLayout mWorkspaceWidgetLayout;
92
93 // The mapping between the pages and the widgets that will be laid out on them
94 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
95
Winson Chung45e1d6e2010-11-09 17:19:49 -080096 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070097 private int mMaxWidgetWidth;
98
Winson Chung45e1d6e2010-11-09 17:19:49 -080099 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -0700100 private int mMaxWidgetsCellHSpan;
101
Winson Chung45e1d6e2010-11-09 17:19:49 -0800102 // The size of the items on the wallpaper tab
103 private int mWallpaperCellHSpan;
104
Winson Chunge3193b92010-09-10 11:44:42 -0700105 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700106 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700107 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700108 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700109 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700110
Winson Chunge3193b92010-09-10 11:44:42 -0700111 private static final int sMinWidgetCellHSpan = 2;
112 private static final int sMaxWidgetCellHSpan = 4;
113
Michael Jurka3125d9d2010-09-27 11:30:20 -0700114 private int mChoiceModeTitleText;
115
Winson Chunge3193b92010-09-10 11:44:42 -0700116 // The scale factor for widget previews inside the widget drawer
117 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700118
119 private final Canvas mCanvas = new Canvas();
120 private final LayoutInflater mInflater;
121
Adam Cohen120980b2010-12-08 11:05:37 -0800122 private final float mTmpFloatPos[] = new float[2];
123 private final float ANIMATION_SCALE = 0.5f;
124 private final int ANIMATION_DURATION = 400;
125 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
126 private ScaleAlphaInterpolator mScaleAlphaInterpolator = new ScaleAlphaInterpolator();
127
Winson Chung80baf5a2010-08-09 16:03:15 -0700128 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700129 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700130 }
131
132 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700133 this(context, attrs, 0);
134 }
135
136 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
137 super(context, attrs, defStyle);
138
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700139 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800140 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
141 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700142 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
143 a.recycle();
144 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
145 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
146 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700147 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800148
Winson Chung80baf5a2010-08-09 16:03:15 -0700149 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700150 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
151 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700152 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700153
Michael Jurka7426c422010-11-11 15:23:47 -0800154 final Resources r = context.getResources();
155 mDragSlopeThreshold =
156 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f;
157
Winson Chung80baf5a2010-08-09 16:03:15 -0700158 setVisibility(View.GONE);
159 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700160 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700161 }
162
Winson Chung7da10252010-10-28 16:07:04 -0700163 @Override
164 protected void init() {
165 super.init();
166 mCenterPagesVertically = false;
167 }
168
Winson Chung80baf5a2010-08-09 16:03:15 -0700169 public void setLauncher(Launcher launcher) {
170 Context context = getContext();
171 mLauncher = launcher;
172 mPackageManager = context.getPackageManager();
173 }
174
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700175 /**
176 * Sets the list of applications that launcher has loaded.
177 */
178 public void setApps(ArrayList<ApplicationInfo> list) {
179 mApps = list;
180 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700181
182 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700183 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700184 }
185
186 /**
187 * Convenience function to add new items to the set of applications that were previously loaded.
188 * Called by both updateApps() and setApps().
189 */
190 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
191 // we add it in place, in alphabetical order
192 final int count = list.size();
193 for (int i = 0; i < count; ++i) {
194 final ApplicationInfo info = list.get(i);
195 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
196 if (index < 0) {
197 mApps.add(-(index + 1), info);
198 }
199 }
200 }
201
202 /**
203 * Adds new applications to the loaded list, and notifies the paged view to update itself.
204 */
205 public void addApps(ArrayList<ApplicationInfo> list) {
206 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700207
208 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700209 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700210 }
211
212 /**
213 * Convenience function to remove items to the set of applications that were previously loaded.
214 * Called by both updateApps() and removeApps().
215 */
216 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
217 // loop through all the apps and remove apps that have the same component
218 final int length = list.size();
219 for (int i = 0; i < length; ++i) {
220 final ApplicationInfo info = list.get(i);
221 int removeIndex = findAppByComponent(mApps, info);
222 if (removeIndex > -1) {
223 mApps.remove(removeIndex);
224 mPageViewIconCache.removeOutline(info);
225 }
226 }
227 }
228
229 /**
230 * Removes applications from the loaded list, and notifies the paged view to update itself.
231 */
232 public void removeApps(ArrayList<ApplicationInfo> list) {
233 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700234
235 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700236 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700237 }
238
239 /**
240 * Updates a set of applications from the loaded list, and notifies the paged view to update
241 * itself.
242 */
243 public void updateApps(ArrayList<ApplicationInfo> list) {
244 // We remove and re-add the updated applications list because it's properties may have
245 // changed (ie. the title), and this will ensure that the items will be in their proper
246 // place in the list.
247 removeAppsWithoutInvalidate(list);
248 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700249
250 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700251 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700252 }
253
254 /**
255 * Convenience function to find matching ApplicationInfos for removal.
256 */
257 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
258 ComponentName removeComponent = item.intent.getComponent();
259 final int length = list.size();
260 for (int i = 0; i < length; ++i) {
261 ApplicationInfo info = list.get(i);
262 if (info.intent.getComponent().equals(removeComponent)) {
263 return i;
264 }
265 }
266 return -1;
267 }
268
Winson Chung80baf5a2010-08-09 16:03:15 -0700269 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700270 // get the list of widgets
271 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
272 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
273 @Override
274 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
275 return object1.label.compareTo(object2.label);
276 }
277 });
278
279 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
280 @Override
281 public int compare(ResolveInfo object1, ResolveInfo object2) {
282 return object1.loadLabel(mPackageManager).toString().compareTo(
283 object2.loadLabel(mPackageManager).toString());
284 }
285 };
286
Winson Chung80baf5a2010-08-09 16:03:15 -0700287 // get the list of shortcuts
288 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
289 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
290 Collections.sort(mShortcutList, resolveInfoComparator);
291
Winson Chunge8878e32010-09-15 20:37:09 -0700292 // get the list of wallpapers
293 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800294 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
295 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700296 Collections.sort(mWallpaperList, resolveInfoComparator);
297
Winson Chung10fefb12010-11-01 11:57:06 -0700298 invalidatePageDataAndIconCache();
299 }
300
301 private void invalidatePageDataAndIconCache() {
302 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700303 mPageViewIconCache.clear();
304
Winson Chunge3193b92010-09-10 11:44:42 -0700305 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700306 invalidatePageData();
307 }
308
309 public void setDragController(DragController dragger) {
310 mDragController = dragger;
311 }
312
313 public void setCustomizationFilter(CustomizationType filterType) {
314 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700315 setCurrentPage(0);
Winson Chungbbc60d82010-11-11 16:34:41 -0800316 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700317 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700318
319 // End the current choice mode so that we don't carry selections across tabs
320 endChoiceMode();
Winson Chung649a4ca2010-11-18 10:38:13 -0800321 // Reset the touch item (if we are mid-dragging)
322 mLastTouchedItem = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700323 }
324
325 @Override
326 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700327 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700328 }
329
330 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800331 public void onDragViewVisible() {
332 }
333
Adam Cohen120980b2010-12-08 11:05:37 -0800334 class ScaleAlphaInterpolator implements Interpolator {
335 public float getInterpolation(float input) {
336 float pivot = 0.5f;
337 if (input < pivot) {
338 return 0;
339 } else {
340 return (input - pivot)/(1 - pivot);
341 }
342 }
343 }
344
345 private void animateItemOntoScreen(View dragView,
346 final CellLayout layout, final ItemInfo info) {
347 mTmpFloatPos[0] = layout.getWidth() / 2;
348 mTmpFloatPos[1] = layout.getHeight() / 2;
349 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
350
351 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
352 final View dragCopy = dragLayer.createDragView(dragView);
353 dragCopy.setAlpha(1.0f);
354
355 int dragViewWidth = dragView.getMeasuredWidth();
356 int dragViewHeight = dragView.getMeasuredHeight();
357 float heightOffset = 0;
358 float widthOffset = 0;
359
360 if (dragView instanceof ImageView) {
361 Drawable d = ((ImageView) dragView).getDrawable();
362 int width = d.getIntrinsicWidth();
363 int height = d.getIntrinsicHeight();
364
365 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
366 float f = (dragViewWidth / (width * 1.0f));
367 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
368 } else {
369 float f = (dragViewHeight / (height * 1.0f));
370 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
371 }
372 }
373
374 float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
375 float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
376
377 ObjectAnimator posAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
378 PropertyValuesHolder.ofFloat("x", toX),
379 PropertyValuesHolder.ofFloat("y", toY));
380 posAnim.setInterpolator(mQuintEaseOutInterpolator);
381 posAnim.setDuration(ANIMATION_DURATION);
382
383 posAnim.addListener(new AnimatorListener() {
384 public void onAnimationCancel(Animator animation) {}
385 public void onAnimationRepeat(Animator animation) {}
386 public void onAnimationStart(Animator animation) {}
387
388 public void onAnimationEnd(Animator animation) {
389 dragLayer.removeView(dragCopy);
390 mLauncher.addExternalItemToScreen(info, layout);
391 post(new Runnable() {
392 public void run() {
393 layout.animateDrop();
394 }
395 });
396 }
397 });
398
399 ObjectAnimator scaleAlphaAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
400 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
401 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
402 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
403 scaleAlphaAnim.setInterpolator(mScaleAlphaInterpolator);
404 scaleAlphaAnim.setDuration(ANIMATION_DURATION);
405
406 posAnim.start();
407 scaleAlphaAnim.start();
408 }
409
Patrick Dubroya669d792010-11-23 14:40:33 -0800410 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800411 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800412 // Return early if this is not initiated from a touch
413 if (!v.isInTouchMode()) return;
414 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800415 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700416
Winson Chungd0d43012010-09-26 17:26:45 -0700417 // On certain pages, we allow single tap to mark items as selected so that they can be
418 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700419 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700420 switch (mCustomizationType) {
421 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700422 mChoiceModeTitleText = R.string.cab_widget_selection_text;
423 enterChoiceMode = true;
424 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700425 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700426 mChoiceModeTitleText = R.string.cab_app_selection_text;
427 enterChoiceMode = true;
428 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700429 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700430 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
431 enterChoiceMode = true;
432 break;
433 default:
434 break;
435 }
Winson Chungd0d43012010-09-26 17:26:45 -0700436
Michael Jurka3125d9d2010-09-27 11:30:20 -0700437 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700438 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700439
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700440 Workspace w = mLauncher.getWorkspace();
441 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
442 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800443 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700444
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700445 animateClickFeedback(v, new Runnable() {
446 @Override
447 public void run() {
Adam Cohen120980b2010-12-08 11:05:37 -0800448 animateItemOntoScreen(dragView, cl, itemInfo);
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700449 }
450 });
Winson Chungd0d43012010-09-26 17:26:45 -0700451 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700452 }
453
454 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700455 switch (mCustomizationType) {
456 case WallpaperCustomization:
457 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700458 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700459 animateClickFeedback(v, new Runnable() {
460 @Override
461 public void run() {
462 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700463 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700464 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
465 ComponentName name = new ComponentName(info.activityInfo.packageName,
466 info.activityInfo.name);
467 createWallpapersIntent.setComponent(name);
468 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700469 }
470 });
Winson Chungd0d43012010-09-26 17:26:45 -0700471 break;
472 default:
473 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700474 }
475 }
476
477 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700478 public boolean onLongClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800479 // Return early if this is not initiated from a touch
480 if (!v.isInTouchMode()) return false;
481 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800482 if (mNextPage != INVALID_PAGE) return false;
Michael Jurka7426c422010-11-11 15:23:47 -0800483 return beginDragging(v);
484 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700485
Michael Jurka7426c422010-11-11 15:23:47 -0800486 @Override
487 public boolean onTouch(View v, MotionEvent event) {
488 mLastTouchedItem = v;
489 return false;
490 }
491
492 /*
493 * Determines if we should change the touch state to start scrolling after the
494 * user moves their touch point too far.
495 */
496 protected void determineScrollingStart(MotionEvent ev) {
497 if (!mIsDragging) super.determineScrollingStart(ev);
498 }
499
500 /*
501 * Determines if we should change the touch state to start dragging after the
502 * user moves their touch point far enough.
503 */
504 protected void determineDraggingStart(MotionEvent ev) {
505 /*
506 * Locally do absolute value. mLastMotionX is set to the y value
507 * of the down event.
508 */
509 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
510 final float x = ev.getX(pointerIndex);
511 final float y = ev.getY(pointerIndex);
512 final int xDiff = (int) Math.abs(x - mLastMotionX);
513 final int yDiff = (int) Math.abs(y - mLastMotionY);
514
515 final int touchSlop = mTouchSlop;
516 boolean yMoved = yDiff > touchSlop;
517 boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
518
Winson Chung649a4ca2010-11-18 10:38:13 -0800519 if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
Michael Jurka7426c422010-11-11 15:23:47 -0800520 // Drag if the user moved far enough along the Y axis
521 beginDragging(mLastTouchedItem);
522
523 // Cancel any pending longpress
524 if (mAllowLongPress) {
525 mAllowLongPress = false;
526 // Try canceling the long press. It could also have been scheduled
527 // by a distant descendant, so use the mAllowLongPress flag to block
528 // everything
529 final View currentPage = getPageAt(mCurrentPage);
530 if (currentPage != null) {
531 currentPage.cancelLongPress();
532 }
533 }
534 }
535 }
536
537 @Override
538 public boolean onInterceptTouchEvent(MotionEvent ev) {
539 final int action = ev.getAction();
540 switch (action & MotionEvent.ACTION_MASK) {
541 case MotionEvent.ACTION_DOWN:
542 mIsDragging = false;
543 break;
544 case MotionEvent.ACTION_MOVE:
545 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
546 determineDraggingStart(ev);
547 }
548 break;
549 }
550 return super.onInterceptTouchEvent(ev);
551 }
552
553 @Override
554 public boolean onTouchEvent(MotionEvent ev) {
555 final int action = ev.getAction();
556 switch (action & MotionEvent.ACTION_MASK) {
557 case MotionEvent.ACTION_DOWN:
558 mIsDragging = false;
559 break;
560 case MotionEvent.ACTION_MOVE:
561 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
562 determineDraggingStart(ev);
563 }
564 break;
565 }
566 return super.onTouchEvent(ev);
567 }
568
Michael Jurkaaf91de02010-11-23 16:23:58 -0800569 Bitmap drawableToBitmap(Drawable d) {
570 Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(),
571 Bitmap.Config.ARGB_8888);
572 Canvas c = new Canvas(b);
573 d.draw(c);
574 return b;
575 }
576
Adam Cohen120980b2010-12-08 11:05:37 -0800577 private View getDragView(View v) {
578 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
579 v.findViewById(R.id.widget_preview) : v;
580 }
581
Michael Jurka7426c422010-11-11 15:23:47 -0800582 private boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700583 // End the current choice mode before we start dragging anything
584 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
585 endChoiceMode();
586 }
Michael Jurka7426c422010-11-11 15:23:47 -0800587 mIsDragging = true;
Winson Chungd0d43012010-09-26 17:26:45 -0700588
Winson Chung80baf5a2010-08-09 16:03:15 -0700589 switch (mCustomizationType) {
590 case WidgetCustomization:
Adam Cohen120980b2010-12-08 11:05:37 -0800591 // Get the widget preview as the drag representation
Michael Jurka3e7c7632010-10-02 16:01:03 -0700592 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Adam Cohen120980b2010-12-08 11:05:37 -0800593 final View dragView = v.findViewById(R.id.widget_preview);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700594
Michael Jurka3e7c7632010-10-02 16:01:03 -0700595 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
596 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
Adam Cohen120980b2010-12-08 11:05:37 -0800597 mDragController.startDrag(dragView, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chunge3193b92010-09-10 11:44:42 -0700598
Winson Chung80baf5a2010-08-09 16:03:15 -0700599 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700600 case ShortcutCustomization:
Adam Cohen120980b2010-12-08 11:05:37 -0800601 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Patrick Dubroya669d792010-11-23 14:40:33 -0800602 mDragController.startDrag(v, this, createItemInfo, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700603 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700604 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700605 case ApplicationCustomization:
606 // Pick up the application for dropping
607 ApplicationInfo app = (ApplicationInfo) v.getTag();
608 app = new ApplicationInfo(app);
609
610 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700611 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700612 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700613 }
614 return false;
615 }
616
Winson Chunge3193b92010-09-10 11:44:42 -0700617 /**
618 * Pre-processes the layout of the different widget pages.
619 * @return the number of pages of widgets that we have
620 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700621 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700622 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700623
Winson Chunge3193b92010-09-10 11:44:42 -0700624 // create a new page for the first set of widgets
625 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700626 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700627 mWidgetPages.add(newPage);
628
629 // do this until we have no more widgets to lay out
630 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
631 final int widgetCount = mWidgetList.size();
632 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700633 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700634 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700635
Winson Chunge3193b92010-09-10 11:44:42 -0700636 // determine the size of the current widget
637 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
638 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700639
Winson Chunge3193b92010-09-10 11:44:42 -0700640 // create a new page if necessary
641 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
642 numCellsInRow = 0;
643 newPage = new ArrayList<AppWidgetProviderInfo>();
644 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700645 }
646
Winson Chunge3193b92010-09-10 11:44:42 -0700647 // add the item to the current page
648 newPage.add(info);
649 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700650 }
Winson Chunge3193b92010-09-10 11:44:42 -0700651
Winson Chung80baf5a2010-08-09 16:03:15 -0700652 return mWidgetPages.size();
653 }
654
Winson Chunge3193b92010-09-10 11:44:42 -0700655 /**
Winson Chung7da10252010-10-28 16:07:04 -0700656 * Helper function to draw a drawable to the specified canvas with the specified bounds.
657 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800658 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700659 if (bitmap != null) mCanvas.setBitmap(bitmap);
660 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800661 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700662 d.draw(mCanvas);
663 mCanvas.restore();
664 }
665
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800666 /*
667 * This method fetches an xml file specified in the manifest identified by
668 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
669 * an image which will be used as the wallpaper preview for an activity
670 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
671 * in the customize drawer.
672 */
673 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
674 Drawable d = null;
675
676 ActivityInfo activityInfo = ri.activityInfo;
677 XmlResourceParser parser = null;
678 try {
679 parser = activityInfo.loadXmlMetaData(mPackageManager,
680 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
681 if (parser == null) {
682 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
683 + "wallpaper provider '" + component + '\'');
684 return null;
685 }
686
687 AttributeSet attrs = Xml.asAttributeSet(parser);
688
689 int type;
690 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
691 && type != XmlPullParser.START_TAG) {
692 // drain whitespace, comments, etc.
693 }
694
695 String nodeName = parser.getName();
696 if (!"wallpaper-preview".equals(nodeName)) {
697 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
698 + "wallpaper provider '" + component + '\'');
699 return null;
700 }
701
702 // If metaData was null, we would have returned earlier when getting
703 // the parser No need to do the check here
704 Resources res = mPackageManager.getResourcesForApplication(
705 activityInfo.applicationInfo);
706
707 TypedArray sa = res.obtainAttributes(attrs,
708 com.android.internal.R.styleable.WallpaperPreviewInfo);
709
710 TypedValue value = sa.peekValue(
711 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
712 if (value == null) return null;
713
714 return res.getDrawable(value.resourceId);
715 } catch (Exception e) {
716 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
717 return null;
718 } finally {
719 if (parser != null) parser.close();
720 }
721 }
722
Winson Chung7da10252010-10-28 16:07:04 -0700723 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800724 * This method will extract the preview image specified by the wallpaper source provider (if it
725 * exists) otherwise, it will try to generate a default image preview.
726 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800727 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800728 // To be implemented later: resolving the up-to-date wallpaper thumbnail
729
730 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
731 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
732 Resources resources = mLauncher.getResources();
733
734 // Create a new bitmap to hold the widget preview
735 int width = (int) (dim * sScaleFactor);
736 int height = (int) (dim * sScaleFactor);
737 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800738
739 Drawable background = parseWallpaperPreviewXml(
740 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
741 boolean foundCustomDrawable = background != null;
742
743 if (!foundCustomDrawable) {
744 background = resources.getDrawable(R.drawable.default_widget_preview);
745 }
746
Winson Chung45e1d6e2010-11-09 17:19:49 -0800747 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
748
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800749 // If we don't have a custom icon, we use the app icon on the default background
750 if (!foundCustomDrawable) {
751 try {
752 final IconCache iconCache =
753 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
754 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
755 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800756
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800757 final int iconSize = minDim / 2;
758 final int offset = iconSize / 4;
759 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
760 } catch (Resources.NotFoundException e) {
761 // if we can't find the icon, then just don't draw it
762 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800763 }
764
Winson Chung29d6fea2010-12-01 15:47:31 -0800765 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800766 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
767 return drawable;
768 }
769
770 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700771 * This method will extract the preview image specified by the widget developer (if it exists),
772 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800773 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700774 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800775 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800776 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700777 String packageName = info.provider.getPackageName();
778 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800779 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700780 if (info.previewImage != 0) {
781 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
782 if (drawable == null) {
783 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
784 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700785 }
786 }
787
788 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700789 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
790 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700791 if (drawable == null) {
792 Resources resources = mLauncher.getResources();
793
Winson Chung80baf5a2010-08-09 16:03:15 -0700794 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700795 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
796 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700797 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
798 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
799 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700800
Winson Chung45e1d6e2010-11-09 17:19:49 -0800801 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700802 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700803 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700804 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700805 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700806 }
807 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700808 icon = resources.getDrawable(R.drawable.ic_launcher_application);
809 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700810
Winson Chunge3193b92010-09-10 11:44:42 -0700811 final int iconSize = minDim / 2;
812 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800813 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700814 } catch (Resources.NotFoundException e) {
815 // if we can't find the icon, then just don't draw it
816 }
817
Winson Chung29d6fea2010-12-01 15:47:31 -0800818 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700819 } else {
820 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800821 final float imageWidth = drawable.getIntrinsicWidth();
822 final float imageHeight = drawable.getIntrinsicHeight();
823 final float aspect = (float) imageWidth / imageHeight;
824 final int scaledWidth =
825 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
826 final int scaledHeight =
827 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700828 int width;
829 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800830 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700831 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800832 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700833 } else {
834 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800835 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700836 }
837
838 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
839 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
840
Winson Chung29d6fea2010-12-01 15:47:31 -0800841 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700842 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800843 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
844 newDrawable.getIntrinsicHeight());
845 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700846 }
847
848 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700849 layout.setCellCount(mCellCountX, mCellCountY);
850 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
851 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700852 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700853 }
854
Winson Chunge3193b92010-09-10 11:44:42 -0700855 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700856 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700857 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
858
859 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
860 }
861
Winson Chung80baf5a2010-08-09 16:03:15 -0700862 private void syncWidgetPages() {
863 if (mWidgetList == null) return;
864
Winson Chunge3193b92010-09-10 11:44:42 -0700865 // we need to repopulate with the LinearLayout layout for the widget pages
866 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700867 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700868 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800869 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700870 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700871 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
872 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700873
Winson Chunge22a8e92010-11-12 13:40:58 -0800874 addView(layout, new LinearLayout.LayoutParams(
875 LinearLayout.LayoutParams.WRAP_CONTENT,
876 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700877 }
878 }
879
880 private void syncWidgetPageItems(int page) {
881 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700882 LinearLayout layout = (LinearLayout) getChildAt(page);
883 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700884 final int count = list.size();
885 layout.removeAllViews();
886 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700887 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
888 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800889 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
890 info.minHeight, null);
891 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700892
Winson Chung29d6fea2010-12-01 15:47:31 -0800893 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700894 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800895 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700896 l.setTag(createItemInfo);
897 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800898 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700899 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700900
Winson Chunge3193b92010-09-10 11:44:42 -0700901 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700902 }
903 }
904
Winson Chung45e1d6e2010-11-09 17:19:49 -0800905 private void syncWallpaperPages() {
906 if (mWallpaperList == null) return;
907
908 // We need to repopulate the LinearLayout for the wallpaper pages
909 removeAllViews();
910 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
911 mMaxWidgetsCellHSpan);
912 for (int i = 0; i < numPages; ++i) {
913 LinearLayout layout = new PagedViewExtendedLayout(getContext());
914 layout.setGravity(Gravity.CENTER_HORIZONTAL);
915 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
916 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
917
Winson Chunge22a8e92010-11-12 13:40:58 -0800918 addView(layout, new LinearLayout.LayoutParams(
919 LinearLayout.LayoutParams.WRAP_CONTENT,
920 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800921 }
922 }
923
924 private void syncWallpaperPageItems(int page) {
925 // Load the items on to the pages
926 LinearLayout layout = (LinearLayout) getChildAt(page);
927 layout.removeAllViews();
928 final int count = mWallpaperList.size();
Winson Chungd28ed492010-11-22 14:34:57 -0800929 final int numItemsPerPage = mMaxWidgetsCellHSpan / mWallpaperCellHSpan;
930 final int startIndex = page * numItemsPerPage;
931 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
932 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800933 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800934 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800935
Winson Chung29d6fea2010-12-01 15:47:31 -0800936 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800937 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800938 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800939 l.setTag(info);
940 l.setOnClickListener(this);
941
Winson Chung45e1d6e2010-11-09 17:19:49 -0800942 layout.addView(l);
943 }
944 }
945
Winson Chung80baf5a2010-08-09 16:03:15 -0700946 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700947 // we need to repopulate with PagedViewCellLayouts
948 removeAllViews();
949
Winson Chung80baf5a2010-08-09 16:03:15 -0700950 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700951 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700952 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700953 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
954 setupPage(layout);
955 addView(layout);
956 }
957 }
958
959 private void syncListPageItems(int page, List<ResolveInfo> list) {
960 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700961 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700962 int startIndex = page * numCells;
963 int endIndex = Math.min(startIndex + numCells, list.size());
964 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
965 // TODO: we can optimize by just re-applying to existing views
966 layout.removeAllViews();
967 for (int i = startIndex; i < endIndex; ++i) {
968 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700969 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
970
Winson Chung241c3b42010-08-25 16:53:03 -0700971 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
972 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700973 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Adam Cohen120980b2010-12-08 11:05:37 -0800974 ((LauncherApplication) mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700975 switch (mCustomizationType) {
976 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700977 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700978 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700979 case ShortcutCustomization:
980 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
981 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
982 info.activityInfo.name);
983 icon.setTag(createItemInfo);
984 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800985 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700986 icon.setOnLongClickListener(this);
987 break;
988 default:
989 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700990 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700991
992 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700993 final int x = index % mCellCountX;
994 final int y = index / mCellCountX;
995 setupPage(layout);
996 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
997 }
998 }
999
1000 private void syncAppPages() {
1001 if (mApps == null) return;
1002
1003 // We need to repopulate with PagedViewCellLayouts
1004 removeAllViews();
1005
1006 // Ensure that we have the right number of pages
1007 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
1008 for (int i = 0; i < numPages; ++i) {
1009 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1010 setupPage(layout);
1011 addView(layout);
1012 }
1013 }
1014
1015 private void syncAppPageItems(int page) {
1016 if (mApps == null) return;
1017
1018 // ensure that we have the right number of items on the pages
1019 int numCells = mCellCountX * mCellCountY;
1020 int startIndex = page * numCells;
1021 int endIndex = Math.min(startIndex + numCells, mApps.size());
1022 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
1023 // TODO: we can optimize by just re-applying to existing views
1024 layout.removeAllViews();
1025 for (int i = startIndex; i < endIndex; ++i) {
1026 final ApplicationInfo info = mApps.get(i);
1027 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1028 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -07001029 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -07001030 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001031 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001032 icon.setOnLongClickListener(this);
1033
1034 final int index = i - startIndex;
1035 final int x = index % mCellCountX;
1036 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001037 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -07001038 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001039 }
1040 }
1041
Winson Chung80baf5a2010-08-09 16:03:15 -07001042 @Override
1043 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -07001044 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001045 switch (mCustomizationType) {
1046 case WidgetCustomization:
1047 syncWidgetPages();
1048 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001049 case ShortcutCustomization:
1050 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001051 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001052 break;
1053 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001054 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -07001055 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001056 case ApplicationCustomization:
1057 syncAppPages();
1058 centerPagedViewCellLayouts = false;
1059 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001060 default:
1061 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001062 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001063 break;
1064 }
1065
1066 // only try and center the page if there is one page
1067 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001068 if (centerPagedViewCellLayouts) {
1069 if (childCount == 1) {
1070 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1071 layout.enableCenteredContent(true);
1072 } else {
1073 for (int i = 0; i < childCount; ++i) {
1074 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1075 layout.enableCenteredContent(false);
1076 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001077 }
1078 }
1079
1080 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001081 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001082 }
1083
1084 @Override
1085 public void syncPageItems(int page) {
1086 switch (mCustomizationType) {
1087 case WidgetCustomization:
1088 syncWidgetPageItems(page);
1089 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001090 case ShortcutCustomization:
1091 syncListPageItems(page, mShortcutList);
1092 break;
1093 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001094 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001095 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001096 case ApplicationCustomization:
1097 syncAppPageItems(page);
1098 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001099 }
1100 }
Winson Chunge3193b92010-09-10 11:44:42 -07001101
Winson Chungd0d43012010-09-26 17:26:45 -07001102 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001103 protected int getAssociatedLowerPageBound(int page) {
1104 return 0;
1105 }
Winson Chungd0d43012010-09-26 17:26:45 -07001106 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001107 protected int getAssociatedUpperPageBound(int page) {
1108 return getChildCount();
1109 }
Winson Chungd0d43012010-09-26 17:26:45 -07001110
1111 @Override
1112 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001113 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001114 return true;
1115 }
1116
1117 @Override
1118 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1119 return true;
1120 }
1121
1122 @Override
1123 public void onDestroyActionMode(ActionMode mode) {
1124 endChoiceMode();
1125 }
1126
1127 @Override
1128 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1129 return false;
1130 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001131}