Skip to content

Commit

Permalink
Merge pull request #137 from maggch97/wjzhao/RemoteSupport
Browse files Browse the repository at this point in the history
Add remote code server support
  • Loading branch information
vhqtvn authored Jun 14, 2022
2 parents 48a4ca3 + 7873372 commit 2af8e57
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 35 deletions.
18 changes: 18 additions & 0 deletions app/src/main/java/vn/vhn/vhscode/preferences/EditorHostPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class EditorHostPrefs(context: Context) {
val kPrefEditorUseSSL = "editor:use-ssl"
val kPrefEditorUseVirtualMouse = "editor:use-vmouse"
val kPrefEditorListenALlInterfaces = "editor:all-interfaces"
val kPrefRemoteCodeEditorURL = "editor:remote-url"

val kFlagGuideDrawerShown = "flag:guide-drawer-shown"
val kFlagGuideEditorSettingsShown = "flag:guide-editor-settings-shown"
Expand Down Expand Up @@ -130,6 +131,23 @@ class EditorHostPrefs(context: Context) {
)
}

var defaultRemoteEditorURL: String
get() =
SharedPreferenceUtils.getString(
mSharedPreferences,
TermuxPreferenceConstants.TERMUX_APP.KEY_SOFT_KEYBOARD_ENABLED,
"https://127.0.0.1:13337",
false
)
set(value) {
SharedPreferenceUtils.setString(
mSharedPreferences,
TermuxPreferenceConstants.TERMUX_APP.KEY_SOFT_KEYBOARD_ENABLED,
value,
false
)
}

var softKeyboardEnabled: Boolean
get() =
SharedPreferenceUtils.getBoolean(
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/vn/vhn/vhscode/root/EditorHostActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ class EditorHostActivity : FragmentActivity(), ServiceConnection,
intent.getBooleanExtra(NewSessionActivity.kSessionSSL, true),
)
}
NewSessionActivity.kSessionTypeRemoteCodeEditor -> {
codeServerService?.sessionsHost?.createCodeEditorSession(
GlobalSessionsManager.getNextSessionId(GlobalSessionsManager.SessionType.REMOTE_CODESERVER_EDITOR),
"RemoteEditor",
false,
false,
true,
intent?.getStringExtra(NewSessionActivity.kRemoteCodeEditorURL)
)
}
}
}
}
Expand Down
25 changes: 22 additions & 3 deletions app/src/main/java/vn/vhn/vhscode/root/NewSessionActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package vn.vhn.vhscode.root

import android.Manifest
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.os.Bundle
import android.text.Html
Expand Down Expand Up @@ -33,6 +35,7 @@ import java.io.FileInputStream
import java.io.InputStreamReader
import java.net.URL


