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