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
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class FakeWatchFaceInstallationRepository : WatchFaceInstallationRepository {
_watchFaceInstallationStatus.value = WatchFaceInstallationStatus.Preparing
}

override suspend fun installAndroidify(nodeId: String) { }

private fun generateTransferId() = UUID.randomUUID().toString().take(8)

public fun setWatchAsConnected() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ fun CustomizeAndExportScreen(
onInstallWatchFaceClicked = {
viewModel.installWatchFace()
},
onInstallAndroidifyClicked = {
viewModel.launchPlayInstallOnWatch()
},
onResetWatchFaceSend = {
viewModel.resetWatchFaceSend()
},
Expand All @@ -151,6 +154,7 @@ private fun CustomizeExportContents(
onToolSelected: (CustomizeTool) -> Unit,
onSelectedToolStateChanged: (ToolState) -> Unit,
onInstallWatchFaceClicked: () -> Unit,
onInstallAndroidifyClicked: suspend () -> Boolean,
onResetWatchFaceSend: () -> Unit,
layoutType: CustomizeExportLayoutType,
snackbarHostState: SnackbarHostState,
Expand Down Expand Up @@ -263,6 +267,9 @@ private fun CustomizeExportContents(
onWatchFaceInstallClick = {
onInstallWatchFaceClicked()
},
onAndroidifyInstallClick = {
onInstallAndroidifyClicked()
},
onLoad = loadWatchFaces,
watchFaceSelectionState = state.watchFaceSelectionState,
onWatchFaceSelect = onWatchFaceSelect,
Expand Down Expand Up @@ -599,6 +606,7 @@ fun CustomizeExportPreview() {
layoutType = CustomizeExportLayoutType.Compact,
onSelectedToolStateChanged = {},
onInstallWatchFaceClicked = {},
onInstallAndroidifyClicked = { true },
onResetWatchFaceSend = {},
loadWatchFaces = {},
onWatchFaceSelect = {},
Expand Down Expand Up @@ -640,6 +648,7 @@ fun CustomizeExportPreviewLarge() {
layoutType = CustomizeExportLayoutType.Medium,
onSelectedToolStateChanged = {},
onInstallWatchFaceClicked = {},
onInstallAndroidifyClicked = { true },
onResetWatchFaceSend = {},
loadWatchFaces = {},
onWatchFaceSelect = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.SnackbarHostState
import androidx.compose.ui.Modifier
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.application
import androidx.lifecycle.viewModelScope
import com.android.developers.androidify.RemoteConfigDataSource
import com.android.developers.androidify.customize.watchface.WatchFaceSelectionState
Expand Down Expand Up @@ -367,6 +368,19 @@ class CustomizeExportViewModel @AssistedInject constructor(
}
}

suspend fun launchPlayInstallOnWatch(): Boolean {
try {
val watch = state.value.connectedWatch
watch?.let {
watchfaceInstallationRepository.installAndroidify(it.nodeId)
}
return true
} catch (e: Exception) {
Timber.e(e, "Failed to open Play Store on watch")
}
return false
}

fun installWatchFace() {
val watchFaceToInstall = _state.value.watchFaceSelectionState.selectedWatchFace ?: return
val bitmap = state.value.exportImageCanvas.imageBitmap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.android.developers.androidify.customize.watchface

import android.content.Intent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -24,24 +23,33 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import com.android.developers.androidify.results.R
import com.android.developers.androidify.theme.AndroidifyTheme
import com.android.developers.androidify.watchface.WatchFaceAsset
import kotlinx.coroutines.launch

@Composable
fun InstallAndroidifyPanel(
onInstallClick: suspend () -> Boolean,
modifier: Modifier = Modifier,
) {
val context = LocalContext.current
var isPlayLaunched by remember { mutableStateOf(false) }
val placeholderWatchFace = WatchFaceAsset(
id = "watch_face_1",
previewPath = R.drawable.watch_app_placeholder,
Expand All @@ -60,21 +68,47 @@ fun InstallAndroidifyPanel(
) {
WatchFacePreviewItem(
watchFace = placeholderWatchFace,
isSelected = false,
isSelected = true,
onClick = { },
)
}

Spacer(modifier = Modifier.height(24.dp))

val buttonText = if (isPlayLaunched) {
stringResource(R.string.continue_on_watch)
} else {
stringResource(R.string.install_androidify)
}
val launchedColors = ButtonDefaults.buttonColors(
contentColor = MaterialTheme.colorScheme.onSurface,
containerColor = MaterialTheme.colorScheme.secondaryContainer,
)
val installColors = ButtonDefaults.buttonColors(
contentColor = MaterialTheme.colorScheme.surface,
containerColor = MaterialTheme.colorScheme.onSurface,
)
val scope = rememberCoroutineScope()
WatchFacePanelButton(
modifier = modifier.padding(horizontal = 16.dp),
buttonText = stringResource(R.string.install_androidify),
buttonText = buttonText,
iconResId = R.drawable.watch_arrow_24,
onClick = {
val uri = "market://details?id=${context.packageName}".toUri()
val intent = Intent(Intent.ACTION_VIEW, uri)
context.startActivity(intent)
if (!isPlayLaunched) {
scope.launch {
val launchResult = onInstallClick()
if (launchResult) {
isPlayLaunched = true
}
}
}
},
colors = if (isPlayLaunched) {
launchedColors
} else {
installColors
},
isInProgress = isPlayLaunched,
)
}
}
Expand All @@ -84,6 +118,6 @@ fun InstallAndroidifyPanel(
@Composable
private fun InstallAndroidifyPanelPreview() {
AndroidifyTheme {
InstallAndroidifyPanel()
InstallAndroidifyPanel({ true })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fun TransferringWatchFacePanel(
WatchFacePanelButton(
modifier = Modifier.padding(horizontal = 16.dp),
buttonText = transferLabel,
isSending = true,
isInProgress = true,
colors = ButtonDefaults.buttonColors(
contentColor = MaterialTheme.colorScheme.onSurface,
containerColor = MaterialTheme.colorScheme.secondaryContainer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import com.android.developers.androidify.wear.common.WatchFaceInstallationStatus
fun WatchFaceModalSheet(
connectedWatch: ConnectedWatch,
onWatchFaceInstallClick: (String) -> Unit,
onAndroidifyInstallClick: suspend () -> Boolean,
installationStatus: WatchFaceInstallationStatus,
sheetState: SheetState,
watchFaceSelectionState: WatchFaceSelectionState,
Expand Down Expand Up @@ -202,7 +203,9 @@ fun WatchFaceModalSheet(
}

else -> {
InstallAndroidifyPanel()
InstallAndroidifyPanel(
onInstallClick = onAndroidifyInstallClick,
)
}
}
}
Expand Down Expand Up @@ -243,6 +246,7 @@ private fun WatchFaceModalSheetPreview() {
onLoad = {},
onDismiss = {},
onWatchFaceInstallClick = {},
onAndroidifyInstallClick = { true },
sheetState = sheetState,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fun WatchFacePanelButton(
modifier: Modifier = Modifier,
onClick: () -> Unit = {},
buttonText: String,
isSending: Boolean = false,
isInProgress: Boolean = false,
iconResId: Int? = null,
colors: ButtonColors = ButtonDefaults.buttonColors(
contentColor = MaterialTheme.colorScheme.surface,
Expand All @@ -67,7 +67,7 @@ fun WatchFacePanelButton(
Row(
verticalAlignment = Alignment.CenterVertically,
) {
if (isSending) {
if (isInProgress) {
ContainedLoadingIndicator(
modifier = Modifier.size(24.dp),
containerColor = colors.containerColor,
Expand All @@ -92,7 +92,7 @@ private fun WatchFaceInstallButtonPreview() {
WatchFacePanelButton(
onClick = { },
buttonText = stringResource(R.string.send_to_watch),
isSending = false,
isInProgress = false,
iconResId = R.drawable.watch_arrow_24,
)
}
Expand All @@ -105,7 +105,7 @@ private fun WatchFaceInstalledButtonPreview() {
WatchFacePanelButton(
onClick = { },
buttonText = stringResource(R.string.watch_face_sent),
isSending = false,
isInProgress = false,
iconResId = R.drawable.check_24,
colors = ButtonDefaults.buttonColors(
contentColor = MaterialTheme.colorScheme.onSurface,
Expand All @@ -122,7 +122,7 @@ private fun WatchFaceInstallingButtonPreview() {
WatchFacePanelButton(
onClick = { },
buttonText = stringResource(R.string.sending_to_watch),
isSending = true,
isInProgress = true,
colors = ButtonDefaults.buttonColors(
contentColor = MaterialTheme.colorScheme.onSurface,
containerColor = MaterialTheme.colorScheme.secondaryContainer,
Expand Down
1 change: 1 addition & 0 deletions feature/results/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@
<string name="complete_error_message">Check your watch is connected and try again</string>
<string name="no_watch_faces">"No available watch faces"</string>
<string name="error_dismiss">OK</string>
<string name="continue_on_watch">Continue on watch</string>
</resources>
1 change: 1 addition & 0 deletions watchface/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {
implementation(libs.validator.push.android) {
exclude(group = "com.google.guava", "listenablefuture")
}
implementation(libs.androidx.wear.remote.interactions)
implementation(libs.bcpkix.jdk18on)
implementation(libs.play.services.wearable)
implementation(libs.kotlinx.coroutines.play.services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ class EmptyWatchFaceInstallationRepositoryImpl @Inject constructor() : WatchFace
override suspend fun resetInstallationStatus() { }

override suspend fun prepareForTransfer() { }

override suspend fun installAndroidify(nodeId: String) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
package com.android.developers.androidify.watchface.transfer

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import androidx.concurrent.futures.await
import androidx.wear.remote.interactions.RemoteActivityHelper
import com.android.developers.androidify.watchface.WatchFaceAsset
import com.android.developers.androidify.watchface.creator.WatchFaceCreator
import com.android.developers.androidify.wear.common.ConnectedWatch
import com.android.developers.androidify.wear.common.WatchFaceInstallError
import com.android.developers.androidify.wear.common.WatchFaceInstallationStatus
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down Expand Up @@ -70,6 +75,8 @@ interface WatchFaceInstallationRepository {
suspend fun resetInstallationStatus()

suspend fun prepareForTransfer()

suspend fun installAndroidify(nodeId: String)
}

class WatchFaceInstallationRepositoryImpl @Inject constructor(
Expand Down Expand Up @@ -133,4 +140,18 @@ class WatchFaceInstallationRepositoryImpl @Inject constructor(
override suspend fun prepareForTransfer() {
manualStatusUpdates.tryEmit(WatchFaceInstallationStatus.Preparing)
}

override suspend fun installAndroidify(nodeId: String) {
val backgroundExecutor = Dispatchers.IO.asExecutor()
val remoteActivityHelper = RemoteActivityHelper(context, backgroundExecutor)

remoteActivityHelper.startRemoteActivity(
Intent(Intent.ACTION_VIEW)
.setData(
Uri.parse("market://details?id=${context.packageName}"),
)
.addCategory(Intent.CATEGORY_BROWSABLE),
nodeId,
).await()
}
}