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