tv: wire up tunnel start/stop
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
dc002d77fa
commit
382e10e103
@ -6,12 +6,20 @@
|
|||||||
package com.wireguard.android.activity
|
package com.wireguard.android.activity
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.wireguard.android.Application
|
import com.wireguard.android.Application
|
||||||
|
import com.wireguard.android.R
|
||||||
|
import com.wireguard.android.backend.GoBackend
|
||||||
|
import com.wireguard.android.backend.Tunnel
|
||||||
|
import com.wireguard.android.databinding.ObservableKeyedRecyclerViewAdapter
|
||||||
import com.wireguard.android.databinding.TvActivityBinding
|
import com.wireguard.android.databinding.TvActivityBinding
|
||||||
|
import com.wireguard.android.databinding.TvTunnelListItemBinding
|
||||||
|
import com.wireguard.android.model.ObservableTunnel
|
||||||
|
import com.wireguard.android.util.ErrorMessages
|
||||||
import com.wireguard.android.util.TunnelImporter
|
import com.wireguard.android.util.TunnelImporter
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@ -24,14 +32,56 @@ class TvMainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var pendingTunnel: ObservableTunnel? = null
|
||||||
|
private val permissionActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||||
|
val tunnel = pendingTunnel
|
||||||
|
if (tunnel != null)
|
||||||
|
setTunnelStateWithPermissionsResult(tunnel)
|
||||||
|
pendingTunnel = null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setTunnelStateWithPermissionsResult(tunnel: ObservableTunnel) {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
try {
|
||||||
|
tunnel.setStateAsync(Tunnel.State.TOGGLE)
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
val error = ErrorMessages[e]
|
||||||
|
val message = getString(R.string.error_up, error)
|
||||||
|
Toast.makeText(this@TvMainActivity, message, Toast.LENGTH_LONG).show()
|
||||||
|
Log.e(TAG, message, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
val binding = TvActivityBinding.inflate(layoutInflater)
|
val binding = TvActivityBinding.inflate(layoutInflater)
|
||||||
lifecycleScope.launch { binding.tunnels = Application.getTunnelManager().getTunnels() }
|
lifecycleScope.launch { binding.tunnels = Application.getTunnelManager().getTunnels() }
|
||||||
|
binding.rowConfigurationHandler = object : ObservableKeyedRecyclerViewAdapter.RowConfigurationHandler<TvTunnelListItemBinding, ObservableTunnel> {
|
||||||
|
override fun onConfigureRow(binding: TvTunnelListItemBinding, item: ObservableTunnel, position: Int) {
|
||||||
|
binding.root.setOnClickListener() {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
if (Application.getBackend() is GoBackend) {
|
||||||
|
val intent = GoBackend.VpnService.prepare(binding.root.context)
|
||||||
|
if (intent != null) {
|
||||||
|
pendingTunnel = item
|
||||||
|
permissionActivityResultLauncher.launch(intent)
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setTunnelStateWithPermissionsResult(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
binding.importButton.setOnClickListener {
|
binding.importButton.setOnClickListener {
|
||||||
tunnelFileImportResultLauncher.launch("*/*")
|
tunnelFileImportResultLauncher.launch("*/*")
|
||||||
}
|
}
|
||||||
binding.executePendingBindings()
|
binding.executePendingBindings()
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "WireGuard/TvMainActivity"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,10 @@
|
|||||||
<variable
|
<variable
|
||||||
name="tunnels"
|
name="tunnels"
|
||||||
type="com.wireguard.android.databinding.ObservableKeyedArrayList<String, ObservableTunnel>" />
|
type="com.wireguard.android.databinding.ObservableKeyedArrayList<String, ObservableTunnel>" />
|
||||||
|
|
||||||
|
<variable
|
||||||
|
name="rowConfigurationHandler"
|
||||||
|
type="com.wireguard.android.databinding.ObservableKeyedRecyclerViewAdapter.RowConfigurationHandler" />
|
||||||
</data>
|
</data>
|
||||||
|
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout
|
<androidx.constraintlayout.widget.ConstraintLayout
|
||||||
@ -22,6 +26,7 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_marginTop="16dp"
|
android:layout_marginTop="16dp"
|
||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
|
app:configurationHandler="@{rowConfigurationHandler}"
|
||||||
app:items="@{tunnels}"
|
app:items="@{tunnels}"
|
||||||
app:layout="@{@layout/tv_tunnel_list_item}"
|
app:layout="@{@layout/tv_tunnel_list_item}"
|
||||||
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
|
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
|
||||||
|
@ -23,7 +23,10 @@
|
|||||||
android:layout_height="150dp"
|
android:layout_height="150dp"
|
||||||
android:layout_marginStart="8dp"
|
android:layout_marginStart="8dp"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
app:cardCornerRadius="12dp">
|
app:cardCornerRadius="12dp"
|
||||||
|
android:checkable="true"
|
||||||
|
android:focusable="true"
|
||||||
|
android:backgroundTint="@{item.state == State.UP ? @color/secondary_light_color : @color/primary_color}">
|
||||||
|
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout
|
<androidx.constraintlayout.widget.ConstraintLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
@ -39,15 +42,6 @@
|
|||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
tools:text="@sample/interface_names.json/names/names/name" />
|
tools:text="@sample/interface_names.json/names/names/name" />
|
||||||
|
|
||||||
<com.wireguard.android.widget.ToggleSwitch
|
|
||||||
android:id="@+id/tunnel_toggle"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
app:checked="@{item.state == State.UP}"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
|
||||||
tools:checked="@sample/interface_names.json/names/checked/checked" />
|
|
||||||
|
|
||||||
<!-- TODO: wire in updates here -->
|
<!-- TODO: wire in updates here -->
|
||||||
<com.google.android.material.textview.MaterialTextView
|
<com.google.android.material.textview.MaterialTextView
|
||||||
android:id="@+id/tunnel_transfer"
|
android:id="@+id/tunnel_transfer"
|
||||||
|
Loading…
Reference in New Issue
Block a user