Android M 去除抽屉模式

本文围绕Launcher3去掉抽屉模式展开,介绍了设置总开关、去掉allapp键加载、更改hotseat布局、将所有应用放第一层、新应用添加时更新workspace、去掉长按时删除选项以及解决添加新应用产生快捷方式等问题的具体操作和代码位置,均与Android开发相关。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 一、设置总开关

1、添加一个开关,控制是否去掉抽屉。

 launcher3\src\com\android\launcher3\launcherappstate.java:

public static boolean isDisableAllApps() {
        // Returns false on non-dogfood builds.
        return android.os.SystemProperties.get("ro.magcomm.launcher3.ishome2","0").equals("1");
    }

 二、allapp键的加载

        在hotseat里面去掉allapp键的加载 ,屏蔽isallappsbuttonrank()占用allapp位置。

        1) 不再占用allapp位置

        2) 在加载workspace时,会留出hotseat的第三个位置给allapp按钮,若不取消该位置的占用,在hotseat加载时会留出空位。hotseat的初始化在hotseat.java中

        launcher3\src\com\android\launcher3\hotseat.java c>isallappsbuttonrank():

public boolean isAllAppsButtonRank(int rank) {
        //add by liaoxiaomin begin @{
        if (LauncherAppState.isDisableAllApps()) {
            return false;
        }
        //add by liaoxiaomin end @}
        return rank == mAllAppsButtonRank;
    }

3) home2没有抽屉,所以不需要allapp按钮。在hotseat里面去掉allapp键的加载,在hotseat.java 的void resetlayout()中初始化hotseat布局。在home2时停止加载allapp按钮。

        launcher3\src\com\android\launcher3\hotseat.java c>resetlayout():

void resetlayout() {
    mcontent.removeallviewsinlayout();
    //添加 @{
    if(launcherappstate.isdisableallapps()){
    //添加 }@
    // add the apps button
    context context = getcontext();
    layoutinflater inflater = layoutinflater.from(context);
    textview allappsbutton = (textview)
        inflater.inflate(r.layout.all_apps_button, mcontent, false);
    drawable d = context.getresources().getdrawable(r.drawable.all_apps_button_icon);
    mlauncher.resizeicondrawable(d);
    allappsbutton.setcompounddrawables(null, d, null, null);
    allappsbutton.setcontentdescription(context.getstring(r.string.all_apps_button_label));
    allappsbutton.setonkeylistener(new hotseaticonkeyeventlistener());
    if (mlauncher != null) {
      mlauncher.setallappsbutton(allappsbutton);
      allappsbutton.setontouchlistener(mlauncher.gethapticfeedbacktouchlistener());
      allappsbutton.setonclicklistener(mlauncher);
      allappsbutton.setonlongclicklistener(mlauncher);
      allappsbutton.setonfocuschangelistener(mlauncher.mfocushandler);
    }
    // note: we do this to ensure that the hotseat is always laid out in the orientation of
    // the hotseat in order regardless of which orientation they were added
    int x = getcellxfromorder(mallappsbuttonrank);
    int y = getcellyfromorder(mallappsbuttonrank);
    celllayout.layoutparams lp = new celllayout.layoutparams(x,y,1,1);
    lp.canreorder = false;
    mcontent.addviewtocelllayout(allappsbutton, -1, allappsbutton.getid(), lp, true);
    }
}//别漏了这里的 }

三、数据初始化类中更改hotseat布局

        invariantdeviceprofile.java launcher3进行布局初始化的一个类。

        在有allapp按钮时hotseat里hotseat图标数量为五个,没有allapp按钮时hotseat图标数量应为四个。

        launcher3\src\com\android\launcher3\invariantdeviceprofile.java:

        1)先加个宏控

//添加 @{
private boolean hasda = launcherappstate.isdisableallapps();
//添加 }@

 2)去掉抽屉时,hotseat的格数为四格,所以不能抛出异常。 ( numhotseaticons 为偶时不抛异常)

        invariantdeviceprofile( ):

invariantdeviceprofile(string n, float w, float h, int r, int c, int fr, int fc, int maapc,
    // ensure that we have an odd number of hotseat items (since we need to place all apps)
    if (hs % 2 == 0&& !hasda) {// 在无抽屉情况下不抛异常
      throw new runtimeexception("all device profiles must have an odd number of hotseat spaces");
    }
    name = n;
     ...  ...
}

