Skip to content

Commit

Permalink
- apply vsboot to all frames
Browse files Browse the repository at this point in the history
- add some annotations to fix lints
  • Loading branch information
vanhoavn committed Jun 3, 2020
1 parent 4011f6b commit 6784ee3
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 54 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/termux/app/TermuxInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* <p/>
* (5.2) For every other zip entry, extract it into $STAGING_PREFIX and set execute permissions if necessary.
*/
@SuppressWarnings("deprecation")
public final class TermuxInstaller {
static String str_fr = "/data/data/com.termux";
static String str_to = "/data/data/vn.vhn.vsc";
Expand Down
42 changes: 35 additions & 7 deletions app/src/main/java/vn/vhn/vhscode/CodeServerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CodeServerService : Service() {
}
extractTarGz(
csSourceFile,
csSourceFile.parentFile,
csSourceFile.parentFile!!,
progressChannel
)
csSourceFile.delete()
Expand Down Expand Up @@ -166,9 +166,9 @@ class CodeServerService : Service() {
copyRawResource(ctx, R.raw.vsboot, configFile.absolutePath)
}
val stream = FileInputStream(configFile)
val data = stream.readBytes()
var windowScriptBytes = stream.readBytes()
stream.close()
return """
val windowScript = """
(function(){
let click = false;
let click2 = false;
Expand All @@ -187,7 +187,35 @@ class CodeServerService : Service() {
}
});
})();
""".trimIndent() + String(data)
""".trimIndent() + String(windowScriptBytes)
return """
(function(){
var single_window_apply = function(window){
if(window.__vscode_boot_included__) return;
window.__vscode_boot_included__ = true;
var document = window.document;
var local_apply = function(){
if(!document.body) {
setTimeout(local_apply, 100);
return;
}
$windowScript
};
local_apply();
}
single_window_apply(window);
var vscode_boot_frames_inner = function(window){
for(var i=0;i<window.frames.length;i++) {
single_window_apply(window.frames[i]);
vscode_boot_frames_inner(window.frames[i]);
}
}
window.vscode_boot_frames = function(){
vscode_boot_frames_inner(window);
}
window.vscode_boot_frames();
})()
""".trimIndent()
}
}

