1.Launcher的图标拖搁从Launcher开始,在Launcher的oncreate方法中设置了长按监听器,当我们长按图标时,会进入到Launcher的onLongClick方法
public boolean onLongClick(View v) {
if (LauncherLog.DEBUG) {
LauncherLog.d(TAG, "onLongClick: View = " + v + ", v.getTag() = " + v.getTag()
+ ", mState = " + mState);
}
//判断是否可以拖搁,如果正在加载workspace,则返回(!mModel.isLoadingWorkspace())
if (!isDraggingEnabled()) {
LauncherLog.d(TAG, "onLongClick: isDraggingEnabled() = " + isDraggingEnabled());
return false;
}
//不知道mWaitingForResult是做什么用的,mWorkspaceLoading是做什么用的
if (isWorkspaceLocked()) {
LauncherLog.d(TAG, "onLongClick: isWorkspaceLocked() mWorkspaceLoading " + mWorkspaceLoading
+ ", mWaitingForResult = " + mWaitingForResult);
return false;
}
//当前显示桌面的状态??
if (mState != State.WORKSPACE) {
LauncherLog.d(TAG, "onLongClick: mState != State.WORKSPACE: = " + mState);
return false;
}
/// M: modidfied for Unread feature, to find CellLayout through while loop.
while (!(v instanceof CellLayout)) {
v = (View) v.getParent();
}
//信息重置??
resetAddInfo();
//取得cellLayout的Tag信息
CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
// This happens when long clicking an item with the dpad/trackball
if (longClickCellInfo == null) {
return true;
}
// The hotseat touch handling does not go through Workspace, and we always allow long press
// on hotseat items.
//取得长按的view信息
final View itemUnderLongClick = longClickCellInfo.cell;
boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
if (allowLongPress && !mDragController.isDragging()) {
//如果长按在空的地方,则弹出壁纸选项
if (itemUnderLongClick == null) {
// User long pressed on empty space
mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
startWallpaper();
} else {
//如果是文件的话,开始拖搁
if (!(itemUnderLongClick instanceof Folder)) {
/// M: Call the appropriate callback for the IMtkWidget on the current page
/// when long click and begin to drag IMtkWidget.
mWorkspace.startDragAppWidget(mWorkspace.getCurrentPage());
// User long pressed on an item
mWorkspace.startDrag(longClickCellInfo);
}
}
}
return true;
}
2.如果为拖动图标的话,将调用StartDrag方法
void startDrag(CellLayout.CellInfo cellInfo) {
View child = cellInfo.cell;
if (LauncherLog.DEBUG_DRAG) {
LauncherLog.d(TAG, "startDrag cellInfo = " + cellInfo + ",child = " + child);
}
/// M: Abnormal case, if user long press on all apps button and then
/// long press on other shortcuts in hotseat, the dragInfo will be
/// null, exception will happen, so need return directly.
if (child != null && child.getTag() == null) {
LauncherLog.d(TAG, "Abnormal start drag: cellInfo = " + cellInfo + ",child = " + child);
return;
}
// Make sure the drag was started by a long press as opposed to a long click.
if (!child.isInTouchMode()) {
if (LauncherLog.DEBUG) {
LauncherLog.i(TAG, "The child " + child + " is not in touch mode.");
}
return;
}
mDragInfo = cellInfo;
//图标设为不可见
child.setVisibility(INVISIBLE);
CellLayout layout = (CellLayout) child.getParent().getParent();
//设图标所占有的地方为false
layout.prepareChildForDrag(child);
//去除焦点
child.clearFocus();
//不可按
child.setPressed(false);
//创建画布
final Canvas canvas = new Canvas();
// The outline is used to visualize where the item will land if dropped
mDragOutline = createDragOutline(child, canvas, DRAG_BITMAP_PADDING);
beginDragShared(child, this);
}