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