blob: 93e1394af267a11df2c2f013ef61a75ec8af5cbc [file] [log] [blame]
Winson Chung80baf5a2010-08-09 16:03:15 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Michael Jurka72b079e2010-12-10 01:03:53 -080019import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070020
Adam Cohen7b9d3a62010-12-07 21:49:34 -080021import org.xmlpull.v1.XmlPullParser;
22
Adam Cohen120980b2010-12-08 11:05:37 -080023import android.animation.Animator;
Patrick Dubroy047379a2010-12-19 22:02:04 -080024import android.animation.AnimatorListenerAdapter;
Adam Cohen120980b2010-12-08 11:05:37 -080025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
27import android.animation.TimeInterpolator;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080028import android.app.WallpaperManager;
Winson Chung80baf5a2010-08-09 16:03:15 -070029import android.appwidget.AppWidgetManager;
30import android.appwidget.AppWidgetProviderInfo;
31import android.content.ComponentName;
32import android.content.Context;
33import android.content.Intent;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080034import android.content.pm.ActivityInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
37import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070038import android.content.res.TypedArray;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080039import android.content.res.XmlResourceParser;
Winson Chung80baf5a2010-08-09 16:03:15 -070040import android.graphics.Bitmap;
Michael Jurkaaf91de02010-11-23 16:23:58 -080041import android.graphics.Bitmap.Config;
Winson Chungcd4bc492010-12-09 18:52:32 -080042import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070043import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.util.AttributeSet;
45import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080046import android.util.Slog;
47import android.util.TypedValue;
48import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070049import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070050import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070051import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070052import android.view.Menu;
53import android.view.MenuItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070054import android.view.View;
Adam Cohen120980b2010-12-08 11:05:37 -080055import android.view.animation.DecelerateInterpolator;
56import android.view.animation.Interpolator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080057import android.widget.Checkable;
Winson Chunge3193b92010-09-10 11:44:42 -070058import android.widget.ImageView;
59import android.widget.LinearLayout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080060import android.widget.TextView;
Winson Chung80baf5a2010-08-09 16:03:15 -070061
Michael Jurka72b079e2010-12-10 01:03:53 -080062import java.util.ArrayList;
63import java.util.Collections;
64import java.util.Comparator;
65import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070066
Adam Cohen7b9d3a62010-12-07 21:49:34 -080067
Michael Jurka72b079e2010-12-10 01:03:53 -080068public class CustomizePagedView extends PagedViewWithDraggableItems
69 implements View.OnClickListener, 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";
Winson Chung80baf5a2010-08-09 16:03:15 -070079
80 private Launcher mLauncher;
81 private DragController mDragController;
82 private PackageManager mPackageManager;
83
84 private CustomizationType mCustomizationType;
85
Winson Chunge3193b92010-09-10 11:44:42 -070086 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
87 private PagedViewCellLayout mWorkspaceWidgetLayout;
88
89 // The mapping between the pages and the widgets that will be laid out on them
90 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
91
Winson Chung45e1d6e2010-11-09 17:19:49 -080092 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070093 private int mMaxWidgetWidth;
94
Winson Chung45e1d6e2010-11-09 17:19:49 -080095 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -070096 private int mMaxWidgetsCellHSpan;
97
Winson Chung45e1d6e2010-11-09 17:19:49 -080098 // The size of the items on the wallpaper tab
99 private int mWallpaperCellHSpan;
100
Winson Chung78bd53c2010-12-09 13:50:24 -0800101 // The max number of wallpaper cells to take a "page" of wallpaper items
102 private int mMaxWallpaperCellHSpan;
103
Winson Chunge3193b92010-09-10 11:44:42 -0700104 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700105 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700106 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700107 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700108 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700109
Winson Chunge3193b92010-09-10 11:44:42 -0700110 private static final int sMinWidgetCellHSpan = 2;
111 private static final int sMaxWidgetCellHSpan = 4;
112
Michael Jurka3125d9d2010-09-27 11:30:20 -0700113 private int mChoiceModeTitleText;
114
Winson Chunge3193b92010-09-10 11:44:42 -0700115 // The scale factor for widget previews inside the widget drawer
116 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700117
118 private final Canvas mCanvas = new Canvas();
119 private final LayoutInflater mInflater;
120
Adam Cohen120980b2010-12-08 11:05:37 -0800121 private final float mTmpFloatPos[] = new float[2];
122 private final float ANIMATION_SCALE = 0.5f;
123 private final int ANIMATION_DURATION = 400;
124 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
125 private ScaleAlphaInterpolator mScaleAlphaInterpolator = new ScaleAlphaInterpolator();
126
Winson Chung80baf5a2010-08-09 16:03:15 -0700127 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700128 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700129 }
130
131 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700132 this(context, attrs, 0);
133 }
134
135 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
136 super(context, attrs, defStyle);
137
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700138 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800139 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
140 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800141 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
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();
Michael Jurka72b079e2010-12-10 01:03:53 -0800155 setDragSlopeThreshold(
156 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7426c422010-11-11 15:23:47 -0800157
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 Chung04998342011-01-05 13:54:43 -0800183 mPageViewIconCache.retainAllApps(list);
184 invalidatePageData();
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 Chung04998342011-01-05 13:54:43 -0800210 invalidatePageData();
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);
Winson Chung04998342011-01-05 13:54:43 -0800225 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700226 }
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 Chung04998342011-01-05 13:54:43 -0800237 invalidatePageData();
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 Chung04998342011-01-05 13:54:43 -0800252 invalidatePageData();
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 Chung04998342011-01-05 13:54:43 -0800299 ArrayList<ResolveInfo> retainShortcutList = new ArrayList<ResolveInfo>(mShortcutList);
300 retainShortcutList.addAll(mWallpaperList);
301 mPageViewIconCache.retainAllShortcuts(retainShortcutList);
302 mPageViewIconCache.retainAllAppWidgets(mWidgetList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700303 invalidatePageData();
304 }
305
306 public void setDragController(DragController dragger) {
307 mDragController = dragger;
308 }
309
310 public void setCustomizationFilter(CustomizationType filterType) {
Winson Chung7d1fcbc2011-01-04 10:22:20 -0800311 cancelDragging();
Winson Chung94569f42011-01-17 14:09:17 -0800312 mCustomizationType = filterType;
Winson Chunga12a2502010-12-20 14:41:35 -0800313 if (getChildCount() > 0) {
314 setCurrentPage(0);
315 updateCurrentPageScroll();
316 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700317
Winson Chunga12a2502010-12-20 14:41:35 -0800318 // End the current choice mode so that we don't carry selections across tabs
319 endChoiceMode();
320 }
321 }
322
323 public CustomizationType getCustomizationFilter() {
324 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700325 }
326
327 @Override
328 public void onDropCompleted(View target, boolean success) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800329 resetCheckedGrandchildren();
Patrick Dubroy7bccb422011-01-20 14:50:55 -0800330 mLauncher.getWorkspace().onDragStopped(success);
Winson Chung400438b2011-01-16 17:53:48 -0800331 mLauncher.unlockScreenOrientation();
Winson Chung80baf5a2010-08-09 16:03:15 -0700332 }
333
334 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800335 public void onDragViewVisible() {
336 }
337
Adam Cohen120980b2010-12-08 11:05:37 -0800338 class ScaleAlphaInterpolator implements Interpolator {
339 public float getInterpolation(float input) {
340 float pivot = 0.5f;
341 if (input < pivot) {
342 return 0;
343 } else {
344 return (input - pivot)/(1 - pivot);
345 }
346 }
347 }
348
349 private void animateItemOntoScreen(View dragView,
350 final CellLayout layout, final ItemInfo info) {
351 mTmpFloatPos[0] = layout.getWidth() / 2;
352 mTmpFloatPos[1] = layout.getHeight() / 2;
353 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
354
355 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
356 final View dragCopy = dragLayer.createDragView(dragView);
357 dragCopy.setAlpha(1.0f);
358
359 int dragViewWidth = dragView.getMeasuredWidth();
360 int dragViewHeight = dragView.getMeasuredHeight();
361 float heightOffset = 0;
362 float widthOffset = 0;
363
364 if (dragView instanceof ImageView) {
365 Drawable d = ((ImageView) dragView).getDrawable();
366 int width = d.getIntrinsicWidth();
367 int height = d.getIntrinsicHeight();
368
369 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
370 float f = (dragViewWidth / (width * 1.0f));
371 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
372 } else {
373 float f = (dragViewHeight / (height * 1.0f));
374 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
375 }
376 }
377
378 float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
379 float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
380
381 ObjectAnimator posAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
382 PropertyValuesHolder.ofFloat("x", toX),
383 PropertyValuesHolder.ofFloat("y", toY));
384 posAnim.setInterpolator(mQuintEaseOutInterpolator);
385 posAnim.setDuration(ANIMATION_DURATION);
386
Patrick Dubroy047379a2010-12-19 22:02:04 -0800387 posAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohen120980b2010-12-08 11:05:37 -0800388 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() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800448 cl.calculateSpans(itemInfo);
449 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
450 animateItemOntoScreen(dragView, cl, itemInfo);
451 } else {
452 mLauncher.showOutOfSpaceMessage();
453 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700454 }
455 });
Winson Chungd0d43012010-09-26 17:26:45 -0700456 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700457 }
458
459 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700460 switch (mCustomizationType) {
461 case WallpaperCustomization:
462 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700463 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700464 animateClickFeedback(v, new Runnable() {
465 @Override
466 public void run() {
467 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700468 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700469 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
470 ComponentName name = new ComponentName(info.activityInfo.packageName,
471 info.activityInfo.name);
472 createWallpapersIntent.setComponent(name);
473 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700474 }
475 });
Winson Chungd0d43012010-09-26 17:26:45 -0700476 break;
477 default:
478 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700479 }
480 }
481
Michael Jurkac4e772e2011-02-10 13:32:01 -0800482 Bitmap drawableToBitmap(Drawable d) {
483 int w = d.getIntrinsicWidth();
484 int h = d.getIntrinsicHeight();
485 Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
486 renderDrawableToBitmap(d, b, 0, 0, w, h);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800487 return b;
488 }
489
Adam Cohen120980b2010-12-08 11:05:37 -0800490 private View getDragView(View v) {
491 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
492 v.findViewById(R.id.widget_preview) : v;
493 }
494
Michael Jurka72b079e2010-12-10 01:03:53 -0800495 protected boolean beginDragging(View v) {
Winson Chung304dcde2011-01-07 11:17:23 -0800496 if (!v.isInTouchMode()) return false;
497 if (!super.beginDragging(v)) return false;
498
Winson Chungd0d43012010-09-26 17:26:45 -0700499 // End the current choice mode before we start dragging anything
500 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
501 endChoiceMode();
502 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800503 boolean result = false;
Winson Chung400438b2011-01-16 17:53:48 -0800504 mLauncher.lockScreenOrientation();
Winson Chung80baf5a2010-08-09 16:03:15 -0700505 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800506 case WidgetCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800507 if (v instanceof PagedViewWidget) {
508 // Get the widget preview as the drag representation
509 final LinearLayout l = (LinearLayout) v;
510 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
Michael Jurkac4e772e2011-02-10 13:32:01 -0800511 Bitmap b = drawableToBitmap(i.getDrawable());
Winson Chung94569f42011-01-17 14:09:17 -0800512 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700513
Winson Chung94569f42011-01-17 14:09:17 -0800514 int[] spanXY = CellLayout.rectToCell(
515 getResources(), createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
516 createWidgetInfo.spanX = spanXY[0];
517 createWidgetInfo.spanY = spanXY[1];
518 mLauncher.getWorkspace().onDragStartedWithItemSpans(spanXY[0], spanXY[1], b);
519 mDragController.startDrag(
520 i, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
521 b.recycle();
522 result = true;
523 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800524 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800525 }
526 case ShortcutCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800527 if (v instanceof PagedViewIcon) {
528 // get icon (top compound drawable, index is 1)
529 final TextView tv = (TextView) v;
530 final Drawable icon = tv.getCompoundDrawables()[1];
Michael Jurkac4e772e2011-02-10 13:32:01 -0800531 Bitmap b = drawableToBitmap(icon);
Winson Chung94569f42011-01-17 14:09:17 -0800532 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chungcd4bc492010-12-09 18:52:32 -0800533
Winson Chung94569f42011-01-17 14:09:17 -0800534 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
535 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
536 null);
537 b.recycle();
538 result = true;
539 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800540 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800541 }
542 case ApplicationCustomization: {
Winson Chung94569f42011-01-17 14:09:17 -0800543 if (v instanceof PagedViewIcon) {
544 // Pick up the application for dropping
545 // get icon (top compound drawable, index is 1)
546 final TextView tv = (TextView) v;
547 final Drawable icon = tv.getCompoundDrawables()[1];
Michael Jurkac4e772e2011-02-10 13:32:01 -0800548 Bitmap b = drawableToBitmap(icon);
Winson Chung94569f42011-01-17 14:09:17 -0800549 ApplicationInfo app = (ApplicationInfo) v.getTag();
550 app = new ApplicationInfo(app);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700551
Winson Chung94569f42011-01-17 14:09:17 -0800552 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
553 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
554 b.recycle();
555 result = true;
556 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800557 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700558 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800559 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800560
561 // We toggle the checked state _after_ we create the view for the drag in case toggling the
562 // checked state changes the view's look
Winson Chung94569f42011-01-17 14:09:17 -0800563 if (result && (v instanceof Checkable)) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800564 // In preparation for drag, we always reset the checked grand children regardless of
565 // what choice mode we are in
566 resetCheckedGrandchildren();
567
568 // Toggle the selection on the dragged app
569 Checkable checkable = (Checkable) v;
570
571 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
572 // of the drag of the item. (The fade-in will occur when all checked states are
573 // disabled when dragging ends)
574 checkable.toggle();
575 }
576
577 return result;
Winson Chung80baf5a2010-08-09 16:03:15 -0700578 }
579
Winson Chunge3193b92010-09-10 11:44:42 -0700580 /**
581 * Pre-processes the layout of the different widget pages.
582 * @return the number of pages of widgets that we have
583 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700584 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700585 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700586
Winson Chunge3193b92010-09-10 11:44:42 -0700587 // create a new page for the first set of widgets
588 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700589 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700590 mWidgetPages.add(newPage);
591
592 // do this until we have no more widgets to lay out
593 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
594 final int widgetCount = mWidgetList.size();
595 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700596 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700597 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700598
Winson Chunge3193b92010-09-10 11:44:42 -0700599 // determine the size of the current widget
600 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
601 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700602
Winson Chunge3193b92010-09-10 11:44:42 -0700603 // create a new page if necessary
604 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
605 numCellsInRow = 0;
606 newPage = new ArrayList<AppWidgetProviderInfo>();
607 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700608 }
609
Winson Chunge3193b92010-09-10 11:44:42 -0700610 // add the item to the current page
611 newPage.add(info);
612 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700613 }
Winson Chunge3193b92010-09-10 11:44:42 -0700614
Winson Chung80baf5a2010-08-09 16:03:15 -0700615 return mWidgetPages.size();
616 }
617
Winson Chunge3193b92010-09-10 11:44:42 -0700618 /**
Winson Chung7da10252010-10-28 16:07:04 -0700619 * Helper function to draw a drawable to the specified canvas with the specified bounds.
620 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800621 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700622 if (bitmap != null) mCanvas.setBitmap(bitmap);
623 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800624 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700625 d.draw(mCanvas);
626 mCanvas.restore();
627 }
628
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800629 /*
630 * This method fetches an xml file specified in the manifest identified by
631 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
632 * an image which will be used as the wallpaper preview for an activity
633 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
634 * in the customize drawer.
635 */
636 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800637 ActivityInfo activityInfo = ri.activityInfo;
638 XmlResourceParser parser = null;
639 try {
640 parser = activityInfo.loadXmlMetaData(mPackageManager,
641 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
642 if (parser == null) {
643 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
644 + "wallpaper provider '" + component + '\'');
645 return null;
646 }
647
648 AttributeSet attrs = Xml.asAttributeSet(parser);
649
650 int type;
651 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
652 && type != XmlPullParser.START_TAG) {
653 // drain whitespace, comments, etc.
654 }
655
656 String nodeName = parser.getName();
657 if (!"wallpaper-preview".equals(nodeName)) {
658 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
659 + "wallpaper provider '" + component + '\'');
660 return null;
661 }
662
663 // If metaData was null, we would have returned earlier when getting
664 // the parser No need to do the check here
665 Resources res = mPackageManager.getResourcesForApplication(
666 activityInfo.applicationInfo);
667
668 TypedArray sa = res.obtainAttributes(attrs,
669 com.android.internal.R.styleable.WallpaperPreviewInfo);
670
671 TypedValue value = sa.peekValue(
672 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
673 if (value == null) return null;
674
675 return res.getDrawable(value.resourceId);
676 } catch (Exception e) {
677 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
678 return null;
679 } finally {
680 if (parser != null) parser.close();
681 }
682 }
683
Winson Chung7da10252010-10-28 16:07:04 -0700684 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800685 * This method will extract the preview image specified by the wallpaper source provider (if it
686 * exists) otherwise, it will try to generate a default image preview.
687 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800688 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800689 // To be implemented later: resolving the up-to-date wallpaper thumbnail
690
691 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
692 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
693 Resources resources = mLauncher.getResources();
694
695 // Create a new bitmap to hold the widget preview
696 int width = (int) (dim * sScaleFactor);
697 int height = (int) (dim * sScaleFactor);
698 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800699
700 Drawable background = parseWallpaperPreviewXml(
701 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
702 boolean foundCustomDrawable = background != null;
703
704 if (!foundCustomDrawable) {
705 background = resources.getDrawable(R.drawable.default_widget_preview);
706 }
707
Winson Chung45e1d6e2010-11-09 17:19:49 -0800708 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
709
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800710 // If we don't have a custom icon, we use the app icon on the default background
711 if (!foundCustomDrawable) {
712 try {
713 final IconCache iconCache =
714 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
715 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
716 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800717
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800718 final int iconSize = minDim / 2;
719 final int offset = iconSize / 4;
720 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
721 } catch (Resources.NotFoundException e) {
722 // if we can't find the icon, then just don't draw it
723 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800724 }
725
Winson Chung29d6fea2010-12-01 15:47:31 -0800726 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800727 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
728 return drawable;
729 }
730
731 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700732 * This method will extract the preview image specified by the widget developer (if it exists),
733 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800734 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700735 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800736 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800737 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700738 String packageName = info.provider.getPackageName();
739 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800740 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700741 if (info.previewImage != 0) {
742 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
743 if (drawable == null) {
744 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
745 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700746 }
747 }
748
749 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700750 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
751 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700752 if (drawable == null) {
753 Resources resources = mLauncher.getResources();
754
Winson Chung80baf5a2010-08-09 16:03:15 -0700755 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700756 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
757 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700758 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
759 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
760 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700761
Winson Chung45e1d6e2010-11-09 17:19:49 -0800762 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700763 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700764 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700765 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700766 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700767 }
768 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700769 icon = resources.getDrawable(R.drawable.ic_launcher_application);
770 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700771
Winson Chunge3193b92010-09-10 11:44:42 -0700772 final int iconSize = minDim / 2;
773 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800774 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700775 } catch (Resources.NotFoundException e) {
776 // if we can't find the icon, then just don't draw it
777 }
778
Winson Chung29d6fea2010-12-01 15:47:31 -0800779 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700780 } else {
781 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800782 final float imageWidth = drawable.getIntrinsicWidth();
783 final float imageHeight = drawable.getIntrinsicHeight();
784 final float aspect = (float) imageWidth / imageHeight;
785 final int scaledWidth =
786 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
787 final int scaledHeight =
788 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700789 int width;
790 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800791 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700792 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800793 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700794 } else {
795 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800796 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700797 }
798
799 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
800 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
801
Winson Chung29d6fea2010-12-01 15:47:31 -0800802 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700803 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800804 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
805 newDrawable.getIntrinsicHeight());
806 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700807 }
808
809 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700810 layout.setCellCount(mCellCountX, mCellCountY);
811 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
812 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700813 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700814 }
815
Winson Chunge3193b92010-09-10 11:44:42 -0700816 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700817 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700818 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
819
820 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
821 }
822
Winson Chung80baf5a2010-08-09 16:03:15 -0700823 private void syncWidgetPages() {
824 if (mWidgetList == null) return;
825
Winson Chunge3193b92010-09-10 11:44:42 -0700826 // we need to repopulate with the LinearLayout layout for the widget pages
827 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700828 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700829 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800830 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700831 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700832 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
833 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700834
Winson Chunge22a8e92010-11-12 13:40:58 -0800835 addView(layout, new LinearLayout.LayoutParams(
836 LinearLayout.LayoutParams.WRAP_CONTENT,
837 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700838 }
839 }
840
841 private void syncWidgetPageItems(int page) {
842 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700843 LinearLayout layout = (LinearLayout) getChildAt(page);
844 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700845 final int count = list.size();
Winson Chung04998342011-01-05 13:54:43 -0800846 final int numPages = getPageCount();
Winson Chung80baf5a2010-08-09 16:03:15 -0700847 layout.removeAllViews();
848 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700849 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
850 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800851 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
852 info.minHeight, null);
853 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700854
Winson Chung29d6fea2010-12-01 15:47:31 -0800855 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700856 R.layout.customize_paged_view_widget, layout, false);
Winson Chung04998342011-01-05 13:54:43 -0800857 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans,
858 mPageViewIconCache, (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -0700859 l.setTag(createItemInfo);
860 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800861 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700862 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700863
Winson Chunge3193b92010-09-10 11:44:42 -0700864 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700865 }
866 }
867
Winson Chung45e1d6e2010-11-09 17:19:49 -0800868 private void syncWallpaperPages() {
869 if (mWallpaperList == null) return;
870
871 // We need to repopulate the LinearLayout for the wallpaper pages
872 removeAllViews();
873 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800874 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800875 for (int i = 0; i < numPages; ++i) {
876 LinearLayout layout = new PagedViewExtendedLayout(getContext());
877 layout.setGravity(Gravity.CENTER_HORIZONTAL);
878 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
879 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
880
Winson Chunge22a8e92010-11-12 13:40:58 -0800881 addView(layout, new LinearLayout.LayoutParams(
882 LinearLayout.LayoutParams.WRAP_CONTENT,
883 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800884 }
885 }
886
887 private void syncWallpaperPageItems(int page) {
888 // Load the items on to the pages
889 LinearLayout layout = (LinearLayout) getChildAt(page);
890 layout.removeAllViews();
891 final int count = mWallpaperList.size();
Winson Chung04998342011-01-05 13:54:43 -0800892 final int numPages = getPageCount();
Winson Chung78bd53c2010-12-09 13:50:24 -0800893 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -0800894 final int startIndex = page * numItemsPerPage;
895 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
896 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800897 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800898 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800899
Winson Chung29d6fea2010-12-01 15:47:31 -0800900 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800901 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung04998342011-01-05 13:54:43 -0800902 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth,
903 mPageViewIconCache, (numPages > 1));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800904 l.setTag(info);
905 l.setOnClickListener(this);
906
Winson Chung45e1d6e2010-11-09 17:19:49 -0800907 layout.addView(l);
908 }
909 }
910
Winson Chung80baf5a2010-08-09 16:03:15 -0700911 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700912 // we need to repopulate with PagedViewCellLayouts
913 removeAllViews();
914
Winson Chung80baf5a2010-08-09 16:03:15 -0700915 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700916 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700917 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700918 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
919 setupPage(layout);
920 addView(layout);
921 }
922 }
923
924 private void syncListPageItems(int page, List<ResolveInfo> list) {
925 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -0800926 final int numPages = getPageCount();
927 final int numCells = mCellCountX * mCellCountY;
928 final int startIndex = page * numCells;
929 final int endIndex = Math.min(startIndex + numCells, list.size());
930 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700931 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -0800932 layout.removeAllViewsOnPage();
Winson Chung80baf5a2010-08-09 16:03:15 -0700933 for (int i = startIndex; i < endIndex; ++i) {
934 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700935 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
936
Winson Chung241c3b42010-08-25 16:53:03 -0700937 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
938 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700939 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Winson Chung04998342011-01-05 13:54:43 -0800940 ((LauncherApplication) mLauncher.getApplication()).getIconCache(),
941 (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -0700942 switch (mCustomizationType) {
943 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700944 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700945 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700946 case ShortcutCustomization:
947 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
948 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
949 info.activityInfo.name);
950 icon.setTag(createItemInfo);
951 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800952 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700953 icon.setOnLongClickListener(this);
954 break;
955 default:
956 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700957 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700958
959 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700960 final int x = index % mCellCountX;
961 final int y = index / mCellCountX;
962 setupPage(layout);
963 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
964 }
965 }
966
967 private void syncAppPages() {
968 if (mApps == null) return;
969
970 // We need to repopulate with PagedViewCellLayouts
971 removeAllViews();
972
973 // Ensure that we have the right number of pages
974 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
975 for (int i = 0; i < numPages; ++i) {
976 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
977 setupPage(layout);
978 addView(layout);
979 }
980 }
981
982 private void syncAppPageItems(int page) {
983 if (mApps == null) return;
984
985 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -0800986 final int numPages = getPageCount();
987 final int numCells = mCellCountX * mCellCountY;
988 final int startIndex = page * numCells;
989 final int endIndex = Math.min(startIndex + numCells, mApps.size());
990 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700991 // TODO: we can optimize by just re-applying to existing views
Michael Jurka8245a862011-02-01 17:53:59 -0800992 layout.removeAllViewsOnPage();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700993 for (int i = startIndex; i < endIndex; ++i) {
994 final ApplicationInfo info = mApps.get(i);
995 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
996 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung04998342011-01-05 13:54:43 -0800997 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -0700998 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800999 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001000 icon.setOnLongClickListener(this);
1001
1002 final int index = i - startIndex;
1003 final int x = index % mCellCountX;
1004 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001005 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -07001006 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001007 }
1008 }
1009
Winson Chung80baf5a2010-08-09 16:03:15 -07001010 @Override
1011 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -07001012 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001013 switch (mCustomizationType) {
1014 case WidgetCustomization:
1015 syncWidgetPages();
1016 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001017 case ShortcutCustomization:
1018 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001019 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001020 break;
1021 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001022 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -07001023 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001024 case ApplicationCustomization:
1025 syncAppPages();
1026 centerPagedViewCellLayouts = false;
1027 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001028 default:
1029 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001030 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001031 break;
1032 }
1033
1034 // only try and center the page if there is one page
1035 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001036 if (centerPagedViewCellLayouts) {
1037 if (childCount == 1) {
1038 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1039 layout.enableCenteredContent(true);
1040 } else {
1041 for (int i = 0; i < childCount; ++i) {
1042 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1043 layout.enableCenteredContent(false);
1044 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001045 }
1046 }
1047
1048 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001049 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001050 }
1051
1052 @Override
1053 public void syncPageItems(int page) {
1054 switch (mCustomizationType) {
1055 case WidgetCustomization:
1056 syncWidgetPageItems(page);
1057 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001058 case ShortcutCustomization:
1059 syncListPageItems(page, mShortcutList);
1060 break;
1061 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001062 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001063 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001064 case ApplicationCustomization:
1065 syncAppPageItems(page);
1066 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001067 }
1068 }
Winson Chunge3193b92010-09-10 11:44:42 -07001069
Winson Chungd0d43012010-09-26 17:26:45 -07001070 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001071 protected int getAssociatedLowerPageBound(int page) {
1072 return 0;
1073 }
Winson Chungd0d43012010-09-26 17:26:45 -07001074 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001075 protected int getAssociatedUpperPageBound(int page) {
1076 return getChildCount();
1077 }
Winson Chungd0d43012010-09-26 17:26:45 -07001078
1079 @Override
1080 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001081 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001082 return true;
1083 }
1084
1085 @Override
1086 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1087 return true;
1088 }
1089
1090 @Override
1091 public void onDestroyActionMode(ActionMode mode) {
1092 endChoiceMode();
1093 }
1094
1095 @Override
1096 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1097 return false;
1098 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001099}