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.Files
|
||||||
import java.nio.file.StandardCopyOption
|
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"
|
val tag = "ReVanced Manager"
|
||||||
private val workdir = createWorkDir()
|
private val workdir = createWorkDir()
|
||||||
override suspend fun doWork(): Result {
|
|
||||||
|
|
||||||
if (runAttemptCount > 0) {
|
override suspend fun getForegroundInfo(): ForegroundInfo {
|
||||||
return Result.failure(
|
return ForegroundInfo(1, createNotification())
|
||||||
androidx.work.Data.Builder()
|
}
|
||||||
.putString("error", "Android requested retrying but retrying is disabled")
|
|
||||||
.build()
|
|
||||||
) // don't retry
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private fun createNotification(): Notification {
|
||||||
val notificationIntent = Intent(applicationContext, PatcherWorker::class.java)
|
val notificationIntent = Intent(applicationContext, PatcherWorker::class.java)
|
||||||
val pendingIntent: PendingIntent = PendingIntent.getActivity(
|
val pendingIntent: PendingIntent = PendingIntent.getActivity(
|
||||||
applicationContext, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE
|
applicationContext, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE
|
||||||
@ -60,24 +57,32 @@ class PatcherWorker(context: Context, parameters: WorkerParameters, private val
|
|||||||
val notificationManager =
|
val notificationManager =
|
||||||
ContextCompat.getSystemService(applicationContext, NotificationManager::class.java)
|
ContextCompat.getSystemService(applicationContext, NotificationManager::class.java)
|
||||||
notificationManager!!.createNotificationChannel(channel)
|
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))
|
.setContentTitle(applicationContext.getText(R.string.patcher_notification_title))
|
||||||
.setContentText(applicationContext.getText(R.string.patcher_notification_message))
|
.setContentText(applicationContext.getText(R.string.patcher_notification_message))
|
||||||
.setLargeIcon(Icon.createWithResource(applicationContext, R.drawable.manager))
|
.setLargeIcon(Icon.createWithResource(applicationContext, R.drawable.manager))
|
||||||
.setSmallIcon(Icon.createWithResource(applicationContext, R.drawable.manager))
|
.setSmallIcon(Icon.createWithResource(applicationContext, R.drawable.manager))
|
||||||
.setContentIntent(pendingIntent).build()
|
.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 {
|
return try {
|
||||||
runPatcher(workdir)
|
runPatcher(workdir)
|
||||||
Result.success()
|
Result.success()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(tag, "Error while patching", e)
|
Log.e(tag, "Error while patching: ${e.message ?: e::class.simpleName}")
|
||||||
Result.failure(
|
Result.failure()
|
||||||
androidx.work.Data.Builder()
|
|
||||||
.putString("error", "Error while patching: ${e.message ?: e::class.simpleName}")
|
|
||||||
.build()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +157,7 @@ class PatcherWorker(context: Context, parameters: WorkerParameters, private val
|
|||||||
Logging.log += "Applying ${patches.size} patch(es)\n"
|
Logging.log += "Applying ${patches.size} patch(es)\n"
|
||||||
patcher.executePatches().forEach { (patch, result) ->
|
patcher.executePatches().forEach { (patch, result) ->
|
||||||
if (result.isFailure) {
|
if (result.isFailure) {
|
||||||
Logging.log += "Failed to apply $patch" + result.exceptionOrNull()!!.cause + "\n"
|
Logging.log += "Failed to apply $patch" + result.exceptionOrNull()!!.cause + "\n"
|
||||||
return@forEach
|
return@forEach
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,19 +171,21 @@ class PatcherWorker(context: Context, parameters: WorkerParameters, private val
|
|||||||
}
|
}
|
||||||
|
|
||||||
ZipFile(patchedFile).use { fs -> // somehow this function is the most resource intensive
|
ZipFile(patchedFile).use { fs -> // somehow this function is the most resource intensive
|
||||||
result.dexFiles.forEach { Logging.log += "Writing dex file ${it.name}\n"
|
result.dexFiles.forEach {
|
||||||
fs.addEntryCompressData(ZipEntry.createWithName(it.name), it.stream.readBytes())}
|
Logging.log += "Writing dex file ${it.name}\n"
|
||||||
|
fs.addEntryCompressData(ZipEntry.createWithName(it.name), it.stream.readBytes())
|
||||||
|
}
|
||||||
|
|
||||||
Logging.log += "Aligning apk!\n"
|
Logging.log += "Aligning apk!\n"
|
||||||
result.resourceFile?.let {
|
result.resourceFile?.let {
|
||||||
fs.copyEntriesFromFileAligned(ZipFile(it), ZipAligner::getEntryAlignment)
|
fs.copyEntriesFromFileAligned(ZipFile(it), ZipAligner::getEntryAlignment)
|
||||||
}
|
}
|
||||||
fs.copyEntriesFromFileAligned(ZipFile(inputFile), ZipAligner::getEntryAlignment)
|
fs.copyEntriesFromFileAligned(ZipFile(inputFile), ZipAligner::getEntryAlignment)
|
||||||
}
|
}
|
||||||
|
|
||||||
Logging.log += "Signing apk\n"
|
Logging.log += "Signing apk\n"
|
||||||
Signer("ReVanced", "s3cur3p@ssw0rd").signApk(patchedFile, outputFile)
|
Signer("ReVanced", "s3cur3p@ssw0rd").signApk(patchedFile, outputFile)
|
||||||
Logging.log += "Successfully patched!\n"
|
Logging.log += "Successfully patched!\n"
|
||||||
} finally {
|
} finally {
|
||||||
Log.d(tag, "Deleting workdir")
|
Log.d(tag, "Deleting workdir")
|
||||||
workdir.deleteRecursively()
|
workdir.deleteRecursively()
|
||||||
@ -190,6 +197,7 @@ class PatcherWorker(context: Context, parameters: WorkerParameters, private val
|
|||||||
val (patches) = patches.value as? Resource.Success ?: return listOf()
|
val (patches) = patches.value as? Resource.Success ?: return listOf()
|
||||||
return patches.filter { patch -> ids.any { it == patch.patchName } }
|
return patches.filter { patch -> ids.any { it == patch.patchName } }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createWorkDir(): File {
|
private fun createWorkDir(): File {
|
||||||
return applicationContext.filesDir.resolve("tmp-${System.currentTimeMillis()}")
|
return applicationContext.filesDir.resolve("tmp-${System.currentTimeMillis()}")
|
||||||
.also { it.mkdirs() }
|
.also { it.mkdirs() }
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package app.revanced.manager.ui.screen
|
package app.revanced.manager.ui.screen
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import androidx.compose.animation.core.LinearEasing
|
import androidx.compose.animation.core.LinearEasing
|
||||||
import androidx.compose.animation.core.tween
|
import androidx.compose.animation.core.tween
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
@ -24,6 +25,7 @@ import kotlinx.coroutines.launch
|
|||||||
import org.koin.androidx.compose.getViewModel
|
import org.koin.androidx.compose.getViewModel
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressLint("UnrememberedMutableState")
|
||||||
@Composable
|
@Composable
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
fun PatchingScreen(
|
fun PatchingScreen(
|
||||||
|
@ -40,6 +40,8 @@ class PatchingScreenViewModel(val app: Application) : ViewModel() {
|
|||||||
|
|
||||||
fun startPatcher() {
|
fun startPatcher() {
|
||||||
cancelPatching() // cancel patching if its still running
|
cancelPatching() // cancel patching if its still running
|
||||||
|
Logging.log = "" // clear logs
|
||||||
|
|
||||||
WorkManager.getInstance(app)
|
WorkManager.getInstance(app)
|
||||||
.enqueueUniqueWork("patching", ExistingWorkPolicy.KEEP, patcherWorker) // enqueue patching process
|
.enqueueUniqueWork("patching", ExistingWorkPolicy.KEEP, patcherWorker) // enqueue patching process
|
||||||
liveData.observeForever(observer) // start observing patch status
|
liveData.observeForever(observer) // start observing patch status
|
||||||
|
Loading…
x
Reference in New Issue
Block a user