闲话少叙,目前原生的Launcher,只有桌面上的文件夹支持重命名。第三方软件如ADW,GOLauncher等长按桌面图标之后在弹出的popup框中也有了重命名选项。本文意在给出此功能实现的demo,如果你有不同的观点,也请多多指教。

1.获取桌面图标的信息。
  当我们长按桌面图标时,会执行Workspace.java中的方法 startDrag(CellLayout.CellInfo cellInfo),其参数cellInfo即为操作对象。

  过程分析:添加到桌面上的app图标,数组会保存其相应信息CellInfo。长按时会响应手指移动的onInterceptTouchEvent事件,并根据移动时的坐标来与数组中的app的信息比较,若手指移动到app的桌面有效区域,app长按事件响应。

View child = cellInfo.cell;
ItemInfo info = (ItemInfo)child.getTag();  //Get the app Info

2.创建能够重命名的对话框。
对话框的作用是为了记录输入,并将其作为该图标新的名字。
EidtName = mEditInput.getText().toString(); //Get the new name 

3.修改桌面图标
桌面上的app,即快捷方式,从安装包获取得Icon和Title并将其显示于桌面。
修改信息:
ShortcutInfo mShortcutInfo = (ShortcutInfo)info;    
mShortcutInfo.setTitle(EidtName); //Set ShortcutInfo Title

修改名字:
View v = mWorkspace.getViewForTag(mShortcutInfo); //Get View for ShortcutInfo
mShortcutInfo.title = EidtName;
((BubbleTextView)v).setText(EidtName); //Set View Title

补充:google提供了修改桌面快捷方式图标以及名字的接口,本文只做修改名字的分析。

4.保存数据库
LauncherModel.updateItemInDatabase(Launcher.this,mShortcutInfo);

5.重新加载
在加载桌面布局时,调用方法LauncherModel.loadWorkspace()中获取app信息getShortcutInfo();
其默认是先从包管理器中获得,而非从数据库中加载。这里要改变其加载顺序。 

        // from the db
        if (c != null) {
            info.title =  c.getString(titleIndex);
        }

        // from the resource
        if (info.title == null) {
            if (resolveInfo != null) {
                info.title = resolveInfo.activityInfo.loadLabel(manager);
            }
        }

关键代码已全部贴出,欢迎各位多多指教!转载请注明作者和出处,谢谢!
posted on 2011-12-28 11:44  sonken_SK  阅读(317)  评论(0编辑  收藏  举报