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 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 19 | import com.android.launcher.R; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 20 | |
| 21 | import android.appwidget.AppWidgetManager; |
| 22 | import android.appwidget.AppWidgetProviderInfo; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.content.res.Resources; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 29 | import android.content.res.TypedArray; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 30 | import android.graphics.Bitmap; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 31 | import android.graphics.Canvas; |
| 32 | import android.graphics.Rect; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 33 | import android.graphics.Bitmap.Config; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 34 | import android.graphics.Region.Op; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 35 | import android.graphics.drawable.Drawable; |
| 36 | import android.provider.LiveFolders; |
| 37 | import android.util.AttributeSet; |
| 38 | import android.util.Log; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 39 | import android.view.ActionMode; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 40 | import android.view.Gravity; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 41 | import android.view.LayoutInflater; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 42 | import android.view.Menu; |
| 43 | import android.view.MenuItem; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 44 | import android.view.MotionEvent; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 45 | import android.view.View; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 46 | import android.view.ViewGroup; |
| 47 | import android.widget.Checkable; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 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 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | import java.util.Collections; |
| 54 | import java.util.Comparator; |
| 55 | import java.util.List; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 56 | |
| 57 | public class CustomizePagedView extends PagedView |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 58 | implements View.OnLongClickListener, View.OnClickListener, |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 59 | DragSource, ActionMode.Callback { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 60 | |
| 61 | public enum CustomizationType { |
| 62 | WidgetCustomization, |
| 63 | FolderCustomization, |
| 64 | ShortcutCustomization, |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 65 | WallpaperCustomization, |
| 66 | ApplicationCustomization |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 69 | /** |
| 70 | * The linear layout used strictly for the widget tab of the customization tray |
| 71 | */ |
| 72 | private class WidgetLayout extends LinearLayout { |
| 73 | public WidgetLayout(Context context) { |
| 74 | super(context); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean onTouchEvent(MotionEvent event) { |
| 79 | // We eat up the touch events here, since the PagedView (which uses the same swiping |
| 80 | // touch code as Workspace previously) uses onInterceptTouchEvent() to determine when |
| 81 | // the user is scrolling between pages. This means that if the pages themselves don't |
| 82 | // handle touch events, it gets forwarded up to PagedView itself, and it's own |
| 83 | // onTouchEvent() handling will prevent further intercept touch events from being called |
| 84 | // (it's the same view in that case). This is not ideal, but to prevent more changes, |
| 85 | // we just always mark the touch event as handled. |
| 86 | return super.onTouchEvent(event) || true; |
| 87 | } |
| 88 | } |
| 89 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 90 | private static final String TAG = "CustomizeWorkspace"; |
| 91 | private static final boolean DEBUG = false; |
| 92 | |
| 93 | private Launcher mLauncher; |
| 94 | private DragController mDragController; |
| 95 | private PackageManager mPackageManager; |
| 96 | |
| 97 | private CustomizationType mCustomizationType; |
| 98 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 99 | // The layout used to emulate the workspace in resolve the cell dimensions of a widget |
| 100 | private PagedViewCellLayout mWorkspaceWidgetLayout; |
| 101 | |
| 102 | // The mapping between the pages and the widgets that will be laid out on them |
| 103 | private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages; |
| 104 | |
| 105 | // The max dimensions for the ImageView we use for displaying the widget |
| 106 | private int mMaxWidgetWidth; |
| 107 | |
| 108 | // The max number of widget cells to take a "page" of widget |
| 109 | private int mMaxWidgetsCellHSpan; |
| 110 | |
| 111 | // 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] | 112 | private List<AppWidgetProviderInfo> mWidgetList; |
| 113 | private List<ResolveInfo> mFolderList; |
| 114 | private List<ResolveInfo> mShortcutList; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 115 | private List<ResolveInfo> mWallpaperList; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 116 | private List<ApplicationInfo> mApps; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 117 | |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 118 | private int mCellCountX; |
| 119 | private int mCellCountY; |
| 120 | private int mPageLayoutPaddingTop; |
| 121 | private int mPageLayoutPaddingBottom; |
| 122 | private int mPageLayoutPaddingLeft; |
| 123 | private int mPageLayoutPaddingRight; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 124 | private static final int sMinWidgetCellHSpan = 2; |
| 125 | private static final int sMaxWidgetCellHSpan = 4; |
| 126 | |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 127 | private int mChoiceModeTitleText; |
| 128 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 129 | // The scale factor for widget previews inside the widget drawer |
| 130 | private static final float sScaleFactor = 0.75f; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 131 | |
| 132 | private final Canvas mCanvas = new Canvas(); |
| 133 | private final LayoutInflater mInflater; |
| 134 | |
| 135 | public CustomizePagedView(Context context) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 136 | this(context, null, 0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public CustomizePagedView(Context context, AttributeSet attrs) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 140 | this(context, attrs, 0); |
| 141 | } |
| 142 | |
| 143 | public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) { |
| 144 | super(context, attrs, defStyle); |
| 145 | |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 146 | TypedArray a; |
| 147 | a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 148 | defStyle, 0); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 149 | mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8); |
| 150 | a.recycle(); |
| 151 | a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0); |
| 152 | mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7); |
| 153 | mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4); |
| 154 | mPageLayoutPaddingTop = a.getDimensionPixelSize( |
| 155 | R.styleable.PagedView_pageLayoutPaddingTop, 10); |
| 156 | mPageLayoutPaddingBottom = a.getDimensionPixelSize( |
| 157 | R.styleable.PagedView_pageLayoutPaddingBottom, 10); |
| 158 | mPageLayoutPaddingLeft = a.getDimensionPixelSize( |
| 159 | R.styleable.PagedView_pageLayoutPaddingLeft, 10); |
| 160 | mPageLayoutPaddingRight = a.getDimensionPixelSize( |
| 161 | R.styleable.PagedView_pageLayoutPaddingRight, 10); |
| 162 | a.recycle(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 163 | mCustomizationType = CustomizationType.WidgetCustomization; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 164 | mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>(); |
| 165 | mWorkspaceWidgetLayout = new PagedViewCellLayout(context); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 166 | mInflater = LayoutInflater.from(context); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 167 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 168 | setVisibility(View.GONE); |
| 169 | setSoundEffectsEnabled(false); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 170 | setupWorkspaceLayout(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | public void setLauncher(Launcher launcher) { |
| 174 | Context context = getContext(); |
| 175 | mLauncher = launcher; |
| 176 | mPackageManager = context.getPackageManager(); |
| 177 | } |
| 178 | |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 179 | /** |
| 180 | * Sets the list of applications that launcher has loaded. |
| 181 | */ |
| 182 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 183 | mApps = list; |
| 184 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame^] | 185 | |
| 186 | // Update the widgets/shortcuts to reflect changes in the set of available apps |
| 187 | update(); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Convenience function to add new items to the set of applications that were previously loaded. |
| 192 | * Called by both updateApps() and setApps(). |
| 193 | */ |
| 194 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 195 | // we add it in place, in alphabetical order |
| 196 | final int count = list.size(); |
| 197 | for (int i = 0; i < count; ++i) { |
| 198 | final ApplicationInfo info = list.get(i); |
| 199 | final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 200 | if (index < 0) { |
| 201 | mApps.add(-(index + 1), info); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Adds new applications to the loaded list, and notifies the paged view to update itself. |
| 208 | */ |
| 209 | public void addApps(ArrayList<ApplicationInfo> list) { |
| 210 | addAppsWithoutInvalidate(list); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame^] | 211 | |
| 212 | // Update the widgets/shortcuts to reflect changes in the set of available apps |
| 213 | update(); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Convenience function to remove items to the set of applications that were previously loaded. |
| 218 | * Called by both updateApps() and removeApps(). |
| 219 | */ |
| 220 | private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 221 | // loop through all the apps and remove apps that have the same component |
| 222 | final int length = list.size(); |
| 223 | for (int i = 0; i < length; ++i) { |
| 224 | final ApplicationInfo info = list.get(i); |
| 225 | int removeIndex = findAppByComponent(mApps, info); |
| 226 | if (removeIndex > -1) { |
| 227 | mApps.remove(removeIndex); |
| 228 | mPageViewIconCache.removeOutline(info); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Removes applications from the loaded list, and notifies the paged view to update itself. |
| 235 | */ |
| 236 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 237 | removeAppsWithoutInvalidate(list); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame^] | 238 | |
| 239 | // Update the widgets/shortcuts to reflect changes in the set of available apps |
| 240 | update(); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Updates a set of applications from the loaded list, and notifies the paged view to update |
| 245 | * itself. |
| 246 | */ |
| 247 | public void updateApps(ArrayList<ApplicationInfo> list) { |
| 248 | // We remove and re-add the updated applications list because it's properties may have |
| 249 | // changed (ie. the title), and this will ensure that the items will be in their proper |
| 250 | // place in the list. |
| 251 | removeAppsWithoutInvalidate(list); |
| 252 | addAppsWithoutInvalidate(list); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame^] | 253 | |
| 254 | // Update the widgets/shortcuts to reflect changes in the set of available apps |
| 255 | update(); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Convenience function to find matching ApplicationInfos for removal. |
| 260 | */ |
| 261 | private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) { |
| 262 | ComponentName removeComponent = item.intent.getComponent(); |
| 263 | final int length = list.size(); |
| 264 | for (int i = 0; i < length; ++i) { |
| 265 | ApplicationInfo info = list.get(i); |
| 266 | if (info.intent.getComponent().equals(removeComponent)) { |
| 267 | return i; |
| 268 | } |
| 269 | } |
| 270 | return -1; |
| 271 | } |
| 272 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 273 | public void update() { |
| 274 | Context context = getContext(); |
| 275 | |
| 276 | // get the list of widgets |
| 277 | mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders(); |
| 278 | Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() { |
| 279 | @Override |
| 280 | public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) { |
| 281 | return object1.label.compareTo(object2.label); |
| 282 | } |
| 283 | }); |
| 284 | |
| 285 | Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() { |
| 286 | @Override |
| 287 | public int compare(ResolveInfo object1, ResolveInfo object2) { |
| 288 | return object1.loadLabel(mPackageManager).toString().compareTo( |
| 289 | object2.loadLabel(mPackageManager).toString()); |
| 290 | } |
| 291 | }; |
| 292 | |
| 293 | // get the list of live folder intents |
| 294 | Intent liveFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER); |
| 295 | mFolderList = mPackageManager.queryIntentActivities(liveFolderIntent, 0); |
| 296 | |
| 297 | // manually create a separate entry for creating a folder in Launcher |
| 298 | ResolveInfo folder = new ResolveInfo(); |
| 299 | folder.icon = R.drawable.ic_launcher_folder; |
| 300 | folder.labelRes = R.string.group_folder; |
| 301 | folder.resolvePackageName = context.getPackageName(); |
| 302 | mFolderList.add(0, folder); |
| 303 | Collections.sort(mFolderList, resolveInfoComparator); |
| 304 | |
| 305 | // get the list of shortcuts |
| 306 | Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); |
| 307 | mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0); |
| 308 | Collections.sort(mShortcutList, resolveInfoComparator); |
| 309 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 310 | // get the list of wallpapers |
| 311 | Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER); |
| 312 | mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0); |
| 313 | Collections.sort(mWallpaperList, resolveInfoComparator); |
| 314 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 315 | // reset the icon cache |
| 316 | mPageViewIconCache.clear(); |
| 317 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 318 | // Refresh all the tabs |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 319 | invalidatePageData(); |
| 320 | } |
| 321 | |
| 322 | public void setDragController(DragController dragger) { |
| 323 | mDragController = dragger; |
| 324 | } |
| 325 | |
| 326 | public void setCustomizationFilter(CustomizationType filterType) { |
| 327 | mCustomizationType = filterType; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 328 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 329 | invalidatePageData(); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 330 | |
| 331 | // End the current choice mode so that we don't carry selections across tabs |
| 332 | endChoiceMode(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public void onDropCompleted(View target, boolean success) { |
| 337 | // do nothing |
| 338 | } |
| 339 | |
| 340 | @Override |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 341 | public void onClick(View v) { |
| 342 | if (!v.isInTouchMode()) { |
| 343 | return; |
| 344 | } |
| 345 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 346 | // On certain pages, we allow single tap to mark items as selected so that they can be |
| 347 | // dropped onto the mini workspaces |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 348 | boolean enterChoiceMode = false; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 349 | switch (mCustomizationType) { |
| 350 | case WidgetCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 351 | mChoiceModeTitleText = R.string.cab_widget_selection_text; |
| 352 | enterChoiceMode = true; |
| 353 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 354 | case ApplicationCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 355 | mChoiceModeTitleText = R.string.cab_app_selection_text; |
| 356 | enterChoiceMode = true; |
| 357 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 358 | case FolderCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 359 | mChoiceModeTitleText = R.string.cab_folder_selection_text; |
| 360 | enterChoiceMode = true; |
| 361 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 362 | case ShortcutCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 363 | mChoiceModeTitleText = R.string.cab_shortcut_selection_text; |
| 364 | enterChoiceMode = true; |
| 365 | break; |
| 366 | default: |
| 367 | break; |
| 368 | } |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 369 | |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 370 | if (enterChoiceMode) { |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 371 | if (v instanceof Checkable) { |
| 372 | final Checkable c = (Checkable) v; |
| 373 | final boolean wasChecked = c.isChecked(); |
| 374 | resetCheckedGrandchildren(); |
| 375 | c.setChecked(!wasChecked); |
| 376 | |
| 377 | // End the current choice mode when we have no items selected |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 378 | /*if (!c.isChecked()) { |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 379 | endChoiceMode(); |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 380 | } else if (isChoiceMode(CHOICE_MODE_NONE)) { |
| 381 | endChoiceMode(); |
| 382 | startChoiceMode(CHOICE_MODE_SINGLE, this); |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 383 | }*/ |
| 384 | mChoiceMode = CHOICE_MODE_SINGLE; |
| 385 | |
| 386 | Workspace w = mLauncher.getWorkspace(); |
| 387 | int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen(); |
| 388 | final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen); |
| 389 | cl.setHover(true); |
| 390 | |
| 391 | animateClickFeedback(v, new Runnable() { |
| 392 | @Override |
| 393 | public void run() { |
| 394 | cl.setHover(false); |
| 395 | mLauncher.onWorkspaceClick(cl); |
| 396 | mChoiceMode = CHOICE_MODE_NONE; |
| 397 | } |
| 398 | }); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 399 | } |
| 400 | return; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // Otherwise, we just handle the single click here |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 404 | switch (mCustomizationType) { |
| 405 | case WallpaperCustomization: |
| 406 | // animate some feedback to the long press |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 407 | final View clickView = v; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 408 | animateClickFeedback(v, new Runnable() { |
| 409 | @Override |
| 410 | public void run() { |
| 411 | // add the shortcut |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 412 | ResolveInfo info = (ResolveInfo) clickView.getTag(); |
Winson Chung | 24ab2f1 | 2010-09-16 14:10:47 -0700 | [diff] [blame] | 413 | Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER); |
| 414 | ComponentName name = new ComponentName(info.activityInfo.packageName, |
| 415 | info.activityInfo.name); |
| 416 | createWallpapersIntent.setComponent(name); |
| 417 | mLauncher.processWallpaper(createWallpapersIntent); |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 418 | } |
| 419 | }); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 420 | break; |
| 421 | default: |
| 422 | break; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | @Override |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 427 | public boolean onLongClick(View v) { |
| 428 | if (!v.isInTouchMode()) { |
| 429 | return false; |
| 430 | } |
| 431 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 432 | // End the current choice mode before we start dragging anything |
| 433 | if (isChoiceMode(CHOICE_MODE_SINGLE)) { |
| 434 | endChoiceMode(); |
| 435 | } |
| 436 | |
| 437 | PendingAddItemInfo createItemInfo; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 438 | switch (mCustomizationType) { |
| 439 | case WidgetCustomization: |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 440 | // Get the icon as the drag representation |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 441 | final LinearLayout l = (LinearLayout) v; |
| 442 | final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 443 | Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), |
| 444 | Bitmap.Config.ARGB_8888); |
| 445 | Canvas c = new Canvas(b); |
| 446 | icon.draw(c); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 447 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 448 | createItemInfo = (PendingAddItemInfo) v.getTag(); |
| 449 | mDragController.startDrag(v, b, this, createItemInfo, DragController.DRAG_ACTION_COPY, |
| 450 | null); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 451 | |
| 452 | // Cleanup the icon |
| 453 | b.recycle(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 454 | return true; |
| 455 | case FolderCustomization: |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 456 | if (v.getTag() instanceof UserFolderInfo) { |
| 457 | // The UserFolderInfo tag is only really used for live folders |
| 458 | UserFolderInfo folderInfo = (UserFolderInfo) v.getTag(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 459 | mDragController.startDrag( |
| 460 | v, this, folderInfo, DragController.DRAG_ACTION_COPY, null); |
| 461 | } else { |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 462 | createItemInfo = (PendingAddItemInfo) v.getTag(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 463 | mDragController.startDrag( |
| 464 | v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null); |
| 465 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 466 | return true; |
| 467 | case ShortcutCustomization: |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 468 | createItemInfo = (PendingAddItemInfo) v.getTag(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 469 | mDragController.startDrag( |
| 470 | v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 471 | return true; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 472 | case ApplicationCustomization: |
| 473 | // Pick up the application for dropping |
| 474 | ApplicationInfo app = (ApplicationInfo) v.getTag(); |
| 475 | app = new ApplicationInfo(app); |
| 476 | |
| 477 | mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY); |
| 478 | return true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 479 | } |
| 480 | return false; |
| 481 | } |
| 482 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 483 | /** |
| 484 | * Pre-processes the layout of the different widget pages. |
| 485 | * @return the number of pages of widgets that we have |
| 486 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 487 | private int relayoutWidgets() { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 488 | if (mWidgetList.isEmpty()) return 0; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 489 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 490 | // create a new page for the first set of widgets |
| 491 | ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 492 | mWidgetPages.clear(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 493 | mWidgetPages.add(newPage); |
| 494 | |
| 495 | // do this until we have no more widgets to lay out |
| 496 | final int maxNumCellsPerRow = mMaxWidgetsCellHSpan; |
| 497 | final int widgetCount = mWidgetList.size(); |
| 498 | int numCellsInRow = 0; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 499 | for (int i = 0; i < widgetCount; ++i) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 500 | final AppWidgetProviderInfo info = mWidgetList.get(i); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 501 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 502 | // determine the size of the current widget |
| 503 | int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan, |
| 504 | mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth))); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 505 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 506 | // create a new page if necessary |
| 507 | if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) { |
| 508 | numCellsInRow = 0; |
| 509 | newPage = new ArrayList<AppWidgetProviderInfo>(); |
| 510 | mWidgetPages.add(newPage); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 513 | // add the item to the current page |
| 514 | newPage.add(info); |
| 515 | numCellsInRow += cellSpanX; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 516 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 517 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 518 | return mWidgetPages.size(); |
| 519 | } |
| 520 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 521 | /** |
| 522 | * This method will extract the preview image specified by the widget developer (if it exists), |
| 523 | * otherwise, it will try to generate a default image preview with the widget's package icon. |
| 524 | * @return the drawable will be used and sized in the ImageView to represent the widget |
| 525 | */ |
| 526 | private Drawable getWidgetIcon(AppWidgetProviderInfo info) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 527 | PackageManager packageManager = mLauncher.getPackageManager(); |
| 528 | String packageName = info.provider.getPackageName(); |
| 529 | Drawable drawable = null; |
| 530 | if (info.previewImage != 0) { |
| 531 | drawable = packageManager.getDrawable(packageName, info.previewImage, null); |
| 532 | if (drawable == null) { |
| 533 | Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon) |
| 534 | + " for provider: " + info.provider); |
| 535 | } else { |
| 536 | return drawable; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // If we don't have a preview image, create a default one |
| 541 | if (drawable == null) { |
| 542 | Resources resources = mLauncher.getResources(); |
| 543 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 544 | // Create a new bitmap to hold the widget preview |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 545 | final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1); |
| 546 | final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3); |
| 547 | int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor); |
| 548 | int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 549 | Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); |
| 550 | mCanvas.setBitmap(bitmap); |
| 551 | // For some reason, we must re-set the clip rect here, otherwise it will be wrong |
| 552 | mCanvas.clipRect(0, 0, width, height, Op.REPLACE); |
| 553 | |
| 554 | Drawable background = resources.getDrawable(R.drawable.default_widget_preview); |
| 555 | background.setBounds(0, 0, width, height); |
| 556 | background.draw(mCanvas); |
| 557 | |
| 558 | // Draw the icon vertically centered, flush left |
| 559 | try { |
| 560 | Rect tmpRect = new Rect(); |
| 561 | Drawable icon = null; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 562 | if (info.icon > 0) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 563 | icon = packageManager.getDrawable(packageName, info.icon, null); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame^] | 564 | } |
| 565 | if (icon == null) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 566 | icon = resources.getDrawable(R.drawable.ic_launcher_application); |
| 567 | } |
| 568 | background.getPadding(tmpRect); |
| 569 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 570 | final int iconSize = minDim / 2; |
| 571 | final int offset = iconSize / 4; |
| 572 | icon.setBounds(new Rect(offset, offset, offset + iconSize, offset + iconSize)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 573 | icon.draw(mCanvas); |
| 574 | } catch (Resources.NotFoundException e) { |
| 575 | // if we can't find the icon, then just don't draw it |
| 576 | } |
| 577 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 578 | drawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 579 | } |
| 580 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); |
| 581 | return drawable; |
| 582 | } |
| 583 | |
| 584 | private void setupPage(PagedViewCellLayout layout) { |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 585 | layout.setCellCount(mCellCountX, mCellCountY); |
| 586 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight, |
| 587 | mPageLayoutPaddingBottom); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 590 | private void setupWorkspaceLayout() { |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 591 | mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 592 | mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0); |
| 593 | |
| 594 | mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan); |
| 595 | } |
| 596 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 597 | private void syncWidgetPages() { |
| 598 | if (mWidgetList == null) return; |
| 599 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 600 | // we need to repopulate with the LinearLayout layout for the widget pages |
| 601 | removeAllViews(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 602 | int numPages = relayoutWidgets(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 603 | for (int i = 0; i < numPages; ++i) { |
| 604 | LinearLayout layout = new WidgetLayout(getContext()); |
| 605 | layout.setGravity(Gravity.CENTER_HORIZONTAL); |
| 606 | |
| 607 | // Temporary change to prevent the last page from being too small (and items bleeding |
| 608 | // onto it). We can remove this once we properly fix the fading algorithm |
| 609 | if (i < numPages - 1) { |
| 610 | addView(layout, new LinearLayout.LayoutParams( |
| 611 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 612 | LinearLayout.LayoutParams.MATCH_PARENT)); |
| 613 | } else { |
| 614 | addView(layout, new LinearLayout.LayoutParams( |
| 615 | LinearLayout.LayoutParams.MATCH_PARENT, |
| 616 | LinearLayout.LayoutParams.MATCH_PARENT)); |
| 617 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | private void syncWidgetPageItems(int page) { |
| 622 | // ensure that we have the right number of items on the pages |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 623 | LinearLayout layout = (LinearLayout) getChildAt(page); |
| 624 | final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 625 | final int count = list.size(); |
| 626 | layout.removeAllViews(); |
| 627 | for (int i = 0; i < count; ++i) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 628 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 629 | PendingAddItemInfo createItemInfo = new PendingAddItemInfo(); |
| 630 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET; |
| 631 | createItemInfo.componentName = info.provider; |
| 632 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 633 | LinearLayout l = (LinearLayout) mInflater.inflate( |
| 634 | R.layout.customize_paged_view_widget, layout, false); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 635 | l.setTag(createItemInfo); |
| 636 | l.setOnClickListener(this); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 637 | l.setOnLongClickListener(this); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 638 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 639 | final Drawable icon = getWidgetIcon(info); |
| 640 | final int hSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth); |
| 641 | final int vSpan = mWorkspaceWidgetLayout.estimateCellHSpan(info.minHeight); |
| 642 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 643 | ImageView image = (ImageView) l.findViewById(R.id.widget_preview); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 644 | image.setMaxWidth(mMaxWidgetWidth); |
| 645 | image.setImageDrawable(icon); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 646 | TextView name = (TextView) l.findViewById(R.id.widget_name); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 647 | name.setText(info.label); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 648 | TextView dims = (TextView) l.findViewById(R.id.widget_dims); |
Winson Chung | 3a47678 | 2010-09-15 15:21:55 -0700 | [diff] [blame] | 649 | dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan)); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 650 | |
| 651 | layout.addView(l); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
| 655 | private void syncListPages(List<ResolveInfo> list) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 656 | // we need to repopulate with PagedViewCellLayouts |
| 657 | removeAllViews(); |
| 658 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 659 | // ensure that we have the right number of pages |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 660 | int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY)); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 661 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 662 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 663 | setupPage(layout); |
| 664 | addView(layout); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | private void syncListPageItems(int page, List<ResolveInfo> list) { |
| 669 | // ensure that we have the right number of items on the pages |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 670 | int numCells = mCellCountX * mCellCountY; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 671 | int startIndex = page * numCells; |
| 672 | int endIndex = Math.min(startIndex + numCells, list.size()); |
| 673 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
| 674 | // TODO: we can optimize by just re-applying to existing views |
| 675 | layout.removeAllViews(); |
| 676 | for (int i = startIndex; i < endIndex; ++i) { |
| 677 | ResolveInfo info = list.get(i); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 678 | PendingAddItemInfo createItemInfo = new PendingAddItemInfo(); |
| 679 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 680 | PagedViewIcon icon = (PagedViewIcon) mInflater.inflate( |
| 681 | R.layout.customize_paged_view_item, layout, false); |
| 682 | icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 683 | switch (mCustomizationType) { |
| 684 | case WallpaperCustomization: |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 685 | icon.setOnClickListener(this); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 686 | break; |
| 687 | case FolderCustomization: |
| 688 | if (info.labelRes != R.string.group_folder) { |
| 689 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER; |
| 690 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 691 | info.activityInfo.name); |
| 692 | icon.setTag(createItemInfo); |
| 693 | } else { |
| 694 | UserFolderInfo folderInfo = new UserFolderInfo(); |
| 695 | folderInfo.title = getResources().getText(R.string.folder_name); |
| 696 | icon.setTag(folderInfo); |
| 697 | } |
| 698 | icon.setOnClickListener(this); |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 699 | icon.setOnLongClickListener(this); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 700 | break; |
| 701 | case ShortcutCustomization: |
| 702 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
| 703 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 704 | info.activityInfo.name); |
| 705 | icon.setTag(createItemInfo); |
| 706 | icon.setOnClickListener(this); |
| 707 | icon.setOnLongClickListener(this); |
| 708 | break; |
| 709 | default: |
| 710 | break; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 711 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 712 | |
| 713 | final int index = i - startIndex; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 714 | final int x = index % mCellCountX; |
| 715 | final int y = index / mCellCountX; |
| 716 | setupPage(layout); |
| 717 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | private void syncAppPages() { |
| 722 | if (mApps == null) return; |
| 723 | |
| 724 | // We need to repopulate with PagedViewCellLayouts |
| 725 | removeAllViews(); |
| 726 | |
| 727 | // Ensure that we have the right number of pages |
| 728 | int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); |
| 729 | for (int i = 0; i < numPages; ++i) { |
| 730 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 731 | setupPage(layout); |
| 732 | addView(layout); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | private void syncAppPageItems(int page) { |
| 737 | if (mApps == null) return; |
| 738 | |
| 739 | // ensure that we have the right number of items on the pages |
| 740 | int numCells = mCellCountX * mCellCountY; |
| 741 | int startIndex = page * numCells; |
| 742 | int endIndex = Math.min(startIndex + numCells, mApps.size()); |
| 743 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
| 744 | // TODO: we can optimize by just re-applying to existing views |
| 745 | layout.removeAllViews(); |
| 746 | for (int i = startIndex; i < endIndex; ++i) { |
| 747 | final ApplicationInfo info = mApps.get(i); |
| 748 | PagedViewIcon icon = (PagedViewIcon) mInflater.inflate( |
| 749 | R.layout.all_apps_paged_view_application, layout, false); |
| 750 | icon.applyFromApplicationInfo(info, mPageViewIconCache); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 751 | icon.setOnClickListener(this); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 752 | icon.setOnLongClickListener(this); |
| 753 | |
| 754 | final int index = i - startIndex; |
| 755 | final int x = index % mCellCountX; |
| 756 | final int y = index / mCellCountX; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 757 | setupPage(layout); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 758 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 762 | @Override |
| 763 | public void syncPages() { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 764 | boolean centerPagedViewCellLayouts = false; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 765 | switch (mCustomizationType) { |
| 766 | case WidgetCustomization: |
| 767 | syncWidgetPages(); |
| 768 | break; |
| 769 | case FolderCustomization: |
| 770 | syncListPages(mFolderList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 771 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 772 | break; |
| 773 | case ShortcutCustomization: |
| 774 | syncListPages(mShortcutList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 775 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 776 | break; |
| 777 | case WallpaperCustomization: |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 778 | syncListPages(mWallpaperList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 779 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 780 | break; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 781 | case ApplicationCustomization: |
| 782 | syncAppPages(); |
| 783 | centerPagedViewCellLayouts = false; |
| 784 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 785 | default: |
| 786 | removeAllViews(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 787 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 788 | break; |
| 789 | } |
| 790 | |
| 791 | // only try and center the page if there is one page |
| 792 | final int childCount = getChildCount(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 793 | if (centerPagedViewCellLayouts) { |
| 794 | if (childCount == 1) { |
| 795 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0); |
| 796 | layout.enableCenteredContent(true); |
| 797 | } else { |
| 798 | for (int i = 0; i < childCount; ++i) { |
| 799 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
| 800 | layout.enableCenteredContent(false); |
| 801 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
| 805 | // bound the current page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 806 | setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage()))); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | @Override |
| 810 | public void syncPageItems(int page) { |
| 811 | switch (mCustomizationType) { |
| 812 | case WidgetCustomization: |
| 813 | syncWidgetPageItems(page); |
| 814 | break; |
| 815 | case FolderCustomization: |
| 816 | syncListPageItems(page, mFolderList); |
| 817 | break; |
| 818 | case ShortcutCustomization: |
| 819 | syncListPageItems(page, mShortcutList); |
| 820 | break; |
| 821 | case WallpaperCustomization: |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 822 | syncListPageItems(page, mWallpaperList); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 823 | break; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 824 | case ApplicationCustomization: |
| 825 | syncAppPageItems(page); |
| 826 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 827 | } |
| 828 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 829 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 830 | @Override |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 831 | protected int getAssociatedLowerPageBound(int page) { |
| 832 | return 0; |
| 833 | } |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 834 | @Override |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 835 | protected int getAssociatedUpperPageBound(int page) { |
| 836 | return getChildCount(); |
| 837 | } |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 838 | |
| 839 | @Override |
| 840 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 841 | mode.setTitle(mChoiceModeTitleText); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 842 | return true; |
| 843 | } |
| 844 | |
| 845 | @Override |
| 846 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 847 | return true; |
| 848 | } |
| 849 | |
| 850 | @Override |
| 851 | public void onDestroyActionMode(ActionMode mode) { |
| 852 | endChoiceMode(); |
| 853 | } |
| 854 | |
| 855 | @Override |
| 856 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 857 | return false; |
| 858 | } |
| 859 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 860 | } |