Skip to content

Commit

Permalink
Add option to auto-connect to a server on app start
Browse files Browse the repository at this point in the history
Re: #204
  • Loading branch information
gujjwal00 committed Mar 4, 2024
1 parent ce5e1ff commit 0545440
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 1 deletion.
23 changes: 23 additions & 0 deletions app/src/androidTest/java/com/gaurav/avnc/ui/home/BasicHomeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ package com.gaurav.avnc.ui.home
import android.app.Activity
import android.app.Instrumentation
import android.content.Intent
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers.*
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.gaurav.avnc.EmptyDatabaseRule
import com.gaurav.avnc.R
import com.gaurav.avnc.TestServer
import com.gaurav.avnc.checkWillBeDisplayed
import com.gaurav.avnc.doClick
import com.gaurav.avnc.model.ServerProfile
import com.gaurav.avnc.ui.about.AboutActivity
import com.gaurav.avnc.ui.prefs.PrefsActivity
import kotlinx.coroutines.runBlocking
import org.hamcrest.Matchers.containsString
import org.hamcrest.core.AllOf.allOf
import org.junit.After
Expand Down Expand Up @@ -90,4 +96,21 @@ class BasicHomeTest {
))
Intents.assertNoUnverifiedIntents()
}
}

class AutoConnectTest {
@Rule
@JvmField
val dbRule = EmptyDatabaseRule()

@Test
fun autoConnectOnStartup() {
val server = TestServer().apply { start() }
val profile = ServerProfile(host = server.host, port = server.port).apply { fConnectOnAppStart = true }
runBlocking { dbRule.db.serverProfileDao.insert(profile) }
ActivityScenario.launch(HomeActivity::class.java).use {
// Starting HomeActivity should automatically launch the connection
onView(withId(R.id.frame_view)).checkWillBeDisplayed()
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/gaurav/avnc/model/ServerProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ data class ServerProfile(
private const val FLAG_LEGACY_KEYSYM = 0x01L
private const val FLAG_BUTTON_UP_DELAY = 0x02L
private const val FLAG_ZOOM_LOCKED = 0x04L
const val FLAG_CONNECT_ON_APP_START = 0x08L
}

/**
Expand Down Expand Up @@ -221,4 +222,10 @@ data class ServerProfile(
*/
@IgnoredOnParcel
var fZoomLocked by Flag(FLAG_ZOOM_LOCKED)

/**
* Try to automatically connect to this server when app starts.
*/
@IgnoredOnParcel
var fConnectOnAppStart by Flag(FLAG_CONNECT_ON_APP_START)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ interface ServerProfileDao {
@Query("SELECT * FROM profiles WHERE name = :name")
suspend fun getByName(name: String): List<ServerProfile>

@Query("SELECT * FROM profiles WHERE flags & ${ServerProfile.FLAG_CONNECT_ON_APP_START} != 0")
suspend fun getConnectableOnAppStart(): List<ServerProfile>

@Query("SELECT * FROM profiles WHERE name LIKE :query OR host LIKE :query OR sshHost LIKE :query ORDER BY useCount DESC")
fun search(query: String): LiveData<List<ServerProfile>>

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/gaurav/avnc/ui/home/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class HomeActivity : AppCompatActivity() {

setupSplashTheme()
showWelcomeMsg()
maybeAutoConnect(savedInstanceState == null)
}

override fun onStart() {
Expand Down Expand Up @@ -195,6 +196,11 @@ class HomeActivity : AppCompatActivity() {
}
}

private fun maybeAutoConnect(isNewStart: Boolean) {
if (isNewStart)
viewModel.maybeConnectOnAppStart()
}

/************************************************************************************
* Shortcuts
************************************************************************************/
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/gaurav/avnc/viewmodel/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class HomeViewModel(app: Application) : BaseViewModel(app) {
*/
fun startConnection(profile: ServerProfile) = newConnectionEvent.fire(profile)

fun maybeConnectOnAppStart() = launch {
serverProfileDao.getConnectableOnAppStart().firstOrNull()?.let { startConnection(it) }
}

/**************************************************************************
* Server Discovery
*
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_auto_connect.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?colorOnSurface"
android:pathData="M4.225,18.3Q3.175,17 2.588,15.388 2,13.775 2,11.975q0,-3.925 2.588,-6.75 2.588,-2.825 6.412,-3.2v2q-3,0.35 -5,2.6 -2,2.25 -2,5.35 0,1.4 0.425,2.638 0.425,1.237 1.225,2.263zM12,21.975q-1.825,0 -3.45,-0.6 -1.625,-0.6 -2.925,-1.675l1.4,-1.425q1.025,0.8 2.287,1.25 1.263,0.45 2.688,0.45 1.425,0 2.688,-0.45 1.263,-0.45 2.287,-1.25l1.4,1.425q-1.3,1.075 -2.925,1.675 -1.625,0.6 -3.45,0.6zM19.775,18.3 L18.35,16.875q0.8,-1.025 1.225,-2.263 0.425,-1.237 0.425,-2.638 0,-3.1 -2,-5.35 -2,-2.25 -5,-2.6v-2q3.825,0.375 6.412,3.2 2.588,2.825 2.588,6.75 0,1.8 -0.587,3.412Q20.825,17 19.775,18.3ZM9.5,16.475v-9l7,4.5z" />
</vector>
12 changes: 11 additions & 1 deletion app/src/main/res/layout/fragment_profile_editor_advanced.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/use_repeater" />

<!--Auto Connect-->
<CheckBox
android:id="@+id/auto_connect"
style="@style/FormField.CheckBox"
android:checked="@={viewModel.profile.fConnectOnAppStart}"
android:text="@string/title_connect_on_app_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_only" />

<!--Key Compatibility mode-->
<CheckBox
android:id="@+id/key_compat_mode"
Expand All @@ -151,7 +161,7 @@
android:text="@string/title_key_compat_mode"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_only" />
app:layout_constraintTop_toBottomOf="@id/auto_connect" />

<ImageButton
android:id="@+id/key_compat_mode_help_btn"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/server_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@
android:src="@drawable/ic_visibility"
app:isVisible="@{profile.viewOnly}" />

<ImageView
style="@style/ServerCardHintIcon"
android:importantForAccessibility="no"
android:src="@drawable/ic_auto_connect"
app:isVisible="@{profile.fConnectOnAppStart}" />

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
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 @@ -29,6 +29,7 @@
<string name="title_export">Export</string>
<string name="title_import">Import</string>
<string name="title_view_only_mode">View-only</string>
<string name="title_connect_on_app_start">Auto-connect when app starts</string>
<string name="title_hide_remote_cursor">Hide remote pointer</string>
<string name="title_key_compat_mode">Send legacy key events</string>
<string name="title_button_up_delay">Send delayed click events</string>
Expand Down

0 comments on commit 0545440

Please sign in to comment.