Skip to content

Commit

Permalink
ui: improve proxy status handling for better UI representation
Browse files Browse the repository at this point in the history
  • Loading branch information
hussainmohd-a committed Sep 28, 2024
1 parent 7af13d5 commit f55a92a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class OneWgConfigAdapter(private val context: Context, private val listener: DnsStatusListener, private val persistentState: PersistentState) :
class OneWgConfigAdapter(private val context: Context, private val listener: DnsStatusListener) :
PagingDataAdapter<WgConfigFiles, OneWgConfigAdapter.WgInterfaceViewHolder>(DIFF_CALLBACK) {

private var lifecycleOwner: LifecycleOwner? = null
Expand Down Expand Up @@ -87,15 +87,6 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
}
}

private enum class ProxyStatus(val id: Long) {
TOK(Backend.TOK),
TUP(Backend.TUP),
TZZ(Backend.TZZ),
TNT(Backend.TNT),
TKO(Backend.TKO),
END(Backend.END)
}

override fun onBindViewHolder(holder: WgInterfaceViewHolder, position: Int) {
val wgConfigFiles: WgConfigFiles = getItem(position) ?: return
holder.update(wgConfigFiles)
Expand Down Expand Up @@ -266,15 +257,15 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
}
}

private fun getStrokeColorForStatus(status: ProxyStatus?, stats: RouterStats?): Int{
private fun getStrokeColorForStatus(status: UIUtils.ProxyStatus?, stats: RouterStats?): Int{
return when (status) {
ProxyStatus.TOK -> if (stats?.lastOK == 0L) R.attr.chipTextNeutral else R.attr.accentGood
ProxyStatus.TUP, ProxyStatus.TZZ, ProxyStatus.TNT -> R.attr.chipTextNeutral
UIUtils.ProxyStatus.TOK -> if (stats?.lastOK == 0L) R.attr.chipTextNeutral else R.attr.accentGood
UIUtils.ProxyStatus.TUP, UIUtils.ProxyStatus.TZZ, UIUtils.ProxyStatus.TNT -> R.attr.chipTextNeutral
else -> R.attr.chipTextNegative
}
}

private fun getStatusText(status: ProxyStatus?, handshakeTime: String? = null, stats: RouterStats?): String {
private fun getStatusText(status: UIUtils.ProxyStatus?, handshakeTime: String? = null, stats: RouterStats?): String {
if (status == null) return context.getString(R.string.status_waiting).replaceFirstChar(Char::titlecase)

val baseText = context.getString(UIUtils.getProxyStatusStringRes(status.id)).replaceFirstChar(Char::titlecase)
Expand All @@ -286,16 +277,16 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
}
}

private fun getIdleStatusText(status: ProxyStatus?, stats: RouterStats?): String {
if (status != ProxyStatus.TZZ && status != ProxyStatus.TNT) return ""
private fun getIdleStatusText(status: UIUtils.ProxyStatus?, stats: RouterStats?): String {
if (status != UIUtils.ProxyStatus.TZZ && status != UIUtils.ProxyStatus.TNT) return ""
if (stats == null || stats.lastOK == 0L) return ""
if (System.currentTimeMillis() - stats.lastOK >= 30 * DateUtils.SECOND_IN_MILLIS) return ""

return context.getString(R.string.dns_connected).replaceFirstChar(Char::titlecase)
}

private fun updateProxyStatusUi(statusId: Long?, stats: RouterStats?) {
val status = ProxyStatus.entries.find { it.id == statusId } // Convert to enum
val status = UIUtils.ProxyStatus.entries.find { it.id == statusId } // Convert to enum

val handshakeTime = getHandshakeTime(stats).toString()

Expand Down
28 changes: 9 additions & 19 deletions app/src/full/java/com/celzero/bravedns/adapter/WgConfigAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import com.celzero.bravedns.database.WgConfigFiles
import com.celzero.bravedns.database.WgConfigFilesImmutable
import com.celzero.bravedns.databinding.ListItemWgGeneralInterfaceBinding
import com.celzero.bravedns.net.doh.Transaction
import com.celzero.bravedns.service.PersistentState
import com.celzero.bravedns.service.ProxyManager
import com.celzero.bravedns.service.VpnController
import com.celzero.bravedns.service.WireguardManager
Expand All @@ -56,7 +55,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class WgConfigAdapter(private val context: Context, private val listener: DnsStatusListener, private val persistentState: PersistentState) :
class WgConfigAdapter(private val context: Context, private val listener: DnsStatusListener, private val splitDns: Boolean) :
PagingDataAdapter<WgConfigFiles, WgConfigAdapter.WgInterfaceViewHolder>(DIFF_CALLBACK) {

private var lifecycleOwner: LifecycleOwner? = null
Expand All @@ -82,15 +81,6 @@ class WgConfigAdapter(private val context: Context, private val listener: DnsSta
}
}

private enum class ProxyStatus(val id: Long) {
TOK(Backend.TOK),
TUP(Backend.TUP),
TZZ(Backend.TZZ),
TNT(Backend.TNT),
TKO(Backend.TKO),
END(Backend.END)
}

override fun onBindViewHolder(holder: WgInterfaceViewHolder, position: Int) {
val item = getItem(position)
val wgConfigFiles: WgConfigFiles = item ?: return
Expand Down Expand Up @@ -216,7 +206,7 @@ class WgConfigAdapter(private val context: Context, private val listener: DnsSta
val pair = VpnController.getSupportedIpVersion(id)
val c = WireguardManager.getConfigById(config.id)
val stats = VpnController.getProxyStats(id)
val dnsStatusId = if (persistentState.splitDns) {
val dnsStatusId = if (splitDns) {
VpnController.getDnsStatus(id)
} else {
null
Expand Down Expand Up @@ -338,16 +328,16 @@ class WgConfigAdapter(private val context: Context, private val listener: DnsSta
}
}

private fun getStrokeColorForStatus(status: ProxyStatus?, stats: RouterStats?): Int {
private fun getStrokeColorForStatus(status: UIUtils.ProxyStatus?, stats: RouterStats?): Int {
return when (status) {
ProxyStatus.TOK -> if (stats?.lastOK == 0L) R.attr.chipTextNeutral else R.attr.accentGood
ProxyStatus.TUP, ProxyStatus.TZZ, ProxyStatus.TNT -> R.attr.chipTextNeutral
UIUtils.ProxyStatus.TOK -> if (stats?.lastOK == 0L) R.attr.chipTextNeutral else R.attr.accentGood
UIUtils.ProxyStatus.TUP, UIUtils.ProxyStatus.TZZ, UIUtils.ProxyStatus.TNT -> R.attr.chipTextNeutral
else -> R.attr.chipTextNegative
}
}

private fun getStatusText(
status: ProxyStatus?,
status: UIUtils.ProxyStatus?,
handshakeTime: String? = null,
stats: RouterStats?
): String {
Expand All @@ -364,16 +354,16 @@ class WgConfigAdapter(private val context: Context, private val listener: DnsSta
}
}

private fun getIdleStatusText(status: ProxyStatus?, stats: RouterStats?): String {
if (status != ProxyStatus.TZZ && status != ProxyStatus.TNT) return ""
private fun getIdleStatusText(status: UIUtils.ProxyStatus?, stats: RouterStats?): String {
if (status != UIUtils.ProxyStatus.TZZ && status != UIUtils.ProxyStatus.TNT) return ""
if (stats == null || stats.lastOK == 0L) return ""
if (System.currentTimeMillis() - stats.lastOK >= 30 * DateUtils.SECOND_IN_MILLIS) return ""

return context.getString(R.string.dns_connected).replaceFirstChar(Char::titlecase)
}

private fun updateProxyStatusUi(statusId: Long?, stats: RouterStats?) {
val status = ProxyStatus.entries.find { it.id == statusId } // Convert to enum
val status = UIUtils.ProxyStatus.entries.find { it.id == statusId } // Convert to enum

val handshakeTime = getHandshakeTime(stats).toString()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ import backend.Backend
import backend.RouterStats
import by.kirich1409.viewbindingdelegate.viewBinding
import com.celzero.bravedns.R
import com.celzero.bravedns.adapter.WgConfigAdapter
import com.celzero.bravedns.adapter.WgIncludeAppsAdapter
import com.celzero.bravedns.adapter.WgPeersAdapter
import com.celzero.bravedns.database.WgConfigFiles
import com.celzero.bravedns.databinding.ActivityWgDetailBinding
import com.celzero.bravedns.service.PersistentState
import com.celzero.bravedns.service.ProxyManager
Expand All @@ -45,11 +43,13 @@ import com.celzero.bravedns.service.WireguardManager.ERR_CODE_VPN_NOT_ACTIVE
import com.celzero.bravedns.service.WireguardManager.ERR_CODE_VPN_NOT_FULL
import com.celzero.bravedns.service.WireguardManager.ERR_CODE_WG_INVALID
import com.celzero.bravedns.service.WireguardManager.INVALID_CONF_ID
import com.celzero.bravedns.ui.activity.NetworkLogsActivity.Companion.RULES_SEARCH_ID_WIREGUARD
import com.celzero.bravedns.ui.dialog.WgAddPeerDialog
import com.celzero.bravedns.ui.dialog.WgIncludeAppsDialog
import com.celzero.bravedns.util.Constants
import com.celzero.bravedns.util.Themes
import com.celzero.bravedns.util.UIUtils
import com.celzero.bravedns.util.UIUtils.fetchColor
import com.celzero.bravedns.util.Utilities
import com.celzero.bravedns.viewmodel.ProxyAppsMappingViewModel
import com.celzero.bravedns.wireguard.Config
Expand Down Expand Up @@ -192,52 +192,88 @@ class WgConfigDetailActivity : AppCompatActivity(R.layout.activity_wg_detail) {
prefillConfig(config)
}


private suspend fun updateStatusUi(id: Int) {
val config = WireguardManager.getConfigFilesById(id)
val cid = ProxyManager.ID_WG_BASE + id
if (config?.isActive == true) {
var status: String
val statusId = VpnController.getProxyStatusById(cid)
val stats = VpnController.getProxyStats(cid)
val ps = UIUtils.ProxyStatus.entries.find { it.id == statusId }
uiCtx {
if (statusId != null) {
var resId = UIUtils.getProxyStatusStringRes(statusId)
// change the color based on the status
if (statusId == Backend.TOK) {
// if the lastOK is 0, then the handshake is not yet completed
// so show the status as waiting
if (stats?.lastOK == 0L) {
resId = R.string.status_waiting
}
}
status = getString(resId).replaceFirstChar(Char::titlecase)

if ((statusId == Backend.TZZ || statusId == Backend.TNT) && stats != null) {
// for idle state, if lastOk is less than 30 sec, then show as connected
if (
stats.lastOK != 0L &&
System.currentTimeMillis() - stats.lastOK <
30 * DateUtils.SECOND_IN_MILLIS
) {
status =
getString(R.string.dns_connected).replaceFirstChar(Char::titlecase)
}
}

val handshakeTime = getHandshakeTime(stats).toString()
val statusText = getIdleStatusText(ps, stats)
.ifEmpty { getStatusText(ps, handshakeTime, stats) }
b.statusText.text = statusText
} else {
status = getString(R.string.status_waiting).replaceFirstChar(Char::titlecase)
b.statusText.text =
getString(R.string.status_waiting).replaceFirstChar(Char::titlecase)
}
b.statusText.text = status
val strokeColor = getStrokeColorForStatus(ps, stats)
b.interfaceDetailCard.strokeWidth = 2
b.interfaceDetailCard.strokeColor = fetchColor(this, strokeColor)
}
} else {
uiCtx {
b.interfaceDetailCard.strokeWidth = 0
b.statusText.text =
getString(R.string.lbl_disabled).replaceFirstChar(Char::titlecase)
}
}
}

private fun getStatusText(
status: UIUtils.ProxyStatus?,
handshakeTime: String? = null,
stats: RouterStats?
): String {
if (status == null) return getString(R.string.status_waiting)
.replaceFirstChar(Char::titlecase)

val baseText = getString(UIUtils.getProxyStatusStringRes(status.id))
.replaceFirstChar(Char::titlecase)

return if (stats?.lastOK != 0L && handshakeTime != null) {
getString(R.string.about_version_install_source, baseText, handshakeTime)
} else {
baseText
}
}

private fun getIdleStatusText(status: UIUtils.ProxyStatus?, stats: RouterStats?): String {
if (status != UIUtils.ProxyStatus.TZZ && status != UIUtils.ProxyStatus.TNT) return ""
if (stats == null || stats.lastOK == 0L) return ""
if (System.currentTimeMillis() - stats.lastOK >= 30 * DateUtils.SECOND_IN_MILLIS) return ""

return getString(R.string.dns_connected).replaceFirstChar(Char::titlecase)
}

private fun getHandshakeTime(stats: RouterStats?): CharSequence {
if (stats == null) {
return ""
}
if (stats.lastOK == 0L) {
return ""
}
val now = System.currentTimeMillis()
// returns a string describing 'time' as a time relative to 'now'
return DateUtils.getRelativeTimeSpanString(
stats.lastOK,
now,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE
)
}

private fun getStrokeColorForStatus(status: UIUtils.ProxyStatus?, stats: RouterStats?): Int {
return when (status) {
UIUtils.ProxyStatus.TOK -> if (stats?.lastOK == 0L) R.attr.chipTextNeutral else R.attr.accentGood
UIUtils.ProxyStatus.TUP, UIUtils.ProxyStatus.TZZ, UIUtils.ProxyStatus.TNT -> R.attr.chipTextNeutral
else -> R.attr.chipTextNegative
}
}

private fun showInvalidConfigDialog() {
val builder = MaterialAlertDialogBuilder(this)
builder.setTitle(getString(R.string.lbl_wireguard))
Expand Down Expand Up @@ -451,18 +487,15 @@ class WgConfigDetailActivity : AppCompatActivity(R.layout.activity_wg_detail) {
b.catchAllCheck.setOnClickListener { updateCatchAll(b.catchAllCheck.isChecked) }

b.logsBtn.setOnClickListener {
startActivity(
NetworkLogsActivity.Tabs.NETWORK_LOGS.screen,
ProxyManager.ID_WG_BASE + configId
)
startActivity(ProxyManager.ID_WG_BASE + configId)
}
}

private fun startActivity(screenToLoad: Int, searchParam: String?) {
private fun startActivity(searchParam: String?) {
val intent = Intent(this, NetworkLogsActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
intent.putExtra(Constants.VIEW_PAGER_SCREEN_TO_LOAD, screenToLoad)
intent.putExtra(Constants.SEARCH_QUERY, searchParam ?: "")
val query = RULES_SEARCH_ID_WIREGUARD + searchParam
intent.putExtra(Constants.SEARCH_QUERY, query)
startActivity(intent)
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/full/java/com/celzero/bravedns/util/UIUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ object UIUtils {
}
}

enum class ProxyStatus(val id: Long) {
TOK(Backend.TOK),
TUP(Backend.TUP),
TZZ(Backend.TZZ),
TNT(Backend.TNT),
TKO(Backend.TKO),
END(Backend.END)
}

fun formatToRelativeTime(context: Context, timestamp: Long): String {
val now = System.currentTimeMillis()
return if (DateUtils.isToday(timestamp)) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_wg_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:textStyle="italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/config_title_layout" />

Expand Down

0 comments on commit f55a92a

Please sign in to comment.