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