mirror of
https://github.com/revanced/revanced-manager-compose-old.git
synced 2025-04-30 22:44:27 +02:00
fix: worker crash on Android <12
This commit is contained in:
parent
b41f483f1c
commit
0a1b46ea41
@ -36,20 +36,17 @@ import java.io.File
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.StandardCopyOption
|
||||
|
||||
class PatcherWorker(context: Context, parameters: WorkerParameters, private val api: API): CoroutineWorker(context, parameters) ,KoinComponent {
|
||||
class PatcherWorker(context: Context, parameters: WorkerParameters, private val api: API) :
|
||||
CoroutineWorker(context, parameters), KoinComponent {
|
||||
|
||||
val tag = "ReVanced Manager"
|
||||
private val workdir = createWorkDir()
|
||||
override suspend fun doWork(): Result {
|
||||
|
||||
if (runAttemptCount > 0) {
|
||||
return Result.failure(
|
||||
androidx.work.Data.Builder()
|
||||
.putString("error", "Android requested retrying but retrying is disabled")
|
||||
.build()
|
||||
) // don't retry
|
||||
override suspend fun getForegroundInfo(): ForegroundInfo {
|
||||
return ForegroundInfo(1, createNotification())
|
||||
}
|
||||
|
||||
private fun createNotification(): Notification {
|
||||
val notificationIntent = Intent(applicationContext, PatcherWorker::class.java)
|
||||
val pendingIntent: PendingIntent = PendingIntent.getActivity(
|
||||
applicationContext, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE
|
||||
@ -60,24 +57,32 @@ class PatcherWorker(context: Context, parameters: WorkerParameters, private val
|
||||
val notificationManager =
|
||||
ContextCompat.getSystemService(applicationContext, NotificationManager::class.java)
|
||||
notificationManager!!.createNotificationChannel(channel)
|
||||
val notification: Notification = Notification.Builder(applicationContext, channel.id)
|
||||
return Notification.Builder(applicationContext, channel.id)
|
||||
.setContentTitle(applicationContext.getText(R.string.patcher_notification_title))
|
||||
.setContentText(applicationContext.getText(R.string.patcher_notification_message))
|
||||
.setLargeIcon(Icon.createWithResource(applicationContext, R.drawable.manager))
|
||||
.setSmallIcon(Icon.createWithResource(applicationContext, R.drawable.manager))
|
||||
.setContentIntent(pendingIntent).build()
|
||||
}
|
||||
|
||||
override suspend fun doWork(): Result {
|
||||
if (runAttemptCount > 0) {
|
||||
Log.d(tag, "Android requested retrying but retrying is disabled.")
|
||||
return Result.failure() // don't retry
|
||||
}
|
||||
|
||||
try {
|
||||
setForeground(ForegroundInfo(1, createNotification()))
|
||||
} catch (e: Exception) {
|
||||
Log.d(tag, "Failed to set foreground info:", e)
|
||||
}
|
||||
|
||||
setForeground(ForegroundInfo(1, notification))
|
||||
return try {
|
||||
runPatcher(workdir)
|
||||
Result.success()
|
||||
} catch (e: Exception) {
|
||||
Log.e(tag, "Error while patching", e)
|
||||
Result.failure(
|
||||
androidx.work.Data.Builder()
|
||||
.putString("error", "Error while patching: ${e.message ?: e::class.simpleName}")
|
||||
.build()
|
||||
)
|
||||
Log.e(tag, "Error while patching: ${e.message ?: e::class.simpleName}")
|
||||
Result.failure()
|
||||
}
|
||||
}
|
||||
|
||||
@ -166,8 +171,10 @@ class PatcherWorker(context: Context, parameters: WorkerParameters, private val
|
||||
}
|
||||
|
||||
ZipFile(patchedFile).use { fs -> // somehow this function is the most resource intensive
|
||||
result.dexFiles.forEach { Logging.log += "Writing dex file ${it.name}\n"
|
||||
fs.addEntryCompressData(ZipEntry.createWithName(it.name), it.stream.readBytes())}
|
||||
result.dexFiles.forEach {
|
||||
Logging.log += "Writing dex file ${it.name}\n"
|
||||
fs.addEntryCompressData(ZipEntry.createWithName(it.name), it.stream.readBytes())
|
||||
}
|
||||
|
||||
Logging.log += "Aligning apk!\n"
|
||||
result.resourceFile?.let {
|
||||
@ -190,6 +197,7 @@ class PatcherWorker(context: Context, parameters: WorkerParameters, private val
|
||||
val (patches) = patches.value as? Resource.Success ?: return listOf()
|
||||
return patches.filter { patch -> ids.any { it == patch.patchName } }
|
||||
}
|
||||
|
||||
private fun createWorkDir(): File {
|
||||
return applicationContext.filesDir.resolve("tmp-${System.currentTimeMillis()}")
|
||||
.also { it.mkdirs() }
|
||||
|
@ -1,5 +1,6 @@
|
||||
package app.revanced.manager.ui.screen
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import androidx.compose.animation.core.LinearEasing
|
||||
import androidx.compose.animation.core.tween
|
||||
import androidx.compose.foundation.layout.*
|
||||
@ -24,6 +25,7 @@ import kotlinx.coroutines.launch
|
||||
import org.koin.androidx.compose.getViewModel
|
||||
|
||||
|
||||
@SuppressLint("UnrememberedMutableState")
|
||||
@Composable
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
fun PatchingScreen(
|
||||
|
@ -40,6 +40,8 @@ class PatchingScreenViewModel(val app: Application) : ViewModel() {
|
||||
|
||||
fun startPatcher() {
|
||||
cancelPatching() // cancel patching if its still running
|
||||
Logging.log = "" // clear logs
|
||||
|
||||
WorkManager.getInstance(app)
|
||||
.enqueueUniqueWork("patching", ExistingWorkPolicy.KEEP, patcherWorker) // enqueue patching process
|
||||
liveData.observeForever(observer) // start observing patch status
|
||||
|
Loading…
x
Reference in New Issue
Block a user