diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6aed96f5..395a1faa 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -20,6 +20,7 @@ androidx-test = "1.6.1" androidx-test-espresso = "3.6.1" androidx-window = "1.3.0" androidxHiltNavigationCompose = "1.2.0" +cameraCompose = "1.5.0-alpha02" coil = "2.7.0" # @keep compileSdk = "34" @@ -50,6 +51,8 @@ targetSdk = "34" version-catalog-update = "0.8.4" wearComposeFoundation = "1.4.0" wearComposeMaterial = "1.4.0" +junitVersion = "1.2.1" +cameraLifecycle = "1.3.4" [libraries] accompanist-adaptive = { module = "com.google.accompanist:accompanist-adaptive", version.ref = "accompanist" } @@ -59,6 +62,7 @@ accompanist-theme-adapter-material = { module = "com.google.accompanist:accompan accompanist-theme-adapter-material3 = { module = "com.google.accompanist:accompanist-themeadapter-material3", version.ref = "accompanist" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity-compose" } androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" } +androidx-camera-compose = { module = "androidx.camera:camera-compose", version.ref = "cameraCompose" } androidx-compose-animation-graphics = { module = "androidx.compose.animation:animation-graphics", version.ref = "compose-latest" } androidx-compose-bom = { module = "androidx.compose:compose-bom", version.ref = "androidx-compose-bom" } androidx-compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose-latest" } @@ -127,6 +131,8 @@ kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutine kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" } play-services-wearable = { module = "com.google.android.gms:play-services-wearable", version.ref = "playServicesWearable" } +androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } +androidx-camera-lifecycle = { group = "androidx.camera", name = "camera-lifecycle", version.ref = "cameraLifecycle" } [plugins] android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" } diff --git a/media/.gitignore b/media/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/media/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/media/build.gradle.kts b/media/build.gradle.kts new file mode 100644 index 00000000..bbd6a36d --- /dev/null +++ b/media/build.gradle.kts @@ -0,0 +1,66 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.compose.compiler) +} + +android { + namespace = "com.example.media" + compileSdk = 35 + + defaultConfig { + applicationId = "com.example.media" + minSdk = 24 + targetSdk = 34 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + kotlinOptions { + jvmTarget = "11" + } + buildFeatures { + compose = true + } +} + +dependencies { + + // Add CameraX Compose dependency + implementation(libs.androidx.camera.compose) + implementation("androidx.camera:camera-core:1.3.4") + implementation("androidx.camera:camera-camera2:1.3.4") + + implementation(libs.accompanist.permissions) + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.lifecycle.runtime) + implementation(libs.androidx.activity.compose) + implementation(platform(libs.androidx.compose.bom)) + implementation(libs.androidx.compose.ui) + implementation(libs.androidx.compose.ui.graphics) + implementation(libs.androidx.compose.ui.tooling.preview) + implementation(libs.androidx.compose.material3) + implementation(libs.androidx.camera.lifecycle) + testImplementation(libs.junit) + androidTestImplementation(libs.androidx.junit) + androidTestImplementation(libs.androidx.test.espresso.core) + androidTestImplementation(platform(libs.androidx.compose.bom)) + androidTestImplementation(libs.androidx.compose.ui.test.junit4) + debugImplementation(libs.androidx.compose.ui.tooling) + debugImplementation(libs.androidx.compose.ui.test.manifest) +} \ No newline at end of file diff --git a/media/proguard-rules.pro b/media/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/media/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/media/src/main/AndroidManifest.xml b/media/src/main/AndroidManifest.xml new file mode 100644 index 00000000..6106da2e --- /dev/null +++ b/media/src/main/AndroidManifest.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/media/src/main/java/com/example/media/MediaSnippetsActivity.kt b/media/src/main/java/com/example/media/MediaSnippetsActivity.kt new file mode 100644 index 00000000..3887b3e0 --- /dev/null +++ b/media/src/main/java/com/example/media/MediaSnippetsActivity.kt @@ -0,0 +1,32 @@ +package com.example.media + +import android.annotation.SuppressLint +import android.os.Bundle +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.activity.enableEdgeToEdge +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material3.Scaffold +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import com.example.media.camera.CameraPreviewScreen +import com.example.media.camera.CameraPreviewViewModel +import com.example.media.ui.theme.SnippetsTheme + +@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") +class MediaSnippetsActivity : ComponentActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + enableEdgeToEdge() + setContent { + SnippetsTheme { + Scaffold(modifier = Modifier.fillMaxSize()) { _ -> + val context = LocalContext.current + val viewModel = remember { CameraPreviewViewModel(context.applicationContext) } + CameraPreviewScreen(viewModel) + } + } + } + } +} \ No newline at end of file diff --git a/media/src/main/java/com/example/media/camera/CameraSnippets.kt b/media/src/main/java/com/example/media/camera/CameraSnippets.kt new file mode 100644 index 00000000..a50c2dc2 --- /dev/null +++ b/media/src/main/java/com/example/media/camera/CameraSnippets.kt @@ -0,0 +1,160 @@ +package com.example.media.camera + +import android.content.Context +import androidx.camera.compose.CameraXViewfinder +import androidx.camera.core.CameraSelector +import androidx.camera.core.ImageCapture +import androidx.camera.core.Preview +import androidx.camera.core.SurfaceRequest +import androidx.camera.core.UseCaseGroup +import androidx.camera.core.takePicture +import androidx.camera.lifecycle.ProcessCameraProvider +import androidx.camera.lifecycle.awaitInstance +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.safeContentPadding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.unit.dp +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.ViewModel +import androidx.lifecycle.compose.LifecycleStartEffect +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import androidx.lifecycle.viewModelScope +import com.google.accompanist.permissions.ExperimentalPermissionsApi +import com.google.accompanist.permissions.isGranted +import com.google.accompanist.permissions.rememberPermissionState +import com.google.accompanist.permissions.shouldShowRationale +import kotlinx.coroutines.Job +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.cancelAndJoin +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch + +// [START android_media_camera_preview_viewmodel] +class CameraPreviewViewModel(private val appContext: Context) : ViewModel() { + private val _surfaceRequest = MutableStateFlow(null) + val surfaceRequest: StateFlow = _surfaceRequest + + private val previewUseCase = Preview.Builder().build().apply { + setSurfaceProvider { newSurfaceRequest -> + _surfaceRequest.value = newSurfaceRequest + } + } + private val captureUseCase = ImageCapture.Builder().build() + private val useCaseGroup = UseCaseGroup.Builder().apply { + addUseCase(previewUseCase) + addUseCase(captureUseCase) + }.build() + + private var runningCameraJob: Job? = null + + fun startCamera(lifecycleOwner: LifecycleOwner) { + viewModelScope.launch { + runningCameraJob?.cancelAndJoin() + val processCameraProvider = ProcessCameraProvider.awaitInstance(appContext) + processCameraProvider.bindToLifecycle( + lifecycleOwner, + CameraSelector.DEFAULT_BACK_CAMERA, + useCaseGroup + ) + + try { + awaitCancellation() + } finally { + processCameraProvider.unbindAll() + } + }.also { runningCameraJob = it } + } + + fun stopCamera() { + runningCameraJob?.cancel() + } + + fun takePicture() { + viewModelScope.launch { + val imageProxy = captureUseCase.takePicture() + // Do something with the image + } + } +} +// [END android_media_camera_preview_viewmodel] + +// [START android_media_camera_preview_screen] +@OptIn(ExperimentalPermissionsApi::class) +@Composable +fun CameraPreviewScreen( + viewModel: CameraPreviewViewModel, + modifier: Modifier = Modifier +) { + val cameraPermissionState = rememberPermissionState(android.Manifest.permission.CAMERA) + if (cameraPermissionState.status.isGranted) { + CameraPreviewContent(viewModel, modifier) + } else { + Column(modifier.safeContentPadding()) { + val textToShow = if (cameraPermissionState.status.shouldShowRationale) { + // If the user has denied the permission but the rationale can be shown, + // then gently explain why the app requires this permission + "The camera is important for this app. Please grant the permission." + } else { + // If it's the first time the user lands on this feature, or the user + // doesn't want to be asked again for this permission, explain that the + // permission is required + "Camera permission required for this feature to be available. " + + "Please grant the permission" + } + Text(textToShow) + Button(onClick = { cameraPermissionState.launchPermissionRequest() }) { + Text("Request permission") + } + } + } +} +// [END android_media_camera_preview_screen] + +// [START android_media_camera_preview_content] +@Composable +fun CameraPreviewContent( + viewModel: CameraPreviewViewModel, + modifier: Modifier = Modifier +) { + val surfaceRequest by viewModel.surfaceRequest.collectAsStateWithLifecycle() + + LifecycleStartEffect(Unit) { + viewModel.startCamera(this) + onStopOrDispose { + viewModel.stopCamera() + } + } + + surfaceRequest?.let { + Box(modifier) { + CameraXViewfinder(it, Modifier.fillMaxSize()) + Spacer( + Modifier + .safeContentPadding() + .padding(bottom = 16.dp) + .size(64.dp) + .clip(CircleShape) + .clickable { viewModel.takePicture() } + .background(Color.White) + .align(Alignment.BottomCenter) + ) + } + } +} +// [END android_media_camera_preview_content] \ No newline at end of file diff --git a/media/src/main/java/com/example/media/ui/theme/Color.kt b/media/src/main/java/com/example/media/ui/theme/Color.kt new file mode 100644 index 00000000..0723279b --- /dev/null +++ b/media/src/main/java/com/example/media/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package com.example.media.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) \ No newline at end of file diff --git a/media/src/main/java/com/example/media/ui/theme/Theme.kt b/media/src/main/java/com/example/media/ui/theme/Theme.kt new file mode 100644 index 00000000..9db56598 --- /dev/null +++ b/media/src/main/java/com/example/media/ui/theme/Theme.kt @@ -0,0 +1,58 @@ +package com.example.media.ui.theme + +import android.app.Activity +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext + +private val DarkColorScheme = darkColorScheme( + primary = Purple80, + secondary = PurpleGrey80, + tertiary = Pink80 +) + +private val LightColorScheme = lightColorScheme( + primary = Purple40, + secondary = PurpleGrey40, + tertiary = Pink40 + + /* Other default colors to override + background = Color(0xFFFFFBFE), + surface = Color(0xFFFFFBFE), + onPrimary = Color.White, + onSecondary = Color.White, + onTertiary = Color.White, + onBackground = Color(0xFF1C1B1F), + onSurface = Color(0xFF1C1B1F), + */ +) + +@Composable +fun SnippetsTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + // Dynamic color is available on Android 12+ + dynamicColor: Boolean = true, + content: @Composable () -> Unit +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + + MaterialTheme( + colorScheme = colorScheme, + typography = Typography, + content = content + ) +} \ No newline at end of file diff --git a/media/src/main/java/com/example/media/ui/theme/Type.kt b/media/src/main/java/com/example/media/ui/theme/Type.kt new file mode 100644 index 00000000..1d1291bb --- /dev/null +++ b/media/src/main/java/com/example/media/ui/theme/Type.kt @@ -0,0 +1,34 @@ +package com.example.media.ui.theme + +import androidx.compose.material3.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// Set of Material typography styles to start with +val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) + /* Other default text styles to override + titleLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 22.sp, + lineHeight = 28.sp, + letterSpacing = 0.sp + ), + labelSmall = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Medium, + fontSize = 11.sp, + lineHeight = 16.sp, + letterSpacing = 0.5.sp + ) + */ +) \ No newline at end of file diff --git a/media/src/main/res/drawable/ic_launcher_background.xml b/media/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 00000000..07d5da9c --- /dev/null +++ b/media/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/media/src/main/res/drawable/ic_launcher_foreground.xml b/media/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 00000000..2b068d11 --- /dev/null +++ b/media/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/media/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/media/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 00000000..6f3b755b --- /dev/null +++ b/media/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/media/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/media/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 00000000..6f3b755b --- /dev/null +++ b/media/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/media/src/main/res/mipmap-hdpi/ic_launcher.webp b/media/src/main/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 00000000..c209e78e Binary files /dev/null and b/media/src/main/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/media/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/media/src/main/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 00000000..b2dfe3d1 Binary files /dev/null and b/media/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/media/src/main/res/mipmap-mdpi/ic_launcher.webp b/media/src/main/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 00000000..4f0f1d64 Binary files /dev/null and b/media/src/main/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/media/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/media/src/main/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 00000000..62b611da Binary files /dev/null and b/media/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/media/src/main/res/mipmap-xhdpi/ic_launcher.webp b/media/src/main/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 00000000..948a3070 Binary files /dev/null and b/media/src/main/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/media/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/media/src/main/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 00000000..1b9a6956 Binary files /dev/null and b/media/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/media/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/media/src/main/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 00000000..28d4b77f Binary files /dev/null and b/media/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/media/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/media/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 00000000..9287f508 Binary files /dev/null and b/media/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/media/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/media/src/main/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 00000000..aa7d6427 Binary files /dev/null and b/media/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/media/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/media/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 00000000..9126ae37 Binary files /dev/null and b/media/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/media/src/main/res/values/colors.xml b/media/src/main/res/values/colors.xml new file mode 100644 index 00000000..f8c6127d --- /dev/null +++ b/media/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/media/src/main/res/values/strings.xml b/media/src/main/res/values/strings.xml new file mode 100644 index 00000000..8bfef432 --- /dev/null +++ b/media/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + media + \ No newline at end of file diff --git a/media/src/main/res/values/themes.xml b/media/src/main/res/values/themes.xml new file mode 100644 index 00000000..65078ebe --- /dev/null +++ b/media/src/main/res/values/themes.xml @@ -0,0 +1,5 @@ + + + +