blob: 799e01793334e21261e153c207ce7c47700277f5 [file] [log] [blame]
Winson Chung80baf5a2010-08-09 16:03:15 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Adam Cohen120980b2010-12-08 11:05:37 -080019import java.util.ArrayList;
20import java.util.Collections;
21import java.util.Comparator;
22import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070023
Adam Cohen7b9d3a62010-12-07 21:49:34 -080024import org.xmlpull.v1.XmlPullParser;
25
Adam Cohen120980b2010-12-08 11:05:37 -080026import android.animation.Animator;
27import android.animation.ObjectAnimator;
28import android.animation.PropertyValuesHolder;
29import android.animation.TimeInterpolator;
30import android.animation.Animator.AnimatorListener;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080031import android.app.WallpaperManager;
Winson Chung80baf5a2010-08-09 16:03:15 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
34import android.content.ComponentName;
35import android.content.Context;
36import android.content.Intent;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080037import android.content.pm.ActivityInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -070038import android.content.pm.PackageManager;
39import android.content.pm.ResolveInfo;
40import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070041import android.content.res.TypedArray;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080042import android.content.res.XmlResourceParser;
Winson Chung80baf5a2010-08-09 16:03:15 -070043import android.graphics.Bitmap;
Winson Chung86f77532010-08-24 11:08:22 -070044import android.graphics.Canvas;
Michael Jurkaaf91de02010-11-23 16:23:58 -080045import android.graphics.Bitmap.Config;
Winson Chung80baf5a2010-08-09 16:03:15 -070046import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070047import android.util.AttributeSet;
48import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080049import android.util.Slog;
50import android.util.TypedValue;
51import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070052import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070053import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070054import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070055import android.view.Menu;
56import android.view.MenuItem;
Michael Jurka7426c422010-11-11 15:23:47 -080057import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070058import android.view.View;
Adam Cohen120980b2010-12-08 11:05:37 -080059import android.view.animation.DecelerateInterpolator;
60import android.view.animation.Interpolator;
Winson Chunge3193b92010-09-10 11:44:42 -070061import android.widget.ImageView;
62import android.widget.LinearLayout;
Michael Jurkad3ef3062010-11-23 16:23:58 -080063import android.widget.TextView;
Winson Chung80baf5a2010-08-09 16:03:15 -070064
Adam Cohen120980b2010-12-08 11:05:37 -080065import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070066
Adam Cohen7b9d3a62010-12-07 21:49:34 -080067
Winson Chung80baf5a2010-08-09 16:03:15 -070068public class CustomizePagedView extends PagedView
Michael Jurka7426c422010-11-11 15:23:47 -080069 implements View.OnLongClickListener, View.OnClickListener, View.OnTouchListener,
Winson Chungd0d43012010-09-26 17:26:45 -070070 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070071
72 public enum CustomizationType {
73 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070074 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070075 WallpaperCustomization,
76 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070077 }
78
79 private static final String TAG = "CustomizeWorkspace";
80 private static final boolean DEBUG = false;
81
Michael Jurka7426c422010-11-11 15:23:47 -080082 private View mLastTouchedItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070083 private Launcher mLauncher;
84 private DragController mDragController;
85 private PackageManager mPackageManager;
Michael Jurka7426c422010-11-11 15:23:47 -080086 private boolean mIsDragging;
87 private float mDragSlopeThreshold;
Winson Chung80baf5a2010-08-09 16:03:15 -070088
89 private CustomizationType mCustomizationType;
90
Winson Chunge3193b92010-09-10 11:44:42 -070091 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
92 private PagedViewCellLayout mWorkspaceWidgetLayout;
93
94 // The mapping between the pages and the widgets that will be laid out on them
95 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
96
Winson Chung45e1d6e2010-11-09 17:19:49 -080097 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070098 private int mMaxWidgetWidth;
99
Winson Chung45e1d6e2010-11-09 17:19:49 -0800100 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -0700101 private int mMaxWidgetsCellHSpan;
102
Winson Chung45e1d6e2010-11-09 17:19:49 -0800103 // The size of the items on the wallpaper tab
104 private int mWallpaperCellHSpan;
105
Winson Chung78bd53c2010-12-09 13:50:24 -0800106 // The max number of wallpaper cells to take a "page" of wallpaper items
107 private int mMaxWallpaperCellHSpan;
108
Winson Chunge3193b92010-09-10 11:44:42 -0700109 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700110 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700111 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700112 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700113 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700114
Winson Chunge3193b92010-09-10 11:44:42 -0700115 private static final int sMinWidgetCellHSpan = 2;
116 private static final int sMaxWidgetCellHSpan = 4;
117
Michael Jurka3125d9d2010-09-27 11:30:20 -0700118 private int mChoiceModeTitleText;
119
Winson Chunge3193b92010-09-10 11:44:42 -0700120 // The scale factor for widget previews inside the widget drawer
121 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700122
123 private final Canvas mCanvas = new Canvas();
124 private final LayoutInflater mInflater;
125
Adam Cohen120980b2010-12-08 11:05:37 -0800126 private final float mTmpFloatPos[] = new float[2];
127 private final float ANIMATION_SCALE = 0.5f;
128 private final int ANIMATION_DURATION = 400;
129 private TimeInterpolator mQuintEaseOutInterpolator = new DecelerateInterpolator(2.5f);
130 private ScaleAlphaInterpolator mScaleAlphaInterpolator = new ScaleAlphaInterpolator();
131
Winson Chung80baf5a2010-08-09 16:03:15 -0700132 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700133 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700134 }
135
136 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700137 this(context, attrs, 0);
138 }
139
140 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
141 super(context, attrs, defStyle);
142
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700143 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800144 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
145 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung78bd53c2010-12-09 13:50:24 -0800146 mMaxWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellCountX, 8);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700147 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
148 a.recycle();
149 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
150 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
151 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700152 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800153
Winson Chung80baf5a2010-08-09 16:03:15 -0700154 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700155 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
156 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700157 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700158
Michael Jurka7426c422010-11-11 15:23:47 -0800159 final Resources r = context.getResources();
160 mDragSlopeThreshold =
161 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f;
162
Winson Chung80baf5a2010-08-09 16:03:15 -0700163 setVisibility(View.GONE);
164 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700165 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700166 }
167
Winson Chung7da10252010-10-28 16:07:04 -0700168 @Override
169 protected void init() {
170 super.init();
171 mCenterPagesVertically = false;
172 }
173
Winson Chung80baf5a2010-08-09 16:03:15 -0700174 public void setLauncher(Launcher launcher) {
175 Context context = getContext();
176 mLauncher = launcher;
177 mPackageManager = context.getPackageManager();
178 }
179
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700180 /**
181 * Sets the list of applications that launcher has loaded.
182 */
183 public void setApps(ArrayList<ApplicationInfo> list) {
184 mApps = list;
185 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700186
187 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700188 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700189 }
190
191 /**
192 * Convenience function to add new items to the set of applications that were previously loaded.
193 * Called by both updateApps() and setApps().
194 */
195 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
196 // we add it in place, in alphabetical order
197 final int count = list.size();
198 for (int i = 0; i < count; ++i) {
199 final ApplicationInfo info = list.get(i);
200 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
201 if (index < 0) {
202 mApps.add(-(index + 1), info);
203 }
204 }
205 }
206
207 /**
208 * Adds new applications to the loaded list, and notifies the paged view to update itself.
209 */
210 public void addApps(ArrayList<ApplicationInfo> list) {
211 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700212
213 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700214 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700215 }
216
217 /**
218 * Convenience function to remove items to the set of applications that were previously loaded.
219 * Called by both updateApps() and removeApps().
220 */
221 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
222 // loop through all the apps and remove apps that have the same component
223 final int length = list.size();
224 for (int i = 0; i < length; ++i) {
225 final ApplicationInfo info = list.get(i);
226 int removeIndex = findAppByComponent(mApps, info);
227 if (removeIndex > -1) {
228 mApps.remove(removeIndex);
229 mPageViewIconCache.removeOutline(info);
230 }
231 }
232 }
233
234 /**
235 * Removes applications from the loaded list, and notifies the paged view to update itself.
236 */
237 public void removeApps(ArrayList<ApplicationInfo> list) {
238 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700239
240 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700241 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700242 }
243
244 /**
245 * Updates a set of applications from the loaded list, and notifies the paged view to update
246 * itself.
247 */
248 public void updateApps(ArrayList<ApplicationInfo> list) {
249 // We remove and re-add the updated applications list because it's properties may have
250 // changed (ie. the title), and this will ensure that the items will be in their proper
251 // place in the list.
252 removeAppsWithoutInvalidate(list);
253 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700254
255 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700256 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700257 }
258
259 /**
260 * Convenience function to find matching ApplicationInfos for removal.
261 */
262 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
263 ComponentName removeComponent = item.intent.getComponent();
264 final int length = list.size();
265 for (int i = 0; i < length; ++i) {
266 ApplicationInfo info = list.get(i);
267 if (info.intent.getComponent().equals(removeComponent)) {
268 return i;
269 }
270 }
271 return -1;
272 }
273
Winson Chung80baf5a2010-08-09 16:03:15 -0700274 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700275 // get the list of widgets
276 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
277 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
278 @Override
279 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
280 return object1.label.compareTo(object2.label);
281 }
282 });
283
284 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
285 @Override
286 public int compare(ResolveInfo object1, ResolveInfo object2) {
287 return object1.loadLabel(mPackageManager).toString().compareTo(
288 object2.loadLabel(mPackageManager).toString());
289 }
290 };
291
Winson Chung80baf5a2010-08-09 16:03:15 -0700292 // get the list of shortcuts
293 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
294 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
295 Collections.sort(mShortcutList, resolveInfoComparator);
296
Winson Chunge8878e32010-09-15 20:37:09 -0700297 // get the list of wallpapers
298 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800299 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
300 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700301 Collections.sort(mWallpaperList, resolveInfoComparator);
302
Winson Chung10fefb12010-11-01 11:57:06 -0700303 invalidatePageDataAndIconCache();
304 }
305
306 private void invalidatePageDataAndIconCache() {
307 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700308 mPageViewIconCache.clear();
309
Winson Chunge3193b92010-09-10 11:44:42 -0700310 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700311 invalidatePageData();
312 }
313
314 public void setDragController(DragController dragger) {
315 mDragController = dragger;
316 }
317
318 public void setCustomizationFilter(CustomizationType filterType) {
319 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700320 setCurrentPage(0);
Winson Chungbbc60d82010-11-11 16:34:41 -0800321 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700323
324 // End the current choice mode so that we don't carry selections across tabs
325 endChoiceMode();
Winson Chung649a4ca2010-11-18 10:38:13 -0800326 // Reset the touch item (if we are mid-dragging)
327 mLastTouchedItem = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700328 }
329
330 @Override
331 public void onDropCompleted(View target, boolean success) {
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
388 posAnim.addListener(new AnimatorListener() {
389 public void onAnimationCancel(Animator animation) {}
390 public void onAnimationRepeat(Animator animation) {}
391 public void onAnimationStart(Animator animation) {}
392
393 public void onAnimationEnd(Animator animation) {
394 dragLayer.removeView(dragCopy);
395 mLauncher.addExternalItemToScreen(info, layout);
396 post(new Runnable() {
397 public void run() {
398 layout.animateDrop();
399 }
400 });
401 }
402 });
403
404 ObjectAnimator scaleAlphaAnim = ObjectAnimator.ofPropertyValuesHolder(dragCopy,
405 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f),
406 PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE),
407 PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE));
408 scaleAlphaAnim.setInterpolator(mScaleAlphaInterpolator);
409 scaleAlphaAnim.setDuration(ANIMATION_DURATION);
410
411 posAnim.start();
412 scaleAlphaAnim.start();
413 }
414
Patrick Dubroya669d792010-11-23 14:40:33 -0800415 @Override
Adam Cohen120980b2010-12-08 11:05:37 -0800416 public void onClick(final View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800417 // Return early if this is not initiated from a touch
418 if (!v.isInTouchMode()) return;
419 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800420 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700421
Winson Chungd0d43012010-09-26 17:26:45 -0700422 // On certain pages, we allow single tap to mark items as selected so that they can be
423 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700424 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700425 switch (mCustomizationType) {
426 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700427 mChoiceModeTitleText = R.string.cab_widget_selection_text;
428 enterChoiceMode = true;
429 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700430 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700431 mChoiceModeTitleText = R.string.cab_app_selection_text;
432 enterChoiceMode = true;
433 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700434 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700435 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
436 enterChoiceMode = true;
437 break;
438 default:
439 break;
440 }
Winson Chungd0d43012010-09-26 17:26:45 -0700441
Michael Jurka3125d9d2010-09-27 11:30:20 -0700442 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700443 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700444
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700445 Workspace w = mLauncher.getWorkspace();
446 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
447 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Adam Cohen120980b2010-12-08 11:05:37 -0800448 final View dragView = getDragView(v);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700449
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700450 animateClickFeedback(v, new Runnable() {
451 @Override
452 public void run() {
Adam Cohen120980b2010-12-08 11:05:37 -0800453 animateItemOntoScreen(dragView, cl, itemInfo);
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700454 }
455 });
Winson Chungd0d43012010-09-26 17:26:45 -0700456 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700457 }
458
459 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700460 switch (mCustomizationType) {
461 case WallpaperCustomization:
462 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700463 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700464 animateClickFeedback(v, new Runnable() {
465 @Override
466 public void run() {
467 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700468 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700469 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
470 ComponentName name = new ComponentName(info.activityInfo.packageName,
471 info.activityInfo.name);
472 createWallpapersIntent.setComponent(name);
473 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700474 }
475 });
Winson Chungd0d43012010-09-26 17:26:45 -0700476 break;
477 default:
478 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700479 }
480 }
481
482 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700483 public boolean onLongClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800484 // Return early if this is not initiated from a touch
485 if (!v.isInTouchMode()) return false;
486 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800487 if (mNextPage != INVALID_PAGE) return false;
Michael Jurka7426c422010-11-11 15:23:47 -0800488 return beginDragging(v);
489 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700490
Michael Jurka7426c422010-11-11 15:23:47 -0800491 @Override
492 public boolean onTouch(View v, MotionEvent event) {
493 mLastTouchedItem = v;
494 return false;
495 }
496
497 /*
498 * Determines if we should change the touch state to start scrolling after the
499 * user moves their touch point too far.
500 */
501 protected void determineScrollingStart(MotionEvent ev) {
502 if (!mIsDragging) super.determineScrollingStart(ev);
503 }
504
505 /*
506 * Determines if we should change the touch state to start dragging after the
507 * user moves their touch point far enough.
508 */
509 protected void determineDraggingStart(MotionEvent ev) {
510 /*
511 * Locally do absolute value. mLastMotionX is set to the y value
512 * of the down event.
513 */
514 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
515 final float x = ev.getX(pointerIndex);
516 final float y = ev.getY(pointerIndex);
517 final int xDiff = (int) Math.abs(x - mLastMotionX);
518 final int yDiff = (int) Math.abs(y - mLastMotionY);
519
520 final int touchSlop = mTouchSlop;
521 boolean yMoved = yDiff > touchSlop;
522 boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
523
Winson Chung649a4ca2010-11-18 10:38:13 -0800524 if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
Michael Jurka7426c422010-11-11 15:23:47 -0800525 // Drag if the user moved far enough along the Y axis
526 beginDragging(mLastTouchedItem);
527
528 // Cancel any pending longpress
529 if (mAllowLongPress) {
530 mAllowLongPress = false;
531 // Try canceling the long press. It could also have been scheduled
532 // by a distant descendant, so use the mAllowLongPress flag to block
533 // everything
534 final View currentPage = getPageAt(mCurrentPage);
535 if (currentPage != null) {
536 currentPage.cancelLongPress();
537 }
538 }
539 }
540 }
541
542 @Override
543 public boolean onInterceptTouchEvent(MotionEvent ev) {
544 final int action = ev.getAction();
545 switch (action & MotionEvent.ACTION_MASK) {
546 case MotionEvent.ACTION_DOWN:
547 mIsDragging = false;
548 break;
549 case MotionEvent.ACTION_MOVE:
550 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
551 determineDraggingStart(ev);
552 }
553 break;
554 }
555 return super.onInterceptTouchEvent(ev);
556 }
557
558 @Override
559 public boolean onTouchEvent(MotionEvent ev) {
560 final int action = ev.getAction();
561 switch (action & MotionEvent.ACTION_MASK) {
562 case MotionEvent.ACTION_DOWN:
563 mIsDragging = false;
564 break;
565 case MotionEvent.ACTION_MOVE:
566 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
567 determineDraggingStart(ev);
568 }
569 break;
570 }
571 return super.onTouchEvent(ev);
572 }
573
Michael Jurkaaf91de02010-11-23 16:23:58 -0800574 Bitmap drawableToBitmap(Drawable d) {
575 Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(),
576 Bitmap.Config.ARGB_8888);
577 Canvas c = new Canvas(b);
578 d.draw(c);
579 return b;
580 }
581
Adam Cohen120980b2010-12-08 11:05:37 -0800582 private View getDragView(View v) {
583 return (mCustomizationType == CustomizationType.WidgetCustomization) ?
584 v.findViewById(R.id.widget_preview) : v;
585 }
586
Michael Jurka7426c422010-11-11 15:23:47 -0800587 private boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700588 // End the current choice mode before we start dragging anything
589 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
590 endChoiceMode();
591 }
Michael Jurka7426c422010-11-11 15:23:47 -0800592 mIsDragging = true;
Winson Chungd0d43012010-09-26 17:26:45 -0700593
Winson Chung80baf5a2010-08-09 16:03:15 -0700594 switch (mCustomizationType) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800595 case WidgetCustomization: {
Adam Cohen120980b2010-12-08 11:05:37 -0800596 // Get the widget preview as the drag representation
Michael Jurkad3ef3062010-11-23 16:23:58 -0800597 final LinearLayout l = (LinearLayout) v;
598 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
599 Bitmap b = drawableToBitmap(icon);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700600 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Adam Cohen120980b2010-12-08 11:05:37 -0800601 final View dragView = v.findViewById(R.id.widget_preview);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700602
Michael Jurkad3ef3062010-11-23 16:23:58 -0800603 int[] spanXY = CellLayout.rectToCell(
604 getResources(), createWidgetInfo.minWidth, createWidgetInfo.minHeight, null);
605 createWidgetInfo.spanX = spanXY[0];
606 createWidgetInfo.spanY = spanXY[1];
607 mLauncher.getWorkspace().onDragStartedWithItemSpans(spanXY[0], spanXY[1], b);
608 mDragController.startDrag(
609 v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chunge3193b92010-09-10 11:44:42 -0700610
Winson Chung80baf5a2010-08-09 16:03:15 -0700611 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800612 }
613 case ShortcutCustomization: {
614 // get icon (top compound drawable, index is 1)
615 final Drawable icon = ((TextView) v).getCompoundDrawables()[1];
616 Bitmap b = drawableToBitmap(icon);
Adam Cohen120980b2010-12-08 11:05:37 -0800617 PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800618 mDragController.startDrag(
619 v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
620 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chung80baf5a2010-08-09 16:03:15 -0700621 return true;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800622 }
623 case ApplicationCustomization: {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700624 // Pick up the application for dropping
Michael Jurkad3ef3062010-11-23 16:23:58 -0800625 // get icon (top compound drawable, index is 1)
626 final Drawable icon = ((TextView) v).getCompoundDrawables()[1];
627 Bitmap b = drawableToBitmap(icon);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700628 ApplicationInfo app = (ApplicationInfo) v.getTag();
629 app = new ApplicationInfo(app);
630
Michael Jurkad3ef3062010-11-23 16:23:58 -0800631 mDragController.startDrag(v, b, this, app, DragController.DRAG_ACTION_COPY, null);
632 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700633 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700634 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800635 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700636 return false;
637 }
638
Winson Chunge3193b92010-09-10 11:44:42 -0700639 /**
640 * Pre-processes the layout of the different widget pages.
641 * @return the number of pages of widgets that we have
642 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700643 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700644 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700645
Winson Chunge3193b92010-09-10 11:44:42 -0700646 // create a new page for the first set of widgets
647 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700648 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700649 mWidgetPages.add(newPage);
650
651 // do this until we have no more widgets to lay out
652 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
653 final int widgetCount = mWidgetList.size();
654 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700655 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700656 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700657
Winson Chunge3193b92010-09-10 11:44:42 -0700658 // determine the size of the current widget
659 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
660 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700661
Winson Chunge3193b92010-09-10 11:44:42 -0700662 // create a new page if necessary
663 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
664 numCellsInRow = 0;
665 newPage = new ArrayList<AppWidgetProviderInfo>();
666 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700667 }
668
Winson Chunge3193b92010-09-10 11:44:42 -0700669 // add the item to the current page
670 newPage.add(info);
671 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700672 }
Winson Chunge3193b92010-09-10 11:44:42 -0700673
Winson Chung80baf5a2010-08-09 16:03:15 -0700674 return mWidgetPages.size();
675 }
676
Winson Chunge3193b92010-09-10 11:44:42 -0700677 /**
Winson Chung7da10252010-10-28 16:07:04 -0700678 * Helper function to draw a drawable to the specified canvas with the specified bounds.
679 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800680 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700681 if (bitmap != null) mCanvas.setBitmap(bitmap);
682 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800683 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700684 d.draw(mCanvas);
685 mCanvas.restore();
686 }
687
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800688 /*
689 * This method fetches an xml file specified in the manifest identified by
690 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
691 * an image which will be used as the wallpaper preview for an activity
692 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
693 * in the customize drawer.
694 */
695 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
696 Drawable d = null;
697
698 ActivityInfo activityInfo = ri.activityInfo;
699 XmlResourceParser parser = null;
700 try {
701 parser = activityInfo.loadXmlMetaData(mPackageManager,
702 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
703 if (parser == null) {
704 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
705 + "wallpaper provider '" + component + '\'');
706 return null;
707 }
708
709 AttributeSet attrs = Xml.asAttributeSet(parser);
710
711 int type;
712 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
713 && type != XmlPullParser.START_TAG) {
714 // drain whitespace, comments, etc.
715 }
716
717 String nodeName = parser.getName();
718 if (!"wallpaper-preview".equals(nodeName)) {
719 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
720 + "wallpaper provider '" + component + '\'');
721 return null;
722 }
723
724 // If metaData was null, we would have returned earlier when getting
725 // the parser No need to do the check here
726 Resources res = mPackageManager.getResourcesForApplication(
727 activityInfo.applicationInfo);
728
729 TypedArray sa = res.obtainAttributes(attrs,
730 com.android.internal.R.styleable.WallpaperPreviewInfo);
731
732 TypedValue value = sa.peekValue(
733 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
734 if (value == null) return null;
735
736 return res.getDrawable(value.resourceId);
737 } catch (Exception e) {
738 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
739 return null;
740 } finally {
741 if (parser != null) parser.close();
742 }
743 }
744
Winson Chung7da10252010-10-28 16:07:04 -0700745 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800746 * This method will extract the preview image specified by the wallpaper source provider (if it
747 * exists) otherwise, it will try to generate a default image preview.
748 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800749 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800750 // To be implemented later: resolving the up-to-date wallpaper thumbnail
751
752 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
753 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
754 Resources resources = mLauncher.getResources();
755
756 // Create a new bitmap to hold the widget preview
757 int width = (int) (dim * sScaleFactor);
758 int height = (int) (dim * sScaleFactor);
759 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800760
761 Drawable background = parseWallpaperPreviewXml(
762 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
763 boolean foundCustomDrawable = background != null;
764
765 if (!foundCustomDrawable) {
766 background = resources.getDrawable(R.drawable.default_widget_preview);
767 }
768
Winson Chung45e1d6e2010-11-09 17:19:49 -0800769 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
770
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800771 // If we don't have a custom icon, we use the app icon on the default background
772 if (!foundCustomDrawable) {
773 try {
774 final IconCache iconCache =
775 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
776 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
777 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800778
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800779 final int iconSize = minDim / 2;
780 final int offset = iconSize / 4;
781 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
782 } catch (Resources.NotFoundException e) {
783 // if we can't find the icon, then just don't draw it
784 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800785 }
786
Winson Chung29d6fea2010-12-01 15:47:31 -0800787 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800788 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
789 return drawable;
790 }
791
792 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700793 * This method will extract the preview image specified by the widget developer (if it exists),
794 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800795 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700796 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800797 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800798 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700799 String packageName = info.provider.getPackageName();
800 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800801 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700802 if (info.previewImage != 0) {
803 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
804 if (drawable == null) {
805 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
806 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700807 }
808 }
809
810 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700811 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
812 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700813 if (drawable == null) {
814 Resources resources = mLauncher.getResources();
815
Winson Chung80baf5a2010-08-09 16:03:15 -0700816 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700817 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
818 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700819 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
820 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
821 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700822
Winson Chung45e1d6e2010-11-09 17:19:49 -0800823 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700824 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700825 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700826 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700827 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700828 }
829 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700830 icon = resources.getDrawable(R.drawable.ic_launcher_application);
831 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700832
Winson Chunge3193b92010-09-10 11:44:42 -0700833 final int iconSize = minDim / 2;
834 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800835 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700836 } catch (Resources.NotFoundException e) {
837 // if we can't find the icon, then just don't draw it
838 }
839
Winson Chung29d6fea2010-12-01 15:47:31 -0800840 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700841 } else {
842 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800843 final float imageWidth = drawable.getIntrinsicWidth();
844 final float imageHeight = drawable.getIntrinsicHeight();
845 final float aspect = (float) imageWidth / imageHeight;
846 final int scaledWidth =
847 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
848 final int scaledHeight =
849 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700850 int width;
851 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800852 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700853 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800854 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700855 } else {
856 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800857 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700858 }
859
860 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
861 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
862
Winson Chung29d6fea2010-12-01 15:47:31 -0800863 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700864 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800865 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
866 newDrawable.getIntrinsicHeight());
867 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700868 }
869
870 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700871 layout.setCellCount(mCellCountX, mCellCountY);
872 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
873 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700874 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700875 }
876
Winson Chunge3193b92010-09-10 11:44:42 -0700877 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700878 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700879 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
880
881 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
882 }
883
Winson Chung80baf5a2010-08-09 16:03:15 -0700884 private void syncWidgetPages() {
885 if (mWidgetList == null) return;
886
Winson Chunge3193b92010-09-10 11:44:42 -0700887 // we need to repopulate with the LinearLayout layout for the widget pages
888 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700889 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700890 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800891 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700892 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700893 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
894 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700895
Winson Chunge22a8e92010-11-12 13:40:58 -0800896 addView(layout, new LinearLayout.LayoutParams(
897 LinearLayout.LayoutParams.WRAP_CONTENT,
898 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700899 }
900 }
901
902 private void syncWidgetPageItems(int page) {
903 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700904 LinearLayout layout = (LinearLayout) getChildAt(page);
905 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700906 final int count = list.size();
907 layout.removeAllViews();
908 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700909 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
910 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800911 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
912 info.minHeight, null);
913 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700914
Winson Chung29d6fea2010-12-01 15:47:31 -0800915 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700916 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800917 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700918 l.setTag(createItemInfo);
919 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800920 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700921 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700922
Winson Chunge3193b92010-09-10 11:44:42 -0700923 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700924 }
925 }
926
Winson Chung45e1d6e2010-11-09 17:19:49 -0800927 private void syncWallpaperPages() {
928 if (mWallpaperList == null) return;
929
930 // We need to repopulate the LinearLayout for the wallpaper pages
931 removeAllViews();
932 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
Winson Chung78bd53c2010-12-09 13:50:24 -0800933 mMaxWallpaperCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800934 for (int i = 0; i < numPages; ++i) {
935 LinearLayout layout = new PagedViewExtendedLayout(getContext());
936 layout.setGravity(Gravity.CENTER_HORIZONTAL);
937 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
938 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
939
Winson Chunge22a8e92010-11-12 13:40:58 -0800940 addView(layout, new LinearLayout.LayoutParams(
941 LinearLayout.LayoutParams.WRAP_CONTENT,
942 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800943 }
944 }
945
946 private void syncWallpaperPageItems(int page) {
947 // Load the items on to the pages
948 LinearLayout layout = (LinearLayout) getChildAt(page);
949 layout.removeAllViews();
950 final int count = mWallpaperList.size();
Winson Chung78bd53c2010-12-09 13:50:24 -0800951 final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan;
Winson Chungd28ed492010-11-22 14:34:57 -0800952 final int startIndex = page * numItemsPerPage;
953 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
954 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800955 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800956 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800957
Winson Chung29d6fea2010-12-01 15:47:31 -0800958 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800959 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800960 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800961 l.setTag(info);
962 l.setOnClickListener(this);
963
Winson Chung45e1d6e2010-11-09 17:19:49 -0800964 layout.addView(l);
965 }
966 }
967
Winson Chung80baf5a2010-08-09 16:03:15 -0700968 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700969 // we need to repopulate with PagedViewCellLayouts
970 removeAllViews();
971
Winson Chung80baf5a2010-08-09 16:03:15 -0700972 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700973 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700974 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700975 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
976 setupPage(layout);
977 addView(layout);
978 }
979 }
980
981 private void syncListPageItems(int page, List<ResolveInfo> list) {
982 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700983 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700984 int startIndex = page * numCells;
985 int endIndex = Math.min(startIndex + numCells, list.size());
986 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
987 // TODO: we can optimize by just re-applying to existing views
988 layout.removeAllViews();
989 for (int i = startIndex; i < endIndex; ++i) {
990 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700991 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
992
Winson Chung241c3b42010-08-25 16:53:03 -0700993 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
994 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700995 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
Adam Cohen120980b2010-12-08 11:05:37 -0800996 ((LauncherApplication) mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700997 switch (mCustomizationType) {
998 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700999 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001000 break;
Winson Chungd0d43012010-09-26 17:26:45 -07001001 case ShortcutCustomization:
1002 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
1003 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
1004 info.activityInfo.name);
1005 icon.setTag(createItemInfo);
1006 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001007 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -07001008 icon.setOnLongClickListener(this);
1009 break;
1010 default:
1011 break;
Winson Chunge8878e32010-09-15 20:37:09 -07001012 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001013
1014 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001015 final int x = index % mCellCountX;
1016 final int y = index / mCellCountX;
1017 setupPage(layout);
1018 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
1019 }
1020 }
1021
1022 private void syncAppPages() {
1023 if (mApps == null) return;
1024
1025 // We need to repopulate with PagedViewCellLayouts
1026 removeAllViews();
1027
1028 // Ensure that we have the right number of pages
1029 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
1030 for (int i = 0; i < numPages; ++i) {
1031 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
1032 setupPage(layout);
1033 addView(layout);
1034 }
1035 }
1036
1037 private void syncAppPageItems(int page) {
1038 if (mApps == null) return;
1039
1040 // ensure that we have the right number of items on the pages
1041 int numCells = mCellCountX * mCellCountY;
1042 int startIndex = page * numCells;
1043 int endIndex = Math.min(startIndex + numCells, mApps.size());
1044 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
1045 // TODO: we can optimize by just re-applying to existing views
1046 layout.removeAllViews();
1047 for (int i = startIndex; i < endIndex; ++i) {
1048 final ApplicationInfo info = mApps.get(i);
1049 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
1050 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -07001051 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -07001052 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -08001053 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001054 icon.setOnLongClickListener(this);
1055
1056 final int index = i - startIndex;
1057 final int x = index % mCellCountX;
1058 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -07001059 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -07001060 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -07001061 }
1062 }
1063
Winson Chung80baf5a2010-08-09 16:03:15 -07001064 @Override
1065 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -07001066 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -07001067 switch (mCustomizationType) {
1068 case WidgetCustomization:
1069 syncWidgetPages();
1070 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001071 case ShortcutCustomization:
1072 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -07001073 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -07001074 break;
1075 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001076 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -07001077 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001078 case ApplicationCustomization:
1079 syncAppPages();
1080 centerPagedViewCellLayouts = false;
1081 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001082 default:
1083 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -07001084 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -07001085 break;
1086 }
1087
1088 // only try and center the page if there is one page
1089 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -07001090 if (centerPagedViewCellLayouts) {
1091 if (childCount == 1) {
1092 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
1093 layout.enableCenteredContent(true);
1094 } else {
1095 for (int i = 0; i < childCount; ++i) {
1096 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
1097 layout.enableCenteredContent(false);
1098 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001099 }
1100 }
1101
1102 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -07001103 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -07001104 }
1105
1106 @Override
1107 public void syncPageItems(int page) {
1108 switch (mCustomizationType) {
1109 case WidgetCustomization:
1110 syncWidgetPageItems(page);
1111 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001112 case ShortcutCustomization:
1113 syncListPageItems(page, mShortcutList);
1114 break;
1115 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001116 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001117 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001118 case ApplicationCustomization:
1119 syncAppPageItems(page);
1120 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001121 }
1122 }
Winson Chunge3193b92010-09-10 11:44:42 -07001123
Winson Chungd0d43012010-09-26 17:26:45 -07001124 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001125 protected int getAssociatedLowerPageBound(int page) {
1126 return 0;
1127 }
Winson Chungd0d43012010-09-26 17:26:45 -07001128 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001129 protected int getAssociatedUpperPageBound(int page) {
1130 return getChildCount();
1131 }
Winson Chungd0d43012010-09-26 17:26:45 -07001132
1133 @Override
1134 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001135 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001136 return true;
1137 }
1138
1139 @Override
1140 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1141 return true;
1142 }
1143
1144 @Override
1145 public void onDestroyActionMode(ActionMode mode) {
1146 endChoiceMode();
1147 }
1148
1149 @Override
1150 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1151 return false;
1152 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001153}