class NewSessionActivity : AppCompatActivity() {
companion object {
val kCurrentServerVersion = "4.4.0"
Expand All @@ -45,13 +48,20 @@ class NewSessionActivity : AppCompatActivity() {
val kSessionType = "SESSION_TYPE"
val kSessionTypeTerminal = "SESSION_TYPE_TERMINAL"
val kSessionTypeCodeEditor = "SESSION_TYPE_CODEEDITOR"
val kSessionTypeRemoteCodeEditor = "SESSION_TYPE_REMOTE_CODEEDITOR"

val kRemoteCodeEditorURL = "REMOTE_CODEEDITOR_URL"
}

private lateinit var binding: ActivityNewSessionBinding
lateinit var preferences: EditorHostPrefs

private var latestRemoteVersion: String? = null

private fun sharedPreferences(): SharedPreferences {
return getSharedPreferences("main_settings", Context.MODE_PRIVATE)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
Expand All @@ -69,7 +79,7 @@ class NewSessionActivity : AppCompatActivity() {

private fun configureUI() {
binding.txtAppVersion.movementMethod = LinkMovementMethod.getInstance()

binding.editTxtRemoteServer.setText(preferences.defaultRemoteEditorURL)
checkLatestVersion()
}

Expand All @@ -82,7 +92,8 @@ class NewSessionActivity : AppCompatActivity() {
TermuxInstaller.setupIfNeeded(this) {
CodeServerService.setupIfNeeded(this) {
CoroutineScope(Dispatchers.Main).launch {
setResult(RESULT_OK,
setResult(
RESULT_OK,
Intent()
.putExtra(kSessionType, kSessionTypeCodeEditor)
.putExtra(kSessionSSL, preferences.editorUseSSL)
Expand All @@ -94,7 +105,15 @@ class NewSessionActivity : AppCompatActivity() {
}
}

fun onStartRemote(view: View) {}
fun onStartRemote(view: View) {
setResult(
RESULT_OK, Intent()
.putExtra(kSessionType, kSessionTypeRemoteCodeEditor)
.putExtra(kRemoteCodeEditorURL, binding.editTxtRemoteServer.text.toString())
)
preferences.defaultRemoteEditorURL = binding.editTxtRemoteServer.text.toString()
finish()
}

private fun checkLatestVersion() {
CoroutineScope(Dispatchers.IO).launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class CodeServerSession(
val ctx: Context,
val listenOnAllInterface: Boolean,
val useSSL: Boolean,
val remote: Boolean = false,
var remoteURL: String? = null,
port: Int? = null,
) {
companion object {
Expand Down Expand Up @@ -78,6 +80,10 @@ class CodeServerSession(
}

fun killIfExecuting(context: Context) {
if(remote){
onProcessFinished(RunStatus.FINISHED, true)
return
}
if (!hasExited()) {
process?.destroy()
for (i in 0..30) {
Expand All @@ -94,6 +100,10 @@ class CodeServerSession(
}

private fun _run() {
if(remote){
status.postValue(RunStatus.RUNNING)
return
}
status.postValue(RunStatus.STARTING)
val nodeBinary = ctx.getFileStreamPath("node")
val envHome = nodeBinary?.parentFile?.absolutePath
Expand Down Expand Up @@ -175,6 +185,10 @@ class CodeServerSession(
}

private fun hasExited(): Boolean {
if(remote){
return true
}

if (process == null) return true
return try {
process?.exitValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,13 @@ class VSCodeFragment : Fragment() {
val isSSL = mSession?.useSSL == true
val protocol = if (isSSL) "https" else "http"
val port = mSession?.port ?: throw Error("St wrong, no port obtained")
val url: String = protocol + "://127.0.0.1:" + port + "/?_=" + System.currentTimeMillis()
val url: String
if(mSession?.remote == true){
url = mSession?.remoteURL ?: throw Error("No remote url")
}else{
url = protocol + "://127.0.0.1:" + port + "/?_=" + System.currentTimeMillis()
}

if (isSSL) {
webView.clearCache(true)
webView.clearSslPreferences()
Expand Down Expand Up @@ -359,4 +365,4 @@ class VSCodeFragment : Fragment() {
fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
binding.loading.visibility = View.VISIBLE
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import vn.vhn.vhscode.service_features.SessionsHost
class GlobalSessionsManager() {
enum class SessionType {
TERMUX_TERMINAL,
CODESERVER_EDITOR;
CODESERVER_EDITOR,
REMOTE_CODESERVER_EDITOR;

companion object {
val Count = values().size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class SessionsHost(
sessionName: String?,
listenOnAllInterface: Boolean,
useSSL: Boolean,
remote: Boolean = false,
remoteURL: String? = null
): CodeServerSession? {
if (mCodeServerSessions.size >= MAX_CODESERVER_SESSIONS) {
mGlobalSessionsManager.notifyMaxCodeEditorSessionsReached()
Expand All @@ -69,7 +71,9 @@ class SessionsHost(
id,
mContext,
listenOnAllInterface,
useSSL
useSSL,
remote,
remoteURL
)
newEditorSession.start()
mCodeServerSessions.add(newEditorSession)
Expand Down
67 changes: 39 additions & 28 deletions app/src/main/res/layout/activity_new_session.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,36 +109,47 @@
app:layout_constraintTop_toBottomOf="@+id/btnNewTerminal"
app:layout_constraintVertical_chainStyle="packed" />

<!-- <EditText-->
<!-- android:id="@+id/editTxtRemoteServer"-->
<!-- android:layout_width="0dp"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:backgroundTint="@color/white"-->
<!-- android:ems="10"-->
<!-- android:inputType="textUri"-->
<!-- android:text="http://127.0.0.1:13337"-->
<!-- android:textColor="@color/white"-->
<!-- app:layout_constraintBaseline_toBaselineOf="@+id/btnStartRemote"-->
<!-- app:layout_constraintEnd_toStartOf="@+id/btnStartRemote"-->
<!-- app:layout_constraintStart_toStartOf="parent" />-->

<!-- <Button-->
<!-- android:id="@+id/btnStartRemote"-->
<!-- style="?android:attr/borderlessButtonStyle"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_marginTop="8dp"-->
<!-- android:background="@drawable/button_bg"-->
<!-- android:onClick="onStartRemote"-->
<!-- android:text="@string/start_remote"-->
<!-- android:textColor="@color/button"-->
<!-- app:layout_constraintBottom_toBottomOf="parent"-->
<!-- app:layout_constraintEnd_toEndOf="parent"-->
<!-- app:layout_constraintStart_toEndOf="@+id/editTxtRemoteServer"-->
<!-- app:layout_constraintTop_toBottomOf="@+id/btnStartCode"-->
<!-- app:layout_constraintVertical_chainStyle="packed" />-->
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/remoteRegionGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/installedRegionGroup"
app:layout_constraintVertical_chainStyle="packed"
>
<EditText
android:id="@+id/editTxtRemoteServer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:ems="10"
android:inputType="textUri"
android:text="http://127.0.0.1:13337"
android:textColor="@color/white"
app:layout_constraintBaseline_toBaselineOf="@+id/btnStartRemote"
app:layout_constraintEnd_toStartOf="@+id/btnStartRemote"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/btnStartRemote"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/button_bg"
android:onClick="onStartRemote"
android:text="@string/start_remote"
android:textColor="@color/button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTxtRemoteServer"
app:layout_constraintVertical_chainStyle="packed" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/btnSettings"
style="?android:attr/borderlessButtonStyle"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<certificates src="@raw/my_ca" />
</trust-anchors>
</domain-config>
<base-config cleartextTrafficPermitted="true"/>
</network-security-config>

0 comments on commit 2af8e57

Please sign in to comment.