blob: 8fb3025c0df4d8b1d35a10ffb1d22fc18691ac48 [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();
Winson Chung24ab2f12010-09-16 14:10:47 -0700234 Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
235 ComponentName name = new ComponentName(info.activityInfo.packageName,
236 info.activityInfo.name);
237 createWallpapersIntent.setComponent(name);
238 mLauncher.processWallpaper(createWallpapersIntent);
Winson Chunge8878e32010-09-15 20:37:09 -0700239 }
240 });
241 }
242 }
243
244 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700245 public boolean onLongClick(View v) {
246 if (!v.isInTouchMode()) {
247 return false;
248 }
249
250 final View animView = v;
251 switch (mCustomizationType) {
252 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700253 // Get the icon as the drag representation
254 final LinearLayout l = (LinearLayout) animView;
255 final Drawable icon = ((ImageView) l.findViewById(R.id.icon)).getDrawable();
256 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
257 Bitmap.Config.ARGB_8888);
258 Canvas c = new Canvas(b);
259 icon.draw(c);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700260
Winson Chung80baf5a2010-08-09 16:03:15 -0700261 AppWidgetProviderInfo appWidgetInfo = (AppWidgetProviderInfo) v.getTag();
262 LauncherAppWidgetInfo dragInfo = new LauncherAppWidgetInfo(appWidgetInfo.provider);
263 dragInfo.minWidth = appWidgetInfo.minWidth;
264 dragInfo.minHeight = appWidgetInfo.minHeight;
Winson Chunge3193b92010-09-10 11:44:42 -0700265 mDragController.startDrag(v, b, this, dragInfo, DragController.DRAG_ACTION_COPY, null);
266
267 // Cleanup the icon
268 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700269 return true;
270 case FolderCustomization:
271 // animate some feedback to the long press
272 animateClickFeedback(v, new Runnable() {
273 @Override
274 public void run() {
275 // add the folder
276 ResolveInfo resolveInfo = (ResolveInfo) animView.getTag();
277 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
278 if (resolveInfo.labelRes == R.string.group_folder) {
279 // Create app shortcuts is a special built-in case of shortcuts
280 createFolderIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
281 getContext().getString(R.string.group_folder));
282 } else {
283 ComponentName name = new ComponentName(resolveInfo.activityInfo.packageName,
284 resolveInfo.activityInfo.name);
285 createFolderIntent.setComponent(name);
286 }
287 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
288 mLauncher.addLiveFolder(createFolderIntent);
289 }
290 });
291 return true;
292 case ShortcutCustomization:
293 // animate some feedback to the long press
294 animateClickFeedback(v, new Runnable() {
295 @Override
296 public void run() {
297 // add the shortcut
298 ResolveInfo info = (ResolveInfo) animView.getTag();
299 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
300 if (info.labelRes == R.string.group_applications) {
301 // Create app shortcuts is a special built-in case of shortcuts
302 createShortcutIntent.putExtra(
303 Intent.EXTRA_SHORTCUT_NAME,getContext().getString(
304 R.string.group_applications));
305 } else {
306 ComponentName name = new ComponentName(info.activityInfo.packageName,
307 info.activityInfo.name);
308 createShortcutIntent.setComponent(name);
309 }
310 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
311 mLauncher.processShortcut(createShortcutIntent);
312 }
313 });
314 return true;
315 }
316 return false;
317 }
318
Winson Chunge3193b92010-09-10 11:44:42 -0700319 /**
320 * Pre-processes the layout of the different widget pages.
321 * @return the number of pages of widgets that we have
322 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700323 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700324 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700325
Winson Chunge3193b92010-09-10 11:44:42 -0700326 // create a new page for the first set of widgets
327 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700328 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700329 mWidgetPages.add(newPage);
330
331 // do this until we have no more widgets to lay out
332 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
333 final int widgetCount = mWidgetList.size();
334 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700335 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700336 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700337
Winson Chunge3193b92010-09-10 11:44:42 -0700338 // determine the size of the current widget
339 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
340 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700341
Winson Chunge3193b92010-09-10 11:44:42 -0700342 // create a new page if necessary
343 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
344 numCellsInRow = 0;
345 newPage = new ArrayList<AppWidgetProviderInfo>();
346 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700347 }
348
Winson Chunge3193b92010-09-10 11:44:42 -0700349 // add the item to the current page
350 newPage.add(info);
351 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700352 }
Winson Chunge3193b92010-09-10 11:44:42 -0700353
Winson Chung80baf5a2010-08-09 16:03:15 -0700354 return mWidgetPages.size();
355 }
356
Winson Chunge3193b92010-09-10 11:44:42 -0700357 /**
358 * This method will extract the preview image specified by the widget developer (if it exists),
359 * otherwise, it will try to generate a default image preview with the widget's package icon.
360 * @return the drawable will be used and sized in the ImageView to represent the widget
361 */
362 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700363 PackageManager packageManager = mLauncher.getPackageManager();
364 String packageName = info.provider.getPackageName();
365 Drawable drawable = null;
366 if (info.previewImage != 0) {
367 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
368 if (drawable == null) {
369 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
370 + " for provider: " + info.provider);
371 } else {
372 return drawable;
373 }
374 }
375
376 // If we don't have a preview image, create a default one
377 if (drawable == null) {
378 Resources resources = mLauncher.getResources();
379
Winson Chung80baf5a2010-08-09 16:03:15 -0700380 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700381 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
382 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
383 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
384 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700385 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
386 mCanvas.setBitmap(bitmap);
387 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
388 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
389
390 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
391 background.setBounds(0, 0, width, height);
392 background.draw(mCanvas);
393
394 // Draw the icon vertically centered, flush left
395 try {
396 Rect tmpRect = new Rect();
397 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700398 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700399 icon = packageManager.getDrawable(packageName, info.icon, null);
400 } else {
401 icon = resources.getDrawable(R.drawable.ic_launcher_application);
402 }
403 background.getPadding(tmpRect);
404
Winson Chunge3193b92010-09-10 11:44:42 -0700405 final int iconSize = minDim / 2;
406 final int offset = iconSize / 4;
407 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700408 icon.draw(mCanvas);
409 } catch (Resources.NotFoundException e) {
410 // if we can't find the icon, then just don't draw it
411 }
412
Winson Chungb3347bb2010-08-19 14:51:28 -0700413 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700414 }
415 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
416 return drawable;
417 }
418
419 private void setupPage(PagedViewCellLayout layout) {
Winson Chunge3193b92010-09-10 11:44:42 -0700420 layout.setCellCount(sCellCountX, sCellCountY);
Winson Chung80baf5a2010-08-09 16:03:15 -0700421 layout.setPadding(20, 10, 20, 0);
422 }
423
Winson Chunge3193b92010-09-10 11:44:42 -0700424 private void setupWorkspaceLayout() {
425 mWorkspaceWidgetLayout.setCellCount(sCellCountX, sCellCountY);
426 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
427
428 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
429 }
430
Winson Chung80baf5a2010-08-09 16:03:15 -0700431 private void syncWidgetPages() {
432 if (mWidgetList == null) return;
433
Winson Chunge3193b92010-09-10 11:44:42 -0700434 // we need to repopulate with the LinearLayout layout for the widget pages
435 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700436 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700437 for (int i = 0; i < numPages; ++i) {
438 LinearLayout layout = new WidgetLayout(getContext());
439 layout.setGravity(Gravity.CENTER_HORIZONTAL);
440
441 // Temporary change to prevent the last page from being too small (and items bleeding
442 // onto it). We can remove this once we properly fix the fading algorithm
443 if (i < numPages - 1) {
444 addView(layout, new LinearLayout.LayoutParams(
445 LinearLayout.LayoutParams.WRAP_CONTENT,
446 LinearLayout.LayoutParams.MATCH_PARENT));
447 } else {
448 addView(layout, new LinearLayout.LayoutParams(
449 LinearLayout.LayoutParams.MATCH_PARENT,
450 LinearLayout.LayoutParams.MATCH_PARENT));
451 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700452 }
453 }
454
455 private void syncWidgetPageItems(int page) {
456 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700457 LinearLayout layout = (LinearLayout) getChildAt(page);
458 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700459 final int count = list.size();
460 layout.removeAllViews();
461 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700462 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
463 LinearLayout l = (LinearLayout) mInflater.inflate(
464 R.layout.customize_paged_view_widget, layout, false);
465 l.setTag(info);
466 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700467
Winson Chunge3193b92010-09-10 11:44:42 -0700468 final Drawable icon = getWidgetIcon(info);
469 final int hSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth);
470 final int vSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minHeight);
471
472 ImageView image = (ImageView) l.findViewById(R.id.icon);
473 image.setMaxWidth(mMaxWidgetWidth);
474 image.setImageDrawable(icon);
475 TextView name = (TextView) l.findViewById(R.id.name);
476 name.setText(info.label);
477 TextView dims = (TextView) l.findViewById(R.id.dims);
Winson Chung3a476782010-09-15 15:21:55 -0700478 dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan));
Winson Chunge3193b92010-09-10 11:44:42 -0700479
480 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700481 }
482 }
483
484 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700485 // we need to repopulate with PagedViewCellLayouts
486 removeAllViews();
487
Winson Chung80baf5a2010-08-09 16:03:15 -0700488 // ensure that we have the right number of pages
Winson Chunge3193b92010-09-10 11:44:42 -0700489 int numPages = (int) Math.ceil((float) list.size() / (sCellCountX * sCellCountY));
490 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700491 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
492 setupPage(layout);
493 addView(layout);
494 }
495 }
496
497 private void syncListPageItems(int page, List<ResolveInfo> list) {
498 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700499 int numCells = sCellCountX * sCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700500 int startIndex = page * numCells;
501 int endIndex = Math.min(startIndex + numCells, list.size());
502 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
503 // TODO: we can optimize by just re-applying to existing views
504 layout.removeAllViews();
505 for (int i = startIndex; i < endIndex; ++i) {
506 ResolveInfo info = list.get(i);
Winson Chung241c3b42010-08-25 16:53:03 -0700507 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
508 R.layout.customize_paged_view_item, layout, false);
509 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
Winson Chunge8878e32010-09-15 20:37:09 -0700510 if (mCustomizationType == CustomizationType.WallpaperCustomization) {
511 icon.setOnClickListener(this);
512 } else {
513 icon.setOnLongClickListener(this);
514 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700515
516 final int index = i - startIndex;
Winson Chunge3193b92010-09-10 11:44:42 -0700517 final int x = index % sCellCountX;
518 final int y = index / sCellCountX;
519 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700520 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700521 }
522 }
523
Winson Chung80baf5a2010-08-09 16:03:15 -0700524 @Override
525 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700526 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700527 switch (mCustomizationType) {
528 case WidgetCustomization:
529 syncWidgetPages();
530 break;
531 case FolderCustomization:
532 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700533 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700534 break;
535 case ShortcutCustomization:
536 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700537 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700538 break;
539 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700540 syncListPages(mWallpaperList);
Winson Chunge3193b92010-09-10 11:44:42 -0700541 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700542 break;
543 default:
544 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700545 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700546 break;
547 }
548
549 // only try and center the page if there is one page
550 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700551 if (centerPagedViewCellLayouts) {
552 if (childCount == 1) {
553 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
554 layout.enableCenteredContent(true);
555 } else {
556 for (int i = 0; i < childCount; ++i) {
557 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
558 layout.enableCenteredContent(false);
559 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700560 }
561 }
562
563 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700564 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700565 }
566
567 @Override
568 public void syncPageItems(int page) {
569 switch (mCustomizationType) {
570 case WidgetCustomization:
571 syncWidgetPageItems(page);
572 break;
573 case FolderCustomization:
574 syncListPageItems(page, mFolderList);
575 break;
576 case ShortcutCustomization:
577 syncListPageItems(page, mShortcutList);
578 break;
579 case WallpaperCustomization:
Winson Chunge8878e32010-09-15 20:37:09 -0700580 syncListPageItems(page, mWallpaperList);
Winson Chung80baf5a2010-08-09 16:03:15 -0700581 break;
582 }
583 }
Winson Chunge3193b92010-09-10 11:44:42 -0700584
585 protected int getAssociatedLowerPageBound(int page) {
586 return 0;
587 }
588 protected int getAssociatedUpperPageBound(int page) {
589 return getChildCount();
590 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700591}