blob: d2edd38608fdeea13b46123424026a2a2c113ef5 [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 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 Chung7d1fcbc2011-01-04 10:22:20 -0800315 cancelDragging();
Winson Chunga12a2502010-12-20 14:41:35 -0800316 if (getChildCount() > 0) {
317 setCurrentPage(0);
318 updateCurrentPageScroll();
319 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700320
Winson Chunga12a2502010-12-20 14:41:35 -0800321 // End the current choice mode so that we don't carry selections across tabs
322 endChoiceMode();
323 }
324 }
325
326 public CustomizationType getCustomizationFilter() {
327 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700328 }
329
330 @Override
331 public void onDropCompleted(View target, boolean success) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800332 resetCheckedGrandchildren();
Michael Jurka3e7c7632010-10-02 16:01:03 -0700333 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700334 }
335
336 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800337 public void onDragViewVisible() {
338 }
339
Adam Cohen120980b2010-12-08 11:05:37 -0800340 class ScaleAlphaInterpolator implements Interpolator {
341 public float getInterpolation(float input) {
342 float pivot = 0.5f;
343 if (input < pivot) {
344 return 0;
345 } else {
346 return (input - pivot)/(1 - pivot);
347 }
348 }
349 }
350
351 private void animateItemOntoScreen(View dragView,
352 final CellLayout layout, final ItemInfo info) {
353 mTmpFloatPos[0] = layout.getWidth() / 2;
354 mTmpFloatPos[1] = layout.getHeight() / 2;
355 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
356
357 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
358 final View dragCopy = dragLayer.createDragView(dragView);
359 dragCopy.setAlpha(1.0f);
360
361 int dragViewWidth = dragView.getMeasuredWidth();
362 int dragViewHeight = dragView.getMeasuredHeight();
363 float heightOffset = 0;
364 float widthOffset = 0;
365
366 if (dragView instanceof ImageView) {
367 Drawable d = ((ImageView) dragView).getDrawable();
368 int width = d.getIntrinsicWidth();
369 int height = d.getIntrinsicHeight();
370
371 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
372 float f = (dragViewWidth / (width * 1.0f));
373 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
374 } else {
375 float f = (dragViewHeight / (height * 1.0f));
376 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
377 }
378 }
379
380 float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
381 float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
382
383 ObjectAnimator posAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
384 PropertyValuesHolder.ofFloat("x", toX),
385 PropertyValuesHolder.ofFloat("y", toY));
386 posAnim.setInterpolator(mQuintEaseOutInterpolator);
387 posAnim.setDuration(ANIMATION_DURATION);
388
Patrick Dubroy047379a2010-12-19 22:02:04 -0800389 posAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohen120980b2010-12-08 11:05:37 -0800390 public void onAnimationEnd(Animator animation) {
391 dragLayer.removeView(dragCopy);
392 mLauncher.addExternalItemToScreen(info, layout);
393 post(new Runnable() {
394 public void run() {
395 layout.animateDrop();
396 }
397 });
398 }
399 });
400
401 ObjectAnimator scaleAlphaAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
402 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
403 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
404 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
405 scaleAlphaAnim.setInterpolator(mScaleAlphaInterpolator);
406 scaleAlphaAnim.setDuration(ANIMATION_DURATION);
407
408 posAnim.start();
409 scaleAlphaAnim.start();
410 }
411
Patrick Dubroya669d792010-11-23 14:40:33 -0800412 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800413 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800414 // Return early if this is not initiated from a touch
415 if (!v.isInTouchMode()) return;
416 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800417 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700418
Winson Chungd0d43012010-09-26 17:26:45 -0700419 // On certain pages, we allow single tap to mark items as selected so that they can be
420 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700421 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700422 switch (mCustomizationType) {
423 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700424 mChoiceModeTitleText = R.string.cab_widget_selection_text;
425 enterChoiceMode = true;
426 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700427 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700428 mChoiceModeTitleText = R.string.cab_app_selection_text;
429 enterChoiceMode = true;
430 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700431 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700432 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
433 enterChoiceMode = true;
434 break;
435 default:
436 break;
437 }
Winson Chungd0d43012010-09-26 17:26:45 -0700438
Michael Jurka3125d9d2010-09-27 11:30:20 -0700439 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700440 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700441
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700442 Workspace w = mLauncher.getWorkspace();
443 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
444 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800445 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700446
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700447 animateClickFeedback(v, new Runnable() {
448 @Override
449 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800450 cl.calculateSpans(itemInfo);
451 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
452 animateItemOntoScreen(dragView, cl, itemInfo);
453 } else {
454 mLauncher.showOutOfSpaceMessage();
455 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700456 }
457 });
Winson Chungd0d43012010-09-26 17:26:45 -0700458 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700459 }
460
461 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700462 switch (mCustomizationType) {
463 case WallpaperCustomization:
464 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700465 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700466 animateClickFeedback(v, new Runnable() {
467 @Override
468 public void run() {
469 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700470 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700471 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
472 ComponentName name = new ComponentName(info.activityInfo.packageName,
473 info.activityInfo.name);
474 createWallpapersIntent.setComponent(name);
475 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700476 }
477 });
Winson Chungd0d43012010-09-26 17:26:45 -0700478 break;
479 default:
480 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700481 }
482 }
483
Winson Chungcd4bc492010-12-09 18:52:32 -0800484 Bitmap drawableToBitmap(Drawable d, View v) {
485 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800486 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800487 c.translate((v.getWidth() - d.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkaaf91de02010-11-23 16:23:58 -0800488 d.draw(c);
489 return b;
490 }
491
Adam Cohen120980b2010-12-08 11:05:37 -0800492 private View getDragView(View v) {
493 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
494 v.findViewById(R.id.widget_preview) : v;
495 }
496
Michael Jurka72b079e2010-12-10 01:03:53 -0800497 protected boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700498 // End the current choice mode before we start dragging anything
499 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
500 endChoiceMode();
501 }
Michael Jurka72b079e2010-12-10 01:03:53 -0800502 super.beginDragging(v);
Winson Chungd0d43012010-09-26 17:26:45 -0700503
Winson Chung59e1f9a2010-12-21 11:31:54 -0800504 boolean result = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700505 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800506 case WidgetCustomization: {
Adam Cohen120980b2010-12-08 11:05:37 -0800507 // Get the widget preview as the drag representation
Michael Jurkad3ef3062010-11-23 16:23:58 -0800508 final LinearLayout l = (LinearLayout) v;
Winson Chungcd4bc492010-12-09 18:52:32 -0800509 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
510 final Drawable icon = i.getDrawable();
511 Bitmap b = drawableToBitmap(icon, i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700512 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700513
Michael Jurkad3ef3062010-11-23 16:23:58 -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(
Winson Chungcd4bc492010-12-09 18:52:32 -0800520 i, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
521 b.recycle();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800522 result = true;
523 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800524 }
525 case ShortcutCustomization: {
526 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800527 final TextView tv = (TextView) v;
528 final Drawable icon = tv.getCompoundDrawables()[1];
529 Bitmap b = drawableToBitmap(icon, tv);
Adam Cohen120980b2010-12-08 11:05:37 -0800530 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chungcd4bc492010-12-09 18:52:32 -0800531
Michael Jurkad3ef3062010-11-23 16:23:58 -0800532 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800533 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
534 null);
535 b.recycle();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800536 result = true;
537 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800538 }
539 case ApplicationCustomization: {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700540 // Pick up the application for dropping
Michael Jurkad3ef3062010-11-23 16:23:58 -0800541 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800542 final TextView tv = (TextView) v;
543 final Drawable icon = tv.getCompoundDrawables()[1];
544 Bitmap b = drawableToBitmap(icon, tv);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700545 ApplicationInfo app = (ApplicationInfo) v.getTag();
546 app = new ApplicationInfo(app);
547
Michael Jurkad3ef3062010-11-23 16:23:58 -0800548 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800549 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
550 b.recycle();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800551 result = true;
552 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700553 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800554 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800555
556 // We toggle the checked state _after_ we create the view for the drag in case toggling the
557 // checked state changes the view's look
558 if (v instanceof Checkable) {
559 // In preparation for drag, we always reset the checked grand children regardless of
560 // what choice mode we are in
561 resetCheckedGrandchildren();
562
563 // Toggle the selection on the dragged app
564 Checkable checkable = (Checkable) v;
565
566 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
567 // of the drag of the item. (The fade-in will occur when all checked states are
568 // disabled when dragging ends)
569 checkable.toggle();
570 }
571
572 return result;
Winson Chung80baf5a2010-08-09 16:03:15 -0700573 }
574
Winson Chunge3193b92010-09-10 11:44:42 -0700575 /**
576 * Pre-processes the layout of the different widget pages.
577 * @return the number of pages of widgets that we have
578 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700579 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700580 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700581
Winson Chunge3193b92010-09-10 11:44:42 -0700582 // create a new page for the first set of widgets
583 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700584 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700585 mWidgetPages.add(newPage);
586
587 // do this until we have no more widgets to lay out
588 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
589 final int widgetCount = mWidgetList.size();
590 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700591 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700592 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700593
Winson Chunge3193b92010-09-10 11:44:42 -0700594 // determine the size of the current widget
595 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
596 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700597
Winson Chunge3193b92010-09-10 11:44:42 -0700598 // create a new page if necessary
599 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
600 numCellsInRow = 0;
601 newPage = new ArrayList<AppWidgetProviderInfo>();
602 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700603 }
604
Winson Chunge3193b92010-09-10 11:44:42 -0700605 // add the item to the current page
606 newPage.add(info);
607 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700608 }
Winson Chunge3193b92010-09-10 11:44:42 -0700609
Winson Chung80baf5a2010-08-09 16:03:15 -0700610 return mWidgetPages.size();
611 }
612
Winson Chunge3193b92010-09-10 11:44:42 -0700613 /**
Winson Chung7da10252010-10-28 16:07:04 -0700614 * Helper function to draw a drawable to the specified canvas with the specified bounds.
615 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800616 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700617 if (bitmap != null) mCanvas.setBitmap(bitmap);
618 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800619 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700620 d.draw(mCanvas);
621 mCanvas.restore();
622 }
623
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800624 /*
625 * This method fetches an xml file specified in the manifest identified by
626 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
627 * an image which will be used as the wallpaper preview for an activity
628 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
629 * in the customize drawer.
630 */
631 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800632 ActivityInfo activityInfo = ri.activityInfo;
633 XmlResourceParser parser = null;
634 try {
635 parser = activityInfo.loadXmlMetaData(mPackageManager,
636 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
637 if (parser == null) {
638 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
639 + "wallpaper provider '" + component + '\'');
640 return null;
641 }
642
643 AttributeSet attrs = Xml.asAttributeSet(parser);
644
645 int type;
646 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
647 && type != XmlPullParser.START_TAG) {
648 // drain whitespace, comments, etc.
649 }
650
651 String nodeName = parser.getName();
652 if (!"wallpaper-preview".equals(nodeName)) {
653 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
654 + "wallpaper provider '" + component + '\'');
655 return null;
656 }
657
658 // If metaData was null, we would have returned earlier when getting
659 // the parser No need to do the check here
660 Resources res = mPackageManager.getResourcesForApplication(
661 activityInfo.applicationInfo);
662
663 TypedArray sa = res.obtainAttributes(attrs,
664 com.android.internal.R.styleable.WallpaperPreviewInfo);
665
666 TypedValue value = sa.peekValue(
667 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
668 if (value == null) return null;
669
670 return res.getDrawable(value.resourceId);
671 } catch (Exception e) {
672 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
673 return null;
674 } finally {
675 if (parser != null) parser.close();
676 }
677 }
678
Winson Chung7da10252010-10-28 16:07:04 -0700679 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800680 * This method will extract the preview image specified by the wallpaper source provider (if it
681 * exists) otherwise, it will try to generate a default image preview.
682 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800683 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800684 // To be implemented later: resolving the up-to-date wallpaper thumbnail
685
686 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
687 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
688 Resources resources = mLauncher.getResources();
689
690 // Create a new bitmap to hold the widget preview
691 int width = (int) (dim * sScaleFactor);
692 int height = (int) (dim * sScaleFactor);
693 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800694
695 Drawable background = parseWallpaperPreviewXml(
696 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
697 boolean foundCustomDrawable = background != null;
698
699 if (!foundCustomDrawable) {
700 background = resources.getDrawable(R.drawable.default_widget_preview);
701 }
702
Winson Chung45e1d6e2010-11-09 17:19:49 -0800703 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
704
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800705 // If we don't have a custom icon, we use the app icon on the default background
706 if (!foundCustomDrawable) {
707 try {
708 final IconCache iconCache =
709 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
710 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
711 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800712
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800713 final int iconSize = minDim / 2;
714 final int offset = iconSize / 4;
715 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
716 } catch (Resources.NotFoundException e) {
717 // if we can't find the icon, then just don't draw it
718 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800719 }
720
Winson Chung29d6fea2010-12-01 15:47:31 -0800721 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800722 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
723 return drawable;
724 }
725
726 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700727 * This method will extract the preview image specified by the widget developer (if it exists),
728 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800729 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700730 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800731 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800732 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700733 String packageName = info.provider.getPackageName();
734 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800735 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700736 if (info.previewImage != 0) {
737 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
738 if (drawable == null) {
739 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
740 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700741 }
742 }
743
744 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700745 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
746 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700747 if (drawable == null) {
748 Resources resources = mLauncher.getResources();
749
Winson Chung80baf5a2010-08-09 16:03:15 -0700750 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700751 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
752 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700753 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
754 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
755 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700756
Winson Chung45e1d6e2010-11-09 17:19:49 -0800757 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700758 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700759 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700760 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700761 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700762 }
763 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700764 icon = resources.getDrawable(R.drawable.ic_launcher_application);
765 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700766
Winson Chunge3193b92010-09-10 11:44:42 -0700767 final int iconSize = minDim / 2;
768 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800769 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700770 } catch (Resources.NotFoundException e) {
771 // if we can't find the icon, then just don't draw it
772 }
773
Winson Chung29d6fea2010-12-01 15:47:31 -0800774 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700775 } else {
776 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800777 final float imageWidth = drawable.getIntrinsicWidth();
778 final float imageHeight = drawable.getIntrinsicHeight();
779 final float aspect = (float) imageWidth / imageHeight;
780 final int scaledWidth =
781 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
782 final int scaledHeight =
783 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700784 int width;
785 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800786 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700787 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800788 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700789 } else {
790 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800791 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700792 }
793
794 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
795 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
796
Winson Chung29d6fea2010-12-01 15:47:31 -0800797 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700798 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800799 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
800 newDrawable.getIntrinsicHeight());
801 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700802 }
803
804 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700805 layout.setCellCount(mCellCountX, mCellCountY);
806 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
807 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700808 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700809 }
810
Winson Chunge3193b92010-09-10 11:44:42 -0700811 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700812 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700813 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
814
815 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
816 }
817
Winson Chung80baf5a2010-08-09 16:03:15 -0700818 private void syncWidgetPages() {
819 if (mWidgetList == null) return;
820
Winson Chunge3193b92010-09-10 11:44:42 -0700821 // we need to repopulate with the LinearLayout layout for the widget pages
822 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700823 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700824 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800825 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700826 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700827 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
828 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700829
Winson Chunge22a8e92010-11-12 13:40:58 -0800830 addView(layout, new LinearLayout.LayoutParams(
831 LinearLayout.LayoutParams.WRAP_CONTENT,
832 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700833 }
834 }
835
836 private void syncWidgetPageItems(int page) {
837 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700838 LinearLayout layout = (LinearLayout) getChildAt(page);
839 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700840 final int count = list.size();
841 layout.removeAllViews();
842 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700843 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
844 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800845 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
846 info.minHeight, null);
847 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700848
Winson Chung29d6fea2010-12-01 15:47:31 -0800849 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700850 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800851 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700852 l.setTag(createItemInfo);
853 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800854 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700855 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700856
Winson Chunge3193b92010-09-10 11:44:42 -0700857 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700858 }
859 }
860
Winson Chung45e1d6e2010-11-09 17:19:49 -0800861 private void syncWallpaperPages() {
862 if (mWallpaperList == null) return;
863
864 // We need to repopulate the LinearLayout for the wallpaper pages
865 removeAllViews();
866 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800867 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800868 for (int i = 0; i < numPages; ++i) {
869 LinearLayout layout = new PagedViewExtendedLayout(getContext());
870 layout.setGravity(Gravity.CENTER_HORIZONTAL);
871 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
872 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
873
Winson Chunge22a8e92010-11-12 13:40:58 -0800874 addView(layout, new LinearLayout.LayoutParams(
875 LinearLayout.LayoutParams.WRAP_CONTENT,
876 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800877 }
878 }
879
880 private void syncWallpaperPageItems(int page) {
881 // Load the items on to the pages
882 LinearLayout layout = (LinearLayout) getChildAt(page);
883 layout.removeAllViews();
884 final int count = mWallpaperList.size();
Winson Chung78bd53c2010-12-09 13:50:24 -0800885 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -0800886 final int startIndex = page * numItemsPerPage;
887 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
888 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800889 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800890 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800891
Winson Chung29d6fea2010-12-01 15:47:31 -0800892 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800893 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800894 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800895 l.setTag(info);
896 l.setOnClickListener(this);
897
Winson Chung45e1d6e2010-11-09 17:19:49 -0800898 layout.addView(l);
899 }
900 }
901
Winson Chung80baf5a2010-08-09 16:03:15 -0700902 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700903 // we need to repopulate with PagedViewCellLayouts
904 removeAllViews();
905
Winson Chung80baf5a2010-08-09 16:03:15 -0700906 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700907 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700908 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700909 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
910 setupPage(layout);
911 addView(layout);
912 }
913 }
914
915 private void syncListPageItems(int page, List<ResolveInfo> list) {
916 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700917 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700918 int startIndex = page * numCells;
919 int endIndex = Math.min(startIndex + numCells, list.size());
920 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
921 // TODO: we can optimize by just re-applying to existing views
922 layout.removeAllViews();
923 for (int i = startIndex; i < endIndex; ++i) {
924 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700925 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
926
Winson Chung241c3b42010-08-25 16:53:03 -0700927 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
928 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700929 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Adam Cohen120980b2010-12-08 11:05:37 -0800930 ((LauncherApplication) mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700931 switch (mCustomizationType) {
932 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700933 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700934 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700935 case ShortcutCustomization:
936 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
937 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
938 info.activityInfo.name);
939 icon.setTag(createItemInfo);
940 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800941 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700942 icon.setOnLongClickListener(this);
943 break;
944 default:
945 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700946 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700947
948 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700949 final int x = index % mCellCountX;
950 final int y = index / mCellCountX;
951 setupPage(layout);
952 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
953 }
954 }
955
956 private void syncAppPages() {
957 if (mApps == null) return;
958
959 // We need to repopulate with PagedViewCellLayouts
960 removeAllViews();
961
962 // Ensure that we have the right number of pages
963 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
964 for (int i = 0; i < numPages; ++i) {
965 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
966 setupPage(layout);
967 addView(layout);
968 }
969 }
970
971 private void syncAppPageItems(int page) {
972 if (mApps == null) return;
973
974 // ensure that we have the right number of items on the pages
975 int numCells = mCellCountX * mCellCountY;
976 int startIndex = page * numCells;
977 int endIndex = Math.min(startIndex + numCells, mApps.size());
978 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
979 // TODO: we can optimize by just re-applying to existing views
980 layout.removeAllViews();
981 for (int i = startIndex; i < endIndex; ++i) {
982 final ApplicationInfo info = mApps.get(i);
983 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
984 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700985 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700986 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800987 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700988 icon.setOnLongClickListener(this);
989
990 final int index = i - startIndex;
991 final int x = index % mCellCountX;
992 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700993 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700994 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700995 }
996 }
997
Winson Chung80baf5a2010-08-09 16:03:15 -0700998 @Override
999 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -07001000 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001001 switch (mCustomizationType) {
1002 case WidgetCustomization:
1003 syncWidgetPages();
1004 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001005 case ShortcutCustomization:
1006 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001007 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001008 break;
1009 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001010 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -07001011 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001012 case ApplicationCustomization:
1013 syncAppPages();
1014 centerPagedViewCellLayouts = false;
1015 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001016 default:
1017 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001018 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001019 break;
1020 }
1021
1022 // only try and center the page if there is one page
1023 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001024 if (centerPagedViewCellLayouts) {
1025 if (childCount == 1) {
1026 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1027 layout.enableCenteredContent(true);
1028 } else {
1029 for (int i = 0; i < childCount; ++i) {
1030 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1031 layout.enableCenteredContent(false);
1032 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001033 }
1034 }
1035
1036 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001037 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001038 }
1039
1040 @Override
1041 public void syncPageItems(int page) {
1042 switch (mCustomizationType) {
1043 case WidgetCustomization:
1044 syncWidgetPageItems(page);
1045 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001046 case ShortcutCustomization:
1047 syncListPageItems(page, mShortcutList);
1048 break;
1049 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001050 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001051 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001052 case ApplicationCustomization:
1053 syncAppPageItems(page);
1054 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001055 }
1056 }
Winson Chunge3193b92010-09-10 11:44:42 -07001057
Winson Chungd0d43012010-09-26 17:26:45 -07001058 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001059 protected int getAssociatedLowerPageBound(int page) {
1060 return 0;
1061 }
Winson Chungd0d43012010-09-26 17:26:45 -07001062 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001063 protected int getAssociatedUpperPageBound(int page) {
1064 return getChildCount();
1065 }
Winson Chungd0d43012010-09-26 17:26:45 -07001066
1067 @Override
1068 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001069 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001070 return true;
1071 }
1072
1073 @Override
1074 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1075 return true;
1076 }
1077
1078 @Override
1079 public void onDestroyActionMode(ActionMode mode) {
1080 endChoiceMode();
1081 }
1082
1083 @Override
1084 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1085 return false;
1086 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001087}