Skip to content

Commit

Permalink
- add disabled state style for buttons
Browse files Browse the repository at this point in the history
- add stop server button
- add external storage permission (resolve #3)
  • Loading branch information
vanhoavn committed Jun 5, 2020
1 parent 97397fa commit 786e73e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 9 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/vn/vhn/vhscode/CodeServerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CodeServerService : Service() {

fun stopService(context: Context) {
val intent = Intent(context, CodeServerService::class.java)
intent.action = kActionStopService
context.stopService(intent)
}

Expand Down Expand Up @@ -279,6 +280,7 @@ class CodeServerService : Service() {
}

private fun updateNotification() {
if (instance == null) return
val resultIntent = Intent(this, MainActivity::class.java)
val stackBuilder: TaskStackBuilder = TaskStackBuilder.create(this)
stackBuilder.addNextIntentWithParentStack(resultIntent)
Expand Down
26 changes: 22 additions & 4 deletions app/src/main/java/vn/vhn/vhscode/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import android.content.SharedPreferences
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.text.method.ScrollingMovementMethod
import android.view.Window
import android.view.WindowManager
import android.widget.ProgressBar
import android.widget.Scroller
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import com.termux.app.TermuxInstaller
import kotlinx.android.synthetic.main.activity_main.*
Expand All @@ -22,6 +23,7 @@ import kotlinx.coroutines.channels.Channel
import java.io.File
import java.io.FileInputStream


class MainActivity : AppCompatActivity() {
companion object {
val kCurrentServerVersion = "202005271607"
Expand Down Expand Up @@ -87,6 +89,12 @@ class MainActivity : AppCompatActivity() {
}
}

fun stopServerService() {
if (CodeServerService.liveServerStarted.value == true) {
CodeServerService.stopService(this)
}
}

fun startEditor(url: String = "http://127.0.0.1:13337") {
val intent = Intent(this, VSCodeActivity::class.java)
intent.putExtra(VSCodeActivity.kConfigUseHardKeyboard, chkHardKeyboard.isChecked)
Expand Down Expand Up @@ -167,6 +175,19 @@ class MainActivity : AppCompatActivity() {
}
}
}
btnStopCode.setOnClickListener {
CoroutineScope(Dispatchers.Main).launch {
stopServerService()
}
}
btnStopCode.isEnabled = CodeServerService.liveServerStarted.value == true
CodeServerService.liveServerStarted.observeForever { value ->
btnStopCode.isEnabled = value == true
}
btnStartRemote.setOnClickListener {
startEditor(editTxtRemoteServer.text.toString())
}

chkHardKeyboard.setOnCheckedChangeListener { _, isChecked ->
sharedPreferences().edit().putBoolean(kPrefHardKeyboard, isChecked).apply()
}
Expand All @@ -187,9 +208,6 @@ class MainActivity : AppCompatActivity() {
sharedPreferences().edit().putString(kPrefRemoteServer, s.toString()).apply()
}
})
btnStartRemote.setOnClickListener {
startEditor(editTxtRemoteServer.text.toString())
}
}

fun updateUI() {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/drawable/button_bg.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<solid
android:color="#77999999" />
<stroke
android:width="1dp"
android:color="#999999" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
<item android:state_pressed="true" >
<shape>
<solid
Expand Down
24 changes: 19 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,25 @@
android:text="@string/start_code"
android:textColor="@color/button"
app:layout_constraintBottom_toTopOf="@+id/btnStartRemote"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnStopCode"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnInstallServer"
app:layout_constraintVertical_chainStyle="packed" />

<Button
android:id="@+id/btnStopCode"
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:background="@drawable/button_bg"
android:text="@string/stop_code"
android:textColor="@color/button"
app:layout_constraintBaseline_toBaselineOf="@+id/btnStartCode"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/btnStartCode"
app:layout_constraintVertical_chainStyle="packed" />

<EditText
android:id="@+id/editTxtRemoteServer"
android:layout_width="0dp"
Expand Down Expand Up @@ -121,16 +135,16 @@
<EditText
android:id="@+id/txtServerLog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:enabled="false"
android:layout_height="0dp"
android:enabled="true"
android:focusable="false"
android:gravity="start|top"
android:inputType="textMultiLine"
android:maxLines="5"
android:scrollbars="vertical"
android:textColor="@color/log"
android:textSize="10dp"
android:typeface="monospace"
android:scrollbarAlwaysDrawVerticalTrack="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<string name="update_server">Update server</string>
<string name="reinstall_server">Reinstall server</string>
<string name="start_code">Start Editor</string>
<string name="stop_code">Stop Server</string>
<string name="dont_show_this_again">Don\'t show this again</string>
<string name="credit"><a href="https://github.com/cdr/code-server">code-server</a> by <a href="https://coder.com/">coder</a>\n<a href="https://github.com/vhqtvn/VHSCode-Android">UI &amp; Android version</a> by <a href="www.linkedin.com/in/vhnvn">vhnvn</a></string>
<string name="extracting">Extracting...</string>
Expand Down

0 comments on commit 786e73e

Please sign in to comment.