blob: d3d11db93709f6e70ad6d4dc590581899889407a [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
55 implements View.OnLongClickListener,
56 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;
111
Winson Chunge3193b92010-09-10 11:44:42 -0700112 private static final int sCellCountX = 8;
113 private static final int sCellCountY = 4;
114 private static final int sMinWidgetCellHSpan = 2;
115 private static final int sMaxWidgetCellHSpan = 4;
116
117 // The scale factor for widget previews inside the widget drawer
118 private static final float sScaleFactor = 0.75f;
Winson Chung80baf5a2010-08-09 16:03:15 -0700119
120 private final Canvas mCanvas = new Canvas();
121 private final LayoutInflater mInflater;
122
123 public CustomizePagedView(Context context) {
Winson Chunge3193b92010-09-10 11:44:42 -0700124 this(context, null, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700125 }
126
127 public CustomizePagedView(Context context, AttributeSet attrs) {
Winson Chunge3193b92010-09-10 11:44:42 -0700128 this(context, attrs, 0);
129 }
130
131 public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) {
132 super(context, attrs, defStyle);
133
134 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView,
135 defStyle, 0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700136 mCustomizationType = CustomizationType.WidgetCustomization;
Winson Chunge3193b92010-09-10 11:44:42 -0700137 mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>();
138 mWorkspaceWidgetLayout = new PagedViewCellLayout(context);
Winson Chung80baf5a2010-08-09 16:03:15 -0700139 mInflater = LayoutInflater.from(context);
Winson Chunge3193b92010-09-10 11:44:42 -0700140 mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8);
141 a.recycle();
142
Winson Chung80baf5a2010-08-09 16:03:15 -0700143 setVisibility(View.GONE);
144 setSoundEffectsEnabled(false);
Winson Chunge3193b92010-09-10 11:44:42 -0700145 setupWorkspaceLayout();
Winson Chung80baf5a2010-08-09 16:03:15 -0700146 }
147
148 public void setLauncher(Launcher launcher) {
149 Context context = getContext();
150 mLauncher = launcher;
151 mPackageManager = context.getPackageManager();
152 }
153
154 public void update() {
155 Context context = getContext();
156
157 // get the list of widgets
158 mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders();
159 Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() {
160 @Override
161 public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) {
162 return object1.label.compareTo(object2.label);
163 }
164 });
165
166 Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() {
167 @Override
168 public int compare(ResolveInfo object1, ResolveInfo object2) {
169 return object1.loadLabel(mPackageManager).toString().compareTo(
170 object2.loadLabel(mPackageManager).toString());
171 }
172 };
173
174 // get the list of live folder intents
175 Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
176 mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0);
177
178 // manually create a separate entry for creating a folder in Launcher
179 ResolveInfo folder = new ResolveInfo();
180 folder.icon = R.drawable.ic_launcher_folder;
181 folder.labelRes = R.string.group_folder;
182 folder.resolvePackageName = context.getPackageName();
183 mFolderList.add(0, folder);
184 Collections.sort(mFolderList, resolveInfoComparator);
185
186 // get the list of shortcuts
187 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
188 mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0);
189 Collections.sort(mShortcutList, resolveInfoComparator);
190
Winson Chung241c3b42010-08-25 16:53:03 -0700191 // reset the icon cache
192 mPageViewIconCache.clear();
193
Winson Chunge3193b92010-09-10 11:44:42 -0700194 // Refresh all the tabs
Winson Chung80baf5a2010-08-09 16:03:15 -0700195 invalidatePageData();
196 }
197
198 public void setDragController(DragController dragger) {
199 mDragController = dragger;
200 }
201
202 public void setCustomizationFilter(CustomizationType filterType) {
203 mCustomizationType = filterType;
Winson Chung86f77532010-08-24 11:08:22 -0700204 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700205 invalidatePageData();
206 }
207
208 @Override
209 public void onDropCompleted(View target, boolean success) {
210 // do nothing
211 }
212
213 @Override
214 public boolean onLongClick(View v) {
215 if (!v.isInTouchMode()) {
216 return false;
217 }
218
219 final View animView = v;
220 switch (mCustomizationType) {
221 case WidgetCustomization:
Winson Chunge3193b92010-09-10 11:44:42 -0700222 // Get the icon as the drag representation
223 final LinearLayout l = (LinearLayout) animView;
224 final Drawable icon = ((ImageView) l.findViewById(R.id.icon)).getDrawable();
225 Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
226 Bitmap.Config.ARGB_8888);
227 Canvas c = new Canvas(b);
228 icon.draw(c);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700229
Winson Chung80baf5a2010-08-09 16:03:15 -0700230 AppWidgetProviderInfo appWidgetInfo = (AppWidgetProviderInfo) v.getTag();
231 LauncherAppWidgetInfo dragInfo = new LauncherAppWidgetInfo(appWidgetInfo.provider);
232 dragInfo.minWidth = appWidgetInfo.minWidth;
233 dragInfo.minHeight = appWidgetInfo.minHeight;
Winson Chunge3193b92010-09-10 11:44:42 -0700234 mDragController.startDrag(v, b, this, dragInfo, DragController.DRAG_ACTION_COPY, null);
235
236 // Cleanup the icon
237 b.recycle();
Winson Chung80baf5a2010-08-09 16:03:15 -0700238 return true;
239 case FolderCustomization:
240 // animate some feedback to the long press
241 animateClickFeedback(v, new Runnable() {
242 @Override
243 public void run() {
244 // add the folder
245 ResolveInfo resolveInfo = (ResolveInfo) animView.getTag();
246 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
247 if (resolveInfo.labelRes == R.string.group_folder) {
248 // Create app shortcuts is a special built-in case of shortcuts
249 createFolderIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
250 getContext().getString(R.string.group_folder));
251 } else {
252 ComponentName name = new ComponentName(resolveInfo.activityInfo.packageName,
253 resolveInfo.activityInfo.name);
254 createFolderIntent.setComponent(name);
255 }
256 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
257 mLauncher.addLiveFolder(createFolderIntent);
258 }
259 });
260 return true;
261 case ShortcutCustomization:
262 // animate some feedback to the long press
263 animateClickFeedback(v, new Runnable() {
264 @Override
265 public void run() {
266 // add the shortcut
267 ResolveInfo info = (ResolveInfo) animView.getTag();
268 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
269 if (info.labelRes == R.string.group_applications) {
270 // Create app shortcuts is a special built-in case of shortcuts
271 createShortcutIntent.putExtra(
272 Intent.EXTRA_SHORTCUT_NAME,getContext().getString(
273 R.string.group_applications));
274 } else {
275 ComponentName name = new ComponentName(info.activityInfo.packageName,
276 info.activityInfo.name);
277 createShortcutIntent.setComponent(name);
278 }
279 mLauncher.prepareAddItemFromHomeCustomizationDrawer();
280 mLauncher.processShortcut(createShortcutIntent);
281 }
282 });
283 return true;
284 }
285 return false;
286 }
287
Winson Chunge3193b92010-09-10 11:44:42 -0700288 /**
289 * Pre-processes the layout of the different widget pages.
290 * @return the number of pages of widgets that we have
291 */
Winson Chung80baf5a2010-08-09 16:03:15 -0700292 private int relayoutWidgets() {
Winson Chunge3193b92010-09-10 11:44:42 -0700293 if (mWidgetList.isEmpty()) return 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700294
Winson Chunge3193b92010-09-10 11:44:42 -0700295 // create a new page for the first set of widgets
296 ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>();
Winson Chung80baf5a2010-08-09 16:03:15 -0700297 mWidgetPages.clear();
Winson Chunge3193b92010-09-10 11:44:42 -0700298 mWidgetPages.add(newPage);
299
300 // do this until we have no more widgets to lay out
301 final int maxNumCellsPerRow = mMaxWidgetsCellHSpan;
302 final int widgetCount = mWidgetList.size();
303 int numCellsInRow = 0;
Winson Chung80baf5a2010-08-09 16:03:15 -0700304 for (int i = 0; i < widgetCount; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700305 final AppWidgetProviderInfo info = mWidgetList.get(i);
Winson Chung80baf5a2010-08-09 16:03:15 -0700306
Winson Chunge3193b92010-09-10 11:44:42 -0700307 // determine the size of the current widget
308 int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan,
309 mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth)));
Winson Chung80baf5a2010-08-09 16:03:15 -0700310
Winson Chunge3193b92010-09-10 11:44:42 -0700311 // create a new page if necessary
312 if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) {
313 numCellsInRow = 0;
314 newPage = new ArrayList<AppWidgetProviderInfo>();
315 mWidgetPages.add(newPage);
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 }
317
Winson Chunge3193b92010-09-10 11:44:42 -0700318 // add the item to the current page
319 newPage.add(info);
320 numCellsInRow += cellSpanX;
Winson Chung80baf5a2010-08-09 16:03:15 -0700321 }
Winson Chunge3193b92010-09-10 11:44:42 -0700322
Winson Chung80baf5a2010-08-09 16:03:15 -0700323 return mWidgetPages.size();
324 }
325
Winson Chunge3193b92010-09-10 11:44:42 -0700326 /**
327 * This method will extract the preview image specified by the widget developer (if it exists),
328 * otherwise, it will try to generate a default image preview with the widget's package icon.
329 * @return the drawable will be used and sized in the ImageView to represent the widget
330 */
331 private Drawable getWidgetIcon(AppWidgetProviderInfo info) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700332 PackageManager packageManager = mLauncher.getPackageManager();
333 String packageName = info.provider.getPackageName();
334 Drawable drawable = null;
335 if (info.previewImage != 0) {
336 drawable = packageManager.getDrawable(packageName, info.previewImage, null);
337 if (drawable == null) {
338 Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
339 + " for provider: " + info.provider);
340 } else {
341 return drawable;
342 }
343 }
344
345 // If we don't have a preview image, create a default one
346 if (drawable == null) {
347 Resources resources = mLauncher.getResources();
348
Winson Chung80baf5a2010-08-09 16:03:15 -0700349 // Create a new bitmap to hold the widget preview
Winson Chunge3193b92010-09-10 11:44:42 -0700350 final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1);
351 final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3);
352 int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor);
353 int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor);
Winson Chung80baf5a2010-08-09 16:03:15 -0700354 Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
355 mCanvas.setBitmap(bitmap);
356 // For some reason, we must re-set the clip rect here, otherwise it will be wrong
357 mCanvas.clipRect(0, 0, width, height, Op.REPLACE);
358
359 Drawable background = resources.getDrawable(R.drawable.default_widget_preview);
360 background.setBounds(0, 0, width, height);
361 background.draw(mCanvas);
362
363 // Draw the icon vertically centered, flush left
364 try {
365 Rect tmpRect = new Rect();
366 Drawable icon = null;
Winson Chunge3193b92010-09-10 11:44:42 -0700367 if (info.icon > 0) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700368 icon = packageManager.getDrawable(packageName, info.icon, null);
369 } else {
370 icon = resources.getDrawable(R.drawable.ic_launcher_application);
371 }
372 background.getPadding(tmpRect);
373
Winson Chunge3193b92010-09-10 11:44:42 -0700374 final int iconSize = minDim / 2;
375 final int offset = iconSize / 4;
376 icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize));
Winson Chung80baf5a2010-08-09 16:03:15 -0700377 icon.draw(mCanvas);
378 } catch (Resources.NotFoundException e) {
379 // if we can't find the icon, then just don't draw it
380 }
381
Winson Chungb3347bb2010-08-19 14:51:28 -0700382 drawable = new FastBitmapDrawable(bitmap);
Winson Chung80baf5a2010-08-09 16:03:15 -0700383 }
384 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
385 return drawable;
386 }
387
388 private void setupPage(PagedViewCellLayout layout) {
Winson Chunge3193b92010-09-10 11:44:42 -0700389 layout.setCellCount(sCellCountX, sCellCountY);
Winson Chung80baf5a2010-08-09 16:03:15 -0700390 layout.setPadding(20, 10, 20, 0);
391 }
392
Winson Chunge3193b92010-09-10 11:44:42 -0700393 private void setupWorkspaceLayout() {
394 mWorkspaceWidgetLayout.setCellCount(sCellCountX, sCellCountY);
395 mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0);
396
397 mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan);
398 }
399
Winson Chung80baf5a2010-08-09 16:03:15 -0700400 private void syncWidgetPages() {
401 if (mWidgetList == null) return;
402
Winson Chunge3193b92010-09-10 11:44:42 -0700403 // we need to repopulate with the LinearLayout layout for the widget pages
404 removeAllViews();
Winson Chung80baf5a2010-08-09 16:03:15 -0700405 int numPages = relayoutWidgets();
Winson Chunge3193b92010-09-10 11:44:42 -0700406 for (int i = 0; i < numPages; ++i) {
407 LinearLayout layout = new WidgetLayout(getContext());
408 layout.setGravity(Gravity.CENTER_HORIZONTAL);
409
410 // Temporary change to prevent the last page from being too small (and items bleeding
411 // onto it). We can remove this once we properly fix the fading algorithm
412 if (i < numPages - 1) {
413 addView(layout, new LinearLayout.LayoutParams(
414 LinearLayout.LayoutParams.WRAP_CONTENT,
415 LinearLayout.LayoutParams.MATCH_PARENT));
416 } else {
417 addView(layout, new LinearLayout.LayoutParams(
418 LinearLayout.LayoutParams.MATCH_PARENT,
419 LinearLayout.LayoutParams.MATCH_PARENT));
420 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700421 }
422 }
423
424 private void syncWidgetPageItems(int page) {
425 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700426 LinearLayout layout = (LinearLayout) getChildAt(page);
427 final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700428 final int count = list.size();
429 layout.removeAllViews();
430 for (int i = 0; i < count; ++i) {
Winson Chunge3193b92010-09-10 11:44:42 -0700431 AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i);
432 LinearLayout l = (LinearLayout) mInflater.inflate(
433 R.layout.customize_paged_view_widget, layout, false);
434 l.setTag(info);
435 l.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700436
Winson Chunge3193b92010-09-10 11:44:42 -0700437 final Drawable icon = getWidgetIcon(info);
438 final int hSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth);
439 final int vSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minHeight);
440
441 ImageView image = (ImageView) l.findViewById(R.id.icon);
442 image.setMaxWidth(mMaxWidgetWidth);
443 image.setImageDrawable(icon);
444 TextView name = (TextView) l.findViewById(R.id.name);
445 name.setText(info.label);
446 TextView dims = (TextView) l.findViewById(R.id.dims);
447 dims.setText("" + hSpan + " x " + vSpan);
448
449 layout.addView(l);
Winson Chung80baf5a2010-08-09 16:03:15 -0700450 }
451 }
452
453 private void syncListPages(List<ResolveInfo> list) {
Winson Chunge3193b92010-09-10 11:44:42 -0700454 // we need to repopulate with PagedViewCellLayouts
455 removeAllViews();
456
Winson Chung80baf5a2010-08-09 16:03:15 -0700457 // ensure that we have the right number of pages
Winson Chunge3193b92010-09-10 11:44:42 -0700458 int numPages = (int) Math.ceil((float) list.size() / (sCellCountX * sCellCountY));
459 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700460 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
461 setupPage(layout);
462 addView(layout);
463 }
464 }
465
466 private void syncListPageItems(int page, List<ResolveInfo> list) {
467 // ensure that we have the right number of items on the pages
Winson Chunge3193b92010-09-10 11:44:42 -0700468 int numCells = sCellCountX * sCellCountY;
Winson Chung80baf5a2010-08-09 16:03:15 -0700469 int startIndex = page * numCells;
470 int endIndex = Math.min(startIndex + numCells, list.size());
471 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
472 // TODO: we can optimize by just re-applying to existing views
473 layout.removeAllViews();
474 for (int i = startIndex; i < endIndex; ++i) {
475 ResolveInfo info = list.get(i);
Winson Chung241c3b42010-08-25 16:53:03 -0700476 PagedViewIcon icon = (PagedViewIcon) mInflater.inflate(
477 R.layout.customize_paged_view_item, layout, false);
478 icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache);
479 icon.setOnLongClickListener(this);
Winson Chung80baf5a2010-08-09 16:03:15 -0700480
481 final int index = i - startIndex;
Winson Chunge3193b92010-09-10 11:44:42 -0700482 final int x = index % sCellCountX;
483 final int y = index / sCellCountX;
484 setupPage(layout);
Winson Chung241c3b42010-08-25 16:53:03 -0700485 layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1));
Winson Chung80baf5a2010-08-09 16:03:15 -0700486 }
487 }
488
489 private void syncWallpaperPages() {
490 // NOT CURRENTLY IMPLEMENTED
Winson Chunge3193b92010-09-10 11:44:42 -0700491
492 // we need to repopulate with PagedViewCellLayouts
493 removeAllViews();
494
Winson Chung80baf5a2010-08-09 16:03:15 -0700495 // add any necessary pages
Winson Chunge3193b92010-09-10 11:44:42 -0700496 int numPages = 1;
497 for (int i = 0; i < numPages; ++i) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700498 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
499 setupPage(layout);
500 addView(layout);
501 }
502 }
503
504 private void syncWallpaperPageItems(int page) {
505 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
506 layout.removeAllViews();
507
508 TextView text = (TextView) mInflater.inflate(
509 R.layout.customize_paged_view_wallpaper_placeholder, layout, false);
510 // NOTE: this is just place holder text until MikeJurka implements wallpaper picker
511 text.setText("Wallpaper customization coming soon!");
512
Winson Chunge3193b92010-09-10 11:44:42 -0700513 setupPage(layout);
Winson Chung80baf5a2010-08-09 16:03:15 -0700514 layout.addViewToCellLayout(text, -1, 0, new PagedViewCellLayout.LayoutParams(0, 0, 3, 1));
515 }
516
517 @Override
518 public void syncPages() {
Winson Chunge3193b92010-09-10 11:44:42 -0700519 boolean centerPagedViewCellLayouts = false;
Winson Chung80baf5a2010-08-09 16:03:15 -0700520 switch (mCustomizationType) {
521 case WidgetCustomization:
522 syncWidgetPages();
523 break;
524 case FolderCustomization:
525 syncListPages(mFolderList);
Winson Chunge3193b92010-09-10 11:44:42 -0700526 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700527 break;
528 case ShortcutCustomization:
529 syncListPages(mShortcutList);
Winson Chunge3193b92010-09-10 11:44:42 -0700530 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700531 break;
532 case WallpaperCustomization:
533 syncWallpaperPages();
Winson Chunge3193b92010-09-10 11:44:42 -0700534 centerPagedViewCellLayouts = true;
Winson Chung80baf5a2010-08-09 16:03:15 -0700535 break;
536 default:
537 removeAllViews();
Winson Chung86f77532010-08-24 11:08:22 -0700538 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -0700539 break;
540 }
541
542 // only try and center the page if there is one page
543 final int childCount = getChildCount();
Winson Chunge3193b92010-09-10 11:44:42 -0700544 if (centerPagedViewCellLayouts) {
545 if (childCount == 1) {
546 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0);
547 layout.enableCenteredContent(true);
548 } else {
549 for (int i = 0; i < childCount; ++i) {
550 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
551 layout.enableCenteredContent(false);
552 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700553 }
554 }
555
556 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700557 setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage())));
Winson Chung80baf5a2010-08-09 16:03:15 -0700558 }
559
560 @Override
561 public void syncPageItems(int page) {
562 switch (mCustomizationType) {
563 case WidgetCustomization:
564 syncWidgetPageItems(page);
565 break;
566 case FolderCustomization:
567 syncListPageItems(page, mFolderList);
568 break;
569 case ShortcutCustomization:
570 syncListPageItems(page, mShortcutList);
571 break;
572 case WallpaperCustomization:
573 syncWallpaperPageItems(page);
574 break;
575 }
576 }
Winson Chunge3193b92010-09-10 11:44:42 -0700577
578 protected int getAssociatedLowerPageBound(int page) {
579 return 0;
580 }
581 protected int getAssociatedUpperPageBound(int page) {
582 return getChildCount();
583 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700584}