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