Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions wear/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@
</intent-filter>
</activity>

<activity
android:name=".snippets.m3.tile.TileActivity"
android:exported="true"
android:taskAffinity=""
android:theme="@android:style/Theme.DeviceDefault">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<intent-filter android:label="Deep Link to Message Detail">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="googleandroidsnippets" android:host="app" />
</intent-filter>
</activity>

<!-- [START android_wear_tile_manifest] -->
<service
android:name=".snippets.tile.MyTileService"
Expand Down Expand Up @@ -69,6 +88,81 @@
</service>
<!-- [END android_wear_m3_tile_manifest] -->

<service
android:name=".snippets.m3.tile.HelloTileService"
android:label="@string/tile_label"
android:description="@string/tile_description"
android:icon="@mipmap/ic_launcher"
android:exported="true"
android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER">
<intent-filter>
<action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />
</intent-filter>

<meta-data android:name="androidx.wear.tiles.PREVIEW"
android:resource="@drawable/tile_preview" />
</service>

<service
android:name=".snippets.m3.tile.InteractionLaunchAction"
android:label="@string/tile_label"
android:description="@string/tile_description"
android:icon="@mipmap/ic_launcher"
android:exported="true"
android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER">
<intent-filter>
<action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />
</intent-filter>

<meta-data android:name="androidx.wear.tiles.PREVIEW"
android:resource="@drawable/tile_preview" />
</service>

<service
android:name=".snippets.m3.tile.InteractionDeepLink"
android:label="@string/tile_label"
android:description="@string/tile_description"
android:icon="@mipmap/ic_launcher"
android:exported="true"
android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER">
<intent-filter>
<action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />
</intent-filter>

<meta-data android:name="androidx.wear.tiles.PREVIEW"
android:resource="@drawable/tile_preview" />
</service>

<service
android:name=".snippets.m3.tile.InteractionRefresh"
android:label="@string/tile_label"
android:description="@string/tile_description"
android:icon="@mipmap/ic_launcher"
android:exported="true"
android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER">
<intent-filter>
<action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />
</intent-filter>

<meta-data android:name="androidx.wear.tiles.PREVIEW"
android:resource="@drawable/tile_preview" />
</service>

<service
android:name=".snippets.m3.tile.InteractionLoadAction"
android:label="@string/tile_label"
android:description="@string/tile_description"
android:icon="@mipmap/ic_launcher"
android:exported="true"
android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER">
<intent-filter>
<action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />
</intent-filter>

<meta-data android:name="androidx.wear.tiles.PREVIEW"
android:resource="@drawable/tile_preview" />
</service>

</application>

</manifest>
272 changes: 272 additions & 0 deletions wear/src/main/java/com/example/wear/snippets/m3/tile/Interaction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.wear.snippets.m3.tile

import android.content.ComponentName
import android.content.Intent
import androidx.core.app.TaskStackBuilder
import androidx.core.net.toUri
import androidx.wear.protolayout.ActionBuilders
import androidx.wear.protolayout.ActionBuilders.launchAction
import androidx.wear.protolayout.LayoutElementBuilders
import androidx.wear.protolayout.ResourceBuilders.Resources
import androidx.wear.protolayout.TimelineBuilders.Timeline
import androidx.wear.protolayout.expression.dynamicDataMapOf
import androidx.wear.protolayout.expression.intAppDataKey
import androidx.wear.protolayout.expression.mapTo
import androidx.wear.protolayout.expression.stringAppDataKey
import androidx.wear.protolayout.material3.MaterialScope
import androidx.wear.protolayout.material3.Typography.BODY_LARGE
import androidx.wear.protolayout.material3.materialScope
import androidx.wear.protolayout.material3.primaryLayout
import androidx.wear.protolayout.material3.text
import androidx.wear.protolayout.material3.textButton
import androidx.wear.protolayout.modifiers.clickable
import androidx.wear.protolayout.modifiers.loadAction
import androidx.wear.protolayout.types.layoutString
import androidx.wear.tiles.RequestBuilders
import androidx.wear.tiles.RequestBuilders.ResourcesRequest
import androidx.wear.tiles.TileBuilders.Tile
import androidx.wear.tiles.TileService
import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture
import java.util.Locale
import kotlin.random.Random

private const val RESOURCES_VERSION = "1"

abstract class BaseTileService : TileService() {

override fun onTileRequest(
requestParams: RequestBuilders.TileRequest
): ListenableFuture<Tile> =
Futures.immediateFuture(
Tile.Builder()
.setResourcesVersion(RESOURCES_VERSION)
.setTileTimeline(
Timeline.fromLayoutElement(
materialScope(this, requestParams.deviceConfiguration) {
tileLayout(requestParams)
}
)
)
.build()
)

override fun onTileResourcesRequest(
requestParams: ResourcesRequest
): ListenableFuture<Resources> =
Futures.immediateFuture(
Resources.Builder().setVersion(requestParams.version).build()
)

abstract fun MaterialScope.tileLayout(
requestParams: RequestBuilders.TileRequest
): LayoutElementBuilders.LayoutElement
}

