blob: b48d4ab492a1bd5d311515c715bbf6b31e11bd95 [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
Winson Chung5974adc2010-10-25 14:29:11 -070019import java.util.ArrayList;
20import java.util.Collections;
21import java.util.Comparator;
22import java.util.List;
Winson Chung80baf5a2010-08-09 16:03:15 -070023
24import android.appwidget.AppWidgetManager;
25import android.appwidget.AppWidgetProviderInfo;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
29import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
31import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070032import android.content.res.TypedArray;
Winson Chung80baf5a2010-08-09 16:03:15 -070033import android.graphics.Bitmap;
Winson Chung5974adc2010-10-25 14:29:11 -070034import android.graphics.Bitmap.Config;
Winson Chung86f77532010-08-24 11:08:22 -070035import android.graphics.Canvas;
Winson Chung7da10252010-10-28 16:07:04 -070036import android.graphics.Color;
Winson Chung86f77532010-08-24 11:08:22 -070037import android.graphics.Rect;
Winson Chung80baf5a2010-08-09 16:03:15 -070038import android.graphics.Region.Op;
Winson Chung80baf5a2010-08-09 16:03:15 -070039import android.graphics.drawable.Drawable;
Winson Chung80baf5a2010-08-09 16:03:15 -070040import android.util.AttributeSet;
41import android.util.Log;
Winson Chungd0d43012010-09-26 17:26:45 -070042import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070043import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070045import android.view.Menu;
46import android.view.MenuItem;
Michael Jurka7426c422010-11-11 15:23:47 -080047import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070048import android.view.View;
Winson Chunge3193b92010-09-10 11:44:42 -070049import android.widget.ImageView;
50import android.widget.LinearLayout;
Winson Chung80baf5a2010-08-09 16:03:15 -070051import android.widget.TextView;
52
Winson Chung5974adc2010-10-25 14:29:11 -070053import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070054
55public class CustomizePagedView extends PagedView
Michael Jurka7426c422010-11-11 15:23:47 -080056 implements View.OnLongClickListener, View.OnClickListener, View.OnTouchListener,
Winson Chungd0d43012010-09-26 17:26:45 -070057 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070058
59 public enum CustomizationType {
60 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070061 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070062 WallpaperCustomization,
63 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070064 }
65
66 private static final String TAG = "CustomizeWorkspace";
67 private static final boolean DEBUG = false;
68
Michael Jurka7426c422010-11-11 15:23:47 -080069 private View mLastTouchedItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070070 private Launcher mLauncher;
71 private DragController mDragController;
72 private PackageManager mPackageManager;
Michael Jurka7426c422010-11-11 15:23:47 -080073 private boolean mIsDragging;
74 private float mDragSlopeThreshold;
Winson Chung80baf5a2010-08-09 16:03:15 -070075
76 private CustomizationType mCustomizationType;
77
Winson Chunge3193b92010-09-10 11:44:42 -070078 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
79 private PagedViewCellLayout mWorkspaceWidgetLayout;
80
81 // The mapping between the pages and the widgets that will be laid out on them
82 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
83
Winson Chung45e1d6e2010-11-09 17:19:49 -080084 // The max dimensions for the ImageView we use for displaying a widget
Winson Chunge3193b92010-09-10 11:44:42 -070085 private int mMaxWidgetWidth;
86
Winson Chung45e1d6e2010-11-09 17:19:49 -080087 // The max number of widget cells to take a "page" of widgets
Winson Chunge3193b92010-09-10 11:44:42 -070088 private int mMaxWidgetsCellHSpan;
89
Winson Chung45e1d6e2010-11-09 17:19:49 -080090 // The size of the items on the wallpaper tab
91 private int mWallpaperCellHSpan;
92
93 // The max dimensions for the ImageView we use for displaying a wallpaper
94 private int mMaxWallpaperWidth;
95
Winson Chunge3193b92010-09-10 11:44:42 -070096 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -070097 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -070098 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -070099 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700100 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -0700101
Winson Chunge3193b92010-09-10 11:44:42 -0700102 private static final int sMinWidgetCellHSpan = 2;
103 private static final int sMaxWidgetCellHSpan = 4;
104
Michael Jurka3125d9d2010-09-27 11:30:20 -0700105 private int mChoiceModeTitleText;
106
Winson Chunge3193b92010-09-10 11:44:42 -0700107 // The scale factor for widget previews inside the widget drawer
108 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700109
110 private final Canvas mCanvas = new Canvas();
111 private final LayoutInflater mInflater;
112
113 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700114 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700115 }
116
117 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700118 this(context, attrs, 0);
119 }
120
121 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
122 super(context, attrs, defStyle);
123
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700124 TypedArray a;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800125 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0);
126 mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700127 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
128 a.recycle();
129 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
130 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
131 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700132 a.recycle();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800133
Winson Chung80baf5a2010-08-09 16:03:15 -0700134 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700135 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
136 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700137 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700138
Michael Jurka7426c422010-11-11 15:23:47 -0800139 final Resources r = context.getResources();
140 mDragSlopeThreshold =
141 r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f;
142
Winson Chung80baf5a2010-08-09 16:03:15 -0700143 setVisibility(View.GONE);
144 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700145 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700146 }
147
Winson Chung7da10252010-10-28 16:07:04 -0700148 @Override
149 protected void init() {
150 super.init();
151 mCenterPagesVertically = false;
152 }
153
Winson Chung80baf5a2010-08-09 16:03:15 -0700154 public void setLauncher(Launcher launcher) {
155 Context context = getContext();
156 mLauncher = launcher;
157 mPackageManager = context.getPackageManager();
158 }
159
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700160 /**
161 * Sets the list of applications that launcher has loaded.
162 */
163 public void setApps(ArrayList<ApplicationInfo> list) {
164 mApps = list;
165 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700166
167 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700168 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700169 }
170
171 /**
172 * Convenience function to add new items to the set of applications that were previously loaded.
173 * Called by both updateApps() and setApps().
174 */
175 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
176 // we add it in place, in alphabetical order
177 final int count = list.size();
178 for (int i = 0; i < count; ++i) {
179 final ApplicationInfo info = list.get(i);
180 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
181 if (index < 0) {
182 mApps.add(-(index + 1), info);
183 }
184 }
185 }
186
187 /**
188 * Adds new applications to the loaded list, and notifies the paged view to update itself.
189 */
190 public void addApps(ArrayList<ApplicationInfo> list) {
191 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700192
193 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700194 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700195 }
196
197 /**
198 * Convenience function to remove items to the set of applications that were previously loaded.
199 * Called by both updateApps() and removeApps().
200 */
201 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
202 // loop through all the apps and remove apps that have the same component
203 final int length = list.size();
204 for (int i = 0; i < length; ++i) {
205 final ApplicationInfo info = list.get(i);
206 int removeIndex = findAppByComponent(mApps, info);
207 if (removeIndex > -1) {
208 mApps.remove(removeIndex);
209 mPageViewIconCache.removeOutline(info);
210 }
211 }
212 }
213
214 /**
215 * Removes applications from the loaded list, and notifies the paged view to update itself.
216 */
217 public void removeApps(ArrayList<ApplicationInfo> list) {
218 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700219
220 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700221 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700222 }
223
224 /**
225 * Updates a set of applications from the loaded list, and notifies the paged view to update
226 * itself.
227 */
228 public void updateApps(ArrayList<ApplicationInfo> list) {
229 // We remove and re-add the updated applications list because it's properties may have
230 // changed (ie. the title), and this will ensure that the items will be in their proper
231 // place in the list.
232 removeAppsWithoutInvalidate(list);
233 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700234
235 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700236 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700237 }
238
239 /**
240 * Convenience function to find matching ApplicationInfos for removal.
241 */
242 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
243 ComponentName removeComponent = item.intent.getComponent();
244 final int length = list.size();
245 for (int i = 0; i < length; ++i) {
246 ApplicationInfo info = list.get(i);
247 if (info.intent.getComponent().equals(removeComponent)) {
248 return i;
249 }
250 }
251 return -1;
252 }
253
Winson Chung80baf5a2010-08-09 16:03:15 -0700254 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700255 // get the list of widgets
256 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
257 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
258 @Override
259 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
260 return object1.label.compareTo(object2.label);
261 }
262 });
263
264 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
265 @Override
266 public int compare(ResolveInfo object1, ResolveInfo object2) {
267 return object1.loadLabel(mPackageManager).toString().compareTo(
268 object2.loadLabel(mPackageManager).toString());
269 }
270 };
271
Winson Chung80baf5a2010-08-09 16:03:15 -0700272 // get the list of shortcuts
273 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
274 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
275 Collections.sort(mShortcutList, resolveInfoComparator);
276
Winson Chunge8878e32010-09-15 20:37:09 -0700277 // get the list of wallpapers
278 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
279 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
280 Collections.sort(mWallpaperList, resolveInfoComparator);
281
Winson Chung10fefb12010-11-01 11:57:06 -0700282 invalidatePageDataAndIconCache();
283 }
284
285 private void invalidatePageDataAndIconCache() {
286 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700287 mPageViewIconCache.clear();
288
Winson Chunge3193b92010-09-10 11:44:42 -0700289 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700290 invalidatePageData();
291 }
292
293 public void setDragController(DragController dragger) {
294 mDragController = dragger;
295 }
296
297 public void setCustomizationFilter(CustomizationType filterType) {
298 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700299 setCurrentPage(0);
Winson Chungbbc60d82010-11-11 16:34:41 -0800300 updateCurrentPageScroll();
Winson Chung80baf5a2010-08-09 16:03:15 -0700301 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700302
303 // End the current choice mode so that we don't carry selections across tabs
304 endChoiceMode();
Winson Chung649a4ca2010-11-18 10:38:13 -0800305 // Reset the touch item (if we are mid-dragging)
306 mLastTouchedItem = null;
Winson Chung80baf5a2010-08-09 16:03:15 -0700307 }
308
309 @Override
310 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700311 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700312 }
313
314 @Override
Patrick Dubroya669d792010-11-23 14:40:33 -0800315 public void onDragViewVisible() {
316 }
317
318 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700319 public void onClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800320 // Return early if this is not initiated from a touch
321 if (!v.isInTouchMode()) return;
322 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800323 if (mNextPage != INVALID_PAGE) return;
Winson Chunge8878e32010-09-15 20:37:09 -0700324
Winson Chungd0d43012010-09-26 17:26:45 -0700325 // On certain pages, we allow single tap to mark items as selected so that they can be
326 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700327 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700328 switch (mCustomizationType) {
329 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700330 mChoiceModeTitleText = R.string.cab_widget_selection_text;
331 enterChoiceMode = true;
332 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700333 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700334 mChoiceModeTitleText = R.string.cab_app_selection_text;
335 enterChoiceMode = true;
336 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700337 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700338 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
339 enterChoiceMode = true;
340 break;
341 default:
342 break;
343 }
Winson Chungd0d43012010-09-26 17:26:45 -0700344
Michael Jurka3125d9d2010-09-27 11:30:20 -0700345 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700346 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700347
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700348 Workspace w = mLauncher.getWorkspace();
349 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
350 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700351
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700352 animateClickFeedback(v, new Runnable() {
353 @Override
354 public void run() {
355 mLauncher.addExternalItemToScreen(itemInfo, cl);
356 }
357 });
Winson Chungd0d43012010-09-26 17:26:45 -0700358 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700359 }
360
361 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700362 switch (mCustomizationType) {
363 case WallpaperCustomization:
364 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700365 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700366 animateClickFeedback(v, new Runnable() {
367 @Override
368 public void run() {
369 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700370 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700371 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
372 ComponentName name = new ComponentName(info.activityInfo.packageName,
373 info.activityInfo.name);
374 createWallpapersIntent.setComponent(name);
375 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700376 }
377 });
Winson Chungd0d43012010-09-26 17:26:45 -0700378 break;
379 default:
380 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700381 }
382 }
383
384 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700385 public boolean onLongClick(View v) {
Winson Chunge22a8e92010-11-12 13:40:58 -0800386 // Return early if this is not initiated from a touch
387 if (!v.isInTouchMode()) return false;
388 // Return early if we are still animating the pages
Winson Chung4f9e1072010-11-15 15:28:24 -0800389 if (mNextPage != INVALID_PAGE) return false;
Michael Jurka7426c422010-11-11 15:23:47 -0800390 return beginDragging(v);
391 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700392
Michael Jurka7426c422010-11-11 15:23:47 -0800393 @Override
394 public boolean onTouch(View v, MotionEvent event) {
395 mLastTouchedItem = v;
396 return false;
397 }
398
399 /*
400 * Determines if we should change the touch state to start scrolling after the
401 * user moves their touch point too far.
402 */
403 protected void determineScrollingStart(MotionEvent ev) {
404 if (!mIsDragging) super.determineScrollingStart(ev);
405 }
406
407 /*
408 * Determines if we should change the touch state to start dragging after the
409 * user moves their touch point far enough.
410 */
411 protected void determineDraggingStart(MotionEvent ev) {
412 /*
413 * Locally do absolute value. mLastMotionX is set to the y value
414 * of the down event.
415 */
416 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
417 final float x = ev.getX(pointerIndex);
418 final float y = ev.getY(pointerIndex);
419 final int xDiff = (int) Math.abs(x - mLastMotionX);
420 final int yDiff = (int) Math.abs(y - mLastMotionY);
421
422 final int touchSlop = mTouchSlop;
423 boolean yMoved = yDiff > touchSlop;
424 boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold;
425
Winson Chung649a4ca2010-11-18 10:38:13 -0800426 if (isUpwardMotion && yMoved && mLastTouchedItem != null) {
Michael Jurka7426c422010-11-11 15:23:47 -0800427 // Drag if the user moved far enough along the Y axis
428 beginDragging(mLastTouchedItem);
429
430 // Cancel any pending longpress
431 if (mAllowLongPress) {
432 mAllowLongPress = false;
433 // Try canceling the long press. It could also have been scheduled
434 // by a distant descendant, so use the mAllowLongPress flag to block
435 // everything
436 final View currentPage = getPageAt(mCurrentPage);
437 if (currentPage != null) {
438 currentPage.cancelLongPress();
439 }
440 }
441 }
442 }
443
444 @Override
445 public boolean onInterceptTouchEvent(MotionEvent ev) {
446 final int action = ev.getAction();
447 switch (action & MotionEvent.ACTION_MASK) {
448 case MotionEvent.ACTION_DOWN:
449 mIsDragging = false;
450 break;
451 case MotionEvent.ACTION_MOVE:
452 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
453 determineDraggingStart(ev);
454 }
455 break;
456 }
457 return super.onInterceptTouchEvent(ev);
458 }
459
460 @Override
461 public boolean onTouchEvent(MotionEvent ev) {
462 final int action = ev.getAction();
463 switch (action & MotionEvent.ACTION_MASK) {
464 case MotionEvent.ACTION_DOWN:
465 mIsDragging = false;
466 break;
467 case MotionEvent.ACTION_MOVE:
468 if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) {
469 determineDraggingStart(ev);
470 }
471 break;
472 }
473 return super.onTouchEvent(ev);
474 }
475
476 private boolean beginDragging(View v) {
Winson Chungd0d43012010-09-26 17:26:45 -0700477 // End the current choice mode before we start dragging anything
478 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
479 endChoiceMode();
480 }
Michael Jurka7426c422010-11-11 15:23:47 -0800481 mIsDragging = true;
Winson Chungd0d43012010-09-26 17:26:45 -0700482
483 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700484 switch (mCustomizationType) {
485 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700486 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700487 final LinearLayout l = (LinearLayout) v;
488 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700489 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
490 Bitmap.Config.ARGB_8888);
491 Canvas c = new Canvas(b);
492 icon.draw(c);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700493 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700494
Michael Jurka3e7c7632010-10-02 16:01:03 -0700495 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
496 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
Patrick Dubroya669d792010-11-23 14:40:33 -0800497 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null);
Winson Chunge3193b92010-09-10 11:44:42 -0700498
499 // Cleanup the icon
500 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700501 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700502 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700503 createItemInfo = (PendingAddItemInfo) v.getTag();
Patrick Dubroya669d792010-11-23 14:40:33 -0800504 mDragController.startDrag(v, this, createItemInfo, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700505 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700506 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700507 case ApplicationCustomization:
508 // Pick up the application for dropping
509 ApplicationInfo app = (ApplicationInfo) v.getTag();
510 app = new ApplicationInfo(app);
511
512 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700513 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700514 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700515 }
516 return false;
517 }
518
Winson Chunge3193b92010-09-10 11:44:42 -0700519 /**
520 * Pre-processes the layout of the different widget pages.
521 * @return the number of pages of widgets that we have
522 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700523 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700524 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700525
Winson Chunge3193b92010-09-10 11:44:42 -0700526 // create a new page for the first set of widgets
527 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700528 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700529 mWidgetPages.add(newPage);
530
531 // do this until we have no more widgets to lay out
532 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
533 final int widgetCount = mWidgetList.size();
534 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700535 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700536 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700537
Winson Chunge3193b92010-09-10 11:44:42 -0700538 // determine the size of the current widget
539 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
540 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700541
Winson Chunge3193b92010-09-10 11:44:42 -0700542 // create a new page if necessary
543 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
544 numCellsInRow = 0;
545 newPage = new ArrayList<AppWidgetProviderInfo>();
546 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700547 }
548
Winson Chunge3193b92010-09-10 11:44:42 -0700549 // add the item to the current page
550 newPage.add(info);
551 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700552 }
Winson Chunge3193b92010-09-10 11:44:42 -0700553
Winson Chung80baf5a2010-08-09 16:03:15 -0700554 return mWidgetPages.size();
555 }
556
Winson Chunge3193b92010-09-10 11:44:42 -0700557 /**
Winson Chung7da10252010-10-28 16:07:04 -0700558 * Helper function to draw a drawable to the specified canvas with the specified bounds.
559 */
Winson Chung45e1d6e2010-11-09 17:19:49 -0800560 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
Winson Chung7da10252010-10-28 16:07:04 -0700561 if (bitmap != null) mCanvas.setBitmap(bitmap);
562 mCanvas.save();
Winson Chung45e1d6e2010-11-09 17:19:49 -0800563 d.setBounds(x, y, x+w, y+h);
Winson Chung7da10252010-10-28 16:07:04 -0700564 d.draw(mCanvas);
565 mCanvas.restore();
566 }
567
568 /**
Winson Chung45e1d6e2010-11-09 17:19:49 -0800569 * This method will extract the preview image specified by the wallpaper source provider (if it
570 * exists) otherwise, it will try to generate a default image preview.
571 */
572 private Drawable getWallpaperPreview(ResolveInfo info) {
573 // To be implemented later: resolving the up-to-date wallpaper thumbnail
574
575 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
576 final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
577 Resources resources = mLauncher.getResources();
578
579 // Create a new bitmap to hold the widget preview
580 int width = (int) (dim * sScaleFactor);
581 int height = (int) (dim * sScaleFactor);
582 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
583 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
584 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
585
586 // Draw the icon flush left
587 try {
588 final IconCache iconCache =
589 ((LauncherApplication) mLauncher.getApplication()).getIconCache();
590 Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap(
591 iconCache.getFullResIcon(info, mPackageManager), mContext));
592
593 final int iconSize = minDim / 2;
594 final int offset = iconSize / 4;
595 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
596 } catch (Resources.NotFoundException e) {
597 // if we can't find the icon, then just don't draw it
598 }
599
600 Drawable drawable = new FastBitmapDrawable(bitmap);
601 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
602 return drawable;
603 }
604
605 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700606 * This method will extract the preview image specified by the widget developer (if it exists),
607 * otherwise, it will try to generate a default image preview with the widget's package icon.
Winson Chung45e1d6e2010-11-09 17:19:49 -0800608 * @return the drawable that will be used and sized in the ImageView to represent the widget
Winson Chunge3193b92010-09-10 11:44:42 -0700609 */
Winson Chung94ba5b12010-11-08 17:17:47 -0800610 private Drawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800611 final PackageManager packageManager = mPackageManager;
Winson Chung80baf5a2010-08-09 16:03:15 -0700612 String packageName = info.provider.getPackageName();
613 Drawable drawable = null;
614 if (info.previewImage != 0) {
615 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
616 if (drawable == null) {
617 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
618 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700619 }
620 }
621
622 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700623 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
624 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700625 if (drawable == null) {
626 Resources resources = mLauncher.getResources();
627
Winson Chung80baf5a2010-08-09 16:03:15 -0700628 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700629 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
630 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700631 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
632 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
633 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700634
Winson Chung45e1d6e2010-11-09 17:19:49 -0800635 // Draw the icon flush left
Winson Chung80baf5a2010-08-09 16:03:15 -0700636 try {
Winson Chung80baf5a2010-08-09 16:03:15 -0700637 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700638 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700639 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700640 }
641 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700642 icon = resources.getDrawable(R.drawable.ic_launcher_application);
643 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700644
Winson Chunge3193b92010-09-10 11:44:42 -0700645 final int iconSize = minDim / 2;
646 final int offset = iconSize / 4;
Winson Chung45e1d6e2010-11-09 17:19:49 -0800647 renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700648 } catch (Resources.NotFoundException e) {
649 // if we can't find the icon, then just don't draw it
650 }
651
Winson Chungb3347bb2010-08-19 14:51:28 -0700652 drawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700653 } else {
654 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800655 final float imageWidth = drawable.getIntrinsicWidth();
656 final float imageHeight = drawable.getIntrinsicHeight();
657 final float aspect = (float) imageWidth / imageHeight;
658 final int scaledWidth =
659 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
660 final int scaledHeight =
661 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700662 int width;
663 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800664 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700665 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800666 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700667 } else {
668 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800669 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700670 }
671
672 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
673 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
674
675 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700676 }
677 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
678 return drawable;
679 }
680
681 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700682 layout.setCellCount(mCellCountX, mCellCountY);
683 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
684 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700685 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700686 }
687
Winson Chunge3193b92010-09-10 11:44:42 -0700688 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700689 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700690 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
691
692 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
Winson Chung45e1d6e2010-11-09 17:19:49 -0800693 mMaxWallpaperWidth = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan);
Winson Chunge3193b92010-09-10 11:44:42 -0700694 }
695
Winson Chung80baf5a2010-08-09 16:03:15 -0700696 private void syncWidgetPages() {
697 if (mWidgetList == null) return;
698
Winson Chunge3193b92010-09-10 11:44:42 -0700699 // we need to repopulate with the LinearLayout layout for the widget pages
700 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700701 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700702 for (int i = 0; i < numPages; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800703 LinearLayout layout = new PagedViewExtendedLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700704 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700705 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
706 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700707
Winson Chunge22a8e92010-11-12 13:40:58 -0800708 addView(layout, new LinearLayout.LayoutParams(
709 LinearLayout.LayoutParams.WRAP_CONTENT,
710 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung80baf5a2010-08-09 16:03:15 -0700711 }
712 }
713
714 private void syncWidgetPageItems(int page) {
715 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700716 LinearLayout layout = (LinearLayout) getChildAt(page);
717 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700718 final int count = list.size();
719 layout.removeAllViews();
720 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700721 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
722 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chungd0d43012010-09-26 17:26:45 -0700723
Winson Chunge3193b92010-09-10 11:44:42 -0700724 LinearLayout l = (LinearLayout) mInflater.inflate(
725 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700726 l.setTag(createItemInfo);
727 l.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800728 l.setOnTouchListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700729 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700730
Winson Chung94ba5b12010-11-08 17:17:47 -0800731 final Drawable icon = getWidgetPreview(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700732
733 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
734 final int hSpan = spans[0];
735 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700736
Winson Chungd0d43012010-09-26 17:26:45 -0700737 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700738 image.setMaxWidth(mMaxWidgetWidth);
739 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700740 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700741 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700742 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700743 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700744
745 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700746 }
747 }
748
Winson Chung45e1d6e2010-11-09 17:19:49 -0800749 private void syncWallpaperPages() {
750 if (mWallpaperList == null) return;
751
752 // We need to repopulate the LinearLayout for the wallpaper pages
753 removeAllViews();
754 int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) /
755 mMaxWidgetsCellHSpan);
756 for (int i = 0; i < numPages; ++i) {
757 LinearLayout layout = new PagedViewExtendedLayout(getContext());
758 layout.setGravity(Gravity.CENTER_HORIZONTAL);
759 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
760 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
761
Winson Chunge22a8e92010-11-12 13:40:58 -0800762 addView(layout, new LinearLayout.LayoutParams(
763 LinearLayout.LayoutParams.WRAP_CONTENT,
764 LinearLayout.LayoutParams.MATCH_PARENT));
Winson Chung45e1d6e2010-11-09 17:19:49 -0800765 }
766 }
767
768 private void syncWallpaperPageItems(int page) {
769 // Load the items on to the pages
770 LinearLayout layout = (LinearLayout) getChildAt(page);
771 layout.removeAllViews();
772 final int count = mWallpaperList.size();
Winson Chungd28ed492010-11-22 14:34:57 -0800773 final int numItemsPerPage = mMaxWidgetsCellHSpan / mWallpaperCellHSpan;
774 final int startIndex = page * numItemsPerPage;
775 final int endIndex = Math.min(count, startIndex + numItemsPerPage);
776 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung45e1d6e2010-11-09 17:19:49 -0800777 final ResolveInfo info = mWallpaperList.get(i);
778
779 LinearLayout l = (LinearLayout) mInflater.inflate(
780 R.layout.customize_paged_view_wallpaper, layout, false);
781 l.setTag(info);
782 l.setOnClickListener(this);
783
784 final Drawable icon = getWallpaperPreview(info);
785
786 ImageView image = (ImageView) l.findViewById(R.id.wallpaper_preview);
787 image.setMaxWidth(mMaxWidgetWidth);
788 image.setImageDrawable(icon);
789 TextView name = (TextView) l.findViewById(R.id.wallpaper_name);
790 name.setText(info.loadLabel(mPackageManager));
791
792 layout.addView(l);
793 }
794 }
795
Winson Chung80baf5a2010-08-09 16:03:15 -0700796 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700797 // we need to repopulate with PagedViewCellLayouts
798 removeAllViews();
799
Winson Chung80baf5a2010-08-09 16:03:15 -0700800 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700801 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700802 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700803 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
804 setupPage(layout);
805 addView(layout);
806 }
807 }
808
809 private void syncListPageItems(int page, List<ResolveInfo> list) {
810 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700811 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700812 int startIndex = page * numCells;
813 int endIndex = Math.min(startIndex + numCells, list.size());
814 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
815 // TODO: we can optimize by just re-applying to existing views
816 layout.removeAllViews();
817 for (int i = startIndex; i < endIndex; ++i) {
818 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700819 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
820
Winson Chung241c3b42010-08-25 16:53:03 -0700821 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
822 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700823 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
824 ((LauncherApplication)mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700825 switch (mCustomizationType) {
826 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700827 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700828 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700829 case ShortcutCustomization:
830 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
831 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
832 info.activityInfo.name);
833 icon.setTag(createItemInfo);
834 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800835 icon.setOnTouchListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700836 icon.setOnLongClickListener(this);
837 break;
838 default:
839 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700840 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700841
842 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700843 final int x = index % mCellCountX;
844 final int y = index / mCellCountX;
845 setupPage(layout);
846 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
847 }
848 }
849
850 private void syncAppPages() {
851 if (mApps == null) return;
852
853 // We need to repopulate with PagedViewCellLayouts
854 removeAllViews();
855
856 // Ensure that we have the right number of pages
857 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
858 for (int i = 0; i < numPages; ++i) {
859 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
860 setupPage(layout);
861 addView(layout);
862 }
863 }
864
865 private void syncAppPageItems(int page) {
866 if (mApps == null) return;
867
868 // ensure that we have the right number of items on the pages
869 int numCells = mCellCountX * mCellCountY;
870 int startIndex = page * numCells;
871 int endIndex = Math.min(startIndex + numCells, mApps.size());
872 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
873 // TODO: we can optimize by just re-applying to existing views
874 layout.removeAllViews();
875 for (int i = startIndex; i < endIndex; ++i) {
876 final ApplicationInfo info = mApps.get(i);
877 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
878 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700879 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700880 icon.setOnClickListener(this);
Michael Jurka7426c422010-11-11 15:23:47 -0800881 icon.setOnTouchListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700882 icon.setOnLongClickListener(this);
883
884 final int index = i - startIndex;
885 final int x = index % mCellCountX;
886 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700887 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700888 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700889 }
890 }
891
Winson Chung80baf5a2010-08-09 16:03:15 -0700892 @Override
893 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700894 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700895 switch (mCustomizationType) {
896 case WidgetCustomization:
897 syncWidgetPages();
898 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700899 case ShortcutCustomization:
900 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700901 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700902 break;
903 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800904 syncWallpaperPages();
Winson Chung80baf5a2010-08-09 16:03:15 -0700905 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700906 case ApplicationCustomization:
907 syncAppPages();
908 centerPagedViewCellLayouts = false;
909 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700910 default:
911 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700912 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700913 break;
914 }
915
916 // only try and center the page if there is one page
917 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700918 if (centerPagedViewCellLayouts) {
919 if (childCount == 1) {
920 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
921 layout.enableCenteredContent(true);
922 } else {
923 for (int i = 0; i < childCount; ++i) {
924 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
925 layout.enableCenteredContent(false);
926 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700927 }
928 }
929
930 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700931 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700932 }
933
934 @Override
935 public void syncPageItems(int page) {
936 switch (mCustomizationType) {
937 case WidgetCustomization:
938 syncWidgetPageItems(page);
939 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700940 case ShortcutCustomization:
941 syncListPageItems(page, mShortcutList);
942 break;
943 case WallpaperCustomization:
Winson Chung45e1d6e2010-11-09 17:19:49 -0800944 syncWallpaperPageItems(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700945 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700946 case ApplicationCustomization:
947 syncAppPageItems(page);
948 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700949 }
950 }
Winson Chunge3193b92010-09-10 11:44:42 -0700951
Winson Chungd0d43012010-09-26 17:26:45 -0700952 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700953 protected int getAssociatedLowerPageBound(int page) {
954 return 0;
955 }
Winson Chungd0d43012010-09-26 17:26:45 -0700956 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700957 protected int getAssociatedUpperPageBound(int page) {
958 return getChildCount();
959 }
Winson Chungd0d43012010-09-26 17:26:45 -0700960
961 @Override
962 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700963 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700964 return true;
965 }
966
967 @Override
968 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
969 return true;
970 }
971
972 @Override
973 public void onDestroyActionMode(ActionMode mode) {
974 endChoiceMode();
975 }
976
977 @Override
978 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
979 return false;
980 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700981}