blob: 0b874bc11e71ac620c20fba5ed77ba1cb337b332 [file] [log] [blame]
Winson Chung80baf5a2010-08-09 16:03:15 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Michael Jurka72b079e2010-12-10 01:03:53 -080019import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070020
Adam Cohen7b9d3a62010-12-07 21:49:34 -080021import org.xmlpull.v1.XmlPullParser;
22
Adam Cohen120980b2010-12-08 11:05:37 -080023import android.animation.Animator;
Patrick Dubroy047379a2010-12-19 22:02:04 -080024import android.animation.AnimatorListenerAdapter;
Adam Cohen120980b2010-12-08 11:05:37 -080025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
27import android.animation.TimeInterpolator;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080028import android.app.WallpaperManager;
Winson Chung80baf5a2010-08-09 16:03:15 -070029import android.appwidget.AppWidgetManager;
30import android.appwidget.AppWidgetProviderInfo;
31import android.content.ComponentName;
32import android.content.Context;
33import android.content.Intent;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080034import android.content.pm.ActivityInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
37import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070038import android.content.res.TypedArray;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080039import android.content.res.XmlResourceParser;
Winson Chung80baf5a2010-08-09 16:03:15 -070040import android.graphics.Bitmap;
Michael Jurkaaf91de02010-11-23 16:23:58 -080041import android.graphics.Bitmap.Config;
Winson Chungcd4bc492010-12-09 18:52:32 -080042import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070043import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.util.AttributeSet;
45import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080046import android.util.Slog;
47import android.util.TypedValue;
48import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070049import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070050import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070051import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070052import android.view.Menu;
53import android.view.MenuItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070054import android.view.View;
Adam Cohen120980b2010-12-08 11:05:37 -080055import android.view.animation.DecelerateInterpolator;
56import android.view.animation.Interpolator;
Winson Chung59e1f9a2010-12-21 11:31:54 -080057import android.widget.Checkable;
Winson Chunge3193b92010-09-10 11:44:42 -070058import android.widget.ImageView;
59import android.widget.LinearLayout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080060import android.widget.TextView;
Winson Chung80baf5a2010-08-09 16:03:15 -070061
Michael Jurka72b079e2010-12-10 01:03:53 -080062import java.util.ArrayList;
63import java.util.Collections;
64import java.util.Comparator;
65import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070066
Adam Cohen7b9d3a62010-12-07 21:49:34 -080067
Michael Jurka72b079e2010-12-10 01:03:53 -080068public class CustomizePagedView extends PagedViewWithDraggableItems
69 implements View.OnClickListener, DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070070
71 public enum CustomizationType {
72 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070073 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070074 WallpaperCustomization,
75 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070076 }
77
78 private static final String TAG = "CustomizeWorkspace";
Winson Chung80baf5a2010-08-09 16:03:15 -070079
80 private Launcher mLauncher;
81 private DragController mDragController;
82 private PackageManager mPackageManager;
83
84 private CustomizationType mCustomizationType;
85
Winson Chunge3193b92010-09-10 11:44:42 -070086 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
87 private PagedViewCellLayout mWorkspaceWidgetLayout;
88
89 // The mapping between the pages and the widgets that will be laid out on them
90 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
91
Winson Chung45e1d6e2010-11-09 17:19:49 -080092 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070093 private int mMaxWidgetWidth;
94
Winson Chung45e1d6e2010-11-09 17:19:49 -080095 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -070096 private int mMaxWidgetsCellHSpan;
97
Winson Chung45e1d6e2010-11-09 17:19:49 -080098 // The size of the items on the wallpaper tab
99 private int mWallpaperCellHSpan;
100
Winson Chung78bd53c2010-12-09 13:50:24 -0800101 // The max number of wallpaper cells to take a "page" of wallpaper items
102 private int mMaxWallpaperCellHSpan;
103
Winson Chunge3193b92010-09-10 11:44:42 -0700104 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700105 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700106 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700107 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700108 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700109
Winson Chunge3193b92010-09-10 11:44:42 -0700110 private static final int sMinWidgetCellHSpan = 2;
111 private static final int sMaxWidgetCellHSpan = 4;
112
Michael Jurka3125d9d2010-09-27 11:30:20 -0700113 private int mChoiceModeTitleText;
114
Winson Chunge3193b92010-09-10 11:44:42 -0700115 // The scale factor for widget previews inside the widget drawer
116 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700117
118 private final Canvas mCanvas = new Canvas();
119 private final LayoutInflater mInflater;
120
Adam Cohen120980b2010-12-08 11:05:37 -0800121 private final float mTmpFloatPos[] = new float[2];
122 private final float ANIMATION_SCALE = 0.5f;
123 private final int ANIMATION_DURATION = 400;
124 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
125 private ScaleAlphaInterpolator mScaleAlphaInterpolator = new ScaleAlphaInterpolator();
126
Winson Chung80baf5a2010-08-09 16:03:15 -0700127 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700128 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700129 }
130
131 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700132 this(context, attrs, 0);
133 }
134
135 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
136 super(context, attrs, defStyle);
137
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700138 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800139 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
140 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800141 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700142 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
143 a.recycle();
144 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
145 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
146 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700147 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800148
Winson Chung80baf5a2010-08-09 16:03:15 -0700149 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700150 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
151 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700152 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700153
Michael Jurka7426c422010-11-11 15:23:47 -0800154 final Resources r = context.getResources();
Michael Jurka72b079e2010-12-10 01:03:53 -0800155 setDragSlopeThreshold(
156 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f);
Michael Jurka7426c422010-11-11 15:23:47 -0800157
Winson Chung80baf5a2010-08-09 16:03:15 -0700158 setVisibility(View.GONE);
159 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700160 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700161 }
162
Winson Chung7da10252010-10-28 16:07:04 -0700163 @Override
164 protected void init() {
165 super.init();
166 mCenterPagesVertically = false;
167 }
168
Winson Chung80baf5a2010-08-09 16:03:15 -0700169 public void setLauncher(Launcher launcher) {
170 Context context = getContext();
171 mLauncher = launcher;
172 mPackageManager = context.getPackageManager();
173 }
174
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700175 /**
176 * Sets the list of applications that launcher has loaded.
177 */
178 public void setApps(ArrayList<ApplicationInfo> list) {
179 mApps = list;
180 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700181
182 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800183 mPageViewIconCache.retainAllApps(list);
184 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700185 }
186
187 /**
188 * Convenience function to add new items to the set of applications that were previously loaded.
189 * Called by both updateApps() and setApps().
190 */
191 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
192 // we add it in place, in alphabetical order
193 final int count = list.size();
194 for (int i = 0; i < count; ++i) {
195 final ApplicationInfo info = list.get(i);
196 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
197 if (index < 0) {
198 mApps.add(-(index + 1), info);
199 }
200 }
201 }
202
203 /**
204 * Adds new applications to the loaded list, and notifies the paged view to update itself.
205 */
206 public void addApps(ArrayList<ApplicationInfo> list) {
207 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700208
209 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800210 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700211 }
212
213 /**
214 * Convenience function to remove items to the set of applications that were previously loaded.
215 * Called by both updateApps() and removeApps().
216 */
217 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
218 // loop through all the apps and remove apps that have the same component
219 final int length = list.size();
220 for (int i = 0; i < length; ++i) {
221 final ApplicationInfo info = list.get(i);
222 int removeIndex = findAppByComponent(mApps, info);
223 if (removeIndex > -1) {
224 mApps.remove(removeIndex);
Winson Chung04998342011-01-05 13:54:43 -0800225 mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700226 }
227 }
228 }
229
230 /**
231 * Removes applications from the loaded list, and notifies the paged view to update itself.
232 */
233 public void removeApps(ArrayList<ApplicationInfo> list) {
234 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700235
236 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800237 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700238 }
239
240 /**
241 * Updates a set of applications from the loaded list, and notifies the paged view to update
242 * itself.
243 */
244 public void updateApps(ArrayList<ApplicationInfo> list) {
245 // We remove and re-add the updated applications list because it's properties may have
246 // changed (ie. the title), and this will ensure that the items will be in their proper
247 // place in the list.
248 removeAppsWithoutInvalidate(list);
249 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700250
251 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung04998342011-01-05 13:54:43 -0800252 invalidatePageData();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700253 }
254
255 /**
256 * Convenience function to find matching ApplicationInfos for removal.
257 */
258 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
259 ComponentName removeComponent = item.intent.getComponent();
260 final int length = list.size();
261 for (int i = 0; i < length; ++i) {
262 ApplicationInfo info = list.get(i);
263 if (info.intent.getComponent().equals(removeComponent)) {
264 return i;
265 }
266 }
267 return -1;
268 }
269
Winson Chung80baf5a2010-08-09 16:03:15 -0700270 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700271 // get the list of widgets
272 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
273 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
274 @Override
275 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
276 return object1.label.compareTo(object2.label);
277 }
278 });
279
280 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
281 @Override
282 public int compare(ResolveInfo object1, ResolveInfo object2) {
283 return object1.loadLabel(mPackageManager).toString().compareTo(
284 object2.loadLabel(mPackageManager).toString());
285 }
286 };
287
Winson Chung80baf5a2010-08-09 16:03:15 -0700288 // get the list of shortcuts
289 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
290 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
291 Collections.sort(mShortcutList, resolveInfoComparator);
292
Winson Chunge8878e32010-09-15 20:37:09 -0700293 // get the list of wallpapers
294 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800295 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
296 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700297 Collections.sort(mWallpaperList, resolveInfoComparator);
298
Winson Chung04998342011-01-05 13:54:43 -0800299 ArrayList<ResolveInfo> retainShortcutList = new ArrayList<ResolveInfo>(mShortcutList);
300 retainShortcutList.addAll(mWallpaperList);
301 mPageViewIconCache.retainAllShortcuts(retainShortcutList);
302 mPageViewIconCache.retainAllAppWidgets(mWidgetList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700303 invalidatePageData();
304 }
305
306 public void setDragController(DragController dragger) {
307 mDragController = dragger;
308 }
309
310 public void setCustomizationFilter(CustomizationType filterType) {
311 mCustomizationType = filterType;
Winson Chung7d1fcbc2011-01-04 10:22:20 -0800312 cancelDragging();
Winson Chunga12a2502010-12-20 14:41:35 -0800313 if (getChildCount() > 0) {
314 setCurrentPage(0);
315 updateCurrentPageScroll();
316 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700317
Winson Chunga12a2502010-12-20 14:41:35 -0800318 // End the current choice mode so that we don't carry selections across tabs
319 endChoiceMode();
320 }
321 }
322
323 public CustomizationType getCustomizationFilter() {
324 return mCustomizationType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700325 }
326
327 @Override
328 public void onDropCompleted(View target, boolean success) {
Winson Chung59e1f9a2010-12-21 11:31:54 -0800329 resetCheckedGrandchildren();
Michael Jurka3e7c7632010-10-02 16:01:03 -0700330 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700331 }
332
333 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800334 public void onDragViewVisible() {
335 }
336
Adam Cohen120980b2010-12-08 11:05:37 -0800337 class ScaleAlphaInterpolator implements Interpolator {
338 public float getInterpolation(float input) {
339 float pivot = 0.5f;
340 if (input < pivot) {
341 return 0;
342 } else {
343 return (input - pivot)/(1 - pivot);
344 }
345 }
346 }
347
348 private void animateItemOntoScreen(View dragView,
349 final CellLayout layout, final ItemInfo info) {
350 mTmpFloatPos[0] = layout.getWidth() / 2;
351 mTmpFloatPos[1] = layout.getHeight() / 2;
352 mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos);
353
354 final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
355 final View dragCopy = dragLayer.createDragView(dragView);
356 dragCopy.setAlpha(1.0f);
357
358 int dragViewWidth = dragView.getMeasuredWidth();
359 int dragViewHeight = dragView.getMeasuredHeight();
360 float heightOffset = 0;
361 float widthOffset = 0;
362
363 if (dragView instanceof ImageView) {
364 Drawable d = ((ImageView) dragView).getDrawable();
365 int width = d.getIntrinsicWidth();
366 int height = d.getIntrinsicHeight();
367
368 if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) {
369 float f = (dragViewWidth / (width * 1.0f));
370 heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2;
371 } else {
372 float f = (dragViewHeight / (height * 1.0f));
373 widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2;
374 }
375 }
376
377 float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset;
378 float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset;
379
380 ObjectAnimator posAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
381 PropertyValuesHolder.ofFloat("x", toX),
382 PropertyValuesHolder.ofFloat("y", toY));
383 posAnim.setInterpolator(mQuintEaseOutInterpolator);
384 posAnim.setDuration(ANIMATION_DURATION);
385
Patrick Dubroy047379a2010-12-19 22:02:04 -0800386 posAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohen120980b2010-12-08 11:05:37 -0800387 public void onAnimationEnd(Animator animation) {
388 dragLayer.removeView(dragCopy);
389 mLauncher.addExternalItemToScreen(info, layout);
390 post(new Runnable() {
391 public void run() {
392 layout.animateDrop();
393 }
394 });
395 }
396 });
397
398 ObjectAnimator scaleAlphaAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
399 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
400 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
401 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
402 scaleAlphaAnim.setInterpolator(mScaleAlphaInterpolator);
403 scaleAlphaAnim.setDuration(ANIMATION_DURATION);
404
405 posAnim.start();
406 scaleAlphaAnim.start();
407 }
408
Patrick Dubroya669d792010-11-23 14:40:33 -0800409 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800410 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800411 // Return early if this is not initiated from a touch
412 if (!v.isInTouchMode()) return;
413 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800414 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700415
Winson Chungd0d43012010-09-26 17:26:45 -0700416 // On certain pages, we allow single tap to mark items as selected so that they can be
417 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700418 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700419 switch (mCustomizationType) {
420 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700421 mChoiceModeTitleText = R.string.cab_widget_selection_text;
422 enterChoiceMode = true;
423 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700424 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700425 mChoiceModeTitleText = R.string.cab_app_selection_text;
426 enterChoiceMode = true;
427 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700428 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700429 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
430 enterChoiceMode = true;
431 break;
432 default:
433 break;
434 }
Winson Chungd0d43012010-09-26 17:26:45 -0700435
Michael Jurka3125d9d2010-09-27 11:30:20 -0700436 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700437 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700438
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700439 Workspace w = mLauncher.getWorkspace();
440 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
441 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800442 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700443
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700444 animateClickFeedback(v, new Runnable() {
445 @Override
446 public void run() {
Patrick Dubroy047379a2010-12-19 22:02:04 -0800447 cl.calculateSpans(itemInfo);
448 if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) {
449 animateItemOntoScreen(dragView, cl, itemInfo);
450 } else {
451 mLauncher.showOutOfSpaceMessage();
452 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700453 }
454 });
Winson Chungd0d43012010-09-26 17:26:45 -0700455 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700456 }
457
458 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700459 switch (mCustomizationType) {
460 case WallpaperCustomization:
461 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700462 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700463 animateClickFeedback(v, new Runnable() {
464 @Override
465 public void run() {
466 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700467 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700468 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
469 ComponentName name = new ComponentName(info.activityInfo.packageName,
470 info.activityInfo.name);
471 createWallpapersIntent.setComponent(name);
472 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700473 }
474 });
Winson Chungd0d43012010-09-26 17:26:45 -0700475 break;
476 default:
477 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700478 }
479 }
480
Winson Chungcd4bc492010-12-09 18:52:32 -0800481 Bitmap drawableToBitmap(Drawable d, View v) {
482 Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Michael Jurkaaf91de02010-11-23 16:23:58 -0800483 Canvas c = new Canvas(b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800484 c.translate((v.getWidth() - d.getIntrinsicWidth()) / 2, v.getPaddingTop());
Michael Jurkaaf91de02010-11-23 16:23:58 -0800485 d.draw(c);
486 return b;
487 }
488
Adam Cohen120980b2010-12-08 11:05:37 -0800489 private View getDragView(View v) {
490 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
491 v.findViewById(R.id.widget_preview) : v;
492 }
493
Michael Jurka72b079e2010-12-10 01:03:53 -0800494 protected boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700495 // End the current choice mode before we start dragging anything
496 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
497 endChoiceMode();
498 }
Michael Jurka72b079e2010-12-10 01:03:53 -0800499 super.beginDragging(v);
Winson Chungd0d43012010-09-26 17:26:45 -0700500
Winson Chung59e1f9a2010-12-21 11:31:54 -0800501 boolean result = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700502 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800503 case WidgetCustomization: {
Adam Cohen120980b2010-12-08 11:05:37 -0800504 // Get the widget preview as the drag representation
Michael Jurkad3ef3062010-11-23 16:23:58 -0800505 final LinearLayout l = (LinearLayout) v;
Winson Chungcd4bc492010-12-09 18:52:32 -0800506 final ImageView i = (ImageView) l.findViewById(R.id.widget_preview);
507 final Drawable icon = i.getDrawable();
508 Bitmap b = drawableToBitmap(icon, i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700509 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700510
Michael Jurkad3ef3062010-11-23 16:23:58 -0800511 int[] spanXY = CellLayout.rectToCell(
512 getResources(), createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
513 createWidgetInfo.spanX = spanXY[0];
514 createWidgetInfo.spanY = spanXY[1];
515 mLauncher.getWorkspace().onDragStartedWithItemSpans(spanXY[0], spanXY[1], b);
516 mDragController.startDrag(
Winson Chungcd4bc492010-12-09 18:52:32 -0800517 i, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
518 b.recycle();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800519 result = true;
520 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800521 }
522 case ShortcutCustomization: {
523 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800524 final TextView tv = (TextView) v;
525 final Drawable icon = tv.getCompoundDrawables()[1];
526 Bitmap b = drawableToBitmap(icon, tv);
Adam Cohen120980b2010-12-08 11:05:37 -0800527 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Winson Chungcd4bc492010-12-09 18:52:32 -0800528
Michael Jurkad3ef3062010-11-23 16:23:58 -0800529 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800530 mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY,
531 null);
532 b.recycle();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800533 result = true;
534 break;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800535 }
536 case ApplicationCustomization: {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700537 // Pick up the application for dropping
Michael Jurkad3ef3062010-11-23 16:23:58 -0800538 // get icon (top compound drawable, index is 1)
Winson Chungcd4bc492010-12-09 18:52:32 -0800539 final TextView tv = (TextView) v;
540 final Drawable icon = tv.getCompoundDrawables()[1];
541 Bitmap b = drawableToBitmap(icon, tv);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700542 ApplicationInfo app = (ApplicationInfo) v.getTag();
543 app = new ApplicationInfo(app);
544
Michael Jurkad3ef3062010-11-23 16:23:58 -0800545 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chungcd4bc492010-12-09 18:52:32 -0800546 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
547 b.recycle();
Winson Chung59e1f9a2010-12-21 11:31:54 -0800548 result = true;
549 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700550 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800551 }
Winson Chung59e1f9a2010-12-21 11:31:54 -0800552
553 // We toggle the checked state _after_ we create the view for the drag in case toggling the
554 // checked state changes the view's look
555 if (v instanceof Checkable) {
556 // In preparation for drag, we always reset the checked grand children regardless of
557 // what choice mode we are in
558 resetCheckedGrandchildren();
559
560 // Toggle the selection on the dragged app
561 Checkable checkable = (Checkable) v;
562
563 // Note: we toggle the checkable state to actually cause an alpha fade for the duration
564 // of the drag of the item. (The fade-in will occur when all checked states are
565 // disabled when dragging ends)
566 checkable.toggle();
567 }
568
569 return result;
Winson Chung80baf5a2010-08-09 16:03:15 -0700570 }
571
Winson Chunge3193b92010-09-10 11:44:42 -0700572 /**
573 * Pre-processes the layout of the different widget pages.
574 * @return the number of pages of widgets that we have
575 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700576 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700577 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700578
Winson Chunge3193b92010-09-10 11:44:42 -0700579 // create a new page for the first set of widgets
580 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700581 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700582 mWidgetPages.add(newPage);
583
584 // do this until we have no more widgets to lay out
585 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
586 final int widgetCount = mWidgetList.size();
587 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700588 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700589 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700590
Winson Chunge3193b92010-09-10 11:44:42 -0700591 // determine the size of the current widget
592 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
593 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700594
Winson Chunge3193b92010-09-10 11:44:42 -0700595 // create a new page if necessary
596 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
597 numCellsInRow = 0;
598 newPage = new ArrayList<AppWidgetProviderInfo>();
599 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700600 }
601
Winson Chunge3193b92010-09-10 11:44:42 -0700602 // add the item to the current page
603 newPage.add(info);
604 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700605 }
Winson Chunge3193b92010-09-10 11:44:42 -0700606
Winson Chung80baf5a2010-08-09 16:03:15 -0700607 return mWidgetPages.size();
608 }
609
Winson Chunge3193b92010-09-10 11:44:42 -0700610 /**
Winson Chung7da10252010-10-28 16:07:04 -0700611 * Helper function to draw a drawable to the specified canvas with the specified bounds.
612 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800613 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700614 if (bitmap != null) mCanvas.setBitmap(bitmap);
615 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800616 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700617 d.draw(mCanvas);
618 mCanvas.restore();
619 }
620
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800621 /*
622 * This method fetches an xml file specified in the manifest identified by
623 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
624 * an image which will be used as the wallpaper preview for an activity
625 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
626 * in the customize drawer.
627 */
628 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800629 ActivityInfo activityInfo = ri.activityInfo;
630 XmlResourceParser parser = null;
631 try {
632 parser = activityInfo.loadXmlMetaData(mPackageManager,
633 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
634 if (parser == null) {
635 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
636 + "wallpaper provider '" + component + '\'');
637 return null;
638 }
639
640 AttributeSet attrs = Xml.asAttributeSet(parser);
641
642 int type;
643 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
644 && type != XmlPullParser.START_TAG) {
645 // drain whitespace, comments, etc.
646 }
647
648 String nodeName = parser.getName();
649 if (!"wallpaper-preview".equals(nodeName)) {
650 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
651 + "wallpaper provider '" + component + '\'');
652 return null;
653 }
654
655 // If metaData was null, we would have returned earlier when getting
656 // the parser No need to do the check here
657 Resources res = mPackageManager.getResourcesForApplication(
658 activityInfo.applicationInfo);
659
660 TypedArray sa = res.obtainAttributes(attrs,
661 com.android.internal.R.styleable.WallpaperPreviewInfo);
662
663 TypedValue value = sa.peekValue(
664 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
665 if (value == null) return null;
666
667 return res.getDrawable(value.resourceId);
668 } catch (Exception e) {
669 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
670 return null;
671 } finally {
672 if (parser != null) parser.close();
673 }
674 }
675
Winson Chung7da10252010-10-28 16:07:04 -0700676 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800677 * This method will extract the preview image specified by the wallpaper source provider (if it
678 * exists) otherwise, it will try to generate a default image preview.
679 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800680 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800681 // To be implemented later: resolving the up-to-date wallpaper thumbnail
682
683 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
684 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
685 Resources resources = mLauncher.getResources();
686
687 // Create a new bitmap to hold the widget preview
688 int width = (int) (dim * sScaleFactor);
689 int height = (int) (dim * sScaleFactor);
690 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800691
692 Drawable background = parseWallpaperPreviewXml(
693 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
694 boolean foundCustomDrawable = background != null;
695
696 if (!foundCustomDrawable) {
697 background = resources.getDrawable(R.drawable.default_widget_preview);
698 }
699
Winson Chung45e1d6e2010-11-09 17:19:49 -0800700 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
701
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800702 // If we don't have a custom icon, we use the app icon on the default background
703 if (!foundCustomDrawable) {
704 try {
705 final IconCache iconCache =
706 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
707 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
708 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800709
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800710 final int iconSize = minDim / 2;
711 final int offset = iconSize / 4;
712 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
713 } catch (Resources.NotFoundException e) {
714 // if we can't find the icon, then just don't draw it
715 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800716 }
717
Winson Chung29d6fea2010-12-01 15:47:31 -0800718 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800719 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
720 return drawable;
721 }
722
723 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700724 * This method will extract the preview image specified by the widget developer (if it exists),
725 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800726 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700727 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800728 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800729 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700730 String packageName = info.provider.getPackageName();
731 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800732 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700733 if (info.previewImage != 0) {
734 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
735 if (drawable == null) {
736 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
737 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700738 }
739 }
740
741 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700742 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
743 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700744 if (drawable == null) {
745 Resources resources = mLauncher.getResources();
746
Winson Chung80baf5a2010-08-09 16:03:15 -0700747 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700748 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
749 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700750 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
751 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
752 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700753
Winson Chung45e1d6e2010-11-09 17:19:49 -0800754 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700755 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700756 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700757 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700758 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700759 }
760 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700761 icon = resources.getDrawable(R.drawable.ic_launcher_application);
762 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700763
Winson Chunge3193b92010-09-10 11:44:42 -0700764 final int iconSize = minDim / 2;
765 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800766 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700767 } catch (Resources.NotFoundException e) {
768 // if we can't find the icon, then just don't draw it
769 }
770
Winson Chung29d6fea2010-12-01 15:47:31 -0800771 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700772 } else {
773 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800774 final float imageWidth = drawable.getIntrinsicWidth();
775 final float imageHeight = drawable.getIntrinsicHeight();
776 final float aspect = (float) imageWidth / imageHeight;
777 final int scaledWidth =
778 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
779 final int scaledHeight =
780 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700781 int width;
782 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800783 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700784 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800785 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700786 } else {
787 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800788 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700789 }
790
791 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
792 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
793
Winson Chung29d6fea2010-12-01 15:47:31 -0800794 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700795 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800796 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
797 newDrawable.getIntrinsicHeight());
798 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700799 }
800
801 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700802 layout.setCellCount(mCellCountX, mCellCountY);
803 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
804 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700805 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700806 }
807
Winson Chunge3193b92010-09-10 11:44:42 -0700808 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700809 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700810 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
811
812 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
813 }
814
Winson Chung80baf5a2010-08-09 16:03:15 -0700815 private void syncWidgetPages() {
816 if (mWidgetList == null) return;
817
Winson Chunge3193b92010-09-10 11:44:42 -0700818 // we need to repopulate with the LinearLayout layout for the widget pages
819 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700820 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700821 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800822 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700823 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700824 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
825 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700826
Winson Chunge22a8e92010-11-12 13:40:58 -0800827 addView(layout, new LinearLayout.LayoutParams(
828 LinearLayout.LayoutParams.WRAP_CONTENT,
829 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700830 }
831 }
832
833 private void syncWidgetPageItems(int page) {
834 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700835 LinearLayout layout = (LinearLayout) getChildAt(page);
836 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700837 final int count = list.size();
Winson Chung04998342011-01-05 13:54:43 -0800838 final int numPages = getPageCount();
Winson Chung80baf5a2010-08-09 16:03:15 -0700839 layout.removeAllViews();
840 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700841 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
842 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800843 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
844 info.minHeight, null);
845 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700846
Winson Chung29d6fea2010-12-01 15:47:31 -0800847 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700848 R.layout.customize_paged_view_widget, layout, false);
Winson Chung04998342011-01-05 13:54:43 -0800849 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans,
850 mPageViewIconCache, (numPages > 1));
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 Chung04998342011-01-05 13:54:43 -0800884 final int numPages = getPageCount();
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 Chung04998342011-01-05 13:54:43 -0800894 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth,
895 mPageViewIconCache, (numPages > 1));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800896 l.setTag(info);
897 l.setOnClickListener(this);
898
Winson Chung45e1d6e2010-11-09 17:19:49 -0800899 layout.addView(l);
900 }
901 }
902
Winson Chung80baf5a2010-08-09 16:03:15 -0700903 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700904 // we need to repopulate with PagedViewCellLayouts
905 removeAllViews();
906
Winson Chung80baf5a2010-08-09 16:03:15 -0700907 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700908 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700909 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700910 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
911 setupPage(layout);
912 addView(layout);
913 }
914 }
915
916 private void syncListPageItems(int page, List<ResolveInfo> list) {
917 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -0800918 final int numPages = getPageCount();
919 final int numCells = mCellCountX * mCellCountY;
920 final int startIndex = page * numCells;
921 final int endIndex = Math.min(startIndex + numCells, list.size());
922 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700923 // TODO: we can optimize by just re-applying to existing views
924 layout.removeAllViews();
925 for (int i = startIndex; i < endIndex; ++i) {
926 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700927 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
928
Winson Chung241c3b42010-08-25 16:53:03 -0700929 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
930 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700931 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Winson Chung04998342011-01-05 13:54:43 -0800932 ((LauncherApplication) mLauncher.getApplication()).getIconCache(),
933 (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -0700934 switch (mCustomizationType) {
935 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700936 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700937 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700938 case ShortcutCustomization:
939 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
940 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
941 info.activityInfo.name);
942 icon.setTag(createItemInfo);
943 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800944 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700945 icon.setOnLongClickListener(this);
946 break;
947 default:
948 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700949 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700950
951 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700952 final int x = index % mCellCountX;
953 final int y = index / mCellCountX;
954 setupPage(layout);
955 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
956 }
957 }
958
959 private void syncAppPages() {
960 if (mApps == null) return;
961
962 // We need to repopulate with PagedViewCellLayouts
963 removeAllViews();
964
965 // Ensure that we have the right number of pages
966 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
967 for (int i = 0; i < numPages; ++i) {
968 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
969 setupPage(layout);
970 addView(layout);
971 }
972 }
973
974 private void syncAppPageItems(int page) {
975 if (mApps == null) return;
976
977 // ensure that we have the right number of items on the pages
Winson Chung04998342011-01-05 13:54:43 -0800978 final int numPages = getPageCount();
979 final int numCells = mCellCountX * mCellCountY;
980 final int startIndex = page * numCells;
981 final int endIndex = Math.min(startIndex + numCells, mApps.size());
982 final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700983 // TODO: we can optimize by just re-applying to existing views
984 layout.removeAllViews();
985 for (int i = startIndex; i < endIndex; ++i) {
986 final ApplicationInfo info = mApps.get(i);
987 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
988 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung04998342011-01-05 13:54:43 -0800989 icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1));
Winson Chungd0d43012010-09-26 17:26:45 -0700990 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800991 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700992 icon.setOnLongClickListener(this);
993
994 final int index = i - startIndex;
995 final int x = index % mCellCountX;
996 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700997 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700998 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700999 }
1000 }
1001
Winson Chung80baf5a2010-08-09 16:03:15 -07001002 @Override
1003 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -07001004 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001005 switch (mCustomizationType) {
1006 case WidgetCustomization:
1007 syncWidgetPages();
1008 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001009 case ShortcutCustomization:
1010 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001011 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001012 break;
1013 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001014 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -07001015 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001016 case ApplicationCustomization:
1017 syncAppPages();
1018 centerPagedViewCellLayouts = false;
1019 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001020 default:
1021 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001022 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001023 break;
1024 }
1025
1026 // only try and center the page if there is one page
1027 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001028 if (centerPagedViewCellLayouts) {
1029 if (childCount == 1) {
1030 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1031 layout.enableCenteredContent(true);
1032 } else {
1033 for (int i = 0; i < childCount; ++i) {
1034 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1035 layout.enableCenteredContent(false);
1036 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001037 }
1038 }
1039
1040 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001041 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001042 }
1043
1044 @Override
1045 public void syncPageItems(int page) {
1046 switch (mCustomizationType) {
1047 case WidgetCustomization:
1048 syncWidgetPageItems(page);
1049 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001050 case ShortcutCustomization:
1051 syncListPageItems(page, mShortcutList);
1052 break;
1053 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001054 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001055 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001056 case ApplicationCustomization:
1057 syncAppPageItems(page);
1058 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001059 }
1060 }
Winson Chunge3193b92010-09-10 11:44:42 -07001061
Winson Chungd0d43012010-09-26 17:26:45 -07001062 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001063 protected int getAssociatedLowerPageBound(int page) {
1064 return 0;
1065 }
Winson Chungd0d43012010-09-26 17:26:45 -07001066 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001067 protected int getAssociatedUpperPageBound(int page) {
1068 return getChildCount();
1069 }
Winson Chungd0d43012010-09-26 17:26:45 -07001070
1071 @Override
1072 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001073 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001074 return true;
1075 }
1076
1077 @Override
1078 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1079 return true;
1080 }
1081
1082 @Override
1083 public void onDestroyActionMode(ActionMode mode) {
1084 endChoiceMode();
1085 }
1086
1087 @Override
1088 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1089 return false;
1090 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001091}