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