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
7 changes: 5 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ env:
jobs:
build-app:
runs-on: macos-latest
strategy:
matrix:
module: [app, app-service]
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -36,6 +39,6 @@ jobs:
- name: Make gradlew executable
run: chmod +x ./gradlew

- name: Build Android Package
- name: Build ${{ matrix.module }} module
run: |
./gradlew assembleRelease --parallel
./gradlew :${{ matrix.module }}:assembleRelease --parallel
1 change: 1 addition & 0 deletions app-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
19 changes: 19 additions & 0 deletions app-service/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Service

This is a sample project on how to use `compose-floating-window` on a service for long running operations

1. Create a new service. Like [this](src/main/java/com/github/only52607/compose/window/service/MyService.kt) for example

2. Add the permission to the manifest file

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

3. Declare the service in the manifest file

```xml
<service android:name=".MyService" />
```

4. Follow the `MyService` sample on how to use it on a service.
70 changes: 70 additions & 0 deletions app-service/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
alias(libs.plugins.compose.compiler)
}

android {
namespace = "com.github.only52607.compose.window.service"
compileSdk = libs.versions.compile.sdk.get().toInt()

defaultConfig {
applicationId = "com.github.only52607.compose.window"
minSdk = libs.versions.min.sdk.get().toInt()
targetSdk = libs.versions.target.sdk.get().toInt()
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
release {
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
signingConfig = signingConfigs.getByName("debug")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
buildFeatures {
compose = true
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {
implementation(project(":library"))
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.activity.compose)
implementation(platform(libs.compose.bom))
implementation(libs.compose.ui)
implementation(libs.compose.ui.graphics)
implementation(libs.compose.ui.tooling.preview)
implementation(libs.compose.material3)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.junit)
androidTestImplementation(libs.androidx.test.espresso)
androidTestImplementation(platform(libs.compose.bom))
androidTestImplementation(libs.compose.ui.test.junit4)
debugImplementation(libs.compose.ui.tooling)
debugImplementation(libs.compose.ui.test.manifest)
debugImplementation(libs.leak.canary)
}
21 changes: 21 additions & 0 deletions app-service/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,24 @@
package com.github.only52607.compose.window

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* 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("com.github.only52607.compose.window", appContext.packageName)
}
}
33 changes: 33 additions & 0 deletions app-service/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

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

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ComposeFloatingWindow"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
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=".MyService" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.github.only52607.compose.window.service

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
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.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
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.compose.collectAsStateWithLifecycle
import com.github.only52607.compose.window.service.ui.DialogPermission
import com.github.only52607.compose.window.service.ui.theme.ComposeFloatingWindowTheme


class MainActivity : ComponentActivity() {



override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeFloatingWindowTheme {
val showDialogPermission = remember { mutableStateOf(false) }

val context = LocalContext.current

val isShowing by MyService.serviceStarted.collectAsStateWithLifecycle(false)

Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(
Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(
onClick = {
MyService.start(context)
},
enabled = !isShowing
) {
Text("Show")
}
Spacer(modifier = Modifier.height(10.dp))
Button(
onClick = {
MyService.stop(context)
},
enabled = isShowing
) {
Text("Hide")
}
}
DialogPermission(showDialogState = showDialogPermission)
}
}
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.github.only52607.compose.window.service

import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import com.github.only52607.compose.window.ComposeFloatingWindow
import com.github.only52607.compose.window.service.ui.FloatingWindowContent
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

class MyService : Service() {
companion object {
private var _serviceStarted = MutableStateFlow(false)
val serviceStarted: StateFlow<Boolean>
get() = _serviceStarted.asStateFlow()

fun start(context: Context) {
val intent = Intent(context, MyService::class.java)
context.startService(intent)
}

fun stop(context: Context) {
val intent = Intent(context, MyService::class.java)
context.stopService(intent)
}
}

private val floatingWindow by lazy {
createFloatingWindow()
}

private fun createFloatingWindow(): ComposeFloatingWindow =
ComposeFloatingWindow(this).apply {
setContent {
FloatingWindowContent()
}
}

override fun onCreate() {
super.onCreate()
_serviceStarted.update { true }
floatingWindow.show()
}

override fun onBind(intent: Intent?): IBinder? = null

override fun onDestroy() {
_serviceStarted.update { false }
// Call close for cleanup and it will hide it in the process
floatingWindow.close()
super.onDestroy()
}
}
Loading