Skip to content

Commit

Permalink
refactor: use cache folder instead of files folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Ferretti committed Nov 15, 2021
1 parent cebf5dd commit a9a0509
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 523 deletions.
114 changes: 15 additions & 99 deletions app/src/main/java/me/hexile/odexpatcher/art/Dex2Oat.kt
Original file line number Diff line number Diff line change
@@ -1,104 +1,20 @@
package me.hexile.odexpatcher.art

object Dex2Oat {
/*fun run(context: Context) {
when {
Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> {
android44_51(context)
}
Build.VERSION.SDK_INT < Build.VERSION_CODES.Q -> {
android6_9(context)
}
else -> {
android10(context)
}
}
}*/

/*private fun android44_51(context: Context) {
// Android 4.4 - 5.1 workflow
// - copy from /data/app/packagename.apk to /data/data/me.hexile.odexpatcher/files/backup.apk
// - copy from /data/data/me.hexile.odexpatcher/files/base.apk to /data/app/packagename.apk
// - dex2oat /data/app/packagename.apk
// - copy from /data/data/me.hexile.odexpatcher/files/backup.apk to /data/app/packagename.apk
val backupApk = context.getFileInFilesDir("backup.apk").absolutePath
//viewModel.addLog("[I] Backing up target apk…")
var shellResult = Shell.su("cp $targetApk $backupApk").exec()
if (!shellResult.isSuccess) {
//viewModel.addLog("[E] ERROR: cp exit code was ${shellResult.code}.")
//viewModel.state.postValue(true)
eventChannel.send(MainViewModel.Event.SnackBarString("ERROR: cp exit code was ${shellResult.code}."))
return@launch
}
//viewModel.addLog(" Done!", false)
//viewModel.addLog("[I] Copying over input file…")
shellResult = Shell.su("cp ${baseApk.absolutePath} $targetApk").exec()
if (!shellResult.isSuccess) {
//viewModel.addLog("[E] ERROR: cp exit code was ${shellResult.code}.")
//viewModel.state.postValue(true)
eventChannel.send(MainViewModel.Event.SnackBarString("ERROR: cp exit code was ${shellResult.code}."))
return@launch
}
//viewModel.addLog(" Done!", false)
viewModel.addLog("[I] Running dex2oat…")
shellResult = Shell.sh(
"dex2oat --dex-file=$targetApk --oat-file=${
App.context.getFileInFilesDir(Const.BASE_ODEX_FILE_NAME).absolutePath
}"
).exec()
if (!shellResult.isSuccess) {
viewModel.addLog("[E] ERROR: dex2oat exit code was ${shellResult.code}.")
viewModel.state.postValue(true)
return@launch
}
viewModel.addLog(" Done!", false)
import com.topjohnwu.superuser.Shell
import me.hexile.odexpatcher.core.App
import me.hexile.odexpatcher.core.Const
import me.hexile.odexpatcher.ktx.getFileInCacheDir
import me.hexile.odexpatcher.utils.logd

viewModel.addLog("[I] Restoring backup…")
shellResult = Shell.su("cp $backupApk $targetApk").exec()
if (!shellResult.isSuccess) {
viewModel.addLog("[E] ERROR: cp exit code was ${shellResult.code}.")
viewModel.state.postValue(true)
return@launch
}
viewModel.addLog(" Done!", false)
}
private fun android6_9(context: Context) {
// Android 6.0 - 9.0 workflow
// - cd /data/data/me.hexile.odexpatcher/files/
// - dex2oat base.apk
viewModel.addLog("[I] Running dex2oat…")
val shellResult = Shell.sh(
"cd ${App.context.getFileInFilesDir("").absolutePath} && dex2oat --dex-file=${Const.BASE_APK_FILE_NAME} --oat-file=${
App.context.getFileInFilesDir(Const.BASE_ODEX_FILE_NAME).absolutePath
}"
).exec()
if (!shellResult.isSuccess) {
viewModel.addLog("[E] ERROR: dex2oat exit code was ${shellResult.code}.")
viewModel.state.postValue(true)
return@launch
}
viewModel.addLog(" Done!", false)
object Dex2Oat {
fun run(
dexFile: String,
dexLocation: String,
oatFile: String = App.getContext().getFileInCacheDir(Const.BASE_ODEX_FILE_NAME).absolutePath
): Boolean {
// Run dex2oat
val dex2OatCommand = "dex2oat --dex-file=$dexFile --dex-location=$dexLocation --oat-file=$oatFile --instruction-set=${Art.ISA} --instruction-set-variant=${Art.ISA_VARIANT} --instruction-set-features=${Art.ISA_FEATURES}"
logd("dex2oat", dex2OatCommand)
return Shell.sh(dex2OatCommand).exec().isSuccess
}
private fun android10(context: Context) {
// Android 10+ workflow
// - cd /data/data/me.hexile.odexpatcher/files/
// - su dex2oat base.apk
viewModel.addLog("[I] Running dex2oat…")
val shellResult = Shell.su(
"cd ${App.context.getFileInFilesDir("").absolutePath} && dex2oat64 --dex-file=${Const.BASE_APK_FILE_NAME} --oat-file=${
App.context.getFileInFilesDir(Const.BASE_ODEX_FILE_NAME).absolutePath
}"
).exec()
if (!shellResult.isSuccess) {
viewModel.addLog("[E] ERROR: dex2oat exit code was ${shellResult.code}.")
viewModel.state.postValue(true)
return@launch
}
viewModel.addLog(" Done!", false)
}*/
}
37 changes: 35 additions & 2 deletions app/src/main/java/me/hexile/odexpatcher/ktx/FilesExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package me.hexile.odexpatcher.ktx

