Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 19 | import android.animation.AnimatorSet; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 20 | import android.animation.ObjectAnimator; |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 22 | import android.appwidget.AppWidgetManager; |
| 23 | import android.appwidget.AppWidgetProviderInfo; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 27 | import android.content.pm.ActivityInfo; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.ResolveInfo; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 30 | import android.content.res.Configuration; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 31 | import android.content.res.Resources; |
| 32 | import android.content.res.TypedArray; |
| 33 | import android.graphics.Bitmap; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 34 | import android.graphics.Bitmap.Config; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 35 | import android.graphics.Canvas; |
| 36 | import android.graphics.Rect; |
| 37 | import android.graphics.drawable.Drawable; |
| 38 | import android.util.AttributeSet; |
| 39 | import android.util.Log; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 40 | import android.util.LruCache; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 41 | import android.view.LayoutInflater; |
| 42 | import android.view.View; |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 43 | import android.view.ViewGroup; |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 44 | import android.view.animation.AccelerateInterpolator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 45 | import android.widget.ImageView; |
| 46 | import android.widget.TextView; |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 47 | import android.widget.Toast; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 48 | |
| 49 | import com.android.launcher.R; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 50 | import com.android.launcher2.DropTarget.DragObject; |
| 51 | |
| 52 | import java.util.ArrayList; |
| 53 | import java.util.Collections; |
| 54 | import java.util.Iterator; |
| 55 | import java.util.List; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 56 | |
| 57 | public class AppsCustomizePagedView extends PagedViewWithDraggableItems implements |
| 58 | AllAppsView, View.OnClickListener, DragSource { |
| 59 | static final String LOG_TAG = "AppsCustomizePagedView"; |
| 60 | |
| 61 | /** |
| 62 | * The different content types that this paged view can show. |
| 63 | */ |
| 64 | public enum ContentType { |
| 65 | Applications, |
Winson Chung | 6a26e5b | 2011-05-26 14:36:06 -0700 | [diff] [blame] | 66 | Widgets |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Refs |
| 70 | private Launcher mLauncher; |
| 71 | private DragController mDragController; |
| 72 | private final LayoutInflater mLayoutInflater; |
| 73 | private final PackageManager mPackageManager; |
| 74 | |
| 75 | // Content |
| 76 | private ContentType mContentType; |
| 77 | private ArrayList<ApplicationInfo> mApps; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 78 | private List<Object> mWidgets; |
| 79 | |
| 80 | // Caching |
| 81 | private Drawable mDefaultWidgetBackground; |
| 82 | private final int sWidgetPreviewCacheSize = 1 * 1024 * 1024; // 1 MiB |
| 83 | private LruCache<Object, Bitmap> mWidgetPreviewCache; |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 84 | private IconCache mIconCache; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 85 | |
| 86 | // Dimens |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 87 | private Runnable mOnSizeChangedCallback; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 88 | private int mContentWidth; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 89 | private int mMaxWidgetSpan, mMinWidgetSpan; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 90 | private int mWidgetWidthGap, mWidgetHeightGap; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 91 | private int mWidgetCountX, mWidgetCountY; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 92 | private final int mWidgetPreviewIconPaddedDimension; |
| 93 | private final float sWidgetPreviewIconPaddingPercentage = 0.25f; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 94 | private PagedViewCellLayout mWidgetSpacingLayout; |
| 95 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 96 | // Animations |
| 97 | private final float ANIMATION_SCALE = 0.5f; |
| 98 | private final int TRANSLATE_ANIM_DURATION = 400; |
| 99 | private final int DROP_ANIM_DURATION = 200; |
| 100 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 101 | public AppsCustomizePagedView(Context context, AttributeSet attrs) { |
| 102 | super(context, attrs); |
| 103 | mLayoutInflater = LayoutInflater.from(context); |
| 104 | mPackageManager = context.getPackageManager(); |
| 105 | mContentType = ContentType.Applications; |
| 106 | mApps = new ArrayList<ApplicationInfo>(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 107 | mWidgets = new ArrayList<Object>(); |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 108 | mIconCache = ((LauncherApplication) context.getApplicationContext()).getIconCache(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 109 | mWidgetPreviewCache = new LruCache<Object, Bitmap>(sWidgetPreviewCacheSize) { |
| 110 | protected int sizeOf(Object key, Bitmap value) { |
| 111 | return value.getByteCount(); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | // Save the default widget preview background |
| 116 | Resources resources = context.getResources(); |
| 117 | mDefaultWidgetBackground = resources.getDrawable(R.drawable.default_widget_preview); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 118 | |
| 119 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, 0, 0); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 120 | // TODO-APPS_CUSTOMIZE: remove these unnecessary attrs after |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 121 | mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6); |
| 122 | mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4); |
| 123 | a.recycle(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 124 | a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 125 | mWidgetWidthGap = |
| 126 | a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellWidthGap, 0); |
| 127 | mWidgetHeightGap = |
| 128 | a.getDimensionPixelSize(R.styleable.AppsCustomizePagedView_widgetCellHeightGap, 0); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 129 | mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2); |
| 130 | mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2); |
| 131 | a.recycle(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 132 | mWidgetSpacingLayout = new PagedViewCellLayout(getContext()); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 133 | |
| 134 | // The max widget span is the length N, such that NxN is the largest bounds that the widget |
| 135 | // preview can be before applying the widget scaling |
| 136 | mMinWidgetSpan = 1; |
| 137 | mMaxWidgetSpan = 3; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 138 | |
| 139 | // The padding on the non-matched dimension for the default widget preview icons |
| 140 | // (top + bottom) |
| 141 | int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size); |
| 142 | mWidgetPreviewIconPaddedDimension = |
| 143 | (int) (iconSize * (1 + (2 * sWidgetPreviewIconPaddingPercentage))); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | protected void init() { |
| 148 | super.init(); |
| 149 | mCenterPagesVertically = false; |
| 150 | |
| 151 | Context context = getContext(); |
| 152 | Resources r = context.getResources(); |
| 153 | setDragSlopeThreshold(r.getInteger(R.integer.config_appsCustomizeDragSlopeThreshold)/100f); |
| 154 | } |
| 155 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 156 | @Override |
| 157 | protected void onWallpaperTap(android.view.MotionEvent ev) { |
| 158 | mLauncher.showWorkspace(true); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * This differs from isDataReady as this is the test done if isDataReady is not set. |
| 163 | */ |
| 164 | private boolean testDataReady() { |
| 165 | return !mApps.isEmpty() && !mWidgets.isEmpty(); |
| 166 | } |
| 167 | |
| 168 | protected void onDataReady(int width, int height) { |
| 169 | // Note that we transpose the counts in portrait so that we get a similar layout |
| 170 | boolean isLandscape = getResources().getConfiguration().orientation == |
| 171 | Configuration.ORIENTATION_LANDSCAPE; |
| 172 | int maxCellCountX = Integer.MAX_VALUE; |
| 173 | int maxCellCountY = Integer.MAX_VALUE; |
| 174 | if (LauncherApplication.isScreenLarge()) { |
| 175 | maxCellCountX = (isLandscape ? LauncherModel.getCellCountX() : |
| 176 | LauncherModel.getCellCountY()); |
| 177 | maxCellCountY = (isLandscape ? LauncherModel.getCellCountY() : |
| 178 | LauncherModel.getCellCountX()); |
| 179 | } |
| 180 | |
| 181 | // Now that the data is ready, we can calculate the content width, the number of cells to |
| 182 | // use for each page |
| 183 | mWidgetSpacingLayout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
| 184 | mWidgetSpacingLayout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 185 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 186 | mWidgetSpacingLayout.calculateCellCount(width, height, maxCellCountX, maxCellCountY); |
| 187 | mCellCountX = mWidgetSpacingLayout.getCellCountX(); |
| 188 | mCellCountY = mWidgetSpacingLayout.getCellCountY(); |
| 189 | mWidgetCountX = Math.max(1, (int) Math.round(mCellCountX / 2f)); |
| 190 | mWidgetCountY = Math.max(1, (int) Math.round(mCellCountY / 3f)); |
| 191 | mContentWidth = mWidgetSpacingLayout.getContentWidth(); |
| 192 | |
| 193 | // Notify our parent so that we can synchronize the tab bar width to this page width |
| 194 | if (mOnSizeChangedCallback != null) { |
| 195 | mOnSizeChangedCallback.run(); |
| 196 | } |
| 197 | |
| 198 | invalidatePageData(); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 203 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 204 | int height = MeasureSpec.getSize(heightMeasureSpec); |
| 205 | if (!isDataReady()) { |
| 206 | if (testDataReady()) { |
| 207 | setDataIsReady(); |
| 208 | setMeasuredDimension(width, height); |
| 209 | onDataReady(width, height); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 214 | } |
| 215 | |
| 216 | public void setOnSizeChangedCallback(Runnable r) { |
| 217 | mOnSizeChangedCallback = r; |
| 218 | } |
| 219 | |
Winson Chung | 34efdaf | 2011-05-24 14:19:56 -0700 | [diff] [blame] | 220 | /** Removes and returns the ResolveInfo with the specified ComponentName */ |
| 221 | private ResolveInfo removeResolveInfoWithComponentName(List<ResolveInfo> list, |
| 222 | ComponentName cn) { |
| 223 | Iterator<ResolveInfo> iter = list.iterator(); |
| 224 | while (iter.hasNext()) { |
| 225 | ResolveInfo rinfo = iter.next(); |
| 226 | ActivityInfo info = rinfo.activityInfo; |
| 227 | ComponentName c = new ComponentName(info.packageName, info.name); |
| 228 | if (c.equals(cn)) { |
| 229 | iter.remove(); |
| 230 | return rinfo; |
| 231 | } |
| 232 | } |
| 233 | return null; |
| 234 | } |
| 235 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 236 | public void onPackagesUpdated() { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 237 | // Get the list of widgets and shortcuts |
| 238 | mWidgets.clear(); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 239 | List<AppWidgetProviderInfo> widgets = |
| 240 | AppWidgetManager.getInstance(mLauncher).getInstalledProviders(); |
| 241 | Collections.sort(widgets, |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 242 | new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager)); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 243 | Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); |
| 244 | List<ResolveInfo> shortcuts = mPackageManager.queryIntentActivities(shortcutsIntent, 0); |
| 245 | Collections.sort(shortcuts, |
| 246 | new LauncherModel.WidgetAndShortcutNameComparator(mPackageManager)); |
| 247 | mWidgets.addAll(widgets); |
| 248 | mWidgets.addAll(shortcuts); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 249 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 250 | // The next layout pass will trigger data-ready if both widgets and apps are set, so request |
| 251 | // a layout to do this test and invalidate the page data when ready. |
| 252 | if (testDataReady()) requestLayout(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void onClick(View v) { |
| 257 | if (v instanceof PagedViewIcon) { |
| 258 | // Animate some feedback to the click |
| 259 | final ApplicationInfo appInfo = (ApplicationInfo) v.getTag(); |
| 260 | animateClickFeedback(v, new Runnable() { |
| 261 | @Override |
| 262 | public void run() { |
| 263 | mLauncher.startActivitySafely(appInfo.intent, appInfo); |
| 264 | } |
| 265 | }); |
| 266 | } else if (v instanceof PagedViewWidget) { |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 267 | // Let the user know that they have to long press to add a widget |
| 268 | Toast.makeText(getContext(), R.string.long_press_widget_to_add, |
| 269 | Toast.LENGTH_SHORT).show(); |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 270 | |
Winson Chung | d2e87b3 | 2011-06-02 10:53:07 -0700 | [diff] [blame] | 271 | // Create a little animation to show that the widget can move |
| 272 | float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY); |
| 273 | final ImageView p = (ImageView) v.findViewById(R.id.widget_preview); |
| 274 | AnimatorSet bounce = new AnimatorSet(); |
| 275 | ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY", offsetY); |
| 276 | tyuAnim.setDuration(125); |
| 277 | ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY", 0f); |
| 278 | tydAnim.setDuration(100); |
| 279 | bounce.play(tyuAnim).before(tydAnim); |
| 280 | bounce.setInterpolator(new AccelerateInterpolator()); |
| 281 | bounce.start(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 282 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* |
| 286 | * PagedViewWithDraggableItems implementation |
| 287 | */ |
| 288 | @Override |
| 289 | protected void determineDraggingStart(android.view.MotionEvent ev) { |
| 290 | // Disable dragging by pulling an app down for now. |
| 291 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 292 | private void beginDraggingApplication(View v) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 293 | // Make a copy of the ApplicationInfo |
| 294 | ApplicationInfo appInfo = new ApplicationInfo((ApplicationInfo) v.getTag()); |
| 295 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 296 | // Compose the drag image (top compound drawable, index is 1) |
| 297 | final TextView tv = (TextView) v; |
| 298 | final Drawable icon = tv.getCompoundDrawables()[1]; |
| 299 | Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), |
| 300 | Bitmap.Config.ARGB_8888); |
| 301 | Canvas c = new Canvas(b); |
| 302 | c.translate((v.getWidth() - icon.getIntrinsicWidth()) / 2, v.getPaddingTop()); |
| 303 | icon.draw(c); |
| 304 | |
| 305 | // Compose the visible rect of the drag image |
| 306 | Rect dragRect = null; |
| 307 | if (v instanceof TextView) { |
| 308 | int iconSize = getResources().getDimensionPixelSize(R.dimen.app_icon_size); |
| 309 | int top = v.getPaddingTop(); |
| 310 | int left = (b.getWidth() - iconSize) / 2; |
| 311 | int right = left + iconSize; |
| 312 | int bottom = top + iconSize; |
| 313 | dragRect = new Rect(left, top, right, bottom); |
| 314 | } |
| 315 | |
| 316 | // Start the drag |
| 317 | mLauncher.lockScreenOrientation(); |
| 318 | mLauncher.getWorkspace().onDragStartedWithItemSpans(1, 1, b); |
| 319 | mDragController.startDrag(v, b, this, appInfo, DragController.DRAG_ACTION_COPY, dragRect); |
| 320 | b.recycle(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 321 | } |
| 322 | private void beginDraggingWidget(View v) { |
| 323 | // Get the widget preview as the drag representation |
| 324 | ImageView image = (ImageView) v.findViewById(R.id.widget_preview); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 325 | PendingAddItemInfo createItemInfo = (PendingAddItemInfo) v.getTag(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 326 | |
| 327 | // Compose the drag image |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 328 | Bitmap b; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 329 | Drawable preview = image.getDrawable(); |
| 330 | int w = preview.getIntrinsicWidth(); |
| 331 | int h = preview.getIntrinsicHeight(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 332 | if (createItemInfo instanceof PendingAddWidgetInfo) { |
| 333 | PendingAddWidgetInfo createWidgetInfo = (PendingAddWidgetInfo) createItemInfo; |
| 334 | int[] spanXY = CellLayout.rectToCell(getResources(), |
| 335 | createWidgetInfo.minWidth, createWidgetInfo.minHeight, null); |
| 336 | createItemInfo.spanX = spanXY[0]; |
| 337 | createItemInfo.spanY = spanXY[1]; |
| 338 | |
| 339 | b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
| 340 | renderDrawableToBitmap(preview, b, 0, 0, w, h, 1, 1); |
| 341 | } else { |
| 342 | // Workaround for the fact that we don't keep the original ResolveInfo associated with |
| 343 | // the shortcut around. To get the icon, we just render the preview image (which has |
| 344 | // the shortcut icon) to a new drag bitmap that clips the non-icon space. |
| 345 | b = Bitmap.createBitmap(mWidgetPreviewIconPaddedDimension, |
| 346 | mWidgetPreviewIconPaddedDimension, Bitmap.Config.ARGB_8888); |
| 347 | Canvas c = new Canvas(b); |
| 348 | preview.draw(c); |
| 349 | createItemInfo.spanX = createItemInfo.spanY = 1; |
| 350 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 351 | |
| 352 | // Start the drag |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 353 | mLauncher.lockScreenOrientation(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 354 | mLauncher.getWorkspace().onDragStartedWithItemSpans(createItemInfo.spanX, |
| 355 | createItemInfo.spanY, b); |
| 356 | mDragController.startDrag(image, b, this, createItemInfo, |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 357 | DragController.DRAG_ACTION_COPY, null); |
| 358 | b.recycle(); |
| 359 | } |
| 360 | @Override |
| 361 | protected boolean beginDragging(View v) { |
| 362 | if (!super.beginDragging(v)) return false; |
| 363 | |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 364 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 365 | if (v instanceof PagedViewIcon) { |
| 366 | beginDraggingApplication(v); |
| 367 | } else if (v instanceof PagedViewWidget) { |
| 368 | beginDraggingWidget(v); |
| 369 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 370 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 371 | // Go into spring loaded mode |
| 372 | int currentPageIndex = mLauncher.getWorkspace().getCurrentPage(); |
| 373 | CellLayout currentPage = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPageIndex); |
| 374 | mLauncher.enterSpringLoadedDragMode(currentPage); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 375 | return true; |
| 376 | } |
| 377 | private void endDragging(boolean success) { |
| 378 | post(new Runnable() { |
| 379 | // Once the drag operation has fully completed, hence the post, we want to disable the |
| 380 | // deleteZone and the appInfoButton in all apps, and re-enable the instance which |
| 381 | // live in the workspace |
| 382 | public void run() { |
| 383 | // if onDestroy was called on Launcher, we might have already deleted the |
| 384 | // all apps delete zone / info button, so check if they are null |
| 385 | DeleteZone allAppsDeleteZone = |
| 386 | (DeleteZone) mLauncher.findViewById(R.id.all_apps_delete_zone); |
| 387 | ApplicationInfoDropTarget allAppsInfoButton = |
| 388 | (ApplicationInfoDropTarget) mLauncher.findViewById(R.id.all_apps_info_target); |
| 389 | |
| 390 | if (allAppsDeleteZone != null) allAppsDeleteZone.setDragAndDropEnabled(false); |
| 391 | if (allAppsInfoButton != null) allAppsInfoButton.setDragAndDropEnabled(false); |
| 392 | } |
| 393 | }); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 394 | mLauncher.exitSpringLoadedDragMode(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 395 | mLauncher.getWorkspace().onDragStopped(success); |
| 396 | mLauncher.unlockScreenOrientation(); |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 397 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* |
| 401 | * DragSource implementation |
| 402 | */ |
| 403 | @Override |
| 404 | public void onDragViewVisible() {} |
| 405 | @Override |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 406 | public void onDropCompleted(View target, DragObject d, boolean success) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 407 | endDragging(success); |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 408 | |
| 409 | // Display an error message if the drag failed due to there not being enough space on the |
| 410 | // target layout we were dropping on. |
| 411 | if (!success) { |
| 412 | boolean showOutOfSpaceMessage = false; |
| 413 | if (target instanceof Workspace) { |
| 414 | int currentScreen = mLauncher.getCurrentWorkspaceScreen(); |
| 415 | Workspace workspace = (Workspace) target; |
| 416 | CellLayout layout = (CellLayout) workspace.getChildAt(currentScreen); |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 417 | ItemInfo itemInfo = (ItemInfo) d.dragInfo; |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 418 | if (layout != null) { |
| 419 | layout.calculateSpans(itemInfo); |
| 420 | showOutOfSpaceMessage = |
| 421 | !layout.findCellForSpan(null, itemInfo.spanX, itemInfo.spanY); |
| 422 | } |
| 423 | } |
| 424 | // TODO-APPS_CUSTOMIZE: We need to handle this for folders as well later. |
| 425 | if (showOutOfSpaceMessage) { |
| 426 | mLauncher.showOutOfSpaceMessage(); |
| 427 | } |
| 428 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | public void setContentType(ContentType type) { |
| 432 | mContentType = type; |
| 433 | setCurrentPage(0); |
| 434 | invalidatePageData(); |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Apps PagedView implementation |
| 439 | */ |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 440 | private void setVisibilityOnChildren(ViewGroup layout, int visibility) { |
| 441 | int childCount = layout.getChildCount(); |
| 442 | for (int i = 0; i < childCount; ++i) { |
| 443 | layout.getChildAt(i).setVisibility(visibility); |
| 444 | } |
| 445 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 446 | private void setupPage(PagedViewCellLayout layout) { |
| 447 | layout.setCellCount(mCellCountX, mCellCountY); |
| 448 | layout.setGap(mPageLayoutWidthGap, mPageLayoutHeightGap); |
| 449 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 450 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
| 451 | |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 452 | // Note: We force a measure here to get around the fact that when we do layout calculations |
| 453 | // immediately after syncing, we don't have a proper width. That said, we already know the |
| 454 | // expected page width, so we can actually optimize by hiding all the TextView-based |
| 455 | // children that are expensive to measure, and let that happen naturally later. |
| 456 | setVisibilityOnChildren(layout, View.GONE); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 457 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
| 458 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 459 | layout.setMinimumWidth(getPageContentWidth()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 460 | layout.measure(widthSpec, heightSpec); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 461 | setVisibilityOnChildren(layout, View.VISIBLE); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 462 | } |
| 463 | public void syncAppsPages() { |
| 464 | // Ensure that we have the right number of pages |
| 465 | Context context = getContext(); |
| 466 | int numPages = (int) Math.ceil((float) mApps.size() / (mCellCountX * mCellCountY)); |
| 467 | for (int i = 0; i < numPages; ++i) { |
| 468 | PagedViewCellLayout layout = new PagedViewCellLayout(context); |
| 469 | setupPage(layout); |
| 470 | addView(layout); |
| 471 | } |
| 472 | } |
| 473 | public void syncAppsPageItems(int page) { |
| 474 | // ensure that we have the right number of items on the pages |
| 475 | int numPages = getPageCount(); |
| 476 | int numCells = mCellCountX * mCellCountY; |
| 477 | int startIndex = page * numCells; |
| 478 | int endIndex = Math.min(startIndex + numCells, mApps.size()); |
| 479 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page); |
| 480 | layout.removeAllViewsOnPage(); |
| 481 | for (int i = startIndex; i < endIndex; ++i) { |
| 482 | ApplicationInfo info = mApps.get(i); |
| 483 | PagedViewIcon icon = (PagedViewIcon) mLayoutInflater.inflate( |
| 484 | R.layout.apps_customize_application, layout, false); |
Michael Jurka | b9b8ce9 | 2011-05-05 15:05:07 -0700 | [diff] [blame] | 485 | icon.applyFromApplicationInfo( |
Winson Chung | 9f4e0fd | 2011-06-02 18:59:36 -0700 | [diff] [blame] | 486 | info, mPageViewIconCache, true, (numPages > 1)); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 487 | icon.setOnClickListener(this); |
| 488 | icon.setOnLongClickListener(this); |
| 489 | icon.setOnTouchListener(this); |
| 490 | |
| 491 | int index = i - startIndex; |
| 492 | int x = index % mCellCountX; |
| 493 | int y = index / mCellCountX; |
Winson Chung | 6a70e9f | 2011-05-17 16:24:49 -0700 | [diff] [blame] | 494 | layout.addViewToCellLayout(icon, -1, i, new PagedViewCellLayout.LayoutParams(x,y, 1,1)); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 495 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 496 | |
| 497 | // Create the hardware layers |
| 498 | layout.allowHardwareLayerCreation(); |
| 499 | layout.createHardwareLayers(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 500 | } |
| 501 | /* |
| 502 | * Widgets PagedView implementation |
| 503 | */ |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 504 | private void setupPage(PagedViewGridLayout layout) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 505 | layout.setPadding(mPageLayoutPaddingLeft, mPageLayoutPaddingTop, |
| 506 | mPageLayoutPaddingRight, mPageLayoutPaddingBottom); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 507 | |
| 508 | // Note: We force a measure here to get around the fact that when we do layout calculations |
| 509 | // immediately after syncing, we don't have a proper width. That said, we already know the |
| 510 | // expected page width, so we can actually optimize by hiding all the TextView-based |
| 511 | // children that are expensive to measure, and let that happen naturally later. |
| 512 | setVisibilityOnChildren(layout, View.GONE); |
| 513 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); |
| 514 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 515 | layout.setMinimumWidth(getPageContentWidth()); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 516 | layout.measure(widthSpec, heightSpec); |
| 517 | setVisibilityOnChildren(layout, View.VISIBLE); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 518 | } |
| 519 | private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, |
| 520 | float scaleX, float scaleY) { |
| 521 | Canvas c = new Canvas(); |
| 522 | if (bitmap != null) c.setBitmap(bitmap); |
| 523 | c.save(); |
| 524 | c.scale(scaleX, scaleY); |
| 525 | Rect oldBounds = d.copyBounds(); |
| 526 | d.setBounds(x, y, x + w, y + h); |
| 527 | d.draw(c); |
| 528 | d.setBounds(oldBounds); // Restore the bounds |
| 529 | c.restore(); |
| 530 | } |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 531 | private FastBitmapDrawable getShortcutPreview(ResolveInfo info, int cellWidth, int cellHeight) { |
| 532 | // Return the cached version if necessary |
| 533 | Bitmap cachedBitmap = mWidgetPreviewCache.get(info); |
| 534 | if (cachedBitmap != null) { |
| 535 | return new FastBitmapDrawable(cachedBitmap); |
| 536 | } |
| 537 | |
| 538 | Resources resources = mLauncher.getResources(); |
| 539 | int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size); |
| 540 | // We only need to make it wide enough so as not allow the preview to be scaled |
| 541 | int expectedWidth = cellWidth; |
| 542 | int expectedHeight = mWidgetPreviewIconPaddedDimension; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 543 | |
| 544 | // Render the icon |
| 545 | Bitmap preview = Bitmap.createBitmap(expectedWidth, expectedHeight, Config.ARGB_8888); |
Winson Chung | 4dbea79 | 2011-05-05 14:21:32 -0700 | [diff] [blame] | 546 | Drawable icon = mIconCache.getFullResIcon(info, mPackageManager); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 547 | renderDrawableToBitmap(icon, preview, 0, 0, iconSize, iconSize, 1f, 1f); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 548 | FastBitmapDrawable iconDrawable = new FastBitmapDrawable(preview); |
| 549 | iconDrawable.setBounds(0, 0, expectedWidth, expectedHeight); |
| 550 | mWidgetPreviewCache.put(info, preview); |
| 551 | return iconDrawable; |
| 552 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 553 | private FastBitmapDrawable getWidgetPreview(AppWidgetProviderInfo info, int cellHSpan, |
| 554 | int cellVSpan, int cellWidth, int cellHeight) { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 555 | // Return the cached version if necessary |
| 556 | Bitmap cachedBitmap = mWidgetPreviewCache.get(info); |
| 557 | if (cachedBitmap != null) { |
| 558 | return new FastBitmapDrawable(cachedBitmap); |
| 559 | } |
| 560 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 561 | // Calculate the size of the drawable |
| 562 | cellHSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellHSpan)); |
| 563 | cellVSpan = Math.max(mMinWidgetSpan, Math.min(mMaxWidgetSpan, cellVSpan)); |
| 564 | int expectedWidth = mWidgetSpacingLayout.estimateCellWidth(cellHSpan); |
| 565 | int expectedHeight = mWidgetSpacingLayout.estimateCellHeight(cellVSpan); |
| 566 | |
| 567 | // Scale down the bitmap to fit the space |
| 568 | float widgetPreviewScale = (float) cellWidth / expectedWidth; |
| 569 | expectedWidth = (int) (widgetPreviewScale * expectedWidth); |
| 570 | expectedHeight = (int) (widgetPreviewScale * expectedHeight); |
| 571 | |
| 572 | // Load the preview image if possible |
| 573 | String packageName = info.provider.getPackageName(); |
| 574 | Drawable drawable = null; |
| 575 | FastBitmapDrawable newDrawable = null; |
| 576 | if (info.previewImage != 0) { |
| 577 | drawable = mPackageManager.getDrawable(packageName, info.previewImage, null); |
| 578 | if (drawable == null) { |
| 579 | Log.w(LOG_TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon) |
| 580 | + " for provider: " + info.provider); |
| 581 | } else { |
| 582 | // Scale down the preview to the dimensions we want |
| 583 | int imageWidth = drawable.getIntrinsicWidth(); |
| 584 | int imageHeight = drawable.getIntrinsicHeight(); |
| 585 | float aspect = (float) imageWidth / imageHeight; |
| 586 | int newWidth = imageWidth; |
| 587 | int newHeight = imageHeight; |
| 588 | if (aspect > 1f) { |
| 589 | newWidth = expectedWidth; |
| 590 | newHeight = (int) (imageHeight * ((float) expectedWidth / imageWidth)); |
| 591 | } else { |
| 592 | newHeight = expectedHeight; |
| 593 | newWidth = (int) (imageWidth * ((float) expectedHeight / imageHeight)); |
| 594 | } |
| 595 | |
| 596 | Bitmap preview = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888); |
| 597 | renderDrawableToBitmap(drawable, preview, 0, 0, newWidth, newHeight, 1f, 1f); |
| 598 | newDrawable = new FastBitmapDrawable(preview); |
| 599 | newDrawable.setBounds(0, 0, newWidth, newHeight); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 600 | mWidgetPreviewCache.put(info, preview); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | // Generate a preview image if we couldn't load one |
| 605 | if (drawable == null) { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 606 | Resources resources = mLauncher.getResources(); |
| 607 | int iconSize = resources.getDimensionPixelSize(R.dimen.app_icon_size); |
| 608 | |
| 609 | // Specify the dimensions of the bitmap |
| 610 | if (info.minWidth >= info.minHeight) { |
| 611 | expectedWidth = cellWidth; |
| 612 | expectedHeight = mWidgetPreviewIconPaddedDimension; |
| 613 | } else { |
| 614 | // Note that in vertical widgets, we might not have enough space due to the text |
| 615 | // label, so be conservative and use the width as a height bound |
| 616 | expectedWidth = mWidgetPreviewIconPaddedDimension; |
| 617 | expectedHeight = cellWidth; |
| 618 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 619 | |
| 620 | Bitmap preview = Bitmap.createBitmap(expectedWidth, expectedHeight, Config.ARGB_8888); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 621 | renderDrawableToBitmap(mDefaultWidgetBackground, preview, 0, 0, expectedWidth, |
| 622 | expectedHeight, 1f,1f); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 623 | |
| 624 | // Draw the icon in the top left corner |
| 625 | try { |
| 626 | Drawable icon = null; |
| 627 | if (info.icon > 0) icon = mPackageManager.getDrawable(packageName, info.icon, null); |
| 628 | if (icon == null) icon = resources.getDrawable(R.drawable.ic_launcher_application); |
| 629 | |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 630 | int offset = (int) (iconSize * sWidgetPreviewIconPaddingPercentage); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 631 | renderDrawableToBitmap(icon, preview, offset, offset, iconSize, iconSize, 1f, 1f); |
| 632 | } catch (Resources.NotFoundException e) {} |
| 633 | |
| 634 | newDrawable = new FastBitmapDrawable(preview); |
| 635 | newDrawable.setBounds(0, 0, expectedWidth, expectedHeight); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 636 | mWidgetPreviewCache.put(info, preview); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 637 | } |
| 638 | return newDrawable; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 639 | } |
| 640 | public void syncWidgetPages() { |
| 641 | // Ensure that we have the right number of pages |
| 642 | Context context = getContext(); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 643 | int numWidgetsPerPage = mWidgetCountX * mWidgetCountY; |
| 644 | int numPages = (int) Math.ceil(mWidgets.size() / (float) numWidgetsPerPage); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 645 | for (int i = 0; i < numPages; ++i) { |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 646 | PagedViewGridLayout layout = new PagedViewGridLayout(context, mWidgetCountX, |
| 647 | mWidgetCountY); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 648 | setupPage(layout); |
| 649 | addView(layout); |
| 650 | } |
| 651 | } |
| 652 | public void syncWidgetPageItems(int page) { |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 653 | PagedViewGridLayout layout = (PagedViewGridLayout) getChildAt(page); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 654 | layout.removeAllViews(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 655 | |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 656 | // Calculate the dimensions of each cell we are giving to each widget |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 657 | int numWidgetsPerPage = mWidgetCountX * mWidgetCountY; |
Winson Chung | 9f4e0fd | 2011-06-02 18:59:36 -0700 | [diff] [blame] | 658 | int numPages = (int) Math.ceil(mWidgets.size() / (float) numWidgetsPerPage); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 659 | int offset = page * numWidgetsPerPage; |
| 660 | int cellWidth = ((mWidgetSpacingLayout.getContentWidth() - mPageLayoutWidthGap |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 661 | - ((mWidgetCountX - 1) * mWidgetWidthGap)) / mWidgetCountX); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 662 | int cellHeight = ((mWidgetSpacingLayout.getContentHeight() - mPageLayoutHeightGap |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 663 | - ((mWidgetCountY - 1) * mWidgetHeightGap)) / mWidgetCountY); |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 664 | for (int i = 0; i < Math.min(numWidgetsPerPage, mWidgets.size() - offset); ++i) { |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 665 | Object rawInfo = mWidgets.get(offset + i); |
| 666 | PendingAddItemInfo createItemInfo = null; |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 667 | PagedViewWidget widget = (PagedViewWidget) mLayoutInflater.inflate( |
| 668 | R.layout.apps_customize_widget, layout, false); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 669 | if (rawInfo instanceof AppWidgetProviderInfo) { |
| 670 | // Fill in the widget information |
| 671 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) rawInfo; |
| 672 | createItemInfo = new PendingAddWidgetInfo(info, null, null); |
| 673 | final int[] cellSpans = CellLayout.rectToCell(getResources(), info.minWidth, |
| 674 | info.minHeight, null); |
| 675 | FastBitmapDrawable preview = getWidgetPreview(info, cellSpans[0], cellSpans[1], |
| 676 | cellWidth, cellHeight); |
Winson Chung | 9f4e0fd | 2011-06-02 18:59:36 -0700 | [diff] [blame] | 677 | widget.applyFromAppWidgetProviderInfo(info, preview, -1, cellSpans, |
| 678 | mPageViewIconCache, (numPages > 1)); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 679 | widget.setTag(createItemInfo); |
| 680 | } else if (rawInfo instanceof ResolveInfo) { |
| 681 | // Fill in the shortcuts information |
| 682 | ResolveInfo info = (ResolveInfo) rawInfo; |
| 683 | createItemInfo = new PendingAddItemInfo(); |
| 684 | createItemInfo.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
| 685 | createItemInfo.componentName = new ComponentName(info.activityInfo.packageName, |
| 686 | info.activityInfo.name); |
| 687 | FastBitmapDrawable preview = getShortcutPreview(info, cellWidth, cellHeight); |
Winson Chung | 9f4e0fd | 2011-06-02 18:59:36 -0700 | [diff] [blame] | 688 | widget.applyFromResolveInfo(mPackageManager, info, preview, mPageViewIconCache, |
| 689 | (numPages > 1)); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 690 | widget.setTag(createItemInfo); |
| 691 | } |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 692 | widget.setOnClickListener(this); |
| 693 | widget.setOnLongClickListener(this); |
| 694 | widget.setOnTouchListener(this); |
| 695 | |
| 696 | // Layout each widget |
Winson Chung | 4b576be | 2011-04-27 17:40:20 -0700 | [diff] [blame] | 697 | int ix = i % mWidgetCountX; |
| 698 | int iy = i / mWidgetCountX; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 699 | PagedViewGridLayout.LayoutParams lp = new PagedViewGridLayout.LayoutParams(cellWidth, |
| 700 | cellHeight); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 701 | lp.leftMargin = (ix * cellWidth) + (ix * mWidgetWidthGap); |
| 702 | lp.topMargin = (iy * cellHeight) + (iy * mWidgetHeightGap); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 703 | layout.addView(widget, lp); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 704 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 705 | } |
Winson Chung | 46af2e8 | 2011-05-09 16:00:53 -0700 | [diff] [blame] | 706 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 707 | @Override |
| 708 | public void syncPages() { |
| 709 | removeAllViews(); |
| 710 | switch (mContentType) { |
| 711 | case Applications: |
| 712 | syncAppsPages(); |
| 713 | break; |
| 714 | case Widgets: |
| 715 | syncWidgetPages(); |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | @Override |
| 720 | public void syncPageItems(int page) { |
| 721 | switch (mContentType) { |
| 722 | case Applications: |
| 723 | syncAppsPageItems(page); |
| 724 | break; |
| 725 | case Widgets: |
| 726 | syncWidgetPageItems(page); |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * Used by the parent to get the content width to set the tab bar to |
| 733 | * @return |
| 734 | */ |
| 735 | public int getPageContentWidth() { |
| 736 | return mContentWidth; |
| 737 | } |
| 738 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 739 | @Override |
| 740 | protected void onPageBeginMoving() { |
| 741 | /* TO BE ENABLED LATER |
| 742 | setChildrenDrawnWithCacheEnabled(true); |
| 743 | for (int i = 0; i < getChildCount(); ++i) { |
| 744 | View v = getChildAt(i); |
| 745 | if (v instanceof PagedViewCellLayout) { |
| 746 | ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(true); |
| 747 | } |
| 748 | } |
| 749 | */ |
| 750 | super.onPageBeginMoving(); |
| 751 | } |
| 752 | |
| 753 | @Override |
| 754 | protected void onPageEndMoving() { |
| 755 | /* TO BE ENABLED LATER |
| 756 | for (int i = 0; i < getChildCount(); ++i) { |
| 757 | View v = getChildAt(i); |
| 758 | if (v instanceof PagedViewCellLayout) { |
| 759 | ((PagedViewCellLayout) v).setChildrenDrawingCacheEnabled(false); |
| 760 | } |
| 761 | } |
| 762 | setChildrenDrawnWithCacheEnabled(false); |
| 763 | */ |
| 764 | super.onPageEndMoving(); |
| 765 | } |
| 766 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 767 | /* |
| 768 | * AllAppsView implementation |
| 769 | */ |
| 770 | @Override |
| 771 | public void setup(Launcher launcher, DragController dragController) { |
| 772 | mLauncher = launcher; |
| 773 | mDragController = dragController; |
| 774 | } |
| 775 | @Override |
| 776 | public void zoom(float zoom, boolean animate) { |
| 777 | // TODO-APPS_CUSTOMIZE: Call back to mLauncher.zoomed() |
| 778 | } |
| 779 | @Override |
| 780 | public boolean isVisible() { |
| 781 | return (getVisibility() == VISIBLE); |
| 782 | } |
| 783 | @Override |
| 784 | public boolean isAnimating() { |
| 785 | return false; |
| 786 | } |
| 787 | @Override |
| 788 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 789 | mApps = list; |
| 790 | Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 791 | |
| 792 | // The next layout pass will trigger data-ready if both widgets and apps are set, so request |
| 793 | // a layout to do this test and invalidate the page data when ready. |
| 794 | if (testDataReady()) requestLayout(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 795 | } |
| 796 | private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 797 | // We add it in place, in alphabetical order |
| 798 | int count = list.size(); |
| 799 | for (int i = 0; i < count; ++i) { |
| 800 | ApplicationInfo info = list.get(i); |
| 801 | int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR); |
| 802 | if (index < 0) { |
| 803 | mApps.add(-(index + 1), info); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | @Override |
| 808 | public void addApps(ArrayList<ApplicationInfo> list) { |
| 809 | addAppsWithoutInvalidate(list); |
| 810 | invalidatePageData(); |
| 811 | } |
| 812 | private int findAppByComponent(List<ApplicationInfo> list, ApplicationInfo item) { |
| 813 | ComponentName removeComponent = item.intent.getComponent(); |
| 814 | int length = list.size(); |
| 815 | for (int i = 0; i < length; ++i) { |
| 816 | ApplicationInfo info = list.get(i); |
| 817 | if (info.intent.getComponent().equals(removeComponent)) { |
| 818 | return i; |
| 819 | } |
| 820 | } |
| 821 | return -1; |
| 822 | } |
| 823 | private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) { |
| 824 | // loop through all the apps and remove apps that have the same component |
| 825 | int length = list.size(); |
| 826 | for (int i = 0; i < length; ++i) { |
| 827 | ApplicationInfo info = list.get(i); |
| 828 | int removeIndex = findAppByComponent(mApps, info); |
| 829 | if (removeIndex > -1) { |
| 830 | mApps.remove(removeIndex); |
| 831 | mPageViewIconCache.removeOutline(new PagedViewIconCache.Key(info)); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | @Override |
| 836 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 837 | removeAppsWithoutInvalidate(list); |
| 838 | invalidatePageData(); |
| 839 | } |
| 840 | @Override |
| 841 | public void updateApps(ArrayList<ApplicationInfo> list) { |
| 842 | // We remove and re-add the updated applications list because it's properties may have |
| 843 | // changed (ie. the title), and this will ensure that the items will be in their proper |
| 844 | // place in the list. |
| 845 | removeAppsWithoutInvalidate(list); |
| 846 | addAppsWithoutInvalidate(list); |
| 847 | invalidatePageData(); |
| 848 | } |
| 849 | @Override |
| 850 | public void reset() { |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 851 | if (mContentType != ContentType.Applications) { |
| 852 | // Reset to the first page of the Apps pane |
| 853 | AppsCustomizeTabHost tabs = (AppsCustomizeTabHost) |
| 854 | mLauncher.findViewById(R.id.apps_customize_pane); |
| 855 | tabs.setCurrentTabByTag(tabs.getTabTagForContentType(ContentType.Applications)); |
| 856 | } else { |
| 857 | setCurrentPage(0); |
| 858 | invalidatePageData(); |
| 859 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 860 | } |
| 861 | @Override |
| 862 | public void dumpState() { |
| 863 | // TODO: Dump information related to current list of Applications, Widgets, etc. |
| 864 | ApplicationInfo.dumpApplicationInfoList(LOG_TAG, "mApps", mApps); |
| 865 | dumpAppWidgetProviderInfoList(LOG_TAG, "mWidgets", mWidgets); |
| 866 | } |
| 867 | private void dumpAppWidgetProviderInfoList(String tag, String label, |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 868 | List<Object> list) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 869 | Log.d(tag, label + " size=" + list.size()); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 870 | for (Object i: list) { |
| 871 | if (i instanceof AppWidgetProviderInfo) { |
| 872 | AppWidgetProviderInfo info = (AppWidgetProviderInfo) i; |
| 873 | Log.d(tag, " label=\"" + info.label + "\" previewImage=" + info.previewImage |
| 874 | + " resizeMode=" + info.resizeMode + " configure=" + info.configure |
| 875 | + " initialLayout=" + info.initialLayout |
| 876 | + " minWidth=" + info.minWidth + " minHeight=" + info.minHeight); |
| 877 | } else if (i instanceof ResolveInfo) { |
| 878 | ResolveInfo info = (ResolveInfo) i; |
| 879 | Log.d(tag, " label=\"" + info.loadLabel(mPackageManager) + "\" icon=" |
| 880 | + info.icon); |
| 881 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | @Override |
| 885 | public void surrender() { |
| 886 | // TODO: If we are in the middle of any process (ie. for holographic outlines, etc) we |
| 887 | // should stop this now. |
| 888 | } |
| 889 | } |