Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collections; |
| 21 | import java.util.Comparator; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import android.appwidget.AppWidgetManager; |
| 25 | import android.appwidget.AppWidgetProviderInfo; |
| 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.ResolveInfo; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 31 | import android.content.res.Configuration; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 32 | import android.content.res.Resources; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 33 | import android.content.res.TypedArray; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 34 | import android.graphics.Bitmap; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 35 | import android.graphics.Bitmap.Config; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 36 | import android.graphics.Canvas; |
| 37 | import android.graphics.Rect; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 38 | import android.graphics.Region.Op; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 39 | import android.graphics.drawable.Drawable; |
| 40 | import android.provider.LiveFolders; |
| 41 | import android.util.AttributeSet; |
| 42 | import android.util.Log; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 43 | import android.view.Gravity; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 44 | import android.view.LayoutInflater; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 45 | import android.view.MotionEvent; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 46 | import android.view.View; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 47 | import android.view.ViewGroup; |
| 48 | import android.widget.ImageView; |
| 49 | import android.widget.LinearLayout; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 50 | import android.widget.TextView; |
| 51 | |
| 52 | import com.android.launcher.R; |
| 53 | |
| 54 | public class CustomizePagedView extends PagedView |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame^] | 55 | implements View.OnLongClickListener, View.OnClickListener, |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 56 | DragSource { |
| 57 | |
| 58 | public enum CustomizationType { |
| 59 | WidgetCustomization, |
| 60 | FolderCustomization, |
| 61 | ShortcutCustomization, |
| 62 | WallpaperCustomization |
| 63 | } |
| 64 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 65 | /** |
| 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 86 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 95 | // 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 108 | private List<AppWidgetProviderInfo> mWidgetList; |
| 109 | private List<ResolveInfo> mFolderList; |
| 110 | private List<ResolveInfo> mShortcutList; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame^] | 111 | private List<ResolveInfo> mWallpaperList; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 112 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 113 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 120 | |
| 121 | private final Canvas mCanvas = new Canvas(); |
| 122 | private final LayoutInflater mInflater; |
| 123 | |
| 124 | public CustomizePagedView(Context context) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 125 | this(context, null, 0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | public CustomizePagedView(Context context, AttributeSet attrs) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 129 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 137 | mCustomizationType = CustomizationType.WidgetCustomization; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 138 | mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>(); |
| 139 | mWorkspaceWidgetLayout = new PagedViewCellLayout(context); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 140 | mInflater = LayoutInflater.from(context); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 141 | mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8); |
| 142 | a.recycle(); |
| 143 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 144 | setVisibility(View.GONE); |
| 145 | setSoundEffectsEnabled(false); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 146 | setupWorkspaceLayout(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 147 | } |
| 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 Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame^] | 192 | // 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 Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 197 | // reset the icon cache |
| 198 | mPageViewIconCache.clear(); |
| 199 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 200 | // Refresh all the tabs |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 201 | invalidatePageData(); |
| 202 | } |
| 203 | |
| 204 | public void setDragController(DragController dragger) { |
| 205 | mDragController = dragger; |
| 206 | } |
| 207 | |
| 208 | public void setCustomizationFilter(CustomizationType filterType) { |
| 209 | mCustomizationType = filterType; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 210 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 211 | invalidatePageData(); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void onDropCompleted(View target, boolean success) { |
| 216 | // do nothing |
| 217 | } |
| 218 | |
| 219 | @Override |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame^] | 220 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 253 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 261 | // 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 Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 268 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 269 | AppWidgetProviderInfo appWidgetInfo = (AppWidgetProviderInfo) v.getTag(); |
| 270 | LauncherAppWidgetInfo dragInfo = new LauncherAppWidgetInfo(appWidgetInfo.provider); |
| 271 | dragInfo.minWidth = appWidgetInfo.minWidth; |
| 272 | dragInfo.minHeight = appWidgetInfo.minHeight; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 273 | mDragController.startDrag(v, b, this, dragInfo, DragController.DRAG_ACTION_COPY, null); |
| 274 | |
| 275 | // Cleanup the icon |
| 276 | b.recycle(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 277 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 327 | /** |
| 328 | * Pre-processes the layout of the different widget pages. |
| 329 | * @return the number of pages of widgets that we have |
| 330 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 331 | private int relayoutWidgets() { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 332 | if (mWidgetList.isEmpty()) return 0; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 333 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 334 | // create a new page for the first set of widgets |
| 335 | ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 336 | mWidgetPages.clear(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 337 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 343 | for (int i = 0; i < widgetCount; ++i) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 344 | final AppWidgetProviderInfo info = mWidgetList.get(i); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 345 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 346 | // determine the size of the current widget |
| 347 | int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan, |
| 348 | mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth))); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 349 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 350 | // create a new page if necessary |
| 351 | if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) { |
| 352 | numCellsInRow = 0; |
| 353 | newPage = new ArrayList<AppWidgetProviderInfo>(); |
| 354 | mWidgetPages.add(newPage); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 357 | // add the item to the current page |
| 358 | newPage.add(info); |
| 359 | numCellsInRow += cellSpanX; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 360 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 361 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 362 | return mWidgetPages.size(); |
| 363 | } |
| 364 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 365 | /** |
| 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 371 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 388 | // Create a new bitmap to hold the widget preview |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 389 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 393 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 406 | if (info.icon > 0) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 407 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 413 | final int iconSize = minDim / 2; |
| 414 | final int offset = iconSize / 4; |
| 415 | icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 416 | 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 Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 421 | drawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 422 | } |
| 423 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); |
| 424 | return drawable; |
| 425 | } |
| 426 | |
| 427 | private void setupPage(PagedViewCellLayout layout) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 428 | layout.setCellCount(sCellCountX, sCellCountY); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 429 | layout.setPadding(20, 10, 20, 0); |
| 430 | } |
| 431 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 432 | private void setupWorkspaceLayout() { |
| 433 | mWorkspaceWidgetLayout.setCellCount(sCellCountX, sCellCountY); |
| 434 | mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0); |
| 435 | |
| 436 | mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan); |
| 437 | } |
| 438 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 439 | private void syncWidgetPages() { |
| 440 | if (mWidgetList == null) return; |
| 441 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 442 | // we need to repopulate with the LinearLayout layout for the widget pages |
| 443 | removeAllViews(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 444 | int numPages = relayoutWidgets(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 445 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | private void syncWidgetPageItems(int page) { |
| 464 | // ensure that we have the right number of items on the pages |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 465 | LinearLayout layout = (LinearLayout) getChildAt(page); |
| 466 | final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 467 | final int count = list.size(); |
| 468 | layout.removeAllViews(); |
| 469 | for (int i = 0; i < count; ++i) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 470 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 475 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 476 | 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 Chung | 3a47678 | 2010-09-15 15:21:55 -0700 | [diff] [blame] | 486 | dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan)); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 487 | |
| 488 | layout.addView(l); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | private void syncListPages(List<ResolveInfo> list) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 493 | // we need to repopulate with PagedViewCellLayouts |
| 494 | removeAllViews(); |
| 495 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 496 | // ensure that we have the right number of pages |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 497 | int numPages = (int) Math.ceil((float) list.size() / (sCellCountX * sCellCountY)); |
| 498 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 499 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 507 | int numCells = sCellCountX * sCellCountY; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 508 | 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 Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 515 | PagedViewIcon icon = (PagedViewIcon) mInflater.inflate( |
| 516 | R.layout.customize_paged_view_item, layout, false); |
| 517 | icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache); |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame^] | 518 | if (mCustomizationType == CustomizationType.WallpaperCustomization) { |
| 519 | icon.setOnClickListener(this); |
| 520 | } else { |
| 521 | icon.setOnLongClickListener(this); |
| 522 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 523 | |
| 524 | final int index = i - startIndex; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 525 | final int x = index % sCellCountX; |
| 526 | final int y = index / sCellCountX; |
| 527 | setupPage(layout); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 528 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 532 | @Override |
| 533 | public void syncPages() { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 534 | boolean centerPagedViewCellLayouts = false; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 535 | switch (mCustomizationType) { |
| 536 | case WidgetCustomization: |
| 537 | syncWidgetPages(); |
| 538 | break; |
| 539 | case FolderCustomization: |
| 540 | syncListPages(mFolderList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 541 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 542 | break; |
| 543 | case ShortcutCustomization: |
| 544 | syncListPages(mShortcutList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 545 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 546 | break; |
| 547 | case WallpaperCustomization: |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame^] | 548 | syncListPages(mWallpaperList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 549 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 550 | break; |
| 551 | default: |
| 552 | removeAllViews(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 553 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 554 | break; |
| 555 | } |
| 556 | |
| 557 | // only try and center the page if there is one page |
| 558 | final int childCount = getChildCount(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 559 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | // bound the current page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 572 | setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage()))); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 573 | } |
| 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 Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame^] | 588 | syncListPageItems(page, mWallpaperList); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 589 | break; |
| 590 | } |
| 591 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 592 | |
| 593 | protected int getAssociatedLowerPageBound(int page) { |
| 594 | return 0; |
| 595 | } |
| 596 | protected int getAssociatedUpperPageBound(int page) { |
| 597 | return getChildCount(); |
| 598 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 599 | } |