From ff1fe5b64692e42a11f14ca18f0fca02d355bd10 Mon Sep 17 00:00:00 2001 From: Raunak Raj <71929976+bajrangCoder@users.noreply.github.com> Date: Sun, 6 Dec 2026 16:45:10 +0530 Subject: [PATCH 1/2] fix(android): don't crash when launched in restricted backup mode When Android starts the process for a backup or restore, it uses the base Application class, so RuntimeBaseApplication.onCreate never runs. Opening the app in that process crashed in SystemBarPlugin because AppPreferences and the bridge thread pools required the static instance. Initialize AppPreferences from the activity as well as the application, and make the thread pools lazy companion singletons. --- .../main/java/com/foxdebug/acode/ftp/Ftp.java | 40 +++++++++---------- .../java/com/foxdebug/acode/runtime/App.kt | 4 +- .../foxdebug/acode/runtime/BaseWebActivity.kt | 2 + .../java/com/foxdebug/acode/runtime/Bridge.kt | 4 +- .../acode/runtime/RuntimeBaseApplication.kt | 31 ++++---------- .../foxdebug/acode/settings/AppPreferences.kt | 11 +++-- 6 files changed, 39 insertions(+), 53 deletions(-) diff --git a/platforms/android/app/src/main/java/com/foxdebug/acode/ftp/Ftp.java b/platforms/android/app/src/main/java/com/foxdebug/acode/ftp/Ftp.java index 6ae42ce6b..5c7e1b667 100644 --- a/platforms/android/app/src/main/java/com/foxdebug/acode/ftp/Ftp.java +++ b/platforms/android/app/src/main/java/com/foxdebug/acode/ftp/Ftp.java @@ -18,9 +18,9 @@ import org.apache.commons.net.ftp.*; import org.apache.commons.net.ftp.parser.ParserInitializationException; -import com.foxdebug.acode.Acode; import com.foxdebug.acode.runtime.BridgeContext; import com.foxdebug.acode.runtime.Callback; +import com.foxdebug.acode.runtime.RuntimeBaseApplication; import com.foxdebug.acode.runtime.Service; import org.json.JSONArray; import org.json.JSONObject; @@ -75,8 +75,7 @@ public void connect( Callback callback, boolean isRetry ) { - Acode.getInstance() - .getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -195,8 +194,7 @@ private static String getParentPath(String path) { } public void listDirectory(JSONArray args, Callback callback) { - Acode.getInstance() - .getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -309,7 +307,7 @@ public void run() { public void exists(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -358,7 +356,7 @@ public void run() { } public void sendNoOp(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -380,7 +378,7 @@ public void run() { } public void deleteFile(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -419,7 +417,7 @@ public void run() { } public void deleteDirectory(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -461,7 +459,7 @@ public void run() { } public void rename(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -547,7 +545,7 @@ public void run() { } public void downloadFile(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -633,7 +631,7 @@ public void run() { } public void uploadFile(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -710,7 +708,7 @@ public void run() { } public void getKeepAlive(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -738,7 +736,7 @@ public void run() { } public void execCommand(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -776,7 +774,7 @@ public void run() { } public void isConnected(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -806,7 +804,7 @@ public void run() { } public void disconnect(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -829,7 +827,7 @@ public void run() { } public void createDirectory(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -866,7 +864,7 @@ public void run() { } public void changeDirectory(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -906,7 +904,7 @@ public void changeToParentDirectory( JSONArray args, Callback callback ) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -937,7 +935,7 @@ public void run() { } public void getWorkingDirectory(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { @@ -968,7 +966,7 @@ public void run() { } public void getStat(JSONArray args, Callback callback) { - Acode.getInstance().getThreadPool() + RuntimeBaseApplication.getThreadPool() .execute( new Runnable() { public void run() { diff --git a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/App.kt b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/App.kt index 91754302a..8333ce9fb 100644 --- a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/App.kt +++ b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/App.kt @@ -51,8 +51,8 @@ class Host(val activity: BaseWebActivity) { val context: Context get() = activity - val threadPool: ExecutorService? - get() = RuntimeBaseApplication.instance!!.threadPool + val threadPool: ExecutorService + get() = RuntimeBaseApplication.threadPool fun hasPermission(permission: String): Boolean { return (ActivityCompat.checkSelfPermission(activity, permission) == diff --git a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/BaseWebActivity.kt b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/BaseWebActivity.kt index 282ae6e5d..a6120c7c8 100644 --- a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/BaseWebActivity.kt +++ b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/BaseWebActivity.kt @@ -24,6 +24,7 @@ import com.foxdebug.acode.BuildConfig import com.foxdebug.acode.runtime.webview.AppWebView import com.foxdebug.acode.runtime.webview.AcodeChromeClient import com.foxdebug.acode.runtime.webview.AcodeWebViewClient +import com.foxdebug.acode.settings.AppPreferences import org.json.JSONException import java.lang.ref.WeakReference import java.util.concurrent.ConcurrentHashMap @@ -42,6 +43,7 @@ open class BaseWebActivity : AppCompatActivity() { public override fun onCreate(state: Bundle?) { installSplashScreen() super.onCreate(state) + AppPreferences.initialize(this) context = WeakReference(this) WindowCompat.setDecorFitsSystemWindows(window, false) host = Host(this) diff --git a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/Bridge.kt b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/Bridge.kt index 8aabd9b87..79b7b3e4c 100644 --- a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/Bridge.kt +++ b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/Bridge.kt @@ -3,7 +3,6 @@ package com.foxdebug.acode.runtime import android.net.Uri import android.util.Log import android.webkit.JavascriptInterface -import com.foxdebug.acode.runtime.RuntimeBaseApplication.Companion.instance import com.foxdebug.acode.runtime.webview.AppWebView import kotlin.collections.LinkedHashMap @@ -60,8 +59,7 @@ class Bridge(private val webView: AppWebView, private val webActivity: BaseWebAc if (generation != requestGeneration) return@post try { val service = getService(definition) - instance!! - .threadPoolSingle!! + RuntimeBaseApplication.threadPoolSingle .execute { if (generation != requestGeneration) return@execute try { diff --git a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/RuntimeBaseApplication.kt b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/RuntimeBaseApplication.kt index e0506e360..253c5925c 100644 --- a/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/RuntimeBaseApplication.kt +++ b/platforms/android/app/src/main/java/com/foxdebug/acode/runtime/RuntimeBaseApplication.kt @@ -1,38 +1,23 @@ package com.foxdebug.acode.runtime import android.app.Application -import kotlinx.coroutines.CoroutineScope +import com.foxdebug.acode.settings.AppPreferences import java.util.concurrent.ExecutorService import java.util.concurrent.Executors open class RuntimeBaseApplication : Application() { - private var executorService: ExecutorService? = null - private var executorServiceSingle: ExecutorService? = null - override fun onCreate() { super.onCreate() - instance = this - executorService = Executors.newCachedThreadPool() - executorServiceSingle = Executors.newSingleThreadExecutor() - } - - @Deprecated(message = "Directly using System threads or threadPools is not reccomended", replaceWith = ReplaceWith("Coroutines")) - val threadPool: ExecutorService? - get() = executorService - - @Deprecated(message = "Directly using System threads or threadPools is not reccomended", replaceWith = ReplaceWith("Coroutines")) - val threadPoolSingle: ExecutorService? - get() = executorServiceSingle - - override fun onTerminate() { - executorService!!.shutdownNow() - executorServiceSingle!!.shutdownNow() - super.onTerminate() + AppPreferences.initialize(this) } companion object { + @Deprecated(message = "Directly using System threads or threadPools is not reccomended", replaceWith = ReplaceWith("Coroutines")) + @JvmStatic + val threadPool: ExecutorService by lazy { Executors.newCachedThreadPool() } + + @Deprecated(message = "Directly using System threads or threadPools is not reccomended", replaceWith = ReplaceWith("Coroutines")) @JvmStatic - var instance: RuntimeBaseApplication? = null - private set + val threadPoolSingle: ExecutorService by lazy { Executors.newSingleThreadExecutor() } } } diff --git a/platforms/android/app/src/main/java/com/foxdebug/acode/settings/AppPreferences.kt b/platforms/android/app/src/main/java/com/foxdebug/acode/settings/AppPreferences.kt index 8e2e5aae3..c39fb83be 100644 --- a/platforms/android/app/src/main/java/com/foxdebug/acode/settings/AppPreferences.kt +++ b/platforms/android/app/src/main/java/com/foxdebug/acode/settings/AppPreferences.kt @@ -2,17 +2,20 @@ package com.foxdebug.acode.settings import android.content.Context import android.content.SharedPreferences -import com.foxdebug.acode.runtime.RuntimeBaseApplication import androidx.core.content.edit object AppPreferences { //do not change this name private const val PREFERENCES_NAME = "Settings" + private lateinit var appContext: Context + private val preferences: SharedPreferences by lazy { - val application = RuntimeBaseApplication.instance - ?: error("AppPreferences requires RuntimeBaseApplication to be initialized") - application.applicationContext.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE) + appContext.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE) + } + + fun initialize(context: Context) { + appContext = context.applicationContext } fun set(name: String, value: String?) { From 95d6474ffe67cf740a59d6bf41f8e28c5eccd070 Mon Sep 17 00:00:00 2001 From: Raunak Raj <71929976+bajrangCoder@users.noreply.github.com> Date: Sun, 6 Dec 2026 16:48:38 +0530 Subject: [PATCH 2/2] test(android): cover startup and native calls with the plain Application --- .../runtime/RestrictedBackupStartupTest.kt | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 platforms/android/app/src/test/java/com/foxdebug/acode/runtime/RestrictedBackupStartupTest.kt diff --git a/platforms/android/app/src/test/java/com/foxdebug/acode/runtime/RestrictedBackupStartupTest.kt b/platforms/android/app/src/test/java/com/foxdebug/acode/runtime/RestrictedBackupStartupTest.kt new file mode 100644 index 000000000..d84cacba0 --- /dev/null +++ b/platforms/android/app/src/test/java/com/foxdebug/acode/runtime/RestrictedBackupStartupTest.kt @@ -0,0 +1,39 @@ +package com.foxdebug.acode.runtime + +import android.app.Application +import android.os.Looper +import com.foxdebug.acode.MainActivity +import com.foxdebug.acode.runtime.webview.AppWebView +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import java.util.concurrent.TimeUnit + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [29], application = Application::class) +class RestrictedBackupStartupTest { + @Test + fun startsAndCompletesNativeCallsWithThePlainApplication() { + val previousHandler = Thread.getDefaultUncaughtExceptionHandler() + try { + Robolectric.buildActivity(MainActivity::class.java).setup().use { controller -> + val view = controller.get().contentView.getChildAt(0) as AppWebView + view.bridge!!.exec(ServiceName.APP.key, "clearCache", "[]", CALLBACK_ID) + shadowOf(Looper.getMainLooper()).idle() + RuntimeBaseApplication.threadPoolSingle.submit {}.get(5, TimeUnit.SECONDS) + shadowOf(Looper.getMainLooper()).idle() + assertTrue(shadowOf(view).lastEvaluatedJavascript.contains("\"id\":$CALLBACK_ID")) + } + } finally { + Thread.setDefaultUncaughtExceptionHandler(previousHandler) + } + } + + companion object { + private const val CALLBACK_ID = 4242L + } +}