12/14
2016

Third-party launcher how to support Android 7.1 shortcuts feature

Android SDK 7.1 allows App custom Shortcuts, similar to the iOS 3D touch. It will show a shortcut list by long pressing the App in the launcher, click a shortcut to quickly begin an operation, while Shortcut can be dragged to the launcher for fixed.

 

At present, only 7.1 system launcher support this feature, the third-party launcher can support this feature through the LauncherApps API, this article describes how the third-party launcher access to this feature.
You can download demo ShortcutViewer from Google Play. Screenshots are as follows:
Android Shortcuts Demo
A comprehensive introduction to Shortcuts can be found at:
The first: Android 7.1 new features Shortcuts introduced
The second: Android 7.1 new features Shortcuts some practice and current problems
If you do not understand the basic use of Shortcuts, you can look at the first article above.

 

1. Manifest supports Home category

Add the category android.intent.category.HOME to the Main&Launcher Activity in AndroidManifest.xml, which represents this is a launcher app, as follows:

<application
    ……>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </activity>
</application>

 

2. Set as the default launcher

The Android SDK LauncherApps API requires that only the default launcher be granted access to all application Shortcuts information.

 

Open theSettings-Apps-Configure apps-Home app, select your own application as the default launcher.

 

3. Get all the Shortcuts information for each App

Get the App Shortcuts information via LauncherApps.getShortcuts:

LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
if (!launcherApps.hasShortcutHostPermission()) {
    // Don't have permission, you may need set this app as default launcher.
    return;
}

PackageManager packageManager = context.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfoList;
if (packageManager == null || CollectionUtils
        .isEmpty(resolveInfoList = packageManager.queryIntentActivities(mainIntent, 0))) {
    // No Main&Launcher Activity
    return;
}

// Get ShortcutInfo for every app
Set<String> packageNameSet = new HashSet<>();
for (ResolveInfo info : resolveInfoList) {
    ApplicationInfo applicationInfo;
    if (info == null || info.activityInfo == null
            || (applicationInfo = info.activityInfo.applicationInfo) == null || !applicationInfo.enabled
            || packageNameSet.contains(applicationInfo.packageName)) {
        continue;
    }
    packageNameSet.add(applicationInfo.packageName);

    int queryFlags = ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_MANIFEST
            | ShortcutQuery.FLAG_MATCH_PINNED;
    List<ShortcutInfo> shortcutInfoList = launcherApps.getShortcuts(
            new ShortcutQuery().setPackage(applicationInfo.packageName).setQueryFlags(queryFlags),
            UserHandle.getUserHandleForUid(applicationInfo.uid));
    ……
}

The main steps above include:
(1) Check whether has permission to shortcuts information through LauncherApps.hasShortcutHostPermission ();
(2) Gets all installed applications with the ACTION_MAIN & CATEGORY_LAUNCHER Intent via PackageManager.queryIntentActivities (...);
(3) Traverse each eligible ResolveInfo and get its shortcuts via LauncherApps.getShortcuts (...).

 

Note: There are other ways to get all the ApplicationInfos that can show on the launcher, but the performance is better via PackageManager.queryIntentActivities (...).

 

int queryFlags = ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_MANIFEST
        | ShortcutQuery.FLAG_MATCH_PINNED;
List<ShortcutInfo> shortcutInfoList = launcherApps.getShortcuts(
        new ShortcutQuery().setPackage(applicationInfo.packageName).setQueryFlags(queryFlags),
        UserHandle.getUserHandleForUid(applicationInfo.uid));

LauncherApps.getShortcuts (LauncherApps.ShortcutQuery query, UserHandle user), the two parameters are query and UserHandle that the queried App corresponding.
The queryFlags above means both Dynamic Shortcuts, Static Shortcuts, Pinned Shortcts.

 

Of course, this feature is only valid for Android SDK 7.1 and above, so it is best to check the system API version before calling the LauncherApps related API.

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

One thought on “Third-party launcher how to support Android 7.1 shortcuts feature