blob: a475a50d27f9e086b55d4b1f0e05b40cca4def21 [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;
36import android.graphics.Rect;
Winson Chung80baf5a2010-08-09 16:03:15 -070037import android.graphics.Region.Op;
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;
Winson Chungd0d43012010-09-26 17:26:45 -070041import android.view.ActionMode;
Winson Chunge3193b92010-09-10 11:44:42 -070042import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070043import android.view.LayoutInflater;
Winson Chungd0d43012010-09-26 17:26:45 -070044import android.view.Menu;
45import android.view.MenuItem;
Winson Chung80baf5a2010-08-09 16:03:15 -070046import android.view.View;
Winson Chunge3193b92010-09-10 11:44:42 -070047import android.widget.ImageView;
48import android.widget.LinearLayout;
Winson Chung80baf5a2010-08-09 16:03:15 -070049import android.widget.TextView;
50
Winson Chung5974adc2010-10-25 14:29:11 -070051import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070052
53public class CustomizePagedView extends PagedView
Winson Chunge8878e32010-09-15 20:37:09 -070054 implements View.OnLongClickListener, View.OnClickListener,
Winson Chungd0d43012010-09-26 17:26:45 -070055 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070056
57 public enum CustomizationType {
58 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070059 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070060 WallpaperCustomization,
61 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070062 }
63
64 private static final String TAG = "CustomizeWorkspace";
65 private static final boolean DEBUG = false;
66
67 private Launcher mLauncher;
68 private DragController mDragController;
69 private PackageManager mPackageManager;
70
71 private CustomizationType mCustomizationType;
72
Winson Chunge3193b92010-09-10 11:44:42 -070073 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
74 private PagedViewCellLayout mWorkspaceWidgetLayout;
75
76 // The mapping between the pages and the widgets that will be laid out on them
77 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
78
79 // The max dimensions for the ImageView we use for displaying the widget
80 private int mMaxWidgetWidth;
81
82 // The max number of widget cells to take a "page" of widget
83 private int mMaxWidgetsCellHSpan;
84
85 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -070086 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -070087 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -070088 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -070089 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -070090
Winson Chunge3193b92010-09-10 11:44:42 -070091 private static final int sMinWidgetCellHSpan = 2;
92 private static final int sMaxWidgetCellHSpan = 4;
93
Michael Jurka3125d9d2010-09-27 11:30:20 -070094 private int mChoiceModeTitleText;
95
Winson Chunge3193b92010-09-10 11:44:42 -070096 // The scale factor for widget previews inside the widget drawer
97 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -070098
99 private final Canvas mCanvas = new Canvas();
100 private final LayoutInflater mInflater;
101
102 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700103 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700104 }
105
106 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700107 this(context, attrs, 0);
108 }
109
110 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
111 super(context, attrs, defStyle);
112
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700113 TypedArray a;
114 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
Winson Chunge3193b92010-09-10 11:44:42 -0700115 defStyle, 0);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700116 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
117 a.recycle();
118 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
119 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
120 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700121
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700122 a.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700123 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700124 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
125 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700126 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700127
Winson Chung80baf5a2010-08-09 16:03:15 -0700128 setVisibility(View.GONE);
129 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700130 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700131 }
132
133 public void setLauncher(Launcher launcher) {
134 Context context = getContext();
135 mLauncher = launcher;
136 mPackageManager = context.getPackageManager();
137 }
138
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700139 /**
140 * Sets the list of applications that launcher has loaded.
141 */
142 public void setApps(ArrayList<ApplicationInfo> list) {
143 mApps = list;
144 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700145
146 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700147 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700148 }
149
150 /**
151 * Convenience function to add new items to the set of applications that were previously loaded.
152 * Called by both updateApps() and setApps().
153 */
154 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
155 // we add it in place, in alphabetical order
156 final int count = list.size();
157 for (int i = 0; i < count; ++i) {
158 final ApplicationInfo info = list.get(i);
159 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
160 if (index < 0) {
161 mApps.add(-(index + 1), info);
162 }
163 }
164 }
165
166 /**
167 * Adds new applications to the loaded list, and notifies the paged view to update itself.
168 */
169 public void addApps(ArrayList<ApplicationInfo> list) {
170 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700171
172 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700173 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700174 }
175
176 /**
177 * Convenience function to remove items to the set of applications that were previously loaded.
178 * Called by both updateApps() and removeApps().
179 */
180 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
181 // loop through all the apps and remove apps that have the same component
182 final int length = list.size();
183 for (int i = 0; i < length; ++i) {
184 final ApplicationInfo info = list.get(i);
185 int removeIndex = findAppByComponent(mApps, info);
186 if (removeIndex > -1) {
187 mApps.remove(removeIndex);
188 mPageViewIconCache.removeOutline(info);
189 }
190 }
191 }
192
193 /**
194 * Removes applications from the loaded list, and notifies the paged view to update itself.
195 */
196 public void removeApps(ArrayList<ApplicationInfo> list) {
197 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700198
199 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700200 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700201 }
202
203 /**
204 * Updates a set of applications from the loaded list, and notifies the paged view to update
205 * itself.
206 */
207 public void updateApps(ArrayList<ApplicationInfo> list) {
208 // We remove and re-add the updated applications list because it's properties may have
209 // changed (ie. the title), and this will ensure that the items will be in their proper
210 // place in the list.
211 removeAppsWithoutInvalidate(list);
212 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700213
214 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700215 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700216 }
217
218 /**
219 * Convenience function to find matching ApplicationInfos for removal.
220 */
221 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
222 ComponentName removeComponent = item.intent.getComponent();
223 final int length = list.size();
224 for (int i = 0; i < length; ++i) {
225 ApplicationInfo info = list.get(i);
226 if (info.intent.getComponent().equals(removeComponent)) {
227 return i;
228 }
229 }
230 return -1;
231 }
232
Winson Chung80baf5a2010-08-09 16:03:15 -0700233 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700234 // get the list of widgets
235 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
236 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
237 @Override
238 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
239 return object1.label.compareTo(object2.label);
240 }
241 });
242
243 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
244 @Override
245 public int compare(ResolveInfo object1, ResolveInfo object2) {
246 return object1.loadLabel(mPackageManager).toString().compareTo(
247 object2.loadLabel(mPackageManager).toString());
248 }
249 };
250
Winson Chung80baf5a2010-08-09 16:03:15 -0700251 // get the list of shortcuts
252 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
253 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
254 Collections.sort(mShortcutList, resolveInfoComparator);
255
Winson Chunge8878e32010-09-15 20:37:09 -0700256 // get the list of wallpapers
257 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
258 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
259 Collections.sort(mWallpaperList, resolveInfoComparator);
260
Winson Chung10fefb12010-11-01 11:57:06 -0700261 invalidatePageDataAndIconCache();
262 }
263
264 private void invalidatePageDataAndIconCache() {
265 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700266 mPageViewIconCache.clear();
267
Winson Chunge3193b92010-09-10 11:44:42 -0700268 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700269 invalidatePageData();
270 }
271
272 public void setDragController(DragController dragger) {
273 mDragController = dragger;
274 }
275
276 public void setCustomizationFilter(CustomizationType filterType) {
277 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700278 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700279 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700280
281 // End the current choice mode so that we don't carry selections across tabs
282 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700283 }
284
285 @Override
286 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700287 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700288 }
289
290 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700291 public void onClick(View v) {
292 if (!v.isInTouchMode()) {
293 return;
294 }
295
Winson Chungd0d43012010-09-26 17:26:45 -0700296 // On certain pages, we allow single tap to mark items as selected so that they can be
297 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700298 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700299 switch (mCustomizationType) {
300 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700301 mChoiceModeTitleText = R.string.cab_widget_selection_text;
302 enterChoiceMode = true;
303 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700304 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700305 mChoiceModeTitleText = R.string.cab_app_selection_text;
306 enterChoiceMode = true;
307 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700308 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700309 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
310 enterChoiceMode = true;
311 break;
312 default:
313 break;
314 }
Winson Chungd0d43012010-09-26 17:26:45 -0700315
Michael Jurka3125d9d2010-09-27 11:30:20 -0700316 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700317 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700318
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700319 Workspace w = mLauncher.getWorkspace();
320 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
321 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700322
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700323 animateClickFeedback(v, new Runnable() {
324 @Override
325 public void run() {
326 mLauncher.addExternalItemToScreen(itemInfo, cl);
327 }
328 });
Winson Chungd0d43012010-09-26 17:26:45 -0700329 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700330 }
331
332 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700333 switch (mCustomizationType) {
334 case WallpaperCustomization:
335 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700336 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700337 animateClickFeedback(v, new Runnable() {
338 @Override
339 public void run() {
340 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700341 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700342 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
343 ComponentName name = new ComponentName(info.activityInfo.packageName,
344 info.activityInfo.name);
345 createWallpapersIntent.setComponent(name);
346 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700347 }
348 });
Winson Chungd0d43012010-09-26 17:26:45 -0700349 break;
350 default:
351 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700352 }
353 }
354
355 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700356 public boolean onLongClick(View v) {
357 if (!v.isInTouchMode()) {
358 return false;
359 }
360
Winson Chungd0d43012010-09-26 17:26:45 -0700361 // End the current choice mode before we start dragging anything
362 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
363 endChoiceMode();
364 }
365
366 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700367 switch (mCustomizationType) {
368 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700369 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700370 final LinearLayout l = (LinearLayout) v;
371 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700372 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
373 Bitmap.Config.ARGB_8888);
374 Canvas c = new Canvas(b);
375 icon.draw(c);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700376 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700377
Michael Jurka3e7c7632010-10-02 16:01:03 -0700378 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
379 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
380 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY,
Winson Chungd0d43012010-09-26 17:26:45 -0700381 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700382
383 // Cleanup the icon
384 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700385 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700386 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700387 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700388 mDragController.startDrag(
389 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700390 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700391 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700392 case ApplicationCustomization:
393 // Pick up the application for dropping
394 ApplicationInfo app = (ApplicationInfo) v.getTag();
395 app = new ApplicationInfo(app);
396
397 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700398 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700399 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700400 }
401 return false;
402 }
403
Winson Chunge3193b92010-09-10 11:44:42 -0700404 /**
405 * Pre-processes the layout of the different widget pages.
406 * @return the number of pages of widgets that we have
407 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700408 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700409 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700410
Winson Chunge3193b92010-09-10 11:44:42 -0700411 // create a new page for the first set of widgets
412 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700413 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700414 mWidgetPages.add(newPage);
415
416 // do this until we have no more widgets to lay out
417 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
418 final int widgetCount = mWidgetList.size();
419 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700420 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700421 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700422
Winson Chunge3193b92010-09-10 11:44:42 -0700423 // determine the size of the current widget
424 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
425 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700426
Winson Chunge3193b92010-09-10 11:44:42 -0700427 // create a new page if necessary
428 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
429 numCellsInRow = 0;
430 newPage = new ArrayList<AppWidgetProviderInfo>();
431 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700432 }
433
Winson Chunge3193b92010-09-10 11:44:42 -0700434 // add the item to the current page
435 newPage.add(info);
436 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700437 }
Winson Chunge3193b92010-09-10 11:44:42 -0700438
Winson Chung80baf5a2010-08-09 16:03:15 -0700439 return mWidgetPages.size();
440 }
441
Winson Chunge3193b92010-09-10 11:44:42 -0700442 /**
443 * This method will extract the preview image specified by the widget developer (if it exists),
444 * otherwise, it will try to generate a default image preview with the widget's package icon.
445 * @return the drawable will be used and sized in the ImageView to represent the widget
446 */
447 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700448 PackageManager packageManager = mLauncher.getPackageManager();
449 String packageName = info.provider.getPackageName();
450 Drawable drawable = null;
451 if (info.previewImage != 0) {
452 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
453 if (drawable == null) {
454 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
455 + " for provider: " + info.provider);
456 } else {
457 return drawable;
458 }
459 }
460
461 // If we don't have a preview image, create a default one
462 if (drawable == null) {
463 Resources resources = mLauncher.getResources();
464
Winson Chung80baf5a2010-08-09 16:03:15 -0700465 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700466 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
467 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
468 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
469 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700470 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
471 mCanvas.setBitmap(bitmap);
472 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
473 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
474
475 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
476 background.setBounds(0, 0, width, height);
477 background.draw(mCanvas);
478
479 // Draw the icon vertically centered, flush left
480 try {
481 Rect tmpRect = new Rect();
482 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700483 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700484 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700485 }
486 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700487 icon = resources.getDrawable(R.drawable.ic_launcher_application);
488 }
489 background.getPadding(tmpRect);
490
Winson Chunge3193b92010-09-10 11:44:42 -0700491 final int iconSize = minDim / 2;
492 final int offset = iconSize / 4;
493 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700494 icon.draw(mCanvas);
495 } catch (Resources.NotFoundException e) {
496 // if we can't find the icon, then just don't draw it
497 }
498
Winson Chungb3347bb2010-08-19 14:51:28 -0700499 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700500 }
501 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
502 return drawable;
503 }
504
505 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700506 layout.setCellCount(mCellCountX, mCellCountY);
507 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
508 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700509 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700510 }
511
Winson Chunge3193b92010-09-10 11:44:42 -0700512 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700513 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700514 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
515
516 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
517 }
518
Winson Chung80baf5a2010-08-09 16:03:15 -0700519 private void syncWidgetPages() {
520 if (mWidgetList == null) return;
521
Winson Chunge3193b92010-09-10 11:44:42 -0700522 // we need to repopulate with the LinearLayout layout for the widget pages
523 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700524 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700525 for (int i = 0; i < numPages; ++i) {
Winson Chung8e4ec312010-10-13 17:22:51 -0700526 LinearLayout layout = new PagedViewWidgetLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700527 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700528 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
529 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700530
531 // Temporary change to prevent the last page from being too small (and items bleeding
532 // onto it). We can remove this once we properly fix the fading algorithm
533 if (i < numPages - 1) {
534 addView(layout, new LinearLayout.LayoutParams(
535 LinearLayout.LayoutParams.WRAP_CONTENT,
536 LinearLayout.LayoutParams.MATCH_PARENT));
537 } else {
538 addView(layout, new LinearLayout.LayoutParams(
539 LinearLayout.LayoutParams.MATCH_PARENT,
540 LinearLayout.LayoutParams.MATCH_PARENT));
541 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700542 }
543 }
544
545 private void syncWidgetPageItems(int page) {
546 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700547 LinearLayout layout = (LinearLayout) getChildAt(page);
548 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700549 final int count = list.size();
550 layout.removeAllViews();
551 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700552 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700553 PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo();
Winson Chungd0d43012010-09-26 17:26:45 -0700554 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
555 createItemInfo.componentName = info.provider;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700556 createItemInfo.minWidth = info.minWidth;
557 createItemInfo.minHeight = info.minHeight;
Winson Chungd0d43012010-09-26 17:26:45 -0700558
Winson Chunge3193b92010-09-10 11:44:42 -0700559 LinearLayout l = (LinearLayout) mInflater.inflate(
560 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700561 l.setTag(createItemInfo);
562 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700563 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700564
Winson Chunge3193b92010-09-10 11:44:42 -0700565 final Drawable icon = getWidgetIcon(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700566
567 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
568 final int hSpan = spans[0];
569 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700570
Winson Chungd0d43012010-09-26 17:26:45 -0700571 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700572 image.setMaxWidth(mMaxWidgetWidth);
573 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700574 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700575 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700576 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700577 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700578
579 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700580 }
581 }
582
583 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700584 // we need to repopulate with PagedViewCellLayouts
585 removeAllViews();
586
Winson Chung80baf5a2010-08-09 16:03:15 -0700587 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700588 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700589 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700590 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
591 setupPage(layout);
592 addView(layout);
593 }
594 }
595
596 private void syncListPageItems(int page, List<ResolveInfo> list) {
597 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700598 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700599 int startIndex = page * numCells;
600 int endIndex = Math.min(startIndex + numCells, list.size());
601 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
602 // TODO: we can optimize by just re-applying to existing views
603 layout.removeAllViews();
604 for (int i = startIndex; i < endIndex; ++i) {
605 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700606 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
607
Winson Chung241c3b42010-08-25 16:53:03 -0700608 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
609 R.layout.customize_paged_view_item, layout, false);
610 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700611 switch (mCustomizationType) {
612 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700613 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700614 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700615 case ShortcutCustomization:
616 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
617 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
618 info.activityInfo.name);
619 icon.setTag(createItemInfo);
620 icon.setOnClickListener(this);
621 icon.setOnLongClickListener(this);
622 break;
623 default:
624 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700625 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700626
627 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700628 final int x = index % mCellCountX;
629 final int y = index / mCellCountX;
630 setupPage(layout);
631 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
632 }
633 }
634
635 private void syncAppPages() {
636 if (mApps == null) return;
637
638 // We need to repopulate with PagedViewCellLayouts
639 removeAllViews();
640
641 // Ensure that we have the right number of pages
642 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
643 for (int i = 0; i < numPages; ++i) {
644 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
645 setupPage(layout);
646 addView(layout);
647 }
648 }
649
650 private void syncAppPageItems(int page) {
651 if (mApps == null) return;
652
653 // ensure that we have the right number of items on the pages
654 int numCells = mCellCountX * mCellCountY;
655 int startIndex = page * numCells;
656 int endIndex = Math.min(startIndex + numCells, mApps.size());
657 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
658 // TODO: we can optimize by just re-applying to existing views
659 layout.removeAllViews();
660 for (int i = startIndex; i < endIndex; ++i) {
661 final ApplicationInfo info = mApps.get(i);
662 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
663 R.layout.all_apps_paged_view_application, layout, false);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700664 icon.applyFromApplicationInfo(info, mPageViewIconCache, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700665 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700666 icon.setOnLongClickListener(this);
667
668 final int index = i - startIndex;
669 final int x = index % mCellCountX;
670 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700671 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700672 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700673 }
674 }
675
Winson Chung80baf5a2010-08-09 16:03:15 -0700676 @Override
677 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700678 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700679 switch (mCustomizationType) {
680 case WidgetCustomization:
681 syncWidgetPages();
682 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700683 case ShortcutCustomization:
684 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700685 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700686 break;
687 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700688 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700689 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700690 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700691 case ApplicationCustomization:
692 syncAppPages();
693 centerPagedViewCellLayouts = false;
694 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700695 default:
696 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700697 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700698 break;
699 }
700
701 // only try and center the page if there is one page
702 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700703 if (centerPagedViewCellLayouts) {
704 if (childCount == 1) {
705 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
706 layout.enableCenteredContent(true);
707 } else {
708 for (int i = 0; i < childCount; ++i) {
709 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
710 layout.enableCenteredContent(false);
711 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700712 }
713 }
714
715 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700716 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700717 }
718
719 @Override
720 public void syncPageItems(int page) {
721 switch (mCustomizationType) {
722 case WidgetCustomization:
723 syncWidgetPageItems(page);
724 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700725 case ShortcutCustomization:
726 syncListPageItems(page, mShortcutList);
727 break;
728 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700729 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700730 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700731 case ApplicationCustomization:
732 syncAppPageItems(page);
733 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700734 }
735 }
Winson Chunge3193b92010-09-10 11:44:42 -0700736
Winson Chungd0d43012010-09-26 17:26:45 -0700737 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700738 protected int getAssociatedLowerPageBound(int page) {
739 return 0;
740 }
Winson Chungd0d43012010-09-26 17:26:45 -0700741 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700742 protected int getAssociatedUpperPageBound(int page) {
743 return getChildCount();
744 }
Winson Chungd0d43012010-09-26 17:26:45 -0700745
746 @Override
747 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700748 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700749 return true;
750 }
751
752 @Override
753 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
754 return true;
755 }
756
757 @Override
758 public void onDestroyActionMode(ActionMode mode) {
759 endChoiceMode();
760 }
761
762 @Override
763 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
764 return false;
765 }
766
Winson Chung80baf5a2010-08-09 16:03:15 -0700767}