diff --git a/app/src/full/java/com/celzero/bravedns/adapter/OneWgConfigAdapter.kt b/app/src/full/java/com/celzero/bravedns/adapter/OneWgConfigAdapter.kt index 288f6332..ef3fdb49 100644 --- a/app/src/full/java/com/celzero/bravedns/adapter/OneWgConfigAdapter.kt +++ b/app/src/full/java/com/celzero/bravedns/adapter/OneWgConfigAdapter.kt @@ -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(DIFF_CALLBACK) { private var lifecycleOwner: LifecycleOwner? = null @@ -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) @@ -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) @@ -286,8 +277,8 @@ 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 "" @@ -295,7 +286,7 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns } 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() diff --git a/app/src/full/java/com/celzero/bravedns/adapter/WgConfigAdapter.kt b/app/src/full/java/com/celzero/bravedns/adapter/WgConfigAdapter.kt index 07df8d68..a7082154 100644 --- a/app/src/full/java/com/celzero/bravedns/adapter/WgConfigAdapter.kt +++ b/app/src/full/java/com/celzero/bravedns/adapter/WgConfigAdapter.kt @@ -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 @@ -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(DIFF_CALLBACK) { private var lifecycleOwner: LifecycleOwner? = null @@ -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 @@ -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 @@ -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 { @@ -364,8 +354,8 @@ 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 "" @@ -373,7 +363,7 @@ class WgConfigAdapter(private val context: Context, private val listener: DnsSta } 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() diff --git a/app/src/full/java/com/celzero/bravedns/ui/activity/WgConfigDetailActivity.kt b/app/src/full/java/com/celzero/bravedns/ui/activity/WgConfigDetailActivity.kt index 7c960889..32ecae9a 100644 --- a/app/src/full/java/com/celzero/bravedns/ui/activity/WgConfigDetailActivity.kt +++ b/app/src/full/java/com/celzero/bravedns/ui/activity/WgConfigDetailActivity.kt @@ -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 @@ -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 @@ -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)) @@ -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) } diff --git a/app/src/full/java/com/celzero/bravedns/util/UIUtils.kt b/app/src/full/java/com/celzero/bravedns/util/UIUtils.kt index 1e79b684..edae9e3a 100644 --- a/app/src/full/java/com/celzero/bravedns/util/UIUtils.kt +++ b/app/src/full/java/com/celzero/bravedns/util/UIUtils.kt @@ -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)) { diff --git a/app/src/main/res/layout/activity_wg_detail.xml b/app/src/main/res/layout/activity_wg_detail.xml index 715e2d71..31953c44 100644 --- a/app/src/main/res/layout/activity_wg_detail.xml +++ b/app/src/main/res/layout/activity_wg_detail.xml @@ -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" />