如何隐藏Launcher中live wallpapers入口

公司某产品性能有限,需要屏蔽live wallpapers防止内存使用量过大影响客户体验,于是按照经验在Launcher2目录寻找设置壁纸对话框对应的代码,前一个"Add to Home screen"Dialog对象创建位置轻松找到,在Launcher.java中有如下代码:

 

Java代码 复制代码
  1. if (mWorkspace.allowLongPress()) {   
  2.     if (cellInfo.cell == null) {   
  3.         if (cellInfo.valid) {   
  4.             // User long pressed on empty space   
  5.             mWorkspace.setAllowLongPress(false);   
  6.             mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,   
  7.                     HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);   
  8.             showAddDialog(cellInfo);   
  9.         }   
  10.     } else {   
  11.         if (!(cellInfo.cell instanceof Folder)) {   
  12.             // User long pressed on an item   
  13.             mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,   
  14.                     HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);   
  15.             mWorkspace.startDrag(cellInfo);   
  16.         }   
  17.     }   
  18. }  
        if (mWorkspace.allowLongPress()) {
            if (cellInfo.cell == null) {
                if (cellInfo.valid) {
                    // User long pressed on empty space
                    mWorkspace.setAllowLongPress(false);
                    mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
                            HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
                    showAddDialog(cellInfo);
                }
            } else {
                if (!(cellInfo.cell instanceof Folder)) {
                    // User long pressed on an item
                    mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
                            HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
                    mWorkspace.startDrag(cellInfo);
                }
            }
        }

 

继续寻找showAddDialog(cellInfo):

 

 

Java代码 复制代码
  1. private void showAddDialog(CellLayout.CellInfo cellInfo) {   
  2.     mAddItemCellInfo = cellInfo;   
  3.     mWaitingForResult = true;   
  4.     showDialog(DIALOG_CREATE_SHORTCUT);   
  5. }  
    private void showAddDialog(CellLayout.CellInfo cellInfo) {
        mAddItemCellInfo = cellInfo;
        mWaitingForResult = true;
        showDialog(DIALOG_CREATE_SHORTCUT);
    }

 

代码携带参数跳入Launcher.java的父类Activity.java的showDialog()方法,在Activity.java源码中查找,发现接下来将会跳入Launcher.java的onCreateDialog(int id)方法,代码如下:

 

 

Java代码 复制代码
  1. @Override  
  2. protected Dialog onCreateDialog(int id) {   
  3.     switch (id) {   
  4.         case DIALOG_CREATE_SHORTCUT:   
  5.             return new CreateShortcut().createDialog();   
  6.         case DIALOG_RENAME_FOLDER:   
  7.             return new RenameFolder().createDialog();   
  8.     }   
  9.   
  10.     return super.onCreateDialog(id);   
  11. }  
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_CREATE_SHORTCUT:
                return new CreateShortcut().createDialog();
            case DIALOG_RENAME_FOLDER:
                return new RenameFolder().createDialog();
        }

        return super.onCreateDialog(id);
    }

 

显然,这里return 的是case  DIALOG_CREATE_SHORTCUT,再进入私有类CreateShortcut

 

 

Java代码 复制代码
  1. Dialog createDialog() {   
  2.     mWaitingForResult = true;   
  3.   
  4.     mAdapter = new AddAdapter(Launcher.this);   
  5.   
  6.     final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);   
  7.     builder.setTitle(getString(R.string.menu_item_add_item));   
  8.     builder.setAdapter(mAdapter, this);   
  9.   
  10.     builder.setInverseBackgroundForced(true);   
  11.   
  12.     AlertDialog dialog = builder.create();   
  13.     dialog.setOnCancelListener(this);   
  14.     dialog.setOnDismissListener(this);   
  15.     dialog.setOnShowListener(this);   
  16.   
  17.     return dialog;   
  18. }  
        Dialog createDialog() {
            mWaitingForResult = true;

            mAdapter = new AddAdapter(Launcher.this);

            final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
            builder.setTitle(getString(R.string.menu_item_add_item));
            builder.setAdapter(mAdapter, this);

            builder.setInverseBackgroundForced(true);

            AlertDialog dialog = builder.create();
            dialog.setOnCancelListener(this);
            dialog.setOnDismissListener(this);
            dialog.setOnShowListener(this);

            return dialog;
        }

 

至此,看到了第一个"Add to Home screen"Dialog的初始化位置,进入AddAdapter的构造器,里面有如下代码:

 

 

