android launcher 之踩到的坑

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/asd1031/article/details/53140343

需求:
1、 用android系统launcher 隐藏主菜单 所有应用显示在桌面 即workspace上;
2、隐藏launcher上方默认的google search;
3、切换一套launcher主题。

实现效果:
这里写图片描述 这里写图片描述

分析:
1、 隐藏主菜单 ,google默认在android L 版本有一个隐藏主菜单的开关——LauncherAppState.isDisableAllApps()
返回 true 隐藏,返回 false 显示。
2、隐藏google search ,有好多方法。比较简单 ,本文将一笔带过
3、切换主题 上次有提过实现思路,具体看安装包性能优化,动态加载资源

下面是填坑之路
1、隐藏主菜单
首先设置 LauncherAppState.isDisableAllApps() 隐藏主菜单
具体原理的话
这里写图片描述
这个将会在Hotseat.java中根据上面条件进行判断是显示还是隐藏 ,Allapp其实就是一个TextView 当然仅仅这样是不够的 更细节的东西有时间的话在深入分析

坑一、通过上面的步骤的话 我们默认是把所有应用显示在了workspace上,然而用系统launcher的人都知道在workspace上默认只有移除动作的如下这里写图片描述

只有在主菜单中拖拽才有 应用信息和卸载操作,这个问题将直接导致,如果你想卸载应用只能到设置里面去卸载,在桌面上移除只是remove掉了 而它依然还存在 重启launcher就会发现这个应用又回来了,这就是个比较坑爹的事了,

解决办法:
首先找到顶部显示 uninstall和Appinfo的地方
通过分析最后发现是在ButtonDropTarget两个子类里面显示的
DeleteDropTarget和InfoDropTarget 看名字也知道他们是干什么的了,而ButtonDropTarget又实现了launcher定义的两个接口

public class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener

发现google工程师就是比较牛逼起名字都起的这么好。
DropTarget 监听目标的放置操作
DragController 拖拽事件的控制类

而我们会发现这两个类里面都有一个isVisible 通过分析发现这个就是控制launcher顶部的两个标签是否显示的开关。
在InfoDropTarget里面 我们发现

 if (!source.supportsAppInfoDropTarget()) {
            isVisible = false;
        }

而supportsAppInfoDropTarget 是一个接口的方法,它有三个实现类
这里写图片描述
只有是AppsCustomizePagedView时才返回true
另外两个都是false 所以我们知道了为什么在主菜单里能显示应用信息而桌面不能显示应用信息的原因了

所以这里把workspace的这个方法也修改为true

@Override
    public void onDragStart(DragSource source, Object info, int dragAction) {
        if (LauncherLog.DEBUG) {
            LauncherLog.d(TAG, "onDratStart: source = " + source + ", info = " + info
                    + ", dragAction = " + dragAction);
        }

        boolean isVisible = true;

        // Hide this button unless we are dragging something from AllApps
        //by lly for disableAllapp 20161112 start
         if(info instanceof FolderInfo){
            isVisible = false;
        }
        if(info instanceof LauncherAppWidgetInfo){
            isVisible = false;
        }
         if(info instanceof PendingAddWidgetInfo){
            isVisible = false;
        }
        //by lly for disableAllapp 20161112 end
        if (!source.supportsAppInfoDropTarget()) {
            isVisible = false;
        }
        ....
        }

然后在各个情况判断下 当是文件 或者桌面小部件或者从widgets视图中拖拽时都让它不显示appinfo
至此 InfoDropTarget 处理 就完成了

下面看下DeleteDropTarget的处理
其实和处理InfoDropTarget 类似,只是这里更繁琐一点 下面就直接上代码了

 @Override
    public void onDragStart(DragSource source, Object info, int dragAction) {
        boolean isVisible = true;
        //by lly for disableAllapp
        boolean useUninstallLabel =  isShortcut(source,info);//isAllAppsApplication(source, info);
        boolean useDeleteLabel = !useUninstallLabel && source.supportsDeleteDropTarget();

        // If we are dragging an application from AppsCustomize, only show the control if we can
        // delete the app (it was downloaded), and rename the string to "uninstall" in such a case.
        // Hide the delete target if it is a widget from AppsCustomize.
        //by lly for disableAllapp start
         if(info instanceof ShortcutInfo){
            try{
                ShortcutInfo appInfo = (ShortcutInfo) info;
                PackageManager packageManager = getContext().getPackageManager();
                ApplicationInfo ai = packageManager.getApplicationInfo(appInfo.intent.getComponent().getPackageName(), 0); 
                        mIsSysApp = (ai.flags & ApplicationInfo.FLAG_SYSTEM)>0;
            } catch (NameNotFoundException e) {
            e.printStackTrace();
            }
            }
            //by lly for disableAllapp end
        if (!willAcceptDrop(info) || isAllAppsWidget(source, info)) {
            isVisible = false;
        }

  //by lly for disallapp 20161112 start
    private boolean isShortcut(DragSource source, Object info) {
        return source.supportsAppInfoDropTarget() && (info instanceof ShortcutInfo);
    }
    //by lly for disallapp 20161112 start

private boolean isAllAppsApplication(DragSource source, Object info) {
        return source.supportsAppInfoDropTarget() && (info instanceof AppInfo);
    }
 public static boolean willAcceptDrop(Object info) {
        if (info instanceof ItemInfo) {
            ItemInfo item = (ItemInfo) info;

            ...
            if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
                item instanceof ShortcutInfo) {
                if (LauncherAppState.isDisableAllApps()) {
                    ShortcutInfo shortcutInfo = (ShortcutInfo) info;
                    //by lly for disableAllapp start
                    if(!mIsSysApp){
                        shortcutInfo.flags = 1;
                    }
                    //by lly for disableAllapp end
                    return (shortcutInfo.flags & AppInfo.DOWNLOADED_FLAG) != 0;
                } else {
                    return true;
                }
            }
        }
        return false;
    }

