blob: 5cfc38fb1dab1232cdd004ea568c34df3c5be971 [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
147 update();
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
173 update();
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
200 update();
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
215 update();
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() {
234 Context context = getContext();
235
236 // get the list of widgets
237 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
238 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
239 @Override
240 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
241 return object1.label.compareTo(object2.label);
242 }
243 });
244
245 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
246 @Override
247 public int compare(ResolveInfo object1, ResolveInfo object2) {
248 return object1.loadLabel(mPackageManager).toString().compareTo(
249 object2.loadLabel(mPackageManager).toString());
250 }
251 };
252
Winson Chung80baf5a2010-08-09 16:03:15 -0700253 // get the list of shortcuts
254 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
255 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
256 Collections.sort(mShortcutList, resolveInfoComparator);
257
Winson Chunge8878e32010-09-15 20:37:09 -0700258 // get the list of wallpapers
259 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
260 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
261 Collections.sort(mWallpaperList, resolveInfoComparator);
262
Winson Chung241c3b42010-08-25 16:53:03 -0700263 // reset the icon cache
264 mPageViewIconCache.clear();
265
Winson Chunge3193b92010-09-10 11:44:42 -0700266 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700267 invalidatePageData();
268 }
269
270 public void setDragController(DragController dragger) {
271 mDragController = dragger;
272 }
273
274 public void setCustomizationFilter(CustomizationType filterType) {
275 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700276 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700277 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700278
279 // End the current choice mode so that we don't carry selections across tabs
280 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700281 }
282
283 @Override
284 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700285 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700286 }
287
288 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700289 public void onClick(View v) {
290 if (!v.isInTouchMode()) {
291 return;
292 }
293
Winson Chungd0d43012010-09-26 17:26:45 -0700294 // On certain pages, we allow single tap to mark items as selected so that they can be
295 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700296 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700297 switch (mCustomizationType) {
298 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700299 mChoiceModeTitleText = R.string.cab_widget_selection_text;
300 enterChoiceMode = true;
301 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700302 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700303 mChoiceModeTitleText = R.string.cab_app_selection_text;
304 enterChoiceMode = true;
305 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700306 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700307 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
308 enterChoiceMode = true;
309 break;
310 default:
311 break;
312 }
Winson Chungd0d43012010-09-26 17:26:45 -0700313
Michael Jurka3125d9d2010-09-27 11:30:20 -0700314 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700315 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700316
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700317 Workspace w = mLauncher.getWorkspace();
318 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
319 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700320
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700321 animateClickFeedback(v, new Runnable() {
322 @Override
323 public void run() {
324 mLauncher.addExternalItemToScreen(itemInfo, cl);
325 }
326 });
Winson Chungd0d43012010-09-26 17:26:45 -0700327 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700328 }
329
330 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700331 switch (mCustomizationType) {
332 case WallpaperCustomization:
333 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700334 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700335 animateClickFeedback(v, new Runnable() {
336 @Override
337 public void run() {
338 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700339 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700340 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
341 ComponentName name = new ComponentName(info.activityInfo.packageName,
342 info.activityInfo.name);
343 createWallpapersIntent.setComponent(name);
344 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700345 }
346 });
Winson Chungd0d43012010-09-26 17:26:45 -0700347 break;
348 default:
349 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700350 }
351 }
352
353 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700354 public boolean onLongClick(View v) {
355 if (!v.isInTouchMode()) {
356 return false;
357 }
358
Winson Chungd0d43012010-09-26 17:26:45 -0700359 // End the current choice mode before we start dragging anything
360 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
361 endChoiceMode();
362 }
363
364 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700365 switch (mCustomizationType) {
366 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700367 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700368 final LinearLayout l = (LinearLayout) v;
369 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700370 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
371 Bitmap.Config.ARGB_8888);
372 Canvas c = new Canvas(b);
373 icon.draw(c);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700374 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700375
Michael Jurka3e7c7632010-10-02 16:01:03 -0700376 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
377 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
378 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY,
Winson Chungd0d43012010-09-26 17:26:45 -0700379 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700380
381 // Cleanup the icon
382 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700383 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700384 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700385 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700386 mDragController.startDrag(
387 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700388 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700389 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700390 case ApplicationCustomization:
391 // Pick up the application for dropping
392 ApplicationInfo app = (ApplicationInfo) v.getTag();
393 app = new ApplicationInfo(app);
394
395 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700396 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700397 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700398 }
399 return false;
400 }
401
Winson Chunge3193b92010-09-10 11:44:42 -0700402 /**
403 * Pre-processes the layout of the different widget pages.
404 * @return the number of pages of widgets that we have
405 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700406 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700407 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700408
Winson Chunge3193b92010-09-10 11:44:42 -0700409 // create a new page for the first set of widgets
410 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700411 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700412 mWidgetPages.add(newPage);
413
414 // do this until we have no more widgets to lay out
415 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
416 final int widgetCount = mWidgetList.size();
417 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700418 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700419 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700420
Winson Chunge3193b92010-09-10 11:44:42 -0700421 // determine the size of the current widget
422 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
423 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700424
Winson Chunge3193b92010-09-10 11:44:42 -0700425 // create a new page if necessary
426 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
427 numCellsInRow = 0;
428 newPage = new ArrayList<AppWidgetProviderInfo>();
429 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700430 }
431
Winson Chunge3193b92010-09-10 11:44:42 -0700432 // add the item to the current page
433 newPage.add(info);
434 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700435 }
Winson Chunge3193b92010-09-10 11:44:42 -0700436
Winson Chung80baf5a2010-08-09 16:03:15 -0700437 return mWidgetPages.size();
438 }
439
Winson Chunge3193b92010-09-10 11:44:42 -0700440 /**
441 * This method will extract the preview image specified by the widget developer (if it exists),
442 * otherwise, it will try to generate a default image preview with the widget's package icon.
443 * @return the drawable will be used and sized in the ImageView to represent the widget
444 */
445 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700446 PackageManager packageManager = mLauncher.getPackageManager();
447 String packageName = info.provider.getPackageName();
448 Drawable drawable = null;
449 if (info.previewImage != 0) {
450 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
451 if (drawable == null) {
452 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
453 + " for provider: " + info.provider);
454 } else {
455 return drawable;
456 }
457 }
458
459 // If we don't have a preview image, create a default one
460 if (drawable == null) {
461 Resources resources = mLauncher.getResources();
462
Winson Chung80baf5a2010-08-09 16:03:15 -0700463 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700464 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
465 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
466 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
467 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700468 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
469 mCanvas.setBitmap(bitmap);
470 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
471 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
472
473 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
474 background.setBounds(0, 0, width, height);
475 background.draw(mCanvas);
476
477 // Draw the icon vertically centered, flush left
478 try {
479 Rect tmpRect = new Rect();
480 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700481 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700482 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700483 }
484 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700485 icon = resources.getDrawable(R.drawable.ic_launcher_application);
486 }
487 background.getPadding(tmpRect);
488
Winson Chunge3193b92010-09-10 11:44:42 -0700489 final int iconSize = minDim / 2;
490 final int offset = iconSize / 4;
491 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700492 icon.draw(mCanvas);
493 } catch (Resources.NotFoundException e) {
494 // if we can't find the icon, then just don't draw it
495 }
496
Winson Chungb3347bb2010-08-19 14:51:28 -0700497 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700498 }
499 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
500 return drawable;
501 }
502
503 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700504 layout.setCellCount(mCellCountX, mCellCountY);
505 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
506 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700507 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700508 }
509
Winson Chunge3193b92010-09-10 11:44:42 -0700510 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700511 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700512 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
513
514 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
515 }
516
Winson Chung80baf5a2010-08-09 16:03:15 -0700517 private void syncWidgetPages() {
518 if (mWidgetList == null) return;
519
Winson Chunge3193b92010-09-10 11:44:42 -0700520 // we need to repopulate with the LinearLayout layout for the widget pages
521 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700522 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700523 for (int i = 0; i < numPages; ++i) {
Winson Chung8e4ec312010-10-13 17:22:51 -0700524 LinearLayout layout = new PagedViewWidgetLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700525 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700526 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
527 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700528
529 // Temporary change to prevent the last page from being too small (and items bleeding
530 // onto it). We can remove this once we properly fix the fading algorithm
531 if (i < numPages - 1) {
532 addView(layout, new LinearLayout.LayoutParams(
533 LinearLayout.LayoutParams.WRAP_CONTENT,
534 LinearLayout.LayoutParams.MATCH_PARENT));
535 } else {
536 addView(layout, new LinearLayout.LayoutParams(
537 LinearLayout.LayoutParams.MATCH_PARENT,
538 LinearLayout.LayoutParams.MATCH_PARENT));
539 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700540 }
541 }
542
543 private void syncWidgetPageItems(int page) {
544 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700545 LinearLayout layout = (LinearLayout) getChildAt(page);
546 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700547 final int count = list.size();
548 layout.removeAllViews();
549 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700550 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700551 PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo();
Winson Chungd0d43012010-09-26 17:26:45 -0700552 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
553 createItemInfo.componentName = info.provider;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700554 createItemInfo.minWidth = info.minWidth;
555 createItemInfo.minHeight = info.minHeight;
Winson Chungd0d43012010-09-26 17:26:45 -0700556
Winson Chunge3193b92010-09-10 11:44:42 -0700557 LinearLayout l = (LinearLayout) mInflater.inflate(
558 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700559 l.setTag(createItemInfo);
560 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700561 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700562
Winson Chunge3193b92010-09-10 11:44:42 -0700563 final Drawable icon = getWidgetIcon(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700564
565 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
566 final int hSpan = spans[0];
567 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700568
Winson Chungd0d43012010-09-26 17:26:45 -0700569 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700570 image.setMaxWidth(mMaxWidgetWidth);
571 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700572 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700573 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700574 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700575 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700576
577 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700578 }
579 }
580
581 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700582 // we need to repopulate with PagedViewCellLayouts
583 removeAllViews();
584
Winson Chung80baf5a2010-08-09 16:03:15 -0700585 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700586 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700587 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700588 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
589 setupPage(layout);
590 addView(layout);
591 }
592 }
593
594 private void syncListPageItems(int page, List<ResolveInfo> list) {
595 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700596 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700597 int startIndex = page * numCells;
598 int endIndex = Math.min(startIndex + numCells, list.size());
599 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
600 // TODO: we can optimize by just re-applying to existing views
601 layout.removeAllViews();
602 for (int i = startIndex; i < endIndex; ++i) {
603 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700604 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
605
Winson Chung241c3b42010-08-25 16:53:03 -0700606 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
607 R.layout.customize_paged_view_item, layout, false);
608 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chungd0d43012010-09-26 17:26:45 -0700609 switch (mCustomizationType) {
610 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700611 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700612 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700613 case ShortcutCustomization:
614 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
615 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
616 info.activityInfo.name);
617 icon.setTag(createItemInfo);
618 icon.setOnClickListener(this);
619 icon.setOnLongClickListener(this);
620 break;
621 default:
622 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700623 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700624
625 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700626 final int x = index % mCellCountX;
627 final int y = index / mCellCountX;
628 setupPage(layout);
629 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
630 }
631 }
632
633 private void syncAppPages() {
634 if (mApps == null) return;
635
636 // We need to repopulate with PagedViewCellLayouts
637 removeAllViews();
638
639 // Ensure that we have the right number of pages
640 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
641 for (int i = 0; i < numPages; ++i) {
642 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
643 setupPage(layout);
644 addView(layout);
645 }
646 }
647
648 private void syncAppPageItems(int page) {
649 if (mApps == null) return;
650
651 // ensure that we have the right number of items on the pages
652 int numCells = mCellCountX * mCellCountY;
653 int startIndex = page * numCells;
654 int endIndex = Math.min(startIndex + numCells, mApps.size());
655 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
656 // TODO: we can optimize by just re-applying to existing views
657 layout.removeAllViews();
658 for (int i = startIndex; i < endIndex; ++i) {
659 final ApplicationInfo info = mApps.get(i);
660 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
661 R.layout.all_apps_paged_view_application, layout, false);
Winson Chungdf4b83d2010-10-20 17:49:27 -0700662 icon.applyFromApplicationInfo(info, mPageViewIconCache, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700663 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700664 icon.setOnLongClickListener(this);
665
666 final int index = i - startIndex;
667 final int x = index % mCellCountX;
668 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700669 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700670 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700671 }
672 }
673
Winson Chung80baf5a2010-08-09 16:03:15 -0700674 @Override
675 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700676 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700677 switch (mCustomizationType) {
678 case WidgetCustomization:
679 syncWidgetPages();
680 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700681 case ShortcutCustomization:
682 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700683 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700684 break;
685 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700686 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700687 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700688 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700689 case ApplicationCustomization:
690 syncAppPages();
691 centerPagedViewCellLayouts = false;
692 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700693 default:
694 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700695 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700696 break;
697 }
698
699 // only try and center the page if there is one page
700 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700701 if (centerPagedViewCellLayouts) {
702 if (childCount == 1) {
703 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
704 layout.enableCenteredContent(true);
705 } else {
706 for (int i = 0; i < childCount; ++i) {
707 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
708 layout.enableCenteredContent(false);
709 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700710 }
711 }
712
713 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700714 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700715 }
716
717 @Override
718 public void syncPageItems(int page) {
719 switch (mCustomizationType) {
720 case WidgetCustomization:
721 syncWidgetPageItems(page);
722 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700723 case ShortcutCustomization:
724 syncListPageItems(page, mShortcutList);
725 break;
726 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700727 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700728 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700729 case ApplicationCustomization:
730 syncAppPageItems(page);
731 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700732 }
733 }
Winson Chunge3193b92010-09-10 11:44:42 -0700734
Winson Chungd0d43012010-09-26 17:26:45 -0700735 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700736 protected int getAssociatedLowerPageBound(int page) {
737 return 0;
738 }
Winson Chungd0d43012010-09-26 17:26:45 -0700739 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700740 protected int getAssociatedUpperPageBound(int page) {
741 return getChildCount();
742 }
Winson Chungd0d43012010-09-26 17:26:45 -0700743
744 @Override
745 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700746 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700747 return true;
748 }
749
750 @Override
751 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
752 return true;
753 }
754
755 @Override
756 public void onDestroyActionMode(ActionMode mode) {
757 endChoiceMode();
758 }
759
760 @Override
761 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
762 return false;
763 }
764
Winson Chung80baf5a2010-08-09 16:03:15 -0700765}