blob: 28de3883de1ff7fec96c8194a51a95afc6b36d50 [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 */
465 private Drawable getWidgetIcon(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
513 final float aspect = (float) info.minWidth / info.minHeight;
514 final int boundedWidth = (int) Math.max(minDim, Math.min(maxDim, info.minWidth));
515 final int boundedHeight = (int) Math.max(minDim, Math.min(maxDim, info.minHeight));
516 final int scaledWidth = (int) (boundedWidth * sScaleFactor);
517 final int scaledHeight = (int) (boundedHeight * sScaleFactor);
518 int width;
519 int height;
520 if (scaledWidth > scaledHeight) {
521 width = scaledWidth;
522 height = (int) (width / aspect);
523 } else {
524 height = scaledHeight;
525 width = (int) (height * aspect);
526 }
527
528 final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
529 renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height);
530
531 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700532 }
533 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
534 return drawable;
535 }
536
537 private void setupPage(PagedViewCellLayout layout) {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700538 layout.setCellCount(mCellCountX, mCellCountY);
539 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight,
540 mPageLayoutPaddingBottom);
Winson Chungef0066b2010-10-21 11:55:00 -0700541 layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700542 }
543
Winson Chunge3193b92010-09-10 11:44:42 -0700544 private void setupWorkspaceLayout() {
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700545 mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY);
Winson Chunge3193b92010-09-10 11:44:42 -0700546 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
547
548 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
549 }
550
Winson Chung80baf5a2010-08-09 16:03:15 -0700551 private void syncWidgetPages() {
552 if (mWidgetList == null) return;
553
Winson Chunge3193b92010-09-10 11:44:42 -0700554 // we need to repopulate with the LinearLayout layout for the widget pages
555 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700556 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700557 for (int i = 0; i < numPages; ++i) {
Winson Chung8e4ec312010-10-13 17:22:51 -0700558 LinearLayout layout = new PagedViewWidgetLayout(getContext());
Winson Chunge3193b92010-09-10 11:44:42 -0700559 layout.setGravity(Gravity.CENTER_HORIZONTAL);
Winson Chungef0066b2010-10-21 11:55:00 -0700560 layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop,
561 mPageLayoutPaddingRight, mPageLayoutPaddingBottom);
Winson Chunge3193b92010-09-10 11:44:42 -0700562
563 // Temporary change to prevent the last page from being too small (and items bleeding
564 // onto it). We can remove this once we properly fix the fading algorithm
565 if (i < numPages - 1) {
566 addView(layout, new LinearLayout.LayoutParams(
567 LinearLayout.LayoutParams.WRAP_CONTENT,
568 LinearLayout.LayoutParams.MATCH_PARENT));
569 } else {
570 addView(layout, new LinearLayout.LayoutParams(
571 LinearLayout.LayoutParams.MATCH_PARENT,
572 LinearLayout.LayoutParams.MATCH_PARENT));
573 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700574 }
575 }
576
577 private void syncWidgetPageItems(int page) {
578 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700579 LinearLayout layout = (LinearLayout) getChildAt(page);
580 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700581 final int count = list.size();
582 layout.removeAllViews();
583 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700584 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
Michael Jurka3e7c7632010-10-02 16:01:03 -0700585 PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo();
Winson Chungd0d43012010-09-26 17:26:45 -0700586 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
587 createItemInfo.componentName = info.provider;
Michael Jurka3e7c7632010-10-02 16:01:03 -0700588 createItemInfo.minWidth = info.minWidth;
589 createItemInfo.minHeight = info.minHeight;
Winson Chungd0d43012010-09-26 17:26:45 -0700590
Winson Chunge3193b92010-09-10 11:44:42 -0700591 LinearLayout l = (LinearLayout) mInflater.inflate(
592 R.layout.customize_paged_view_widget, layout, false);
Winson Chungd0d43012010-09-26 17:26:45 -0700593 l.setTag(createItemInfo);
594 l.setOnClickListener(this);
Winson Chunge3193b92010-09-10 11:44:42 -0700595 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700596
Winson Chunge3193b92010-09-10 11:44:42 -0700597 final Drawable icon = getWidgetIcon(info);
Michael Jurka9987a5c2010-10-08 16:58:12 -0700598
599 int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null);
600 final int hSpan = spans[0];
601 final int vSpan = spans[1];
Winson Chunge3193b92010-09-10 11:44:42 -0700602
Winson Chungd0d43012010-09-26 17:26:45 -0700603 ImageView image = (ImageView) l.findViewById(R.id.widget_preview);
Winson Chunge3193b92010-09-10 11:44:42 -0700604 image.setMaxWidth(mMaxWidgetWidth);
605 image.setImageDrawable(icon);
Winson Chungd0d43012010-09-26 17:26:45 -0700606 TextView name = (TextView) l.findViewById(R.id.widget_name);
Winson Chunge3193b92010-09-10 11:44:42 -0700607 name.setText(info.label);
Winson Chungd0d43012010-09-26 17:26:45 -0700608 TextView dims = (TextView) l.findViewById(R.id.widget_dims);
Winson Chung3a476782010-09-15 15:21:55 -0700609 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700610
611 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700612 }
613 }
614
615 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700616 // we need to repopulate with PagedViewCellLayouts
617 removeAllViews();
618
Winson Chung80baf5a2010-08-09 16:03:15 -0700619 // ensure that we have the right number of pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700620 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
Winson Chunge3193b92010-09-10 11:44:42 -0700621 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700622 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
623 setupPage(layout);
624 addView(layout);
625 }
626 }
627
628 private void syncListPageItems(int page, List<ResolveInfo> list) {
629 // ensure that we have the right number of items on the pages
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700630 int numCells = mCellCountX * mCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700631 int startIndex = page * numCells;
632 int endIndex = Math.min(startIndex + numCells, list.size());
633 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
634 // TODO: we can optimize by just re-applying to existing views
635 layout.removeAllViews();
636 for (int i = startIndex; i < endIndex; ++i) {
637 ResolveInfo info = list.get(i);
Winson Chungd0d43012010-09-26 17:26:45 -0700638 PendingAddItemInfo createItemInfo = new PendingAddItemInfo();
639
Winson Chung241c3b42010-08-25 16:53:03 -0700640 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
641 R.layout.customize_paged_view_item, layout, false);
Michael Jurkac9a96192010-11-01 11:52:08 -0700642 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache,
643 ((LauncherApplication)mLauncher.getApplication()).getIconCache());
Winson Chungd0d43012010-09-26 17:26:45 -0700644 switch (mCustomizationType) {
645 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700646 icon.setOnClickListener(this);
Winson Chungd0d43012010-09-26 17:26:45 -0700647 break;
Winson Chungd0d43012010-09-26 17:26:45 -0700648 case ShortcutCustomization:
649 createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
650 createItemInfo.componentName = new ComponentName(info.activityInfo.packageName,
651 info.activityInfo.name);
652 icon.setTag(createItemInfo);
653 icon.setOnClickListener(this);
654 icon.setOnLongClickListener(this);
655 break;
656 default:
657 break;
Winson Chunge8878e32010-09-15 20:37:09 -0700658 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700659
660 final int index = i - startIndex;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700661 final int x = index % mCellCountX;
662 final int y = index / mCellCountX;
663 setupPage(layout);
664 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
665 }
666 }
667
668 private void syncAppPages() {
669 if (mApps == null) return;
670
671 // We need to repopulate with PagedViewCellLayouts
672 removeAllViews();
673
674 // Ensure that we have the right number of pages
675 int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY));
676 for (int i = 0; i < numPages; ++i) {
677 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
678 setupPage(layout);
679 addView(layout);
680 }
681 }
682
683 private void syncAppPageItems(int page) {
684 if (mApps == null) return;
685
686 // ensure that we have the right number of items on the pages
687 int numCells = mCellCountX * mCellCountY;
688 int startIndex = page * numCells;
689 int endIndex = Math.min(startIndex + numCells, mApps.size());
690 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
691 // TODO: we can optimize by just re-applying to existing views
692 layout.removeAllViews();
693 for (int i = startIndex; i < endIndex; ++i) {
694 final ApplicationInfo info = mApps.get(i);
695 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
696 R.layout.all_apps_paged_view_application, layout, false);
Winson Chung7da10252010-10-28 16:07:04 -0700697 icon.applyFromApplicationInfo(info, mPageViewIconCache, true);
Winson Chungd0d43012010-09-26 17:26:45 -0700698 icon.setOnClickListener(this);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700699 icon.setOnLongClickListener(this);
700
701 final int index = i - startIndex;
702 final int x = index % mCellCountX;
703 final int y = index / mCellCountX;
Winson Chunge3193b92010-09-10 11:44:42 -0700704 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700705 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700706 }
707 }
708
Winson Chung80baf5a2010-08-09 16:03:15 -0700709 @Override
710 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700711 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700712 switch (mCustomizationType) {
713 case WidgetCustomization:
714 syncWidgetPages();
715 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700716 case ShortcutCustomization:
717 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700718 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700719 break;
720 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700721 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700722 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700723 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700724 case ApplicationCustomization:
725 syncAppPages();
726 centerPagedViewCellLayouts = false;
727 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700728 default:
729 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700730 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700731 break;
732 }
733
734 // only try and center the page if there is one page
735 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700736 if (centerPagedViewCellLayouts) {
737 if (childCount == 1) {
738 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
739 layout.enableCenteredContent(true);
740 } else {
741 for (int i = 0; i < childCount; ++i) {
742 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
743 layout.enableCenteredContent(false);
744 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700745 }
746 }
747
748 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700749 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700750 }
751
752 @Override
753 public void syncPageItems(int page) {
754 switch (mCustomizationType) {
755 case WidgetCustomization:
756 syncWidgetPageItems(page);
757 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700758 case ShortcutCustomization:
759 syncListPageItems(page, mShortcutList);
760 break;
761 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700762 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700763 break;
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700764 case ApplicationCustomization:
765 syncAppPageItems(page);
766 break;
Winson Chung80baf5a2010-08-09 16:03:15 -0700767 }
768 }
Winson Chunge3193b92010-09-10 11:44:42 -0700769
Winson Chungd0d43012010-09-26 17:26:45 -0700770 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700771 protected int getAssociatedLowerPageBound(int page) {
772 return 0;
773 }
Winson Chungd0d43012010-09-26 17:26:45 -0700774 @Override
Winson Chunge3193b92010-09-10 11:44:42 -0700775 protected int getAssociatedUpperPageBound(int page) {
776 return getChildCount();
777 }
Winson Chungd0d43012010-09-26 17:26:45 -0700778
779 @Override
780 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
Michael Jurka3125d9d2010-09-27 11:30:20 -0700781 mode.setTitle(mChoiceModeTitleText);
Winson Chungd0d43012010-09-26 17:26:45 -0700782 return true;
783 }
784
785 @Override
786 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
787 return true;
788 }
789
790 @Override
791 public void onDestroyActionMode(ActionMode mode) {
792 endChoiceMode();
793 }
794
795 @Override
796 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
797 return false;
798 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700799}