Skip to content

Commit

Permalink
feat: abort patching process at any time (#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAabedKhan authored Aug 3, 2023
1 parent fe75b75 commit 374eb3d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ private const val INSTALLER_CHANNEL = "app.revanced.manager.flutter/installer"
class MainActivity : FlutterActivity() {
private val handler = Handler(Looper.getMainLooper())
private lateinit var installerChannel: MethodChannel
private var cancel: Boolean = false
private var stopResult: MethodChannel.Result? = null

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
Expand Down Expand Up @@ -57,6 +59,7 @@ class MainActivity : FlutterActivity() {
keyStoreFilePath != null &&
keystorePassword != null
) {
cancel = false
runPatcher(
result,
patchBundleFilePath,
Expand All @@ -74,6 +77,10 @@ class MainActivity : FlutterActivity() {
result.notImplemented()
}
}
"stopPatcher" -> {
cancel = true
stopResult = result
}
else -> result.notImplemented()
}
}
Expand Down Expand Up @@ -111,6 +118,12 @@ class MainActivity : FlutterActivity() {
)
)
}

if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}

originalFile.copyTo(inputFile, true)

handler.post {
Expand All @@ -123,6 +136,12 @@ class MainActivity : FlutterActivity() {
)
)
}

if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}

val patcher =
Patcher(
PatcherOptions(
Expand All @@ -134,6 +153,11 @@ class MainActivity : FlutterActivity() {
)
)

if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}

handler.post {
installerChannel.invokeMethod(
"update",
Expand All @@ -150,8 +174,19 @@ class MainActivity : FlutterActivity() {
)
)
}

if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}

patcher.addIntegrations(listOf(integrations)) {}

if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}

handler.post {
installerChannel.invokeMethod(
"update",
Expand All @@ -163,6 +198,11 @@ class MainActivity : FlutterActivity() {
)
}

if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}

val patches = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
PatchBundle.Dex(
patchBundleFilePath,
Expand All @@ -179,6 +219,12 @@ class MainActivity : FlutterActivity() {
} else {
TODO("VERSION.SDK_INT < CUPCAKE")
}

if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}

patcher.addPatches(patches)
patcher.executePatches().forEach { (patch, res) ->
if (res.isSuccess) {
Expand All @@ -193,15 +239,24 @@ class MainActivity : FlutterActivity() {
)
)
}
if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}
return@forEach
}
val msg = "Failed to apply $patch: " + "${res.exceptionOrNull()!!.message ?: res.exceptionOrNull()!!.cause!!::class.simpleName}"
val msg =
"Failed to apply $patch: " + "${res.exceptionOrNull()!!.message ?: res.exceptionOrNull()!!.cause!!::class.simpleName}"
handler.post {
installerChannel.invokeMethod(
"update",
mapOf("progress" to 0.5, "header" to "", "log" to msg)
)
}
if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}
}

handler.post {
Expand All @@ -214,9 +269,17 @@ class MainActivity : FlutterActivity() {
)
)
}
if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}
val res = patcher.save()
ZipFile(patchedFile).use { file ->
res.dexFiles.forEach {
if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}
file.addEntryCompressData(
ZipEntry.createWithName(it.name),
it.stream.readBytes()
Expand All @@ -233,6 +296,10 @@ class MainActivity : FlutterActivity() {
ZipAligner::getEntryAlignment
)
}
if(cancel) {
handler.post { stopResult!!.success(null) }
return@Thread
}
handler.post {
installerChannel.invokeMethod(
"update",
Expand All @@ -244,10 +311,12 @@ class MainActivity : FlutterActivity() {
)
}

// Signer("ReVanced", "s3cur3p@ssw0rd").signApk(patchedFile, outFile, keyStoreFile)

try {
Signer("ReVanced", keystorePassword).signApk(patchedFile, outFile, keyStoreFile)
Signer("ReVanced", keystorePassword).signApk(
patchedFile,
outFile,
keyStoreFile
)
} catch (e: Exception) {
//log to console
print("Error signing apk: ${e.message}")
Expand Down
1 change: 1 addition & 0 deletions assets/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"widgetTitle": "Installer",
"installButton": "Install",
"installRootButton": "Install as Root",
"pressBackAgain": "Press back again to cancel",
"openButton": "Open",
"shareButton": "Share file",

Expand Down
10 changes: 10 additions & 0 deletions lib/services/patcher_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ class PatcherAPI {
}
}

Future<void> stopPatcher() async {
try {
await patcherChannel.invokeMethod('stopPatcher');
} on Exception catch (e) {
if (kDebugMode) {
print(e);
}
}
}

Future<bool> installPatchedFile(PatchedApplication patchedApp) async {
if (_outFile != null) {
try {
Expand Down
30 changes: 28 additions & 2 deletions lib/ui/views/installer/installer_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class InstallerViewModel extends BaseViewModel {
bool isPatching = true;
bool isInstalled = false;
bool hasErrors = false;
bool isCanceled = false;
bool cancel = false;

Future<void> initialize(BuildContext context) async {
isRooted = await _rootAPI.isRooted();
Expand Down Expand Up @@ -162,6 +164,19 @@ class InstallerViewModel extends BaseViewModel {
}
}

Future<void> stopPatcher() async {
try {
isCanceled = true;
update(0.5, 'Aborting...', 'Canceling patching process');
await _patcherAPI.stopPatcher();
update(-100.0, 'Aborted...', 'Press back to exit');
} on Exception catch (e) {
if (kDebugMode) {
print(e);
}
}
}

Future<void> installResult(BuildContext context, bool installAsRoot) async {
try {
_app.isRooted = installAsRoot;
Expand Down Expand Up @@ -280,10 +295,21 @@ class InstallerViewModel extends BaseViewModel {

Future<bool> onWillPop(BuildContext context) async {
if (isPatching) {
_toast.showBottom('installerView.noExit');
if (!cancel) {
cancel = true;
_toast.showBottom('installerView.pressBackAgain');
} else if (!isCanceled) {
await stopPatcher();
} else {
_toast.showBottom('installerView.noExit');
}
return false;
}
cleanPatcher();
if (!cancel) {
cleanPatcher();
} else {
_patcherAPI.cleanPatcher();
}
Navigator.of(context).pop();
return true;
}
Expand Down

0 comments on commit 374eb3d

Please sign in to comment.