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