Skip to content

Commit

Permalink
Merge branch 'main' into jv/broadcast_receiver_new
Browse files Browse the repository at this point in the history
  • Loading branch information
JolandaVerhoef authored Nov 28, 2024
2 parents 7f3ccc4 + fb654a9 commit fd4d608
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ fun PagerIndicator() {
}
}

// [START android_compose_autoadvancepager]
@Composable
fun AutoAdvancePager(pageItems: List<Color>, modifier: Modifier = Modifier) {
Box(modifier = Modifier.fillMaxSize()) {
Expand Down Expand Up @@ -457,6 +458,7 @@ fun AutoAdvancePager(pageItems: List<Color>, modifier: Modifier = Modifier) {
PagerIndicator(pageItems.size, pagerState.currentPage)
}
}
// [END android_compose_autoadvancepager]

@Preview
@Composable
Expand All @@ -470,6 +472,7 @@ private fun AutoAdvancePagerPreview() {
AutoAdvancePager(pageItems = pageItems)
}

// [START android_compose_pagerindicator]
@Composable
fun PagerIndicator(pageCount: Int, currentPageIndex: Int, modifier: Modifier = Modifier) {
Box(modifier = Modifier.fillMaxSize()) {
Expand All @@ -494,6 +497,7 @@ fun PagerIndicator(pageCount: Int, currentPageIndex: Int, modifier: Modifier = M
}
}
}
// [END android_compose_pagerindicator]

@Preview
@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* Copyright 2024 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.compose.snippets.predictiveback

import android.os.SystemClock
import androidx.activity.BackEventCompat
import androidx.activity.compose.PredictiveBackHandler
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.input.pointer.util.VelocityTracker
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import kotlin.coroutines.cancellation.CancellationException
import kotlinx.coroutines.flow.Flow

@Composable
private fun PredictiveBackOverrideExit(
modifier: Modifier,
) {
val navController = rememberNavController()

// [START android_compose_predictiveback_navhost]
NavHost(
navController = navController,
startDestination = "home",
popExitTransition = {
scaleOut(
targetScale = 0.9f,
transformOrigin = TransformOrigin(pivotFractionX = 0.5f, pivotFractionY = 0.5f)
)
},
popEnterTransition = {
EnterTransition.None
},
modifier = modifier,
)
// [END android_compose_predictiveback_navhost]
{
composable("home") {
HomeScreen(
modifier = modifier,
navController = navController,
)
}
composable("settings") {
SettingsScreen(
modifier = modifier,
navController = navController,
)
}
}
}

@Composable
private fun HomeScreen(
modifier: Modifier = Modifier,
navController: NavHostController
) {
}

@Composable
private fun SettingsScreen(
modifier: Modifier = Modifier,
navController: NavHostController
) {
}

@Composable
private fun PredictiveBackHandlerBasicExample() {

var boxScale by remember { mutableFloatStateOf(1F) }

Box(
modifier = Modifier
.fillMaxSize(boxScale)
.background(Color.Blue)
)

// [START android_compose_predictivebackhandler_basic]
PredictiveBackHandler(true) { progress: Flow<BackEventCompat> ->
// code for gesture back started
try {
progress.collect { backEvent ->
// code for progress
boxScale = 1F - (1F * backEvent.progress)
}
// code for completion
} catch (e: CancellationException) {
// code for cancellation
boxScale = 1F
}
}
// [END android_compose_predictivebackhandler_basic]
}

@Composable
private fun PredictiveBackHandlerManualProgress() {

Surface(
modifier = Modifier.fillMaxSize()
) {
var drawerState by remember {
mutableStateOf(DrawerState.Closed)
}

val translationX = remember {
Animatable(0f)
}

val drawerWidth = with(LocalDensity.current) {
DrawerWidth.toPx()
}
translationX.updateBounds(0f, drawerWidth)

val coroutineScope = rememberCoroutineScope()

suspend fun closeDrawer(velocity: Float = 0f) {
translationX.animateTo(targetValue = 0f, initialVelocity = velocity)
drawerState = DrawerState.Closed
}
suspend fun openDrawer(velocity: Float = 0f) {
translationX.animateTo(targetValue = drawerWidth, initialVelocity = velocity)
drawerState = DrawerState.Open
}

val velocityTracker = remember {
VelocityTracker()
}

// [START android_compose_predictivebackhandler_manualprogress]
PredictiveBackHandler(drawerState == DrawerState.Open) { progress ->
try {
progress.collect { backEvent ->
val targetSize = (drawerWidth - (drawerWidth * backEvent.progress))
translationX.snapTo(targetSize)
velocityTracker.addPosition(
SystemClock.uptimeMillis(),
Offset(backEvent.touchX, backEvent.touchY)
)
}
closeDrawer(velocityTracker.calculateVelocity().x)
} catch (e: CancellationException) {
openDrawer(velocityTracker.calculateVelocity().x)
}
velocityTracker.resetTracking()
}
// [END android_compose_predictivebackhandler_manualprogress]
}
}

private enum class DrawerState {
Open,
Closed
}

private val DrawerWidth = 300.dp
22 changes: 22 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ coroutines = "1.9.0"
glide = "1.0.0-beta01"
google-maps = "19.0.0"
gradle-versions = "0.51.0"
guava = "33.2.1-android"
hilt = "2.52"
horologist = "0.6.20"
junit = "4.13.2"
Expand All @@ -44,12 +45,22 @@ media3 = "1.4.1"
# @keep
minSdk = "21"
playServicesWearable = "18.2.0"
protolayout = "1.3.0-alpha04"
protolayoutExpression = "1.3.0-alpha04"
protolayoutMaterial = "1.3.0-alpha04"
recyclerview = "1.3.2"
# @keep
targetSdk = "34"
tiles = "1.5.0-alpha04"
tilesRenderer = "1.5.0-alpha04"
tilesTesting = "1.5.0-alpha04"
tilesTooling = "1.5.0-alpha04"
tilesToolingPreview = "1.5.0-alpha04"
version-catalog-update = "0.8.5"
wear = "1.3.0"
wearComposeFoundation = "1.4.0"
wearComposeMaterial = "1.4.0"
wearToolingPreview = "1.0.0"

[libraries]
accompanist-adaptive = { module = "com.google.accompanist:accompanist-adaptive", version.ref = "accompanist" }
Expand Down Expand Up @@ -103,10 +114,20 @@ androidx-media3-common = { module = "androidx.media3:media3-common", version.ref
androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3" }
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "androidx-navigation" }
androidx-paging-compose = { module = "androidx.paging:paging-compose", version.ref = "androidx-paging" }
androidx-protolayout = { module = "androidx.wear.protolayout:protolayout", version.ref = "protolayout" }
androidx-protolayout-expression = { module = "androidx.wear.protolayout:protolayout-expression", version.ref = "protolayoutExpression" }
androidx-protolayout-material = { module = "androidx.wear.protolayout:protolayout-material", version.ref = "protolayoutMaterial" }
androidx-recyclerview = { module = "androidx.recyclerview:recyclerview", version.ref = "recyclerview" }
androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test" }
androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx-test-espresso" }
androidx-test-runner = "androidx.test:runner:1.6.2"
androidx-tiles = { module = "androidx.wear.tiles:tiles", version.ref = "tiles" }
androidx-tiles-renderer = { module = "androidx.wear.tiles:tiles-renderer", version.ref = "tilesRenderer" }
androidx-tiles-testing = { module = "androidx.wear.tiles:tiles-testing", version.ref = "tilesTesting" }
androidx-tiles-tooling = { module = "androidx.wear.tiles:tiles-tooling", version.ref = "tilesTooling" }
androidx-tiles-tooling-preview = { module = "androidx.wear.tiles:tiles-tooling-preview", version.ref = "tilesToolingPreview" }
androidx-wear = { module = "androidx.wear:wear", version.ref = "wear" }
androidx-wear-tooling-preview = { module = "androidx.wear:wear-tooling-preview", version.ref = "wearToolingPreview" }
androidx-window-core = { module = "androidx.window:window-core", version.ref = "androidx-window" }
androidx-work-runtime-ktx = "androidx.work:work-runtime-ktx:2.10.0"
coil-kt-compose = { module = "io.coil-kt:coil-compose", version.ref = "coil" }
Expand All @@ -117,6 +138,7 @@ glide-compose = { module = "com.github.bumptech.glide:compose", version.ref = "g
google-android-material = { module = "com.google.android.material:material", version.ref = "material" }
googlemaps-compose = { module = "com.google.maps.android:maps-compose", version.ref = "maps-compose" }
googlemaps-maps = { module = "com.google.android.gms:play-services-maps", version.ref = "google-maps" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt" }
hilt-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt" }
horologist-compose-layout = { module = "com.google.android.horologist:horologist-compose-layout", version.ref = "horologist" }
Expand Down
11 changes: 11 additions & 0 deletions wear/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ dependencies {

implementation(libs.compose.ui.tooling)
implementation(libs.play.services.wearable)
implementation(libs.androidx.tiles)
implementation(libs.androidx.wear)
implementation(libs.androidx.protolayout)
implementation(libs.androidx.protolayout.material)
implementation(libs.androidx.protolayout.expression)
debugImplementation(libs.androidx.tiles.renderer)
testImplementation(libs.androidx.tiles.testing)
implementation(libs.androidx.wear.tooling.preview)
implementation(libs.androidx.tiles.tooling.preview)
debugImplementation(libs.androidx.tiles.tooling)
implementation(libs.guava)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.tooling.preview)
Expand Down
18 changes: 18 additions & 0 deletions wear/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- [START android_wear_tile_manifest] -->
<service
android:name=".snippets.tile.MyTileService"
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>
<!-- [END android_wear_tile_manifest] -->

</application>

</manifest>
53 changes: 53 additions & 0 deletions wear/src/main/java/com/example/wear/snippets/tile/Tile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2022 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.tile

import androidx.wear.protolayout.ColorBuilders.argb
import androidx.wear.protolayout.ResourceBuilders.Resources
import androidx.wear.protolayout.TimelineBuilders.Timeline
import androidx.wear.protolayout.material.Text
import androidx.wear.protolayout.material.Typography
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

private const val RESOURCES_VERSION = "1"

// [START android_wear_tile_mytileservice]
class MyTileService : TileService() {

override fun onTileRequest(requestParams: RequestBuilders.TileRequest) =
Futures.immediateFuture(Tile.Builder()
.setResourcesVersion(RESOURCES_VERSION)
.setTileTimeline(
Timeline.fromLayoutElement(
Text.Builder(this, "Hello World!")
.setTypography(Typography.TYPOGRAPHY_BODY1)
.setColor(argb(0xFFFFFFFF.toInt()))
.build()))
.build())

override fun onTileResourcesRequest(requestParams: ResourcesRequest) =
Futures.immediateFuture(Resources.Builder()
.setVersion(RESOURCES_VERSION)
.build()
)

}
// [END android_wear_tile_mytileservice]
Binary file added wear/src/main/res/drawable/tile_preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions wear/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
<string name="voice_text_entry_label">Voice Input</string>
<string name="voice_input_label">Voice Text Entry</string>
<string name="message_list">Message List</string>
<string name="tile_label">Hello Tile</string>
<string name="tile_description">Hello Tile Description</string>
</resources>

0 comments on commit fd4d608

Please sign in to comment.