Android 7.0 Launcher3 去掉應用抽屜

年初作過一個項目,有一個需求就是須要將桌面變爲單層不須要二級菜單。最近幾回有小夥伴有這個問我這個解決辦法。如今我將分享給你們。java

先上效果圖:
這裏寫圖片描述 這裏寫圖片描述android

功能分解數據庫

  1. 去除Allapp鍵,調整HotSeat佈局
  2. 將全部應用擺在launcher第一層
  3. 去掉長按時刪除選項

解決方案緩存

一,設置總開關
按照6.0 Launcher3 的模式,添加一個開關,控制是否去掉抽屜。app

LauncherAppState類:單例模式,主要在啓動的時候用,他初始化了一些對象,而且註冊了廣播監聽器和ContentObserver。爲了能靈活切換模式,在此類中添加靜態開關。less

Launcher3\src\com\android\launcher3\LauncherAppState.java:ide

public static boolean isDisableAllApps() {
        // Returns false on non-dogfood builds.
        return false;
    }

二,Allapp鍵的加載
在HotSeat裏面去掉Allapp鍵的加載 ,屏蔽isAllAppsButtonRank()佔用allapp位置。佈局

1) 再也不佔用allapp位置
2) 在加載Workspace時,會留出HotSeat的第三個位置給allapp按鈕,若不取消該位置的佔用,在HotSeat加載時會留出空位。HotSeat的初始化在HotSeat.java中ui

Launcher3\src\com\android\launcher3\HotSeat.java –>isAllAppsButtonRank():this

public boolean isAllAppsButtonRank(int rank) {

        //添加 @{
        if (LauncherAppState.isDisableAllApps()) {
            return false;
            }
        //添加 @}
        return rank == mAllAppsButtonRank;
    }

3) Home2沒有抽屜,因此不須要allapp按鈕。在HotSeat裏面去掉Allapp鍵的加載,在HotSeat.java 的void resetLayout()中初始化HotSeat佈局。在Home2時中止加載Allapp按鈕。

Launcher3\src\com\android\launcher3\HotSeat.java –>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.
        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));
        // 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;
    }

5)記得改下 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 –> run():

public void run() {
            ... ...
            // 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();

                //添加 @{
                if (LauncherAppState.isDisableAllApps()) {
                    verifyApplications();
                }
                //添加 }@
            }

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

添加verifyApplications():

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);//7.0 雖然去掉了去抽屜的代碼,但留了這個方法給咱們。
        }
    }

五,有新應用添加時更新Workspace

當安裝新應用時,咱們須要對左面更新,保證安裝的應用添加在第一層上。

Launcher3\src\com\android\launcher3\LauncherModel.java:

LauncherModel$PackageUpdatedTask –> run():

public void run() {
            if (!mHasLoaderCompletedOnce) {
                // Loader has not yet run.
                return;
            }
            final Context context = mApp.getContext();
            ... ...
            if (added != null) {
                // 添加 @{
                if(LauncherAppState.isDisableAllApps()){
                        final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
                        addAndBindAddedWorkspaceItems(context, addedInfos);
                }else{
                // 添加 }@
                addAppsToAllApps(context, added);
                }
                for (AppInfo ai : added) {
                    addedOrUpdatedApps.put(ai.componentName, ai);
                }
            }
            ... ...
   }

六,去掉長按時的刪除選項

長按時,不應有刪除選項 。

DeleteDropTarget.java: 中更改長按時的監聽,開始時直接屏蔽刪除按鈕,後來發現應用自身發出的快捷方式沒法刪除 因此作了以下處理。

Launcher3\src\com\android\launcher3\DeleteDropTarget.java –>supportsDrop():

public static boolean supportsDrop(Object info) {


         //添加 @{
        if (LauncherAppState.isDisableAllApps()) {             
            if (info instanceof ShortcutInfo) {           
                ShortcutInfo item = (ShortcutInfo) info;               
                return item.itemType != LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;                  
            }
            return info instanceof LauncherAppWidgetInfo;
        }
        //添加 }@
        return (info instanceof ShortcutInfo)
                || (info instanceof LauncherAppWidgetInfo)
                || (info instanceof FolderInfo);
    }

寫在最後

到此,Launcher3去掉應用抽屜的改動已經完成。還有不少咱們須要去美化的,就好比HotSeat佈局自適應等。

相關文章
相關標籤/搜索