Skip to content

Commit

Permalink
- live-patch vscode-server code to disable online check
Browse files Browse the repository at this point in the history
- fix start vscode button
  • Loading branch information
vanhoavn committed Jul 1, 2020
1 parent e93beb8 commit 3beff85
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 29 deletions.
71 changes: 58 additions & 13 deletions app/src/main/java/vn/vhn/vhscode/CodeServerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@ 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
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/"
Expand All @@ -43,8 +44,9 @@ class CodeServerService : Service() {
val channelId = "VSCodeServer"
val channelName = "VSCodeServer"
var instance: CodeServerService? = null
val liveServerStarted: MutableLiveData<Int> by lazy { MutableLiveData<Int>() }
val liveServerLog: MutableLiveData<String> by lazy { MutableLiveData<String>() }
val liveServerStarted: MutableLiveData<Int> = MutableLiveData<Int>().apply { postValue(0) }
val liveServerLog: MutableLiveData<String> =
MutableLiveData<String>().apply { postValue("") }
private var isServerStarting = false

fun startService(context: Context) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -315,6 +360,7 @@ class CodeServerService : Service() {
setTimeout(local_apply, 100);
return;
}
Object.defineProperty(window.navigator,'onLine',{value:true, writable: false});
$windowScript
};
local_apply();
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down
39 changes: 25 additions & 14 deletions app/src/main/java/vn/vhn/vhscode/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> { 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()
}
Expand All @@ -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)
}
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/vn/vhn/vhscode/VSCodeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -142,7 +145,7 @@ class VSCodeActivity : AppCompatActivity() {
if (webView.canGoBack()) {
webView.goBack()
} else {
super.onBackPressed()
finish()
}
}
}

0 comments on commit 3beff85

Please sign in to comment.