App update module
This commit is contained in:
@@ -38,6 +38,7 @@ android {
|
||||
|
||||
buildFeatures {
|
||||
compose = true
|
||||
buildConfig = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
|
||||
|
||||
<application
|
||||
android:label="@string/app_name"
|
||||
@@ -16,6 +17,16 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:authorities="${applicationId}.fileprovider"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/file_paths" />
|
||||
</provider>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
||||
@@ -1,42 +1,121 @@
|
||||
package de.haushalt.app
|
||||
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.aspectRatio
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
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.unit.dp
|
||||
import de.haushalt.app.data.AppUpdater
|
||||
import de.haushalt.app.data.AppVersion
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
private sealed interface UpdateState {
|
||||
data object Idle : UpdateState
|
||||
data object Checking : UpdateState
|
||||
data object NoUpdate : UpdateState
|
||||
data class Available(val version: AppVersion) : UpdateState
|
||||
data object Downloading : UpdateState
|
||||
data class Error(val message: String) : UpdateState
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun StartScreen(onOpenTasks: () -> Unit = {}) {
|
||||
LazyVerticalGrid(
|
||||
columns = GridCells.Adaptive(minSize = 160.dp),
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp),
|
||||
) {
|
||||
item {
|
||||
Card(
|
||||
onClick = onOpenTasks,
|
||||
modifier = Modifier
|
||||
.padding(8.dp)
|
||||
.aspectRatio(1f),
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentAlignment = Alignment.Center,
|
||||
val context = LocalContext.current
|
||||
val scope = rememberCoroutineScope()
|
||||
var updateState by remember { mutableStateOf<UpdateState>(UpdateState.Idle) }
|
||||
|
||||
Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
|
||||
LazyVerticalGrid(
|
||||
columns = GridCells.Adaptive(minSize = 160.dp),
|
||||
modifier = Modifier.weight(1f),
|
||||
) {
|
||||
item {
|
||||
Card(
|
||||
onClick = onOpenTasks,
|
||||
modifier = Modifier
|
||||
.padding(8.dp)
|
||||
.aspectRatio(1f),
|
||||
) {
|
||||
Text(
|
||||
text = "Tasks",
|
||||
style = MaterialTheme.typography.titleLarge,
|
||||
)
|
||||
Box(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Text(
|
||||
text = "Tasks",
|
||||
style = MaterialTheme.typography.titleLarge,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(Modifier.padding(8.dp))
|
||||
|
||||
Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
|
||||
when (val state = updateState) {
|
||||
UpdateState.Idle -> TextButton(onClick = {
|
||||
scope.launch {
|
||||
updateState = UpdateState.Checking
|
||||
try {
|
||||
val version = withContext(Dispatchers.IO) { AppUpdater.checkUpdate() }
|
||||
updateState = if (version != null) UpdateState.Available(version)
|
||||
else UpdateState.NoUpdate
|
||||
} catch (e: Exception) {
|
||||
updateState = UpdateState.Error(e.message ?: "Fehler")
|
||||
}
|
||||
}
|
||||
}) { Text("Update prüfen") }
|
||||
|
||||
UpdateState.Checking -> Text("Prüfe…", style = MaterialTheme.typography.bodySmall)
|
||||
|
||||
UpdateState.NoUpdate -> Text(
|
||||
"App ist aktuell",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
|
||||
is UpdateState.Available -> Button(onClick = {
|
||||
scope.launch {
|
||||
updateState = UpdateState.Downloading
|
||||
try {
|
||||
withContext(Dispatchers.IO) {
|
||||
AppUpdater.downloadAndInstall(context, state.version)
|
||||
}
|
||||
updateState = UpdateState.Idle
|
||||
} catch (e: Exception) {
|
||||
updateState = UpdateState.Error(e.message ?: "Fehler")
|
||||
}
|
||||
}
|
||||
}) { Text("Update installieren (v${state.version.versionCode})") }
|
||||
|
||||
UpdateState.Downloading -> Text(
|
||||
"Lade herunter…",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
|
||||
is UpdateState.Error -> TextButton(onClick = { updateState = UpdateState.Idle }) {
|
||||
Text(state.message, color = MaterialTheme.colorScheme.error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,23 +8,32 @@ import retrofit2.Retrofit
|
||||
import retrofit2.converter.kotlinx.serialization.asConverterFactory
|
||||
|
||||
object ApiClient {
|
||||
private const val BASE_URL = "http://192.168.178.34:8080/api/"
|
||||
const val BASE_URL = "http://192.168.178.34:8080/"
|
||||
private const val API_URL = "${BASE_URL}api/"
|
||||
|
||||
private val json = Json {
|
||||
ignoreUnknownKeys = true
|
||||
explicitNulls = false
|
||||
}
|
||||
|
||||
private val okHttp: OkHttpClient = OkHttpClient.Builder()
|
||||
val okHttp: OkHttpClient = OkHttpClient.Builder()
|
||||
.addInterceptor(
|
||||
HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BASIC }
|
||||
)
|
||||
.build()
|
||||
|
||||
val taskApi: TaskApi = Retrofit.Builder()
|
||||
private val retrofit: Retrofit = Retrofit.Builder()
|
||||
.baseUrl(API_URL)
|
||||
.client(okHttp)
|
||||
.addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
|
||||
.build()
|
||||
|
||||
val taskApi: TaskApi = retrofit.create(TaskApi::class.java)
|
||||
|
||||
val appUpdateApi: AppUpdateApi = Retrofit.Builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.client(okHttp)
|
||||
.addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
|
||||
.build()
|
||||
.create(TaskApi::class.java)
|
||||
.create(AppUpdateApi::class.java)
|
||||
}
|
||||
|
||||
15
app/app/src/main/java/de/haushalt/app/data/AppUpdateApi.kt
Normal file
15
app/app/src/main/java/de/haushalt/app/data/AppUpdateApi.kt
Normal file
@@ -0,0 +1,15 @@
|
||||
package de.haushalt.app.data
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import retrofit2.http.GET
|
||||
|
||||
interface AppUpdateApi {
|
||||
@GET("app/version.json")
|
||||
suspend fun getVersion(): AppVersion
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class AppVersion(
|
||||
val versionCode: Int,
|
||||
val apkFile: String,
|
||||
)
|
||||
36
app/app/src/main/java/de/haushalt/app/data/AppUpdater.kt
Normal file
36
app/app/src/main/java/de/haushalt/app/data/AppUpdater.kt
Normal file
@@ -0,0 +1,36 @@
|
||||
package de.haushalt.app.data
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.core.content.FileProvider
|
||||
import de.haushalt.app.BuildConfig
|
||||
import okhttp3.Request
|
||||
import java.io.File
|
||||
|
||||
object AppUpdater {
|
||||
suspend fun checkUpdate(): AppVersion? {
|
||||
val remote = ApiClient.appUpdateApi.getVersion()
|
||||
return if (remote.versionCode > BuildConfig.VERSION_CODE) remote else null
|
||||
}
|
||||
|
||||
fun downloadAndInstall(context: Context, version: AppVersion) {
|
||||
val apkUrl = "${ApiClient.BASE_URL}app/${version.apkFile}"
|
||||
val apkFile = File(context.cacheDir, "apk/haushalt.apk")
|
||||
apkFile.parentFile?.mkdirs()
|
||||
|
||||
val request = Request.Builder().url(apkUrl).build()
|
||||
ApiClient.okHttp.newCall(request).execute().use { response ->
|
||||
apkFile.outputStream().use { out ->
|
||||
response.body!!.byteStream().copyTo(out)
|
||||
}
|
||||
}
|
||||
|
||||
val uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", apkFile)
|
||||
val intent = Intent(Intent.ACTION_VIEW).apply {
|
||||
setDataAndType(uri, "application/vnd.android.package-archive")
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
context.startActivity(intent)
|
||||
}
|
||||
}
|
||||
4
app/app/src/main/res/xml/file_paths.xml
Normal file
4
app/app/src/main/res/xml/file_paths.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths>
|
||||
<cache-path name="apk" path="apk/" />
|
||||
</paths>
|
||||
Reference in New Issue
Block a user