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