blob: 44884972823ac593bfcccb7f331c83d573ab3c23 [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
139 invalidatePageData();
140 }
141
142 public void setDragController(DragController dragger) {
143 mDragController = dragger;
144 }
145
146 public void setCustomizationFilter(CustomizationType filterType) {
147 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700148 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700149 invalidatePageData();
150 }
151
152 @Override
153 public void onDropCompleted(View target, boolean success) {
154 // do nothing
155 }
156
157 @Override
158 public boolean onLongClick(View v) {
159 if (!v.isInTouchMode()) {
160 return false;
161 }
162
163 final View animView = v;
164 switch (mCustomizationType) {
165 case WidgetCustomization:
Michael Jurkaa63c4522010-08-19 13:52:27 -0700166 // We assume that the view v is a TextView with a compound drawable on top, and that the
167 // whole text view is centered horizontally and top aligned. We get a more precise
168 // drag point using this information
169 final TextView textView = (TextView) animView;
170 final Drawable[] drawables = textView.getCompoundDrawables();
171 final Drawable icon = drawables[1];
172 int dragPointOffsetX = 0;
173 int dragPointOffsetY = 0;
174 Rect bounds = null;
175 if (icon != null) {
176 bounds = icon.getBounds();
177 bounds.left = (v.getWidth() - bounds.right) / 2;
178 bounds.right += bounds.left;
179 }
180
Winson Chung80baf5a2010-08-09 16:03:15 -0700181 AppWidgetProviderInfo appWidgetInfo = (AppWidgetProviderInfo) v.getTag();
182 LauncherAppWidgetInfo dragInfo = new LauncherAppWidgetInfo(appWidgetInfo.provider);
183 dragInfo.minWidth = appWidgetInfo.minWidth;
184 dragInfo.minHeight = appWidgetInfo.minHeight;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700185 mDragController.startDrag(v, this, dragInfo, DragController.DRAG_ACTION_COPY, bounds);
Winson Chung80baf5a2010-08-09 16:03:15 -0700186 return true;
187 case FolderCustomization:
188 // animate some feedback to the long press
189 animateClickFeedback(v, new Runnable() {
190 @Override
191 public void run() {
192 // add the folder
193 ResolveInfo resolveInfo = (ResolveInfo) animView.getTag();
194 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
195 if (resolveInfo.labelRes == R.string.group_folder) {
196 // Create app shortcuts is a special built-in case of shortcuts
197 createFolderIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
198 getContext().getString(R.string.group_folder));
199 } else {
200 ComponentName name = new ComponentName(resolveInfo.activityInfo.packageName,
201 resolveInfo.activityInfo.name);
202 createFolderIntent.setComponent(name);
203 }
204 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
205 mLauncher.addLiveFolder(createFolderIntent);
206 }
207 });
208 return true;
209 case ShortcutCustomization:
210 // animate some feedback to the long press
211 animateClickFeedback(v, new Runnable() {
212 @Override
213 public void run() {
214 // add the shortcut
215 ResolveInfo info = (ResolveInfo) animView.getTag();
216 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
217 if (info.labelRes == R.string.group_applications) {
218 // Create app shortcuts is a special built-in case of shortcuts
219 createShortcutIntent.putExtra(
220 Intent.EXTRA_SHORTCUT_NAME,getContext().getString(
221 R.string.group_applications));
222 } else {
223 ComponentName name = new ComponentName(info.activityInfo.packageName,
224 info.activityInfo.name);
225 createShortcutIntent.setComponent(name);
226 }
227 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
228 mLauncher.processShortcut(createShortcutIntent);
229 }
230 });
231 return true;
232 }
233 return false;
234 }
235
236 private int relayoutWidgets() {
237 final int widgetCount = mWidgetList.size();
238 if (widgetCount == 0) return 0;
239
240 mWidgetPages.clear();
241 ArrayList<PagedViewCellLayout.LayoutParams> page =
242 new ArrayList<PagedViewCellLayout.LayoutParams>();
243 mWidgetPages.add(page);
244 int rowOffsetX = 0;
245 int rowOffsetY = 0;
246 int curRowHeight = 0;
247 // we only get the cell dims this way for the layout calculations because
248 // we know that we aren't going to change the dims when we construct it
249 // afterwards
250 for (int i = 0; i < widgetCount; ++i) {
251 AppWidgetProviderInfo info = mWidgetList.get(i);
252 PagedViewCellLayout.LayoutParams params;
253
254 final int cellSpanX = mTmpWidgetLayout.estimateCellHSpan(info.minWidth);
255 final int cellSpanY = mTmpWidgetLayout.estimateCellVSpan(info.minHeight);
256
257 if (((rowOffsetX + cellSpanX) <= mCellCountX) &&
258 ((rowOffsetY + cellSpanY) <= mCellCountY)) {
259 // just add to end of current row
260 params = new PagedViewCellLayout.LayoutParams(rowOffsetX, rowOffsetY,
261 cellSpanX, cellSpanY);
262
263 rowOffsetX += cellSpanX;
264 curRowHeight = Math.max(curRowHeight, cellSpanY);
265 } else {
266 /*
267 // fix all the items in this last row to be bottom aligned
268 int prevRowOffsetX = rowOffsetX;
269 for (int j = page.size() - 1; j >= 0; --j) {
270 PagedViewCellLayout.LayoutParams params = page.get(j);
271 // skip once we get to the previous row
272 if (params.cellX > prevRowOffsetX)
273 break;
274
275 params.cellY += curRowHeight - params.cellVSpan;
276 prevRowOffsetX = params.cellX;
277 }
278 */
279
280 // doesn't fit on current row, see if we can start a new row on
281 // this page
282 if ((rowOffsetY + curRowHeight + cellSpanY) > mCellCountY) {
283 // start a new page and add this item to it
284 page = new ArrayList<PagedViewCellLayout.LayoutParams>();
285 mWidgetPages.add(page);
286
287 params = new PagedViewCellLayout.LayoutParams(0, 0, cellSpanX, cellSpanY);
288 rowOffsetX = cellSpanX;
289 rowOffsetY = 0;
290 curRowHeight = cellSpanY;
291 } else {
292 // add it to the current page on this new row
293 params = new PagedViewCellLayout.LayoutParams(0, rowOffsetY + curRowHeight,
294 cellSpanX, cellSpanY);
295
296 rowOffsetX = cellSpanX;
297 rowOffsetY += curRowHeight;
298 curRowHeight = cellSpanY;
299 }
300 }
301
302 params.setTag(info);
303 page.add(params);
304 }
305 return mWidgetPages.size();
306 }
307
308 private Drawable getWidgetIcon(PagedViewCellLayout.LayoutParams params,
309 AppWidgetProviderInfo info) {
310 PackageManager packageManager = mLauncher.getPackageManager();
311 String packageName = info.provider.getPackageName();
312 Drawable drawable = null;
313 if (info.previewImage != 0) {
314 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
315 if (drawable == null) {
316 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
317 + " for provider: " + info.provider);
318 } else {
319 return drawable;
320 }
321 }
322
323 // If we don't have a preview image, create a default one
324 if (drawable == null) {
325 Resources resources = mLauncher.getResources();
326
327 // Determine the size the widget will take in the layout
328 // Create a new bitmap to hold the widget preview
329 int[] dims = mTmpWidgetLayout.estimateCellDimensions(getMeasuredWidth(),
330 getMeasuredHeight(), params.cellHSpan, params.cellVSpan);
331 final int width = dims[0];
332 final int height = dims[1] - 35;
333 // TEMP
334 // TEMP: HACK ABOVE TO GET TEXT TO SHOW
335 // TEMP
336 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
337 mCanvas.setBitmap(bitmap);
338 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
339 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
340
341 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
342 background.setBounds(0, 0, width, height);
343 background.draw(mCanvas);
344
345 // Draw the icon vertically centered, flush left
346 try {
347 Rect tmpRect = new Rect();
348 Drawable icon = null;
349 if (info.icon != 0) {
350 icon = packageManager.getDrawable(packageName, info.icon, null);
351 } else {
352 icon = resources.getDrawable(R.drawable.ic_launcher_application);
353 }
354 background.getPadding(tmpRect);
355
356 final int iconSize = Math.min(
357 Math.min(icon.getIntrinsicWidth(), width - tmpRect.left - tmpRect.right),
358 Math.min(icon.getIntrinsicHeight(), height - tmpRect.top - tmpRect.bottom));
359 final int left = (width / 2) - (iconSize / 2);
360 final int top = (height / 2) - (iconSize / 2);
361 icon.setBounds(new Rect(left, top, left + iconSize, top + iconSize));
362 icon.draw(mCanvas);
363 } catch (Resources.NotFoundException e) {
364 // if we can't find the icon, then just don't draw it
365 }
366
Winson Chungb3347bb2010-08-19 14:51:28 -0700367 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700368 }
369 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
370 return drawable;
371 }
372
373 private void setupPage(PagedViewCellLayout layout) {
374 layout.setCellCount(mCellCountX, mCellCountY);
375 layout.setPadding(20, 10, 20, 0);
376 }
377
378 private void syncWidgetPages() {
379 if (mWidgetList == null) return;
380
381 // calculate the layout for all the widget pages first and ensure that
382 // we have the right number of pages
383 int numPages = relayoutWidgets();
384 int curNumPages = getChildCount();
385 // remove any extra pages after the "last" page
386 int extraPageDiff = curNumPages - numPages;
387 for (int i = 0; i < extraPageDiff; ++i) {
388 removeViewAt(numPages);
389 }
390 // add any necessary pages
391 for (int i = curNumPages; i < numPages; ++i) {
392 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
393 setupPage(layout);
394 addView(layout);
395 }
396 }
397
398 private void syncWidgetPageItems(int page) {
399 // ensure that we have the right number of items on the pages
400 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
401 final ArrayList<PagedViewCellLayout.LayoutParams> list = mWidgetPages.get(page);
402 final int count = list.size();
403 layout.removeAllViews();
404 for (int i = 0; i < count; ++i) {
405 PagedViewCellLayout.LayoutParams params = list.get(i);
406 AppWidgetProviderInfo info = (AppWidgetProviderInfo) params.getTag();
407 final Drawable icon = getWidgetIcon(params, info);
408 TextView text = (TextView) mInflater.inflate(R.layout.customize_paged_view_widget,
409 layout, false);
410 text.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
411 text.setText(info.label);
412 text.setTag(info);
413 text.setOnLongClickListener(this);
414
415 layout.addViewToCellLayout(text, -1, mWidgetList.indexOf(info), params);
416 }
417 }
418
419 private void syncListPages(List<ResolveInfo> list) {
420 // ensure that we have the right number of pages
421 int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY));
422 int curNumPages = getChildCount();
423 // remove any extra pages after the "last" page
424 int extraPageDiff = curNumPages - numPages;
425 for (int i = 0; i < extraPageDiff; ++i) {
426 removeViewAt(numPages);
427 }
428 // add any necessary pages
429 for (int i = curNumPages; i < numPages; ++i) {
430 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
431 setupPage(layout);
432 addView(layout);
433 }
434 }
435
436 private void syncListPageItems(int page, List<ResolveInfo> list) {
437 // ensure that we have the right number of items on the pages
438 int numCells = mCellCountX * mCellCountY;
439 int startIndex = page * numCells;
440 int endIndex = Math.min(startIndex + numCells, list.size());
441 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
442 // TODO: we can optimize by just re-applying to existing views
443 layout.removeAllViews();
444 for (int i = startIndex; i < endIndex; ++i) {
445 ResolveInfo info = list.get(i);
446 Drawable image = info.loadIcon(mPackageManager);
447 TextView text = (TextView) mInflater.inflate(R.layout.customize_paged_view_item,
448 layout, false);
449 image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
450 text.setCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
451 text.setText(info.loadLabel(mPackageManager));
452 text.setTag(info);
453 text.setOnLongClickListener(this);
454
455 final int index = i - startIndex;
456 final int x = index % mCellCountX;
457 final int y = index / mCellCountX;
458 layout.addViewToCellLayout(text, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
459 }
460 }
461
462 private void syncWallpaperPages() {
463 // NOT CURRENTLY IMPLEMENTED
464 // ensure that we have the right number of pages
465 int numPages = 1;
466 int curNumPages = getChildCount();
467 // remove any extra pages after the "last" page
468 int extraPageDiff = curNumPages - numPages;
469 for (int i = 0; i < extraPageDiff; ++i) {
470 removeViewAt(numPages);
471 }
472 // add any necessary pages
473 for (int i = curNumPages; i < numPages; ++i) {
474 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
475 setupPage(layout);
476 addView(layout);
477 }
478 }
479
480 private void syncWallpaperPageItems(int page) {
481 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
482 layout.removeAllViews();
483
484 TextView text = (TextView) mInflater.inflate(
485 R.layout.customize_paged_view_wallpaper_placeholder, layout, false);
486 // NOTE: this is just place holder text until MikeJurka implements wallpaper picker
487 text.setText("Wallpaper customization coming soon!");
488
489 layout.addViewToCellLayout(text, -1, 0, new PagedViewCellLayout.LayoutParams(0, 0, 3, 1));
490 }
491
492 @Override
493 public void syncPages() {
494 switch (mCustomizationType) {
495 case WidgetCustomization:
496 syncWidgetPages();
497 break;
498 case FolderCustomization:
499 syncListPages(mFolderList);
500 break;
501 case ShortcutCustomization:
502 syncListPages(mShortcutList);
503 break;
504 case WallpaperCustomization:
505 syncWallpaperPages();
506 break;
507 default:
508 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700509 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700510 break;
511 }
512
513 // only try and center the page if there is one page
514 final int childCount = getChildCount();
515 if (childCount == 1) {
516 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
517 layout.enableCenteredContent(true);
518 } else {
519 for (int i = 0; i < childCount; ++i) {
520 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
521 layout.enableCenteredContent(false);
522 }
523 }
524
525 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700526 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700527 }
528
529 @Override
530 public void syncPageItems(int page) {
531 switch (mCustomizationType) {
532 case WidgetCustomization:
533 syncWidgetPageItems(page);
534 break;
535 case FolderCustomization:
536 syncListPageItems(page, mFolderList);
537 break;
538 case ShortcutCustomization:
539 syncListPageItems(page, mShortcutList);
540 break;
541 case WallpaperCustomization:
542 syncWallpaperPageItems(page);
543 break;
544 }
545 }
546}