Skip to content

Commit 6415656

Browse files
CopilotArthurKun21
andcommitted
Add service-metro sample app with Metro DI framework
Co-authored-by: ArthurKun21 <16458204+ArthurKun21@users.noreply.github.com>
1 parent 5c14e44 commit 6415656

40 files changed

+1036
-0
lines changed

gradle/libs.versions.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ leakcanary = "2.14"
2525
hilt = "2.59"
2626
ksp = "2.3.5"
2727

28+
metro = "0.10.2"
29+
2830
datastore = "1.2.0"
2931

3032
spotless = "8.2.1"
@@ -61,6 +63,8 @@ leak-canary = { module = "com.squareup.leakcanary:leakcanary-android", version.r
6163
dagger-hilt-compiler = { group = "com.google.dagger", name = "hilt-compiler", version.ref = "hilt" }
6264
dagger-hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
6365

66+
metro-runtime = { module = "dev.zacsweers.metro:runtime", version.ref = "metro" }
67+
6468
datastore = { module = "androidx.datastore:datastore", version.ref = "datastore" }
6569
datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastore" }
6670

@@ -86,6 +90,7 @@ ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
8690
hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
8791

8892
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
93+
metro = { id = "dev.zacsweers.metro", version.ref = "metro" }
8994

9095
[bundles]
9196
datastore = ["datastore", "datastore-preferences"]

samples/service-metro/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
plugins {
2+
id("android.application")
3+
id("android.tests")
4+
id("sample.common.deps")
5+
6+
alias(libs.plugins.metro)
7+
}
8+
9+
android {
10+
namespace = "io.github.arthurkun.service.metro"
11+
12+
defaultConfig {
13+
applicationId = "io.github.arthurkun.floating.window.metro"
14+
versionCode = 1
15+
versionName = "1.0"
16+
}
17+
18+
buildTypes {
19+
release {
20+
isMinifyEnabled = false
21+
proguardFiles(
22+
getDefaultProguardFile("proguard-android-optimize.txt"),
23+
"proguard-rules.pro",
24+
)
25+
signingConfig = signingConfigs.getByName("debug")
26+
}
27+
}
28+
}
29+
30+
dependencies {
31+
implementation(project(":library"))
32+
33+
debugImplementation(libs.compose.ui.tooling)
34+
debugImplementation(libs.compose.ui.test.manifest)
35+
36+
implementation(libs.metro.runtime)
37+
38+
implementation(libs.bundles.datastore)
39+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.arthurkun.service.metro
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import androidx.test.platform.app.InstrumentationRegistry
5+
import org.junit.Assert.assertEquals
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
/**
10+
* Instrumented test, which will execute on an Android device.
11+
*
12+
* See [testing documentation](http://d.android.com/tools/testing).
13+
*/
14+
@RunWith(AndroidJUnit4::class)
15+
class ExampleInstrumentedTest {
16+
@Test
17+
fun useAppContext() {
18+
// Context of the app under test.
19+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
20+
assertEquals("io.github.arthurkun.floating.window.metro", appContext.packageName)
21+
}
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
5+
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
6+
7+
<application
8+
android:name=".FloatingApplication"
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/Theme.ComposeFloatingWindow">
15+
<activity
16+
android:name=".MainActivity"
17+
android:exported="true"
18+
android:theme="@style/Theme.ComposeFloatingWindow">
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
26+
<service android:name=".service.MyService" />
27+
</application>
28+
29+
</manifest>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.github.arthurkun.service.metro
2+
3+
import androidx.compose.material.icons.Icons
4+
import androidx.compose.material.icons.filled.Warning
5+
import androidx.compose.material3.AlertDialog
6+
import androidx.compose.material3.Icon
7+
import androidx.compose.material3.Text
8+
import androidx.compose.material3.TextButton
9+
import androidx.compose.runtime.Composable
10+
import androidx.compose.runtime.DisposableEffect
11+
import androidx.compose.ui.platform.LocalContext
12+
import androidx.compose.ui.res.stringResource
13+
import androidx.lifecycle.DefaultLifecycleObserver
14+
import androidx.lifecycle.LifecycleOwner
15+
import androidx.lifecycle.compose.LocalLifecycleOwner
16+
import com.github.only52607.compose.core.checkOverlayPermission
17+
import com.github.only52607.compose.core.requestOverlayPermission
18+
19+
@Composable
20+
fun DialogPermission(
21+
onDismiss: () -> Unit = { },
22+
) {
23+
val context = LocalContext.current
24+
val lifecycleOwner = LocalLifecycleOwner.current
25+
26+
DisposableEffect(lifecycleOwner.lifecycle) {
27+
val observer = object : DefaultLifecycleObserver {
28+
override fun onResume(owner: LifecycleOwner) {
29+
val overlayGranted = checkOverlayPermission(context)
30+
if (overlayGranted) {
31+
onDismiss()
32+
}
33+
}
34+
}
35+
lifecycleOwner.lifecycle.addObserver(observer)
36+
onDispose {
37+
lifecycleOwner.lifecycle.removeObserver(observer)
38+
}
39+
}
40+
41+
AlertDialog(
42+
icon = {
43+
Icon(
44+
Icons.Default.Warning,
45+
contentDescription = stringResource(R.string.permission_required),
46+
)
47+
},
48+
title = {
49+
Text(text = stringResource(id = R.string.permission_required))
50+
},
51+
text = {
52+
Text(text = stringResource(R.string.message_permission_to_draw_on_top_others_apps))
53+
},
54+
onDismissRequest = onDismiss,
55+
confirmButton = {
56+
TextButton(
57+
onClick = {
58+
requestOverlayPermission(context)
59+
},
60+
) {
61+
Text(stringResource(R.string.grant_permission))
62+
}
63+
},
64+
dismissButton = {
65+
TextButton(onClick = onDismiss) {
66+
Text(stringResource(android.R.string.cancel))
67+
}
68+
},
69+
)
70+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.arthurkun.service.metro
2+
3+
import android.app.Application
4+
import dev.zacsweers.metro.createGraphFactory
5+
import io.github.arthurkun.service.metro.di.AppGraph
6+
7+
class FloatingApplication : Application() {
8+
9+
val appGraph: AppGraph by lazy {
10+
createGraphFactory<AppGraph.Factory>().create(this)
11+
}
12+
13+
companion object {
14+
lateinit var instance: FloatingApplication
15+
private set
16+
}
17+
18+
override fun onCreate() {
19+
super.onCreate()
20+
instance = this
21+
}
22+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package io.github.arthurkun.service.metro
2+
3+
import android.os.Bundle
4+
import androidx.activity.compose.setContent
5+
import androidx.activity.enableEdgeToEdge
6+
import androidx.appcompat.app.AppCompatActivity
7+
import androidx.appcompat.app.AppCompatDelegate
8+
import androidx.compose.foundation.layout.Arrangement
9+
import androidx.compose.foundation.layout.Column
10+
import androidx.compose.foundation.layout.Spacer
11+
import androidx.compose.foundation.layout.fillMaxSize
12+
import androidx.compose.foundation.layout.height
13+
import androidx.compose.foundation.layout.padding
14+
import androidx.compose.material3.Button
15+
import androidx.compose.material3.Scaffold
16+
import androidx.compose.material3.Text
17+
import androidx.compose.runtime.getValue
18+
import androidx.compose.runtime.mutableStateOf
19+
import androidx.compose.runtime.saveable.rememberSaveable
20+
import androidx.compose.runtime.setValue
21+
import androidx.compose.ui.Alignment
22+
import androidx.compose.ui.Modifier
23+
import androidx.compose.ui.platform.LocalContext
24+
import androidx.compose.ui.unit.dp
25+
import androidx.lifecycle.Lifecycle
26+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
27+
import androidx.lifecycle.lifecycleScope
28+
import androidx.lifecycle.repeatOnLifecycle
29+
import com.github.only52607.compose.core.checkOverlayPermission
30+
import io.github.arthurkun.service.metro.repository.UserPreferencesRepository
31+
import io.github.arthurkun.service.metro.service.MyService
32+
import io.github.arthurkun.service.metro.ui.theme.ComposeFloatingWindowTheme
33+
import kotlinx.coroutines.Dispatchers
34+
import kotlinx.coroutines.launch
35+
import kotlinx.coroutines.withContext
36+
37+
class MainActivity : AppCompatActivity() {
38+
39+
private val userPreferencesRepository: UserPreferencesRepository by lazy {
40+
FloatingApplication.instance.appGraph.userPreferencesRepository
41+
}
42+
43+
override fun onCreate(savedInstanceState: Bundle?) {
44+
super.onCreate(savedInstanceState)
45+
enableEdgeToEdge()
46+
47+
lifecycleScope.launch {
48+
repeatOnLifecycle(Lifecycle.State.CREATED) {
49+
userPreferencesRepository.darkModeFlow.collect { darkMode ->
50+
withContext(Dispatchers.Main) {
51+
AppCompatDelegate.setDefaultNightMode(
52+
if (darkMode) {
53+
AppCompatDelegate.MODE_NIGHT_YES
54+
} else {
55+
AppCompatDelegate.MODE_NIGHT_NO
56+
},
57+
)
58+
}
59+
}
60+
}
61+
}
62+
63+
setContent {
64+
ComposeFloatingWindowTheme {
65+
var showDialogPermission by rememberSaveable { mutableStateOf(false) }
66+
val isShowing by MyService.serviceStarted.collectAsStateWithLifecycle()
67+
val context = LocalContext.current
68+
69+
Scaffold(
70+
modifier = Modifier.fillMaxSize(),
71+
) { innerPadding ->
72+
Column(
73+
Modifier
74+
.padding(innerPadding)
75+
.fillMaxSize(),
76+
verticalArrangement = Arrangement.Center,
77+
horizontalAlignment = Alignment.CenterHorizontally,
78+
) {
79+
Button(
80+
onClick = {
81+
val overlayPermission = checkOverlayPermission(context)
82+
if (overlayPermission) {
83+
MyService.start(context)
84+
} else {
85+
showDialogPermission = true
86+
}
87+
},
88+
enabled = !isShowing,
89+
) {
90+
Text("Show")
91+
}
92+
Spacer(modifier = Modifier.height(8.dp))
93+
Button(
94+
onClick = {
95+
MyService.stop(context)
96+
},
97+
enabled = isShowing,
98+
) {
99+
Text("Hide")
100+
}
101+
}
102+
}
103+
104+
if (showDialogPermission) {
105+
DialogPermission(
106+
onDismiss = {
107+
showDialogPermission = false
108+
},
109+
)
110+
}
111+
}
112+
}
113+
}
114+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.arthurkun.service.metro.di
2+
3+
import android.app.Application
4+
import android.content.Context
5+
import androidx.datastore.core.DataStore
6+
import androidx.datastore.preferences.core.Preferences
7+
import dev.zacsweers.metro.AppScope
8+
import dev.zacsweers.metro.DependencyGraph
9+
import dev.zacsweers.metro.Provides
10+
import dev.zacsweers.metro.SingleIn
11+
import io.github.arthurkun.service.metro.module.dataStore
12+
import io.github.arthurkun.service.metro.repository.UserPreferencesRepository
13+
14+
@DependencyGraph(AppScope::class)
15+
interface AppGraph {
16+
17+
val userPreferencesRepository: UserPreferencesRepository
18+
19+
val serviceGraphFactory: ServiceGraph.Factory
20+
21+
@Provides
22+
fun provideApplicationContext(application: Application): Context = application
23+
24+
@SingleIn(AppScope::class)
25+
@Provides
26+
fun provideDataStore(context: Context): DataStore<Preferences> {
27+
return context.dataStore
28+
}
29+
30+
@DependencyGraph.Factory
31+
fun interface Factory {
32+
fun create(@Provides application: Application): AppGraph
33+
}
34+
}

0 commit comments

Comments
 (0)