看上面有注释的地方差不多就知道我是怎么做的了,首先我是让上面都显示uninstall
主要就是useUninstallLabel 为true

接着判读是否是系统应用 因为系统应用是不能卸载的

if(info instanceof ShortcutInfo){
            try{
                ShortcutInfo appInfo = (ShortcutInfo) info;
                PackageManager packageManager = getContext().getPackageManager();
                ApplicationInfo ai = packageManager.getApplicationInfo(appInfo.intent.getComponent().getPackageName(), 0); 
                        mIsSysApp = (ai.flags & ApplicationInfo.FLAG_SYSTEM)>0;
            } catch (NameNotFoundException e) {
            e.printStackTrace();
            }
            }

根据是否是系统应用来确定 flags

从而控制isVisible的值
至此 显示uninstall已经出来完成
接下来看功能即 拖拽到uninstall标签怎么删除

 private void completeDrop(DragObject d) {
        ...
        if (LauncherLog.DEBUG) {
            LauncherLog.d(TAG, "completeDrop: item = " + item + ", d = " + d);
        }
        if (isAllAppsApplication(d.dragSource, item)) {
          ...
        } else if (isUninstallFromDisableAllApp(d)) {//by lly for 
            ...

                mWaitingForUninstall = mLauncher.startApplicationUninstallActivity(
                        componentName, shortcut.flags, user);
        }
     }

 private boolean isUninstallFromWorkspace(DragObject d) {
        if (LauncherAppState.isDisableAllApps() && isWorkspaceOrFolderApplication(d)) {
            ShortcutInfo shortcut = (ShortcutInfo) d.dragInfo;
            // Only allow manifest shortcuts to initiate an un-install.
            return !InstallShortcutReceiver.isValidShortcutLaunchIntent(shortcut.intent);
        }
        return false;
    }
    //by lly for disableAllapp start
      private boolean isUninstallFromDisableAllApp(DragObject d) {
        if (d.dragInfo instanceof LauncherAppWidgetInfo) {

            return false;
        }
        return true;
    }
    //by lly for disableAllapp end

依然是重写判读条件 没什么好说的
到这一步基本功能 就已经实现 但是操作的话发现拖拽到标签上松手 图标消失了 不管是appinfo标签还是uninstall标签
这里直接给出答案 他不是消失之时被隐藏了你拖拽个别的图标那个位置他就又出现了 但这确实是个bug 怎么解呢

/** Indicates that the drag operation was cancelled */
    public boolean cancelled = false;

 if (componentName != null) {
            mLauncher.startApplicationDetailsActivity(componentName, user);
        }
        d.cancelled = true;//by lly

当显示应用信息的时候直接让cancelled 为true表示取消这个拖拽事件就OK了

在DeleteDropTarget中 把animateToTrashAndCompleteDrop(d)提到acceptDrop里来操作

 @Override
    public boolean acceptDrop(DragObject d) {
        //by lly for disableAllapp start
            animateToTrashAndCompleteDrop(d);
            if(isUninstallFromDisableAllApp(d)){
                d.cancelled = true;
            }
        return false;//willAcceptDrop(d.dragInfo);
      //by lly for disableAllapp end
    }

到这里坑一就填平了,测试了一下暂时没有发现这样修改有新坑出现。

坑二
通过google提供的方法隐藏主菜单后 1、按菜单键桌面小部件界面出现不了,2、 当长按桌面空白处进入小部件界面然后返回 界面显示异常,3、拖拽小部件到桌面松开手后显示异常。

这个就不买关子了直接说明原因,至于我为什么知道你没看这都星期几了 我还在解bug么这里写图片描述这里写图片描述
因为按google这样设置后可以看到

void resetLayout() {
        mContent.removeAllViewsInLayout();

        if (!LauncherAppState.isDisableAllApps()) {
            ...
            if (mLauncher != null) {
                allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
                mLauncher.setAllAppsButton(allAppsButton);
                allAppsButton.setOnClickListener(mLauncher);
                allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler);
            }

           ...
        }
    }

这个地方主要就是那个allapp菜单添加的地方但是那个对我们分析来说没什么用 主要是看

 mLauncher.setAllAppsButton(allAppsButton);

这里把这个菜单设置给了launcher


    /**
     * Sets the all apps button. This method is called from {@link Hotseat}.
     */
    public void setAllAppsButton(View allAppsButton) {
        mAllAppsButton = allAppsButton;
    }

    public View getAllAppsButton() {
        return mAllAppsButton;
    }

然后通过getAllAppsButton得到这个mAllAppsButton最后在showAppsCustomizeHelper和hideAppsCustomizeHelper里面用到了这个mAllAppsButton其实是个TextView 然而 我们设置隐藏主菜单后 这个mAllAppsButton是空的所以导致了这个问题,那么知道了原因解决就很好办了 。

 // If for some reason our views aren't initialized, don't animate
       // boolean initialized = getAllAppsButton() != null;

        if (animated /*&& initialized*/) {
        ...
        }

直接不要他了不就可以。 当然下面几个地方也要一并去掉。

至此坑二也填平了

2、隐藏launcher上方默认的google search;
3、切换一套launcher主题。

这两个需求比较简单就不再分析了 如有需要请私信给我。

猜你喜欢

转载自blog.csdn.net/asd1031/article/details/53140343