The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | |
| 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 23 | import android.animation.ObjectAnimator; |
| 24 | import android.animation.PropertyValuesHolder; |
| 25 | import android.animation.ValueAnimator; |
| 26 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 28 | import android.graphics.Matrix; |
Romain Guy | fb5411e | 2010-02-24 10:04:17 -0800 | [diff] [blame] | 29 | import android.graphics.Rect; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 31 | import android.view.LayoutInflater; |
Adam Cohen | 0c872ba | 2011-05-05 10:34:16 -0700 | [diff] [blame] | 32 | import android.view.MotionEvent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | import android.view.View; |
| 34 | import android.view.View.OnClickListener; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 35 | import android.view.animation.AccelerateInterpolator; |
| 36 | import android.view.animation.DecelerateInterpolator; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | import android.widget.AdapterView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 38 | import android.widget.LinearLayout; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 39 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 40 | import android.widget.AdapterView.OnItemClickListener; |
| 41 | import android.widget.AdapterView.OnItemLongClickListener; |
| 42 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 43 | import com.android.launcher.R; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 44 | import com.android.launcher2.FolderInfo.FolderListener; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 45 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Represents a set of icons chosen by the user or generated by the system. |
| 48 | */ |
| 49 | public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener, |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 50 | OnItemClickListener, OnClickListener, View.OnLongClickListener, DropTarget, FolderListener { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 51 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 52 | protected DragController mDragController; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 53 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 54 | protected Launcher mLauncher; |
| 55 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 56 | protected FolderInfo mInfo; |
| 57 | |
| 58 | /** |
| 59 | * Which item is being dragged |
| 60 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 61 | protected ShortcutInfo mDragItem; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 62 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 63 | private static final String TAG = "Launcher.Folder"; |
| 64 | |
| 65 | static final int STATE_NONE = -1; |
| 66 | static final int STATE_SMALL = 0; |
| 67 | static final int STATE_ANIMATING = 1; |
| 68 | static final int STATE_OPEN = 2; |
| 69 | |
| 70 | private int mExpandDuration; |
| 71 | protected CellLayout mContent; |
| 72 | private final LayoutInflater mInflater; |
| 73 | private final IconCache mIconCache; |
| 74 | private int mState = STATE_NONE; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 75 | private int[] mDragItemPosition = new int[2]; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 76 | private static final int FULL_GROW = 0; |
| 77 | private static final int PARTIAL_GROW = 1; |
| 78 | private int mMode = PARTIAL_GROW; |
| 79 | private boolean mRearrangeOnClose = false; |
| 80 | private FolderIcon mFolderIcon; |
| 81 | private int mMaxCountX; |
| 82 | private int mMaxCountY; |
| 83 | private Rect mNewSize = new Rect(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 84 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Used to inflate the Workspace from XML. |
| 87 | * |
| 88 | * @param context The application's context. |
| 89 | * @param attrs The attribtues set containing the Workspace's customization values. |
| 90 | */ |
| 91 | public Folder(Context context, AttributeSet attrs) { |
| 92 | super(context, attrs); |
| 93 | setAlwaysDrawnWithCacheEnabled(false); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 94 | mInflater = LayoutInflater.from(context); |
| 95 | mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache(); |
| 96 | mExpandDuration = getResources().getInteger(R.integer.config_folderAnimDuration); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 97 | |
| 98 | mMaxCountX = LauncherModel.getCellCountX() - 1; |
| 99 | mMaxCountY = LauncherModel.getCellCountY() - 1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | protected void onFinishInflate() { |
| 104 | super.onFinishInflate(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 105 | mContent = (CellLayout) findViewById(R.id.folder_content); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 106 | mContent.setGridSize(0, 0); |
| 107 | mContent.enableHardwareLayers(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 109 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 110 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 111 | ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position); |
Romain Guy | fb5411e | 2010-02-24 10:04:17 -0800 | [diff] [blame] | 112 | int[] pos = new int[2]; |
| 113 | v.getLocationOnScreen(pos); |
| 114 | app.intent.setSourceBounds(new Rect(pos[0], pos[1], |
| 115 | pos[0] + v.getWidth(), pos[1] + v.getHeight())); |
Joe Onorato | f984e85 | 2010-03-25 09:47:45 -0700 | [diff] [blame] | 116 | mLauncher.startActivitySafely(app.intent, app); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public void onClick(View v) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 120 | Object tag = v.getTag(); |
| 121 | if (tag instanceof ShortcutInfo) { |
| 122 | // refactor this code from Folder |
| 123 | ShortcutInfo item = (ShortcutInfo) tag; |
| 124 | int[] pos = new int[2]; |
| 125 | v.getLocationOnScreen(pos); |
| 126 | item.intent.setSourceBounds(new Rect(pos[0], pos[1], |
| 127 | pos[0] + v.getWidth(), pos[1] + v.getHeight())); |
| 128 | mLauncher.startActivitySafely(item.intent, item); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 129 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | public boolean onLongClick(View v) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 133 | Object tag = v.getTag(); |
| 134 | if (tag instanceof ShortcutInfo) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 135 | mLauncher.closeFolder(this); |
| 136 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 137 | ShortcutInfo item = (ShortcutInfo) tag; |
| 138 | if (!v.isInTouchMode()) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | mLauncher.getWorkspace().onDragStartedWithItem(v); |
| 143 | mDragController.startDrag(v, this, item, DragController.DRAG_ACTION_COPY); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 144 | mDragItemPosition[0] = item.cellX; |
| 145 | mDragItemPosition[1] = item.cellY; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 146 | mInfo.remove(item); |
| 147 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 148 | mDragItem = item; |
| 149 | } else { |
| 150 | mLauncher.closeFolder(this); |
| 151 | mLauncher.showRenameDialog(mInfo); |
| 152 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
Adam Cohen | 0c872ba | 2011-05-05 10:34:16 -0700 | [diff] [blame] | 156 | /** |
| 157 | * We need to handle touch events to prevent them from falling through to the workspace below. |
| 158 | */ |
| 159 | @Override |
| 160 | public boolean onTouchEvent(MotionEvent ev) { |
| 161 | return true; |
| 162 | } |
| 163 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 164 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
| 165 | if (!view.isInTouchMode()) { |
| 166 | return false; |
| 167 | } |
| 168 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 169 | ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 170 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 171 | mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 172 | mLauncher.closeFolder(this); |
| 173 | mDragItem = app; |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 178 | public void setDragController(DragController dragController) { |
| 179 | mDragController = dragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 182 | public void onDragViewVisible() { |
| 183 | } |
| 184 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 185 | void setLauncher(Launcher launcher) { |
| 186 | mLauncher = launcher; |
| 187 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 188 | |
| 189 | void setFolderIcon(FolderIcon icon) { |
| 190 | mFolderIcon = icon; |
| 191 | } |
| 192 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 193 | /** |
| 194 | * @return the FolderInfo object associated with this folder |
| 195 | */ |
| 196 | FolderInfo getInfo() { |
| 197 | return mInfo; |
| 198 | } |
| 199 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 200 | void onOpen() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 201 | // When the folder opens, we need to refresh the GridView's selection by |
| 202 | // forcing a layout |
| 203 | // TODO: find out if this is still necessary |
| 204 | mContent.requestLayout(); |
| 205 | requestFocus(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void onClose() { |
| 209 | final Workspace workspace = mLauncher.getWorkspace(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 210 | workspace.getChildAt(workspace.getCurrentPage()).requestFocus(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void bind(FolderInfo info) { |
| 214 | mInfo = info; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 215 | ArrayList<ShortcutInfo> children = info.contents; |
| 216 | for (int i = 0; i < children.size(); i++) { |
| 217 | ShortcutInfo child = (ShortcutInfo) children.get(i); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 218 | onAdd(child); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 219 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 220 | mInfo.addListener(this); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Creates a new UserFolder, inflated from R.layout.user_folder. |
| 225 | * |
| 226 | * @param context The application's context. |
| 227 | * |
| 228 | * @return A new UserFolder. |
| 229 | */ |
| 230 | static Folder fromXml(Context context) { |
| 231 | return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * This method is intended to make the UserFolder to be visually identical in size and position |
| 236 | * to its associated FolderIcon. This allows for a seamless transition into the expanded state. |
| 237 | */ |
| 238 | private void positionAndSizeAsIcon() { |
| 239 | if (!(getParent() instanceof CellLayoutChildren)) return; |
| 240 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 241 | CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 242 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 243 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 244 | if (mMode == PARTIAL_GROW) { |
| 245 | setScaleX(0.8f); |
| 246 | setScaleY(0.8f); |
| 247 | setAlpha(0f); |
| 248 | } else { |
| 249 | lp.width = iconLp.width; |
| 250 | lp.height = iconLp.height; |
| 251 | lp.x = iconLp.x; |
| 252 | lp.y = iconLp.y; |
| 253 | mContent.setAlpha(0); |
| 254 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 255 | mState = STATE_SMALL; |
| 256 | } |
| 257 | |
| 258 | public void animateOpen() { |
| 259 | if (mState != STATE_SMALL) { |
| 260 | positionAndSizeAsIcon(); |
| 261 | } |
| 262 | if (!(getParent() instanceof CellLayoutChildren)) return; |
| 263 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 264 | ObjectAnimator oa; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 265 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 266 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 267 | centerAboutIcon(); |
| 268 | if (mMode == PARTIAL_GROW) { |
| 269 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1); |
| 270 | PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f); |
| 271 | PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f); |
| 272 | oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY); |
| 273 | } else { |
| 274 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width()); |
| 275 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height()); |
| 276 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left); |
| 277 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top); |
| 278 | oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y); |
| 279 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 280 | public void onAnimationUpdate(ValueAnimator animation) { |
| 281 | requestLayout(); |
| 282 | } |
| 283 | }); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 284 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 285 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f); |
| 286 | ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha); |
| 287 | alphaOa.setDuration(mExpandDuration); |
| 288 | alphaOa.setInterpolator(new AccelerateInterpolator(2.0f)); |
| 289 | alphaOa.start(); |
| 290 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 291 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 292 | oa.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 293 | @Override |
| 294 | public void onAnimationStart(Animator animation) { |
| 295 | mState = STATE_ANIMATING; |
| 296 | } |
| 297 | @Override |
| 298 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 299 | mState = STATE_OPEN; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 300 | } |
| 301 | }); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 302 | oa.setDuration(mExpandDuration); |
| 303 | oa.start(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | public void animateClosed() { |
| 307 | if (!(getParent() instanceof CellLayoutChildren)) return; |
| 308 | |
| 309 | CellLayoutChildren clc = (CellLayoutChildren) getParent(); |
| 310 | final CellLayout cellLayout = (CellLayout) clc.getParent(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 311 | ObjectAnimator oa; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 312 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 313 | if (mMode == PARTIAL_GROW) { |
| 314 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0); |
| 315 | PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f); |
| 316 | PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f); |
| 317 | oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY); |
| 318 | } else { |
| 319 | CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams(); |
| 320 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 321 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 322 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", iconLp.width); |
| 323 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", iconLp.height); |
| 324 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x",iconLp.x); |
| 325 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", iconLp.y); |
| 326 | oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y); |
| 327 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 328 | public void onAnimationUpdate(ValueAnimator animation) { |
| 329 | requestLayout(); |
| 330 | } |
| 331 | }); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 332 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 333 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f); |
| 334 | ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha); |
| 335 | alphaOa.setDuration(mExpandDuration); |
| 336 | alphaOa.setInterpolator(new DecelerateInterpolator(2.0f)); |
| 337 | alphaOa.start(); |
| 338 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 339 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 340 | oa.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 341 | @Override |
| 342 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 343 | onCloseComplete(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 344 | cellLayout.removeViewWithoutMarkingCells(Folder.this); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 345 | mState = STATE_SMALL; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 346 | } |
| 347 | @Override |
| 348 | public void onAnimationStart(Animator animation) { |
| 349 | mState = STATE_ANIMATING; |
| 350 | } |
| 351 | }); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 352 | oa.setDuration(mExpandDuration); |
| 353 | oa.start(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void notifyDataSetChanged() { |
| 357 | // recreate all the children if the data set changes under us. We may want to do this more |
| 358 | // intelligently (ie just removing the views that should no longer exist) |
| 359 | mContent.removeAllViewsInLayout(); |
| 360 | bind(mInfo); |
| 361 | } |
| 362 | |
| 363 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 364 | DragView dragView, Object dragInfo) { |
| 365 | final ItemInfo item = (ItemInfo) dragInfo; |
| 366 | final int itemType = item.itemType; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 367 | return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 368 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && |
| 369 | !isFull()); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 373 | DragView dragView, Object dragInfo) { |
| 374 | ShortcutInfo item; |
| 375 | if (dragInfo instanceof ApplicationInfo) { |
| 376 | // Came from all apps -- make a copy |
| 377 | item = ((ApplicationInfo)dragInfo).makeShortcut(); |
| 378 | item.spanX = 1; |
| 379 | item.spanY = 1; |
| 380 | } else { |
| 381 | item = (ShortcutInfo)dragInfo; |
| 382 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 383 | mInfo.add(item); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 384 | LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); |
| 385 | } |
| 386 | |
| 387 | protected boolean findAndSetEmptyCells(ShortcutInfo item) { |
| 388 | int[] emptyCell = new int[2]; |
| 389 | if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) { |
| 390 | item.cellX = emptyCell[0]; |
| 391 | item.cellY = emptyCell[1]; |
| 392 | LauncherModel.addOrMoveItemInDatabase( |
| 393 | mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); |
| 394 | return true; |
| 395 | } else { |
| 396 | return false; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | protected void createAndAddShortcut(ShortcutInfo item) { |
| 401 | final TextView textView = |
| 402 | (TextView) mInflater.inflate(R.layout.application_boxed, this, false); |
| 403 | textView.setCompoundDrawablesWithIntrinsicBounds(null, |
| 404 | new FastBitmapDrawable(item.getIcon(mIconCache)), null, null); |
| 405 | textView.setText(item.title); |
| 406 | textView.setTag(item); |
| 407 | |
| 408 | textView.setOnClickListener(this); |
| 409 | textView.setOnLongClickListener(this); |
| 410 | |
| 411 | CellLayout.LayoutParams lp = |
| 412 | new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY); |
| 413 | boolean insert = false; |
| 414 | mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true); |
| 415 | } |
| 416 | |
| 417 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 418 | DragView dragView, Object dragInfo) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 419 | mContent.onDragEnter(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 423 | DragView dragView, Object dragInfo) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 424 | float[] r = mapPointFromScreenToContent(x, y, null); |
| 425 | mContent.visualizeDropLocation(null, null, (int) r[0], (int) r[1], 1, 1); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 429 | DragView dragView, Object dragInfo) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 430 | mContent.onDragExit(); |
| 431 | } |
| 432 | |
| 433 | public float[] mapPointFromScreenToContent(int x, int y, float[] r) { |
| 434 | if (r == null) { |
| 435 | r = new float[2]; |
| 436 | } |
| 437 | |
| 438 | int[] screenLocation = new int[2]; |
| 439 | mContent.getLocationOnScreen(screenLocation); |
| 440 | |
| 441 | r[0] = x - screenLocation[0]; |
| 442 | r[1] = y - screenLocation[1]; |
| 443 | return r; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | public void onDropCompleted(View target, Object dragInfo, boolean success) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | public boolean isDropEnabled() { |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset, |
| 454 | DragView dragView, Object dragInfo) { |
| 455 | return null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 456 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 457 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 458 | private void setupContentDimension(int count) { |
| 459 | ArrayList<View> list = getItemsInReadingOrder(); |
| 460 | |
| 461 | int countX = mContent.getCountX(); |
| 462 | int countY = mContent.getCountY(); |
| 463 | if (countX * countY < count) { |
| 464 | // Current grid is too small, expand it |
| 465 | if (countX <= countY && countX < mMaxCountX) { |
| 466 | countX++; |
| 467 | } else if (countY < mMaxCountY) { |
| 468 | countY++; |
| 469 | } |
| 470 | if (countY == 0) countY++; |
| 471 | |
| 472 | mContent.setGridSize(countX, countY); |
| 473 | } else if ((countX - 1) * countY >= count || (countY - 1) * countX >= count) { |
| 474 | // Current grid is too big, shrink it |
| 475 | if (countX <= countY) { |
| 476 | countY--; |
| 477 | } else { |
| 478 | countX--; |
| 479 | } |
| 480 | mContent.setGridSize(countX, countY); |
| 481 | } |
| 482 | arrangeChildren(list); |
| 483 | } |
| 484 | |
| 485 | public boolean isFull() { |
| 486 | return getItemCount() >= mMaxCountX * mMaxCountY; |
| 487 | } |
| 488 | |
| 489 | private void centerAboutIcon() { |
| 490 | CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams(); |
| 491 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 492 | |
| 493 | int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth(); |
| 494 | int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight(); |
| 495 | |
| 496 | int centerX = iconLp.x + iconLp.width / 2; |
| 497 | int centerY = iconLp.y + iconLp.height / 2; |
| 498 | int centeredLeft = centerX - width / 2; |
| 499 | int centeredTop = centerY - height / 2; |
| 500 | |
| 501 | CellLayoutChildren clc = (CellLayoutChildren) getParent(); |
| 502 | int parentWidth = 0; |
| 503 | int parentHeight = 0; |
| 504 | if (clc != null) { |
| 505 | parentWidth = clc.getMeasuredWidth(); |
| 506 | parentHeight = clc.getMeasuredHeight(); |
| 507 | } |
| 508 | |
| 509 | int left = Math.min(Math.max(0, centeredLeft), parentWidth - width); |
| 510 | int top = Math.min(Math.max(0, centeredTop), parentHeight - height); |
| 511 | |
| 512 | int folderPivotX = width / 2 + (centeredLeft - left); |
| 513 | int folderPivotY = height / 2 + (centeredTop - top); |
| 514 | setPivotX(folderPivotX); |
| 515 | setPivotY(folderPivotY); |
| 516 | int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() * |
| 517 | (1.0f * folderPivotX / width)); |
| 518 | int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() * |
| 519 | (1.0f * folderPivotY / height)); |
| 520 | mFolderIcon.setPivotX(folderIconPivotX); |
| 521 | mFolderIcon.setPivotY(folderIconPivotY); |
| 522 | |
| 523 | if (mMode == PARTIAL_GROW) { |
| 524 | lp.width = width; |
| 525 | lp.height = height; |
| 526 | lp.x = left; |
| 527 | lp.y = top; |
| 528 | } else { |
| 529 | mNewSize.set(left, top, left + width, top + height); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | private void setupContentForNumItems(int count) { |
| 534 | |
| 535 | setupContentDimension(count); |
| 536 | |
| 537 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 538 | if (lp == null) { |
| 539 | lp = new CellLayout.LayoutParams(0, 0, -1, -1); |
| 540 | lp.isLockedToGrid = false; |
| 541 | setLayoutParams(lp); |
| 542 | } |
| 543 | centerAboutIcon(); |
| 544 | } |
| 545 | |
| 546 | private void arrangeChildren(ArrayList<View> list) { |
| 547 | int[] vacant = new int[2]; |
| 548 | if (list == null) { |
| 549 | list = getItemsInReadingOrder(); |
| 550 | } |
| 551 | mContent.removeAllViews(); |
| 552 | |
| 553 | for (int i = 0; i < list.size(); i++) { |
| 554 | View v = list.get(i); |
| 555 | mContent.getVacantCell(vacant, 1, 1); |
| 556 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams(); |
| 557 | lp.cellX = vacant[0]; |
| 558 | lp.cellY = vacant[1]; |
| 559 | ItemInfo info = (ItemInfo) v.getTag(); |
| 560 | info.cellX = vacant[0]; |
| 561 | info.cellY = vacant[1]; |
| 562 | boolean insert = false; |
| 563 | mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true); |
| 564 | } |
| 565 | } |
| 566 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 567 | public void onAdd(ShortcutInfo item) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 568 | if (!findAndSetEmptyCells(item)) { |
| 569 | // The current layout is full, can we expand it? |
| 570 | setupContentForNumItems(getItemCount() + 1); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 571 | findAndSetEmptyCells(item); |
| 572 | } |
| 573 | createAndAddShortcut(item); |
| 574 | } |
| 575 | |
| 576 | public int getItemCount() { |
| 577 | return mContent.getChildrenLayout().getChildCount(); |
| 578 | } |
| 579 | |
| 580 | public View getItemAt(int index) { |
| 581 | return mContent.getChildrenLayout().getChildAt(index); |
| 582 | } |
| 583 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 584 | private ArrayList<View> getItemsInReadingOrder() { |
| 585 | ArrayList<View> list = new ArrayList<View>(); |
| 586 | for (int j = 0; j < mContent.getCountY(); j++) { |
| 587 | for (int i = 0; i < mContent.getCountX(); i++) { |
| 588 | View v = mContent.getChildAt(i, j); |
| 589 | if (v != null) { |
| 590 | list.add(v); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | return list; |
| 595 | } |
| 596 | |
| 597 | private void onCloseComplete() { |
| 598 | if (mRearrangeOnClose) { |
| 599 | setupContentForNumItems(getItemCount()); |
| 600 | mRearrangeOnClose = false; |
| 601 | } |
| 602 | } |
| 603 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 604 | public void onRemove(ShortcutInfo item) { |
| 605 | View v = mContent.getChildAt(mDragItemPosition[0], mDragItemPosition[1]); |
| 606 | mContent.removeView(v); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 607 | if (mState == STATE_ANIMATING) { |
| 608 | mRearrangeOnClose = true; |
| 609 | } else { |
| 610 | setupContentForNumItems(getItemCount()); |
| 611 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 612 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 613 | } |