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