feat(ui/sif): download indicator

Signed-off-by: rhunk <101876869+rhunk@users.noreply.github.com>
This commit is contained in:
rhunk 2024-07-21 00:15:17 +02:00
parent de0342fdac
commit b2bc23d0e0
5 changed files with 103 additions and 13 deletions

View File

@ -1,7 +1,14 @@
package me.rhunk.snapenhance package me.rhunk.snapenhance
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build import android.os.Build
import androidx.core.net.toUri
import me.rhunk.snapenhance.common.BuildConfig
import me.rhunk.snapenhance.common.bridge.InternalFileHandleType import me.rhunk.snapenhance.common.bridge.InternalFileHandleType
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
@ -16,7 +23,7 @@ class RemoteSharedLibraryManager(
return runCatching { return runCatching {
okHttpClient.newCall( okHttpClient.newCall(
Request.Builder() Request.Builder()
.url("https://raw.githubusercontent.com/SnapEnhance/resources/main/sif/version") .url("${BuildConfig.SIF_ENDPOINT}/version")
.build() .build()
).execute().use { response -> ).execute().use { response ->
if (!response.isSuccessful) { if (!response.isSuccessful) {
@ -30,7 +37,7 @@ class RemoteSharedLibraryManager(
private fun downloadLatest(outputFile: File): Boolean { private fun downloadLatest(outputFile: File): Boolean {
val abi = Build.SUPPORTED_ABIS.firstOrNull() ?: return false val abi = Build.SUPPORTED_ABIS.firstOrNull() ?: return false
val request = Request.Builder() val request = Request.Builder()
.url("https://raw.githubusercontent.com/SnapEnhance/resources/main/sif/$abi.so") .url("${BuildConfig.SIF_ENDPOINT}/$abi.so")
.build() .build()
runCatching { runCatching {
okHttpClient.newCall(request).execute().use { response -> okHttpClient.newCall(request).execute().use { response ->
@ -60,8 +67,7 @@ class RemoteSharedLibraryManager(
return return
} }
val latestVersion = getVersion()?.trim() ?: run { val latestVersion = getVersion()?.trim() ?: run {
remoteSideContext.log.warn("Failed to get latest sif version") throw Exception("Failed to get latest sif version")
return
} }
if (currentVersion == latestVersion) { if (currentVersion == latestVersion) {
@ -73,12 +79,45 @@ class RemoteSharedLibraryManager(
if (downloadLatest(libraryFile)) { if (downloadLatest(libraryFile)) {
remoteSideContext.sharedPreferences.edit().putString("sif", latestVersion).commit() remoteSideContext.sharedPreferences.edit().putString("sif", latestVersion).commit()
remoteSideContext.shortToast("SIF updated to $latestVersion!") remoteSideContext.shortToast("SIF updated to $latestVersion!")
if (currentVersion.isNotEmpty()) {
val notificationManager = remoteSideContext.androidContext.getSystemService(NotificationManager::class.java)
val channelId = "sif_update"
notificationManager.createNotificationChannel(
NotificationChannel(
channelId,
"SIF Updates",
NotificationManager.IMPORTANCE_DEFAULT
)
)
notificationManager.notify(
System.nanoTime().toInt(),
Notification.Builder(remoteSideContext.androidContext, channelId)
.setContentTitle("SnapEnhance")
.setContentText("Security Features have been updated to version $latestVersion")
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentIntent(PendingIntent.getActivity(
remoteSideContext.androidContext,
0,
Intent().apply {
action = Intent.ACTION_VIEW
data = "https://github.com/SnapEnhance/resources".toUri()
flags = Intent.FLAG_ACTIVITY_NEW_TASK
},
PendingIntent.FLAG_UPDATE_CURRENT
)).build()
)
}
// force restart snapchat // force restart snapchat
runCatching { runCatching {
remoteSideContext.config.configStateListener?.takeIf { it.asBinder().pingBinder() }?.onRestartRequired() remoteSideContext.config.configStateListener?.takeIf { it.asBinder().pingBinder() }?.onRestartRequired()
} }
} else { } else {
remoteSideContext.log.warn("Failed to download latest sif") remoteSideContext.log.warn("Failed to download latest sif")
throw Exception("Failed to download latest sif")
} }
} }
} }

View File

@ -133,7 +133,11 @@ class RemoteSideContext(
} }
} }
coroutineScope.launch { coroutineScope.launch {
remoteSharedLibraryManager.init() runCatching {
remoteSharedLibraryManager.init()
}.onFailure {
log.error("Failed to init RemoteSharedLibraryManager", it)
}
} }
} }
}.onFailure { }.onFailure {

View File

@ -66,9 +66,9 @@ class SetupActivity : ComponentActivity() {
if (isFirstRun || hasRequirement(Requirements.MAPPINGS)) { if (isFirstRun || hasRequirement(Requirements.MAPPINGS)) {
add(MappingsScreen().apply { route = "mappings" }) add(MappingsScreen().apply { route = "mappings" })
} }
/*if (isFirstRun || hasRequirement(Requirements.SIF)) { if (hasRequirement(Requirements.SIF)) {
add(SecurityScreen().apply { route = "security" }) add(SecurityScreen().apply { route = "security" })
}*/ }
} }
// If there are no required screens, we can just finish the activity // If there are no required screens, we can just finish the activity

View File

@ -1,6 +1,9 @@
package me.rhunk.snapenhance.ui.setup.screens.impl package me.rhunk.snapenhance.ui.setup.screens.impl
import android.annotation.SuppressLint
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.WarningAmber import androidx.compose.material.icons.filled.WarningAmber
import androidx.compose.material3.* import androidx.compose.material3.*
@ -10,16 +13,22 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.rhunk.snapenhance.ui.setup.screens.SetupScreen import me.rhunk.snapenhance.ui.setup.screens.SetupScreen
class SecurityScreen : SetupScreen() { class SecurityScreen : SetupScreen() {
@SuppressLint("ApplySharedPref")
@Composable @Composable
override fun Content() { override fun Content() {
Icon( Icon(
imageVector = Icons.Default.WarningAmber, imageVector = Icons.Default.WarningAmber,
contentDescription = null, contentDescription = null,
modifier = Modifier.padding(16.dp).size(30.dp), modifier = Modifier
.padding(16.dp)
.size(30.dp),
) )
DialogText( DialogText(
@ -57,6 +66,47 @@ class SecurityScreen : SetupScreen() {
) )
} }
var downloadJob by remember { mutableStateOf(null as Job?) }
var jobError by remember { mutableStateOf(null as Throwable?) }
if (downloadJob != null) {
AlertDialog(onDismissRequest = {
downloadJob?.cancel()
downloadJob = null
}, confirmButton = {}, text = {
Column(
modifier = Modifier.verticalScroll(rememberScrollState()).fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
if (jobError != null) {
Text("Failed to download the required files.\n\n${jobError?.message}")
} else {
Text("Downloading the required files...")
CircularProgressIndicator(modifier = Modifier.padding(16.dp))
}
}
})
}
fun newDownloadJob() {
downloadJob?.cancel()
downloadJob = context.coroutineScope.launch {
context.sharedPreferences.edit().putString("sif", "").commit()
runCatching {
context.remoteSharedLibraryManager.init()
}.onFailure {
jobError = it
context.log.error("Failed to download the required files", it)
}.onSuccess {
downloadJob = null
withContext(Dispatchers.Main) {
goNext()
}
}
}
}
Column ( Column (
modifier = Modifier.padding(16.dp), modifier = Modifier.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
@ -64,11 +114,7 @@ class SecurityScreen : SetupScreen() {
) { ) {
Button( Button(
onClick = { onClick = {
context.coroutineScope.launch { newDownloadJob()
context.sharedPreferences.edit().putString("sif", "").commit()
context.remoteSharedLibraryManager.init()
}
goNext()
} }
) { ) {
Text("Accept and continue", fontSize = 18.sp, fontWeight = FontWeight.Bold) Text("Accept and continue", fontSize = 18.sp, fontWeight = FontWeight.Bold)

View File

@ -30,6 +30,7 @@ android {
standardOutput = gitHash standardOutput = gitHash
} }
buildConfigField("String", "GIT_HASH", "\"${gitHash.toString(Charsets.UTF_8).trim()}\"") buildConfigField("String", "GIT_HASH", "\"${gitHash.toString(Charsets.UTF_8).trim()}\"")
buildConfigField("String", "SIF_ENDPOINT", "\"${properties["debug_sif_endpoint"]?.toString() ?: "https://raw.githubusercontent.com/SnapEnhance/resources/main/sif"}\"")
} }
compileOptions { compileOptions {