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 |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 315 | public void onClick(View v) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 316 | // Return early if this is not initiated from a touch |
| 317 | if (!v.isInTouchMode()) return; |
| 318 | // Return early if we are still animating the pages |
Winson Chung | 4f9e107 | 2010-11-15 15:28:24 -0800 | [diff] [blame] | 319 | if (mNextPage != INVALID_PAGE) return; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 320 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 321 | // On certain pages, we allow single tap to mark items as selected so that they can be |
| 322 | // dropped onto the mini workspaces |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 323 | boolean enterChoiceMode = false; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 324 | switch (mCustomizationType) { |
| 325 | case WidgetCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 326 | mChoiceModeTitleText = R.string.cab_widget_selection_text; |
| 327 | enterChoiceMode = true; |
| 328 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 329 | case ApplicationCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 330 | mChoiceModeTitleText = R.string.cab_app_selection_text; |
| 331 | enterChoiceMode = true; |
| 332 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 333 | case ShortcutCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 334 | mChoiceModeTitleText = R.string.cab_shortcut_selection_text; |
| 335 | enterChoiceMode = true; |
| 336 | break; |
| 337 | default: |
| 338 | break; |
| 339 | } |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 340 | |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 341 | if (enterChoiceMode) { |
Michael Jurka | 6b4b25d | 2010-10-20 18:19:45 -0700 | [diff] [blame] | 342 | final ItemInfo itemInfo = (ItemInfo) v.getTag(); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 343 | |
Michael Jurka | 6b4b25d | 2010-10-20 18:19:45 -0700 | [diff] [blame] | 344 | Workspace w = mLauncher.getWorkspace(); |
| 345 | int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen(); |
| 346 | final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen); |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 347 | |
Michael Jurka | 6b4b25d | 2010-10-20 18:19:45 -0700 | [diff] [blame] | 348 | animateClickFeedback(v, new Runnable() { |
| 349 | @Override |
| 350 | public void run() { |
| 351 | mLauncher.addExternalItemToScreen(itemInfo, cl); |
| 352 | } |
| 353 | }); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 354 | return; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // Otherwise, we just handle the single click here |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 358 | switch (mCustomizationType) { |
| 359 | case WallpaperCustomization: |
| 360 | // animate some feedback to the long press |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 361 | final View clickView = v; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 362 | animateClickFeedback(v, new Runnable() { |
| 363 | @Override |
| 364 | public void run() { |
| 365 | // add the shortcut |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 366 | ResolveInfo info = (ResolveInfo) clickView.getTag(); |
Winson Chung | 24ab2f1 | 2010-09-16 14:10:47 -0700 | [diff] [blame] | 367 | Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER); |
| 368 | ComponentName name = new ComponentName(info.activityInfo.packageName, |
| 369 | info.activityInfo.name); |
| 370 | createWallpapersIntent.setComponent(name); |
| 371 | mLauncher.processWallpaper(createWallpapersIntent); |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 372 | } |
| 373 | }); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 374 | break; |
| 375 | default: |
| 376 | break; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | @Override |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 381 | public boolean onLongClick(View v) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 382 | // Return early if this is not initiated from a touch |
| 383 | if (!v.isInTouchMode()) return false; |
| 384 | // Return early if we are still animating the pages |
Winson Chung | 4f9e107 | 2010-11-15 15:28:24 -0800 | [diff] [blame] | 385 | if (mNextPage != INVALID_PAGE) return false; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 386 | return beginDragging(v); |
| 387 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 388 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 389 | @Override |
| 390 | public boolean onTouch(View v, MotionEvent event) { |
| 391 | mLastTouchedItem = v; |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Determines if we should change the touch state to start scrolling after the |
| 397 | * user moves their touch point too far. |
| 398 | */ |
| 399 | protected void determineScrollingStart(MotionEvent ev) { |
| 400 | if (!mIsDragging) super.determineScrollingStart(ev); |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Determines if we should change the touch state to start dragging after the |
| 405 | * user moves their touch point far enough. |
| 406 | */ |
| 407 | protected void determineDraggingStart(MotionEvent ev) { |
| 408 | /* |
| 409 | * Locally do absolute value. mLastMotionX is set to the y value |
| 410 | * of the down event. |
| 411 | */ |
| 412 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 413 | final float x = ev.getX(pointerIndex); |
| 414 | final float y = ev.getY(pointerIndex); |
| 415 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 416 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 417 | |
| 418 | final int touchSlop = mTouchSlop; |
| 419 | boolean yMoved = yDiff > touchSlop; |
| 420 | boolean isUpwardMotion = (yDiff / (float) xDiff) > mDragSlopeThreshold; |
| 421 | |
Winson Chung | 649a4ca | 2010-11-18 10:38:13 -0800 | [diff] [blame] | 422 | if (isUpwardMotion && yMoved && mLastTouchedItem != null) { |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 423 | // Drag if the user moved far enough along the Y axis |
| 424 | beginDragging(mLastTouchedItem); |
| 425 | |
| 426 | // Cancel any pending longpress |
| 427 | if (mAllowLongPress) { |
| 428 | mAllowLongPress = false; |
| 429 | // Try canceling the long press. It could also have been scheduled |
| 430 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 431 | // everything |
| 432 | final View currentPage = getPageAt(mCurrentPage); |
| 433 | if (currentPage != null) { |
| 434 | currentPage.cancelLongPress(); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 442 | final int action = ev.getAction(); |
| 443 | switch (action & MotionEvent.ACTION_MASK) { |
| 444 | case MotionEvent.ACTION_DOWN: |
| 445 | mIsDragging = false; |
| 446 | break; |
| 447 | case MotionEvent.ACTION_MOVE: |
| 448 | if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) { |
| 449 | determineDraggingStart(ev); |
| 450 | } |
| 451 | break; |
| 452 | } |
| 453 | return super.onInterceptTouchEvent(ev); |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public boolean onTouchEvent(MotionEvent ev) { |
| 458 | final int action = ev.getAction(); |
| 459 | switch (action & MotionEvent.ACTION_MASK) { |
| 460 | case MotionEvent.ACTION_DOWN: |
| 461 | mIsDragging = false; |
| 462 | break; |
| 463 | case MotionEvent.ACTION_MOVE: |
| 464 | if (mTouchState != TOUCH_STATE_SCROLLING && !mIsDragging) { |
| 465 | determineDraggingStart(ev); |
| 466 | } |
| 467 | break; |
| 468 | } |
| 469 | return super.onTouchEvent(ev); |
| 470 | } |
| 471 | |
| 472 | private boolean beginDragging(View v) { |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 473 | // End the current choice mode before we start dragging anything |
| 474 | if (isChoiceMode(CHOICE_MODE_SINGLE)) { |
| 475 | endChoiceMode(); |
| 476 | } |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 477 | mIsDragging = true; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 478 | |
| 479 | PendingAddItemInfo createItemInfo; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 480 | switch (mCustomizationType) { |
| 481 | case WidgetCustomization: |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 482 | // Get the icon as the drag representation |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 483 | final LinearLayout l = (LinearLayout) v; |
| 484 | final Drawable icon = ((ImageView) l.findViewById(R.id.widget_preview)).getDrawable(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 485 | Bitmap b = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), |
| 486 | Bitmap.Config.ARGB_8888); |
| 487 | Canvas c = new Canvas(b); |
| 488 | icon.draw(c); |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 489 | PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 490 | |
Michael Jurka | 3e7c763 | 2010-10-02 16:01:03 -0700 | [diff] [blame] | 491 | mLauncher.getWorkspace().onDragStartedWithItemMinSize( |
| 492 | createWidgetInfo.minWidth, createWidgetInfo.minHeight); |
| 493 | mDragController.startDrag(v, b, this, createWidgetInfo, DragController.DRAG_ACTION_COPY, |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 494 | null); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 495 | |
| 496 | // Cleanup the icon |
| 497 | b.recycle(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 498 | return true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 499 | case ShortcutCustomization: |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 500 | createItemInfo = (PendingAddItemInfo) v.getTag(); |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 501 | mDragController.startDrag( |
| 502 | v, this, createItemInfo, DragController.DRAG_ACTION_COPY, null); |
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 | */ |
| 570 | private Drawable getWallpaperPreview(ResolveInfo info) { |
| 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 | |
| 598 | Drawable drawable = new FastBitmapDrawable(bitmap); |
| 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 | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 608 | private Drawable 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; |
| 612 | if (info.previewImage != 0) { |
| 613 | drawable = packageManager.getDrawable(packageName, info.previewImage, null); |
| 614 | if (drawable == null) { |
| 615 | Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon) |
| 616 | + " for provider: " + info.provider); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
| 620 | // If we don't have a preview image, create a default one |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 621 | final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1); |
| 622 | final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 623 | if (drawable == null) { |
| 624 | Resources resources = mLauncher.getResources(); |
| 625 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 626 | // Create a new bitmap to hold the widget preview |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 627 | int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor); |
| 628 | int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 629 | final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); |
| 630 | final Drawable background = resources.getDrawable(R.drawable.default_widget_preview); |
| 631 | renderDrawableToBitmap(background, bitmap, 0, 0, width, height); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 632 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 633 | // Draw the icon flush left |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 634 | try { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 635 | Drawable icon = null; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 636 | if (info.icon > 0) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 637 | icon = packageManager.getDrawable(packageName, info.icon, null); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame] | 638 | } |
| 639 | if (icon == null) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 640 | icon = resources.getDrawable(R.drawable.ic_launcher_application); |
| 641 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 642 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 643 | final int iconSize = minDim / 2; |
| 644 | final int offset = iconSize / 4; |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 645 | renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 646 | } catch (Resources.NotFoundException e) { |
| 647 | // if we can't find the icon, then just don't draw it |
| 648 | } |
| 649 | |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 650 | drawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 651 | } else { |
| 652 | // Scale down the preview if necessary |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 653 | final float imageWidth = drawable.getIntrinsicWidth(); |
| 654 | final float imageHeight = drawable.getIntrinsicHeight(); |
| 655 | final float aspect = (float) imageWidth / imageHeight; |
| 656 | final int scaledWidth = |
| 657 | (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor); |
| 658 | final int scaledHeight = |
| 659 | (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 660 | int width; |
| 661 | int height; |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 662 | if (aspect >= 1.0f) { |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 663 | width = scaledWidth; |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 664 | height = (int) (((float) scaledWidth / imageWidth) * imageHeight); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 665 | } else { |
| 666 | height = scaledHeight; |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 667 | width = (int) (((float) scaledHeight / imageHeight) * imageWidth); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); |
| 671 | renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height); |
| 672 | |
| 673 | drawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 674 | } |
| 675 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); |
| 676 | return drawable; |
| 677 | } |
| 678 | |
| 679 | private void setupPage(PagedViewCellLayout layout) { |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 680 | layout.setCellCount(mCellCountX, mCellCountY); |
| 681 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight, |
| 682 | mPageLayoutPaddingBottom); |
Winson Chung | ef0066b | 2010-10-21 11:55:00 -0700 | [diff] [blame] | 683 | layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 686 | private void setupWorkspaceLayout() { |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 687 | mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 688 | mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0); |
| 689 | |
| 690 | mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 691 | mMaxWallpaperWidth = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 694 | private void syncWidgetPages() { |
| 695 | if (mWidgetList == null) return; |
| 696 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 697 | // we need to repopulate with the LinearLayout layout for the widget pages |
| 698 | removeAllViews(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 699 | int numPages = relayoutWidgets(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 700 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 701 | LinearLayout layout = new PagedViewExtendedLayout(getContext()); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 702 | layout.setGravity(Gravity.CENTER_HORIZONTAL); |
Winson Chung | ef0066b | 2010-10-21 11:55:00 -0700 | [diff] [blame] | 703 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 704 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 705 | |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 706 | addView(layout, new LinearLayout.LayoutParams( |
| 707 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 708 | LinearLayout.LayoutParams.MATCH_PARENT)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | private void syncWidgetPageItems(int page) { |
| 713 | // ensure that we have the right number of items on the pages |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 714 | LinearLayout layout = (LinearLayout) getChildAt(page); |
| 715 | final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 716 | final int count = list.size(); |
| 717 | layout.removeAllViews(); |
| 718 | for (int i = 0; i < count; ++i) { |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 719 | final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i); |
| 720 | final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 721 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 722 | LinearLayout l = (LinearLayout) mInflater.inflate( |
| 723 | R.layout.customize_paged_view_widget, layout, false); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 724 | l.setTag(createItemInfo); |
| 725 | l.setOnClickListener(this); |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 726 | l.setOnTouchListener(this); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 727 | l.setOnLongClickListener(this); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 728 | |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 729 | final Drawable icon = getWidgetPreview(info); |
Michael Jurka | 9987a5c | 2010-10-08 16:58:12 -0700 | [diff] [blame] | 730 | |
| 731 | int[] spans = CellLayout.rectToCell(getResources(), info.minWidth, info.minHeight, null); |
| 732 | final int hSpan = spans[0]; |
| 733 | final int vSpan = spans[1]; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 734 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 735 | ImageView image = (ImageView) l.findViewById(R.id.widget_preview); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 736 | image.setMaxWidth(mMaxWidgetWidth); |
| 737 | image.setImageDrawable(icon); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 738 | TextView name = (TextView) l.findViewById(R.id.widget_name); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 739 | name.setText(info.label); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 740 | TextView dims = (TextView) l.findViewById(R.id.widget_dims); |
Winson Chung | 3a47678 | 2010-09-15 15:21:55 -0700 | [diff] [blame] | 741 | dims.setText(mContext.getString(R.string.widget_dims_format, hSpan, vSpan)); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 742 | |
| 743 | layout.addView(l); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 747 | private void syncWallpaperPages() { |
| 748 | if (mWallpaperList == null) return; |
| 749 | |
| 750 | // We need to repopulate the LinearLayout for the wallpaper pages |
| 751 | removeAllViews(); |
| 752 | int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) / |
| 753 | mMaxWidgetsCellHSpan); |
| 754 | for (int i = 0; i < numPages; ++i) { |
| 755 | LinearLayout layout = new PagedViewExtendedLayout(getContext()); |
| 756 | layout.setGravity(Gravity.CENTER_HORIZONTAL); |
| 757 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 758 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 759 | |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 760 | addView(layout, new LinearLayout.LayoutParams( |
| 761 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 762 | LinearLayout.LayoutParams.MATCH_PARENT)); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
| 766 | private void syncWallpaperPageItems(int page) { |
| 767 | // Load the items on to the pages |
| 768 | LinearLayout layout = (LinearLayout) getChildAt(page); |
| 769 | layout.removeAllViews(); |
| 770 | final int count = mWallpaperList.size(); |
Winson Chung | d28ed49 | 2010-11-22 14:34:57 -0800 | [diff] [blame^] | 771 | final int numItemsPerPage = mMaxWidgetsCellHSpan / mWallpaperCellHSpan; |
| 772 | final int startIndex = page * numItemsPerPage; |
| 773 | final int endIndex = Math.min(count, startIndex + numItemsPerPage); |
| 774 | for (int i = startIndex; i < endIndex; ++i) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 775 | final ResolveInfo info = mWallpaperList.get(i); |
| 776 | |
| 777 | LinearLayout l = (LinearLayout) mInflater.inflate( |
| 778 | R.layout.customize_paged_view_wallpaper, layout, false); |
| 779 | l.setTag(info); |
| 780 | l.setOnClickListener(this); |
| 781 | |
| 782 | final Drawable icon = getWallpaperPreview(info); |
| 783 | |
| 784 | ImageView image = (ImageView) l.findViewById(R.id.wallpaper_preview); |
| 785 | image.setMaxWidth(mMaxWidgetWidth); |
| 786 | image.setImageDrawable(icon); |
| 787 | TextView name = (TextView) l.findViewById(R.id.wallpaper_name); |
| 788 | name.setText(info.loadLabel(mPackageManager)); |
| 789 | |
| 790 | layout.addView(l); |
| 791 | } |
| 792 | } |
| 793 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 794 | private void syncListPages(List<ResolveInfo> list) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 795 | // we need to repopulate with PagedViewCellLayouts |
| 796 | removeAllViews(); |
| 797 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 798 | // ensure that we have the right number of pages |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 799 | int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY)); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 800 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 801 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 802 | setupPage(layout); |
| 803 | addView(layout); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | private void syncListPageItems(int page, List<ResolveInfo> list) { |
| 808 | // ensure that we have the right number of items on the pages |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 809 | int numCells = mCellCountX * mCellCountY; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 810 | int startIndex = page * numCells; |
| 811 | int endIndex = Math.min(startIndex + numCells, list.size()); |
| 812 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
| 813 | // TODO: we can optimize by just re-applying to existing views |
| 814 | layout.removeAllViews(); |
| 815 | for (int i = startIndex; i < endIndex; ++i) { |
| 816 | ResolveInfo info = list.get(i); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 817 | PendingAddItemInfo createItemInfo = new PendingAddItemInfo(); |
| 818 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 819 | PagedViewIcon icon = (PagedViewIcon) mInflater.inflate( |
| 820 | R.layout.customize_paged_view_item, layout, false); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 821 | icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache, |
| 822 | ((LauncherApplication)mLauncher.getApplication()).getIconCache()); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 823 | switch (mCustomizationType) { |
| 824 | case WallpaperCustomization: |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 825 | icon.setOnClickListener(this); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 826 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 827 | case ShortcutCustomization: |
| 828 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
| 829 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 830 | info.activityInfo.name); |
| 831 | icon.setTag(createItemInfo); |
| 832 | icon.setOnClickListener(this); |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 833 | icon.setOnTouchListener(this); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 834 | icon.setOnLongClickListener(this); |
| 835 | break; |
| 836 | default: |
| 837 | break; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 838 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 839 | |
| 840 | final int index = i - startIndex; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 841 | final int x = index % mCellCountX; |
| 842 | final int y = index / mCellCountX; |
| 843 | setupPage(layout); |
| 844 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | private void syncAppPages() { |
| 849 | if (mApps == null) return; |
| 850 | |
| 851 | // We need to repopulate with PagedViewCellLayouts |
| 852 | removeAllViews(); |
| 853 | |
| 854 | // Ensure that we have the right number of pages |
| 855 | int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); |
| 856 | for (int i = 0; i < numPages; ++i) { |
| 857 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 858 | setupPage(layout); |
| 859 | addView(layout); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | private void syncAppPageItems(int page) { |
| 864 | if (mApps == null) return; |
| 865 | |
| 866 | // ensure that we have the right number of items on the pages |
| 867 | int numCells = mCellCountX * mCellCountY; |
| 868 | int startIndex = page * numCells; |
| 869 | int endIndex = Math.min(startIndex + numCells, mApps.size()); |
| 870 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
| 871 | // TODO: we can optimize by just re-applying to existing views |
| 872 | layout.removeAllViews(); |
| 873 | for (int i = startIndex; i < endIndex; ++i) { |
| 874 | final ApplicationInfo info = mApps.get(i); |
| 875 | PagedViewIcon icon = (PagedViewIcon) mInflater.inflate( |
| 876 | R.layout.all_apps_paged_view_application, layout, false); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 877 | icon.applyFromApplicationInfo(info, mPageViewIconCache, true); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 878 | icon.setOnClickListener(this); |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 879 | icon.setOnTouchListener(this); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 880 | icon.setOnLongClickListener(this); |
| 881 | |
| 882 | final int index = i - startIndex; |
| 883 | final int x = index % mCellCountX; |
| 884 | final int y = index / mCellCountX; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 885 | setupPage(layout); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 886 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 890 | @Override |
| 891 | public void syncPages() { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 892 | boolean centerPagedViewCellLayouts = false; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 893 | switch (mCustomizationType) { |
| 894 | case WidgetCustomization: |
| 895 | syncWidgetPages(); |
| 896 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 897 | case ShortcutCustomization: |
| 898 | syncListPages(mShortcutList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 899 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 900 | break; |
| 901 | case WallpaperCustomization: |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 902 | syncWallpaperPages(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 903 | break; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 904 | case ApplicationCustomization: |
| 905 | syncAppPages(); |
| 906 | centerPagedViewCellLayouts = false; |
| 907 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 908 | default: |
| 909 | removeAllViews(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 910 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 911 | break; |
| 912 | } |
| 913 | |
| 914 | // only try and center the page if there is one page |
| 915 | final int childCount = getChildCount(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 916 | if (centerPagedViewCellLayouts) { |
| 917 | if (childCount == 1) { |
| 918 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0); |
| 919 | layout.enableCenteredContent(true); |
| 920 | } else { |
| 921 | for (int i = 0; i < childCount; ++i) { |
| 922 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
| 923 | layout.enableCenteredContent(false); |
| 924 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
| 928 | // bound the current page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 929 | setCurrentPage(Math.max(0, Math.min(childCount - 1, getCurrentPage()))); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | @Override |
| 933 | public void syncPageItems(int page) { |
| 934 | switch (mCustomizationType) { |
| 935 | case WidgetCustomization: |
| 936 | syncWidgetPageItems(page); |
| 937 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 938 | case ShortcutCustomization: |
| 939 | syncListPageItems(page, mShortcutList); |
| 940 | break; |
| 941 | case WallpaperCustomization: |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 942 | syncWallpaperPageItems(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 943 | break; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 944 | case ApplicationCustomization: |
| 945 | syncAppPageItems(page); |
| 946 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 947 | } |
| 948 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 949 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 950 | @Override |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 951 | protected int getAssociatedLowerPageBound(int page) { |
| 952 | return 0; |
| 953 | } |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 954 | @Override |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 955 | protected int getAssociatedUpperPageBound(int page) { |
| 956 | return getChildCount(); |
| 957 | } |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 958 | |
| 959 | @Override |
| 960 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 961 | mode.setTitle(mChoiceModeTitleText); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 962 | return true; |
| 963 | } |
| 964 | |
| 965 | @Override |
| 966 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 967 | return true; |
| 968 | } |
| 969 | |
| 970 | @Override |
| 971 | public void onDestroyActionMode(ActionMode mode) { |
| 972 | endChoiceMode(); |
| 973 | } |
| 974 | |
| 975 | @Override |
| 976 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 977 | return false; |
| 978 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 979 | } |