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 |
| 55 | implements View.OnLongClickListener, |
| 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; |
| 111 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 112 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 119 | |
| 120 | private final Canvas mCanvas = new Canvas(); |
| 121 | private final LayoutInflater mInflater; |
| 122 | |
| 123 | public CustomizePagedView(Context context) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 124 | this(context, null, 0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public CustomizePagedView(Context context, AttributeSet attrs) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 128 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 136 | mCustomizationType = CustomizationType.WidgetCustomization; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 137 | mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>(); |
| 138 | mWorkspaceWidgetLayout = new PagedViewCellLayout(context); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 139 | mInflater = LayoutInflater.from(context); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 140 | mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8); |
| 141 | a.recycle(); |
| 142 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 143 | setVisibility(View.GONE); |
| 144 | setSoundEffectsEnabled(false); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 145 | setupWorkspaceLayout(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 146 | } |
| 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 Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 191 | // reset the icon cache |
| 192 | mPageViewIconCache.clear(); |
| 193 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 194 | // Refresh all the tabs |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 195 | invalidatePageData(); |
| 196 | } |
| 197 | |
| 198 | public void setDragController(DragController dragger) { |
| 199 | mDragController = dragger; |
| 200 | } |
| 201 | |
| 202 | public void setCustomizationFilter(CustomizationType filterType) { |
| 203 | mCustomizationType = filterType; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 204 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 205 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 222 | // 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 Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 229 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 230 | AppWidgetProviderInfo appWidgetInfo = (AppWidgetProviderInfo) v.getTag(); |
| 231 | LauncherAppWidgetInfo dragInfo = new LauncherAppWidgetInfo(appWidgetInfo.provider); |
| 232 | dragInfo.minWidth = appWidgetInfo.minWidth; |
| 233 | dragInfo.minHeight = appWidgetInfo.minHeight; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 234 | mDragController.startDrag(v, b, this, dragInfo, DragController.DRAG_ACTION_COPY, null); |
| 235 | |
| 236 | // Cleanup the icon |
| 237 | b.recycle(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 238 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 288 | /** |
| 289 | * Pre-processes the layout of the different widget pages. |
| 290 | * @return the number of pages of widgets that we have |
| 291 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 292 | private int relayoutWidgets() { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 293 | if (mWidgetList.isEmpty()) return 0; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 294 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 295 | // create a new page for the first set of widgets |
| 296 | ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 297 | mWidgetPages.clear(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 298 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 304 | for (int i = 0; i < widgetCount; ++i) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 305 | final AppWidgetProviderInfo info = mWidgetList.get(i); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 306 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 307 | // determine the size of the current widget |
| 308 | int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan, |
| 309 | mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth))); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 310 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 311 | // create a new page if necessary |
| 312 | if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) { |
| 313 | numCellsInRow = 0; |
| 314 | newPage = new ArrayList<AppWidgetProviderInfo>(); |
| 315 | mWidgetPages.add(newPage); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 318 | // add the item to the current page |
| 319 | newPage.add(info); |
| 320 | numCellsInRow += cellSpanX; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 321 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 322 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 323 | return mWidgetPages.size(); |
| 324 | } |
| 325 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 326 | /** |
| 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 332 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 349 | // Create a new bitmap to hold the widget preview |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 350 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 354 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 367 | if (info.icon > 0) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 368 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 374 | final int iconSize = minDim / 2; |
| 375 | final int offset = iconSize / 4; |
| 376 | icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 377 | 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 Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 382 | drawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 383 | } |
| 384 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); |
| 385 | return drawable; |
| 386 | } |
| 387 | |
| 388 | private void setupPage(PagedViewCellLayout layout) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 389 | layout.setCellCount(sCellCountX, sCellCountY); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 390 | layout.setPadding(20, 10, 20, 0); |
| 391 | } |
| 392 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 393 | private void setupWorkspaceLayout() { |
| 394 | mWorkspaceWidgetLayout.setCellCount(sCellCountX, sCellCountY); |
| 395 | mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0); |
| 396 | |
| 397 | mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan); |
| 398 | } |
| 399 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 400 | private void syncWidgetPages() { |
| 401 | if (mWidgetList == null) return; |
| 402 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 403 | // we need to repopulate with the LinearLayout layout for the widget pages |
| 404 | removeAllViews(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 405 | int numPages = relayoutWidgets(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 406 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | private void syncWidgetPageItems(int page) { |
| 425 | // ensure that we have the right number of items on the pages |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 426 | LinearLayout layout = (LinearLayout) getChildAt(page); |
| 427 | final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 428 | final int count = list.size(); |
| 429 | layout.removeAllViews(); |
| 430 | for (int i = 0; i < count; ++i) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 431 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 436 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 437 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | private void syncListPages(List<ResolveInfo> list) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 454 | // we need to repopulate with PagedViewCellLayouts |
| 455 | removeAllViews(); |
| 456 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 457 | // ensure that we have the right number of pages |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 458 | int numPages = (int) Math.ceil((float) list.size() / (sCellCountX * sCellCountY)); |
| 459 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 460 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 468 | int numCells = sCellCountX * sCellCountY; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 469 | 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 Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 476 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 480 | |
| 481 | final int index = i - startIndex; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 482 | final int x = index % sCellCountX; |
| 483 | final int y = index / sCellCountX; |
| 484 | setupPage(layout); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 485 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | private void syncWallpaperPages() { |
| 490 | // NOT CURRENTLY IMPLEMENTED |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 491 | |
| 492 | // we need to repopulate with PagedViewCellLayouts |
| 493 | removeAllViews(); |
| 494 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 495 | // add any necessary pages |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 496 | int numPages = 1; |
| 497 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 498 | 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 513 | setupPage(layout); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 514 | layout.addViewToCellLayout(text, -1, 0, new PagedViewCellLayout.LayoutParams(0, 0, 3, 1)); |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public void syncPages() { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 519 | boolean centerPagedViewCellLayouts = false; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 520 | switch (mCustomizationType) { |
| 521 | case WidgetCustomization: |
| 522 | syncWidgetPages(); |
| 523 | break; |
| 524 | case FolderCustomization: |
| 525 | syncListPages(mFolderList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 526 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 527 | break; |
| 528 | case ShortcutCustomization: |
| 529 | syncListPages(mShortcutList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 530 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 531 | break; |
| 532 | case WallpaperCustomization: |
| 533 | syncWallpaperPages(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 534 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 535 | break; |
| 536 | default: |
| 537 | removeAllViews(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 538 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 539 | break; |
| 540 | } |
| 541 | |
| 542 | // only try and center the page if there is one page |
| 543 | final int childCount = getChildCount(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 544 | 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 Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | // bound the current page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 557 | setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage()))); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 558 | } |
| 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 Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame^] | 577 | |
| 578 | protected int getAssociatedLowerPageBound(int page) { |
| 579 | return 0; |
| 580 | } |
| 581 | protected int getAssociatedUpperPageBound(int page) { |
| 582 | return getChildCount(); |
| 583 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 584 | } |