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