import android.content.Context
import com.topjohnwu.superuser.Shell
import java.io.File
import java.io.InputStream
import java.io.RandomAccessFile
Expand All @@ -31,14 +32,18 @@ fun File.copyInputStreamToFile(inputStream: InputStream) {
}
}

fun Context.getPackageBaseApk(packageName: String): String {
return this.packageManager.getPackageInfo(packageName, 0).applicationInfo.sourceDir
fun Context.getPackageApk(packageName: String): File {
return File(this.packageManager.getPackageInfo(packageName, 0).applicationInfo.sourceDir)
}

fun Context.getFileInFilesDir(filename: String): File {
return File(this.filesDir.absolutePath, filename)
}

fun Context.getFileInCacheDir(filename: String): File {
return File(this.cacheDir.absolutePath, filename)
}

fun RandomAccessFile.readIntLittleEndian(): Int {
val data = ByteArray(4)
this.read(data)
Expand Down Expand Up @@ -68,4 +73,32 @@ fun RandomAccessFile.readBytes(offset: Long, amount: Int): ByteArray {

fun RandomAccessFile.readBytes(offset: Int, amount: Int): ByteArray {
return this.readBytes(offset.toLong(), amount)
}

fun chown(path: String, uid: Int, gid: Int, recursive: Boolean = false): Boolean {
return Shell.sh("chown ${if (recursive) "-R" else ""} $uid:$gid $path").exec().isSuccess
}

fun chmod(path: String, chmod: String, recursive: Boolean = false): Boolean {
return Shell.sh("chmod ${if (recursive) "-R" else ""} $chmod $path").exec().isSuccess
}

fun restorecon(path: String, recursive: Boolean = false): Boolean {
return Shell.sh("restorecon ${if (recursive) "-R" else ""} $path").exec().isSuccess
}

fun fixCacheFolderPermission(fileInCacheDir: File, recursive: Boolean = false): Boolean {
if (!chown(fileInCacheDir.absolutePath, selfAppUid(), cacheAppGid(selfAppUid()), recursive)) {
return false
}

if (!chmod(fileInCacheDir.absolutePath, "600", recursive)) {
return false
}

if (!restorecon(fileInCacheDir.absolutePath, recursive)) {
return false
}

return true
}
Loading

0 comments on commit a9a0509

Please sign in to comment.