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