blob: 91a3bdb5416fe4f1b7efba9a1acbbdc205b929d8 [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 Jurkaaf91de02010-11-23 16:23:58 -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
23import android.app.WallpaperManager;
Winson Chung80baf5a2010-08-09 16:03:15 -070024import android.appwidget.AppWidgetManager;
25import android.appwidget.AppWidgetProviderInfo;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080029import android.content.pm.ActivityInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -070030import android.content.pm.PackageManager;
31import android.content.pm.ResolveInfo;
32import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070033import android.content.res.TypedArray;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080034import android.content.res.XmlResourceParser;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.graphics.Bitmap;
Winson Chung86f77532010-08-24 11:08:22 -070036import android.graphics.Canvas;
Michael Jurkaaf91de02010-11-23 16:23:58 -080037import android.graphics.Bitmap.Config;
Winson Chung80baf5a2010-08-09 16:03:15 -070038import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070039import android.util.AttributeSet;
40import android.util.Log;
Adam Cohen7b9d3a62010-12-07 21:49:34 -080041import android.util.Slog;
42import android.util.TypedValue;
43import android.util.Xml;
Winson Chungd0d43012010-09-26 17:26:45 -070044import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070045import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070046import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070047import android.view.Menu;
48import android.view.MenuItem;
Michael Jurka7426c422010-11-11 15:23:47 -080049import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070050import android.view.View;
Winson Chunge3193b92010-09-10 11:44:42 -070051import android.widget.ImageView;
52import android.widget.LinearLayout;
Winson Chung80baf5a2010-08-09 16:03:15 -070053
Michael Jurkaaf91de02010-11-23 16:23:58 -080054import java.util.ArrayList;
55import java.util.Collections;
56import java.util.Comparator;
57import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070058
Adam Cohen7b9d3a62010-12-07 21:49:34 -080059
Winson Chung80baf5a2010-08-09 16:03:15 -070060public class CustomizePagedView extends PagedView
Michael Jurka7426c422010-11-11 15:23:47 -080061 implements View.OnLongClickListener, View.OnClickListener, View.OnTouchListener,
Winson Chungd0d43012010-09-26 17:26:45 -070062 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070063
64 public enum CustomizationType {
65 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070066 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070067 WallpaperCustomization,
68 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070069 }
70
71 private static final String TAG = "CustomizeWorkspace";
72 private static final boolean DEBUG = false;
73
Michael Jurka7426c422010-11-11 15:23:47 -080074 private View mLastTouchedItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070075 private Launcher mLauncher;
76 private DragController mDragController;
77 private PackageManager mPackageManager;
Michael Jurka7426c422010-11-11 15:23:47 -080078 private boolean mIsDragging;
79 private float mDragSlopeThreshold;
Winson Chung80baf5a2010-08-09 16:03:15 -070080
81 private CustomizationType mCustomizationType;
82
Winson Chunge3193b92010-09-10 11:44:42 -070083 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
84 private PagedViewCellLayout mWorkspaceWidgetLayout;
85
86 // The mapping between the pages and the widgets that will be laid out on them
87 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
88
Winson Chung45e1d6e2010-11-09 17:19:49 -080089 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070090 private int mMaxWidgetWidth;
91
Winson Chung45e1d6e2010-11-09 17:19:49 -080092 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -070093 private int mMaxWidgetsCellHSpan;
94
Winson Chung45e1d6e2010-11-09 17:19:49 -080095 // The size of the items on the wallpaper tab
96 private int mWallpaperCellHSpan;
97
Winson Chunge3193b92010-09-10 11:44:42 -070098 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -070099 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700100 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700101 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700102 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700103
Winson Chunge3193b92010-09-10 11:44:42 -0700104 private static final int sMinWidgetCellHSpan = 2;
105 private static final int sMaxWidgetCellHSpan = 4;
106
Michael Jurka3125d9d2010-09-27 11:30:20 -0700107 private int mChoiceModeTitleText;
108
Winson Chunge3193b92010-09-10 11:44:42 -0700109 // The scale factor for widget previews inside the widget drawer
110 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700111
112 private final Canvas mCanvas = new Canvas();
113 private final LayoutInflater mInflater;
114
115 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700116 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700117 }
118
119 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700120 this(context, attrs, 0);
121 }
122
123 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
124 super(context, attrs, defStyle);
125
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700126 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800127 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
128 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700129 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
130 a.recycle();
131 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
132 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
133 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700134 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800135
Winson Chung80baf5a2010-08-09 16:03:15 -0700136 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700137 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
138 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700139 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700140
Michael Jurka7426c422010-11-11 15:23:47 -0800141 final Resources r = context.getResources();
142 mDragSlopeThreshold =
143 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f;
144
Winson Chung80baf5a2010-08-09 16:03:15 -0700145 setVisibility(View.GONE);
146 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700147 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700148 }
149
Winson Chung7da10252010-10-28 16:07:04 -0700150 @Override
151 protected void init() {
152 super.init();
153 mCenterPagesVertically = false;
154 }
155
Winson Chung80baf5a2010-08-09 16:03:15 -0700156 public void setLauncher(Launcher launcher) {
157 Context context = getContext();
158 mLauncher = launcher;
159 mPackageManager = context.getPackageManager();
160 }
161
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700162 /**
163 * Sets the list of applications that launcher has loaded.
164 */
165 public void setApps(ArrayList<ApplicationInfo> list) {
166 mApps = list;
167 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700168
169 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700170 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700171 }
172
173 /**
174 * Convenience function to add new items to the set of applications that were previously loaded.
175 * Called by both updateApps() and setApps().
176 */
177 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
178 // we add it in place, in alphabetical order
179 final int count = list.size();
180 for (int i = 0; i < count; ++i) {
181 final ApplicationInfo info = list.get(i);
182 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
183 if (index < 0) {
184 mApps.add(-(index + 1), info);
185 }
186 }
187 }
188
189 /**
190 * Adds new applications to the loaded list, and notifies the paged view to update itself.
191 */
192 public void addApps(ArrayList<ApplicationInfo> list) {
193 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700194
195 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700196 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700197 }
198
199 /**
200 * Convenience function to remove items to the set of applications that were previously loaded.
201 * Called by both updateApps() and removeApps().
202 */
203 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
204 // loop through all the apps and remove apps that have the same component
205 final int length = list.size();
206 for (int i = 0; i < length; ++i) {
207 final ApplicationInfo info = list.get(i);
208 int removeIndex = findAppByComponent(mApps, info);
209 if (removeIndex > -1) {
210 mApps.remove(removeIndex);
211 mPageViewIconCache.removeOutline(info);
212 }
213 }
214 }
215
216 /**
217 * Removes applications from the loaded list, and notifies the paged view to update itself.
218 */
219 public void removeApps(ArrayList<ApplicationInfo> list) {
220 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700221
222 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700223 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700224 }
225
226 /**
227 * Updates a set of applications from the loaded list, and notifies the paged view to update
228 * itself.
229 */
230 public void updateApps(ArrayList<ApplicationInfo> list) {
231 // We remove and re-add the updated applications list because it's properties may have
232 // changed (ie. the title), and this will ensure that the items will be in their proper
233 // place in the list.
234 removeAppsWithoutInvalidate(list);
235 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700236
237 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700238 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700239 }
240
241 /**
242 * Convenience function to find matching ApplicationInfos for removal.
243 */
244 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
245 ComponentName removeComponent = item.intent.getComponent();
246 final int length = list.size();
247 for (int i = 0; i < length; ++i) {
248 ApplicationInfo info = list.get(i);
249 if (info.intent.getComponent().equals(removeComponent)) {
250 return i;
251 }
252 }
253 return -1;
254 }
255
Winson Chung80baf5a2010-08-09 16:03:15 -0700256 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700257 // get the list of widgets
258 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
259 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
260 @Override
261 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
262 return object1.label.compareTo(object2.label);
263 }
264 });
265
266 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
267 @Override
268 public int compare(ResolveInfo object1, ResolveInfo object2) {
269 return object1.loadLabel(mPackageManager).toString().compareTo(
270 object2.loadLabel(mPackageManager).toString());
271 }
272 };
273
Winson Chung80baf5a2010-08-09 16:03:15 -0700274 // get the list of shortcuts
275 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
276 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
277 Collections.sort(mShortcutList, resolveInfoComparator);
278
Winson Chunge8878e32010-09-15 20:37:09 -0700279 // get the list of wallpapers
280 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800281 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent,
282 PackageManager.GET_META_DATA);
Winson Chunge8878e32010-09-15 20:37:09 -0700283 Collections.sort(mWallpaperList, resolveInfoComparator);
284
Winson Chung10fefb12010-11-01 11:57:06 -0700285 invalidatePageDataAndIconCache();
286 }
287
288 private void invalidatePageDataAndIconCache() {
289 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700290 mPageViewIconCache.clear();
291
Winson Chunge3193b92010-09-10 11:44:42 -0700292 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700293 invalidatePageData();
294 }
295
296 public void setDragController(DragController dragger) {
297 mDragController = dragger;
298 }
299
300 public void setCustomizationFilter(CustomizationType filterType) {
301 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700302 setCurrentPage(0);
Winson Chungbbc60d82010-11-11 16:34:41 -0800303 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700304 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700305
306 // End the current choice mode so that we don't carry selections across tabs
307 endChoiceMode();
Winson Chung649a4ca2010-11-18 10:38:13 -0800308 // Reset the touch item (if we are mid-dragging)
309 mLastTouchedItem = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700310 }
311
312 @Override
313 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700314 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700315 }
316
317 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800318 public void onDragViewVisible() {
319 }
320
321 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700322 public void onClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800323 // Return early if this is not initiated from a touch
324 if (!v.isInTouchMode()) return;
325 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800326 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700327
Winson Chungd0d43012010-09-26 17:26:45 -0700328 // On certain pages, we allow single tap to mark items as selected so that they can be
329 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700330 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700331 switch (mCustomizationType) {
332 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700333 mChoiceModeTitleText = R.string.cab_widget_selection_text;
334 enterChoiceMode = true;
335 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700336 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700337 mChoiceModeTitleText = R.string.cab_app_selection_text;
338 enterChoiceMode = true;
339 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700340 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700341 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
342 enterChoiceMode = true;
343 break;
344 default:
345 break;
346 }
Winson Chungd0d43012010-09-26 17:26:45 -0700347
Michael Jurka3125d9d2010-09-27 11:30:20 -0700348 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700349 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700350
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700351 Workspace w = mLauncher.getWorkspace();
352 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
353 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700354
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700355 animateClickFeedback(v, new Runnable() {
356 @Override
357 public void run() {
358 mLauncher.addExternalItemToScreen(itemInfo, cl);
359 }
360 });
Winson Chungd0d43012010-09-26 17:26:45 -0700361 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700362 }
363
364 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700365 switch (mCustomizationType) {
366 case WallpaperCustomization:
367 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700368 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700369 animateClickFeedback(v, new Runnable() {
370 @Override
371 public void run() {
372 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700373 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700374 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
375 ComponentName name = new ComponentName(info.activityInfo.packageName,
376 info.activityInfo.name);
377 createWallpapersIntent.setComponent(name);
378 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700379 }
380 });
Winson Chungd0d43012010-09-26 17:26:45 -0700381 break;
382 default:
383 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700384 }
385 }
386
387 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700388 public boolean onLongClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800389 // Return early if this is not initiated from a touch
390 if (!v.isInTouchMode()) return false;
391 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800392 if (mNextPage != INVALID_PAGE) return false;
Michael Jurka7426c422010-11-11 15:23:47 -0800393 return beginDragging(v);
394 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700395
Michael Jurka7426c422010-11-11 15:23:47 -0800396 @Override
397 public boolean onTouch(View v, MotionEvent event) {
398 mLastTouchedItem = v;
399 return false;
400 }
401
402 /*
403 * Determines if we should change the touch state to start scrolling after the
404 * user moves their touch point too far.
405 */
406 protected void determineScrollingStart(MotionEvent ev) {
407 if (!mIsDragging) super.determineScrollingStart(ev);
408 }
409
410 /*
411 * Determines if we should change the touch state to start dragging after the
412 * user moves their touch point far enough.
413 */
414 protected void determineDraggingStart(MotionEvent ev) {
415 /*
416 * Locally do absolute value. mLastMotionX is set to the y value
417 * of the down event.
418 */
419 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
420 final float x = ev.getX(pointerIndex);
421 final float y = ev.getY(pointerIndex);
422 final int xDiff = (int) Math.abs(x - mLastMotionX);
423 final int yDiff = (int) Math.abs(y - mLastMotionY);
424
425 final int touchSlop = mTouchSlop;
426 boolean yMoved = yDiff > touchSlop;
427 boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
428
Winson Chung649a4ca2010-11-18 10:38:13 -0800429 if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
Michael Jurka7426c422010-11-11 15:23:47 -0800430 // Drag if the user moved far enough along the Y axis
431 beginDragging(mLastTouchedItem);
432
433 // Cancel any pending longpress
434 if (mAllowLongPress) {
435 mAllowLongPress = false;
436 // Try canceling the long press. It could also have been scheduled
437 // by a distant descendant, so use the mAllowLongPress flag to block
438 // everything
439 final View currentPage = getPageAt(mCurrentPage);
440 if (currentPage != null) {
441 currentPage.cancelLongPress();
442 }
443 }
444 }
445 }
446
447 @Override
448 public boolean onInterceptTouchEvent(MotionEvent ev) {
449 final int action = ev.getAction();
450 switch (action & MotionEvent.ACTION_MASK) {
451 case MotionEvent.ACTION_DOWN:
452 mIsDragging = false;
453 break;
454 case MotionEvent.ACTION_MOVE:
455 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
456 determineDraggingStart(ev);
457 }
458 break;
459 }
460 return super.onInterceptTouchEvent(ev);
461 }
462
463 @Override
464 public boolean onTouchEvent(MotionEvent ev) {
465 final int action = ev.getAction();
466 switch (action & MotionEvent.ACTION_MASK) {
467 case MotionEvent.ACTION_DOWN:
468 mIsDragging = false;
469 break;
470 case MotionEvent.ACTION_MOVE:
471 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
472 determineDraggingStart(ev);
473 }
474 break;
475 }
476 return super.onTouchEvent(ev);
477 }
478
Michael Jurkaaf91de02010-11-23 16:23:58 -0800479 Bitmap drawableToBitmap(Drawable d) {
480 Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(),
481 Bitmap.Config.ARGB_8888);
482 Canvas c = new Canvas(b);
483 d.draw(c);
484 return b;
485 }
486
Michael Jurka7426c422010-11-11 15:23:47 -0800487 private boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700488 // End the current choice mode before we start dragging anything
489 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
490 endChoiceMode();
491 }
Michael Jurka7426c422010-11-11 15:23:47 -0800492 mIsDragging = true;
Winson Chungd0d43012010-09-26 17:26:45 -0700493
494 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700495 switch (mCustomizationType) {
496 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700497 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700498 final LinearLayout l = (LinearLayout) v;
499 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Michael Jurkaaf91de02010-11-23 16:23:58 -0800500 Bitmap b = drawableToBitmap(icon);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700501 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700502
Michael Jurka3e7c7632010-10-02 16:01:03 -0700503 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
504 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
Patrick Dubroya669d792010-11-23 14:40:33 -0800505 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chunge3193b92010-09-10 11:44:42 -0700506
507 // Cleanup the icon
508 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700509 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700510 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700511 createItemInfo = (PendingAddItemInfo) v.getTag();
Patrick Dubroya669d792010-11-23 14:40:33 -0800512 mDragController.startDrag(v, this, createItemInfo, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700513 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700514 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700515 case ApplicationCustomization:
516 // Pick up the application for dropping
517 ApplicationInfo app = (ApplicationInfo) v.getTag();
518 app = new ApplicationInfo(app);
519
520 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700521 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700522 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700523 }
524 return false;
525 }
526
Winson Chunge3193b92010-09-10 11:44:42 -0700527 /**
528 * Pre-processes the layout of the different widget pages.
529 * @return the number of pages of widgets that we have
530 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700531 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700532 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700533
Winson Chunge3193b92010-09-10 11:44:42 -0700534 // create a new page for the first set of widgets
535 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700536 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700537 mWidgetPages.add(newPage);
538
539 // do this until we have no more widgets to lay out
540 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
541 final int widgetCount = mWidgetList.size();
542 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700543 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700544 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700545
Winson Chunge3193b92010-09-10 11:44:42 -0700546 // determine the size of the current widget
547 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
548 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700549
Winson Chunge3193b92010-09-10 11:44:42 -0700550 // create a new page if necessary
551 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
552 numCellsInRow = 0;
553 newPage = new ArrayList<AppWidgetProviderInfo>();
554 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700555 }
556
Winson Chunge3193b92010-09-10 11:44:42 -0700557 // add the item to the current page
558 newPage.add(info);
559 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700560 }
Winson Chunge3193b92010-09-10 11:44:42 -0700561
Winson Chung80baf5a2010-08-09 16:03:15 -0700562 return mWidgetPages.size();
563 }
564
Winson Chunge3193b92010-09-10 11:44:42 -0700565 /**
Winson Chung7da10252010-10-28 16:07:04 -0700566 * Helper function to draw a drawable to the specified canvas with the specified bounds.
567 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800568 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700569 if (bitmap != null) mCanvas.setBitmap(bitmap);
570 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800571 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700572 d.draw(mCanvas);
573 mCanvas.restore();
574 }
575
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800576 /*
577 * This method fetches an xml file specified in the manifest identified by
578 * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies
579 * an image which will be used as the wallpaper preview for an activity
580 * which responds to ACTION_SET_WALLPAPER. This image is returned and used
581 * in the customize drawer.
582 */
583 private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) {
584 Drawable d = null;
585
586 ActivityInfo activityInfo = ri.activityInfo;
587 XmlResourceParser parser = null;
588 try {
589 parser = activityInfo.loadXmlMetaData(mPackageManager,
590 WallpaperManager.WALLPAPER_PREVIEW_META_DATA);
591 if (parser == null) {
592 Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for "
593 + "wallpaper provider '" + component + '\'');
594 return null;
595 }
596
597 AttributeSet attrs = Xml.asAttributeSet(parser);
598
599 int type;
600 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
601 && type != XmlPullParser.START_TAG) {
602 // drain whitespace, comments, etc.
603 }
604
605 String nodeName = parser.getName();
606 if (!"wallpaper-preview".equals(nodeName)) {
607 Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for "
608 + "wallpaper provider '" + component + '\'');
609 return null;
610 }
611
612 // If metaData was null, we would have returned earlier when getting
613 // the parser No need to do the check here
614 Resources res = mPackageManager.getResourcesForApplication(
615 activityInfo.applicationInfo);
616
617 TypedArray sa = res.obtainAttributes(attrs,
618 com.android.internal.R.styleable.WallpaperPreviewInfo);
619
620 TypedValue value = sa.peekValue(
621 com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview);
622 if (value == null) return null;
623
624 return res.getDrawable(value.resourceId);
625 } catch (Exception e) {
626 Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e);
627 return null;
628 } finally {
629 if (parser != null) parser.close();
630 }
631 }
632
Winson Chung7da10252010-10-28 16:07:04 -0700633 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800634 * This method will extract the preview image specified by the wallpaper source provider (if it
635 * exists) otherwise, it will try to generate a default image preview.
636 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800637 private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800638 // To be implemented later: resolving the up-to-date wallpaper thumbnail
639
640 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
641 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
642 Resources resources = mLauncher.getResources();
643
644 // Create a new bitmap to hold the widget preview
645 int width = (int) (dim * sScaleFactor);
646 int height = (int) (dim * sScaleFactor);
647 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800648
649 Drawable background = parseWallpaperPreviewXml(
650 new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info);
651 boolean foundCustomDrawable = background != null;
652
653 if (!foundCustomDrawable) {
654 background = resources.getDrawable(R.drawable.default_widget_preview);
655 }
656
Winson Chung45e1d6e2010-11-09 17:19:49 -0800657 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
658
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800659 // If we don't have a custom icon, we use the app icon on the default background
660 if (!foundCustomDrawable) {
661 try {
662 final IconCache iconCache =
663 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
664 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
665 iconCache.getFullResIcon(info, mPackageManager), mContext));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800666
Adam Cohen7b9d3a62010-12-07 21:49:34 -0800667 final int iconSize = minDim / 2;
668 final int offset = iconSize / 4;
669 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
670 } catch (Resources.NotFoundException e) {
671 // if we can't find the icon, then just don't draw it
672 }
Winson Chung45e1d6e2010-11-09 17:19:49 -0800673 }
674
Winson Chung29d6fea2010-12-01 15:47:31 -0800675 FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800676 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
677 return drawable;
678 }
679
680 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700681 * This method will extract the preview image specified by the widget developer (if it exists),
682 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800683 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700684 */
Winson Chung29d6fea2010-12-01 15:47:31 -0800685 private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800686 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700687 String packageName = info.provider.getPackageName();
688 Drawable drawable = null;
Winson Chung29d6fea2010-12-01 15:47:31 -0800689 FastBitmapDrawable newDrawable = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700690 if (info.previewImage != 0) {
691 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
692 if (drawable == null) {
693 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
694 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700695 }
696 }
697
698 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700699 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
700 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700701 if (drawable == null) {
702 Resources resources = mLauncher.getResources();
703
Winson Chung80baf5a2010-08-09 16:03:15 -0700704 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700705 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
706 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700707 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
708 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
709 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700710
Winson Chung45e1d6e2010-11-09 17:19:49 -0800711 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700712 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700713 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700714 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700715 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700716 }
717 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700718 icon = resources.getDrawable(R.drawable.ic_launcher_application);
719 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700720
Winson Chunge3193b92010-09-10 11:44:42 -0700721 final int iconSize = minDim / 2;
722 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800723 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700724 } catch (Resources.NotFoundException e) {
725 // if we can't find the icon, then just don't draw it
726 }
727
Winson Chung29d6fea2010-12-01 15:47:31 -0800728 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700729 } else {
730 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800731 final float imageWidth = drawable.getIntrinsicWidth();
732 final float imageHeight = drawable.getIntrinsicHeight();
733 final float aspect = (float) imageWidth / imageHeight;
734 final int scaledWidth =
735 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
736 final int scaledHeight =
737 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700738 int width;
739 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800740 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700741 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800742 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700743 } else {
744 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800745 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700746 }
747
748 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
749 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
750
Winson Chung29d6fea2010-12-01 15:47:31 -0800751 newDrawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700752 }
Winson Chung29d6fea2010-12-01 15:47:31 -0800753 newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(),
754 newDrawable.getIntrinsicHeight());
755 return newDrawable;
Winson Chung80baf5a2010-08-09 16:03:15 -0700756 }
757
758 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700759 layout.setCellCount(mCellCountX, mCellCountY);
760 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
761 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700762 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700763 }
764
Winson Chunge3193b92010-09-10 11:44:42 -0700765 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700766 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700767 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
768
769 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
770 }
771
Winson Chung80baf5a2010-08-09 16:03:15 -0700772 private void syncWidgetPages() {
773 if (mWidgetList == null) return;
774
Winson Chunge3193b92010-09-10 11:44:42 -0700775 // we need to repopulate with the LinearLayout layout for the widget pages
776 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700777 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700778 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800779 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700780 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700781 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
782 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700783
Winson Chunge22a8e92010-11-12 13:40:58 -0800784 addView(layout, new LinearLayout.LayoutParams(
785 LinearLayout.LayoutParams.WRAP_CONTENT,
786 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700787 }
788 }
789
790 private void syncWidgetPageItems(int page) {
791 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700792 LinearLayout layout = (LinearLayout) getChildAt(page);
793 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700794 final int count = list.size();
795 layout.removeAllViews();
796 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700797 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
798 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chung29d6fea2010-12-01 15:47:31 -0800799 final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth,
800 info.minHeight, null);
801 final FastBitmapDrawable icon = getWidgetPreview(info);
Winson Chungd0d43012010-09-26 17:26:45 -0700802
Winson Chung29d6fea2010-12-01 15:47:31 -0800803 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chunge3193b92010-09-10 11:44:42 -0700804 R.layout.customize_paged_view_widget, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800805 l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans);
Winson Chungd0d43012010-09-26 17:26:45 -0700806 l.setTag(createItemInfo);
807 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800808 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700809 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700810
Winson Chunge3193b92010-09-10 11:44:42 -0700811 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700812 }
813 }
814
Winson Chung45e1d6e2010-11-09 17:19:49 -0800815 private void syncWallpaperPages() {
816 if (mWallpaperList == null) return;
817
818 // We need to repopulate the LinearLayout for the wallpaper pages
819 removeAllViews();
820 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
821 mMaxWidgetsCellHSpan);
822 for (int i = 0; i < numPages; ++i) {
823 LinearLayout layout = new PagedViewExtendedLayout(getContext());
824 layout.setGravity(Gravity.CENTER_HORIZONTAL);
825 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
826 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
827
Winson Chunge22a8e92010-11-12 13:40:58 -0800828 addView(layout, new LinearLayout.LayoutParams(
829 LinearLayout.LayoutParams.WRAP_CONTENT,
830 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800831 }
832 }
833
834 private void syncWallpaperPageItems(int page) {
835 // Load the items on to the pages
836 LinearLayout layout = (LinearLayout) getChildAt(page);
837 layout.removeAllViews();
838 final int count = mWallpaperList.size();
Winson Chungd28ed492010-11-22 14:34:57 -0800839 final int numItemsPerPage = mMaxWidgetsCellHSpan / mWallpaperCellHSpan;
840 final int startIndex = page * numItemsPerPage;
841 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
842 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800843 final ResolveInfo info = mWallpaperList.get(i);
Winson Chung29d6fea2010-12-01 15:47:31 -0800844 final FastBitmapDrawable icon = getWallpaperPreview(info);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800845
Winson Chung29d6fea2010-12-01 15:47:31 -0800846 PagedViewWidget l = (PagedViewWidget) mInflater.inflate(
Winson Chung45e1d6e2010-11-09 17:19:49 -0800847 R.layout.customize_paged_view_wallpaper, layout, false);
Winson Chung29d6fea2010-12-01 15:47:31 -0800848 l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800849 l.setTag(info);
850 l.setOnClickListener(this);
851
Winson Chung45e1d6e2010-11-09 17:19:49 -0800852 layout.addView(l);
853 }
854 }
855
Winson Chung80baf5a2010-08-09 16:03:15 -0700856 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700857 // we need to repopulate with PagedViewCellLayouts
858 removeAllViews();
859
Winson Chung80baf5a2010-08-09 16:03:15 -0700860 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700861 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700862 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700863 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
864 setupPage(layout);
865 addView(layout);
866 }
867 }
868
869 private void syncListPageItems(int page, List<ResolveInfo> list) {
870 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700871 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700872 int startIndex = page * numCells;
873 int endIndex = Math.min(startIndex + numCells, list.size());
874 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
875 // TODO: we can optimize by just re-applying to existing views
876 layout.removeAllViews();
877 for (int i = startIndex; i < endIndex; ++i) {
878 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700879 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
880
Winson Chung241c3b42010-08-25 16:53:03 -0700881 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
882 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700883 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
884 ((LauncherApplication)mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700885 switch (mCustomizationType) {
886 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700887 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700888 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700889 case ShortcutCustomization:
890 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
891 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
892 info.activityInfo.name);
893 icon.setTag(createItemInfo);
894 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800895 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700896 icon.setOnLongClickListener(this);
897 break;
898 default:
899 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700900 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700901
902 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700903 final int x = index % mCellCountX;
904 final int y = index / mCellCountX;
905 setupPage(layout);
906 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
907 }
908 }
909
910 private void syncAppPages() {
911 if (mApps == null) return;
912
913 // We need to repopulate with PagedViewCellLayouts
914 removeAllViews();
915
916 // Ensure that we have the right number of pages
917 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
918 for (int i = 0; i < numPages; ++i) {
919 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
920 setupPage(layout);
921 addView(layout);
922 }
923 }
924
925 private void syncAppPageItems(int page) {
926 if (mApps == null) return;
927
928 // ensure that we have the right number of items on the pages
929 int numCells = mCellCountX * mCellCountY;
930 int startIndex = page * numCells;
931 int endIndex = Math.min(startIndex + numCells, mApps.size());
932 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
933 // TODO: we can optimize by just re-applying to existing views
934 layout.removeAllViews();
935 for (int i = startIndex; i < endIndex; ++i) {
936 final ApplicationInfo info = mApps.get(i);
937 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
938 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700939 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700940 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800941 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700942 icon.setOnLongClickListener(this);
943
944 final int index = i - startIndex;
945 final int x = index % mCellCountX;
946 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700947 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700948 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700949 }
950 }
951
Winson Chung80baf5a2010-08-09 16:03:15 -0700952 @Override
953 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700954 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700955 switch (mCustomizationType) {
956 case WidgetCustomization:
957 syncWidgetPages();
958 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700959 case ShortcutCustomization:
960 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700961 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700962 break;
963 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800964 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -0700965 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700966 case ApplicationCustomization:
967 syncAppPages();
968 centerPagedViewCellLayouts = false;
969 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700970 default:
971 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700972 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700973 break;
974 }
975
976 // only try and center the page if there is one page
977 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700978 if (centerPagedViewCellLayouts) {
979 if (childCount == 1) {
980 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
981 layout.enableCenteredContent(true);
982 } else {
983 for (int i = 0; i < childCount; ++i) {
984 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
985 layout.enableCenteredContent(false);
986 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700987 }
988 }
989
990 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700991 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700992 }
993
994 @Override
995 public void syncPageItems(int page) {
996 switch (mCustomizationType) {
997 case WidgetCustomization:
998 syncWidgetPageItems(page);
999 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001000 case ShortcutCustomization:
1001 syncListPageItems(page, mShortcutList);
1002 break;
1003 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -08001004 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -07001005 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -07001006 case ApplicationCustomization:
1007 syncAppPageItems(page);
1008 break;
Winson Chung80baf5a2010-08-09 16:03:15 -07001009 }
1010 }
Winson Chunge3193b92010-09-10 11:44:42 -07001011
Winson Chungd0d43012010-09-26 17:26:45 -07001012 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001013 protected int getAssociatedLowerPageBound(int page) {
1014 return 0;
1015 }
Winson Chungd0d43012010-09-26 17:26:45 -07001016 @Override
Winson Chunge3193b92010-09-10 11:44:42 -07001017 protected int getAssociatedUpperPageBound(int page) {
1018 return getChildCount();
1019 }
Winson Chungd0d43012010-09-26 17:26:45 -07001020
1021 @Override
1022 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -07001023 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -07001024 return true;
1025 }
1026
1027 @Override
1028 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
1029 return true;
1030 }
1031
1032 @Override
1033 public void onDestroyActionMode(ActionMode mode) {
1034 endChoiceMode();
1035 }
1036
1037 @Override
1038 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
1039 return false;
1040 }
Winson Chung80baf5a2010-08-09 16:03:15 -07001041}