Skip to content

Commit

Permalink
ui: optimize app name retrieval in Network Log adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
hussainmohd-a committed Sep 28, 2024
1 parent 8d374f9 commit 7af13d5
Showing 1 changed file with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.celzero.bravedns.util.KnownPorts
import com.celzero.bravedns.util.Protocol
import com.celzero.bravedns.util.UIUtils.getDurationInHumanReadableFormat
import com.celzero.bravedns.util.Utilities
import com.celzero.bravedns.util.Utilities.getDefaultIcon
import com.celzero.bravedns.util.Utilities.getIcon
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -75,6 +76,7 @@ class ConnectionTrackerAdapter(private val context: Context) :
private const val MAX_BYTES = 500000 // 500 KB
private const val MAX_TIME_TCP = 135 // seconds
private const val MAX_TIME_UDP = 135 // seconds
private const val NO_USER_ID = 0
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ConnectionTrackerViewHolder {
Expand Down Expand Up @@ -146,40 +148,31 @@ class ConnectionTrackerAdapter(private val context: Context) :
private fun displayAppDetails(ct: ConnectionTracker) {
io {
uiCtx {
// append the usrId with app name if the usrId is not 0
// fixme: move the 0 to a constant
if (ct.usrId != 0) {
b.connectionAppName.text =
context.getString(
R.string.about_version_install_source,
ct.appName,
ct.usrId.toString()
)
} else {
b.connectionAppName.text = ct.appName
}

val apps = FirewallManager.getPackageNamesByUid(ct.uid)
val count = apps.count()

if (apps.isEmpty()) {
loadAppIcon(Utilities.getDefaultIcon(context))
return@uiCtx
}
val appName = when {
ct.usrId != NO_USER_ID -> context.getString(
R.string.about_version_install_source,
ct.appName,
ct.usrId.toString()
)

val count = apps.count()
val appName =
if (count > 1) {
context.getString(
R.string.ctbs_app_other_apps,
ct.appName,
(count).minus(1).toString()
)
} else {
ct.appName
}
count > 1 -> context.getString(
R.string.ctbs_app_other_apps,
ct.appName,
"${count - 1}"
)

else -> ct.appName
}

b.connectionAppName.text = appName
loadAppIcon(getIcon(context, apps[0], /*No app name */ ""))
if (apps.isEmpty()) {
loadAppIcon(getDefaultIcon(context))
} else {
loadAppIcon(getIcon(context, apps[0]))
}
}
}
}
Expand Down

0 comments on commit 7af13d5

Please sign in to comment.