class HelloTileService : BaseTileService() {
override fun MaterialScope.tileLayout(
requestParams: RequestBuilders.TileRequest
) = primaryLayout(mainSlot = { text("Hello, World!".layoutString) })
}

class InteractionRefresh : BaseTileService() {
override fun MaterialScope.tileLayout(
requestParams: RequestBuilders.TileRequest
) =
primaryLayout(
// Output a debug code so we can see the layout changing
titleSlot = {
text(
String.format(
Locale.ENGLISH,
"Debug %06d",
Random.nextInt(0, 1_000_000),
)
.layoutString
)
},
mainSlot = {
// [START android_wear_m3_interaction_refresh]
textButton(
onClick = clickable(loadAction()),
labelContent = { text("Refresh".layoutString) },
)
// [END android_wear_m3_interaction_refresh]
},
)
}

class InteractionDeepLink : TileService() {

// [START android_wear_m3_interaction_deeplink_tile]
override fun onTileRequest(
requestParams: RequestBuilders.TileRequest
): ListenableFuture<Tile?> {
val lastClickableId = requestParams.currentState.lastClickableId
if (lastClickableId == "foo") {
TaskStackBuilder.create(this)
.addNextIntentWithParentStack(
Intent(
Intent.ACTION_VIEW,
"googleandroidsnippets://app/message_detail/1".toUri(),
this,
TileActivity::class.java,
)
)
.startActivities()
}
// ... User didn't tap a button (either first load or tapped somewhere else)
// [START_EXCLUDE]
return Futures.immediateFuture(
Tile.Builder()
.setResourcesVersion(RESOURCES_VERSION)
.setTileTimeline(
Timeline.fromLayoutElement(
materialScope(this, requestParams.deviceConfiguration) {
tileLayout(requestParams)
}
)
)
.build()
)
// [END_EXCLUDE]
}

// [END android_wear_m3_interaction_deeplink_tile]

override fun onTileResourcesRequest(
requestParams: ResourcesRequest
): ListenableFuture<Resources?> =
Futures.immediateFuture(
Resources.Builder().setVersion(requestParams.version).build()
)

fun MaterialScope.tileLayout(requestParams: RequestBuilders.TileRequest) =
primaryLayout(
mainSlot = {
// [START android_wear_m3_interaction_deeplink_layout]
textButton(
labelContent = {
text("Deep Link me!".layoutString, typography = BODY_LARGE)
},
onClick = clickable(id = "foo", action = loadAction()),
)
// [END android_wear_m3_interaction_deeplink_layout]
}
)
}

class InteractionLoadAction : BaseTileService() {

override fun onTileRequest(
requestParams: RequestBuilders.TileRequest
): ListenableFuture<Tile> {

val name: String?
val age: Int?

// When triggered by loadAction(), "name" will be "Javier", and "age" will
// be 37.
with(requestParams.currentState.stateMap) {
name = this[stringAppDataKey("name")]
age = this[intAppDataKey("age")]
}

return Futures.immediateFuture(
Tile.Builder()
.setResourcesVersion(RESOURCES_VERSION)
.setTileTimeline(
Timeline.fromLayoutElement(
materialScope(this, requestParams.deviceConfiguration) {
tileLayout(requestParams)
}
)
)
.build()
)
}

override fun MaterialScope.tileLayout(
requestParams: RequestBuilders.TileRequest
) =
primaryLayout(
// Output a debug code so we can verify that the reload happens
titleSlot = {
text(
String.format(
Locale.ENGLISH,
"Debug %06d",
Random.nextInt(0, 1_000_000),
)
.layoutString
)
},
mainSlot = {
// [START android_wear_m3_interaction_loadaction_layout]
textButton(
labelContent = {
text("loadAction()".layoutString, typography = BODY_LARGE)
},
onClick =
clickable(
action =
loadAction(
dynamicDataMapOf(
stringAppDataKey("name") mapTo "Javier",
intAppDataKey("age") mapTo 37,
)
)
),
)
// [END android_wear_m3_interaction_loadaction_layout]
},
)
}

class InteractionLaunchAction : BaseTileService() {

override fun MaterialScope.tileLayout(
requestParams: RequestBuilders.TileRequest
) =
primaryLayout(
mainSlot = {
// [START android_wear_m3_interactions_launchaction]
textButton(
labelContent = {
text("launchAction()".layoutString, typography = BODY_LARGE)
},
onClick =
clickable(
action =
launchAction(
ComponentName(
"com.example.wear",
"com.example.wear.snippets.m3.tile.TileActivity",
),
mapOf(
"name" to ActionBuilders.stringExtra("Bartholomew"),
"age" to ActionBuilders.intExtra(21),
),
)
),
)
// [END android_wear_m3_interactions_launchaction]
}
)
}
Loading