Java代码 复制代码
  1. mItems.add(new ListItem(res, R.string.group_shortcuts,   
  2.         R.drawable.ic_launcher_shortcut, ITEM_SHORTCUT));   
  3.   
  4. mItems.add(new ListItem(res, R.string.group_widgets,   
  5.         R.drawable.ic_launcher_appwidget, ITEM_APPWIDGET));   
  6.   
  7. mItems.add(new ListItem(res, R.string.group_live_folders,   
  8.         R.drawable.ic_launcher_folder, ITEM_LIVE_FOLDER));   
  9.   
  10. mItems.add(new ListItem(res, R.string.group_wallpapers,   
  11.         R.drawable.ic_launcher_wallpaper, ITEM_WALLPAPER));  
        mItems.add(new ListItem(res, R.string.group_shortcuts,
                R.drawable.ic_launcher_shortcut, ITEM_SHORTCUT));

        mItems.add(new ListItem(res, R.string.group_widgets,
                R.drawable.ic_launcher_appwidget, ITEM_APPWIDGET));
        
        mItems.add(new ListItem(res, R.string.group_live_folders,
                R.drawable.ic_launcher_folder, ITEM_LIVE_FOLDER));
        
        mItems.add(new ListItem(res, R.string.group_wallpapers,
                R.drawable.ic_launcher_wallpaper, ITEM_WALLPAPER));

 

如果要对"Add to Home screen"对话框下手,此处正当其位.

然而,我的目标却不是它,继续往下走,郁闷的事情发生了,在"Add to Home screen"Dialog的ItemClick事件处理中,只有如下代码:

 

 

Java代码 复制代码
  1. case AddAdapter.ITEM_WALLPAPER: {   
  2.     startWallpaper();   
  3.     break;  
                case AddAdapter.ITEM_WALLPAPER: {
                    startWallpaper();
                    break;

 

再看startWallpaper()方法

 

 

Java代码 复制代码
  1.     private void startWallpaper() {   
  2.         closeAllApps(true);   
  3.         final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);   
  4.         Intent chooser = Intent.createChooser(pickWallpaper,   
  5.                 getText(R.string.chooser_wallpaper));   
  6.         // NOTE: Adds a configure option to the chooser if the wallpaper supports it   
  7.         //       Removed in Eclair MR1   
  8. //        WallpaperManager wm = (WallpaperManager)   
  9. //                getSystemService(Context.WALLPAPER_SERVICE);   
  10. //        WallpaperInfo wi = wm.getWallpaperInfo();   
  11. //        if (wi != null && wi.getSettingsActivity() != null) {   
  12. //            LabeledIntent li = new LabeledIntent(getPackageName(),   
  13. //                    R.string.configure_wallpaper, 0);   
  14. //            li.setClassName(wi.getPackageName(), wi.getSettingsActivity());   
  15. //            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });   
  16. //        }   
  17.         startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);   
  18.     }  
    private void startWallpaper() {
        closeAllApps(true);
        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
        Intent chooser = Intent.createChooser(pickWallpaper,
                getText(R.string.chooser_wallpaper));
        // NOTE: Adds a configure option to the chooser if the wallpaper supports it
        //       Removed in Eclair MR1
//        WallpaperManager wm = (WallpaperManager)
//                getSystemService(Context.WALLPAPER_SERVICE);
//        WallpaperInfo wi = wm.getWallpaperInfo();
//        if (wi != null && wi.getSettingsActivity() != null) {
//            LabeledIntent li = new LabeledIntent(getPackageName(),
//                    R.string.configure_wallpaper, 0);
//            li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
//            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
//        }
        startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
    }

 

由于对Intent的粗浅了解,很自然的,我直奔Launcher中注册有ACTION_SET_WALLPAPER的Activity--WallpaperChooser.java,悲剧的是,迎接我的,却是选择壁纸的初始化界面...

另一个dialog的初始化代码神秘的消失了...

 

我跳到Launcher的资源文件目录,想从strings.xml和drawable中找到第二个对话框引用的资源文件,然后所有资源名,找到加载资源的代码,但是,一个都没找到.....反倒在package目录的Wallpaper中找到.

百思不得其解折腾了一小时后,郁闷的上洗手间的时候我终于想到了它的原理:

原来,这第二个dialog并不属于Launcher应用,而是系统接收到这个叫chooser的intent后,查找所有能匹配的Activity得到结果,第二个dialog中的图片文字,在launcher的资源文件中也是没有的,这也是我在Launcher的strings.xml找不到线索的原因.幽默的是,我上洗手间之前,决定删除live Wallpaper对应的apk源码做尝试,而实际上,这就是切合原理的解决之道,而不会引起异常,当然,还有另一种方法,修改live wallpaper应用的intent注册内容也可以做到.

下午复查了下所有wallpaper app,果然都有

<action android:name="android.intent.action.SET_WALLPAPER" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值