Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ Using Custom Tabs with Android 11

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š Using Custom Tabs with Android 11


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: developers.google.com

Using Custom Tabs with Android 11

Android 11 introduced changes on how apps can interact with other apps that the user has installed on the device. You can read more about those changes on Android documentation.

When an Android app using Custom Tabs targets SDK level 30 or above some changes may be necessary. This article goes over the changes that may be needed for those apps.

In the simplest case, Custom Tabs can be launched with a one-liner like so:

new CustomTabsIntent.Builder().build()
        .launchUrl(this, Uri.parse("https://www.example.com"));

Applications launching applications using this approach, or even adding UI customizations like changing the toolbar color, adding an action button wonโ€™t need to do any changes in the application.

Preferring Native Apps

But, if you followed the best practices some changes may be required.

The first relevant best practice is that applications should prefer a native app to handle the intent instead of a Custom Tab if an app that is capable of handling it is installed.

On Android 11 and above

Android 11 introduces a new Intent flag, FLAG_ACTIVITY_REQUIRE_NON_BROWSER, which is the recommended way to try opening a native app, as it doesnโ€™t require the app to declare any package manager queries.

static boolean launchNativeApi30(Context context, Uri uri) {
    Intent nativeAppIntent = new Intent(Intent.ACTION_VIEW, uri)
            .addCategory(Intent.CATEGORY_BROWSABLE)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER);
    try {
        context.startActivity(nativeAppIntent);
        return true;
    } catch (ActivityNotFoundException ex) {
        return false;
    }
}

The solution is to try to launch the Intent and use FLAG_ACTIVITY_REQUIRE_NON_BROWSER to ask Android to avoid browsers when launching.

If a native app that is capable of handling this Intent is not found, an ActivityNotFoundException will be thrown.

Before Android 11

Even though the application may target Android 11, or API level 30, previous Android versions will not understand the FLAG_ACTIVITY_REQUIRE_NON_BROWSER flag, so we need to resort to querying the Package Manager in those cases:

private static boolean launchNativeBeforeApi30(Context context, Uri uri) {
    PackageManager pm = context.getPackageManager();

    // Get all Apps that resolve a generic url
    Intent browserActivityIntent = new Intent()
            .setAction(Intent.ACTION_VIEW)
            .addCategory(Intent.CATEGORY_BROWSABLE)
            .setData(Uri.fromParts("http", "", null));
    Set<String> genericResolvedList = extractPackageNames(
            pm.queryIntentActivities(browserActivityIntent, 0));

    // Get all apps that resolve the specific Url
    Intent specializedActivityIntent = new Intent(Intent.ACTION_VIEW, uri)
            .addCategory(Intent.CATEGORY_BROWSABLE);
    Set<String> resolvedSpecializedList = extractPackageNames(
            pm.queryIntentActivities(specializedActivityIntent, 0));

    // Keep only the Urls that resolve the specific, but not the generic
    // urls.
    resolvedSpecializedList.removeAll(genericResolvedList);

    // If the list is empty, no native app handlers were found.
    if (resolvedSpecializedList.isEmpty()) {
        return false;
    }

    // We found native handlers. Launch the Intent.
    specializedActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(specializedActivityIntent);
    return true;
}

The approach used here is to query the Package Manager for applications that support a generic http intent. Those applications are likely browsers.

Then, query for applications that handle itents for the specific URL we want to launch. This will return both browsers and native applications setup to handle that URL.

Now, remove all browsers found on the first list from the second list, and weโ€™ll be left only with native apps.

If the list is empty, we know there are no native handlers and return false. Otherwise, we launch the intent for the native handler.

Putting it all together

We need to ensure using the right method for each occasion:

static void launchUri(Context context, Uri uri) {
    boolean launched = Build.VERSION.SDK_INT >= 30 ?
            launchNativeApi30(context, uri) :
            launchNativeBeforeApi30(context, uri);

    if (!launched) {
        new CustomTabsIntent.Builder()
                .build()
                .launchUrl(context, uri);
    }
}

Build.VERSION.SDK_INT provides the information we need. If itโ€™s equal or larger than 30, Android knows the FLAG_ACTIVITY_REQUIRE_NON_BROWSER and we can try launching a nativa app with the new approach. Otherwise, we try launching with the old approach.

If launching a native app fails, we then launch a Custom Tabs.

Thereโ€™s some boilerplate involved in this best practice. Weโ€™re working on making this simpler by encapsulating the complexity in a library. Stay tuned for updates to the android-browser-helper support library.

Detecting browsers that support Custom Tabs

Another common pattern is to use the PackageManager to detect which browsers support Custom Tabs on the device. Common use-cases for this are setting the package on the Intent to avoid the app disambiguation dialog or choosing which browser to connect to when connecting to the Custom Tabs service.

When targeting API level 30, developers will need to add a queries section to their Android Manifest, declaring an intent-filter that matches browsers with Custom Tabs support.

<queries>
    <intent>
        <action android:name=
            "android.support.customtabs.action.CustomTabsService" />
    </intent>
</queries>

With the markup in place, the existing code used to query for browsers that support Custom Tabs will work as expected.

Frequently Asked Questions

Q: The code that looks for Custom Tabs providers queries for applications that can handle https:// intents, but the query filter only declares an android.support.customtabs.action.CustomTabsService query. Shouldnโ€™t a query for https:// intents be declared?

A: When declaring a query filter, it will filter the responses to a query to the PackageManager, not the query itself. Since browsers that support Custom Tabs declare handling the CustomTabsService, they wonโ€™t be filtered out. Browsers that donโ€™t support Custom Tabs will be filtered out.

Conclusion

