diff options
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt b/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt index fcac6b3e..290e14a4 100644 --- a/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt +++ b/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt @@ -7,9 +7,12 @@ package com.wireguard.android.activity import android.os.Bundle import android.util.Log +import android.view.View import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity +import androidx.core.view.forEach +import androidx.databinding.DataBindingUtil import androidx.lifecycle.lifecycleScope import com.wireguard.android.Application import com.wireguard.android.R @@ -20,7 +23,9 @@ 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.QuantityFormatter import com.wireguard.android.util.TunnelImporter +import kotlinx.coroutines.delay import kotlinx.coroutines.launch class TvMainActivity : AppCompatActivity() { @@ -50,12 +55,15 @@ class TvMainActivity : AppCompatActivity() { Toast.makeText(this@TvMainActivity, message, Toast.LENGTH_LONG).show() Log.e(TAG, message, e) } + updateStats() } } + lateinit var binding: TvActivityBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val binding = TvActivityBinding.inflate(layoutInflater) + binding = TvActivityBinding.inflate(layoutInflater) lifecycleScope.launch { binding.tunnels = Application.getTunnelManager().getTunnels() } binding.rowConfigurationHandler = object : ObservableKeyedRecyclerViewAdapter.RowConfigurationHandler<TvTunnelListItemBinding, ObservableTunnel> { override fun onConfigureRow(binding: TvTunnelListItemBinding, item: ObservableTunnel, position: Int) { @@ -79,6 +87,33 @@ class TvMainActivity : AppCompatActivity() { } binding.executePendingBindings() setContentView(binding.root) + + lifecycleScope.launch { + while (true) { + updateStats() + delay(1000) + } + } + } + + private suspend fun updateStats() { + binding.tunnelList.forEach { viewItem -> + val listItem = DataBindingUtil.findBinding<TvTunnelListItemBinding>(viewItem) ?: return@forEach + try { + val tunnel = listItem.item!! + if (tunnel.state != Tunnel.State.UP) { + throw Exception() + } + val statistics = tunnel.getStatisticsAsync() + val rx = statistics.totalRx() + val tx = statistics.totalTx() + listItem.tunnelTransfer.text = getString(R.string.transfer_rx_tx, QuantityFormatter.formatBytes(rx), QuantityFormatter.formatBytes(tx)) + listItem.tunnelTransfer.visibility = View.VISIBLE + } catch (_: Throwable) { + listItem.tunnelTransfer.visibility = View.GONE + listItem.tunnelTransfer.text = "" + } + } } companion object { |