3)去掉抽屉的情况下加载不同的布局

        getpredefineddeviceprofiles() :

ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles() {
        ArrayList<InvariantDeviceProfile> predefinedDeviceProfiles = new ArrayList<>();
        // width, height, #rows, #columns, #folder rows, #folder columns,
        // iconSize, iconTextSize, #hotseat, #hotseatIconSize, defaultLayoutId.
        //modify by liaoxiaomin begin
        /*predefinedDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby",
                255, 300,     2, 3, 2, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby",
                255, 400,     3, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby",
                275, 420,     3, 4, 3, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Stubby",
                255, 450,     3, 4, 3, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus S",
                296, 491.33f, 4, 4, 4, 4, 4, 48, 13, 5, 48, R.xml.default_workspace_4x4));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 4",
                335, 567,     4, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 5",
                359, 567,     4, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, 5, 56, R.xml.default_workspace_4x4));*/
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Super Short Stubby",
                255, 300,     2, 3, 2, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Shorter Stubby",
                255, 400,     3, 3, 3, 3, 3, 48, 13, 3, 48, R.xml.default_workspace_4x4));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Short Stubby",
                275, 420,     3, 4, 3, 4, 4, 48, 13, (hasDA ? 4 : 5), 48, (hasDA ? R.xml.default_workspace_4x4_no_all_apps : R.xml.default_workspace_4x4 )));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Stubby",
                255, 450,     3, 4, 3, 4, 4, 48, 13, (hasDA ? 4 : 5), 48, (hasDA ? R.xml.default_workspace_4x4_no_all_apps : R.xml.default_workspace_4x4 )));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus S",
                296, 491.33f, 4, 4, 4, 4, 4, 48, 13,(hasDA ? 4 : 5), 48, (hasDA ? R.xml.default_workspace_4x4_no_all_apps : R.xml.default_workspace_4x4 )));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 4",
                335, 567,     4, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13, (hasDA ? 4 : 5), 56, (hasDA ? R.xml.default_workspace_4x4_no_all_apps : R.xml.default_workspace_4x4 )));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 5",
                359, 567,     4, 4, 4, 4, 4, DEFAULT_ICON_SIZE_DP, 13,(hasDA ? 4 : 5), 56, (hasDA ? R.xml.default_workspace_4x4_no_all_apps : R.xml.default_workspace_4x4 )));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Large Phone",
                406, 694,     5, 5, 4, 4, 4, 64, 14.4f,  5, 56, R.xml.default_workspace_5x5));
        //modify by liaoxiaomin end
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Large Phone",
                406, 694,     5, 5, 4, 4, 4, 64, 14.4f,  5, 56, R.xml.default_workspace_5x5));
        // The tablet profile is odd in that the landscape orientation
        // also includes the nav bar on the side
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 7",
                575, 904,     5, 6, 4, 5, 4, 72, 14.4f,  7, 60, R.xml.default_workspace_5x6));
        // Larger tablet profiles always have system bars on the top & bottom
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("Nexus 10",
                727, 1207,    5, 6, 4, 5, 4, 76, 14.4f,  7, 64, R.xml.default_workspace_5x6));
        predefinedDeviceProfiles.add(new InvariantDeviceProfile("20-inch Tablet",
                1527, 2527,   7, 7, 6, 6, 4, 100, 20,  7, 72, R.xml.default_workspace_4x4));
        return predefinedDeviceProfiles;
    }
    

4)记得改下 dw_phone_hotseat.xml 的布局 ,因为hotseat只有5格了。

 四、将所有应用放在第一层

        launcher3加载流程:进入 launcherapplication -> launcherappstate -> 进行初始化环境(通过传递scontext)。进行事件监听&&初始化一些环境。例如:横竖屏、当局语言、像素密度、小部件和快捷图标数据库操作对象、应用图标缓存对象、初始化launchermode等。在初始化过后,从launcher的oncreate方法入手。mmodel.startloader(mworkspace.getrestorepage());里加载数据 。在加载完成所有快捷方式后将其余为加载完的应用布局在第一层。

        1) 成所有快捷方式后将其余为加载完的应用布局在第一层。

        launcher3\src\com\android\launcher3\launchermodel.java:

        launchermodel$loadertask c> run():

