From 3beff854e0c921521157f623021ab5833c15adfd Mon Sep 17 00:00:00 2001 From: Hoa Nguyen Van Date: Thu, 2 Jul 2020 00:29:54 +0700 Subject: [PATCH] - live-patch vscode-server code to disable online check - fix start vscode button --- .../java/vn/vhn/vhscode/CodeServerService.kt | 71 +++++++++++++++---- .../main/java/vn/vhn/vhscode/MainActivity.kt | 39 ++++++---- .../java/vn/vhn/vhscode/VSCodeActivity.kt | 7 +- 3 files changed, 88 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/vn/vhn/vhscode/CodeServerService.kt b/app/src/main/java/vn/vhn/vhscode/CodeServerService.kt index 4bfe0006..88fd87f2 100644 --- a/app/src/main/java/vn/vhn/vhscode/CodeServerService.kt +++ b/app/src/main/java/vn/vhn/vhscode/CodeServerService.kt @@ -4,15 +4,18 @@ import android.app.* import android.content.Context import android.content.DialogInterface import android.content.Intent +import android.content.Intent.makeRestartActivityTask import android.graphics.Color import android.os.Build import android.os.IBinder import android.os.UserManager import android.system.Os import android.util.Log +import android.widget.Toast import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.content.ContextCompat +import androidx.core.content.IntentCompat import androidx.lifecycle.MutableLiveData import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -20,20 +23,18 @@ import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.apache.commons.compress.archivers.tar.TarArchiveInputStream -import java.io.ByteArrayInputStream -import java.io.DataInputStream -import java.io.File -import java.io.FileInputStream -import java.io.FileOutputStream -import java.nio.file.Files +import java.io.* +import java.nio.charset.Charset import java.util.zip.GZIPInputStream + class CodeServerService : Service() { companion object { val TAG = "CodeServerService" - val ROOT_PATH = "/data/data/vn.vhn.vsc/files" - val HOME_PATH = ROOT_PATH + "/home" + val BASE_PATH = "/data/data/vn.vhn.vsc" + val ROOT_PATH = "${BASE_PATH}/files" + val HOME_PATH = "${ROOT_PATH}/home" val PREFIX_PATH = ROOT_PATH val BOOTJS = ".vsboot.js" val ASSET_PREFIX = "/vscode_local_asset/" @@ -43,8 +44,9 @@ class CodeServerService : Service() { val channelId = "VSCodeServer" val channelName = "VSCodeServer" var instance: CodeServerService? = null - val liveServerStarted: MutableLiveData by lazy { MutableLiveData() } - val liveServerLog: MutableLiveData by lazy { MutableLiveData() } + val liveServerStarted: MutableLiveData = MutableLiveData().apply { postValue(0) } + val liveServerLog: MutableLiveData = + MutableLiveData().apply { postValue("") } private var isServerStarting = false fun startService(context: Context) { @@ -75,6 +77,29 @@ class CodeServerService : Service() { return canon.canonicalFile != canon.absoluteFile } + fun clearCacheFolder(dir: File?): Int { + var deletedFiles = 0 + if (dir != null && dir.isDirectory) { + try { + for (child in dir.listFiles()) { + if (child.isDirectory) { + deletedFiles += clearCacheFolder(child) + } + + if (child.delete()) { + deletedFiles++ + } + } + } catch (e: java.lang.Exception) { + Log.e( + TAG, + String.format("Failed to clean the cache, error %s", e.message) + ) + } + } + return deletedFiles + } + fun setupIfNeeded(context: Context, whenDone: () -> Unit) { // region home symlink @@ -113,6 +138,26 @@ class CodeServerService : Service() { } // endregion + "$ROOT_PATH/code-server/release-static/dist/serviceWorker.js".apply { + val cs = Charset.forName("utf-8") + val content = File(this).readText(cs) + val new_content = content.replace("navigator.onLine", "(true)") + if (content != new_content) { + File(this).writeText(new_content, cs) + Log.d(TAG, "Clear cache ${context.cacheDir}") + clearCacheFolder(context.cacheDir) + File("${BASE_PATH}/cache").apply { + if (exists()) clearCacheFolder(this) + } + File("${BASE_PATH}/app_webview").apply { + if (exists()) clearCacheFolder(this) + } + context.externalCacheDir?.apply { + if (exists()) clearCacheFolder(this) + } + } + } + // region global inject "$PREFIX_PATH/globalinject.js".apply { copyRawResource(context, R.raw.globalinject, this) @@ -315,6 +360,7 @@ class CodeServerService : Service() { setTimeout(local_apply, 100); return; } + Object.defineProperty(window.navigator,'onLine',{value:true, writable: false}); $windowScript }; local_apply(); @@ -480,7 +526,7 @@ class CodeServerService : Service() { } fun runServer(ctx: Context) { - if (isServerStarting || (liveServerStarted.value == 1)) return + if (isServerStarting || (liveServerStarted.value != 0)) return isServerStarting = true liveServerStarted.postValue(-1) val nodeBinary = applicationContext.getFileStreamPath("node") @@ -553,17 +599,16 @@ class CodeServerService : Service() { } catch (e: Exception) { } } - liveServerStarted.postValue(0) } catch (e: Exception) { error = e.toString() logData += "\nException: ${error}" liveServerLog.postValue(logData) - liveServerStarted.postValue(0) } finally { isServerStarting = false updateNotification() logData += "Finished.\n" liveServerLog.postValue(logData) + liveServerStarted.postValue(0) try { stderrThread?.interrupt() } catch (e: Exception) { diff --git a/app/src/main/java/vn/vhn/vhscode/MainActivity.kt b/app/src/main/java/vn/vhn/vhscode/MainActivity.kt index ff968a12..7422fb1f 100644 --- a/app/src/main/java/vn/vhn/vhscode/MainActivity.kt +++ b/app/src/main/java/vn/vhn/vhscode/MainActivity.kt @@ -70,35 +70,46 @@ class MainActivity : AppCompatActivity() { super.onDestroy() } + fun cleanupStartServerObserver() { + if (startServerObserver != null) { + CodeServerService.liveServerStarted.removeObserver( + startServerObserver!! + ) + startServerObserver = null + } + } + @Suppress("DEPRECATION") suspend fun startServerService() { File(CodeServerService.HOME_PATH).mkdirs() if (CodeServerService.liveServerStarted.value != 1) { - val progressDialog = ProgressDialog(this) - progressDialog.setTitle(R.string.starting_server) - progressDialog.setMessage(getString(R.string.please_wait_starting_server)) - progressDialog.setCancelable(false) - progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER) - progressDialog.show() if (startServerObserver == null) { + val progressDialog = ProgressDialog(this) + progressDialog.setTitle(R.string.starting_server) + progressDialog.setMessage(getString(R.string.please_wait_starting_server)) + progressDialog.setCancelable(false) + progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER) + progressDialog.show() + var skipOnce = false startServerObserver = Observer { value -> if (value == 1) { progressDialog.dismiss() - if (startServerObserver != null) { - CodeServerService.liveServerStarted.removeObserver( - startServerObserver!! - ) - } + cleanupStartServerObserver() startEditor() } else { + if (value == 0 && !skipOnce) { + skipOnce = true + return@Observer + } if (!CodeServerService.isServerStarting()) { progressDialog.dismiss() + cleanupStartServerObserver() } } } CodeServerService.liveServerStarted.observeForever(startServerObserver!!) + CodeServerService.startService(this) } - CodeServerService.startService(this) } else { startEditor() } @@ -110,10 +121,10 @@ class MainActivity : AppCompatActivity() { } } - fun startEditor(url: String = "http://127.0.0.1:13337") { + fun startEditor(url: String? = null) { val intent = Intent(this, VSCodeActivity::class.java) intent.putExtra(VSCodeActivity.kConfigUseHardKeyboard, chkHardKeyboard.isChecked) - intent.putExtra(VSCodeActivity.kConfigUrl, url) + if (url != null) intent.putExtra(VSCodeActivity.kConfigUrl, url) intent.putExtra(VSCodeActivity.kConfigScreenAlive, chkKeepScreenAlive.isChecked) startActivity(intent) } diff --git a/app/src/main/java/vn/vhn/vhscode/VSCodeActivity.kt b/app/src/main/java/vn/vhn/vhscode/VSCodeActivity.kt index b9c9dc0c..65c9d290 100644 --- a/app/src/main/java/vn/vhn/vhscode/VSCodeActivity.kt +++ b/app/src/main/java/vn/vhn/vhscode/VSCodeActivity.kt @@ -73,9 +73,9 @@ class VSCodeActivity : AppCompatActivity() { webView.settings.allowFileAccess = true webView.settings.allowFileAccessFromFileURLs = true webView.settings.allowUniversalAccessFromFileURLs = true - webView.settings.cacheMode = WebSettings.LOAD_DEFAULT webView.settings.setAppCachePath("/data/data/vn.vhn.vsc/cache") webView.settings.setAppCacheEnabled(true) + webView.settings.cacheMode = WebSettings.LOAD_DEFAULT webView.settings.allowFileAccessFromFileURLs = true webView.webChromeClient = VSCodeWebChromeClient() webView.settings.fixedFontFamily = "vscode-monospace" @@ -90,6 +90,9 @@ class VSCodeActivity : AppCompatActivity() { kConfigUrl, "http://127.0.0.1:13337/?_=" + System.currentTimeMillis() )!! + if(url.startsWith("http://127.0.0.1:13337")) { + webView.clearCache(true) + } webView.webViewClient = VSCodeWebClient(url) webView.addJavascriptInterface(jsInterface, "_vn_vhn_vscjs_") webView.loadUrl(url) @@ -142,7 +145,7 @@ class VSCodeActivity : AppCompatActivity() { if (webView.canGoBack()) { webView.goBack() } else { - super.onBackPressed() + finish() } } }