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