public void run() {
            synchronized (mLock) {
                if (DEBUG_LOADERS) {
                    LauncherLog.d(TAG, "Set load task running flag >>>>, mIsLaunching = " +
                            ",this = " + this);
                }

                if (mStopped) {
                    return;
                }
                mIsLoaderTaskRunning = true;
            }
            // Optimize for end-user experience: if the Launcher is up and // running with the
            // All Apps interface in the foreground, load All Apps first. Otherwise, load the
            // workspace first (default).
            keep_running: {
                if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
                loadAndBindWorkspace();

                if (mStopped) {
                    LauncherLog.i(TAG, "LoadTask break in the middle, this = " + this);
                    break keep_running;
                }

                waitForIdle();

                // second step
                if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
                loadAndBindAllApps();
                //add by liaoxiaomin begin  @{
                if (LauncherAppState.isDisableAllApps()) {
                    verifyApplications();
                }
                //add by liaoxiaomin end }@
            }

            // Clear out this reference, otherwise we end up holding it until all of the
            // callback runnables are done.
            mContext = null;

            synchronized (mLock) {
                // If we are still the last one to be scheduled, remove ourselves.
                if (mLoaderTask == this) {
                    mLoaderTask = null;
                }
                if (DEBUG_LOADERS) {
                    LauncherLog.d(TAG, "Reset load task running flag <<<<, this = " + this);
                }
                mIsLoaderTaskRunning = false;
                mHasLoaderCompletedOnce = true;
            }
        }
        //add by liaoxiaomin begin
        private void verifyApplications() {
            final Context context = mApp.getContext();

            // Cross reference all the applications in our apps list with items in the workspace
            ArrayList<ItemInfo> tmpInfos;
            ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
            synchronized (sBgLock) {
                for (AppInfo app : mBgAllAppsList.data) {
                    tmpInfos = getItemInfoForComponentName(app.componentName, app.user);
                    if (tmpInfos.isEmpty()) {
                        // We are missing an application icon, so add this to the workspace
                        added.add(app);
                        // This is a rare event, so lets log it
                        Log.e(TAG, "Missing Application on load: " + app);
                    }
                }
            }
            if (!added.isEmpty()) {
                addAndBindAddedWorkspaceItems(context, added);
            }
        }
        //add by liaoxiaomin end

 五、有新应用添加时更新workspace

        当安装新应用时,我们需要对左面更新,保证安装的应用添加在第一层上。

        launcher3\src\com\android\launcher3\launchermodel.java:

        launchermodel$packageupdatedtask c> run():

        if (added != null) {
                // add by liaoxiaomin begin @{
                if(LauncherAppState.isDisableAllApps()){
                    final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
                    addAndBindAddedWorkspaceItems(context, addedInfos);
                }else {
                    // add by liaoxiaomin end }@
                    addAppsToAllApps(context, added);
                }// add by liaoxiaomin
                for (AppInfo ai : added) {
                    addedOrUpdatedApps.put(ai.componentName, ai);
                }
            }

 六、去掉长按时的删除选项

        长按时,不该有删除选项 。

        deletedroptarget.java: 中更改长按时的监听,开始时直接屏蔽删除按钮,后来发现应用自身发出的快捷方式无法删除 所以做了如下处理。

        launcher3\src\com\android\launcher3\deletedroptarget.java c>supportsdrop():


    public static boolean supportsDrop(Object info) {
        //add by liaoxiaomin begin @{
        if (LauncherAppState.isDisableAllApps()) {
            if (info instanceof ShortcutInfo) {
                ShortcutInfo item = (ShortcutInfo) info;
                return item.itemType != LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
            }
            return info instanceof LauncherAppWidgetInfo;
        }
        //add by liaoxiaomin end }@
        return (info instanceof ShortcutInfo)
                || (info instanceof LauncherAppWidgetInfo)
                || (info instanceof FolderInfo);
    }

 七、解决添加新应用后使用产生快捷方式问题

Launcher3/src/com/android/launcher3/LauncherModel.java 

if (targetIntent != null && info.user.equals(user)) {
        String s = targetIntent.toUri(0);
        +                        //liaoxiaomin modify for launcher add item 20190514 begin
        +                        String mTargetComponentName = targetIntent.getComponent().toString();
        +                        if(intent.getComponent() != null && mTargetComponentName.equals(intent.getComponent().toString())) {
        +                            return true;
        +                        }
        +                        //liaoxiaomin modify for launcher add item 20190514 end
        if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) {
        return true;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值