Migrate App shortcuts snippets - #1084
Conversation
Snippets for the create, manage and capabilities app shortcuts guides.
|
Here is the summary of changes. You are about to add 5 region tags.
This comment is generated by snippet-bot.
|
|
|
||
| fun pinShortcut(context: Context) { | ||
| // [START android_shortcuts_pin_shortcut] | ||
| val shortcutManager = context.getSystemService<ShortcutManager>() |
There was a problem hiding this comment.
Should've been fixed long ago but lets use ShortcutInfoCompat and the relevant methods when used below.
https://developer.android.com/reference/androidx/core/content/pm/ShortcutManagerCompat
Otherwise looks good to me.
There was a problem hiding this comment.
Done, thanks. The pin_shortcut block was the only one still on the platform API. The other two already used ShortcutManagerCompat.
isRequestPinShortcutSupported, createShortcutResultIntent and requestPinShortcut now all go through ShortcutManagerCompat, and the shortcut is built with ShortcutInfoCompat.Builder. This removes the getSystemService<ShortcutManager>() line and its !! as a side effect.
This is a page change too. The guide still prints the platform version, so I added it to the page-edit list in the description.
The same push fixes the lintDebug failure. This change adds three shortcut label strings, and they had no entry in values-es, so lint reported MissingTranslation. I added the Spanish entries, the way #933 handled a new string.
One thing to flag: ShortcutInfoCompat.Builder(...).build() throws without a short label and an intent, where the platform builder does not. The page prints a bare .build(), so the published line now fails at runtime instead of only being incomplete. Tell me if you want the snippet to set a label and an intent. That is a larger difference from the page.
There was a problem hiding this comment.
Tell me if you want the snippet to set a label and an intent. That is a larger difference from the page.
Yes, let's add those requirements.
…its strings The review asked for the Compat API. The pin_shortcut block was the only one of the three still on the platform classes. The other two already used ShortcutManagerCompat. isRequestPinShortcutSupported, createShortcutResultIntent and requestPinShortcut now go through ShortcutManagerCompat, and the shortcut is built with ShortcutInfoCompat.Builder. The getSystemService call and its non-null assertion are no longer necessary, so both are gone, and three imports with them. The guide still prints the platform version, so this is a page change. The description lists it. The same change fixes the lintDebug failure. This branch adds three shortcut label strings and gave them no entry in values-es, so lint reported MissingTranslation on all three. The Spanish entries are here now.
ShortcutInfoCompat.Builder throws without both values, where the platform ShortcutInfo.Builder does not. The page prints a bare build() call, so the published line failed at runtime after the move to the Compat API. The label and the intent match android_shortcuts_create_dynamic, higher up the same page, so the two blocks agree.
|
@riggaroo can you help review/unblock for submission. Note that like notifications, shortcuts are ui tooling agnostic, so very little to no compose related code here. |
a2690b6 to
8f2fede
Compare
Code snippets are for:
Region tags:
android_shortcuts_xml_static,android_shortcuts_create_dynamic,android_shortcuts_pin_shortcut(Create shortcuts),android_shortcuts_restore_dynamic(Manage shortcuts),android_shortcuts_xml_capability(Add capabilities).List of modifications:
android_shortcuts_pin_shortcut: D3. The page passes/* flags */ 0toPendingIntent.getBroadcast(). A mutability flag has been mandatory since API level 31, so the published call throws on any current device. It readsPendingIntent.FLAG_IMMUTABLEhere. Nothing in the block callssetPendingIntentTemplate()orsetOnClickFillInIntent(), which is what would make itFLAG_MUTABLE.android_shortcuts_restore_dynamic: D24. The page callsShortcutManagerCompat.getPinnedShortcuts(this), and that method does not exist. Typing the page's own token into the module givesUnresolved reference 'getPinnedShortcuts';androidx.corepublishesgetDynamicShortcuts()andgetShortcuts(Context, int)and has never publishedgetPinnedShortcutsat any version. The block readsShortcutManagerCompat.getShortcuts(this, ShortcutManagerCompat.FLAG_MATCH_PINNED), written out in full rather than through an import, because an import sits outside the region and would leave the published block showing a bare constant with no owner. The page's block does not compile as published, so this wants a page edit as well.android_shortcuts_pin_shortcut: the block now uses the AndroidX Compat API, on review.ShortcutManagerCompat.isRequestPinShortcutSupported(),ShortcutInfoCompat.Builder,ShortcutManagerCompat.createShortcutResultIntent()andShortcutManagerCompat.requestPinShortcut()replace the platformShortcutManagerandShortcutInfo. The other two Kotlin blocks on these pages already used the Compat classes, so this makes all three agree. The page prints the platform version, so this wants a page edit.This also settles a second defect in the same block. The page named one thing two ways, a bare
getSystemService()that needs aContextreceiver, and acontextvariable two lines down. The Compat API is static and takes theContextas an argument, so thegetSystemService()line and its non-null assertion are gone rather than repaired.One caveat for the page owner:
ShortcutInfoCompat.Builder(context, "my-shortcut").build()throws without a short label and an intent, where the platform builder does not. The page prints a bare.build(), so that line now fails at runtime instead of only being incomplete. The snippet keeps the page's shape. Say the word if you want it to set a label and an intent.spotlessApplymoved four lines inandroid_shortcuts_create_dynamicand four inandroid_shortcuts_pin_shortcut: four-space continuation indents, one argument per line, and no blank line before a closing brace.android_shortcuts_restore_dynamicmoved one. The two XML blocks are verbatim, 20/20 and 12/12.R.drawable.icon_websiteis scaffolding written for this extraction, becauseandroid_shortcuts_create_dynamicpasses it toIconCompat.createWithResource()and it has to resolve. It is the Materiallanguageicon;compose_icon, named by the static shortcuts XML, is the Materialwidgetsicon. Both record their source in the file. Neither declares a tint: the guide's own attribute table says a shortcut icon cannot carry one, and the launcher draws them outside this module's theme.compose/snippets/src/main/res/xml/, and the four resources the static block names are declared in the module.values/strings.xml, and invalues-es/strings.xmlas well. Without the Spanish entrieslintDebugfails withMissingTranslation.Snippets not migrated:
AndroidManifest.xmlactivity with the shortcuts<meta-data>. Manifest fragment; merging one changes the application manifest.gradle.propertiesfor AndroidX. Build configuration, not module code.dependencies { }for the Google Shortcuts Integration Library. Build script, and it pins versions that would go stale here.ShortcutManagerobject performs operations on it."adb shell cmd shortcut reset-throttling. A shell command.No Java blocks on any of the three pages.
D21, two sections that promise code and show none:
ACTION_CREATE_SHORTCUTto an activity's<intent-filter>, then given five steps namingcreateShortcutResultIntent(),setResult()andfinish(). OnlycreateShortcutResultIntent()appears in any block on the page, and that block is about pinning.android:taskAffinity=""on the trampoline activity inAndroidManifest.xml, and reference that activity from the intent in the shortcuts resource file. No block for either.Writing those blocks here would be new teaching content rather than a copy of anything. Every other step on all three pages was read against the blocks and is answered by one.