Expand Down Expand Up @@ -249,8 +277,8 @@ class CodeServerService : Service() {
stopIntent.action = kActionStopService
val pendingStopStackBuilder = TaskStackBuilder.create(this)
pendingStopStackBuilder.addNextIntent(stopIntent)
val pendingStopIntent =
pendingStopStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
// val pendingStopIntent =
// pendingStopStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)

val chan =
NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
Expand All @@ -259,7 +287,7 @@ class CodeServerService : Service() {
val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
service.createNotificationChannel(chan)

var status: String = ""
var status: String
if (isServerStarting) {
if (liveServerStarted.value == true) status = "running";
else status = "starting"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/vn/vhn/vhscode/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MainActivity : AppCompatActivity() {
super.onDestroy()
}

@Suppress("DEPRECATION")
suspend fun startServerService() {
File(CodeServerService.HOME_PATH).mkdirs()
if (CodeServerService.liveServerStarted.value != true) {
Expand Down Expand Up @@ -86,7 +87,7 @@ class MainActivity : AppCompatActivity() {
}
}

fun startEditor(url: String = "http://127.0.0.1:1337") {
fun startEditor(url: String = "http://127.0.0.1:13337") {
val intent = Intent(this, VSCodeActivity::class.java)
intent.putExtra(VSCodeActivity.kConfigUseHardKeyboard, chkHardKeyboard.isChecked)
intent.putExtra(VSCodeActivity.kConfigUrl, url)
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/vn/vhn/vhscode/VSCodeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.os.Bundle
import android.util.Log
import android.view.*
import android.webkit.JavascriptInterface
import android.webkit.WebSettings
import android.webkit.WebView
import kotlinx.android.synthetic.main.activity_vscode.*
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -55,6 +56,9 @@ class VSCodeActivity : AppCompatActivity() {
webView.settings.domStorageEnabled = true
webView.settings.allowContentAccess = true
webView.settings.allowFileAccess = true
webView.settings.cacheMode = WebSettings.LOAD_DEFAULT
webView.settings.setAppCachePath("/data/data/vn.vhn.vsc/cache")
webView.settings.setAppCacheEnabled(true)
webView.settings.allowFileAccessFromFileURLs = true
webView.webChromeClient = VSCodeWebChromeClient()
webView.webViewClient = VSCodeWebClient()
Expand All @@ -75,7 +79,7 @@ class VSCodeActivity : AppCompatActivity() {

override fun dispatchKeyEvent(ev: KeyEvent?): Boolean {
if (genericMotionEventDispatcher != null && ev != null) {
if (genericMotionEventDispatcher!!.dispatchKeyEvent(ev!!))
if (genericMotionEventDispatcher!!.dispatchKeyEvent(ev))
return true
}
if (webView.dispatchKeyEvent(ev)) return true
Expand All @@ -84,7 +88,7 @@ class VSCodeActivity : AppCompatActivity() {

override fun dispatchGenericMotionEvent(ev: MotionEvent?): Boolean {
if (genericMotionEventDispatcher != null && ev != null) {
if (genericMotionEventDispatcher!!.dispatchGenericMotionEvent(ev!!))
if (genericMotionEventDispatcher!!.dispatchGenericMotionEvent(ev))
return true
}
return super.dispatchGenericMotionEvent(ev)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package vn.vhn.vhscode.chromebrowser.webclient
import android.webkit.WebChromeClient

class VSCodeWebChromeClient: WebChromeClient() {

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
package vn.vhn.vhscode.chromebrowser.webclient

import android.graphics.Bitmap
import android.util.Log
import android.view.KeyEvent
import android.webkit.WebView
import android.webkit.WebViewClient
import vn.vhn.vhscode.CodeServerService

class VSCodeWebClient : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
val HOME = CodeServerService.homePath(view!!.context)
CodeServerService.getBootjs(view!!.context)
view!!.context
view!!.evaluateJavascript(CodeServerService.getBootjs(view!!.context)
, null
);
companion object {
val TAG = "VSCodeWebClient"
}

var resource_html = Regex("\\.html?(\\?|\$)")
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
view?.apply {
CodeServerService.getBootjs(context)
context
evaluateJavascript(
CodeServerService.getBootjs(context)
, null
)
}
}


override fun onLoadResource(view: WebView?, url: String?) {
super.onLoadResource(view, url)
if (url != null && url.contains(resource_html)) {
Log.d(TAG, "Loading url $url")
view!!.evaluateJavascript("setTimeout(window.vscode_boot_frames,500)", null)
}
}
}
68 changes: 33 additions & 35 deletions app/src/main/res/raw/vsboot.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
(function(){
if(!window.vscodeOrigKeyboardEventDescriptorCode) window.vscodeOrigKeyboardEventDescriptorCode = Object.getOwnPropertyDescriptor(KeyboardEvent.prototype, 'code');
var codeGetter = window.vscodeOrigKeyboardEventDescriptorCode.get;
// if(!window.vscodeOrigKeyboardEventDescriptorShift) window.vscodeOrigKeyboardEventDescriptorShift = Object.getOwnPropertyDescriptor(KeyboardEvent.prototype, 'shiftKey');
// var shiftGetter = window.vscodeOrigKeyboardEventDescriptorShift.get;
var customMapper = {
27: 'Escape'
};
var customKeyRemapper = {
'Backspace': 'Backspace',
'Control': 'ControlLeft',
'Alt': 'AltLeft',
};
var prepareCustomProps = function() {
if(this._vscode_modded) return;
this._vscode_modded = true;
var orig = codeGetter.apply(this);
if (this.key == 'Alt') {
this._vscodeCode = "";
} else if (this.key === 'Shift') {
this._vscodeCode = "ShiftLeft";
} else if (this.which in customMapper) {
this._vscodeCode = customMapper[this.which];
} else if (orig === "" && typeof this.key === "string" && this.key.length) {
if (this.key in customKeyRemapper) this._vscodeCode = customKeyRemapper[this.key];
else if(this.key>="a" && this.key<="z") this._vscodeCode = "Key" + this.key.toUpperCase();
} else this._vscodeCode = orig;
}
Object.defineProperty(KeyboardEvent.prototype, 'code', {
get(){
prepareCustomProps.apply(this);
return this._vscodeCode;
}
});
})()
if(!window.vscodeOrigKeyboardEventDescriptorCode) window.vscodeOrigKeyboardEventDescriptorCode = Object.getOwnPropertyDescriptor(window.KeyboardEvent.prototype, 'code');
var codeGetter = window.vscodeOrigKeyboardEventDescriptorCode.get;
// if(!window.vscodeOrigKeyboardEventDescriptorShift) window.vscodeOrigKeyboardEventDescriptorShift = Object.getOwnPropertyDescriptor(KeyboardEvent.prototype, 'shiftKey');
// var shiftGetter = window.vscodeOrigKeyboardEventDescriptorShift.get;
var customMapper = {
27: 'Escape'
};
var customKeyRemapper = {
'Backspace': 'Backspace',
'Control': 'ControlLeft',
'Alt': 'AltLeft',
};
var prepareCustomProps = function() {
if(this._vscode_modded) return;
this._vscode_modded = true;
var orig = codeGetter.apply(this);
if (this.key == 'Alt') {
this._vscodeCode = "";
} else if (this.key === 'Shift') {
this._vscodeCode = "ShiftLeft";
} else if (this.which in customMapper) {
this._vscodeCode = customMapper[this.which];
} else if (orig === "" && typeof this.key === "string" && this.key.length) {
if (this.key in customKeyRemapper) this._vscodeCode = customKeyRemapper[this.key];
else if(this.key>="a" && this.key<="z") this._vscodeCode = "Key" + this.key.toUpperCase();
} else this._vscodeCode = orig;
}
Object.defineProperty(window.KeyboardEvent.prototype, 'code', {
get(){
prepareCustomProps.apply(this);
return this._vscodeCode;
}
});

0 comments on commit 6784ee3

Please sign in to comment.