blob: c432f37c1a723ad52993d50fae17ff997992f716 [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;
Winson Chung80baf5a2010-08-09 16:03:15 -070047import android.view.View;
Winson Chunge3193b92010-09-10 11:44:42 -070048import android.widget.ImageView;
49import android.widget.LinearLayout;
Winson Chung80baf5a2010-08-09 16:03:15 -070050import android.widget.TextView;
51
Winson Chung5974adc2010-10-25 14:29:11 -070052import com.android.launcher.R;
Winson Chung80baf5a2010-08-09 16:03:15 -070053
54public class CustomizePagedView extends PagedView
Winson Chunge8878e32010-09-15 20:37:09 -070055 implements View.OnLongClickListener, View.OnClickListener,
Winson Chungd0d43012010-09-26 17:26:45 -070056 DragSource, ActionMode.Callback {
Winson Chung80baf5a2010-08-09 16:03:15 -070057
58 public enum CustomizationType {
59 WidgetCustomization,
Winson Chung80baf5a2010-08-09 16:03:15 -070060 ShortcutCustomization,
Winson Chung5ffd8ea2010-09-23 18:40:29 -070061 WallpaperCustomization,
62 ApplicationCustomization
Winson Chung80baf5a2010-08-09 16:03:15 -070063 }
64
65 private static final String TAG = "CustomizeWorkspace";
66 private static final boolean DEBUG = false;
67
68 private Launcher mLauncher;
69 private DragController mDragController;
70 private PackageManager mPackageManager;
71
72 private CustomizationType mCustomizationType;
73
Winson Chunge3193b92010-09-10 11:44:42 -070074 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
75 private PagedViewCellLayout mWorkspaceWidgetLayout;
76
77 // The mapping between the pages and the widgets that will be laid out on them
78 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
79
80 // The max dimensions for the ImageView we use for displaying the widget
81 private int mMaxWidgetWidth;
82
83 // The max number of widget cells to take a "page" of widget
84 private int mMaxWidgetsCellHSpan;
85
86 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -070087 private List<AppWidgetProviderInfo> mWidgetList;
Winson Chung80baf5a2010-08-09 16:03:15 -070088 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -070089 private List<ResolveInfo> mWallpaperList;
Winson Chung5ffd8ea2010-09-23 18:40:29 -070090 private List<ApplicationInfo> mApps;
Winson Chung80baf5a2010-08-09 16:03:15 -070091
Winson Chunge3193b92010-09-10 11:44:42 -070092 private static final int sMinWidgetCellHSpan = 2;
93 private static final int sMaxWidgetCellHSpan = 4;
94
Michael Jurka3125d9d2010-09-27 11:30:20 -070095 private int mChoiceModeTitleText;
96
Winson Chunge3193b92010-09-10 11:44:42 -070097 // The scale factor for widget previews inside the widget drawer
98 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -070099
100 private final Canvas mCanvas = new Canvas();
101 private final LayoutInflater mInflater;
102
103 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700104 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700105 }
106
107 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700108 this(context, attrs, 0);
109 }
110
111 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
112 super(context, attrs, defStyle);
113
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700114 TypedArray a;
115 a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
Winson Chunge3193b92010-09-10 11:44:42 -0700116 defStyle, 0);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700117 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
118 a.recycle();
119 a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
120 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7);
121 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700122
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700123 a.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700124 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700125 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
126 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700127 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700128
Winson Chung80baf5a2010-08-09 16:03:15 -0700129 setVisibility(View.GONE);
130 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700131 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700132 }
133
Winson Chung7da10252010-10-28 16:07:04 -0700134 @Override
135 protected void init() {
136 super.init();
137 mCenterPagesVertically = false;
138 }
139
Winson Chung80baf5a2010-08-09 16:03:15 -0700140 public void setLauncher(Launcher launcher) {
141 Context context = getContext();
142 mLauncher = launcher;
143 mPackageManager = context.getPackageManager();
144 }
145
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700146 /**
147 * Sets the list of applications that launcher has loaded.
148 */
149 public void setApps(ArrayList<ApplicationInfo> list) {
150 mApps = list;
151 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung5f941722010-09-28 16:36:43 -0700152
153 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700154 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700155 }
156
157 /**
158 * Convenience function to add new items to the set of applications that were previously loaded.
159 * Called by both updateApps() and setApps().
160 */
161 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
162 // we add it in place, in alphabetical order
163 final int count = list.size();
164 for (int i = 0; i < count; ++i) {
165 final ApplicationInfo info = list.get(i);
166 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
167 if (index < 0) {
168 mApps.add(-(index + 1), info);
169 }
170 }
171 }
172
173 /**
174 * Adds new applications to the loaded list, and notifies the paged view to update itself.
175 */
176 public void addApps(ArrayList<ApplicationInfo> list) {
177 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700178
179 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700180 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700181 }
182
183 /**
184 * Convenience function to remove items to the set of applications that were previously loaded.
185 * Called by both updateApps() and removeApps().
186 */
187 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
188 // loop through all the apps and remove apps that have the same component
189 final int length = list.size();
190 for (int i = 0; i < length; ++i) {
191 final ApplicationInfo info = list.get(i);
192 int removeIndex = findAppByComponent(mApps, info);
193 if (removeIndex > -1) {
194 mApps.remove(removeIndex);
195 mPageViewIconCache.removeOutline(info);
196 }
197 }
198 }
199
200 /**
201 * Removes applications from the loaded list, and notifies the paged view to update itself.
202 */
203 public void removeApps(ArrayList<ApplicationInfo> list) {
204 removeAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700205
206 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700207 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700208 }
209
210 /**
211 * Updates a set of applications from the loaded list, and notifies the paged view to update
212 * itself.
213 */
214 public void updateApps(ArrayList<ApplicationInfo> list) {
215 // We remove and re-add the updated applications list because it's properties may have
216 // changed (ie. the title), and this will ensure that the items will be in their proper
217 // place in the list.
218 removeAppsWithoutInvalidate(list);
219 addAppsWithoutInvalidate(list);
Winson Chung5f941722010-09-28 16:36:43 -0700220
221 // Update the widgets/shortcuts to reflect changes in the set of available apps
Winson Chung10fefb12010-11-01 11:57:06 -0700222 invalidatePageDataAndIconCache();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700223 }
224
225 /**
226 * Convenience function to find matching ApplicationInfos for removal.
227 */
228 private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) {
229 ComponentName removeComponent = item.intent.getComponent();
230 final int length = list.size();
231 for (int i = 0; i < length; ++i) {
232 ApplicationInfo info = list.get(i);
233 if (info.intent.getComponent().equals(removeComponent)) {
234 return i;
235 }
236 }
237 return -1;
238 }
239
Winson Chung80baf5a2010-08-09 16:03:15 -0700240 public void update() {
Winson Chung80baf5a2010-08-09 16:03:15 -0700241 // get the list of widgets
242 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
243 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
244 @Override
245 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
246 return object1.label.compareTo(object2.label);
247 }
248 });
249
250 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
251 @Override
252 public int compare(ResolveInfo object1, ResolveInfo object2) {
253 return object1.loadLabel(mPackageManager).toString().compareTo(
254 object2.loadLabel(mPackageManager).toString());
255 }
256 };
257
Winson Chung80baf5a2010-08-09 16:03:15 -0700258 // get the list of shortcuts
259 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
260 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
261 Collections.sort(mShortcutList, resolveInfoComparator);
262
Winson Chunge8878e32010-09-15 20:37:09 -0700263 // get the list of wallpapers
264 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
265 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
266 Collections.sort(mWallpaperList, resolveInfoComparator);
267
Winson Chung10fefb12010-11-01 11:57:06 -0700268 invalidatePageDataAndIconCache();
269 }
270
271 private void invalidatePageDataAndIconCache() {
272 // Reset the icon cache
Winson Chung241c3b42010-08-25 16:53:03 -0700273 mPageViewIconCache.clear();
274
Winson Chunge3193b92010-09-10 11:44:42 -0700275 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700276 invalidatePageData();
277 }
278
279 public void setDragController(DragController dragger) {
280 mDragController = dragger;
281 }
282
283 public void setCustomizationFilter(CustomizationType filterType) {
284 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700285 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700286 invalidatePageData();
Winson Chungd0d43012010-09-26 17:26:45 -0700287
288 // End the current choice mode so that we don't carry selections across tabs
289 endChoiceMode();
Winson Chung80baf5a2010-08-09 16:03:15 -0700290 }
291
292 @Override
293 public void onDropCompleted(View target, boolean success) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700294 mLauncher.getWorkspace().onDragStopped();
Winson Chung80baf5a2010-08-09 16:03:15 -0700295 }
296
297 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700298 public void onClick(View v) {
299 if (!v.isInTouchMode()) {
300 return;
301 }
302
Winson Chungd0d43012010-09-26 17:26:45 -0700303 // On certain pages, we allow single tap to mark items as selected so that they can be
304 // dropped onto the mini workspaces
Michael Jurka3125d9d2010-09-27 11:30:20 -0700305 boolean enterChoiceMode = false;
Winson Chungd0d43012010-09-26 17:26:45 -0700306 switch (mCustomizationType) {
307 case WidgetCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700308 mChoiceModeTitleText = R.string.cab_widget_selection_text;
309 enterChoiceMode = true;
310 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700311 case ApplicationCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700312 mChoiceModeTitleText = R.string.cab_app_selection_text;
313 enterChoiceMode = true;
314 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700315 case ShortcutCustomization:
Michael Jurka3125d9d2010-09-27 11:30:20 -0700316 mChoiceModeTitleText = R.string.cab_shortcut_selection_text;
317 enterChoiceMode = true;
318 break;
319 default:
320 break;
321 }
Winson Chungd0d43012010-09-26 17:26:45 -0700322
Michael Jurka3125d9d2010-09-27 11:30:20 -0700323 if (enterChoiceMode) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700324 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Winson Chungd0d43012010-09-26 17:26:45 -0700325
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700326 Workspace w = mLauncher.getWorkspace();
327 int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen();
328 final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen);
Michael Jurkae17e19c2010-09-28 11:01:39 -0700329
Michael Jurka6b4b25d2010-10-20 18:19:45 -0700330 animateClickFeedback(v, new Runnable() {
331 @Override
332 public void run() {
333 mLauncher.addExternalItemToScreen(itemInfo, cl);
334 }
335 });
Winson Chungd0d43012010-09-26 17:26:45 -0700336 return;
Winson Chungd0d43012010-09-26 17:26:45 -0700337 }
338
339 // Otherwise, we just handle the single click here
Winson Chunge8878e32010-09-15 20:37:09 -0700340 switch (mCustomizationType) {
341 case WallpaperCustomization:
342 // animate some feedback to the long press
Winson Chungd0d43012010-09-26 17:26:45 -0700343 final View clickView = v;
Winson Chunge8878e32010-09-15 20:37:09 -0700344 animateClickFeedback(v, new Runnable() {
345 @Override
346 public void run() {
347 // add the shortcut
Winson Chungd0d43012010-09-26 17:26:45 -0700348 ResolveInfo info = (ResolveInfo) clickView.getTag();
Winson Chung24ab2f12010-09-16 14:10:47 -0700349 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
350 ComponentName name = new ComponentName(info.activityInfo.packageName,
351 info.activityInfo.name);
352 createWallpapersIntent.setComponent(name);
353 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700354 }
355 });
Winson Chungd0d43012010-09-26 17:26:45 -0700356 break;
357 default:
358 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700359 }
360 }
361
362 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700363 public boolean onLongClick(View v) {
364 if (!v.isInTouchMode()) {
365 return false;
366 }
367
Winson Chungd0d43012010-09-26 17:26:45 -0700368 // End the current choice mode before we start dragging anything
369 if (isChoiceMode(CHOICE_MODE_SINGLE)) {
370 endChoiceMode();
371 }
372
373 PendingAddItemInfo createItemInfo;
Winson Chung80baf5a2010-08-09 16:03:15 -0700374 switch (mCustomizationType) {
375 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700376 // Get the icon as the drag representation
Winson Chungd0d43012010-09-26 17:26:45 -0700377 final LinearLayout l = (LinearLayout) v;
378 final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable();
Winson Chunge3193b92010-09-10 11:44:42 -0700379 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
380 Bitmap.Config.ARGB_8888);
381 Canvas c = new Canvas(b);
382 icon.draw(c);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700383 PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag();
Michael Jurkaa63c4522010-08-19 13:52:27 -0700384
Michael Jurka3e7c7632010-10-02 16:01:03 -0700385 mLauncher.getWorkspace().onDragStartedWithItemMinSize(
386 createWidgetInfo.minWidth, createWidgetInfo.minHeight);
387 mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY,
Winson Chungd0d43012010-09-26 17:26:45 -0700388 null);
Winson Chunge3193b92010-09-10 11:44:42 -0700389
390 // Cleanup the icon
391 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700392 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700393 case ShortcutCustomization:
Winson Chungd0d43012010-09-26 17:26:45 -0700394 createItemInfo = (PendingAddItemInfo) v.getTag();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700395 mDragController.startDrag(
396 v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700397 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung80baf5a2010-08-09 16:03:15 -0700398 return true;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700399 case ApplicationCustomization:
400 // Pick up the application for dropping
401 ApplicationInfo app = (ApplicationInfo) v.getTag();
402 app = new ApplicationInfo(app);
403
404 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700405 mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700406 return true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700407 }
408 return false;
409 }
410
Winson Chunge3193b92010-09-10 11:44:42 -0700411 /**
412 * Pre-processes the layout of the different widget pages.
413 * @return the number of pages of widgets that we have
414 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700415 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700416 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700417
Winson Chunge3193b92010-09-10 11:44:42 -0700418 // create a new page for the first set of widgets
419 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700420 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700421 mWidgetPages.add(newPage);
422
423 // do this until we have no more widgets to lay out
424 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
425 final int widgetCount = mWidgetList.size();
426 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700427 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700428 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700429
Winson Chunge3193b92010-09-10 11:44:42 -0700430 // determine the size of the current widget
431 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
432 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700433
Winson Chunge3193b92010-09-10 11:44:42 -0700434 // create a new page if necessary
435 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
436 numCellsInRow = 0;
437 newPage = new ArrayList<AppWidgetProviderInfo>();
438 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700439 }
440
Winson Chunge3193b92010-09-10 11:44:42 -0700441 // add the item to the current page
442 newPage.add(info);
443 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700444 }
Winson Chunge3193b92010-09-10 11:44:42 -0700445
Winson Chung80baf5a2010-08-09 16:03:15 -0700446 return mWidgetPages.size();
447 }
448
Winson Chunge3193b92010-09-10 11:44:42 -0700449 /**
Winson Chung7da10252010-10-28 16:07:04 -0700450 * Helper function to draw a drawable to the specified canvas with the specified bounds.
451 */
452 private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int l, int t, int r, int b) {
453 if (bitmap != null) mCanvas.setBitmap(bitmap);
454 mCanvas.save();
455 d.setBounds(l, t, r, b);
456 d.draw(mCanvas);
457 mCanvas.restore();
458 }
459
460 /**
Winson Chunge3193b92010-09-10 11:44:42 -0700461 * This method will extract the preview image specified by the widget developer (if it exists),
462 * otherwise, it will try to generate a default image preview with the widget's package icon.
463 * @return the drawable will be used and sized in the ImageView to represent the widget
464 */
Winson Chung94ba5b12010-11-08 17:17:47 -0800465 private Drawable getWidgetPreview(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700466 PackageManager packageManager = mLauncher.getPackageManager();
467 String packageName = info.provider.getPackageName();
468 Drawable drawable = null;
469 if (info.previewImage != 0) {
470 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
471 if (drawable == null) {
472 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
473 + " for provider: " + info.provider);
Winson Chung80baf5a2010-08-09 16:03:15 -0700474 }
475 }
476
477 // If we don't have a preview image, create a default one
Winson Chung7da10252010-10-28 16:07:04 -0700478 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
479 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
Winson Chung80baf5a2010-08-09 16:03:15 -0700480 if (drawable == null) {
481 Resources resources = mLauncher.getResources();
482
Winson Chung80baf5a2010-08-09 16:03:15 -0700483 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700484 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
485 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700486 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
487 final Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
488 renderDrawableToBitmap(background, bitmap, 0, 0, width, height);
Winson Chung80baf5a2010-08-09 16:03:15 -0700489
490 // Draw the icon vertically centered, flush left
491 try {
492 Rect tmpRect = new Rect();
493 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700494 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700495 icon = packageManager.getDrawable(packageName, info.icon, null);
Winson Chung5f941722010-09-28 16:36:43 -0700496 }
497 if (icon == null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700498 icon = resources.getDrawable(R.drawable.ic_launcher_application);
499 }
500 background.getPadding(tmpRect);
501
Winson Chunge3193b92010-09-10 11:44:42 -0700502 final int iconSize = minDim / 2;
503 final int offset = iconSize / 4;
Winson Chung7da10252010-10-28 16:07:04 -0700504 final int offsetIconSize = offset + iconSize;
505 renderDrawableToBitmap(icon, null, offset, offset, offsetIconSize, offsetIconSize);
Winson Chung80baf5a2010-08-09 16:03:15 -0700506 } catch (Resources.NotFoundException e) {
507 // if we can't find the icon, then just don't draw it
508 }
509
Winson Chungb3347bb2010-08-19 14:51:28 -0700510 drawable = new FastBitmapDrawable(bitmap);
Winson Chung7da10252010-10-28 16:07:04 -0700511 } else {
512 // Scale down the preview if necessary
Winson Chung94ba5b12010-11-08 17:17:47 -0800513 final float imageWidth = drawable.getIntrinsicWidth();
514 final float imageHeight = drawable.getIntrinsicHeight();
515 final float aspect = (float) imageWidth / imageHeight;
516 final int scaledWidth =
517 (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor);
518 final int scaledHeight =
519 (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor);
Winson Chung7da10252010-10-28 16:07:04 -0700520 int width;
521 int height;
Winson Chung94ba5b12010-11-08 17:17:47 -0800522 if (aspect >= 1.0f) {
Winson Chung7da10252010-10-28 16:07:04 -0700523 width = scaledWidth;
Winson Chung94ba5b12010-11-08 17:17:47 -0800524 height = (int) (((float) scaledWidth / imageWidth) * imageHeight);
Winson Chung7da10252010-10-28 16:07:04 -0700525 } else {
526 height = scaledHeight;
Winson Chung94ba5b12010-11-08 17:17:47 -0800527 width = (int) (((float) scaledHeight / imageHeight) * imageWidth);
Winson Chung7da10252010-10-28 16:07:04 -0700528 }
529
530 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
531 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
532
533 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700534 }
535 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
536 return drawable;
537 }
538
539 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700540 layout.setCellCount(mCellCountX, mCellCountY);
541 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
542 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700543 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700544 }
545
Winson Chunge3193b92010-09-10 11:44:42 -0700546 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700547 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700548 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
549
550 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
551 }
552
Winson Chung80baf5a2010-08-09 16:03:15 -0700553 private void syncWidgetPages() {
554 if (mWidgetList == null) return;
555
Winson Chunge3193b92010-09-10 11:44:42 -0700556 // we need to repopulate with the LinearLayout layout for the widget pages
557 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700558 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700559 for (int i = 0; i < numPages; ++i) {
Winson Chung8e4ec312010-10-13 17:22:51 -0700560 LinearLayout layout = new PagedViewWidgetLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700561 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700562 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
563 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700564
565 // Temporary change to prevent the last page from being too small (and items bleeding
566 // onto it). We can remove this once we properly fix the fading algorithm
567 if (i < numPages - 1) {
568 addView(layout, new LinearLayout.LayoutParams(
569 LinearLayout.LayoutParams.WRAP_CONTENT,
570 LinearLayout.LayoutParams.MATCH_PARENT));
571 } else {
572 addView(layout, new LinearLayout.LayoutParams(
573 LinearLayout.LayoutParams.MATCH_PARENT,
574 LinearLayout.LayoutParams.MATCH_PARENT));
575 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700576 }
577 }
578
579 private void syncWidgetPageItems(int page) {
580 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700581 LinearLayout layout = (LinearLayout) getChildAt(page);
582 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700583 final int count = list.size();
584 layout.removeAllViews();
585 for (int i = 0; i < count; ++i) {
Winson Chung68846fd2010-10-29 11:00:27 -0700586 final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
587 final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null);
Winson Chungd0d43012010-09-26 17:26:45 -0700588
Winson Chunge3193b92010-09-10 11:44:42 -0700589 LinearLayout l = (LinearLayout) mInflater.inflate(
590 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700591 l.setTag(createItemInfo);
592 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700593 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700594
Winson Chung94ba5b12010-11-08 17:17:47 -0800595 final Drawable icon = getWidgetPreview(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700596
597 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
598 final int hSpan = spans[0];
599 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700600
Winson Chungd0d43012010-09-26 17:26:45 -0700601 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700602 image.setMaxWidth(mMaxWidgetWidth);
603 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700604 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700605 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700606 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700607 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700608
609 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700610 }
611 }
612
613 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700614 // we need to repopulate with PagedViewCellLayouts
615 removeAllViews();
616
Winson Chung80baf5a2010-08-09 16:03:15 -0700617 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700618 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700619 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700620 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
621 setupPage(layout);
622 addView(layout);
623 }
624 }
625
626 private void syncListPageItems(int page, List<ResolveInfo> list) {
627 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700628 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700629 int startIndex = page * numCells;
630 int endIndex = Math.min(startIndex + numCells, list.size());
631 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
632 // TODO: we can optimize by just re-applying to existing views
633 layout.removeAllViews();
634 for (int i = startIndex; i < endIndex; ++i) {
635 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700636 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
637
Winson Chung241c3b42010-08-25 16:53:03 -0700638 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
639 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700640 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
641 ((LauncherApplication)mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700642 switch (mCustomizationType) {
643 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700644 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700645 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700646 case ShortcutCustomization:
647 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
648 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
649 info.activityInfo.name);
650 icon.setTag(createItemInfo);
651 icon.setOnClickListener(this);
652 icon.setOnLongClickListener(this);
653 break;
654 default:
655 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700656 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700657
658 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700659 final int x = index % mCellCountX;
660 final int y = index / mCellCountX;
661 setupPage(layout);
662 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
663 }
664 }
665
666 private void syncAppPages() {
667 if (mApps == null) return;
668
669 // We need to repopulate with PagedViewCellLayouts
670 removeAllViews();
671
672 // Ensure that we have the right number of pages
673 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
674 for (int i = 0; i < numPages; ++i) {
675 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
676 setupPage(layout);
677 addView(layout);
678 }
679 }
680
681 private void syncAppPageItems(int page) {
682 if (mApps == null) return;
683
684 // ensure that we have the right number of items on the pages
685 int numCells = mCellCountX * mCellCountY;
686 int startIndex = page * numCells;
687 int endIndex = Math.min(startIndex + numCells, mApps.size());
688 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
689 // TODO: we can optimize by just re-applying to existing views
690 layout.removeAllViews();
691 for (int i = startIndex; i < endIndex; ++i) {
692 final ApplicationInfo info = mApps.get(i);
693 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
694 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700695 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700696 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700697 icon.setOnLongClickListener(this);
698
699 final int index = i - startIndex;
700 final int x = index % mCellCountX;
701 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700702 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700703 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700704 }
705 }
706
Winson Chung80baf5a2010-08-09 16:03:15 -0700707 @Override
708 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700709 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700710 switch (mCustomizationType) {
711 case WidgetCustomization:
712 syncWidgetPages();
713 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700714 case ShortcutCustomization:
715 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700716 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700717 break;
718 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700719 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700720 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700721 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700722 case ApplicationCustomization:
723 syncAppPages();
724 centerPagedViewCellLayouts = false;
725 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700726 default:
727 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700728 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700729 break;
730 }
731
732 // only try and center the page if there is one page
733 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700734 if (centerPagedViewCellLayouts) {
735 if (childCount == 1) {
736 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
737 layout.enableCenteredContent(true);
738 } else {
739 for (int i = 0; i < childCount; ++i) {
740 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
741 layout.enableCenteredContent(false);
742 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700743 }
744 }
745
746 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700747 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700748 }
749
750 @Override
751 public void syncPageItems(int page) {
752 switch (mCustomizationType) {
753 case WidgetCustomization:
754 syncWidgetPageItems(page);
755 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700756 case ShortcutCustomization:
757 syncListPageItems(page, mShortcutList);
758 break;
759 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700760 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700761 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700762 case ApplicationCustomization:
763 syncAppPageItems(page);
764 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700765 }
766 }
Winson Chunge3193b92010-09-10 11:44:42 -0700767
Winson Chungd0d43012010-09-26 17:26:45 -0700768 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700769 protected int getAssociatedLowerPageBound(int page) {
770 return 0;
771 }
Winson Chungd0d43012010-09-26 17:26:45 -0700772 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700773 protected int getAssociatedUpperPageBound(int page) {
774 return getChildCount();
775 }
Winson Chungd0d43012010-09-26 17:26:45 -0700776
777 @Override
778 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700779 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700780 return true;
781 }
782
783 @Override
784 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
785 return true;
786 }
787
788 @Override
789 public void onDestroyActionMode(ActionMode mode) {
790 endChoiceMode();
791 }
792
793 @Override
794 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
795 return false;
796 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700797}