blob: ef619be4d3b52c81c06bf5d07c56e71c6a1d10e1 [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:
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);
449 Drawable image = info.loadIcon(mPackageManager);
450 TextView text = (TextView) mInflater.inflate(R.layout.customize_paged_view_item,
451 layout, false);
452 image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
453 text.setCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
454 text.setText(info.loadLabel(mPackageManager));
455 text.setTag(info);
456 text.setOnLongClickListener(this);
457
458 final int index = i - startIndex;
459 final int x = index % mCellCountX;
460 final int y = index / mCellCountX;
461 layout.addViewToCellLayout(text, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
462 }
463 }
464
465 private void syncWallpaperPages() {
466 // NOT CURRENTLY IMPLEMENTED
467 // ensure that we have the right number of pages
468 int numPages = 1;
469 int curNumPages = getChildCount();
470 // remove any extra pages after the "last" page
471 int extraPageDiff = curNumPages - numPages;
472 for (int i = 0; i < extraPageDiff; ++i) {
473 removeViewAt(numPages);
474 }
475 // add any necessary pages
476 for (int i = curNumPages; i < numPages; ++i) {
477 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
478 setupPage(layout);
479 addView(layout);
480 }
481 }
482
483 private void syncWallpaperPageItems(int page) {
484 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
485 layout.removeAllViews();
486
487 TextView text = (TextView) mInflater.inflate(
488 R.layout.customize_paged_view_wallpaper_placeholder, layout, false);
489 // NOTE: this is just place holder text until MikeJurka implements wallpaper picker
490 text.setText("Wallpaper customization coming soon!");
491
492 layout.addViewToCellLayout(text, -1, 0, new PagedViewCellLayout.LayoutParams(0, 0, 3, 1));
493 }
494
495 @Override
496 public void syncPages() {
497 switch (mCustomizationType) {
498 case WidgetCustomization:
499 syncWidgetPages();
500 break;
501 case FolderCustomization:
502 syncListPages(mFolderList);
503 break;
504 case ShortcutCustomization:
505 syncListPages(mShortcutList);
506 break;
507 case WallpaperCustomization:
508 syncWallpaperPages();
509 break;
510 default:
511 removeAllViews();
512 setCurrentScreen(0);
513 break;
514 }
515
516 // only try and center the page if there is one page
517 final int childCount = getChildCount();
518 if (childCount == 1) {
519 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
520 layout.enableCenteredContent(true);
521 } else {
522 for (int i = 0; i < childCount; ++i) {
523 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
524 layout.enableCenteredContent(false);
525 }
526 }
527
528 // bound the current page
529 setCurrentScreen(Math.max(0, Math.min(childCount - 1, getCurrentScreen())));
530 }
531
532 @Override
533 public void syncPageItems(int page) {
534 switch (mCustomizationType) {
535 case WidgetCustomization:
536 syncWidgetPageItems(page);
537 break;
538 case FolderCustomization:
539 syncListPageItems(page, mFolderList);
540 break;
541 case ShortcutCustomization:
542 syncListPageItems(page, mShortcutList);
543 break;
544 case WallpaperCustomization:
545 syncWallpaperPageItems(page);
546 break;
547 }
548 }
549}