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