Skip to content
Closed
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
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ leakcanary = "2.14"
hilt = "2.59.1"
ksp = "2.3.5"

metro = "0.10.2"

datastore = "1.2.0"

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

metro-runtime = { module = "dev.zacsweers.metro:runtime", version.ref = "metro" }

datastore = { module = "androidx.datastore:datastore", version.ref = "datastore" }
datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastore" }

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

spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
metro = { id = "dev.zacsweers.metro", version.ref = "metro" }

[bundles]
datastore = ["datastore", "datastore-preferences"]
Expand Down
1 change: 1 addition & 0 deletions samples/service-metro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
39 changes: 39 additions & 0 deletions samples/service-metro/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
plugins {
id("android.application")
id("android.tests")
id("sample.common.deps")

alias(libs.plugins.metro)
}

android {
namespace = "io.github.arthurkun.service.metro"

defaultConfig {
applicationId = "io.github.arthurkun.floating.window.metro"
versionCode = 1
versionName = "1.0"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
signingConfig = signingConfigs.getByName("debug")
}
}
}

dependencies {
implementation(project(":library"))

debugImplementation(libs.compose.ui.tooling)
debugImplementation(libs.compose.ui.test.manifest)

implementation(libs.metro.runtime)

implementation(libs.bundles.datastore)
}
21 changes: 21 additions & 0 deletions samples/service-metro/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.arthurkun.service.metro

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("io.github.arthurkun.floating.window.metro", appContext.packageName)
}
}
29 changes: 29 additions & 0 deletions samples/service-metro/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
android:name=".FloatingApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ComposeFloatingWindow">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.ComposeFloatingWindow">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<service android:name=".service.MyService" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.github.arthurkun.service.metro

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Warning
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.compose.LocalLifecycleOwner
import com.github.only52607.compose.core.checkOverlayPermission
import com.github.only52607.compose.core.requestOverlayPermission

@Composable
fun DialogPermission(
onDismiss: () -> Unit = { },
) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current

DisposableEffect(lifecycleOwner.lifecycle) {
val observer = object : DefaultLifecycleObserver {
override fun onResume(owner: LifecycleOwner) {
val overlayGranted = checkOverlayPermission(context)
if (overlayGranted) {
onDismiss()
}
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}

AlertDialog(
icon = {
Icon(
Icons.Default.Warning,
contentDescription = stringResource(R.string.permission_required),
)
},
title = {
Text(text = stringResource(id = R.string.permission_required))
},
text = {
Text(text = stringResource(R.string.message_permission_to_draw_on_top_others_apps))
},
onDismissRequest = onDismiss,
confirmButton = {
TextButton(
onClick = {
requestOverlayPermission(context)
},
) {
Text(stringResource(R.string.grant_permission))
}
},
dismissButton = {
TextButton(onClick = onDismiss) {
Text(stringResource(android.R.string.cancel))
}
},
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.arthurkun.service.metro

import android.app.Application
import dev.zacsweers.metro.createGraphFactory
import io.github.arthurkun.service.metro.di.AppGraph

class FloatingApplication : Application() {

val appGraph: AppGraph by lazy {
createGraphFactory<AppGraph.Factory>().create(this)
}

companion object {
lateinit var instance: FloatingApplication
private set
}

override fun onCreate() {
super.onCreate()
instance = this
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package io.github.arthurkun.service.metro

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
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.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.github.only52607.compose.core.checkOverlayPermission
import io.github.arthurkun.service.metro.repository.UserPreferencesRepository
import io.github.arthurkun.service.metro.service.MyService
import io.github.arthurkun.service.metro.ui.theme.ComposeFloatingWindowTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity : AppCompatActivity() {

private val userPreferencesRepository: UserPreferencesRepository by lazy {
FloatingApplication.instance.appGraph.userPreferencesRepository
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
userPreferencesRepository.darkModeFlow.collect { darkMode ->
withContext(Dispatchers.Main) {
AppCompatDelegate.setDefaultNightMode(
if (darkMode) {
AppCompatDelegate.MODE_NIGHT_YES
} else {
AppCompatDelegate.MODE_NIGHT_NO
},
)
}
}
}
}

setContent {
ComposeFloatingWindowTheme {
var showDialogPermission by rememberSaveable { mutableStateOf(false) }
val isShowing by MyService.serviceStarted.collectAsStateWithLifecycle()
val context = LocalContext.current

Scaffold(
modifier = Modifier.fillMaxSize(),
) { innerPadding ->
Column(
Modifier
.padding(innerPadding)
.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
Button(
onClick = {
val overlayPermission = checkOverlayPermission(context)
if (overlayPermission) {
MyService.start(context)
} else {
showDialogPermission = true
}
},
enabled = !isShowing,
) {
Text("Show")
}
Spacer(modifier = Modifier.height(8.dp))
Button(
onClick = {
MyService.stop(context)
},
enabled = isShowing,
) {
Text("Hide")
}
}
}

if (showDialogPermission) {
DialogPermission(
onDismiss = {
showDialogPermission = false
},
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.arthurkun.service.metro.di

import android.app.Application
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.DependencyGraph
import dev.zacsweers.metro.Provides
import dev.zacsweers.metro.SingleIn
import io.github.arthurkun.service.metro.module.dataStore
import io.github.arthurkun.service.metro.repository.UserPreferencesRepository

@DependencyGraph(AppScope::class)
interface AppGraph {

val userPreferencesRepository: UserPreferencesRepository

val serviceGraphFactory: ServiceGraph.Factory

@Provides
fun provideApplicationContext(application: Application): Context = application

@SingleIn(AppScope::class)
@Provides
fun provideDataStore(context: Context): DataStore<Preferences> {
return context.dataStore
}

@DependencyGraph.Factory
fun interface Factory {
fun create(@Provides application: Application): AppGraph
}
}
Loading