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 | ea2daff | 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 | 1a00933 | 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 | ea2daff | 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 | 1a00933 | 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 | 12ac0d6 | 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 | 1a00933 | 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 | ea2daff | 2011-05-17 18:21:03 -0700 | [diff] [blame] | 194 | @Override |
Patrick Dubroy | 1a00933 | 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 | ea2daff | 2011-05-17 18:21:03 -0700 | [diff] [blame] | 199 | |
Patrick Dubroy | 1a00933 | 2011-05-23 16:15:09 -0700 | [diff] [blame^] | 200 | if (cellCountX != mCellCountX || cellCountY != mCellCountY) { |
| 201 | mCellCountX = cellCountX; |
| 202 | mCellCountY = cellCountY; |
Michael Jurka | ea2daff | 2011-05-17 18:21:03 -0700 | [diff] [blame] | 203 | |
Patrick Dubroy | 1a00933 | 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 | ea2daff | 2011-05-17 18:21:03 -0700 | [diff] [blame] | 206 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 207 | setupPage(layout); |
| 208 | mPageContentWidth = layout.getContentWidth(); |
Patrick Dubroy | 1a00933 | 2011-05-23 16:15:09 -0700 | [diff] [blame^] | 209 | mPageContentHeight = layout.getContentHeight(); |
Michael Jurka | ae78551 | 2011-05-18 15:31:55 -0700 | [diff] [blame] | 210 | mMinPageWidth = layout.getWidthBeforeFirstLayout(); |
Michael Jurka | ea2daff | 2011-05-17 18:21:03 -0700 | [diff] [blame] | 211 | } |
Patrick Dubroy | 1a00933 | 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 | ea2daff | 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 | 1a00933 | 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 | |
| 350 | Comparator<ResolveInfo> resolveInfoComparator = new Comparator<ResolveInfo>() { |
| 351 | @Override |
| 352 | public int compare(ResolveInfo object1, ResolveInfo object2) { |
| 353 | return object1.loadLabel(mPackageManager).toString().compareTo( |
| 354 | object2.loadLabel(mPackageManager).toString()); |
| 355 | } |
| 356 | }; |
| 357 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 358 | // get the list of shortcuts |
| 359 | Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); |
| 360 | mShortcutList = mPackageManager.queryIntentActivities(shortcutsIntent, 0); |
| 361 | Collections.sort(mShortcutList, resolveInfoComparator); |
| 362 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 363 | // get the list of wallpapers |
| 364 | Intent wallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER); |
Adam Cohen | 7b9d3a6 | 2010-12-07 21:49:34 -0800 | [diff] [blame] | 365 | mWallpaperList = mPackageManager.queryIntentActivities(wallpapersIntent, |
| 366 | PackageManager.GET_META_DATA); |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 367 | Collections.sort(mWallpaperList, resolveInfoComparator); |
| 368 | |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 369 | ArrayList<ResolveInfo> retainShortcutList = new ArrayList<ResolveInfo>(mShortcutList); |
| 370 | retainShortcutList.addAll(mWallpaperList); |
| 371 | mPageViewIconCache.retainAllShortcuts(retainShortcutList); |
| 372 | mPageViewIconCache.retainAllAppWidgets(mWidgetList); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 373 | invalidatePageData(); |
| 374 | } |
| 375 | |
| 376 | public void setDragController(DragController dragger) { |
| 377 | mDragController = dragger; |
| 378 | } |
| 379 | |
| 380 | public void setCustomizationFilter(CustomizationType filterType) { |
Winson Chung | 7d1fcbc | 2011-01-04 10:22:20 -0800 | [diff] [blame] | 381 | cancelDragging(); |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 382 | mCustomizationType = filterType; |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 383 | if (getChildCount() > 0) { |
| 384 | setCurrentPage(0); |
| 385 | updateCurrentPageScroll(); |
| 386 | invalidatePageData(); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 387 | |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 388 | // End the current choice mode so that we don't carry selections across tabs |
| 389 | endChoiceMode(); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | public CustomizationType getCustomizationFilter() { |
| 394 | return mCustomizationType; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 397 | /** |
| 398 | * 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] | 399 | */ |
| 400 | private void resetCheckedItem(boolean animated) { |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 401 | final Checkable checkable = getSingleCheckedGrandchild(); |
| 402 | if (checkable != null) { |
| 403 | if (checkable instanceof PagedViewWidget) { |
| 404 | ((PagedViewWidget) checkable).setChecked(false, animated); |
| 405 | } else { |
| 406 | ((PagedViewIcon) checkable).setChecked(false, animated); |
| 407 | } |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | public void onDropCompleted(View target, Object dragInfo, boolean success) { |
| 412 | final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer); |
| 413 | |
| 414 | // Create a view, identical to the drag view, that is only used for animating the |
| 415 | // 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] | 416 | final int[] pos = mDragController.getDragView().getPosition(null); |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 417 | final View animView = dragLayer.createDragView(mDragBitmap, pos[0], pos[1]); |
| 418 | animView.setVisibility(View.VISIBLE); |
| 419 | |
| 420 | if (success) { |
| 421 | resetCheckedItem(true); |
| 422 | animateDropOntoScreen(animView, (ItemInfo) dragInfo, DROP_ANIM_DURATION, 0); |
| 423 | } else { |
| 424 | // Animate the icon/widget back to its original position |
| 425 | animateIntoPosition(animView, mDragViewOrigin[0], mDragViewOrigin[1], new Runnable() { |
| 426 | public void run() { |
| 427 | resetCheckedItem(false); |
| 428 | dragLayer.removeView(animView); |
| 429 | } |
| 430 | }); |
| 431 | } |
Patrick Dubroy | 7bccb42 | 2011-01-20 14:50:55 -0800 | [diff] [blame] | 432 | mLauncher.getWorkspace().onDragStopped(success); |
Winson Chung | 400438b | 2011-01-16 17:53:48 -0800 | [diff] [blame] | 433 | mLauncher.unlockScreenOrientation(); |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 434 | mDragBitmap = null; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | @Override |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 438 | public void onDragViewVisible() { |
| 439 | } |
| 440 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 441 | /** |
| 442 | * Animates the given item onto the center of a home screen, and then scales the item to |
| 443 | * look as though it's disappearing onto that screen. |
| 444 | */ |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 445 | private void animateItemOntoScreen(View dragView, |
| 446 | final CellLayout layout, final ItemInfo info) { |
| 447 | mTmpFloatPos[0] = layout.getWidth() / 2; |
| 448 | mTmpFloatPos[1] = layout.getHeight() / 2; |
| 449 | mLauncher.getWorkspace().mapPointFromChildToSelf(layout, mTmpFloatPos); |
| 450 | |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 451 | int dragViewWidth = dragView.getMeasuredWidth(); |
| 452 | int dragViewHeight = dragView.getMeasuredHeight(); |
| 453 | float heightOffset = 0; |
| 454 | float widthOffset = 0; |
| 455 | |
| 456 | if (dragView instanceof ImageView) { |
| 457 | Drawable d = ((ImageView) dragView).getDrawable(); |
| 458 | int width = d.getIntrinsicWidth(); |
| 459 | int height = d.getIntrinsicHeight(); |
| 460 | |
| 461 | if ((1.0 * width / height) >= (1.0f * dragViewWidth) / dragViewHeight) { |
| 462 | float f = (dragViewWidth / (width * 1.0f)); |
| 463 | heightOffset = ANIMATION_SCALE * (dragViewHeight - f * height) / 2; |
| 464 | } else { |
| 465 | float f = (dragViewHeight / (height * 1.0f)); |
| 466 | widthOffset = ANIMATION_SCALE * (dragViewWidth - f * width) / 2; |
| 467 | } |
| 468 | } |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 469 | final float toX = mTmpFloatPos[0] - dragView.getMeasuredWidth() / 2 + widthOffset; |
| 470 | final float toY = mTmpFloatPos[1] - dragView.getMeasuredHeight() / 2 + heightOffset; |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 471 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 472 | final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer); |
| 473 | final View dragCopy = dragLayer.createDragView(dragView); |
| 474 | dragCopy.setAlpha(1.0f); |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 475 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 476 | // Translate the item to the center of the appropriate home screen |
| 477 | animateIntoPosition(dragCopy, toX, toY, null); |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 478 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 479 | // The drop-onto-screen animation begins a bit later, but ends at the same time. |
| 480 | final int startDelay = TRANSLATE_ANIM_DURATION - DROP_ANIM_DURATION; |
| 481 | |
| 482 | // Scale down the icon and fade out the alpha |
| 483 | animateDropOntoScreen(dragCopy, info, DROP_ANIM_DURATION, startDelay); |
| 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 | * Animation which scales the view down and animates its alpha, making it appear to disappear |
| 488 | * onto a home screen. |
| 489 | */ |
| 490 | private void animateDropOntoScreen( |
| 491 | final View view, final ItemInfo info, int duration, int delay) { |
| 492 | final DragLayer dragLayer = (DragLayer) mLauncher.findViewById(R.id.drag_layer); |
| 493 | final CellLayout layout = mLauncher.getWorkspace().getCurrentDropLayout(); |
| 494 | |
| 495 | ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view, |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 496 | PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f), |
| 497 | PropertyValuesHolder.ofFloat("scaleX", ANIMATION_SCALE), |
| 498 | PropertyValuesHolder.ofFloat("scaleY", ANIMATION_SCALE)); |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 499 | anim.setInterpolator(new LinearInterpolator()); |
| 500 | if (delay > 0) { |
| 501 | anim.setStartDelay(delay); |
| 502 | } |
| 503 | anim.setDuration(duration); |
| 504 | anim.addListener(new AnimatorListenerAdapter() { |
| 505 | public void onAnimationEnd(Animator animation) { |
| 506 | dragLayer.removeView(view); |
| 507 | mLauncher.addExternalItemToScreen(info, layout); |
Patrick Dubroy | bbaa75c | 2011-03-08 18:47:40 -0800 | [diff] [blame] | 508 | info.dropPos = null; |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 509 | } |
| 510 | }); |
| 511 | anim.start(); |
| 512 | } |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 513 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 514 | /** |
| 515 | * Animates the x,y position of the view, and optionally execute a Runnable on animation end. |
| 516 | */ |
| 517 | private void animateIntoPosition( |
| 518 | View view, float toX, float toY, final Runnable endRunnable) { |
| 519 | ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view, |
| 520 | PropertyValuesHolder.ofFloat("x", toX), |
| 521 | PropertyValuesHolder.ofFloat("y", toY)); |
| 522 | anim.setInterpolator(mQuintEaseOutInterpolator); |
| 523 | anim.setDuration(TRANSLATE_ANIM_DURATION); |
| 524 | if (endRunnable != null) { |
| 525 | anim.addListener(new AnimatorListenerAdapter() { |
| 526 | @Override |
| 527 | public void onAnimationEnd(Animator animation) { |
| 528 | endRunnable.run(); |
| 529 | } |
| 530 | }); |
| 531 | } |
| 532 | anim.start(); |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 535 | @Override |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 536 | public void onClick(final View v) { |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 537 | // Return early if this is not initiated from a touch |
| 538 | if (!v.isInTouchMode()) return; |
| 539 | // Return early if we are still animating the pages |
Winson Chung | 4f9e107 | 2010-11-15 15:28:24 -0800 | [diff] [blame] | 540 | if (mNextPage != INVALID_PAGE) return; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 541 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 542 | // On certain pages, we allow single tap to mark items as selected so that they can be |
| 543 | // dropped onto the mini workspaces |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 544 | boolean enterChoiceMode = false; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 545 | switch (mCustomizationType) { |
| 546 | case WidgetCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 547 | mChoiceModeTitleText = R.string.cab_widget_selection_text; |
| 548 | enterChoiceMode = true; |
| 549 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 550 | case ApplicationCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 551 | mChoiceModeTitleText = R.string.cab_app_selection_text; |
| 552 | enterChoiceMode = true; |
| 553 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 554 | case ShortcutCustomization: |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 555 | mChoiceModeTitleText = R.string.cab_shortcut_selection_text; |
| 556 | enterChoiceMode = true; |
| 557 | break; |
| 558 | default: |
| 559 | break; |
| 560 | } |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 561 | |
Michael Jurka | 3125d9d | 2010-09-27 11:30:20 -0700 | [diff] [blame] | 562 | if (enterChoiceMode) { |
Michael Jurka | 6b4b25d | 2010-10-20 18:19:45 -0700 | [diff] [blame] | 563 | final ItemInfo itemInfo = (ItemInfo) v.getTag(); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 564 | |
Michael Jurka | 6b4b25d | 2010-10-20 18:19:45 -0700 | [diff] [blame] | 565 | Workspace w = mLauncher.getWorkspace(); |
| 566 | int currentWorkspaceScreen = mLauncher.getCurrentWorkspaceScreen(); |
| 567 | final CellLayout cl = (CellLayout)w.getChildAt(currentWorkspaceScreen); |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 568 | final View dragView = getDragView(v); |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 569 | |
Michael Jurka | 6b4b25d | 2010-10-20 18:19:45 -0700 | [diff] [blame] | 570 | animateClickFeedback(v, new Runnable() { |
| 571 | @Override |
| 572 | public void run() { |
Patrick Dubroy | 047379a | 2010-12-19 22:02:04 -0800 | [diff] [blame] | 573 | cl.calculateSpans(itemInfo); |
| 574 | if (cl.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY)) { |
| 575 | animateItemOntoScreen(dragView, cl, itemInfo); |
| 576 | } else { |
| 577 | mLauncher.showOutOfSpaceMessage(); |
| 578 | } |
Michael Jurka | 6b4b25d | 2010-10-20 18:19:45 -0700 | [diff] [blame] | 579 | } |
| 580 | }); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 581 | return; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // Otherwise, we just handle the single click here |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 585 | switch (mCustomizationType) { |
| 586 | case WallpaperCustomization: |
| 587 | // animate some feedback to the long press |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 588 | final View clickView = v; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 589 | animateClickFeedback(v, new Runnable() { |
| 590 | @Override |
| 591 | public void run() { |
| 592 | // add the shortcut |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 593 | ResolveInfo info = (ResolveInfo) clickView.getTag(); |
Winson Chung | 24ab2f1 | 2010-09-16 14:10:47 -0700 | [diff] [blame] | 594 | Intent createWallpapersIntent = new Intent(Intent.ACTION_SET_WALLPAPER); |
| 595 | ComponentName name = new ComponentName(info.activityInfo.packageName, |
| 596 | info.activityInfo.name); |
| 597 | createWallpapersIntent.setComponent(name); |
| 598 | mLauncher.processWallpaper(createWallpapersIntent); |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 599 | } |
| 600 | }); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 601 | break; |
| 602 | default: |
| 603 | break; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 607 | private Bitmap drawableToBitmap(Drawable d, float scaleX, float scaleY) { |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 608 | final Rect bounds = d.getBounds(); |
| 609 | final int w = bounds.width(); |
| 610 | final int h = bounds.height(); |
Michael Jurka | c4e772e | 2011-02-10 13:32:01 -0800 | [diff] [blame] | 611 | Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 612 | renderDrawableToBitmap(d, b, 0, 0, w, h, scaleX, scaleY); |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 613 | return b; |
| 614 | } |
| 615 | |
Adam Cohen | 120980b | 2010-12-08 11:05:37 -0800 | [diff] [blame] | 616 | private View getDragView(View v) { |
| 617 | return (mCustomizationType == CustomizationType.WidgetCustomization) ? |
| 618 | v.findViewById(R.id.widget_preview) : v; |
| 619 | } |
| 620 | |
Michael Jurka | 72b079e | 2010-12-10 01:03:53 -0800 | [diff] [blame] | 621 | protected boolean beginDragging(View v) { |
Winson Chung | 304dcde | 2011-01-07 11:17:23 -0800 | [diff] [blame] | 622 | if (!v.isInTouchMode()) return false; |
| 623 | if (!super.beginDragging(v)) return false; |
| 624 | |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 625 | // End the current choice mode before we start dragging anything |
| 626 | if (isChoiceMode(CHOICE_MODE_SINGLE)) { |
| 627 | endChoiceMode(); |
| 628 | } |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 629 | final Workspace workspace = mLauncher.getWorkspace(); |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 630 | boolean result = false; |
Winson Chung | 400438b | 2011-01-16 17:53:48 -0800 | [diff] [blame] | 631 | mLauncher.lockScreenOrientation(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 632 | switch (mCustomizationType) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 633 | case WidgetCustomization: { |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 634 | if (v instanceof PagedViewWidget) { |
| 635 | // Get the widget preview as the drag representation |
| 636 | final LinearLayout l = (LinearLayout) v; |
| 637 | final ImageView i = (ImageView) l.findViewById(R.id.widget_preview); |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 638 | |
| 639 | // Calculate how much to scale the drag preview |
| 640 | RectF tmpScaleRect = new RectF(0,0,1,1); |
| 641 | i.getImageMatrix().mapRect(tmpScaleRect); |
| 642 | |
| 643 | mDragBitmap = drawableToBitmap(i.getDrawable(), tmpScaleRect.right, |
| 644 | tmpScaleRect.bottom); |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 645 | i.getLocationOnScreen(mDragViewOrigin); |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 646 | PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) v.getTag(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 647 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 648 | int[] spanXY = CellLayout.rectToCell(getResources(), |
| 649 | createWidgetInfo.minWidth, createWidgetInfo.minHeight, null); |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 650 | createWidgetInfo.spanX = spanXY[0]; |
| 651 | createWidgetInfo.spanY = spanXY[1]; |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 652 | workspace.onDragStartedWithItemSpans(spanXY[0], spanXY[1], mDragBitmap); |
| 653 | mDragController.startDrag(i, mDragBitmap, this, createWidgetInfo, |
| 654 | DragController.DRAG_ACTION_COPY, null); |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 655 | result = true; |
| 656 | } |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 657 | break; |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 658 | } |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 659 | case ShortcutCustomization: |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 660 | case ApplicationCustomization: { |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 661 | if (v instanceof PagedViewIcon) { |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 662 | // get icon (top compound drawable, index is 1) |
| 663 | final TextView tv = (TextView) v; |
| 664 | final Drawable icon = tv.getCompoundDrawables()[1]; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 665 | mDragBitmap = drawableToBitmap(icon, 1.0f, 1.0f); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 666 | |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 667 | Object dragInfo = v.getTag(); |
| 668 | if (mCustomizationType == CustomizationType.ApplicationCustomization) { |
| 669 | // TODO: Not sure why we have to copy this |
| 670 | dragInfo = new ApplicationInfo((ApplicationInfo) dragInfo); |
| 671 | } |
| 672 | workspace.onDragStartedWithItemSpans(1, 1, mDragBitmap); |
| 673 | |
| 674 | // Calculate where to place the drag view in order to align the icon pixels with |
| 675 | // the original view. |
| 676 | v.getLocationOnScreen(mDragViewOrigin); |
| 677 | mDragViewOrigin[0] += (v.getWidth() - icon.getIntrinsicWidth()) / 2; |
| 678 | mDragViewOrigin[1] += v.getPaddingTop(); |
| 679 | |
| 680 | mDragController.startDrag(mDragBitmap, mDragViewOrigin[0], mDragViewOrigin[1], |
| 681 | this, dragInfo, DragController.DRAG_ACTION_COPY); |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 682 | result = true; |
| 683 | } |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 684 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 685 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 686 | } |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 687 | |
| 688 | // We toggle the checked state _after_ we create the view for the drag in case toggling the |
| 689 | // checked state changes the view's look |
Winson Chung | 94569f4 | 2011-01-17 14:09:17 -0800 | [diff] [blame] | 690 | if (result && (v instanceof Checkable)) { |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 691 | // In preparation for drag, we always reset the checked grand children regardless of |
| 692 | // what choice mode we are in |
| 693 | resetCheckedGrandchildren(); |
| 694 | |
| 695 | // Toggle the selection on the dragged app |
| 696 | Checkable checkable = (Checkable) v; |
| 697 | |
| 698 | // Note: we toggle the checkable state to actually cause an alpha fade for the duration |
| 699 | // of the drag of the item. (The fade-in will occur when all checked states are |
| 700 | // disabled when dragging ends) |
| 701 | checkable.toggle(); |
| 702 | } |
| 703 | |
| 704 | return result; |
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 | /** |
| 708 | * Pre-processes the layout of the different widget pages. |
| 709 | * @return the number of pages of widgets that we have |
| 710 | */ |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 711 | private int relayoutWidgets() { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 712 | if (mWidgetList.isEmpty()) return 0; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 713 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 714 | // create a new page for the first set of widgets |
| 715 | ArrayList<AppWidgetProviderInfo> newPage = new ArrayList<AppWidgetProviderInfo>(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 716 | mWidgetPages.clear(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 717 | mWidgetPages.add(newPage); |
| 718 | |
| 719 | // do this until we have no more widgets to lay out |
| 720 | final int maxNumCellsPerRow = mMaxWidgetsCellHSpan; |
| 721 | final int widgetCount = mWidgetList.size(); |
| 722 | int numCellsInRow = 0; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 723 | for (int i = 0; i < widgetCount; ++i) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 724 | final AppWidgetProviderInfo info = mWidgetList.get(i); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 725 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 726 | // determine the size of the current widget |
| 727 | int cellSpanX = Math.max(sMinWidgetCellHSpan, Math.min(sMaxWidgetCellHSpan, |
| 728 | mWorkspaceWidgetLayout.estimateCellHSpan(info.minWidth))); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 729 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 730 | // create a new page if necessary |
| 731 | if ((numCellsInRow + cellSpanX) > maxNumCellsPerRow) { |
| 732 | numCellsInRow = 0; |
| 733 | newPage = new ArrayList<AppWidgetProviderInfo>(); |
| 734 | mWidgetPages.add(newPage); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 737 | // add the item to the current page |
| 738 | newPage.add(info); |
| 739 | numCellsInRow += cellSpanX; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 740 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 741 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 742 | return mWidgetPages.size(); |
| 743 | } |
| 744 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 745 | /** |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 746 | * Helper function to draw a drawable to the specified canvas with the specified bounds. |
| 747 | */ |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 748 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, |
| 749 | float scaleX, float scaleY) { |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 750 | if (bitmap != null) mCanvas.setBitmap(bitmap); |
| 751 | mCanvas.save(); |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 752 | mCanvas.scale(scaleX, scaleY); |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 753 | final Rect oldBounds = d.copyBounds(); |
| 754 | d.setBounds(x, y, x + w, y + h); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 755 | d.draw(mCanvas); |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 756 | d.setBounds(oldBounds); // Restore the bounds |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 757 | mCanvas.restore(); |
| 758 | } |
| 759 | |
Adam Cohen | 7b9d3a6 | 2010-12-07 21:49:34 -0800 | [diff] [blame] | 760 | /* |
| 761 | * This method fetches an xml file specified in the manifest identified by |
| 762 | * WallpaperManager.WALLPAPER_PREVIEW_META_DATA). The xml file specifies |
| 763 | * an image which will be used as the wallpaper preview for an activity |
| 764 | * which responds to ACTION_SET_WALLPAPER. This image is returned and used |
| 765 | * in the customize drawer. |
| 766 | */ |
| 767 | private Drawable parseWallpaperPreviewXml(ComponentName component, ResolveInfo ri) { |
Adam Cohen | 7b9d3a6 | 2010-12-07 21:49:34 -0800 | [diff] [blame] | 768 | ActivityInfo activityInfo = ri.activityInfo; |
| 769 | XmlResourceParser parser = null; |
| 770 | try { |
| 771 | parser = activityInfo.loadXmlMetaData(mPackageManager, |
| 772 | WallpaperManager.WALLPAPER_PREVIEW_META_DATA); |
| 773 | if (parser == null) { |
| 774 | Slog.w(TAG, "No " + WallpaperManager.WALLPAPER_PREVIEW_META_DATA + " meta-data for " |
| 775 | + "wallpaper provider '" + component + '\''); |
| 776 | return null; |
| 777 | } |
| 778 | |
| 779 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 780 | |
| 781 | int type; |
| 782 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 783 | && type != XmlPullParser.START_TAG) { |
| 784 | // drain whitespace, comments, etc. |
| 785 | } |
| 786 | |
| 787 | String nodeName = parser.getName(); |
| 788 | if (!"wallpaper-preview".equals(nodeName)) { |
| 789 | Slog.w(TAG, "Meta-data does not start with wallpaper-preview tag for " |
| 790 | + "wallpaper provider '" + component + '\''); |
| 791 | return null; |
| 792 | } |
| 793 | |
| 794 | // If metaData was null, we would have returned earlier when getting |
| 795 | // the parser No need to do the check here |
| 796 | Resources res = mPackageManager.getResourcesForApplication( |
| 797 | activityInfo.applicationInfo); |
| 798 | |
| 799 | TypedArray sa = res.obtainAttributes(attrs, |
| 800 | com.android.internal.R.styleable.WallpaperPreviewInfo); |
| 801 | |
| 802 | TypedValue value = sa.peekValue( |
| 803 | com.android.internal.R.styleable.WallpaperPreviewInfo_staticWallpaperPreview); |
| 804 | if (value == null) return null; |
| 805 | |
| 806 | return res.getDrawable(value.resourceId); |
| 807 | } catch (Exception e) { |
| 808 | Slog.w(TAG, "XML parsing failed for wallpaper provider '" + component + '\'', e); |
| 809 | return null; |
| 810 | } finally { |
| 811 | if (parser != null) parser.close(); |
| 812 | } |
| 813 | } |
| 814 | |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 815 | /** |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 816 | * This method will extract the preview image specified by the wallpaper source provider (if it |
| 817 | * exists) otherwise, it will try to generate a default image preview. |
| 818 | */ |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 819 | private FastBitmapDrawable getWallpaperPreview(ResolveInfo info) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 820 | // To be implemented later: resolving the up-to-date wallpaper thumbnail |
| 821 | |
| 822 | final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1); |
| 823 | final int dim = mWorkspaceWidgetLayout.estimateCellWidth(mWallpaperCellHSpan); |
| 824 | Resources resources = mLauncher.getResources(); |
| 825 | |
| 826 | // Create a new bitmap to hold the widget preview |
| 827 | int width = (int) (dim * sScaleFactor); |
| 828 | int height = (int) (dim * sScaleFactor); |
| 829 | final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); |
Adam Cohen | 7b9d3a6 | 2010-12-07 21:49:34 -0800 | [diff] [blame] | 830 | |
| 831 | Drawable background = parseWallpaperPreviewXml( |
| 832 | new ComponentName(info.activityInfo.packageName, info.activityInfo.name), info); |
| 833 | boolean foundCustomDrawable = background != null; |
| 834 | |
| 835 | if (!foundCustomDrawable) { |
| 836 | background = resources.getDrawable(R.drawable.default_widget_preview); |
| 837 | } |
| 838 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 839 | renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 840 | |
Adam Cohen | 7b9d3a6 | 2010-12-07 21:49:34 -0800 | [diff] [blame] | 841 | // If we don't have a custom icon, we use the app icon on the default background |
| 842 | if (!foundCustomDrawable) { |
| 843 | try { |
| 844 | final IconCache iconCache = |
| 845 | ((LauncherApplication) mLauncher.getApplication()).getIconCache(); |
| 846 | Drawable icon = new FastBitmapDrawable(Utilities.createIconBitmap( |
| 847 | iconCache.getFullResIcon(info, mPackageManager), mContext)); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 848 | |
Adam Cohen | 7b9d3a6 | 2010-12-07 21:49:34 -0800 | [diff] [blame] | 849 | final int iconSize = minDim / 2; |
| 850 | final int offset = iconSize / 4; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 851 | renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f); |
Adam Cohen | 7b9d3a6 | 2010-12-07 21:49:34 -0800 | [diff] [blame] | 852 | } catch (Resources.NotFoundException e) { |
| 853 | // if we can't find the icon, then just don't draw it |
| 854 | } |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 855 | } |
| 856 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 857 | FastBitmapDrawable drawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 858 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); |
| 859 | return drawable; |
| 860 | } |
| 861 | |
| 862 | /** |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 863 | * This method will extract the preview image specified by the widget developer (if it exists), |
| 864 | * 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] | 865 | * @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] | 866 | */ |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 867 | private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 868 | final PackageManager packageManager = mPackageManager; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 869 | String packageName = info.provider.getPackageName(); |
| 870 | Drawable drawable = null; |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 871 | FastBitmapDrawable newDrawable = null; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 872 | if (info.previewImage != 0) { |
| 873 | drawable = packageManager.getDrawable(packageName, info.previewImage, null); |
| 874 | if (drawable == null) { |
| 875 | Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon) |
| 876 | + " for provider: " + info.provider); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
| 880 | // If we don't have a preview image, create a default one |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 881 | final int minDim = mWorkspaceWidgetLayout.estimateCellWidth(1); |
| 882 | final int maxDim = mWorkspaceWidgetLayout.estimateCellWidth(3); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 883 | if (drawable == null) { |
| 884 | Resources resources = mLauncher.getResources(); |
| 885 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 886 | // Create a new bitmap to hold the widget preview |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 887 | int width = (int) (Math.max(minDim, Math.min(maxDim, info.minWidth)) * sScaleFactor); |
| 888 | int height = (int) (Math.max(minDim, Math.min(maxDim, info.minHeight)) * sScaleFactor); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 889 | final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); |
| 890 | final Drawable background = resources.getDrawable(R.drawable.default_widget_preview); |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 891 | renderDrawableToBitmap(background, bitmap, 0, 0, width, height, 1.0f, 1.0f); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 892 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 893 | // Draw the icon flush left |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 894 | try { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 895 | Drawable icon = null; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 896 | if (info.icon > 0) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 897 | icon = packageManager.getDrawable(packageName, info.icon, null); |
Winson Chung | 5f94172 | 2010-09-28 16:36:43 -0700 | [diff] [blame] | 898 | } |
| 899 | if (icon == null) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 900 | icon = resources.getDrawable(R.drawable.ic_launcher_application); |
| 901 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 902 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 903 | final int iconSize = minDim / 2; |
| 904 | final int offset = iconSize / 4; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 905 | renderDrawableToBitmap(icon, null, offset, offset, iconSize, iconSize, 1.0f, 1.0f); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 906 | } catch (Resources.NotFoundException e) { |
| 907 | // if we can't find the icon, then just don't draw it |
| 908 | } |
| 909 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 910 | newDrawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 911 | } else { |
| 912 | // Scale down the preview if necessary |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 913 | final float imageWidth = drawable.getIntrinsicWidth(); |
| 914 | final float imageHeight = drawable.getIntrinsicHeight(); |
| 915 | final float aspect = (float) imageWidth / imageHeight; |
| 916 | final int scaledWidth = |
| 917 | (int) (Math.max(minDim, Math.min(maxDim, imageWidth)) * sScaleFactor); |
| 918 | final int scaledHeight = |
| 919 | (int) (Math.max(minDim, Math.min(maxDim, imageHeight)) * sScaleFactor); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 920 | int width; |
| 921 | int height; |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 922 | if (aspect >= 1.0f) { |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 923 | width = scaledWidth; |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 924 | height = (int) (((float) scaledWidth / imageWidth) * imageHeight); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 925 | } else { |
| 926 | height = scaledHeight; |
Winson Chung | 94ba5b1 | 2010-11-08 17:17:47 -0800 | [diff] [blame] | 927 | width = (int) (((float) scaledHeight / imageHeight) * imageWidth); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 931 | renderDrawableToBitmap(drawable, bitmap, 0, 0, width, height, 1.0f, 1.0f); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 932 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 933 | newDrawable = new FastBitmapDrawable(bitmap); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 934 | } |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 935 | newDrawable.setBounds(0, 0, newDrawable.getIntrinsicWidth(), |
| 936 | newDrawable.getIntrinsicHeight()); |
| 937 | return newDrawable; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | private void setupPage(PagedViewCellLayout layout) { |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 941 | layout.setCellCount(mCellCountX, mCellCountY); |
| 942 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, mPageLayoutPaddingRight, |
| 943 | mPageLayoutPaddingBottom); |
Winson Chung | ef0066b | 2010-10-21 11:55:00 -0700 | [diff] [blame] | 944 | layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 947 | private void setupWorkspaceLayout() { |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 948 | mWorkspaceWidgetLayout.setCellCount(mCellCountX, mCellCountY); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 949 | mWorkspaceWidgetLayout.setPadding(20, 10, 20, 0); |
| 950 | |
| 951 | mMaxWidgetWidth = mWorkspaceWidgetLayout.estimateCellWidth(sMaxWidgetCellHSpan); |
| 952 | } |
| 953 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 954 | private void syncWidgetPages() { |
| 955 | if (mWidgetList == null) return; |
| 956 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 957 | // we need to repopulate with the LinearLayout layout for the widget pages |
| 958 | removeAllViews(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 959 | int numPages = relayoutWidgets(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 960 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 961 | LinearLayout layout = new PagedViewExtendedLayout(getContext()); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 962 | layout.setGravity(Gravity.CENTER_HORIZONTAL); |
Winson Chung | ef0066b | 2010-10-21 11:55:00 -0700 | [diff] [blame] | 963 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 964 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 965 | |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 966 | addView(layout, new LinearLayout.LayoutParams( |
| 967 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 968 | LinearLayout.LayoutParams.MATCH_PARENT)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
| 972 | private void syncWidgetPageItems(int page) { |
| 973 | // ensure that we have the right number of items on the pages |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 974 | LinearLayout layout = (LinearLayout) getChildAt(page); |
| 975 | final ArrayList<AppWidgetProviderInfo> list = mWidgetPages.get(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 976 | final int count = list.size(); |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 977 | final int numPages = getPageCount(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 978 | layout.removeAllViews(); |
| 979 | for (int i = 0; i < count; ++i) { |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 980 | final AppWidgetProviderInfo info = (AppWidgetProviderInfo) list.get(i); |
| 981 | final PendingAddWidgetInfo createItemInfo = new PendingAddWidgetInfo(info, null, null); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 982 | final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth, |
| 983 | info.minHeight, null); |
| 984 | final FastBitmapDrawable icon = getWidgetPreview(info); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 985 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 986 | PagedViewWidget l = (PagedViewWidget) mInflater.inflate( |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 987 | R.layout.customize_paged_view_widget, layout, false); |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 988 | |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 989 | l.applyFromAppWidgetProviderInfo(info, icon, mMaxWidgetWidth, cellSpans, |
| 990 | mPageViewIconCache, (numPages > 1)); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 991 | l.setTag(createItemInfo); |
| 992 | l.setOnClickListener(this); |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 993 | l.setOnTouchListener(this); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 994 | l.setOnLongClickListener(this); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 995 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 996 | layout.addView(l); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1000 | private void syncWallpaperPages() { |
| 1001 | if (mWallpaperList == null) return; |
| 1002 | |
| 1003 | // We need to repopulate the LinearLayout for the wallpaper pages |
| 1004 | removeAllViews(); |
| 1005 | int numPages = (int) Math.ceil((float) (mWallpaperList.size() * mWallpaperCellHSpan) / |
Winson Chung | 78bd53c | 2010-12-09 13:50:24 -0800 | [diff] [blame] | 1006 | mMaxWallpaperCellHSpan); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1007 | for (int i = 0; i < numPages; ++i) { |
| 1008 | LinearLayout layout = new PagedViewExtendedLayout(getContext()); |
| 1009 | layout.setGravity(Gravity.CENTER_HORIZONTAL); |
| 1010 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 1011 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 1012 | |
Winson Chung | e22a8e9 | 2010-11-12 13:40:58 -0800 | [diff] [blame] | 1013 | addView(layout, new LinearLayout.LayoutParams( |
| 1014 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 1015 | LinearLayout.LayoutParams.MATCH_PARENT)); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | private void syncWallpaperPageItems(int page) { |
| 1020 | // Load the items on to the pages |
| 1021 | LinearLayout layout = (LinearLayout) getChildAt(page); |
| 1022 | layout.removeAllViews(); |
| 1023 | final int count = mWallpaperList.size(); |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 1024 | final int numPages = getPageCount(); |
Winson Chung | 78bd53c | 2010-12-09 13:50:24 -0800 | [diff] [blame] | 1025 | final int numItemsPerPage = mMaxWallpaperCellHSpan / mWallpaperCellHSpan; |
Winson Chung | d28ed49 | 2010-11-22 14:34:57 -0800 | [diff] [blame] | 1026 | final int startIndex = page * numItemsPerPage; |
| 1027 | final int endIndex = Math.min(count, startIndex + numItemsPerPage); |
| 1028 | for (int i = startIndex; i < endIndex; ++i) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1029 | final ResolveInfo info = mWallpaperList.get(i); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 1030 | final FastBitmapDrawable icon = getWallpaperPreview(info); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1031 | |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 1032 | PagedViewWidget l = (PagedViewWidget) mInflater.inflate( |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1033 | R.layout.customize_paged_view_wallpaper, layout, false); |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 1034 | l.applyFromWallpaperInfo(info, mPackageManager, icon, mMaxWidgetWidth, |
| 1035 | mPageViewIconCache, (numPages > 1)); |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1036 | l.setTag(info); |
| 1037 | l.setOnClickListener(this); |
| 1038 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1039 | layout.addView(l); |
| 1040 | } |
| 1041 | } |
| 1042 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1043 | private void syncListPages(List<ResolveInfo> list) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1044 | // we need to repopulate with PagedViewCellLayouts |
| 1045 | removeAllViews(); |
| 1046 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1047 | // ensure that we have the right number of pages |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 1048 | int numPages = (int) Math.ceil((float) list.size() / (mCellCountX * mCellCountY)); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1049 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1050 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 1051 | setupPage(layout); |
| 1052 | addView(layout); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | private void syncListPageItems(int page, List<ResolveInfo> list) { |
| 1057 | // ensure that we have the right number of items on the pages |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 1058 | final int numPages = getPageCount(); |
| 1059 | final int numCells = mCellCountX * mCellCountY; |
| 1060 | final int startIndex = page * numCells; |
| 1061 | final int endIndex = Math.min(startIndex + numCells, list.size()); |
| 1062 | final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1063 | // TODO: we can optimize by just re-applying to existing views |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1064 | layout.removeAllViewsOnPage(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1065 | for (int i = startIndex; i < endIndex; ++i) { |
| 1066 | ResolveInfo info = list.get(i); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 1067 | PendingAddItemInfo createItemInfo = new PendingAddItemInfo(); |
| 1068 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 1069 | PagedViewIcon icon = (PagedViewIcon) mInflater.inflate( |
| 1070 | R.layout.customize_paged_view_item, layout, false); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 1071 | icon.applyFromResolveInfo(info, mPackageManager, mPageViewIconCache, |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 1072 | ((LauncherApplication) mLauncher.getApplication()).getIconCache(), |
| 1073 | (numPages > 1)); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 1074 | switch (mCustomizationType) { |
| 1075 | case WallpaperCustomization: |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 1076 | icon.setOnClickListener(this); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 1077 | break; |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 1078 | case ShortcutCustomization: |
| 1079 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
| 1080 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 1081 | info.activityInfo.name); |
| 1082 | icon.setTag(createItemInfo); |
| 1083 | icon.setOnClickListener(this); |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 1084 | icon.setOnTouchListener(this); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 1085 | icon.setOnLongClickListener(this); |
| 1086 | break; |
| 1087 | default: |
| 1088 | break; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 1089 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1090 | |
| 1091 | final int index = i - startIndex; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 1092 | final int x = index % mCellCountX; |
| 1093 | final int y = index / mCellCountX; |
| 1094 | setupPage(layout); |
| 1095 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | private void syncAppPages() { |
| 1100 | if (mApps == null) return; |
| 1101 | |
| 1102 | // We need to repopulate with PagedViewCellLayouts |
| 1103 | removeAllViews(); |
| 1104 | |
| 1105 | // Ensure that we have the right number of pages |
| 1106 | int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); |
| 1107 | for (int i = 0; i < numPages; ++i) { |
| 1108 | PagedViewCellLayout layout = new PagedViewCellLayout(getContext()); |
| 1109 | setupPage(layout); |
| 1110 | addView(layout); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | private void syncAppPageItems(int page) { |
| 1115 | if (mApps == null) return; |
| 1116 | |
| 1117 | // ensure that we have the right number of items on the pages |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 1118 | final int numPages = getPageCount(); |
| 1119 | final int numCells = mCellCountX * mCellCountY; |
| 1120 | final int startIndex = page * numCells; |
| 1121 | final int endIndex = Math.min(startIndex + numCells, mApps.size()); |
| 1122 | final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 1123 | // TODO: we can optimize by just re-applying to existing views |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1124 | layout.removeAllViewsOnPage(); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 1125 | for (int i = startIndex; i < endIndex; ++i) { |
| 1126 | final ApplicationInfo info = mApps.get(i); |
| 1127 | PagedViewIcon icon = (PagedViewIcon) mInflater.inflate( |
| 1128 | R.layout.all_apps_paged_view_application, layout, false); |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 1129 | icon.applyFromApplicationInfo(info, mPageViewIconCache, true, (numPages > 1)); |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 1130 | icon.setOnClickListener(this); |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 1131 | icon.setOnTouchListener(this); |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 1132 | icon.setOnLongClickListener(this); |
| 1133 | |
| 1134 | final int index = i - startIndex; |
| 1135 | final int x = index % mCellCountX; |
| 1136 | final int y = index / mCellCountX; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1137 | setupPage(layout); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 1138 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1142 | @Override |
| 1143 | public void syncPages() { |
Michael Jurka | ea2daff | 2011-05-17 18:21:03 -0700 | [diff] [blame] | 1144 | if (mFirstMeasure) { |
| 1145 | // We don't know our size yet, which means we haven't calculated cell count x/y; |
| 1146 | // onMeasure will call us once we figure out our size |
| 1147 | return; |
| 1148 | } |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1149 | boolean enforceMinimumPagedWidths = false; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1150 | boolean centerPagedViewCellLayouts = false; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1151 | switch (mCustomizationType) { |
| 1152 | case WidgetCustomization: |
| 1153 | syncWidgetPages(); |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1154 | enforceMinimumPagedWidths = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1155 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1156 | case ShortcutCustomization: |
| 1157 | syncListPages(mShortcutList); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1158 | centerPagedViewCellLayouts = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1159 | break; |
| 1160 | case WallpaperCustomization: |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1161 | syncWallpaperPages(); |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1162 | enforceMinimumPagedWidths = true; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1163 | break; |
Winson Chung | 5ffd8ea | 2010-09-23 18:40:29 -0700 | [diff] [blame] | 1164 | case ApplicationCustomization: |
| 1165 | syncAppPages(); |
| 1166 | centerPagedViewCellLayouts = false; |
| 1167 | break; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1168 | default: |
| 1169 | removeAllViews(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1170 | setCurrentPage(0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1171 | break; |
| 1172 | } |
| 1173 | |
| 1174 | // only try and center the page if there is one page |
| 1175 | final int childCount = getChildCount(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1176 | if (centerPagedViewCellLayouts) { |
| 1177 | if (childCount == 1) { |
| 1178 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(0); |
| 1179 | layout.enableCenteredContent(true); |
| 1180 | } else { |
| 1181 | for (int i = 0; i < childCount; ++i) { |
| 1182 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
| 1183 | layout.enableCenteredContent(false); |
| 1184 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1188 | // Set a min page width for PagedView layout if we have more than a single page |
| 1189 | if (enforceMinimumPagedWidths) { |
| 1190 | setMinimumWidthOverride((childCount > 1) ? mMinPageWidth : 0); |
| 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 | 12ac0d6 | 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 | } |