diff options
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/Application.kt')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/Application.kt | 93 |
1 files changed, 43 insertions, 50 deletions
diff --git a/ui/src/main/java/com/wireguard/android/Application.kt b/ui/src/main/java/com/wireguard/android/Application.kt index 5563fe62..8b3be8de 100644 --- a/ui/src/main/java/com/wireguard/android/Application.kt +++ b/ui/src/main/java/com/wireguard/android/Application.kt @@ -8,13 +8,10 @@ import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.content.SharedPreferences.OnSharedPreferenceChangeListener -import android.os.AsyncTask import android.os.Build -import android.os.Handler -import android.os.Looper import android.os.StrictMode -import android.os.StrictMode.VmPolicy import android.os.StrictMode.ThreadPolicy +import android.os.StrictMode.VmPolicy import android.util.Log import androidx.appcompat.app.AppCompatDelegate import androidx.preference.PreferenceManager @@ -23,18 +20,18 @@ import com.wireguard.android.backend.GoBackend import com.wireguard.android.backend.WgQuickBackend import com.wireguard.android.configStore.FileConfigStore import com.wireguard.android.model.TunnelManager -import com.wireguard.android.util.AsyncWorker -import com.wireguard.android.util.ExceptionLoggers import com.wireguard.android.util.ModuleLoader import com.wireguard.android.util.RootShell import com.wireguard.android.util.ToolsInstaller -import java9.util.concurrent.CompletableFuture +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import java.lang.ref.WeakReference import java.util.Locale class Application : android.app.Application(), OnSharedPreferenceChangeListener { - private val futureBackend = CompletableFuture<Backend>() - private lateinit var asyncWorker: AsyncWorker + private val futureBackend = CompletableDeferred<Backend>() private var backend: Backend? = null private lateinit var moduleLoader: ModuleLoader private lateinit var rootShell: RootShell @@ -58,10 +55,37 @@ class Application : android.app.Application(), OnSharedPreferenceChangeListener } } + private fun determineBackend(): Backend { + var backend: Backend? = null + var didStartRootShell = false + if (!ModuleLoader.isModuleLoaded() && moduleLoader.moduleMightExist()) { + try { + rootShell.start() + didStartRootShell = true + moduleLoader.loadModule() + } catch (ignored: Exception) { + } + } + if (!sharedPreferences.getBoolean("disable_kernel_module", false) && ModuleLoader.isModuleLoaded()) { + try { + if (!didStartRootShell) + rootShell.start() + val wgQuickBackend = WgQuickBackend(applicationContext, rootShell, toolsInstaller) + wgQuickBackend.setMultipleTunnels(sharedPreferences.getBoolean("multiple_tunnels", false)) + backend = wgQuickBackend + } catch (ignored: Exception) { + } + } + if (backend == null) { + backend = GoBackend(applicationContext) + GoBackend.setAlwaysOnCallback { get().tunnelManager.restoreState(true) } + } + return backend + } + override fun onCreate() { Log.i(TAG, USER_AGENT) super.onCreate() - asyncWorker = AsyncWorker(AsyncTask.SERIAL_EXECUTOR, Handler(Looper.getMainLooper())) rootShell = RootShell(applicationContext) toolsInstaller = ToolsInstaller(applicationContext, rootShell) moduleLoader = ModuleLoader(applicationContext, rootShell, USER_AGENT) @@ -74,7 +98,14 @@ class Application : android.app.Application(), OnSharedPreferenceChangeListener } tunnelManager = TunnelManager(FileConfigStore(applicationContext)) tunnelManager.onCreate() - asyncWorker.supplyAsync(Companion::getBackend).thenAccept { futureBackend.complete(it) } + GlobalScope.launch(Dispatchers.IO) { + try { + backend = determineBackend() + futureBackend.complete(backend!!) + } catch (e: Throwable) { + Log.println(Log.ERROR, TAG, Log.getStackTraceString(e)) + } + } sharedPreferences.registerOnSharedPreferenceChangeListener(this) } @@ -99,45 +130,7 @@ class Application : android.app.Application(), OnSharedPreferenceChangeListener } @JvmStatic - fun getAsyncWorker() = get().asyncWorker - - @JvmStatic - fun getBackend(): Backend { - val app = get() - synchronized(app.futureBackend) { - if (app.backend == null) { - var backend: Backend? = null - var didStartRootShell = false - if (!ModuleLoader.isModuleLoaded() && app.moduleLoader.moduleMightExist()) { - try { - app.rootShell.start() - didStartRootShell = true - app.moduleLoader.loadModule() - } catch (ignored: Exception) { - } - } - if (!app.sharedPreferences.getBoolean("disable_kernel_module", false) && ModuleLoader.isModuleLoaded()) { - try { - if (!didStartRootShell) - app.rootShell.start() - val wgQuickBackend = WgQuickBackend(app.applicationContext, app.rootShell, app.toolsInstaller) - wgQuickBackend.setMultipleTunnels(app.sharedPreferences.getBoolean("multiple_tunnels", false)) - backend = wgQuickBackend - } catch (ignored: Exception) { - } - } - if (backend == null) { - backend = GoBackend(app.applicationContext) - GoBackend.setAlwaysOnCallback { get().tunnelManager.restoreState(true).whenComplete(ExceptionLoggers.D) } - } - app.backend = backend - } - return app.backend!! - } - } - - @JvmStatic - fun getBackendAsync() = get().futureBackend + suspend fun getBackend() = get().futureBackend.await() @JvmStatic fun getModuleLoader() = get().moduleLoader |