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