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