blob: ec9d52e4fcb6c63a08b262ded0a4df88e313aa17 [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 Chunga12a2502010-12-20 14:41:35 -0800315 if (getChildCount() > 0) {
316 setCurrentPage(0);
317 updateCurrentPageScroll();
318 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700319
Winson Chunga12a2502010-12-20 14:41:35 -0800320 // End the current choice mode so that we don't carry selections across tabs
321 endChoiceMode();
322 }
323 }
324
325 public CustomizationType getCustomizationFilter() {
326 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700327 }
328
329 @Override
330 public void onDropCompleted(View target, boolean success) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800331 resetCheckedGrandchildren();
Michael Jurka3e7c7632010-10-02 16:01:03 -0700332 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700333 }
334
335 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800336 public void onDragViewVisible() {
337 }
338
Adam Cohen120980b2010-12-08 11:05:37 -0800339 class ScaleAlphaInterpolator implements Interpolator {
340 public float getInterpolation(float input) {
341 float pivot = 0.5f;
342 if (input < pivot) {
343 return 0;
344 } else {
345 return (input - pivot)/(1 - pivot);
346 }
347 }
348 }
349
350 private void animateItemOntoScreen(View dragView,
351 final CellLayout layout, final ItemInfo info) {
352 mTmpFloatPos[0] = layout.getWidth() / 2;
353 mTmpFloatPos[1] = layout.getHeight() / 2;
354 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
355
356 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
357 final View dragCopy = dragLayer.createDragView(dragView);
358 dragCopy.setAlpha(1.0f);
359
360 int dragViewWidth = dragView.getMeasuredWidth();
361 int dragViewHeight = dragView.getMeasuredHeight();
362 float heightOffset = 0;
363 float widthOffset = 0;
364
365 if (dragView instanceof ImageView) {
366 Drawable d = ((ImageView) dragView).getDrawable();
367 int width = d.getIntrinsicWidth();
368 int height = d.getIntrinsicHeight();
369
370 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
371 float f = (dragViewWidth / (width * 1.0f));
372 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
373 } else {
374 float f = (dragViewHeight / (height * 1.0f));
375 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
376 }
377 }
378
379 float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
380 float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
381
382 ObjectAnimator posAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
383 PropertyValuesHolder.ofFloat("x", toX),
384 PropertyValuesHolder.ofFloat("y", toY));
385 posAnim.setInterpolator(mQuintEaseOutInterpolator);
386 posAnim.setDuration(ANIMATION_DURATION);
387
Patrick Dubroy047379a2010-12-19 22:02:04 -0800388 posAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohen120980b2010-12-08 11:05:37 -0800389 public void onAnimationEnd(Animator animation) {
390 dragLayer.removeView(dragCopy);
391 mLauncher.addExternalItemToScreen(info, layout);
392 post(new Runnable() {
393 public void run() {
394 layout.animateDrop();
395 }
396 });
397 }
398 });
399
400 ObjectAnimator scaleAlphaAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
401 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
402 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
403 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
404 scaleAlphaAnim.setInterpolator(mScaleAlphaInterpolator);
405 scaleAlphaAnim.setDuration(ANIMATION_DURATION);
406
407 posAnim.start();
408 scaleAlphaAnim.start();
409 }
410
Patrick Dubroya669d792010-11-23 14:40:33 -0800411 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800412 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800413 // Return early if this is not initiated from a touch
414 if (!v.isInTouchMode()) return;
415 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800416 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700417
Winson Chungd0d43012010-09-26 17:26:45 -0700418 // On certain pages, we allow single tap to mark items as selected so that they can be
419 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700420 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700421 switch (mCustomizationType) {
422 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700423 mChoiceModeTitleText = R.string.cab_widget_selection_text;
424 enterChoiceMode = true;
425 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700426 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700427 mChoiceModeTitleText = R.string.cab_app_selection_text;
428 enterChoiceMode = true;
429 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700430 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700431 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
432 enterChoiceMode = true;
433 break;
434 default:
435 break;
436 }
Winson Chungd0d43012010-09-26 17:26:45 -0700437
Michael Jurka3125d9d2010-09-27 11:30:20 -0700438 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700439 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700440
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700441 Workspace w = mLauncher.getWorkspace();
442 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
443 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800444 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700445
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700446 animateClickFeedback(v, new Runnable() {
447 @Override
448 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800449 cl.calculateSpans(itemInfo);
450 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
451 animateItemOntoScreen(dragView, cl, itemInfo);
452 } else {
453 mLauncher.showOutOfSpaceMessage();
454 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700455 }
456 });
Winson Chungd0d43012010-09-26 17:26:45 -0700457 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700458 }
459
460 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700461 switch (mCustomizationType) {
462 case WallpaperCustomization:
463 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700464 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700465 animateClickFeedback(v, new Runnable() {
466 @Override
467 public void run() {
468 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700469 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700470 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
471 ComponentName name = new ComponentName(info.activityInfo.packageName,
472 info.activityInfo.name);
473 createWallpapersIntent.setComponent(name);
474 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700475 }
476 });
Winson Chungd0d43012010-09-26 17:26:45 -0700477 break;
478 default:
479 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700480 }
481 }
482
Winson Chungcd4bc492010-12-09 18:52:32 -0800483 Bitmap drawableToBitmap(Drawable d, View v) {
484 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800485 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800486 c.translate((v.getWidth() - d.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkaaf91de02010-11-23 16:23:58 -0800487 d.draw(c);
488 return b;
489 }
490
Adam Cohen120980b2010-12-08 11:05:37 -0800491 private View getDragView(View v) {
492 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
493 v.findViewById(R.id.widget_preview) : v;
494 }
495
Michael Jurka72b079e2010-12-10 01:03:53 -0800496 protected boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700497 // End the current choice mode before we start dragging anything
498 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
499 endChoiceMode();
500 }
Michael Jurka72b079e2010-12-10 01:03:53 -0800501 super.beginDragging(v);
Winson Chungd0d43012010-09-26 17:26:45 -0700502
Winson Chung59e1f9a2010-12-21 11:31:54 -0800503 boolean result = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700504 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800505 case WidgetCustomization: {
Adam Cohen120980b2010-12-08 11:05:37 -0800506 // Get the widget preview as the drag representation
Michael Jurkad3ef3062010-11-23 16:23:58 -0800507 final LinearLayout l = (LinearLayout) v;
Winson Chungcd4bc492010-12-09 18:52:32 -0800508 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
509 final Drawable icon = i.getDrawable();
510 Bitmap b = drawableToBitmap(icon, i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700511 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700512
Michael Jurkad3ef3062010-11-23 16:23:58 -0800513 int[] spanXY = CellLayout.rectToCell(
514 getResources(), createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
515 createWidgetInfo.spanX = spanXY[0];
516 createWidgetInfo.spanY = spanXY[1];
517 mLauncher.getWorkspace().onDragStartedWithItemSpans(spanXY[0], spanXY[1], b);
518 mDragController.startDrag(
Winson Chungcd4bc492010-12-09 18:52:32 -0800519 i, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
520 b.recycle();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800521 result = true;
522 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800523 }
524 case ShortcutCustomization: {
525 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800526 final TextView tv = (TextView) v;
527 final Drawable icon = tv.getCompoundDrawables()[1];
528 Bitmap b = drawableToBitmap(icon, tv);
Adam Cohen120980b2010-12-08 11:05:37 -0800529 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chungcd4bc492010-12-09 18:52:32 -0800530
Michael Jurkad3ef3062010-11-23 16:23:58 -0800531 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800532 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
533 null);
534 b.recycle();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800535 result = true;
536 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800537 }
538 case ApplicationCustomization: {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700539 // Pick up the application for dropping
Michael Jurkad3ef3062010-11-23 16:23:58 -0800540 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800541 final TextView tv = (TextView) v;
542 final Drawable icon = tv.getCompoundDrawables()[1];
543 Bitmap b = drawableToBitmap(icon, tv);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700544 ApplicationInfo app = (ApplicationInfo) v.getTag();
545 app = new ApplicationInfo(app);
546
Michael Jurkad3ef3062010-11-23 16:23:58 -0800547 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800548 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
549 b.recycle();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800550 result = true;
551 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700552 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800553 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800554
555 // We toggle the checked state _after_ we create the view for the drag in case toggling the
556 // checked state changes the view's look
557 if (v instanceof Checkable) {
558 // In preparation for drag, we always reset the checked grand children regardless of
559 // what choice mode we are in
560 resetCheckedGrandchildren();
561
562 // Toggle the selection on the dragged app
563 Checkable checkable = (Checkable) v;
564
565 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
566 // of the drag of the item. (The fade-in will occur when all checked states are
567 // disabled when dragging ends)
568 checkable.toggle();
569 }
570
571 return result;
Winson Chung80baf5a2010-08-09 16:03:15 -0700572 }
573
Winson Chunge3193b92010-09-10 11:44:42 -0700574 /**
575 * Pre-processes the layout of the different widget pages.
576 * @return the number of pages of widgets that we have
577 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700578 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700579 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700580
Winson Chunge3193b92010-09-10 11:44:42 -0700581 // create a new page for the first set of widgets
582 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700583 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700584 mWidgetPages.add(newPage);
585
586 // do this until we have no more widgets to lay out
587 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
588 final int widgetCount = mWidgetList.size();
589 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700590 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700591 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700592
Winson Chunge3193b92010-09-10 11:44:42 -0700593 // determine the size of the current widget
594 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
595 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700596
Winson Chunge3193b92010-09-10 11:44:42 -0700597 // create a new page if necessary
598 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
599 numCellsInRow = 0;
600 newPage = new ArrayList<AppWidgetProviderInfo>();
601 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700602 }
603
Winson Chunge3193b92010-09-10 11:44:42 -0700604 // add the item to the current page
605 newPage.add(info);
606 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700607 }
Winson Chunge3193b92010-09-10 11:44:42 -0700608
Winson Chung80baf5a2010-08-09 16:03:15 -0700609 return mWidgetPages.size();
610 }
611
Winson Chunge3193b92010-09-10 11:44:42 -0700612 /**
Winson Chung7da10252010-10-28 16:07:04 -0700613 * Helper function to draw a drawable to the specified canvas with the specified bounds.
614 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800615 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700616 if (bitmap != null) mCanvas.setBitmap(bitmap);
617 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800618 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700619 d.draw(mCanvas);
620 mCanvas.restore();
621 }
622
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800623 /*
624 * This method fetches an xml file specified in the manifest identified by
625 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
626 * an image which will be used as the wallpaper preview for an activity
627 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
628 * in the customize drawer.
629 */
630 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800631 ActivityInfo activityInfo = ri.activityInfo;
632 XmlResourceParser parser = null;
633 try {
634 parser = activityInfo.loadXmlMetaData(mPackageManager,
635 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
636 if (parser == null) {
637 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
638 + "wallpaper provider '" + component + '\'');
639 return null;
640 }
641
642 AttributeSet attrs = Xml.asAttributeSet(parser);
643
644 int type;
645 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
646 && type != XmlPullParser.START_TAG) {
647 // drain whitespace, comments, etc.
648 }
649
650 String nodeName = parser.getName();
651 if (!"wallpaper-preview".equals(nodeName)) {
652 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
653 + "wallpaper provider '" + component + '\'');
654 return null;
655 }
656
657 // If metaData was null, we would have returned earlier when getting
658 // the parser No need to do the check here
659 Resources res = mPackageManager.getResourcesForApplication(
660 activityInfo.applicationInfo);
661
662 TypedArray sa = res.obtainAttributes(attrs,
663 com.android.internal.R.styleable.WallpaperPreviewInfo);
664
665 TypedValue value = sa.peekValue(
666 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
667 if (value == null) return null;
668
669 return res.getDrawable(value.resourceId);
670 } catch (Exception e) {
671 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
672 return null;
673 } finally {
674 if (parser != null) parser.close();
675 }
676 }
677
Winson Chung7da10252010-10-28 16:07:04 -0700678 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800679 * This method will extract the preview image specified by the wallpaper source provider (if it
680 * exists) otherwise, it will try to generate a default image preview.
681 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800682 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800683 // To be implemented later: resolving the up-to-date wallpaper thumbnail
684
685 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
686 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
687 Resources resources = mLauncher.getResources();
688
689 // Create a new bitmap to hold the widget preview
690 int width = (int) (dim * sScaleFactor);
691 int height = (int) (dim * sScaleFactor);
692 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800693
694 Drawable background = parseWallpaperPreviewXml(
695 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
696 boolean foundCustomDrawable = background != null;
697
698 if (!foundCustomDrawable) {
699 background = resources.getDrawable(R.drawable.default_widget_preview);
700 }
701
Winson Chung45e1d6e2010-11-09 17:19:49 -0800702 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
703
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800704 // If we don't have a custom icon, we use the app icon on the default background
705 if (!foundCustomDrawable) {
706 try {
707 final IconCache iconCache =
708 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
709 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
710 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800711
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800712 final int iconSize = minDim / 2;
713 final int offset = iconSize / 4;
714 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
715 } catch (Resources.NotFoundException e) {
716 // if we can't find the icon, then just don't draw it
717 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800718 }
719
Winson Chung29d6fea2010-12-01 15:47:31 -0800720 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800721 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
722 return drawable;
723 }
724
725 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700726 * This method will extract the preview image specified by the widget developer (if it exists),
727 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800728 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700729 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800730 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800731 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700732 String packageName = info.provider.getPackageName();
733 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800734 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700735 if (info.previewImage != 0) {
736 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
737 if (drawable == null) {
738 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
739 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700740 }
741 }
742
743 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700744 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
745 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700746 if (drawable == null) {
747 Resources resources = mLauncher.getResources();
748
Winson Chung80baf5a2010-08-09 16:03:15 -0700749 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700750 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
751 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700752 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
753 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
754 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700755
Winson Chung45e1d6e2010-11-09 17:19:49 -0800756 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700757 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700758 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700759 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700760 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700761 }
762 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700763 icon = resources.getDrawable(R.drawable.ic_launcher_application);
764 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700765
Winson Chunge3193b92010-09-10 11:44:42 -0700766 final int iconSize = minDim / 2;
767 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800768 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700769 } catch (Resources.NotFoundException e) {
770 // if we can't find the icon, then just don't draw it
771 }
772
Winson Chung29d6fea2010-12-01 15:47:31 -0800773 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700774 } else {
775 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800776 final float imageWidth = drawable.getIntrinsicWidth();
777 final float imageHeight = drawable.getIntrinsicHeight();
778 final float aspect = (float) imageWidth / imageHeight;
779 final int scaledWidth =
780 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
781 final int scaledHeight =
782 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700783 int width;
784 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800785 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700786 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800787 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700788 } else {
789 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800790 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700791 }
792
793 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
794 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
795
Winson Chung29d6fea2010-12-01 15:47:31 -0800796 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700797 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800798 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
799 newDrawable.getIntrinsicHeight());
800 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700801 }
802
803 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700804 layout.setCellCount(mCellCountX, mCellCountY);
805 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
806 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700807 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700808 }
809
Winson Chunge3193b92010-09-10 11:44:42 -0700810 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700811 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700812 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
813
814 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
815 }
816
Winson Chung80baf5a2010-08-09 16:03:15 -0700817 private void syncWidgetPages() {
818 if (mWidgetList == null) return;
819
Winson Chunge3193b92010-09-10 11:44:42 -0700820 // we need to repopulate with the LinearLayout layout for the widget pages
821 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700822 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700823 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800824 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700825 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700826 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
827 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700828
Winson Chunge22a8e92010-11-12 13:40:58 -0800829 addView(layout, new LinearLayout.LayoutParams(
830 LinearLayout.LayoutParams.WRAP_CONTENT,
831 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700832 }
833 }
834
835 private void syncWidgetPageItems(int page) {
836 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700837 LinearLayout layout = (LinearLayout) getChildAt(page);
838 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700839 final int count = list.size();
840 layout.removeAllViews();
841 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700842 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
843 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800844 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
845 info.minHeight, null);
846 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700847
Winson Chung29d6fea2010-12-01 15:47:31 -0800848 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700849 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800850 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700851 l.setTag(createItemInfo);
852 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800853 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700854 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700855
Winson Chunge3193b92010-09-10 11:44:42 -0700856 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700857 }
858 }
859
Winson Chung45e1d6e2010-11-09 17:19:49 -0800860 private void syncWallpaperPages() {
861 if (mWallpaperList == null) return;
862
863 // We need to repopulate the LinearLayout for the wallpaper pages
864 removeAllViews();
865 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800866 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800867 for (int i = 0; i < numPages; ++i) {
868 LinearLayout layout = new PagedViewExtendedLayout(getContext());
869 layout.setGravity(Gravity.CENTER_HORIZONTAL);
870 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
871 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
872
Winson Chunge22a8e92010-11-12 13:40:58 -0800873 addView(layout, new LinearLayout.LayoutParams(
874 LinearLayout.LayoutParams.WRAP_CONTENT,
875 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800876 }
877 }
878
879 private void syncWallpaperPageItems(int page) {
880 // Load the items on to the pages
881 LinearLayout layout = (LinearLayout) getChildAt(page);
882 layout.removeAllViews();
883 final int count = mWallpaperList.size();
Winson Chung78bd53c2010-12-09 13:50:24 -0800884 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -0800885 final int startIndex = page * numItemsPerPage;
886 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
887 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800888 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800889 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800890
Winson Chung29d6fea2010-12-01 15:47:31 -0800891 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800892 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800893 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800894 l.setTag(info);
895 l.setOnClickListener(this);
896
Winson Chung45e1d6e2010-11-09 17:19:49 -0800897 layout.addView(l);
898 }
899 }
900
Winson Chung80baf5a2010-08-09 16:03:15 -0700901 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700902 // we need to repopulate with PagedViewCellLayouts
903 removeAllViews();
904
Winson Chung80baf5a2010-08-09 16:03:15 -0700905 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700906 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700907 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700908 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
909 setupPage(layout);
910 addView(layout);
911 }
912 }
913
914 private void syncListPageItems(int page, List<ResolveInfo> list) {
915 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700916 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700917 int startIndex = page * numCells;
918 int endIndex = Math.min(startIndex + numCells, list.size());
919 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
920 // TODO: we can optimize by just re-applying to existing views
921 layout.removeAllViews();
922 for (int i = startIndex; i < endIndex; ++i) {
923 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700924 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
925
Winson Chung241c3b42010-08-25 16:53:03 -0700926 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
927 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700928 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Adam Cohen120980b2010-12-08 11:05:37 -0800929 ((LauncherApplication) mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700930 switch (mCustomizationType) {
931 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700932 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700933 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700934 case ShortcutCustomization:
935 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
936 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
937 info.activityInfo.name);
938 icon.setTag(createItemInfo);
939 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800940 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700941 icon.setOnLongClickListener(this);
942 break;
943 default:
944 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700945 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700946
947 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700948 final int x = index % mCellCountX;
949 final int y = index / mCellCountX;
950 setupPage(layout);
951 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
952 }
953 }
954
955 private void syncAppPages() {
956 if (mApps == null) return;
957
958 // We need to repopulate with PagedViewCellLayouts
959 removeAllViews();
960
961 // Ensure that we have the right number of pages
962 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
963 for (int i = 0; i < numPages; ++i) {
964 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
965 setupPage(layout);
966 addView(layout);
967 }
968 }
969
970 private void syncAppPageItems(int page) {
971 if (mApps == null) return;
972
973 // ensure that we have the right number of items on the pages
974 int numCells = mCellCountX * mCellCountY;
975 int startIndex = page * numCells;
976 int endIndex = Math.min(startIndex + numCells, mApps.size());
977 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
978 // TODO: we can optimize by just re-applying to existing views
979 layout.removeAllViews();
980 for (int i = startIndex; i < endIndex; ++i) {
981 final ApplicationInfo info = mApps.get(i);
982 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
983 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700984 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700985 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800986 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700987 icon.setOnLongClickListener(this);
988
989 final int index = i - startIndex;
990 final int x = index % mCellCountX;
991 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700992 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700993 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700994 }
995 }
996
Winson Chung80baf5a2010-08-09 16:03:15 -0700997 @Override
998 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700999 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001000 switch (mCustomizationType) {
1001 case WidgetCustomization:
1002 syncWidgetPages();
1003 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001004 case ShortcutCustomization:
1005 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001006 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001007 break;
1008 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001009 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -07001010 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001011 case ApplicationCustomization:
1012 syncAppPages();
1013 centerPagedViewCellLayouts = false;
1014 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001015 default:
1016 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001017 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001018 break;
1019 }
1020
1021 // only try and center the page if there is one page
1022 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001023 if (centerPagedViewCellLayouts) {
1024 if (childCount == 1) {
1025 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1026 layout.enableCenteredContent(true);
1027 } else {
1028 for (int i = 0; i < childCount; ++i) {
1029 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1030 layout.enableCenteredContent(false);
1031 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001032 }
1033 }
1034
1035 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001036 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001037 }
1038
1039 @Override
1040 public void syncPageItems(int page) {
1041 switch (mCustomizationType) {
1042 case WidgetCustomization:
1043 syncWidgetPageItems(page);
1044 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001045 case ShortcutCustomization:
1046 syncListPageItems(page, mShortcutList);
1047 break;
1048 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001049 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001050 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001051 case ApplicationCustomization:
1052 syncAppPageItems(page);
1053 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001054 }
1055 }
Winson Chunge3193b92010-09-10 11:44:42 -07001056
Winson Chungd0d43012010-09-26 17:26:45 -07001057 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001058 protected int getAssociatedLowerPageBound(int page) {
1059 return 0;
1060 }
Winson Chungd0d43012010-09-26 17:26:45 -07001061 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001062 protected int getAssociatedUpperPageBound(int page) {
1063 return getChildCount();
1064 }
Winson Chungd0d43012010-09-26 17:26:45 -07001065
1066 @Override
1067 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001068 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001069 return true;
1070 }
1071
1072 @Override
1073 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1074 return true;
1075 }
1076
1077 @Override
1078 public void onDestroyActionMode(ActionMode mode) {
1079 endChoiceMode();
1080 }
1081
1082 @Override
1083 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1084 return false;
1085 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001086}