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