Skip to content

Commit

Permalink
Merge pull request #1398 from hussainmohd-a/v055i
Browse files Browse the repository at this point in the history
v055i
  • Loading branch information
hussainmohd-a committed Apr 30, 2024
2 parents b742223 + 0789f9c commit 7df3853
Show file tree
Hide file tree
Showing 19 changed files with 333 additions and 203 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ dependencies {
fullImplementation 'com.github.kirich1409:viewbindingpropertydelegate-noreflection:1.5.9'

// from: https://jitpack.io/#celzero/firestack
download 'com.github.celzero:firestack:1f812ec9e6@aar'
implementation 'com.github.celzero:firestack:1f812ec9e6@aar'
download 'com.github.celzero:firestack:dd04f72717@aar'
implementation 'com.github.celzero:firestack:dd04f72717@aar'

// Work manager
implementation('androidx.work:work-runtime-ktx:2.9.0') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.celzero.bravedns.adapter
import Logger
import Logger.LOG_TAG_UI
import android.content.Context
import android.content.res.ColorStateList
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -33,8 +32,10 @@ import com.celzero.bravedns.data.AppConnection
import com.celzero.bravedns.databinding.ListItemAppDomainDetailsBinding
import com.celzero.bravedns.service.DomainRulesManager
import com.celzero.bravedns.ui.bottomsheet.AppDomainRulesBottomSheet
import com.celzero.bravedns.util.UIUtils.fetchColor
import com.celzero.bravedns.util.UIUtils
import com.celzero.bravedns.util.Utilities
import com.celzero.bravedns.util.Utilities.removeBeginningTrailingCommas
import kotlin.math.log2

class AppWiseDomainsAdapter(
val context: Context,
Expand All @@ -46,6 +47,9 @@ class AppWiseDomainsAdapter(
),
AppDomainRulesBottomSheet.OnBottomSheetDialogFragmentDismiss {

private var maxValue: Int = 0
private var minPercentage: Int = 100

companion object {
private val DIFF_CALLBACK =
object : DiffUtil.ItemCallback<AppConnection>() {
Expand Down Expand Up @@ -90,29 +94,48 @@ class AppWiseDomainsAdapter(
holder.update(appConnection)
}

private fun calculatePercentage(c: Double): Int {
val value = (log2(c) * 100).toInt()
// maxValue will be based on the count returned by db query (order by count desc)
if (value > maxValue) {
maxValue = value
}
return if (maxValue == 0) {
0
} else {
val percentage = (value * 100 / maxValue)
// minPercentage is used to show the progress bar when the percentage is 0
if (percentage < minPercentage && percentage != 0) {
minPercentage = percentage
}
percentage
}
}

inner class ConnectionDetailsViewHolder(private val b: ListItemAppDomainDetailsBinding) :
RecyclerView.ViewHolder(b.root) {
fun update(conn: AppConnection) {
displayTransactionDetails(conn)
setupClickListeners(conn)
}

private fun displayTransactionDetails(appConnection: AppConnection) {
b.acdCount.text = appConnection.count.toString()
b.acdDomain.text = appConnection.appOrDnsName
if (appConnection.ipAddress.isNotEmpty()) {
private fun displayTransactionDetails(conn: AppConnection) {
b.acdCount.text = conn.count.toString()
b.acdDomain.text = conn.appOrDnsName
b.acdFlag.text = conn.flag
if (conn.ipAddress.isNotEmpty()) {
b.acdIpAddress.visibility = View.VISIBLE
b.acdIpAddress.text = beautifyIpString(appConnection.ipAddress)
b.acdIpAddress.text = beautifyIpString(conn.ipAddress)
} else {
b.acdIpAddress.visibility = View.GONE
}
updateStatusUi(appConnection.uid, appConnection.appOrDnsName)
updateStatusUi(conn)
}

private fun setupClickListeners(appConn: AppConnection) {
private fun setupClickListeners(conn: AppConnection) {
b.acdContainer.setOnClickListener {
// open bottom sheet to apply domain/ip rules
openBottomSheet(appConn)
openBottomSheet(conn)
}
}

Expand Down Expand Up @@ -141,52 +164,40 @@ class AppWiseDomainsAdapter(
return removeBeginningTrailingCommas(d).replace(",,", ",").replace(",", ", ")
}

private fun updateStatusUi(uid: Int, domain: String?) {
if (domain == null) {
b.acdFlag.text = context.getString(R.string.ci_no_rule_initial)
private fun updateStatusUi(conn: AppConnection) {
if (conn.appOrDnsName.isNullOrEmpty()) {
b.progress.visibility = View.GONE
return
}

val status = DomainRulesManager.getDomainRule(domain, uid)
val status = DomainRulesManager.getDomainRule(conn.appOrDnsName, uid)
when (status) {
DomainRulesManager.Status.NONE -> {
b.acdFlag.text = context.getString(R.string.ci_no_rule_initial)
}
DomainRulesManager.Status.BLOCK -> {
b.acdFlag.text = context.getString(R.string.ci_blocked_initial)
}
DomainRulesManager.Status.TRUST -> {
b.acdFlag.text = context.getString(R.string.ci_trust_initial)
}
}

// returns the text and background color for the button
val t = getToggleBtnUiParams(status)
b.acdFlag.setTextColor(t.txtColor)
b.acdFlag.backgroundTintList = ColorStateList.valueOf(t.bgColor)
}

private fun getToggleBtnUiParams(id: DomainRulesManager.Status): ToggleBtnUi {
return when (id) {
DomainRulesManager.Status.NONE -> {
ToggleBtnUi(
fetchColor(context, R.attr.chipTextNeutral),
fetchColor(context, R.attr.chipBgColorNeutral)
b.progress.setIndicatorColor(
UIUtils.fetchToggleBtnColors(context, R.color.chipTextNeutral)
)
}
DomainRulesManager.Status.BLOCK -> {
ToggleBtnUi(
fetchColor(context, R.attr.chipTextNegative),
fetchColor(context, R.attr.chipBgColorNegative)
b.progress.setIndicatorColor(
UIUtils.fetchToggleBtnColors(context, R.color.accentBad)
)
}
DomainRulesManager.Status.TRUST -> {
ToggleBtnUi(
fetchColor(context, R.attr.chipTextPositive),
fetchColor(context, R.attr.chipBgColorPositive)
b.progress.setIndicatorColor(
UIUtils.fetchToggleBtnColors(context, R.color.accentGood)
)
}
}

var p = calculatePercentage(conn.count.toDouble())
if (p == 0) {
p = minPercentage / 2
}

if (Utilities.isAtleastN()) {
b.progress.setProgress(p, true)
} else {
b.progress.progress = p
}
}
}

Expand Down
122 changes: 60 additions & 62 deletions app/src/full/java/com/celzero/bravedns/adapter/AppWiseIpsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.celzero.bravedns.adapter
import Logger
import Logger.LOG_TAG_UI
import android.content.Context
import android.content.res.ColorStateList
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -33,34 +32,29 @@ import com.celzero.bravedns.data.AppConnection
import com.celzero.bravedns.databinding.ListItemAppIpDetailsBinding
import com.celzero.bravedns.service.IpRulesManager
import com.celzero.bravedns.ui.bottomsheet.AppIpRulesBottomSheet
import com.celzero.bravedns.util.UIUtils.fetchColor
import com.celzero.bravedns.util.UIUtils
import com.celzero.bravedns.util.Utilities
import com.celzero.bravedns.util.Utilities.removeBeginningTrailingCommas
import kotlin.math.log2

class AppWiseIpsAdapter(val context: Context, val lifecycleOwner: LifecycleOwner, val uid: Int) :
PagingDataAdapter<AppConnection, AppWiseIpsAdapter.ConnectionDetailsViewHolder>(DIFF_CALLBACK),
AppIpRulesBottomSheet.OnBottomSheetDialogFragmentDismiss {

private var maxValue: Int = 0
private var minPercentage: Int = 100

companion object {
private val DIFF_CALLBACK =
object : DiffUtil.ItemCallback<AppConnection>() {
override fun areItemsTheSame(old: AppConnection, new: AppConnection) = old == new

override fun areItemsTheSame(
oldConnection: AppConnection,
newConnection: AppConnection
) = oldConnection == newConnection

override fun areContentsTheSame(
oldConnection: AppConnection,
newConnection: AppConnection
) = oldConnection == newConnection
override fun areContentsTheSame(old: AppConnection, new: AppConnection) = old == new
}
}

private lateinit var adapter: AppWiseIpsAdapter

// ui component to update/toggle the buttons
data class ToggleBtnUi(val txtColor: Int, val bgColor: Int)

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
Expand All @@ -80,23 +74,41 @@ class AppWiseIpsAdapter(val context: Context, val lifecycleOwner: LifecycleOwner
holder.update(appConnection)
}

private fun calculatePercentage(c: Double): Int {
val value = (log2(c) * 100).toInt()
// maxValue will be based on the count returned by db query (order by count desc)
if (value > maxValue) {
maxValue = value
}
return if (maxValue == 0) {
0
} else {
val percentage = (value * 100 / maxValue)
// minPercentage is used to show the progress bar when the percentage is 0
if (percentage < minPercentage && percentage != 0) {
minPercentage = percentage
}
percentage
}
}

inner class ConnectionDetailsViewHolder(private val b: ListItemAppIpDetailsBinding) :
RecyclerView.ViewHolder(b.root) {
fun update(conn: AppConnection) {
displayTransactionDetails(conn)
setupClickListeners(conn)
}

private fun setupClickListeners(appConn: AppConnection) {
private fun setupClickListeners(conn: AppConnection) {
b.acdContainer.setOnClickListener {
// open bottom sheet to apply domain/ip rules
openBottomSheet(appConn)
openBottomSheet(conn)
}
}

private fun openBottomSheet(appConn: AppConnection) {
private fun openBottomSheet(conn: AppConnection) {
if (context !is AppCompatActivity) {
Logger.w(LOG_TAG_UI, "Error opening the app conn bottom sheet")
Logger.w(LOG_TAG_UI, "err opening the app conn bottom sheet")
return
}

Expand All @@ -107,26 +119,27 @@ class AppWiseIpsAdapter(val context: Context, val lifecycleOwner: LifecycleOwner
// so sending the data using Bundles
val bundle = Bundle()
bundle.putInt(AppIpRulesBottomSheet.UID, uid)
bundle.putString(AppIpRulesBottomSheet.IP_ADDRESS, appConn.ipAddress)
bundle.putString(AppIpRulesBottomSheet.IP_ADDRESS, conn.ipAddress)
bundle.putString(
AppIpRulesBottomSheet.DOMAINS,
beautifyDomainString(appConn.appOrDnsName ?: "")
beautifyDomainString(conn.appOrDnsName ?: "")
)
bottomSheetFragment.arguments = bundle
bottomSheetFragment.dismissListener(adapter, absoluteAdapterPosition)
bottomSheetFragment.show(context.supportFragmentManager, bottomSheetFragment.tag)
}

private fun displayTransactionDetails(appConnection: AppConnection) {
b.acdCount.text = appConnection.count.toString()
b.acdIpAddress.text = appConnection.ipAddress
if (!appConnection.appOrDnsName.isNullOrEmpty()) {
private fun displayTransactionDetails(conn: AppConnection) {
b.acdCount.text = conn.count.toString()
b.acdIpAddress.text = conn.ipAddress
b.acdFlag.text = conn.flag
if (!conn.appOrDnsName.isNullOrEmpty()) {
b.acdDomainName.visibility = View.VISIBLE
b.acdDomainName.text = beautifyDomainString(appConnection.appOrDnsName)
b.acdDomainName.text = beautifyDomainString(conn.appOrDnsName)
} else {
b.acdDomainName.visibility = View.GONE
}
updateStatusUi(appConnection.uid, appConnection.ipAddress)
updateStatusUi(conn)
}

private fun beautifyDomainString(d: String): String {
Expand All @@ -135,56 +148,41 @@ class AppWiseIpsAdapter(val context: Context, val lifecycleOwner: LifecycleOwner
return removeBeginningTrailingCommas(d).replace(",,", ",").replace(",", ", ")
}

private fun updateStatusUi(uid: Int, ipAddress: String) {
val status = IpRulesManager.getMostSpecificRuleMatch(uid, ipAddress)
private fun updateStatusUi(conn: AppConnection) {
val status = IpRulesManager.getMostSpecificRuleMatch(conn.uid, conn.ipAddress)
when (status) {
IpRulesManager.IpRuleStatus.NONE -> {
b.acdFlag.text = context.getString(R.string.ci_no_rule_initial)
}
IpRulesManager.IpRuleStatus.BLOCK -> {
b.acdFlag.text = context.getString(R.string.ci_blocked_initial)
}
IpRulesManager.IpRuleStatus.BYPASS_UNIVERSAL -> {
b.acdFlag.text = context.getString(R.string.ci_bypass_universal_initial)
}
IpRulesManager.IpRuleStatus.TRUST -> {
b.acdFlag.text = context.getString(R.string.ci_trust_initial)
}
}

// returns the text and background color for the button
val t = getToggleBtnUiParams(status)
b.acdFlag.setTextColor(t.txtColor)
b.acdFlag.backgroundTintList = ColorStateList.valueOf(t.bgColor)
}

private fun getToggleBtnUiParams(id: IpRulesManager.IpRuleStatus): ToggleBtnUi {
return when (id) {
IpRulesManager.IpRuleStatus.NONE -> {
ToggleBtnUi(
fetchColor(context, R.attr.chipTextNeutral),
fetchColor(context, R.attr.chipBgColorNeutral)
b.progress.setIndicatorColor(
UIUtils.fetchToggleBtnColors(context, R.color.chipTextNeutral)
)
}
IpRulesManager.IpRuleStatus.BLOCK -> {
ToggleBtnUi(
fetchColor(context, R.attr.chipTextNegative),
fetchColor(context, R.attr.chipBgColorNegative)
b.progress.setIndicatorColor(
UIUtils.fetchToggleBtnColors(context, R.color.accentBad)
)
}
IpRulesManager.IpRuleStatus.BYPASS_UNIVERSAL -> {
ToggleBtnUi(
fetchColor(context, R.attr.chipTextPositive),
fetchColor(context, R.attr.chipBgColorPositive)
b.progress.setIndicatorColor(
UIUtils.fetchToggleBtnColors(context, R.color.accentGood)
)
}
IpRulesManager.IpRuleStatus.TRUST -> {
ToggleBtnUi(
fetchColor(context, R.attr.chipTextPositive),
fetchColor(context, R.attr.chipBgColorPositive)
b.progress.setIndicatorColor(
UIUtils.fetchToggleBtnColors(context, R.color.accentGood)
)
}
}

var p = calculatePercentage(conn.count.toDouble())
if (p == 0) {
p = minPercentage / 2
}

if (Utilities.isAtleastN()) {
b.progress.setProgress(p, true)
} else {
b.progress.progress = p
}
}
}

Expand Down
Loading

0 comments on commit 7df3853

Please sign in to comment.