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