diff --git a/manager/src/main/java/moe/shizuku/manager/starter/Starter.kt b/manager/src/main/java/moe/shizuku/manager/starter/Starter.kt index e803d37a3..5aa9caa51 100644 --- a/manager/src/main/java/moe/shizuku/manager/starter/Starter.kt +++ b/manager/src/main/java/moe/shizuku/manager/starter/Starter.kt @@ -3,10 +3,11 @@ package moe.shizuku.manager.starter import android.content.Context import android.os.Build import android.os.UserManager +import android.system.ErrnoException +import android.system.Os import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch -import moe.shizuku.manager.BuildConfig import moe.shizuku.manager.R import moe.shizuku.manager.ktx.createDeviceProtectedStorageContextCompat import moe.shizuku.manager.ktx.logd @@ -51,18 +52,40 @@ object Starter { } } - fun writeDataFiles(context: Context) { - if (commandInternal[0] != null) { + fun writeDataFiles(context: Context, permission: Boolean = false) { + if (commandInternal[0] != null && !permission) { logd("already written") return } + val dir = context.createDeviceProtectedStorageContextCompat().filesDir?.parentFile ?: return + + if (permission) { + try { + Os.chmod(dir.absolutePath, 457 /* 0711 */) + } catch (e: ErrnoException) { + e.printStackTrace() + } + } + try { - val dir = context.createDeviceProtectedStorageContextCompat().filesDir?.parentFile ?: return val starter = copyStarter(context, File(dir, "starter")) val sh = writeScript(context, File(dir, "start.sh"), starter) commandInternal[0] = "sh $sh" logd(commandInternal[0]!!) + + if (permission) { + try { + Os.chmod(starter, 420 /* 0644 */) + } catch (e: ErrnoException) { + e.printStackTrace() + } + try { + Os.chmod(sh, 420 /* 0644 */) + } catch (e: ErrnoException) { + e.printStackTrace() + } + } } catch (e: IOException) { loge("write files", e) } diff --git a/manager/src/main/java/moe/shizuku/manager/starter/StarterActivity.kt b/manager/src/main/java/moe/shizuku/manager/starter/StarterActivity.kt index 19f542fd7..cae310f84 100644 --- a/manager/src/main/java/moe/shizuku/manager/starter/StarterActivity.kt +++ b/manager/src/main/java/moe/shizuku/manager/starter/StarterActivity.kt @@ -12,6 +12,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import moe.shizuku.manager.AppConstants.EXTRA +import moe.shizuku.manager.BuildConfig import moe.shizuku.manager.R import moe.shizuku.manager.ShizukuSettings import moe.shizuku.manager.adb.AdbClient @@ -205,6 +206,34 @@ private class ViewModel(context: Context, root: Boolean, host: String?, port: In sb.append('\n').append(it.toString()) postResult(it) } + + /* Adb on MIUI Android 11 has no permission to access Android/data. + Before MIUI Android 12, we can temporarily use /data/user_de. + After that, is better to implement "adb push" and push files directly to /data/local/tmp. + */ + if (sb.contains("/Android/data/${BuildConfig.APPLICATION_ID}/start.sh: Permission denied")) { + sb.append('\n') + .appendLine("adb have no permission to access Android/data, how could this possible ?!") + .appendLine("try /data/user_de instead...") + .appendLine() + postResult() + + Starter.writeDataFiles(application, true) + + AdbClient(host, port, key).runCatching { + connect() + shellCommand(Starter.dataCommand) { + sb.append(String(it)) + postResult() + } + close() + }.onFailure { + it.printStackTrace() + + sb.append('\n').append(it.toString()) + postResult(it) + } + } } } } \ No newline at end of file