Android Launcher源码分析(二)
2014-06-07 22:46
260 查看
今天主要分析创建shortcut、widget流程。
创建ShortCut流程,首先看completeAdd函数。
如果shortcutName是apps,则选择应用,若不是,则创建shortcut。在onActivityResult处理返回结果。
如果workspace正在被锁住,则加入列表,在finishBindingItems被执行
public void finishBindingItems() {
......
for (int i = 0; i < sPendingAddList.size(); i++) {
completeAdd(sPendingAddList.get(i));
}
sPendingAddList.clear();
.......
}
若workspace没有被锁住,则直接创建,看completeAdd()
private boolean completeAdd(PendingAddArguments args) {
boolean result = false;
switch (args.requestCode) {
......
case REQUEST_CREATE_SHORTCUT:
completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
args.cellY);
result = true;
break;
......
}
resetAddInfo();
return result;
}
completeAddShortcut(),创建shortcut到workspace
private void completeAddShortcut(Intent data, long container, int screen, int cellX,
int cellY) {
.......
ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
......
final View view = createShortcut(info);
// First we check if we already know the exact location where we want to add this item.
//首先判断希望添加的位置是否合适
if (cellX >= 0 && cellY >= 0) {
cellXY[0] = cellX;
cellXY[1] = cellY;
foundCellSpan = true;
// If appropriate, either create a folder or add to an existing folder
if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
true, null,null)) {
return;
}
DragObject dragObject = new DragObject();
dragObject.dragInfo = info;
if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
true)) {
return;
}
} else if (touchXY != null) {
// when dragging and dropping, just find the closest free spot
int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
foundCellSpan = (result != null);
} else {
foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
}
if (!foundCellSpan) {//如果已经没有空余的位置,则显示相应的提示
showOutOfSpaceMessage(isHotseatLayout(layout));
return;
}
LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);//将相应的数据添加到数据库
if (!mRestoring) {
mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
isWorkspaceLocked());
}
}在completeAddShortcut里,先创建shortcut视图,判断添加shortcut的位置是否合适,如果已经没有空余的位置,则显示相应的提示,添加相应的数据到数据库中,最后将shortcut添加到workspace中
创建widget流程与创建shortcut基本一致。
创建ShortCut流程,首先看completeAdd函数。
private boolean completeAdd(PendingAddArguments args) { boolean result = false; switch (args.requestCode) { ...... case REQUEST_PICK_SHORTCUT: processShortcut(args.intent); break; ...... } resetAddInfo(); return result; } 在completeAdd里接收到REQUEST_PICK_SHORTCUT请求,执行processShortcut()创建shortcut void processShortcut(Intent intent) { String applicationName = getResources().getString(R.string.group_applications);//applicationName是apps String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); if (applicationName != null && applicationName.equals(shortcutName)) { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);//选择应用 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent); pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application)); startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION); } else { startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT); } }
如果shortcutName是apps,则选择应用,若不是,则创建shortcut。在onActivityResult处理返回结果。
protected void onActivityResult( final int requestCode, final int resultCode, final Intent data) { ...... if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) { ...... //如果workspace正在被锁住,则加入列表,否则则进行创建 if (isWorkspaceLocked()) { sPendingAddList.add(args); } else { delayExitSpringLoadedMode = completeAdd(args); } } ....... }
如果workspace正在被锁住,则加入列表,在finishBindingItems被执行
public void finishBindingItems() {
......
for (int i = 0; i < sPendingAddList.size(); i++) {
completeAdd(sPendingAddList.get(i));
}
sPendingAddList.clear();
.......
}
若workspace没有被锁住,则直接创建,看completeAdd()
private boolean completeAdd(PendingAddArguments args) {
boolean result = false;
switch (args.requestCode) {
......
case REQUEST_CREATE_SHORTCUT:
completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
args.cellY);
result = true;
break;
......
}
resetAddInfo();
return result;
}
completeAddShortcut(),创建shortcut到workspace
private void completeAddShortcut(Intent data, long container, int screen, int cellX,
int cellY) {
.......
ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
......
final View view = createShortcut(info);
// First we check if we already know the exact location where we want to add this item.
//首先判断希望添加的位置是否合适
if (cellX >= 0 && cellY >= 0) {
cellXY[0] = cellX;
cellXY[1] = cellY;
foundCellSpan = true;
// If appropriate, either create a folder or add to an existing folder
if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
true, null,null)) {
return;
}
DragObject dragObject = new DragObject();
dragObject.dragInfo = info;
if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
true)) {
return;
}
} else if (touchXY != null) {
// when dragging and dropping, just find the closest free spot
int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
foundCellSpan = (result != null);
} else {
foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
}
if (!foundCellSpan) {//如果已经没有空余的位置,则显示相应的提示
showOutOfSpaceMessage(isHotseatLayout(layout));
return;
}
LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);//将相应的数据添加到数据库
if (!mRestoring) {
mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
isWorkspaceLocked());
}
}在completeAddShortcut里,先创建shortcut视图,判断添加shortcut的位置是否合适,如果已经没有空余的位置,则显示相应的提示,添加相应的数据到数据库中,最后将shortcut添加到workspace中
创建widget流程与创建shortcut基本一致。
相关文章推荐
- android launcher源码分析
- Android_launcher的源码详细分析
- Android 4.0 Launcher源码分析系列(三)
- Android 4.0 Launcher源码分析系列(一)
- Android中ICS4.0源码Launcher启动流程分析【android源码Launcher系列一】
- android_launcher的源码详细分析
- android_launcher的源码详细分析
- Android 4.0 Launcher源码分析系列(一)
- Launcher-Android_launcher的源码详细分析
- Android_launcher的源码详细分析
- android_launcher的源码详细分析
- Android_launcher的源码详细分析
- android launcher源码分析 .
- android_launcher的源码详细分析
- android_launcher的源码详细分析
- android launcher源码分析
- android_launcher的源码详细分析
- Android中ICS4.0源码Launcher启动流程分析
- Android 4.0 Launcher源码分析系列
- Android 4.0 Launcher源码分析系列(二)