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