blob: 786300bf79277c446f1766d52347aa4ece0682ee [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
19import java.util.ArrayList;
20import java.util.Collections;
21import java.util.Comparator;
22import java.util.List;
23
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;
32import android.graphics.Bitmap;
Winson Chung80baf5a2010-08-09 16:03:15 -070033import android.graphics.Bitmap.Config;
Winson Chung86f77532010-08-24 11:08:22 -070034import android.graphics.Canvas;
35import android.graphics.Rect;
Winson Chung80baf5a2010-08-09 16:03:15 -070036import android.graphics.Region.Op;
Winson Chung80baf5a2010-08-09 16:03:15 -070037import android.graphics.drawable.Drawable;
38import android.provider.LiveFolders;
39import android.util.AttributeSet;
40import android.util.Log;
Winson Chung80baf5a2010-08-09 16:03:15 -070041import android.view.LayoutInflater;
42import android.view.View;
43import android.widget.TextView;
44
45import com.android.launcher.R;
46
47public class CustomizePagedView extends PagedView
48 implements View.OnLongClickListener,
49 DragSource {
50
51 public enum CustomizationType {
52 WidgetCustomization,
53 FolderCustomization,
54 ShortcutCustomization,
55 WallpaperCustomization
56 }
57
58 private static final String TAG = "CustomizeWorkspace";
59 private static final boolean DEBUG = false;
60
61 private Launcher mLauncher;
62 private DragController mDragController;
63 private PackageManager mPackageManager;
64
65 private CustomizationType mCustomizationType;
66
67 private PagedViewCellLayout mTmpWidgetLayout;
68 private ArrayList<ArrayList<PagedViewCellLayout.LayoutParams>> mWidgetPages;
69 private List<AppWidgetProviderInfo> mWidgetList;
70 private List<ResolveInfo> mFolderList;
71 private List<ResolveInfo> mShortcutList;
72
73 private int mCellCountX;
74 private int mCellCountY;
75
76 private final Canvas mCanvas = new Canvas();
77 private final LayoutInflater mInflater;
78
79 public CustomizePagedView(Context context) {
80 this(context, null);
81 }
82
83 public CustomizePagedView(Context context, AttributeSet attrs) {
84 super(context, attrs);
85 mCellCountX = 8;
86 mCellCountY = 4;
87 mCustomizationType = CustomizationType.WidgetCustomization;
88 mWidgetPages = new ArrayList<ArrayList<PagedViewCellLayout.LayoutParams>>();
89 mTmpWidgetLayout = new PagedViewCellLayout(context);
90 mInflater = LayoutInflater.from(context);
91 setupPage(mTmpWidgetLayout);
92 setVisibility(View.GONE);
93 setSoundEffectsEnabled(false);
94 }
95
96 public void setLauncher(Launcher launcher) {
97 Context context = getContext();
98 mLauncher = launcher;
99 mPackageManager = context.getPackageManager();
100 }
101
102 public void update() {
103 Context context = getContext();
104
105 // get the list of widgets
106 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
107 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
108 @Override
109 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
110 return object1.label.compareTo(object2.label);
111 }
112 });
113
114 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
115 @Override
116 public int compare(ResolveInfo object1, ResolveInfo object2) {
117 return object1.loadLabel(mPackageManager).toString().compareTo(
118 object2.loadLabel(mPackageManager).toString());
119 }
120 };
121
122 // get the list of live folder intents
123 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
124 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
125
126 // manually create a separate entry for creating a folder in Launcher
127 ResolveInfo folder = new ResolveInfo();
128 folder.icon = R.drawable.ic_launcher_folder;
129 folder.labelRes = R.string.group_folder;
130 folder.resolvePackageName = context.getPackageName();
131 mFolderList.add(0, folder);
132 Collections.sort(mFolderList, resolveInfoComparator);
133
134 // get the list of shortcuts
135 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
136 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
137 Collections.sort(mShortcutList, resolveInfoComparator);
138
Winson Chung241c3b42010-08-25 16:53:03 -0700139 // reset the icon cache
140 mPageViewIconCache.clear();
141
Winson Chung80baf5a2010-08-09 16:03:15 -0700142 invalidatePageData();
143 }
144
145 public void setDragController(DragController dragger) {
146 mDragController = dragger;
147 }
148
149 public void setCustomizationFilter(CustomizationType filterType) {
150 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700151 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700152 invalidatePageData();
153 }
154
155 @Override
156 public void onDropCompleted(View target, boolean success) {
157 // do nothing
158 }
159
160 @Override
161 public boolean onLongClick(View v) {
162 if (!v.isInTouchMode()) {
163 return false;
164 }
165
166 final View animView = v;
167 switch (mCustomizationType) {
168 case WidgetCustomization:
Michael Jurkaa63c4522010-08-19 13:52:27 -0700169 // We assume that the view v is a TextView with a compound drawable on top, and that the
170 // whole text view is centered horizontally and top aligned. We get a more precise
171 // drag point using this information
172 final TextView textView = (TextView) animView;
173 final Drawable[] drawables = textView.getCompoundDrawables();
174 final Drawable icon = drawables[1];
175 int dragPointOffsetX = 0;
176 int dragPointOffsetY = 0;
177 Rect bounds = null;
178 if (icon != null) {
179 bounds = icon.getBounds();
180 bounds.left = (v.getWidth() - bounds.right) / 2;
181 bounds.right += bounds.left;
182 }
183
Winson Chung80baf5a2010-08-09 16:03:15 -0700184 AppWidgetProviderInfo appWidgetInfo = (AppWidgetProviderInfo) v.getTag();
185 LauncherAppWidgetInfo dragInfo = new LauncherAppWidgetInfo(appWidgetInfo.provider);
186 dragInfo.minWidth = appWidgetInfo.minWidth;
187 dragInfo.minHeight = appWidgetInfo.minHeight;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700188 mDragController.startDrag(v, this, dragInfo, DragController.DRAG_ACTION_COPY, bounds);
Winson Chung80baf5a2010-08-09 16:03:15 -0700189 return true;
190 case FolderCustomization:
191 // animate some feedback to the long press
192 animateClickFeedback(v, new Runnable() {
193 @Override
194 public void run() {
195 // add the folder
196 ResolveInfo resolveInfo = (ResolveInfo) animView.getTag();
197 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
198 if (resolveInfo.labelRes == R.string.group_folder) {
199 // Create app shortcuts is a special built-in case of shortcuts
200 createFolderIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
201 getContext().getString(R.string.group_folder));
202 } else {
203 ComponentName name = new ComponentName(resolveInfo.activityInfo.packageName,
204 resolveInfo.activityInfo.name);
205 createFolderIntent.setComponent(name);
206 }
207 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
208 mLauncher.addLiveFolder(createFolderIntent);
209 }
210 });
211 return true;
212 case ShortcutCustomization:
213 // animate some feedback to the long press
214 animateClickFeedback(v, new Runnable() {
215 @Override
216 public void run() {
217 // add the shortcut
218 ResolveInfo info = (ResolveInfo) animView.getTag();
219 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
220 if (info.labelRes == R.string.group_applications) {
221 // Create app shortcuts is a special built-in case of shortcuts
222 createShortcutIntent.putExtra(
223 Intent.EXTRA_SHORTCUT_NAME,getContext().getString(
224 R.string.group_applications));
225 } else {
226 ComponentName name = new ComponentName(info.activityInfo.packageName,
227 info.activityInfo.name);
228 createShortcutIntent.setComponent(name);
229 }
230 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
231 mLauncher.processShortcut(createShortcutIntent);
232 }
233 });
234 return true;
235 }
236 return false;
237 }
238
239 private int relayoutWidgets() {
240 final int widgetCount = mWidgetList.size();
241 if (widgetCount == 0) return 0;
242
243 mWidgetPages.clear();
244 ArrayList<PagedViewCellLayout.LayoutParams> page =
245 new ArrayList<PagedViewCellLayout.LayoutParams>();
246 mWidgetPages.add(page);
247 int rowOffsetX = 0;
248 int rowOffsetY = 0;
249 int curRowHeight = 0;
250 // we only get the cell dims this way for the layout calculations because
251 // we know that we aren't going to change the dims when we construct it
252 // afterwards
253 for (int i = 0; i < widgetCount; ++i) {
254 AppWidgetProviderInfo info = mWidgetList.get(i);
255 PagedViewCellLayout.LayoutParams params;
256
257 final int cellSpanX = mTmpWidgetLayout.estimateCellHSpan(info.minWidth);
258 final int cellSpanY = mTmpWidgetLayout.estimateCellVSpan(info.minHeight);
259
260 if (((rowOffsetX + cellSpanX) <= mCellCountX) &&
261 ((rowOffsetY + cellSpanY) <= mCellCountY)) {
262 // just add to end of current row
263 params = new PagedViewCellLayout.LayoutParams(rowOffsetX, rowOffsetY,
264 cellSpanX, cellSpanY);
265
266 rowOffsetX += cellSpanX;
267 curRowHeight = Math.max(curRowHeight, cellSpanY);
268 } else {
269 /*
270 // fix all the items in this last row to be bottom aligned
271 int prevRowOffsetX = rowOffsetX;
272 for (int j = page.size() - 1; j >= 0; --j) {
273 PagedViewCellLayout.LayoutParams params = page.get(j);
274 // skip once we get to the previous row
275 if (params.cellX > prevRowOffsetX)
276 break;
277
278 params.cellY += curRowHeight - params.cellVSpan;
279 prevRowOffsetX = params.cellX;
280 }
281 */
282
283 // doesn't fit on current row, see if we can start a new row on
284 // this page
285 if ((rowOffsetY + curRowHeight + cellSpanY) > mCellCountY) {
286 // start a new page and add this item to it
287 page = new ArrayList<PagedViewCellLayout.LayoutParams>();
288 mWidgetPages.add(page);
289
290 params = new PagedViewCellLayout.LayoutParams(0, 0, cellSpanX, cellSpanY);
291 rowOffsetX = cellSpanX;
292 rowOffsetY = 0;
293 curRowHeight = cellSpanY;
294 } else {
295 // add it to the current page on this new row
296 params = new PagedViewCellLayout.LayoutParams(0, rowOffsetY + curRowHeight,
297 cellSpanX, cellSpanY);
298
299 rowOffsetX = cellSpanX;
300 rowOffsetY += curRowHeight;
301 curRowHeight = cellSpanY;
302 }
303 }
304
305 params.setTag(info);
306 page.add(params);
307 }
308 return mWidgetPages.size();
309 }
310
311 private Drawable getWidgetIcon(PagedViewCellLayout.LayoutParams params,
312 AppWidgetProviderInfo info) {
313 PackageManager packageManager = mLauncher.getPackageManager();
314 String packageName = info.provider.getPackageName();
315 Drawable drawable = null;
316 if (info.previewImage != 0) {
317 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
318 if (drawable == null) {
319 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
320 + " for provider: " + info.provider);
321 } else {
322 return drawable;
323 }
324 }
325
326 // If we don't have a preview image, create a default one
327 if (drawable == null) {
328 Resources resources = mLauncher.getResources();
329
330 // Determine the size the widget will take in the layout
331 // Create a new bitmap to hold the widget preview
332 int[] dims = mTmpWidgetLayout.estimateCellDimensions(getMeasuredWidth(),
333 getMeasuredHeight(), params.cellHSpan, params.cellVSpan);
334 final int width = dims[0];
335 final int height = dims[1] - 35;
336 // TEMP
337 // TEMP: HACK ABOVE TO GET TEXT TO SHOW
338 // TEMP
339 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
340 mCanvas.setBitmap(bitmap);
341 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
342 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
343
344 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
345 background.setBounds(0, 0, width, height);
346 background.draw(mCanvas);
347
348 // Draw the icon vertically centered, flush left
349 try {
350 Rect tmpRect = new Rect();
351 Drawable icon = null;
352 if (info.icon != 0) {
353 icon = packageManager.getDrawable(packageName, info.icon, null);
354 } else {
355 icon = resources.getDrawable(R.drawable.ic_launcher_application);
356 }
357 background.getPadding(tmpRect);
358
359 final int iconSize = Math.min(
360 Math.min(icon.getIntrinsicWidth(), width - tmpRect.left - tmpRect.right),
361 Math.min(icon.getIntrinsicHeight(), height - tmpRect.top - tmpRect.bottom));
362 final int left = (width / 2) - (iconSize / 2);
363 final int top = (height / 2) - (iconSize / 2);
364 icon.setBounds(new Rect(left, top, left + iconSize, top + iconSize));
365 icon.draw(mCanvas);
366 } catch (Resources.NotFoundException e) {
367 // if we can't find the icon, then just don't draw it
368 }
369
Winson Chungb3347bb2010-08-19 14:51:28 -0700370 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700371 }
372 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
373 return drawable;
374 }
375
376 private void setupPage(PagedViewCellLayout layout) {
377 layout.setCellCount(mCellCountX, mCellCountY);
378 layout.setPadding(20, 10, 20, 0);
379 }
380
381 private void syncWidgetPages() {
382 if (mWidgetList == null) return;
383
384 // calculate the layout for all the widget pages first and ensure that
385 // we have the right number of pages
386 int numPages = relayoutWidgets();
387 int curNumPages = getChildCount();
388 // remove any extra pages after the "last" page
389 int extraPageDiff = curNumPages - numPages;
390 for (int i = 0; i < extraPageDiff; ++i) {
391 removeViewAt(numPages);
392 }
393 // add any necessary pages
394 for (int i = curNumPages; i < numPages; ++i) {
395 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
396 setupPage(layout);
397 addView(layout);
398 }
399 }
400
401 private void syncWidgetPageItems(int page) {
402 // ensure that we have the right number of items on the pages
403 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
404 final ArrayList<PagedViewCellLayout.LayoutParams> list = mWidgetPages.get(page);
405 final int count = list.size();
406 layout.removeAllViews();
407 for (int i = 0; i < count; ++i) {
408 PagedViewCellLayout.LayoutParams params = list.get(i);
409 AppWidgetProviderInfo info = (AppWidgetProviderInfo) params.getTag();
410 final Drawable icon = getWidgetIcon(params, info);
411 TextView text = (TextView) mInflater.inflate(R.layout.customize_paged_view_widget,
412 layout, false);
413 text.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
414 text.setText(info.label);
415 text.setTag(info);
416 text.setOnLongClickListener(this);
417
418 layout.addViewToCellLayout(text, -1, mWidgetList.indexOf(info), params);
419 }
420 }
421
422 private void syncListPages(List<ResolveInfo> list) {
423 // ensure that we have the right number of pages
424 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
425 int curNumPages = getChildCount();
426 // remove any extra pages after the "last" page
427 int extraPageDiff = curNumPages - numPages;
428 for (int i = 0; i < extraPageDiff; ++i) {
429 removeViewAt(numPages);
430 }
431 // add any necessary pages
432 for (int i = curNumPages; i < numPages; ++i) {
433 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
434 setupPage(layout);
435 addView(layout);
436 }
437 }
438
439 private void syncListPageItems(int page, List<ResolveInfo> list) {
440 // ensure that we have the right number of items on the pages
441 int numCells = mCellCountX * mCellCountY;
442 int startIndex = page * numCells;
443 int endIndex = Math.min(startIndex + numCells, list.size());
444 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
445 // TODO: we can optimize by just re-applying to existing views
446 layout.removeAllViews();
447 for (int i = startIndex; i < endIndex; ++i) {
448 ResolveInfo info = list.get(i);
Winson Chung241c3b42010-08-25 16:53:03 -0700449 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
450 R.layout.customize_paged_view_item, layout, false);
451 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
452 icon.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700453
454 final int index = i - startIndex;
455 final int x = index % mCellCountX;
456 final int y = index / mCellCountX;
Winson Chung241c3b42010-08-25 16:53:03 -0700457 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700458 }
459 }
460
461 private void syncWallpaperPages() {
462 // NOT CURRENTLY IMPLEMENTED
463 // ensure that we have the right number of pages
464 int numPages = 1;
465 int curNumPages = getChildCount();
466 // remove any extra pages after the "last" page
467 int extraPageDiff = curNumPages - numPages;
468 for (int i = 0; i < extraPageDiff; ++i) {
469 removeViewAt(numPages);
470 }
471 // add any necessary pages
472 for (int i = curNumPages; i < numPages; ++i) {
473 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
474 setupPage(layout);
475 addView(layout);
476 }
477 }
478
479 private void syncWallpaperPageItems(int page) {
480 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
481 layout.removeAllViews();
482
483 TextView text = (TextView) mInflater.inflate(
484 R.layout.customize_paged_view_wallpaper_placeholder, layout, false);
485 // NOTE: this is just place holder text until MikeJurka implements wallpaper picker
486 text.setText("Wallpaper customization coming soon!");
487
488 layout.addViewToCellLayout(text, -1, 0, new PagedViewCellLayout.LayoutParams(0, 0, 3, 1));
489 }
490
491 @Override
492 public void syncPages() {
493 switch (mCustomizationType) {
494 case WidgetCustomization:
495 syncWidgetPages();
496 break;
497 case FolderCustomization:
498 syncListPages(mFolderList);
499 break;
500 case ShortcutCustomization:
501 syncListPages(mShortcutList);
502 break;
503 case WallpaperCustomization:
504 syncWallpaperPages();
505 break;
506 default:
507 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700508 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700509 break;
510 }
511
512 // only try and center the page if there is one page
513 final int childCount = getChildCount();
514 if (childCount == 1) {
515 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
516 layout.enableCenteredContent(true);
517 } else {
518 for (int i = 0; i < childCount; ++i) {
519 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
520 layout.enableCenteredContent(false);
521 }
522 }
523
524 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700525 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700526 }
527
528 @Override
529 public void syncPageItems(int page) {
530 switch (mCustomizationType) {
531 case WidgetCustomization:
532 syncWidgetPageItems(page);
533 break;
534 case FolderCustomization:
535 syncListPageItems(page, mFolderList);
536 break;
537 case ShortcutCustomization:
538 syncListPageItems(page, mShortcutList);
539 break;
540 case WallpaperCustomization:
541 syncWallpaperPageItems(page);
542 break;
543 }
544 }
545}