Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.launcher2; |
| 18 | |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 19 | import com.android.launcher.R; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 20 | |
| 21 | import android.appwidget.AppWidgetManager; |
| 22 | import android.appwidget.AppWidgetProviderInfo; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.content.res.Resources; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 29 | import android.content.res.TypedArray; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 30 | import android.graphics.Bitmap; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 31 | import android.graphics.Canvas; |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 32 | import android.graphics.Bitmap.Config; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 33 | import android.graphics.drawable.Drawable; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 34 | import android.util.AttributeSet; |
| 35 | import android.util.Log; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 36 | import android.view.ActionMode; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 37 | import android.view.Gravity; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 38 | import android.view.LayoutInflater; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 39 | import android.view.Menu; |
| 40 | import android.view.MenuItem; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 41 | import android.view.MotionEvent; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 42 | import android.view.View; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 43 | import android.widget.ImageView; |
| 44 | import android.widget.LinearLayout; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 45 | |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 46 | import java.util.ArrayList; |
| 47 | import java.util.Collections; |
| 48 | import java.util.Comparator; |
| 49 | import java.util.List; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 50 | |
| 51 | public class CustomizePagedView extends PagedView |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 52 | implements View.OnLongClickListener, View.OnClickListener, View.OnTouchListener, |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 53 | DragSource, ActionMode.Callback { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 54 | |
| 55 | public enum CustomizationType { |
| 56 | WidgetCustomization, |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 57 | ShortcutCustomization, |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 58 | WallpaperCustomization, |
| 59 | ApplicationCustomization |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | private static final String TAG = "CustomizeWorkspace"; |
| 63 | private static final boolean DEBUG = false; |
| 64 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 65 | private View mLastTouchedItem; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 66 | private Launcher mLauncher; |
| 67 | private DragController mDragController; |
| 68 | private PackageManager mPackageManager; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 69 | private boolean mIsDragging; |
| 70 | private float mDragSlopeThreshold; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 71 | |
| 72 | private CustomizationType mCustomizationType; |
| 73 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 74 | // The layout used to emulate the workspace in resolve the cell dimensions of a widget |
| 75 | private PagedViewCellLayout mWorkspaceWidgetLayout; |
| 76 | |
| 77 | // The mapping between the pages and the widgets that will be laid out on them |
| 78 | private ArrayList<ArrayList<AppWidgetProviderInfo>> mWidgetPages; |
| 79 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 80 | // The max dimensions for the ImageView we use for displaying a widget |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 81 | private int mMaxWidgetWidth; |
| 82 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 83 | // The max number of widget cells to take a "page" of widgets |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 84 | private int mMaxWidgetsCellHSpan; |
| 85 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 86 | // The size of the items on the wallpaper tab |
| 87 | private int mWallpaperCellHSpan; |
| 88 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 89 | // 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] | 90 | private List<AppWidgetProviderInfo> mWidgetList; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 91 | private List<ResolveInfo> mShortcutList; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 92 | private List<ResolveInfo> mWallpaperList; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 93 | private List<ApplicationInfo> mApps; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 94 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 95 | private static final int sMinWidgetCellHSpan = 2; |
| 96 | private static final int sMaxWidgetCellHSpan = 4; |
| 97 | |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 98 | private int mChoiceModeTitleText; |
| 99 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 100 | // The scale factor for widget previews inside the widget drawer |
| 101 | private static final float sScaleFactor = 0.75f; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 102 | |
| 103 | private final Canvas mCanvas = new Canvas(); |
| 104 | private final LayoutInflater mInflater; |
| 105 | |
| 106 | public CustomizePagedView(Context context) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 107 | this(context, null, 0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | public CustomizePagedView(Context context, AttributeSet attrs) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 111 | this(context, attrs, 0); |
| 112 | } |
| 113 | |
| 114 | public CustomizePagedView(Context context, AttributeSet attrs, int defStyle) { |
| 115 | super(context, attrs, defStyle); |
| 116 | |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 117 | TypedArray a; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 118 | a = context.obtainStyledAttributes(attrs, R.styleable.CustomizePagedView, defStyle, 0); |
| 119 | mWallpaperCellHSpan = a.getInt(R.styleable.CustomizePagedView_wallpaperCellSpanX, 4); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 120 | mMaxWidgetsCellHSpan = a.getInt(R.styleable.CustomizePagedView_widgetCellCountX, 8); |
| 121 | a.recycle(); |
| 122 | a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0); |
| 123 | mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 7); |
| 124 | mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 125 | a.recycle(); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 126 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 127 | mCustomizationType = CustomizationType.WidgetCustomization; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 128 | mWidgetPages = new ArrayList<ArrayList<AppWidgetProviderInfo>>(); |
| 129 | mWorkspaceWidgetLayout = new PagedViewCellLayout(context); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 130 | mInflater = LayoutInflater.from(context); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 131 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 132 | final Resources r = context.getResources(); |
| 133 | mDragSlopeThreshold = |
| 134 | r.getInteger(R.integer.config_customizationDrawerDragSlopeThreshold) / 100.0f; |
| 135 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 136 | setVisibility(View.GONE); |
| 137 | setSoundEffectsEnabled(false); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 138 | setupWorkspaceLayout(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 141 | @Override |
| 142 | protected void init() { |
| 143 | super.init(); |
| 144 | mCenterPagesVertically = false; |
| 145 | } |
| 146 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 147 | public void setLauncher(Launcher launcher) { |
| 148 | Context context = getContext(); |
| 149 | mLauncher = launcher; |
| 150 | mPackageManager = context.getPackageManager(); |
| 151 | } |
| 152 | |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 153 | /** |
| 154 | * Sets the list of applications that launcher has loaded. |
| 155 | */ |
| 156 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 157 | mApps = list; |
| 158 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame] | 159 | |
| 160 | // 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] | 161 | invalidatePageDataAndIconCache(); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Convenience function to add new items to the set of applications that were previously loaded. |
| 166 | * Called by both updateApps() and setApps(). |
| 167 | */ |
| 168 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 169 | // we add it in place, in alphabetical order |
| 170 | final int count = list.size(); |
| 171 | for (int i = 0; i < count; ++i) { |
| 172 | final ApplicationInfo info = list.get(i); |
| 173 | final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 174 | if (index < 0) { |
| 175 | mApps.add(-(index + 1), info); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Adds new applications to the loaded list, and notifies the paged view to update itself. |
| 182 | */ |
| 183 | public void addApps(ArrayList<ApplicationInfo> list) { |
| 184 | addAppsWithoutInvalidate(list); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame] | 185 | |
| 186 | // Update the widgets/shortcuts to reflect changes in the set of available apps |
Winson Chung | 10fefb1 | 2010-11-01 11:57:06 -0700 | [diff] [blame] | 187 | invalidatePageDataAndIconCache(); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Convenience function to remove items to the set of applications that were previously loaded. |
| 192 | * Called by both updateApps() and removeApps(). |
| 193 | */ |
| 194 | private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 195 | // loop through all the apps and remove apps that have the same component |
| 196 | final int length = list.size(); |
| 197 | for (int i = 0; i < length; ++i) { |
| 198 | final ApplicationInfo info = list.get(i); |
| 199 | int removeIndex = findAppByComponent(mApps, info); |
| 200 | if (removeIndex > -1) { |
| 201 | mApps.remove(removeIndex); |
| 202 | mPageViewIconCache.removeOutline(info); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Removes applications from the loaded list, and notifies the paged view to update itself. |
| 209 | */ |
| 210 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 211 | removeAppsWithoutInvalidate(list); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame] | 212 | |
| 213 | // 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] | 214 | invalidatePageDataAndIconCache(); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Updates a set of applications from the loaded list, and notifies the paged view to update |
| 219 | * itself. |
| 220 | */ |
| 221 | public void updateApps(ArrayList<ApplicationInfo> list) { |
| 222 | // We remove and re-add the updated applications list because it's properties may have |
| 223 | // changed (ie. the title), and this will ensure that the items will be in their proper |
| 224 | // place in the list. |
| 225 | removeAppsWithoutInvalidate(list); |
| 226 | addAppsWithoutInvalidate(list); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame] | 227 | |
| 228 | // 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] | 229 | invalidatePageDataAndIconCache(); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Convenience function to find matching ApplicationInfos for removal. |
| 234 | */ |
| 235 | private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) { |
| 236 | ComponentName removeComponent = item.intent.getComponent(); |
| 237 | final int length = list.size(); |
| 238 | for (int i = 0; i < length; ++i) { |
| 239 | ApplicationInfo info = list.get(i); |
| 240 | if (info.intent.getComponent().equals(removeComponent)) { |
| 241 | return i; |
| 242 | } |
| 243 | } |
| 244 | return -1; |
| 245 | } |
| 246 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 247 | public void update() { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 248 | // get the list of widgets |
| 249 | mWidgetList = AppWidgetManager.getInstance(mLauncher).getInstalledProviders(); |
| 250 | Collections.sort(mWidgetList, new Comparator<AppWidgetProviderInfo>() { |
| 251 | @Override |
| 252 | public int compare(AppWidgetProviderInfo object1, AppWidgetProviderInfo object2) { |
| 253 | return object1.label.compareTo(object2.label); |
| 254 | } |
| 255 | }); |
| 256 | |
| 257 | Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() { |
| 258 | @Override |
| 259 | public int compare(ResolveInfo object1, ResolveInfo object2) { |
| 260 | return object1.loadLabel(mPackageManager).toString().compareTo( |
| 261 | object2.loadLabel(mPackageManager).toString()); |
| 262 | } |
| 263 | }; |
| 264 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 265 | // get the list of shortcuts |
| 266 | Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); |
| 267 | mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0); |
| 268 | Collections.sort(mShortcutList, resolveInfoComparator); |
| 269 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 270 | // get the list of wallpapers |
| 271 | Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER); |
| 272 | mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, 0); |
| 273 | Collections.sort(mWallpaperList, resolveInfoComparator); |
| 274 | |
Winson Chung | 10fefb1 | 2010-11-01 11:57:06 -0700 | [diff] [blame] | 275 | invalidatePageDataAndIconCache(); |
| 276 | } |
| 277 | |
| 278 | private void invalidatePageDataAndIconCache() { |
| 279 | // Reset the icon cache |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 280 | mPageViewIconCache.clear(); |
| 281 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 282 | // Refresh all the tabs |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 283 | invalidatePageData(); |
| 284 | } |
| 285 | |
| 286 | public void setDragController(DragController dragger) { |
| 287 | mDragController = dragger; |
| 288 | } |
| 289 | |
| 290 | public void setCustomizationFilter(CustomizationType filterType) { |
| 291 | mCustomizationType = filterType; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 292 | setCurrentPage(0); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 293 | updateCurrentPageScroll(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 294 | invalidatePageData(); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 295 | |
| 296 | // End the current choice mode so that we don't carry selections across tabs |
| 297 | endChoiceMode(); |
Winson Chung | 649a4ca | 2010-11-18 10:38:13 -0800 | [diff] [blame] | 298 | // Reset the touch item (if we are mid-dragging) |
| 299 | mLastTouchedItem = null; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void onDropCompleted(View target, boolean success) { |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 304 | mLauncher.getWorkspace().onDragStopped(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @Override |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 308 | public void onDragViewVisible() { |
| 309 | } |
| 310 | |
| 311 | @Override |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 312 | public void onClick(View v) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 313 | // Return early if this is not initiated from a touch |
| 314 | if (!v.isInTouchMode()) return; |
| 315 | // Return early if we are still animating the pages |
Winson Chung | 4f9e107 | 2010-11-15 15:28:24 -0800 | [diff] [blame] | 316 | if (mNextPage != INVALID_PAGE) return; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 317 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 318 | // On certain pages, we allow single tap to mark items as selected so that they can be |
| 319 | // dropped onto the mini workspaces |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 320 | boolean enterChoiceMode = false; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 321 | switch (mCustomizationType) { |
| 322 | case WidgetCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 323 | mChoiceModeTitleText = R.string.cab_widget_selection_text; |
| 324 | enterChoiceMode = true; |
| 325 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 326 | case ApplicationCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 327 | mChoiceModeTitleText = R.string.cab_app_selection_text; |
| 328 | enterChoiceMode = true; |
| 329 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 330 | case ShortcutCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 331 | mChoiceModeTitleText = R.string.cab_shortcut_selection_text; |
| 332 | enterChoiceMode = true; |
| 333 | break; |
| 334 | default: |
| 335 | break; |
| 336 | } |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 337 | |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 338 | if (enterChoiceMode) { |
Michael Jurka | 6b4b25d | 2010-10-20 18:19:45 -0700 | [diff] [blame] | 339 | final ItemInfo itemInfo = (ItemInfo) v.getTag(); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 340 | |
Michael Jurka | 6b4b25d | 2010-10-20 18:19:45 -0700 | [diff] [blame] | 341 | Workspace w = mLauncher.getWorkspace(); |
| 342 | int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen(); |
| 343 | final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen); |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 344 | |
Michael Jurka | 6b4b25d | 2010-10-20 18:19:45 -0700 | [diff] [blame] | 345 | animateClickFeedback(v, new Runnable() { |
| 346 | @Override |
| 347 | public void run() { |
| 348 | mLauncher.addExternalItemToScreen(itemInfo, cl); |
| 349 | } |
| 350 | }); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 351 | return; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // Otherwise, we just handle the single click here |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 355 | switch (mCustomizationType) { |
| 356 | case WallpaperCustomization: |
| 357 | // animate some feedback to the long press |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 358 | final View clickView = v; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 359 | animateClickFeedback(v, new Runnable() { |
| 360 | @Override |
| 361 | public void run() { |
| 362 | // add the shortcut |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 363 | ResolveInfo info = (ResolveInfo) clickView.getTag(); |
Winson Chung | 24ab2f1 | 2010-09-16 14:10:47 -0700 | [diff] [blame] | 364 | Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER); |
| 365 | ComponentName name = new ComponentName(info.activityInfo.packageName, |
| 366 | info.activityInfo.name); |
| 367 | createWallpapersIntent.setComponent(name); |
| 368 | mLauncher.processWallpaper(createWallpapersIntent); |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 369 | } |
| 370 | }); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 371 | break; |
| 372 | default: |
| 373 | break; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | @Override |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 378 | public boolean onLongClick(View v) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 379 | // Return early if this is not initiated from a touch |
| 380 | if (!v.isInTouchMode()) return false; |
| 381 | // Return early if we are still animating the pages |
Winson Chung | 4f9e107 | 2010-11-15 15:28:24 -0800 | [diff] [blame] | 382 | if (mNextPage != INVALID_PAGE) return false; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 383 | return beginDragging(v); |
| 384 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 385 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 386 | @Override |
| 387 | public boolean onTouch(View v, MotionEvent event) { |
| 388 | mLastTouchedItem = v; |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Determines if we should change the touch state to start scrolling after the |
| 394 | * user moves their touch point too far. |
| 395 | */ |
| 396 | protected void determineScrollingStart(MotionEvent ev) { |
| 397 | if (!mIsDragging) super.determineScrollingStart(ev); |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Determines if we should change the touch state to start dragging after the |
| 402 | * user moves their touch point far enough. |
| 403 | */ |
| 404 | protected void determineDraggingStart(MotionEvent ev) { |
| 405 | /* |
| 406 | * Locally do absolute value. mLastMotionX is set to the y value |
| 407 | * of the down event. |
| 408 | */ |
| 409 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 410 | final float x = ev.getX(pointerIndex); |
| 411 | final float y = ev.getY(pointerIndex); |
| 412 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 413 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 414 | |
| 415 | final int touchSlop = mTouchSlop; |
| 416 | boolean yMoved = yDiff > touchSlop; |
| 417 | boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold; |
| 418 | |
Winson Chung | 649a4ca | 2010-11-18 10:38:13 -0800 | [diff] [blame] | 419 | if (isUpwardMotion && yMoved && mLastTouchedItem != null) { |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 420 | // Drag if the user moved far enough along the Y axis |
| 421 | beginDragging(mLastTouchedItem); |
| 422 | |
| 423 | // Cancel any pending longpress |
| 424 | if (mAllowLongPress) { |
| 425 | mAllowLongPress = false; |
| 426 | // Try canceling the long press. It could also have been scheduled |
| 427 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 428 | // everything |
| 429 | final View currentPage = getPageAt(mCurrentPage); |
| 430 | if (currentPage != null) { |
| 431 | currentPage.cancelLongPress(); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 439 | final int action = ev.getAction(); |
| 440 | switch (action & MotionEvent.ACTION_MASK) { |
| 441 | case MotionEvent.ACTION_DOWN: |
| 442 | mIsDragging = false; |
| 443 | break; |
| 444 | case MotionEvent.ACTION_MOVE: |
| 445 | if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) { |
| 446 | determineDraggingStart(ev); |
| 447 | } |
| 448 | break; |
| 449 | } |
| 450 | return super.onInterceptTouchEvent(ev); |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public boolean onTouchEvent(MotionEvent ev) { |
| 455 | final int action = ev.getAction(); |
| 456 | switch (action & MotionEvent.ACTION_MASK) { |
| 457 | case MotionEvent.ACTION_DOWN: |
| 458 | mIsDragging = false; |
| 459 | break; |
| 460 | case MotionEvent.ACTION_MOVE: |
| 461 | if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) { |
| 462 | determineDraggingStart(ev); |
| 463 | } |
| 464 | break; |
| 465 | } |
| 466 | return super.onTouchEvent(ev); |
| 467 | } |
| 468 | |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 469 | Bitmap drawableToBitmap(Drawable d) { |
| 470 | Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), |
| 471 | Bitmap.Config.ARGB_8888); |
| 472 | Canvas c = new Canvas(b); |
| 473 | d.draw(c); |
| 474 | return b; |
| 475 | } |
| 476 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 477 | private boolean beginDragging(View v) { |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 478 | // End the current choice mode before we start dragging anything |
| 479 | if (isChoiceMode(CHOICE_MODE_SINGLE)) { |
| 480 | endChoiceMode(); |
| 481 | } |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 482 | mIsDragging = true; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 483 | |
| 484 | PendingAddItemInfo createItemInfo; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 485 | switch (mCustomizationType) { |
| 486 | case WidgetCustomization: |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 487 | // Get the icon as the drag representation |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 488 | final LinearLayout l = (LinearLayout) v; |
| 489 | final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable(); |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 490 | Bitmap b = drawableToBitmap(icon); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 491 | PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 492 | |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 493 | mLauncher.getWorkspace().onDragStartedWithItemMinSize( |
| 494 | createWidgetInfo.minWidth, createWidgetInfo.minHeight); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 495 | mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, null); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 496 | |
| 497 | // Cleanup the icon |
| 498 | b.recycle(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 499 | return true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 500 | case ShortcutCustomization: |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 501 | createItemInfo = (PendingAddItemInfo) v.getTag(); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 502 | mDragController.startDrag(v, this, createItemInfo, DragController.DRAG_ACTION_COPY); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 503 | mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 504 | return true; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 505 | case ApplicationCustomization: |
| 506 | // Pick up the application for dropping |
| 507 | ApplicationInfo app = (ApplicationInfo) v.getTag(); |
| 508 | app = new ApplicationInfo(app); |
| 509 | |
| 510 | mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 511 | mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 512 | return true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 513 | } |
| 514 | return false; |
| 515 | } |
| 516 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 517 | /** |
| 518 | * Pre-processes the layout of the different widget pages. |
| 519 | * @return the number of pages of widgets that we have |
| 520 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 521 | private int relayoutWidgets() { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 522 | if (mWidgetList.isEmpty()) return 0; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 523 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 524 | // create a new page for the first set of widgets |
| 525 | ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 526 | mWidgetPages.clear(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 527 | mWidgetPages.add(newPage); |
| 528 | |
| 529 | // do this until we have no more widgets to lay out |
| 530 | final int maxNumCellsPerRow = mMaxWidgetsCellHSpan; |
| 531 | final int widgetCount = mWidgetList.size(); |
| 532 | int numCellsInRow = 0; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 533 | for (int i = 0; i < widgetCount; ++i) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 534 | final AppWidgetProviderInfo info = mWidgetList.get(i); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 535 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 536 | // determine the size of the current widget |
| 537 | int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan, |
| 538 | mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth))); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 539 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 540 | // create a new page if necessary |
| 541 | if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) { |
| 542 | numCellsInRow = 0; |
| 543 | newPage = new ArrayList<AppWidgetProviderInfo>(); |
| 544 | mWidgetPages.add(newPage); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 547 | // add the item to the current page |
| 548 | newPage.add(info); |
| 549 | numCellsInRow += cellSpanX; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 550 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 551 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 552 | return mWidgetPages.size(); |
| 553 | } |
| 554 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 555 | /** |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 556 | * Helper function to draw a drawable to the specified canvas with the specified bounds. |
| 557 | */ |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 558 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) { |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 559 | if (bitmap != null) mCanvas.setBitmap(bitmap); |
| 560 | mCanvas.save(); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 561 | d.setBounds(x, y, x+w, y+h); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 562 | d.draw(mCanvas); |
| 563 | mCanvas.restore(); |
| 564 | } |
| 565 | |
| 566 | /** |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 567 | * This method will extract the preview image specified by the wallpaper source provider (if it |
| 568 | * exists) otherwise, it will try to generate a default image preview. |
| 569 | */ |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 570 | private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 571 | // To be implemented later: resolving the up-to-date wallpaper thumbnail |
| 572 | |
| 573 | final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1); |
| 574 | final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan); |
| 575 | Resources resources = mLauncher.getResources(); |
| 576 | |
| 577 | // Create a new bitmap to hold the widget preview |
| 578 | int width = (int) (dim * sScaleFactor); |
| 579 | int height = (int) (dim * sScaleFactor); |
| 580 | final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); |
| 581 | final Drawable background = resources.getDrawable(R.drawable.default_widget_preview); |
| 582 | renderDrawableToBitmap(background, bitmap, 0, 0, width, height); |
| 583 | |
| 584 | // Draw the icon flush left |
| 585 | try { |
| 586 | final IconCache iconCache = |
| 587 | ((LauncherApplication) mLauncher.getApplication()).getIconCache(); |
| 588 | Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap( |
| 589 | iconCache.getFullResIcon(info, mPackageManager), mContext)); |
| 590 | |
| 591 | final int iconSize = minDim / 2; |
| 592 | final int offset = iconSize / 4; |
| 593 | renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize); |
| 594 | } catch (Resources.NotFoundException e) { |
| 595 | // if we can't find the icon, then just don't draw it |
| 596 | } |
| 597 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 598 | FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 599 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); |
| 600 | return drawable; |
| 601 | } |
| 602 | |
| 603 | /** |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 604 | * This method will extract the preview image specified by the widget developer (if it exists), |
| 605 | * otherwise, it will try to generate a default image preview with the widget's package icon. |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 606 | * @return the drawable that will be used and sized in the ImageView to represent the widget |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 607 | */ |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 608 | private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 609 | final PackageManager packageManager = mPackageManager; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 610 | String packageName = info.provider.getPackageName(); |
| 611 | Drawable drawable = null; |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 612 | FastBitmapDrawable newDrawable = null; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 613 | if (info.previewImage != 0) { |
| 614 | drawable = packageManager.getDrawable(packageName, info.previewImage, null); |
| 615 | if (drawable == null) { |
| 616 | Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon) |
| 617 | + " for provider: " + info.provider); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | // If we don't have a preview image, create a default one |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 622 | final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1); |
| 623 | final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 624 | if (drawable == null) { |
| 625 | Resources resources = mLauncher.getResources(); |
| 626 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 627 | // Create a new bitmap to hold the widget preview |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 628 | int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor); |
| 629 | int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 630 | final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); |
| 631 | final Drawable background = resources.getDrawable(R.drawable.default_widget_preview); |
| 632 | renderDrawableToBitmap(background, bitmap, 0, 0, width, height); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 633 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 634 | // Draw the icon flush left |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 635 | try { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 636 | Drawable icon = null; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 637 | if (info.icon > 0) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 638 | icon = packageManager.getDrawable(packageName, info.icon, null); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame] | 639 | } |
| 640 | if (icon == null) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 641 | icon = resources.getDrawable(R.drawable.ic_launcher_application); |
| 642 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 643 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 644 | final int iconSize = minDim / 2; |
| 645 | final int offset = iconSize / 4; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 646 | renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 647 | } catch (Resources.NotFoundException e) { |
| 648 | // if we can't find the icon, then just don't draw it |
| 649 | } |
| 650 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 651 | newDrawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 652 | } else { |
| 653 | // Scale down the preview if necessary |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 654 | final float imageWidth = drawable.getIntrinsicWidth(); |
| 655 | final float imageHeight = drawable.getIntrinsicHeight(); |
| 656 | final float aspect = (float) imageWidth / imageHeight; |
| 657 | final int scaledWidth = |
| 658 | (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor); |
| 659 | final int scaledHeight = |
| 660 | (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 661 | int width; |
| 662 | int height; |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 663 | if (aspect >= 1.0f) { |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 664 | width = scaledWidth; |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 665 | height = (int) (((float) scaledWidth / imageWidth) * imageHeight); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 666 | } else { |
| 667 | height = scaledHeight; |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 668 | width = (int) (((float) scaledHeight / imageHeight) * imageWidth); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); |
| 672 | renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height); |
| 673 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 674 | newDrawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 675 | } |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 676 | newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(), |
| 677 | newDrawable.getIntrinsicHeight()); |
| 678 | return newDrawable; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | private void setupPage(PagedViewCellLayout layout) { |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 682 | layout.setCellCount(mCellCountX, mCellCountY); |
| 683 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight, |
| 684 | mPageLayoutPaddingBottom); |
Winson Chung | ef0066b | 2010-10-21 11:55:00 -0700 | [diff] [blame] | 685 | layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 688 | private void setupWorkspaceLayout() { |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 689 | mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 690 | mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0); |
| 691 | |
| 692 | mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan); |
| 693 | } |
| 694 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 695 | private void syncWidgetPages() { |
| 696 | if (mWidgetList == null) return; |
| 697 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 698 | // we need to repopulate with the LinearLayout layout for the widget pages |
| 699 | removeAllViews(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 700 | int numPages = relayoutWidgets(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 701 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 702 | LinearLayout layout = new PagedViewExtendedLayout(getContext()); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 703 | layout.setGravity(Gravity.CENTER_HORIZONTAL); |
Winson Chung | ef0066b | 2010-10-21 11:55:00 -0700 | [diff] [blame] | 704 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 705 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 706 | |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 707 | addView(layout, new LinearLayout.LayoutParams( |
| 708 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 709 | LinearLayout.LayoutParams.MATCH_PARENT)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
| 713 | private void syncWidgetPageItems(int page) { |
| 714 | // ensure that we have the right number of items on the pages |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 715 | LinearLayout layout = (LinearLayout) getChildAt(page); |
| 716 | final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 717 | final int count = list.size(); |
| 718 | layout.removeAllViews(); |
| 719 | for (int i = 0; i < count; ++i) { |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 720 | final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i); |
| 721 | final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 722 | final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth, |
| 723 | info.minHeight, null); |
| 724 | final FastBitmapDrawable icon = getWidgetPreview(info); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 725 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 726 | PagedViewWidget l = (PagedViewWidget) mInflater.inflate( |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 727 | R.layout.customize_paged_view_widget, layout, false); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 728 | l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 729 | l.setTag(createItemInfo); |
| 730 | l.setOnClickListener(this); |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 731 | l.setOnTouchListener(this); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 732 | l.setOnLongClickListener(this); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 733 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 734 | layout.addView(l); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 738 | private void syncWallpaperPages() { |
| 739 | if (mWallpaperList == null) return; |
| 740 | |
| 741 | // We need to repopulate the LinearLayout for the wallpaper pages |
| 742 | removeAllViews(); |
| 743 | int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) / |
| 744 | mMaxWidgetsCellHSpan); |
| 745 | for (int i = 0; i < numPages; ++i) { |
| 746 | LinearLayout layout = new PagedViewExtendedLayout(getContext()); |
| 747 | layout.setGravity(Gravity.CENTER_HORIZONTAL); |
| 748 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 749 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 750 | |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 751 | addView(layout, new LinearLayout.LayoutParams( |
| 752 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 753 | LinearLayout.LayoutParams.MATCH_PARENT)); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | private void syncWallpaperPageItems(int page) { |
| 758 | // Load the items on to the pages |
| 759 | LinearLayout layout = (LinearLayout) getChildAt(page); |
| 760 | layout.removeAllViews(); |
| 761 | final int count = mWallpaperList.size(); |
Winson Chung | d28ed49 | 2010-11-22 14:34:57 -0800 | [diff] [blame] | 762 | final int numItemsPerPage = mMaxWidgetsCellHSpan / mWallpaperCellHSpan; |
| 763 | final int startIndex = page * numItemsPerPage; |
| 764 | final int endIndex = Math.min(count, startIndex + numItemsPerPage); |
| 765 | for (int i = startIndex; i < endIndex; ++i) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 766 | final ResolveInfo info = mWallpaperList.get(i); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 767 | final FastBitmapDrawable icon = getWallpaperPreview(info); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 768 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 769 | PagedViewWidget l = (PagedViewWidget) mInflater.inflate( |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 770 | R.layout.customize_paged_view_wallpaper, layout, false); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 771 | l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 772 | l.setTag(info); |
| 773 | l.setOnClickListener(this); |
| 774 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 775 | layout.addView(l); |
| 776 | } |
| 777 | } |
| 778 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 779 | private void syncListPages(List<ResolveInfo> list) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 780 | // we need to repopulate with PagedViewCellLayouts |
| 781 | removeAllViews(); |
| 782 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 783 | // ensure that we have the right number of pages |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 784 | int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY)); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 785 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 786 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 787 | setupPage(layout); |
| 788 | addView(layout); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | private void syncListPageItems(int page, List<ResolveInfo> list) { |
| 793 | // ensure that we have the right number of items on the pages |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 794 | int numCells = mCellCountX * mCellCountY; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 795 | int startIndex = page * numCells; |
| 796 | int endIndex = Math.min(startIndex + numCells, list.size()); |
| 797 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
| 798 | // TODO: we can optimize by just re-applying to existing views |
| 799 | layout.removeAllViews(); |
| 800 | for (int i = startIndex; i < endIndex; ++i) { |
| 801 | ResolveInfo info = list.get(i); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 802 | PendingAddItemInfo createItemInfo = new PendingAddItemInfo(); |
| 803 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 804 | PagedViewIcon icon = (PagedViewIcon) mInflater.inflate( |
| 805 | R.layout.customize_paged_view_item, layout, false); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 806 | icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache, |
| 807 | ((LauncherApplication)mLauncher.getApplication()).getIconCache()); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 808 | switch (mCustomizationType) { |
| 809 | case WallpaperCustomization: |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 810 | icon.setOnClickListener(this); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 811 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 812 | case ShortcutCustomization: |
| 813 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
| 814 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 815 | info.activityInfo.name); |
| 816 | icon.setTag(createItemInfo); |
| 817 | icon.setOnClickListener(this); |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 818 | icon.setOnTouchListener(this); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 819 | icon.setOnLongClickListener(this); |
| 820 | break; |
| 821 | default: |
| 822 | break; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 823 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 824 | |
| 825 | final int index = i - startIndex; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 826 | final int x = index % mCellCountX; |
| 827 | final int y = index / mCellCountX; |
| 828 | setupPage(layout); |
| 829 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | private void syncAppPages() { |
| 834 | if (mApps == null) return; |
| 835 | |
| 836 | // We need to repopulate with PagedViewCellLayouts |
| 837 | removeAllViews(); |
| 838 | |
| 839 | // Ensure that we have the right number of pages |
| 840 | int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); |
| 841 | for (int i = 0; i < numPages; ++i) { |
| 842 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 843 | setupPage(layout); |
| 844 | addView(layout); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | private void syncAppPageItems(int page) { |
| 849 | if (mApps == null) return; |
| 850 | |
| 851 | // ensure that we have the right number of items on the pages |
| 852 | int numCells = mCellCountX * mCellCountY; |
| 853 | int startIndex = page * numCells; |
| 854 | int endIndex = Math.min(startIndex + numCells, mApps.size()); |
| 855 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
| 856 | // TODO: we can optimize by just re-applying to existing views |
| 857 | layout.removeAllViews(); |
| 858 | for (int i = startIndex; i < endIndex; ++i) { |
| 859 | final ApplicationInfo info = mApps.get(i); |
| 860 | PagedViewIcon icon = (PagedViewIcon) mInflater.inflate( |
| 861 | R.layout.all_apps_paged_view_application, layout, false); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 862 | icon.applyFromApplicationInfo(info, mPageViewIconCache, true); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 863 | icon.setOnClickListener(this); |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 864 | icon.setOnTouchListener(this); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 865 | icon.setOnLongClickListener(this); |
| 866 | |
| 867 | final int index = i - startIndex; |
| 868 | final int x = index % mCellCountX; |
| 869 | final int y = index / mCellCountX; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 870 | setupPage(layout); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 871 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 875 | @Override |
| 876 | public void syncPages() { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 877 | boolean centerPagedViewCellLayouts = false; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 878 | switch (mCustomizationType) { |
| 879 | case WidgetCustomization: |
| 880 | syncWidgetPages(); |
| 881 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 882 | case ShortcutCustomization: |
| 883 | syncListPages(mShortcutList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 884 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 885 | break; |
| 886 | case WallpaperCustomization: |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 887 | syncWallpaperPages(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 888 | break; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 889 | case ApplicationCustomization: |
| 890 | syncAppPages(); |
| 891 | centerPagedViewCellLayouts = false; |
| 892 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 893 | default: |
| 894 | removeAllViews(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 895 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 896 | break; |
| 897 | } |
| 898 | |
| 899 | // only try and center the page if there is one page |
| 900 | final int childCount = getChildCount(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 901 | if (centerPagedViewCellLayouts) { |
| 902 | if (childCount == 1) { |
| 903 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0); |
| 904 | layout.enableCenteredContent(true); |
| 905 | } else { |
| 906 | for (int i = 0; i < childCount; ++i) { |
| 907 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
| 908 | layout.enableCenteredContent(false); |
| 909 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
| 913 | // bound the current page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 914 | setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage()))); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | @Override |
| 918 | public void syncPageItems(int page) { |
| 919 | switch (mCustomizationType) { |
| 920 | case WidgetCustomization: |
| 921 | syncWidgetPageItems(page); |
| 922 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 923 | case ShortcutCustomization: |
| 924 | syncListPageItems(page, mShortcutList); |
| 925 | break; |
| 926 | case WallpaperCustomization: |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 927 | syncWallpaperPageItems(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 928 | break; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 929 | case ApplicationCustomization: |
| 930 | syncAppPageItems(page); |
| 931 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 932 | } |
| 933 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 934 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 935 | @Override |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 936 | protected int getAssociatedLowerPageBound(int page) { |
| 937 | return 0; |
| 938 | } |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 939 | @Override |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 940 | protected int getAssociatedUpperPageBound(int page) { |
| 941 | return getChildCount(); |
| 942 | } |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 943 | |
| 944 | @Override |
| 945 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 946 | mode.setTitle(mChoiceModeTitleText); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 947 | return true; |
| 948 | } |
| 949 | |
| 950 | @Override |
| 951 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 952 | return true; |
| 953 | } |
| 954 | |
| 955 | @Override |
| 956 | public void onDestroyActionMode(ActionMode mode) { |
| 957 | endChoiceMode(); |
| 958 | } |
| 959 | |
| 960 | @Override |
| 961 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 962 | return false; |
| 963 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 964 | } |