Android8.1 O1版本去掉应用抽屉

hi what's up!

    我是博主illa!

    今天给大家介绍一下在android O1版本 launcher3实现的 去掉应用抽屉!

    思路:第一步要实现去掉应用抽屉首先需要让所有的icon加载到桌面上不然用户无法看到所有的app

               第二步手动安装第三方app的时候需要add一个icon到桌面

               第三步去掉原先allapp的入口以及o1版本上滑的那个箭头还有hotseat抖动

    理清了思路做起来就比较顺手,其实app的信息主要是存储在一个ArrayList里面可以用

    LauncherAppsCompat.getActivityList()得到而我们要做的就是遍历这个list得到没有被预置在桌面上的

    应用信息并add到list里面再加载到桌面即可

    首先在Launcher3\src\com\android\launcher3\model\LoaderTask.java

    里的run方法中添加一个loadAllApplications()方法的调用,主要是循环遍历LauncherActivityInfo并通过

    LauncherModel的addAndBindAddedWorkspaceItems()方法添加到桌面上,其中主要涉及的知识就是

    循环遍历,代码如下

    

	private void loadAllApplications() {
	        final Context context = mApp.getContext();
			final List profiles = mUserManager.getUserProfiles();
			android.util.Log.e("gaoshifeng","111111111111111111111");
			for (UserHandle user : profiles) {
				final List apps = mLauncherApps.getActivityList(null, user);
				android.util.Log.e("gaoshifeng","22222222222222222");
				ArrayList added = new ArrayList();
				synchronized (this) {
					for (LauncherActivityInfo app : apps) {
							PendingInstallShortcutInfo pendingInstallShortcutInfo =  new PendingInstallShortcutInfo(app,context);
							added.add(pendingInstallShortcutInfo);
							android.util.Log.e("gaoshifeng","app ===== " + app);
							android.util.Log.e("gaoshifeng","333333333333333333333");
					}
				}
				if (!added.isEmpty()) {
					android.util.Log.e("gaoshifeng","4444444444444444444444444");
					mLauncherModel.addAndBindAddedWorkspaceItems(new LazyShortcutsProvider(context.getApplicationContext(), added));
				}
			}
	}

当时在加上这段代码之后,桌面的iocn还是一直加载不出来,检查了很多遍代码之后,苦思不得其解,只好加log将加载

流程一个个打印看看哪里出了问题。最后发现在Launcher3\src\com\android\launcher3\model\BaseModelUpdateTask.java

 

的run方法中

if (!mModel.isModelLoaded()) {

            if (DEBUG_TASKS) {
                Log.d(TAG, "Ignoring model task since loader is pending=" + this);
            }
            // Loader has not yet run.
            return;

  }

这里return掉了。

怎么办呢,解决方法其实很简单,注释掉即可。

然后再把icon拖动时的删除给干掉

Launcher3\src\com\android\launcher3\DeleteDropTarget.java中

    @Override
    protected boolean supportsDrop(DragSource source, ItemInfo info) {
//add by gaoshifeng start
        if (info instanceof ShortcutInfo) {           
            ShortcutInfo item = (ShortcutInfo) info;               
            return item.itemType != LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;                  
        }
        return info instanceof LauncherAppWidgetInfo;
        //return true;
//add by gaoshifeng end

    }

 

到这里第一步基本已经完成了,接下来进行第二步。当安装apk时,将icon加载到桌面上。

安装apk时其实走的是Launcher3\src\com\android\launcher3\model\PackageUpdatedTask.java

所以我们在这个task的run方法里面加上相应处理即可,具体代码跟上面加载到桌面的方法大同小异

不过要注意加在added这个ArrayList之前被clear之前

        addedOrModified.addAll(appsList.added);
		//add by gaoshifeng start
		final List profiles = UserManagerCompat.getInstance(context).getUserProfiles();
		ArrayList added = new ArrayList();
		for (UserHandle user : profiles) {
			final List apps = LauncherAppsCompat.getInstance(context).getActivityList(null, user);
			
			android.util.Log.e("gaoshifeng","+++++++++++++++");
			synchronized (this) {
				for (LauncherActivityInfo info : apps) {
						for (AppInfo appInfo : appsList.added) {
							if(info.getComponentName().equals(appInfo.componentName)){
								PendingInstallShortcutInfo mPendingInstallShortcutInfo =  new PendingInstallShortcutInfo(info,context);
								added.add(mPendingInstallShortcutInfo);
							}
						}
				}
			}
		}
        if (!added.isEmpty()) {
			android.util.Log.e("gaoshifeng","------------------------");
            app.getModel().addAndBindAddedWorkspaceItems(new LazyShortcutsProvider(context.getApplicationContext(), added));
        }
		//add by gaoshifeng end
        appsList.added.clear();

 

第三步去掉原先allapp的入口以及o1版本上滑的那个箭头还有hotseat抖动

 

Launcher3\src\com\android\launcher3\allapps\AllAppsTransitionController.java

滑动事件在onDragStart以及onDrag的时候 return掉即可

每次进入桌面hotseat抖动,其实是用BounceInterpolator来写的只要将

Launcher3\src\com\android\launcher3\Launcher.java中onresume的

        if (shouldShowDiscoveryBounce()) {
            //mAllAppsController.showDiscoveryBounce();delete by gaoshifeng

        }

delete掉即可

最后把箭头图标删除,

Launcher3/src/com/android/launcher3/pageindicators/PageIndicatorLineCaret.java 的 onFinishInflate()

add 以下这一行即可

mAllAppsHandle.setImageDrawable(null);

 

今天就到这里,希望以上代码可以对阅读者有所启发。我们来日方长!

 

 

 

 

 

 

        

    

你可能感兴趣的:(Android功能)