blob: 6e7f9b6b866a8411e51aedbbc29f06761a0db995 [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;
Winson Chunge3193b92010-09-10 11:44:42 -070031import android.content.res.Configuration;
Winson Chung80baf5a2010-08-09 16:03:15 -070032import android.content.res.Resources;
Winson Chunge3193b92010-09-10 11:44:42 -070033import android.content.res.TypedArray;
Winson Chung80baf5a2010-08-09 16:03:15 -070034import android.graphics.Bitmap;
Winson Chung80baf5a2010-08-09 16:03:15 -070035import android.graphics.Bitmap.Config;
Winson Chung86f77532010-08-24 11:08:22 -070036import android.graphics.Canvas;
37import 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;
40import android.provider.LiveFolders;
41import android.util.AttributeSet;
42import android.util.Log;
Winson Chunge3193b92010-09-10 11:44:42 -070043import android.view.Gravity;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.view.LayoutInflater;
Winson Chunge3193b92010-09-10 11:44:42 -070045import android.view.MotionEvent;
Winson Chung80baf5a2010-08-09 16:03:15 -070046import android.view.View;
Winson Chunge3193b92010-09-10 11:44:42 -070047import android.view.ViewGroup;
48import android.widget.ImageView;
49import android.widget.LinearLayout;
Winson Chung80baf5a2010-08-09 16:03:15 -070050import android.widget.TextView;
51
52import com.android.launcher.R;
53
54public class CustomizePagedView extends PagedView
Winson Chunge8878e32010-09-15 20:37:09 -070055 implements View.OnLongClickListener, View.OnClickListener,
Winson Chung80baf5a2010-08-09 16:03:15 -070056 DragSource {
57
58 public enum CustomizationType {
59 WidgetCustomization,
60 FolderCustomization,
61 ShortcutCustomization,
62 WallpaperCustomization
63 }
64
Winson Chunge3193b92010-09-10 11:44:42 -070065 /**
66 * The linear layout used strictly for the widget tab of the customization tray
67 */
68 private class WidgetLayout extends LinearLayout {
69 public WidgetLayout(Context context) {
70 super(context);
71 }
72
73 @Override
74 public boolean onTouchEvent(MotionEvent event) {
75 // We eat up the touch events here, since the PagedView (which uses the same swiping
76 // touch code as Workspace previously) uses onInterceptTouchEvent() to determine when
77 // the user is scrolling between pages. This means that if the pages themselves don't
78 // handle touch events, it gets forwarded up to PagedView itself, and it's own
79 // onTouchEvent() handling will prevent further intercept touch events from being called
80 // (it's the same view in that case). This is not ideal, but to prevent more changes,
81 // we just always mark the touch event as handled.
82 return super.onTouchEvent(event) || true;
83 }
84 }
85
Winson Chung80baf5a2010-08-09 16:03:15 -070086 private static final String TAG = "CustomizeWorkspace";
87 private static final boolean DEBUG = false;
88
89 private Launcher mLauncher;
90 private DragController mDragController;
91 private PackageManager mPackageManager;
92
93 private CustomizationType mCustomizationType;
94
Winson Chunge3193b92010-09-10 11:44:42 -070095 // The layout used to emulate the workspace in resolve the cell dimensions of a widget
96 private PagedViewCellLayout mWorkspaceWidgetLayout;
97
98 // The mapping between the pages and the widgets that will be laid out on them
99 private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages;
100
101 // The max dimensions for the ImageView we use for displaying the widget
102 private int mMaxWidgetWidth;
103
104 // The max number of widget cells to take a "page" of widget
105 private int mMaxWidgetsCellHSpan;
106
107 // The raw sources of data for each of the different tabs of the customization page
Winson Chung80baf5a2010-08-09 16:03:15 -0700108 private List<AppWidgetProviderInfo> mWidgetList;
109 private List<ResolveInfo> mFolderList;
110 private List<ResolveInfo> mShortcutList;
Winson Chunge8878e32010-09-15 20:37:09 -0700111 private List<ResolveInfo> mWallpaperList;
Winson Chung80baf5a2010-08-09 16:03:15 -0700112
Winson Chunge3193b92010-09-10 11:44:42 -0700113 private static final int sCellCountX = 8;
114 private static final int sCellCountY = 4;
115 private static final int sMinWidgetCellHSpan = 2;
116 private static final int sMaxWidgetCellHSpan = 4;
117
118 // The scale factor for widget previews inside the widget drawer
119 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700120
121 private final Canvas mCanvas = new Canvas();
122 private final LayoutInflater mInflater;
123
124 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700125 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700126 }
127
128 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700129 this(context, attrs, 0);
130 }
131
132 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
133 super(context, attrs, defStyle);
134
135 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
136 defStyle, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700137 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700138 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
139 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700140 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700141 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
142 a.recycle();
143
Winson Chung80baf5a2010-08-09 16:03:15 -0700144 setVisibility(View.GONE);
145 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700146 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700147 }
148
149 public void setLauncher(Launcher launcher) {
150 Context context = getContext();
151 mLauncher = launcher;
152 mPackageManager = context.getPackageManager();
153 }
154
155 public void update() {
156 Context context = getContext();
157
158 // get the list of widgets
159 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
160 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
161 @Override
162 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
163 return object1.label.compareTo(object2.label);
164 }
165 });
166
167 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
168 @Override
169 public int compare(ResolveInfo object1, ResolveInfo object2) {
170 return object1.loadLabel(mPackageManager).toString().compareTo(
171 object2.loadLabel(mPackageManager).toString());
172 }
173 };
174
175 // get the list of live folder intents
176 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
177 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
178
179 // manually create a separate entry for creating a folder in Launcher
180 ResolveInfo folder = new ResolveInfo();
181 folder.icon = R.drawable.ic_launcher_folder;
182 folder.labelRes = R.string.group_folder;
183 folder.resolvePackageName = context.getPackageName();
184 mFolderList.add(0, folder);
185 Collections.sort(mFolderList, resolveInfoComparator);
186
187 // get the list of shortcuts
188 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
189 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
190 Collections.sort(mShortcutList, resolveInfoComparator);
191
Winson Chunge8878e32010-09-15 20:37:09 -0700192 // get the list of wallpapers
193 Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
194 mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0);
195 Collections.sort(mWallpaperList, resolveInfoComparator);
196
Winson Chung241c3b42010-08-25 16:53:03 -0700197 // reset the icon cache
198 mPageViewIconCache.clear();
199
Winson Chunge3193b92010-09-10 11:44:42 -0700200 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700201 invalidatePageData();
202 }
203
204 public void setDragController(DragController dragger) {
205 mDragController = dragger;
206 }
207
208 public void setCustomizationFilter(CustomizationType filterType) {
209 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700210 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700211 invalidatePageData();
212 }
213
214 @Override
215 public void onDropCompleted(View target, boolean success) {
216 // do nothing
217 }
218
219 @Override
Winson Chunge8878e32010-09-15 20:37:09 -0700220 public void onClick(View v) {
221 if (!v.isInTouchMode()) {
222 return;
223 }
224
225 final View animView = v;
226 switch (mCustomizationType) {
227 case WallpaperCustomization:
228 // animate some feedback to the long press
229 animateClickFeedback(v, new Runnable() {
230 @Override
231 public void run() {
232 // add the shortcut
233 ResolveInfo info = (ResolveInfo) animView.getTag();
234 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
235 if (info.labelRes == R.string.group_applications) {
236 // Create app shortcuts is a special built-in case of shortcuts
237 createShortcutIntent.putExtra(
238 Intent.EXTRA_SHORTCUT_NAME,getContext().getString(
239 R.string.group_applications));
240 } else {
241 ComponentName name = new ComponentName(info.activityInfo.packageName,
242 info.activityInfo.name);
243 createShortcutIntent.setComponent(name);
244 }
245 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
246 mLauncher.processShortcut(createShortcutIntent);
247 }
248 });
249 }
250 }
251
252 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700253 public boolean onLongClick(View v) {
254 if (!v.isInTouchMode()) {
255 return false;
256 }
257
258 final View animView = v;
259 switch (mCustomizationType) {
260 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700261 // Get the icon as the drag representation
262 final LinearLayout l = (LinearLayout) animView;
263 final Drawable icon = ((ImageView) l.findViewById(R.id.icon)).getDrawable();
264 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
265 Bitmap.Config.ARGB_8888);
266 Canvas c = new Canvas(b);
267 icon.draw(c);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700268
Winson Chung80baf5a2010-08-09 16:03:15 -0700269 AppWidgetProviderInfo appWidgetInfo = (AppWidgetProviderInfo) v.getTag();
270 LauncherAppWidgetInfo dragInfo = new LauncherAppWidgetInfo(appWidgetInfo.provider);
271 dragInfo.minWidth = appWidgetInfo.minWidth;
272 dragInfo.minHeight = appWidgetInfo.minHeight;
Winson Chunge3193b92010-09-10 11:44:42 -0700273 mDragController.startDrag(v, b, this, dragInfo, DragController.DRAG_ACTION_COPY, null);
274
275 // Cleanup the icon
276 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700277 return true;
278 case FolderCustomization:
279 // animate some feedback to the long press
280 animateClickFeedback(v, new Runnable() {
281 @Override
282 public void run() {
283 // add the folder
284 ResolveInfo resolveInfo = (ResolveInfo) animView.getTag();
285 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
286 if (resolveInfo.labelRes == R.string.group_folder) {
287 // Create app shortcuts is a special built-in case of shortcuts
288 createFolderIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
289 getContext().getString(R.string.group_folder));
290 } else {
291 ComponentName name = new ComponentName(resolveInfo.activityInfo.packageName,
292 resolveInfo.activityInfo.name);
293 createFolderIntent.setComponent(name);
294 }
295 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
296 mLauncher.addLiveFolder(createFolderIntent);
297 }
298 });
299 return true;
300 case ShortcutCustomization:
301 // animate some feedback to the long press
302 animateClickFeedback(v, new Runnable() {
303 @Override
304 public void run() {
305 // add the shortcut
306 ResolveInfo info = (ResolveInfo) animView.getTag();
307 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
308 if (info.labelRes == R.string.group_applications) {
309 // Create app shortcuts is a special built-in case of shortcuts
310 createShortcutIntent.putExtra(
311 Intent.EXTRA_SHORTCUT_NAME,getContext().getString(
312 R.string.group_applications));
313 } else {
314 ComponentName name = new ComponentName(info.activityInfo.packageName,
315 info.activityInfo.name);
316 createShortcutIntent.setComponent(name);
317 }
318 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
319 mLauncher.processShortcut(createShortcutIntent);
320 }
321 });
322 return true;
323 }
324 return false;
325 }
326
Winson Chunge3193b92010-09-10 11:44:42 -0700327 /**
328 * Pre-processes the layout of the different widget pages.
329 * @return the number of pages of widgets that we have
330 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700331 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700332 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700333
Winson Chunge3193b92010-09-10 11:44:42 -0700334 // create a new page for the first set of widgets
335 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700336 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700337 mWidgetPages.add(newPage);
338
339 // do this until we have no more widgets to lay out
340 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
341 final int widgetCount = mWidgetList.size();
342 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700343 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700344 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700345
Winson Chunge3193b92010-09-10 11:44:42 -0700346 // determine the size of the current widget
347 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
348 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700349
Winson Chunge3193b92010-09-10 11:44:42 -0700350 // create a new page if necessary
351 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
352 numCellsInRow = 0;
353 newPage = new ArrayList<AppWidgetProviderInfo>();
354 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700355 }
356
Winson Chunge3193b92010-09-10 11:44:42 -0700357 // add the item to the current page
358 newPage.add(info);
359 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700360 }
Winson Chunge3193b92010-09-10 11:44:42 -0700361
Winson Chung80baf5a2010-08-09 16:03:15 -0700362 return mWidgetPages.size();
363 }
364
Winson Chunge3193b92010-09-10 11:44:42 -0700365 /**
366 * This method will extract the preview image specified by the widget developer (if it exists),
367 * otherwise, it will try to generate a default image preview with the widget's package icon.
368 * @return the drawable will be used and sized in the ImageView to represent the widget
369 */
370 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700371 PackageManager packageManager = mLauncher.getPackageManager();
372 String packageName = info.provider.getPackageName();
373 Drawable drawable = null;
374 if (info.previewImage != 0) {
375 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
376 if (drawable == null) {
377 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
378 + " for provider: " + info.provider);
379 } else {
380 return drawable;
381 }
382 }
383
384 // If we don't have a preview image, create a default one
385 if (drawable == null) {
386 Resources resources = mLauncher.getResources();
387
Winson Chung80baf5a2010-08-09 16:03:15 -0700388 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700389 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
390 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
391 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
392 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700393 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
394 mCanvas.setBitmap(bitmap);
395 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
396 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
397
398 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
399 background.setBounds(0, 0, width, height);
400 background.draw(mCanvas);
401
402 // Draw the icon vertically centered, flush left
403 try {
404 Rect tmpRect = new Rect();
405 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700406 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700407 icon = packageManager.getDrawable(packageName, info.icon, null);
408 } else {
409 icon = resources.getDrawable(R.drawable.ic_launcher_application);
410 }
411 background.getPadding(tmpRect);
412
Winson Chunge3193b92010-09-10 11:44:42 -0700413 final int iconSize = minDim / 2;
414 final int offset = iconSize / 4;
415 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700416 icon.draw(mCanvas);
417 } catch (Resources.NotFoundException e) {
418 // if we can't find the icon, then just don't draw it
419 }
420
Winson Chungb3347bb2010-08-19 14:51:28 -0700421 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700422 }
423 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
424 return drawable;
425 }
426
427 private void setupPage(PagedViewCellLayout layout) {
Winson Chunge3193b92010-09-10 11:44:42 -0700428 layout.setCellCount(sCellCountX, sCellCountY);
Winson Chung80baf5a2010-08-09 16:03:15 -0700429 layout.setPadding(20, 10, 20, 0);
430 }
431
Winson Chunge3193b92010-09-10 11:44:42 -0700432 private void setupWorkspaceLayout() {
433 mWorkspaceWidgetLayout.setCellCount(sCellCountX, sCellCountY);
434 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
435
436 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
437 }
438
Winson Chung80baf5a2010-08-09 16:03:15 -0700439 private void syncWidgetPages() {
440 if (mWidgetList == null) return;
441
Winson Chunge3193b92010-09-10 11:44:42 -0700442 // we need to repopulate with the LinearLayout layout for the widget pages
443 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700444 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700445 for (int i = 0; i < numPages; ++i) {
446 LinearLayout layout = new WidgetLayout(getContext());
447 layout.setGravity(Gravity.CENTER_HORIZONTAL);
448
449 // Temporary change to prevent the last page from being too small (and items bleeding
450 // onto it). We can remove this once we properly fix the fading algorithm
451 if (i < numPages - 1) {
452 addView(layout, new LinearLayout.LayoutParams(
453 LinearLayout.LayoutParams.WRAP_CONTENT,
454 LinearLayout.LayoutParams.MATCH_PARENT));
455 } else {
456 addView(layout, new LinearLayout.LayoutParams(
457 LinearLayout.LayoutParams.MATCH_PARENT,
458 LinearLayout.LayoutParams.MATCH_PARENT));
459 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700460 }
461 }
462
463 private void syncWidgetPageItems(int page) {
464 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700465 LinearLayout layout = (LinearLayout) getChildAt(page);
466 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700467 final int count = list.size();
468 layout.removeAllViews();
469 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700470 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
471 LinearLayout l = (LinearLayout) mInflater.inflate(
472 R.layout.customize_paged_view_widget, layout, false);
473 l.setTag(info);
474 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700475
Winson Chunge3193b92010-09-10 11:44:42 -0700476 final Drawable icon = getWidgetIcon(info);
477 final int hSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth);
478 final int vSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minHeight);
479
480 ImageView image = (ImageView) l.findViewById(R.id.icon);
481 image.setMaxWidth(mMaxWidgetWidth);
482 image.setImageDrawable(icon);
483 TextView name = (TextView) l.findViewById(R.id.name);
484 name.setText(info.label);
485 TextView dims = (TextView) l.findViewById(R.id.dims);
Winson Chung3a476782010-09-15 15:21:55 -0700486 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700487
488 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700489 }
490 }
491
492 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700493 // we need to repopulate with PagedViewCellLayouts
494 removeAllViews();
495
Winson Chung80baf5a2010-08-09 16:03:15 -0700496 // ensure that we have the right number of pages
Winson Chunge3193b92010-09-10 11:44:42 -0700497 int numPages = (int) Math.ceil((float) list.size() / (sCellCountX * sCellCountY));
498 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700499 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
500 setupPage(layout);
501 addView(layout);
502 }
503 }
504
505 private void syncListPageItems(int page, List<ResolveInfo> list) {
506 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700507 int numCells = sCellCountX * sCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700508 int startIndex = page * numCells;
509 int endIndex = Math.min(startIndex + numCells, list.size());
510 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
511 // TODO: we can optimize by just re-applying to existing views
512 layout.removeAllViews();
513 for (int i = startIndex; i < endIndex; ++i) {
514 ResolveInfo info = list.get(i);
Winson Chung241c3b42010-08-25 16:53:03 -0700515 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
516 R.layout.customize_paged_view_item, layout, false);
517 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chunge8878e32010-09-15 20:37:09 -0700518 if (mCustomizationType == CustomizationType.WallpaperCustomization) {
519 icon.setOnClickListener(this);
520 } else {
521 icon.setOnLongClickListener(this);
522 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700523
524 final int index = i - startIndex;
Winson Chunge3193b92010-09-10 11:44:42 -0700525 final int x = index % sCellCountX;
526 final int y = index / sCellCountX;
527 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700528 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700529 }
530 }
531
Winson Chung80baf5a2010-08-09 16:03:15 -0700532 @Override
533 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700534 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700535 switch (mCustomizationType) {
536 case WidgetCustomization:
537 syncWidgetPages();
538 break;
539 case FolderCustomization:
540 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700541 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700542 break;
543 case ShortcutCustomization:
544 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700545 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700546 break;
547 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700548 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700549 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700550 break;
551 default:
552 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700553 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700554 break;
555 }
556
557 // only try and center the page if there is one page
558 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700559 if (centerPagedViewCellLayouts) {
560 if (childCount == 1) {
561 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
562 layout.enableCenteredContent(true);
563 } else {
564 for (int i = 0; i < childCount; ++i) {
565 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
566 layout.enableCenteredContent(false);
567 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700568 }
569 }
570
571 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700572 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700573 }
574
575 @Override
576 public void syncPageItems(int page) {
577 switch (mCustomizationType) {
578 case WidgetCustomization:
579 syncWidgetPageItems(page);
580 break;
581 case FolderCustomization:
582 syncListPageItems(page, mFolderList);
583 break;
584 case ShortcutCustomization:
585 syncListPageItems(page, mShortcutList);
586 break;
587 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700588 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700589 break;
590 }
591 }
Winson Chunge3193b92010-09-10 11:44:42 -0700592
593 protected int getAssociatedLowerPageBound(int page) {
594 return 0;
595 }
596 protected int getAssociatedUpperPageBound(int page) {
597 return getChildCount();
598 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700599}