Skip to content

Commit

Permalink
Enable creating configurable app shortcuts
Browse files Browse the repository at this point in the history
Hook up a prebuild task that generated a shortcuts.xml resource with
static shortcuts. These are configurable via twaManifest.shortcuts,
and they closely resemble the WebAppManifest shortcuts dictionary.
  • Loading branch information
rayankans committed Oct 9, 2019
1 parent 46adae3 commit 9c8b320
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
55 changes: 54 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import groovy.xml.MarkupBuilder;

apply plugin: 'com.android.application'

def twaManifest = [
Expand All @@ -24,7 +26,16 @@ def twaManifest = [
themeColor: '#303F9F', // The color used for the status bar.
backgroundColor: '#bababa', // The color used for the splash screen background.
enableNotifications: false, // Set to true to enable notification delegation.
useBrowserOnChromeOS: true // Set to false if you've added interaction with Android system APIs.
useBrowserOnChromeOS: true, // Set to false if you've added interaction with Android system APIs.
// Add shortcuts for your app here. Every shortcut must include the following fields:
// - name: String that will show up in the shortcut.
// - short_name: Shorter string used if |name| is too long.
// - url: Absolute path of the URL to launch the app with (e.g '/create').
// - icon: Name of the resource in the drawable folder to use as an icon.
shortcuts: [
// Insert shortcuts here, for example:
// ['name': 'Open SVG', 'short_name': 'Open', url: '/open', 'icon': 'splash']
]
]

android {
Expand Down Expand Up @@ -77,6 +88,11 @@ android {
// TWA. When enabled, it causes the web app to be installed as a Desktop PWA on ChromeOS
// but does not provide any native Android interaction.
resValue "bool", "useBrowserOnChromeOS", twaManifest.useBrowserOnChromeOS.toString()

twaManifest.shortcuts.eachWithIndex { shortcut, index ->
resValue "string", "shortcut_name_$index", "$shortcut.name"
resValue "string", "shortcut_short_name_$index", "$shortcut.short_name"
}
}
buildTypes {
release {
Expand All @@ -89,6 +105,43 @@ android {
}
}

task generateShorcutsFile {
assert twaManifest.shortcuts.size() < 5, "You can have at most 4 shortcuts."
twaManifest.shortcuts.eachWithIndex { s, i ->
assert s.name != null, 'Missing `asd` in shortcut #' + i
assert s.short_name != null, 'Missing `short_name` in shortcut #' + i
assert s.url != null, 'Missing `icon` in shortcut #' + i
assert s.icon != null, 'Missing `url` in shortcut #' + i
}

def shortcutsFile = new File("$projectDir/src/main/res/xml", "shortcuts.xml")

def xmlWriter = new StringWriter()
def xmlMarkup = new MarkupBuilder(new IndentPrinter(xmlWriter, " ", true))

xmlMarkup
.'shortcuts'('xmlns:android': 'http://schemas.android.com/apk/res/android') {
twaManifest.shortcuts.eachWithIndex { s, i ->
'shortcut'(
'android:shortcutId': 'shortcut' + i,
'android:enabled': 'true',
'android:icon': '@drawable/' + s.icon,
'android:shortcutShortLabel': '@string/shortcut_short_name_' + i,
'android:shortcutLongLabel': '@string/shortcut_name_' + i) {
'intent'(
'android:action': 'android.intent.action.MAIN',
'android:targetPackage': twaManifest.applicationId,
'android:targetClass': 'android.support.customtabs.trusted.LauncherActivity',
'android:data': 'https://' + twaManifest.hostName + s.url)
'categories'('android:name': 'android.intent.category.LAUNCHER')
}
}
}
shortcutsFile.text = xmlWriter.toString()
}

preBuild.dependsOn(generateShorcutsFile)

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.github.GoogleChrome.custom-tabs-client:customtabs:809a55cfa2'
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
<meta-data android:name="android.support.customtabs.trusted.FILE_PROVIDER_AUTHORITY"
android:value="@string/providerAuthority"/>

<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand All @@ -69,6 +71,7 @@
<data android:scheme="https"
android:host="@string/hostName"/>
</intent-filter>

</activity>

<provider
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/xml/shortcuts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<shortcuts xmlns:android='http://schemas.android.com/apk/res/android' />

0 comments on commit 9c8b320

Please sign in to comment.