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