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 android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 21 | import android.animation.ObjectAnimator; |
| 22 | import android.animation.PropertyValuesHolder; |
| 23 | import android.animation.ValueAnimator; |
| 24 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 26 | import android.content.res.Resources; |
Romain Guy | fb5411e | 2010-02-24 10:04:17 -0800 | [diff] [blame] | 27 | import android.graphics.Rect; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Adam Cohen | 7a14d0b | 2011-06-24 11:36:35 -0700 | [diff] [blame^] | 29 | import android.text.InputType; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 31 | import android.view.ActionMode; |
| 32 | import android.view.KeyEvent; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 34 | import android.view.Menu; |
| 35 | import android.view.MenuItem; |
Adam Cohen | 0c872ba | 2011-05-05 10:34:16 -0700 | [diff] [blame] | 36 | import android.view.MotionEvent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | import android.view.View; |
| 38 | import android.view.View.OnClickListener; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 39 | import android.view.animation.AccelerateInterpolator; |
| 40 | import android.view.animation.DecelerateInterpolator; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 41 | import android.view.inputmethod.EditorInfo; |
| 42 | import android.view.inputmethod.InputMethodManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | import android.widget.AdapterView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | import android.widget.LinearLayout; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 45 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | import android.widget.AdapterView.OnItemClickListener; |
| 47 | import android.widget.AdapterView.OnItemLongClickListener; |
| 48 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 49 | import com.android.launcher.R; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 50 | import com.android.launcher2.FolderInfo.FolderListener; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 51 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Represents a set of icons chosen by the user or generated by the system. |
| 56 | */ |
| 57 | public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener, |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 58 | OnItemClickListener, OnClickListener, View.OnLongClickListener, DropTarget, FolderListener, |
| 59 | TextView.OnEditorActionListener { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 61 | protected DragController mDragController; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 62 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 63 | protected Launcher mLauncher; |
| 64 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 65 | protected FolderInfo mInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 66 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 67 | private static final String TAG = "Launcher.Folder"; |
| 68 | |
| 69 | static final int STATE_NONE = -1; |
| 70 | static final int STATE_SMALL = 0; |
| 71 | static final int STATE_ANIMATING = 1; |
| 72 | static final int STATE_OPEN = 2; |
| 73 | |
| 74 | private int mExpandDuration; |
| 75 | protected CellLayout mContent; |
| 76 | private final LayoutInflater mInflater; |
| 77 | private final IconCache mIconCache; |
| 78 | private int mState = STATE_NONE; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 79 | private int[] mDragItemPosition = new int[2]; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 80 | private static final int FULL_GROW = 0; |
| 81 | private static final int PARTIAL_GROW = 1; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 82 | private static final int REORDER_ANIMATION_DURATION = 230; |
| 83 | private static final int ON_EXIT_CLOSE_DELAY = 800; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 84 | private int mMode = PARTIAL_GROW; |
| 85 | private boolean mRearrangeOnClose = false; |
| 86 | private FolderIcon mFolderIcon; |
| 87 | private int mMaxCountX; |
| 88 | private int mMaxCountY; |
| 89 | private Rect mNewSize = new Rect(); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 90 | private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>(); |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 91 | private Drawable mIconDrawable; |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 92 | boolean mItemsInvalidated = false; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 93 | private ShortcutInfo mCurrentDragInfo; |
| 94 | private View mCurrentDragView; |
| 95 | boolean mSuppressOnAdd = false; |
| 96 | private int[] mTargetCell = new int[2]; |
| 97 | private int[] mPreviousTargetCell = new int[2]; |
| 98 | private int[] mEmptyCell = new int[2]; |
| 99 | private Alarm mReorderAlarm = new Alarm(); |
| 100 | private Alarm mOnExitAlarm = new Alarm(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 101 | private TextView mFolderName; |
| 102 | private int mFolderNameHeight; |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 103 | private static String sDefaultFolderName; |
| 104 | private Rect mHitRect = new Rect(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 105 | |
| 106 | private boolean mIsEditingName = false; |
| 107 | private InputMethodManager mInputMethodManager; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 108 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 109 | /** |
| 110 | * Used to inflate the Workspace from XML. |
| 111 | * |
| 112 | * @param context The application's context. |
| 113 | * @param attrs The attribtues set containing the Workspace's customization values. |
| 114 | */ |
| 115 | public Folder(Context context, AttributeSet attrs) { |
| 116 | super(context, attrs); |
| 117 | setAlwaysDrawnWithCacheEnabled(false); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 118 | mInflater = LayoutInflater.from(context); |
| 119 | mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 120 | mMaxCountX = LauncherModel.getCellCountX() - 1; |
| 121 | mMaxCountY = LauncherModel.getCellCountY() - 1; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 122 | |
| 123 | mInputMethodManager = (InputMethodManager) |
| 124 | mContext.getSystemService(Context.INPUT_METHOD_SERVICE); |
| 125 | |
| 126 | Resources res = getResources(); |
| 127 | mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration); |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 128 | |
| 129 | if (sDefaultFolderName == null) { |
| 130 | sDefaultFolderName = res.getString(R.string.folder_name); |
| 131 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | protected void onFinishInflate() { |
| 136 | super.onFinishInflate(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 137 | mContent = (CellLayout) findViewById(R.id.folder_content); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 138 | mContent.setGridSize(0, 0); |
| 139 | mContent.enableHardwareLayers(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 140 | mFolderName = (TextView) findViewById(R.id.folder_name); |
| 141 | |
| 142 | // We find out how tall the text view wants to be (it is set to wrap_content), so that |
| 143 | // we can allocate the appropriate amount of space for it. |
| 144 | int measureSpec = MeasureSpec.UNSPECIFIED; |
| 145 | mFolderName.measure(measureSpec, measureSpec); |
| 146 | mFolderNameHeight = mFolderName.getMeasuredHeight(); |
| 147 | |
| 148 | // We disable action mode for now since it messes up the view on phones |
| 149 | mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback); |
| 150 | mFolderName.setCursorVisible(false); |
| 151 | mFolderName.setOnEditorActionListener(this); |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 152 | mFolderName.setSelectAllOnFocus(true); |
Adam Cohen | 7a14d0b | 2011-06-24 11:36:35 -0700 | [diff] [blame^] | 153 | mFolderName.setInputType(mFolderName.getInputType() | |
| 154 | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 155 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 156 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 157 | private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() { |
| 158 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | public void onDestroyActionMode(ActionMode mode) { |
| 167 | } |
| 168 | |
| 169 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 170 | return false; |
| 171 | } |
| 172 | }; |
| 173 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 174 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 175 | ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position); |
Romain Guy | fb5411e | 2010-02-24 10:04:17 -0800 | [diff] [blame] | 176 | int[] pos = new int[2]; |
| 177 | v.getLocationOnScreen(pos); |
| 178 | app.intent.setSourceBounds(new Rect(pos[0], pos[1], |
| 179 | pos[0] + v.getWidth(), pos[1] + v.getHeight())); |
Joe Onorato | f984e85 | 2010-03-25 09:47:45 -0700 | [diff] [blame] | 180 | mLauncher.startActivitySafely(app.intent, app); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | public void onClick(View v) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 184 | Object tag = v.getTag(); |
| 185 | if (tag instanceof ShortcutInfo) { |
| 186 | // refactor this code from Folder |
| 187 | ShortcutInfo item = (ShortcutInfo) tag; |
| 188 | int[] pos = new int[2]; |
| 189 | v.getLocationOnScreen(pos); |
| 190 | item.intent.setSourceBounds(new Rect(pos[0], pos[1], |
| 191 | pos[0] + v.getWidth(), pos[1] + v.getHeight())); |
| 192 | mLauncher.startActivitySafely(item.intent, item); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 193 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 196 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 197 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 198 | mFolderName.getHitRect(mHitRect); |
| 199 | if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) { |
| 200 | startEditingFolderName(); |
| 201 | } |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 206 | public boolean onLongClick(View v) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 207 | Object tag = v.getTag(); |
| 208 | if (tag instanceof ShortcutInfo) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 209 | ShortcutInfo item = (ShortcutInfo) tag; |
| 210 | if (!v.isInTouchMode()) { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | mLauncher.getWorkspace().onDragStartedWithItem(v); |
| 215 | mDragController.startDrag(v, this, item, DragController.DRAG_ACTION_COPY); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 216 | mDragItemPosition[0] = item.cellX; |
| 217 | mDragItemPosition[1] = item.cellY; |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 218 | mIconDrawable = ((TextView) v).getCompoundDrawables()[1]; |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 219 | |
| 220 | mCurrentDragInfo = item; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 221 | mEmptyCell[0] = item.cellX; |
| 222 | mEmptyCell[1] = item.cellY; |
| 223 | mCurrentDragView = v; |
| 224 | mContent.removeView(mCurrentDragView); |
| 225 | mInfo.remove(item); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 226 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 227 | return true; |
| 228 | } |
| 229 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 230 | public boolean isEditingName() { |
| 231 | return mIsEditingName; |
| 232 | } |
| 233 | |
| 234 | public void startEditingFolderName() { |
| 235 | mFolderName.setCursorVisible(true); |
| 236 | mIsEditingName = true; |
| 237 | } |
| 238 | |
| 239 | public void dismissEditingName() { |
| 240 | mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0); |
| 241 | doneEditingFolderName(true); |
| 242 | } |
| 243 | |
| 244 | public void doneEditingFolderName(boolean commit) { |
| 245 | mInfo.setTitle(mFolderName.getText()); |
| 246 | LauncherModel.updateItemInDatabase(mLauncher, mInfo); |
| 247 | mFolderName.setCursorVisible(false); |
| 248 | mFolderName.clearFocus(); |
| 249 | mIsEditingName = false; |
| 250 | } |
| 251 | |
| 252 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 253 | if (actionId == EditorInfo.IME_ACTION_DONE) { |
| 254 | dismissEditingName(); |
| 255 | return true; |
| 256 | } |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | public View getEditTextRegion() { |
| 261 | return mFolderName; |
| 262 | } |
| 263 | |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 264 | public Drawable getDragDrawable() { |
| 265 | return mIconDrawable; |
| 266 | } |
| 267 | |
Adam Cohen | 0c872ba | 2011-05-05 10:34:16 -0700 | [diff] [blame] | 268 | /** |
| 269 | * We need to handle touch events to prevent them from falling through to the workspace below. |
| 270 | */ |
| 271 | @Override |
| 272 | public boolean onTouchEvent(MotionEvent ev) { |
| 273 | return true; |
| 274 | } |
| 275 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 276 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
| 277 | if (!view.isInTouchMode()) { |
| 278 | return false; |
| 279 | } |
| 280 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 281 | ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 282 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 283 | mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 284 | mLauncher.closeFolder(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 285 | return true; |
| 286 | } |
| 287 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 288 | public void setDragController(DragController dragController) { |
| 289 | mDragController = dragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 292 | public void onDragViewVisible() { |
| 293 | } |
| 294 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 295 | void setLauncher(Launcher launcher) { |
| 296 | mLauncher = launcher; |
| 297 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 298 | |
| 299 | void setFolderIcon(FolderIcon icon) { |
| 300 | mFolderIcon = icon; |
| 301 | } |
| 302 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 303 | /** |
| 304 | * @return the FolderInfo object associated with this folder |
| 305 | */ |
| 306 | FolderInfo getInfo() { |
| 307 | return mInfo; |
| 308 | } |
| 309 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 310 | void onOpen() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 311 | // When the folder opens, we need to refresh the GridView's selection by |
| 312 | // forcing a layout |
| 313 | // TODO: find out if this is still necessary |
| 314 | mContent.requestLayout(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void onClose() { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 318 | CellLayoutChildren clc = (CellLayoutChildren) getParent(); |
| 319 | final CellLayout cellLayout = (CellLayout) clc.getParent(); |
| 320 | cellLayout.removeViewWithoutMarkingCells(Folder.this); |
| 321 | clearFocus(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void bind(FolderInfo info) { |
| 325 | mInfo = info; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 326 | ArrayList<ShortcutInfo> children = info.contents; |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 327 | setupContentForNumItems(children.size()); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 328 | for (int i = 0; i < children.size(); i++) { |
| 329 | ShortcutInfo child = (ShortcutInfo) children.get(i); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 330 | createAndAddShortcut(child); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 331 | } |
Adam Cohen | 4dbe6d9 | 2011-05-18 17:14:15 -0700 | [diff] [blame] | 332 | mItemsInvalidated = true; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 333 | mInfo.addListener(this); |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 334 | |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 335 | if (!sDefaultFolderName.contentEquals(mInfo.title)) { |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 336 | mFolderName.setText(mInfo.title); |
| 337 | } else { |
| 338 | mFolderName.setText(""); |
| 339 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Creates a new UserFolder, inflated from R.layout.user_folder. |
| 344 | * |
| 345 | * @param context The application's context. |
| 346 | * |
| 347 | * @return A new UserFolder. |
| 348 | */ |
| 349 | static Folder fromXml(Context context) { |
| 350 | return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * This method is intended to make the UserFolder to be visually identical in size and position |
| 355 | * to its associated FolderIcon. This allows for a seamless transition into the expanded state. |
| 356 | */ |
| 357 | private void positionAndSizeAsIcon() { |
| 358 | if (!(getParent() instanceof CellLayoutChildren)) return; |
| 359 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 360 | CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 361 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 362 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 363 | if (mMode == PARTIAL_GROW) { |
| 364 | setScaleX(0.8f); |
| 365 | setScaleY(0.8f); |
| 366 | setAlpha(0f); |
| 367 | } else { |
| 368 | lp.width = iconLp.width; |
| 369 | lp.height = iconLp.height; |
| 370 | lp.x = iconLp.x; |
| 371 | lp.y = iconLp.y; |
| 372 | mContent.setAlpha(0); |
| 373 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 374 | mState = STATE_SMALL; |
| 375 | } |
| 376 | |
| 377 | public void animateOpen() { |
| 378 | if (mState != STATE_SMALL) { |
| 379 | positionAndSizeAsIcon(); |
| 380 | } |
| 381 | if (!(getParent() instanceof CellLayoutChildren)) return; |
| 382 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 383 | ObjectAnimator oa; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 384 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 385 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 386 | centerAboutIcon(); |
| 387 | if (mMode == PARTIAL_GROW) { |
| 388 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1); |
| 389 | PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f); |
| 390 | PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f); |
| 391 | oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY); |
| 392 | } else { |
| 393 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width()); |
| 394 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height()); |
| 395 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left); |
| 396 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top); |
| 397 | oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y); |
| 398 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 399 | public void onAnimationUpdate(ValueAnimator animation) { |
| 400 | requestLayout(); |
| 401 | } |
| 402 | }); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 403 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 404 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f); |
| 405 | ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha); |
| 406 | alphaOa.setDuration(mExpandDuration); |
| 407 | alphaOa.setInterpolator(new AccelerateInterpolator(2.0f)); |
| 408 | alphaOa.start(); |
| 409 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 410 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 411 | oa.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 412 | @Override |
| 413 | public void onAnimationStart(Animator animation) { |
| 414 | mState = STATE_ANIMATING; |
| 415 | } |
| 416 | @Override |
| 417 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 418 | mState = STATE_OPEN; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 419 | } |
| 420 | }); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 421 | oa.setDuration(mExpandDuration); |
| 422 | oa.start(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | public void animateClosed() { |
| 426 | if (!(getParent() instanceof CellLayoutChildren)) return; |
| 427 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 428 | ObjectAnimator oa; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 429 | if (mMode == PARTIAL_GROW) { |
| 430 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0); |
| 431 | PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f); |
| 432 | PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f); |
| 433 | oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY); |
| 434 | } else { |
| 435 | CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams(); |
| 436 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 437 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 438 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", iconLp.width); |
| 439 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", iconLp.height); |
| 440 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x",iconLp.x); |
| 441 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", iconLp.y); |
| 442 | oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y); |
| 443 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 444 | public void onAnimationUpdate(ValueAnimator animation) { |
| 445 | requestLayout(); |
| 446 | } |
| 447 | }); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 448 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 449 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f); |
| 450 | ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha); |
| 451 | alphaOa.setDuration(mExpandDuration); |
| 452 | alphaOa.setInterpolator(new DecelerateInterpolator(2.0f)); |
| 453 | alphaOa.start(); |
| 454 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 455 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 456 | oa.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 457 | @Override |
| 458 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 459 | onClose(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 460 | onCloseComplete(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 461 | mState = STATE_SMALL; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 462 | } |
| 463 | @Override |
| 464 | public void onAnimationStart(Animator animation) { |
| 465 | mState = STATE_ANIMATING; |
| 466 | } |
| 467 | }); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 468 | oa.setDuration(mExpandDuration); |
| 469 | oa.start(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void notifyDataSetChanged() { |
| 473 | // recreate all the children if the data set changes under us. We may want to do this more |
| 474 | // intelligently (ie just removing the views that should no longer exist) |
| 475 | mContent.removeAllViewsInLayout(); |
| 476 | bind(mInfo); |
| 477 | } |
| 478 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 479 | public boolean acceptDrop(DragObject d) { |
| 480 | final ItemInfo item = (ItemInfo) d.dragInfo; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 481 | final int itemType = item.itemType; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 482 | return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 483 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && |
| 484 | !isFull()); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 487 | protected boolean findAndSetEmptyCells(ShortcutInfo item) { |
| 488 | int[] emptyCell = new int[2]; |
| 489 | if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) { |
| 490 | item.cellX = emptyCell[0]; |
| 491 | item.cellY = emptyCell[1]; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 492 | return true; |
| 493 | } else { |
| 494 | return false; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | protected void createAndAddShortcut(ShortcutInfo item) { |
| 499 | final TextView textView = |
| 500 | (TextView) mInflater.inflate(R.layout.application_boxed, this, false); |
| 501 | textView.setCompoundDrawablesWithIntrinsicBounds(null, |
| 502 | new FastBitmapDrawable(item.getIcon(mIconCache)), null, null); |
| 503 | textView.setText(item.title); |
| 504 | textView.setTag(item); |
| 505 | |
| 506 | textView.setOnClickListener(this); |
| 507 | textView.setOnLongClickListener(this); |
| 508 | |
| 509 | CellLayout.LayoutParams lp = |
| 510 | new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY); |
| 511 | boolean insert = false; |
| 512 | mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true); |
| 513 | } |
| 514 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 515 | public void onDragEnter(DragObject d) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 516 | mPreviousTargetCell[0] = -1; |
| 517 | mPreviousTargetCell[1] = -1; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 518 | mContent.onDragEnter(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 519 | mOnExitAlarm.cancelAlarm(); |
| 520 | } |
| 521 | |
| 522 | OnAlarmListener mReorderAlarmListener = new OnAlarmListener() { |
| 523 | public void onAlarm(Alarm alarm) { |
| 524 | realTimeReorder(mEmptyCell, mTargetCell); |
| 525 | } |
| 526 | }; |
| 527 | |
| 528 | boolean readingOrderGreaterThan(int[] v1, int[] v2) { |
| 529 | if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) { |
| 530 | return true; |
| 531 | } else { |
| 532 | return false; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | private void realTimeReorder(int[] empty, int[] target) { |
| 537 | boolean wrap; |
| 538 | int startX; |
| 539 | int endX; |
| 540 | int startY; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 541 | int delay = 0; |
| 542 | float delayAmount = 30; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 543 | if (readingOrderGreaterThan(target, empty)) { |
| 544 | wrap = empty[0] >= mContent.getCountX() - 1; |
| 545 | startY = wrap ? empty[1] + 1 : empty[1]; |
| 546 | for (int y = startY; y <= target[1]; y++) { |
| 547 | startX = y == empty[1] ? empty[0] + 1 : 0; |
| 548 | endX = y < target[1] ? mContent.getCountX() - 1 : target[0]; |
| 549 | for (int x = startX; x <= endX; x++) { |
| 550 | View v = mContent.getChildAt(x,y); |
| 551 | if (mContent.animateChildToPosition(v, empty[0], empty[1], |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 552 | REORDER_ANIMATION_DURATION, delay)) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 553 | empty[0] = x; |
| 554 | empty[1] = y; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 555 | delay += delayAmount; |
| 556 | delayAmount *= 0.9; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | } |
| 560 | } else { |
| 561 | wrap = empty[0] == 0; |
| 562 | startY = wrap ? empty[1] - 1 : empty[1]; |
| 563 | for (int y = startY; y >= target[1]; y--) { |
| 564 | startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1; |
| 565 | endX = y > target[1] ? 0 : target[0]; |
| 566 | for (int x = startX; x >= endX; x--) { |
| 567 | View v = mContent.getChildAt(x,y); |
| 568 | if (mContent.animateChildToPosition(v, empty[0], empty[1], |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 569 | REORDER_ANIMATION_DURATION, delay)) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 570 | empty[0] = x; |
| 571 | empty[1] = y; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 572 | delay += delayAmount; |
| 573 | delayAmount *= 0.9; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | } |
| 577 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 580 | public void onDragOver(DragObject d) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 581 | float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null); |
| 582 | mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell); |
| 583 | |
| 584 | if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) { |
| 585 | mReorderAlarm.cancelAlarm(); |
| 586 | mReorderAlarm.setOnAlarmListener(mReorderAlarmListener); |
| 587 | mReorderAlarm.setAlarm(150); |
| 588 | mPreviousTargetCell[0] = mTargetCell[0]; |
| 589 | mPreviousTargetCell[1] = mTargetCell[1]; |
| 590 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 593 | // This is used to compute the visual center of the dragView. The idea is that |
| 594 | // the visual center represents the user's interpretation of where the item is, and hence |
| 595 | // is the appropriate point to use when determining drop location. |
| 596 | private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset, |
| 597 | DragView dragView, float[] recycle) { |
| 598 | float res[]; |
| 599 | if (recycle == null) { |
| 600 | res = new float[2]; |
| 601 | } else { |
| 602 | res = recycle; |
| 603 | } |
| 604 | |
| 605 | // These represent the visual top and left of drag view if a dragRect was provided. |
| 606 | // If a dragRect was not provided, then they correspond to the actual view left and |
| 607 | // top, as the dragRect is in that case taken to be the entire dragView. |
| 608 | // R.dimen.dragViewOffsetY. |
| 609 | int left = x - xOffset; |
| 610 | int top = y - yOffset; |
| 611 | |
| 612 | // In order to find the visual center, we shift by half the dragRect |
| 613 | res[0] = left + dragView.getDragRegion().width() / 2; |
| 614 | res[1] = top + dragView.getDragRegion().height() / 2; |
| 615 | |
| 616 | return res; |
| 617 | } |
| 618 | |
| 619 | OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() { |
| 620 | public void onAlarm(Alarm alarm) { |
Adam Cohen | 1d9af7d | 2011-06-22 15:26:58 -0700 | [diff] [blame] | 621 | mLauncher.closeFolder(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 622 | mCurrentDragInfo = null; |
| 623 | mCurrentDragView = null; |
| 624 | mSuppressOnAdd = false; |
| 625 | mRearrangeOnClose = true; |
| 626 | } |
| 627 | }; |
| 628 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 629 | public void onDragExit(DragObject d) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 630 | // We only close the folder if this is a true drag exit, ie. not because a drop |
| 631 | // has occurred above the folder. |
| 632 | if (!d.dragComplete) { |
| 633 | mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener); |
| 634 | mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY); |
| 635 | } |
| 636 | mReorderAlarm.cancelAlarm(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 637 | mContent.onDragExit(); |
| 638 | } |
| 639 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 640 | public void onDropCompleted(View target, DragObject d, boolean success) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 641 | mCurrentDragInfo = null; |
| 642 | mCurrentDragView = null; |
| 643 | mSuppressOnAdd = false; |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 644 | if (!success) { |
| 645 | if (d.dragView != null) { |
| 646 | if (target instanceof CellLayout) { |
| 647 | // TODO: we should animate the item back to the folder in this case |
| 648 | } |
| 649 | } |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 650 | // TODO: if the drag fails, we need to re-add the item |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 651 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | public boolean isDropEnabled() { |
| 655 | return true; |
| 656 | } |
| 657 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 658 | public DropTarget getDropTargetDelegate(DragObject d) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 659 | return null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 660 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 661 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 662 | private void setupContentDimension(int count) { |
| 663 | ArrayList<View> list = getItemsInReadingOrder(); |
| 664 | |
| 665 | int countX = mContent.getCountX(); |
| 666 | int countY = mContent.getCountY(); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 667 | boolean done = false; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 668 | |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 669 | while (!done) { |
| 670 | int oldCountX = countX; |
| 671 | int oldCountY = countY; |
| 672 | if (countX * countY < count) { |
| 673 | // Current grid is too small, expand it |
| 674 | if (countX <= countY && countX < mMaxCountX) { |
| 675 | countX++; |
| 676 | } else if (countY < mMaxCountY) { |
| 677 | countY++; |
| 678 | } |
| 679 | if (countY == 0) countY++; |
| 680 | } else if ((countY - 1) * countX >= count && countY >= countX) { |
| 681 | countY = Math.max(0, countY - 1); |
| 682 | } else if ((countX - 1) * countY >= count) { |
| 683 | countX = Math.max(0, countX - 1); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 684 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 685 | done = countX == oldCountX && countY == oldCountY; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 686 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 687 | mContent.setGridSize(countX, countY); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 688 | arrangeChildren(list); |
| 689 | } |
| 690 | |
| 691 | public boolean isFull() { |
| 692 | return getItemCount() >= mMaxCountX * mMaxCountY; |
| 693 | } |
| 694 | |
| 695 | private void centerAboutIcon() { |
| 696 | CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams(); |
| 697 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 698 | |
| 699 | int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 700 | // Technically there is no padding at the bottom, but we add space equal to the padding |
| 701 | // and have to account for that here. |
| 702 | int height = getPaddingTop() + mContent.getDesiredHeight() + mFolderNameHeight; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 703 | |
| 704 | int centerX = iconLp.x + iconLp.width / 2; |
| 705 | int centerY = iconLp.y + iconLp.height / 2; |
| 706 | int centeredLeft = centerX - width / 2; |
| 707 | int centeredTop = centerY - height / 2; |
| 708 | |
| 709 | CellLayoutChildren clc = (CellLayoutChildren) getParent(); |
| 710 | int parentWidth = 0; |
| 711 | int parentHeight = 0; |
| 712 | if (clc != null) { |
| 713 | parentWidth = clc.getMeasuredWidth(); |
| 714 | parentHeight = clc.getMeasuredHeight(); |
| 715 | } |
| 716 | |
| 717 | int left = Math.min(Math.max(0, centeredLeft), parentWidth - width); |
| 718 | int top = Math.min(Math.max(0, centeredTop), parentHeight - height); |
| 719 | |
| 720 | int folderPivotX = width / 2 + (centeredLeft - left); |
| 721 | int folderPivotY = height / 2 + (centeredTop - top); |
| 722 | setPivotX(folderPivotX); |
| 723 | setPivotY(folderPivotY); |
| 724 | int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() * |
| 725 | (1.0f * folderPivotX / width)); |
| 726 | int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() * |
| 727 | (1.0f * folderPivotY / height)); |
| 728 | mFolderIcon.setPivotX(folderIconPivotX); |
| 729 | mFolderIcon.setPivotY(folderIconPivotY); |
| 730 | |
| 731 | if (mMode == PARTIAL_GROW) { |
| 732 | lp.width = width; |
| 733 | lp.height = height; |
| 734 | lp.x = left; |
| 735 | lp.y = top; |
| 736 | } else { |
| 737 | mNewSize.set(left, top, left + width, top + height); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | private void setupContentForNumItems(int count) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 742 | setupContentDimension(count); |
| 743 | |
| 744 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams(); |
| 745 | if (lp == null) { |
| 746 | lp = new CellLayout.LayoutParams(0, 0, -1, -1); |
| 747 | lp.isLockedToGrid = false; |
| 748 | setLayoutParams(lp); |
| 749 | } |
| 750 | centerAboutIcon(); |
| 751 | } |
| 752 | |
| 753 | private void arrangeChildren(ArrayList<View> list) { |
| 754 | int[] vacant = new int[2]; |
| 755 | if (list == null) { |
| 756 | list = getItemsInReadingOrder(); |
| 757 | } |
| 758 | mContent.removeAllViews(); |
| 759 | |
| 760 | for (int i = 0; i < list.size(); i++) { |
| 761 | View v = list.get(i); |
| 762 | mContent.getVacantCell(vacant, 1, 1); |
| 763 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams(); |
| 764 | lp.cellX = vacant[0]; |
| 765 | lp.cellY = vacant[1]; |
| 766 | ItemInfo info = (ItemInfo) v.getTag(); |
| 767 | info.cellX = vacant[0]; |
| 768 | info.cellY = vacant[1]; |
| 769 | boolean insert = false; |
| 770 | mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 771 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0, |
| 772 | info.cellX, info.cellY); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 773 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 774 | mItemsInvalidated = true; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 777 | public int getItemCount() { |
| 778 | return mContent.getChildrenLayout().getChildCount(); |
| 779 | } |
| 780 | |
| 781 | public View getItemAt(int index) { |
| 782 | return mContent.getChildrenLayout().getChildAt(index); |
| 783 | } |
| 784 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 785 | private void onCloseComplete() { |
| 786 | if (mRearrangeOnClose) { |
| 787 | setupContentForNumItems(getItemCount()); |
| 788 | mRearrangeOnClose = false; |
| 789 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 790 | if (getItemCount() <= 1) { |
| 791 | replaceFolderWithFinalItem(); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | private void replaceFolderWithFinalItem() { |
| 796 | ItemInfo finalItem = null; |
| 797 | |
| 798 | if (getItemCount() == 1) { |
| 799 | finalItem = mInfo.contents.get(0); |
| 800 | } |
| 801 | |
| 802 | // Remove the folder completely |
| 803 | final CellLayout cellLayout = (CellLayout) |
| 804 | mLauncher.getWorkspace().getChildAt(mInfo.screen); |
| 805 | cellLayout.removeView(mFolderIcon); |
| 806 | if (mFolderIcon instanceof DropTarget) { |
| 807 | mDragController.removeDropTarget((DropTarget) mFolderIcon); |
| 808 | } |
| 809 | mLauncher.removeFolder(mInfo); |
| 810 | |
| 811 | if (finalItem != null) { |
| 812 | LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, |
| 813 | LauncherSettings.Favorites.CONTAINER_DESKTOP, mInfo.screen, |
| 814 | mInfo.cellX, mInfo.cellY); |
| 815 | } |
| 816 | LauncherModel.deleteFolderContentsFromDatabase(mLauncher, mInfo, true); |
| 817 | |
| 818 | // Add the last remaining child to the workspace in place of the folder |
| 819 | if (finalItem != null) { |
| 820 | View child = mLauncher.createShortcut(R.layout.application, cellLayout, |
| 821 | (ShortcutInfo) finalItem); |
| 822 | |
| 823 | mLauncher.getWorkspace().addInScreen(child, mInfo.screen, mInfo.cellX, mInfo.cellY, |
| 824 | mInfo.spanX, mInfo.spanY); |
| 825 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 828 | public void onDrop(DragObject d) { |
| 829 | ShortcutInfo item; |
| 830 | if (d.dragInfo instanceof ApplicationInfo) { |
| 831 | // Came from all apps -- make a copy |
| 832 | item = ((ApplicationInfo) d.dragInfo).makeShortcut(); |
| 833 | item.spanX = 1; |
| 834 | item.spanY = 1; |
| 835 | } else { |
| 836 | item = (ShortcutInfo) d.dragInfo; |
| 837 | } |
| 838 | // Dragged from self onto self |
| 839 | if (item == mCurrentDragInfo) { |
| 840 | ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag(); |
| 841 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams(); |
| 842 | si.cellX = lp.cellX = mEmptyCell[0]; |
| 843 | si.cellX = lp.cellY = mEmptyCell[1]; |
| 844 | mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true); |
| 845 | mSuppressOnAdd = true; |
| 846 | mItemsInvalidated = true; |
| 847 | setupContentDimension(getItemCount()); |
| 848 | } |
| 849 | mInfo.add(item); |
| 850 | } |
| 851 | |
| 852 | public void onAdd(ShortcutInfo item) { |
| 853 | mItemsInvalidated = true; |
| 854 | if (mSuppressOnAdd) return; |
| 855 | if (!findAndSetEmptyCells(item)) { |
| 856 | // The current layout is full, can we expand it? |
| 857 | setupContentForNumItems(getItemCount() + 1); |
| 858 | findAndSetEmptyCells(item); |
| 859 | } |
| 860 | createAndAddShortcut(item); |
| 861 | LauncherModel.addOrMoveItemInDatabase( |
| 862 | mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); |
| 863 | } |
| 864 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 865 | public void onRemove(ShortcutInfo item) { |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 866 | mItemsInvalidated = true; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 867 | if (item == mCurrentDragInfo) return; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 868 | View v = mContent.getChildAt(mDragItemPosition[0], mDragItemPosition[1]); |
| 869 | mContent.removeView(v); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 870 | if (mState == STATE_ANIMATING) { |
| 871 | mRearrangeOnClose = true; |
| 872 | } else { |
| 873 | setupContentForNumItems(getItemCount()); |
| 874 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 875 | if (getItemCount() <= 1) { |
| 876 | replaceFolderWithFinalItem(); |
| 877 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 878 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 879 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 880 | public void onItemsChanged() { |
| 881 | } |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 882 | public void onTitleChanged(CharSequence title) { |
| 883 | } |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 884 | |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 885 | public ArrayList<View> getItemsInReadingOrder() { |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 886 | return getItemsInReadingOrder(true); |
| 887 | } |
| 888 | |
| 889 | public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) { |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 890 | if (mItemsInvalidated) { |
| 891 | mItemsInReadingOrder.clear(); |
| 892 | for (int j = 0; j < mContent.getCountY(); j++) { |
| 893 | for (int i = 0; i < mContent.getCountX(); i++) { |
| 894 | View v = mContent.getChildAt(i, j); |
| 895 | if (v != null) { |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 896 | ShortcutInfo info = (ShortcutInfo) v.getTag(); |
| 897 | if (info != mCurrentDragInfo || includeCurrentDragItem) { |
| 898 | mItemsInReadingOrder.add(v); |
| 899 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | } |
| 903 | mItemsInvalidated = false; |
| 904 | } |
| 905 | return mItemsInReadingOrder; |
| 906 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 907 | } |