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