Launcher3 custom desktop application [Android13 version]

Article directory

    • summary
    • Change default layout
    • Dynamically add apps to the home screen in code
    • summary

Summary

Launcher3 project address: AOSP source code/packages/apps/Launcher3

Launcher3 customizes desktop applications. I currently have two methods in practice:

  • Change the default layout file and specify which apps to add to the home screen
  • Dynamically add apps to the home screen in the code (including deleting the original app icon and adding new ones)

Change default layout

Tip: Which layout file should you find based on the screen size?

1. Enter the source code path of launcher3 layout (packages/apps/Launcher3/res/xml) and find the default_workspace_axb.xml to be modified.

Focus on: devices_profiles.xml. In this file, specify the default_workspace_axb.xml to be loaded.

A simpler method: After compiling aosp, run the simulator. On the launcher desktop, press and hold the icon and drag it. Number one grid by one, and you will know the layout of how many rows and columns are used in the current project.

For example, my device uses default_workspace_5x5.xml. When I open this file, it displays as follows:

The label specifies the application, where launcher:container=”-101″ represents the application added to hotseat, launcher:x=”0″ specifies the column, launcher:y=”0″ specifies the row, so in Here you can change the applications and their positional relationships in hotseat.

Look down:

Here without launcher:container=”-101″ represents the application in the workspace (home screen), so changing the content of launcher:uri here can change the application. At the same time, modify launcher:screen=”0″ to represent which screen it is on, and x and y to represent which position (-1 is the default value: first row or first column)

To sum up: after changing the layout file, recompile aosp and you will see that the application in the launcher screen has been modified.

If you modify the layout file, but run the simulator after recompiling, and find that the desktop application has not changed, it is possible that the launcher database has not been updated, because the contents of these layout files will be changed when the launcher is started for the first time. Create a database and save the data into it. Later, it can be used to read directly from the db without repeatedly reading the layout file
adb root
adb shell rm /data/data/com.android.launcher3/databases/launcher.db
Restart the simulator to take effect

The home screen application information recorded in the database is as follows:

Dynamically add app to home screen in code

has this function: the user selects several apps on the All apps interface (that is, the pull-up drawer interface) and needs to add them to the home screen. This can only be added dynamically through code

Modify the address:/packages/apps/Launcher3/src/com/android/launcher3/Launcher.java

Assume that there is already a list of applications to be added List and the corresponding response event changeWorkspaceItems

first step:
To delete the application in the original workspace, Launcher.java has provided the relevant method removeItem()

/**
* Launcher already provides a method to delete apps by default
* @param v The view of the application to be removed. The application icon is displayed in the form of BubbleTextView. For details, you can see how this class operates.
* @param itemInfo The application information to be removed, use this information to delete the corresponding element in the db
* @param deleteFromDb whether to delete data in the database
*/
public boolean removeItem(View v, final ItemInfo itemInfo, boolean deleteFromDb) {<!-- -->
        return removeItem(v, itemInfo, deleteFromDb, null);
}

(1) We only need to build a hash table to store the home screen application information and its corresponding view.
Create a new HashMap in Launcher.java and create the object in onCreate()
private Map homeIconMap;
Collect home screen application information into a hash table in the Launcher#bindItems() method

After you have the hash table, you can use this method when you need to delete it later.

(2) Create a new delete method removeWorkspaceItems in Launcher.java

public void removeWorkspaceItems(HashMap<ItemInfo, View> orignItemsMap){<!-- -->
Set<ItemInfo> itemInfos = orignItemsMap.keySet();
    for (ItemInfo info: itemInfos){<!-- -->
        View icon = orignItemsMap.get(info);
        // Call the native method in the launcher to delete the application. The bottom layer is to adjust the contentProvider to delete the corresponding data table information.
        removeItem(icon, info, true);
    }
}

Step 2: Create a new method addToWorkspace in Launcher.java

public void addToWorkspace(List<AppInfo> appInfoList){<!-- -->
//AppInfo is the corresponding data structure for each application in the all apps interface
//For details, please see:
// 1. LoaderTask#loadAllApps() Where do these all apps data come from?
// 2. How to add Launcher#bindAllApplications() to the all apps interface

// Get user information, the default is one
UserManager mUserManager = getSystemService(UserManager.class);
    final List<UserHandle> profiles = mUserManager.getUserProfiles();
\t
// Traverse the added collection and add it to the home screen
for(AppInfo appInfo: appInfoList){<!-- -->
// Get the package name
String packageName = appInfo.componentName.getPackageName();
ItemInstallQueue.INSTANCE.get(mContext)
                                .queueItem(packageName, profiles.get(0));
}

}

Step 3: Assemble the methods in steps 1 and 2 above into the add event: changeWorkspaceItems

public void changeWorkspaceItems(){<!-- -->
// 1 Delete the original application homeIconMap. We have already collected the information when Launcher started.
removeWorkspaceItems(homeIconMap);
\t
// 2 Add new application
// Generally you can get it through this: AppInfo[] allInfo = mModel.mBgAllAppsList.copyData();
List<AppInfo> listView = "The collection we prepared in advance"
addToWorkspace(listView);
}

As for the call of the changeWorkspaceItems method, it depends on the individual, such as writing a button click event to respond, etc.
After my troubles, changeWorkspaceItems should not be written in sub-threads, because: deletion and new methods will eventually be executed in the relevant handler, which itself is asynchronous, so do not bother to open sub-threads for execution. , otherwise an error may be reported.

Summary

Adding apps to the home screen works both ways I tried.
Method 2: Use code to dynamically add the app to the home screen. This is just the solution I found by reading the source code. There may be other solutions that can be implemented. Here is just a reference.

If you have any questions or suggestions, please feel free to leave a message to me. My skills are limited, so please feel free to let me know if I am wrong~~.