Those are all the changes required to adapt an existing Custom Tabs integration to work with Android 11. To learn more about integrating Custom Tabs into an Android app, start with the implementation guide then check out the best practices to learn about building a first-class integration.

Let us know if you have any questions or feedback!

...



๐Ÿ“Œ Too manys tabs are never enough as Vivaldi stacks tabs on tabs


๐Ÿ“ˆ 33.94 Punkte

๐Ÿ“Œ Using Custom Tabs with Android 11


๐Ÿ“ˆ 27.92 Punkte

๐Ÿ“Œ Google search bug freezes tabs when using a custom date range


๐Ÿ“ˆ 25.41 Punkte

๐Ÿ“Œ How you can create your own custom chatbot with your own custom data using Google Gemini API all for free


๐Ÿ“ˆ 23.03 Punkte

๐Ÿ“Œ Android: Google bringt neuen Browser in die Google-App und verabschiedet sich von den Chrome Custom Tabs


๐Ÿ“ˆ 22.76 Punkte

๐Ÿ“Œ Android: Google testet neue Browser-Oberflรคche als Ersatz fรผr die Chrome Custom Tabs (Screenshots)


๐Ÿ“ˆ 22.76 Punkte

๐Ÿ“Œ Android: Google testet neue Browser-Oberflรคche als Alternative zu den Chrome Custom Tabs (Screenshots)


๐Ÿ“ˆ 22.76 Punkte

๐Ÿ“Œ Android: Google Chrome erhรคlt schwebende Custom Tabs โ€“ neue Bild-in-Bild-Funktion im Test (Screenshots)


๐Ÿ“ˆ 22.76 Punkte

๐Ÿ“Œ 400 Browser-Tabs auf einmal? Firefox-Alternative lรคsst Sie Tabs รผbereinanderstapeln


๐Ÿ“ˆ 22.62 Punkte

๐Ÿ“Œ Google Contemplating Removing Chrome 'Close Other Tabs' and 'Close Tabs to the Right' Options


๐Ÿ“ˆ 22.62 Punkte

๐Ÿ“Œ Chrome May Soon Lose "Close Other Tabs" and "Close Tabs to the Right" Options


๐Ÿ“ˆ 22.62 Punkte

๐Ÿ“Œ 400 Browser-Tabs auf einmal? Firefox-Alternative lรคsst Sie Tabs รผbereinanderstapeln


๐Ÿ“ˆ 22.62 Punkte

๐Ÿ“Œ Microsoft Edge to Let You Set Custom Backgrounds for New Tabs


๐Ÿ“ˆ 20.24 Punkte

๐Ÿ“Œ CVE-2022-3310 | Microsoft Edge Custom Tabs Remote Code Execution


๐Ÿ“ˆ 20.24 Punkte

๐Ÿ“Œ CVE-2022-3310 | Google Chrome prior 106.0.5249.61 Custom Tabs Remote Code Execution


๐Ÿ“ˆ 20.24 Punkte

๐Ÿ“Œ CVE-2022-3447 | Google Chrome prior 106.0.5249.119 Custom Tabs Remote Code Execution


๐Ÿ“ˆ 20.24 Punkte

๐Ÿ“Œ Chrome now lets you minimize Custom Tabs in PiP mode, boosts multitasking


๐Ÿ“ˆ 20.24 Punkte

๐Ÿ“Œ Google Discover Feed: Chrome Custom Tabs haben jetzt einen separaten Teilen-Button


๐Ÿ“ˆ 20.24 Punkte

๐Ÿ“Œ Google Chrome: Custom Tabs mit Bild-in-Bild-Anzeige nutzen


๐Ÿ“ˆ 20.24 Punkte

๐Ÿ“Œ Better content sharing with Custom Tabs


๐Ÿ“ˆ 20.24 Punkte

๐Ÿ“Œ Bugtraq: Admin Custom Login WordPress plugin custom login page affected by persistent Cross-Site Scripting


๐Ÿ“ˆ 17.86 Punkte

๐Ÿ“Œ WordPress Absolutely Glamorous Custom Admin ag-custom-admin Plugin Database Backup Arbitrary File Download Vulnerability


๐Ÿ“ˆ 17.86 Punkte

๐Ÿ“Œ [Custom Linux ISO] An Arch Linux Based Custom Live ISO For Everyone #lovelinux


๐Ÿ“ˆ 17.86 Punkte

๐Ÿ“Œ [Custom Linux ISO] An Arch Linux Based Custom Live ISO For Everyone #lovelinux


๐Ÿ“ˆ 17.86 Punkte

๐Ÿ“Œ Low CVE-2019-14789: Custom 404 pro project Custom 404 pro


๐Ÿ“ˆ 17.86 Punkte

๐Ÿ“Œ HackerOne: Custom Field Attributes may be created and updated for customers with Custom Field Trial enabled


๐Ÿ“ˆ 17.86 Punkte

๐Ÿ“Œ Low CVE-2019-15838: Custom 404 pro project Custom 404 pro


๐Ÿ“ˆ 17.86 Punkte

๐Ÿ“Œ DomainMod up to 4.11.01 Custom Domain admin/domain-fields/ Add Custom cross site scripting


๐Ÿ“ˆ 17.86 Punkte

๐Ÿ“Œ Low CVE-2020-14063: Tc custom javascript project Tc custom javascript


๐Ÿ“ˆ 17.86 Punkte

๐Ÿ“Œ Medium CVE-2015-3173: Custom content type manager project Custom content type manager


๐Ÿ“ˆ 17.86 Punkte

๐Ÿ“Œ CVE-2022-28979 | Lifreay Portal/DXP Custom Facet Widget Custom Parameter Name cross site scripting


๐Ÿ“ˆ 17.86 Punkte











matomo