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