Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions platforms/android/app/src/main/java/com/foxdebug/acode/ftp/Ftp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,8 +75,7 @@ public void connect(
Callback callback,
boolean isRetry
) {
Acode.getInstance()
.getThreadPool()
RuntimeBaseApplication.getThreadPool()
.execute(
new Runnable() {
public void run() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -906,7 +904,7 @@ public void changeToParentDirectory(
JSONArray args,
Callback callback
) {
Acode.getInstance().getThreadPool()
RuntimeBaseApplication.getThreadPool()
.execute(
new Runnable() {
public void run() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) ==
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,6 +43,7 @@ open class BaseWebActivity : AppCompatActivity() {
public override fun onCreate(state: Bundle?) {
installSplashScreen()
super.onCreate(state)
AppPreferences.initialize(this)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
context = WeakReference<Context?>(this)
WindowCompat.setDecorFitsSystemWindows(window, false)
host = Host(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading