Skip to content

Commit

Permalink
Merge pull request #1387 from hussainmohd-a/v055e_rel
Browse files Browse the repository at this point in the history
V055h
  • Loading branch information
hussainmohd-a authored Apr 25, 2024
2 parents 861c092 + 4ee58cb commit 464c3b1
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 57 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:602a1e456d@aar'
implementation 'com.github.celzero:firestack:602a1e456d@aar'
download 'com.github.celzero:firestack:99521f3ca9@aar'
implementation 'com.github.celzero:firestack:99521f3ca9@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 @@ -242,10 +242,13 @@ class ConnectionTrackerAdapter(private val context: Context) :
hasMinSummary = true
} else {
b.connectionDataUsage.text = ""
b.connectionDuration.text =""
}
if (connType.isMetered()) {
b.connectionDelay.text = context.getString(R.string.symbol_currency)
hasMinSummary = true
} else {
b.connectionDelay.text = ""
}

if (isConnectionProxied(ct.blockedByRule, ct.proxyDetails)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
b.interfaceDetailCard.strokeWidth = 0
b.interfaceAppsCount.visibility = View.GONE
b.protocolInfoChipGroup.visibility = View.GONE
b.interfaceActiveLayout.visibility = View.GONE
b.oneWgCheck.isChecked = false
b.interfaceStatus.text =
context.getString(R.string.lbl_disabled).replaceFirstChar(Char::titlecase)
Expand Down Expand Up @@ -187,18 +188,24 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns

private fun updateStatusUi(config: WgConfigFiles, statusId: Long?, stats: Stats?) {
if (config.isActive) {
b.interfaceDetailCard.strokeColor = fetchColor(context, R.color.accentGood)
b.interfaceDetailCard.strokeWidth = 2
b.oneWgCheck.isChecked = true
b.interfaceAppsCount.visibility = View.VISIBLE
b.interfaceAppsCount.text = context.getString(R.string.one_wg_apps_added)
val status: String
val handShakeTime = getHandshakeTime(stats)
if (statusId != null) {
val resId = UIUtils.getProxyStatusStringRes(statusId)
var resId = UIUtils.getProxyStatusStringRes(statusId)
// change the color based on the status
if (statusId == Backend.TOK) {
b.interfaceDetailCard.strokeColor =
fetchColor(context, R.attr.chipTextPositive)
if (stats?.lastOK == 0L) {
b.interfaceDetailCard.strokeColor =
fetchColor(context, R.attr.chipTextNeutral)
resId = R.string.status_waiting
} else {
b.interfaceDetailCard.strokeColor =
fetchColor(context, R.attr.accentGood)
}
// cancel the job, as the status is connected
statusCheckJob?.cancel()
} else if (statusId == Backend.TUP || statusId == Backend.TZZ) {
Expand All @@ -208,7 +215,16 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
b.interfaceDetailCard.strokeColor =
fetchColor(context, R.attr.chipTextNegative)
}
status = context.getString(resId).replaceFirstChar(Char::titlecase)
status =
if (stats?.lastOK == 0L) {
context.getString(resId).replaceFirstChar(Char::titlecase)
} else {
context.getString(
R.string.about_version_install_source,
context.getString(resId).replaceFirstChar(Char::titlecase),
handShakeTime
)
}
} else {
b.interfaceDetailCard.strokeColor = fetchColor(context, R.attr.chipTextNegative)
b.interfaceDetailCard.strokeWidth = 2
Expand All @@ -219,6 +235,7 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
b.interfaceActiveLayout.visibility = View.VISIBLE
val rxtx = getRxTx(stats)
val time = getUpTime(stats)

if (time.isNotEmpty()) {
val t = context.getString(R.string.logs_card_duration, time)
b.interfaceActiveUptime.text =
Expand Down Expand Up @@ -248,8 +265,8 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
val now = System.currentTimeMillis()
// returns a string describing 'time' as a time relative to 'now'
return DateUtils.getRelativeTimeSpanString(
now,
stats.since,
now,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE
)
Expand All @@ -270,6 +287,23 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
return context.getString(R.string.two_argument_space, rx, tx)
}

private fun getHandshakeTime(stats: Stats?): 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
)
}

fun setupClickListeners(config: WgConfigFiles) {
b.interfaceDetailCard.setOnClickListener { launchConfigDetail(config.id) }

Expand Down
57 changes: 50 additions & 7 deletions app/src/full/java/com/celzero/bravedns/adapter/WgConfigAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ import com.celzero.bravedns.ui.activity.WgConfigDetailActivity
import com.celzero.bravedns.ui.activity.WgConfigEditorActivity.Companion.INTENT_EXTRA_WG_ID
import com.celzero.bravedns.util.UIUtils
import com.celzero.bravedns.util.Utilities
import java.util.concurrent.ConcurrentHashMap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.concurrent.ConcurrentHashMap

class WgConfigAdapter(private val context: Context) :
PagingDataAdapter<WgConfigFiles, WgConfigAdapter.WgInterfaceViewHolder>(DIFF_CALLBACK) {
Expand Down Expand Up @@ -127,6 +127,7 @@ class WgConfigAdapter(private val context: Context) :
// if lockdown is enabled, then show the lockdown card even if config is disabled
if (config.isLockdown) {
b.protocolInfoChipGroup.visibility = View.GONE
b.interfaceActiveLayout.visibility = View.GONE
b.interfaceConfigStatus.text =
context.getString(R.string.lbl_disabled).replaceFirstChar(Char::titlecase)
val id = ProxyManager.ID_WG_BASE + config.id
Expand All @@ -135,6 +136,7 @@ class WgConfigAdapter(private val context: Context) :
} else {
b.interfaceStatus.visibility = View.GONE
b.interfaceAppsCount.visibility = View.GONE
b.interfaceActiveLayout.visibility = View.GONE
b.interfaceDetailCard.strokeColor = UIUtils.fetchColor(context, R.attr.background)
b.interfaceDetailCard.strokeWidth = 0
b.interfaceSwitch.isChecked = false
Expand Down Expand Up @@ -257,6 +259,7 @@ class WgConfigAdapter(private val context: Context) :
if (!config.isActive) {
// no need to update the apps count if the config is disabled
b.interfaceAppsCount.visibility = View.GONE
b.interfaceActiveLayout.visibility = View.GONE
return
}

Expand All @@ -281,6 +284,7 @@ class WgConfigAdapter(private val context: Context) :
b.interfaceActiveLayout.visibility = View.VISIBLE
val time = getUpTime(stats)
val rxtx = getRxTx(stats)
val handShakeTime = getHandshakeTime(stats)
if (time.isNotEmpty()) {
val t = context.getString(R.string.logs_card_duration, time)
b.interfaceActiveUptime.text =
Expand All @@ -294,25 +298,47 @@ class WgConfigAdapter(private val context: Context) :
}
b.interfaceActiveRxTx.text = rxtx
if (statusId != null) {
val resId = UIUtils.getProxyStatusStringRes(statusId)
var resId = UIUtils.getProxyStatusStringRes(statusId)
// change the color based on the status
if (statusId == Backend.TOK) {
b.interfaceDetailCard.strokeColor =
UIUtils.fetchColor(context, R.attr.accentGood)
// if the lastOK is 0, then the handshake is not yet completed
// so show the status as waiting
if (stats?.lastOK == 0L) {
b.interfaceDetailCard.strokeColor =
UIUtils.fetchColor(context, R.attr.chipTextNeutral)
resId = R.string.status_waiting
} else {
b.interfaceDetailCard.strokeColor =
UIUtils.fetchColor(context, R.attr.accentGood)
}
cancelJobIfAny(config.id)
} else if (statusId == Backend.TUP || statusId == Backend.TZZ) {
} else if (
statusId == Backend.TUP ||
statusId == Backend.TZZ ||
statusId == Backend.TNT
) {
b.interfaceDetailCard.strokeColor =
UIUtils.fetchColor(context, R.attr.chipTextNeutral)
} else {
b.interfaceDetailCard.strokeColor =
UIUtils.fetchColor(context, R.attr.accentBad)
}
status = context.getString(resId).replaceFirstChar(Char::titlecase)
status =
if (stats?.lastOK == 0L) {
context.getString(resId).replaceFirstChar(Char::titlecase)
} else {
context.getString(
R.string.about_version_install_source,
context.getString(resId).replaceFirstChar(Char::titlecase),
handShakeTime
)
}
} else {
b.interfaceDetailCard.strokeColor =
UIUtils.fetchColor(context, R.attr.accentBad)
status =
context.getString(R.string.status_waiting).replaceFirstChar(Char::titlecase)
b.interfaceActiveLayout.visibility = View.GONE
}
b.interfaceStatus.text = status
} else {
Expand Down Expand Up @@ -350,8 +376,25 @@ class WgConfigAdapter(private val context: Context) :
val now = System.currentTimeMillis()
// returns a string describing 'time' as a time relative to 'now'
return DateUtils.getRelativeTimeSpanString(
now,
stats.since,
now,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE
)
}

private fun getHandshakeTime(stats: Stats?): 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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class WgIncludeAppsAdapter(

fun update(mapping: ProxyApplicationMapping) {
b.wgIncludeAppListApkLabelTv.text = mapping.appName
Logger.i(LOG_TAG_PROXY, "add ${mapping.appName} to ${mapping.proxyId} from $proxyId")

if (mapping.proxyId == "") {
b.wgIncludeAppAppDescTv.text = ""
Expand Down
20 changes: 11 additions & 9 deletions app/src/full/java/com/celzero/bravedns/service/ProxyManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ object ProxyManager : KoinComponent {
}

suspend fun deleteApps(m: Collection<FirewallManager.AppInfoTuple>) {
m.forEach { deleteApp(it) }
m.forEach { deleteApp(it.uid, it.packageName) }
}

suspend fun addApps(m: Collection<AppInfo?>) {
Expand Down Expand Up @@ -219,7 +219,10 @@ object ProxyManager : KoinComponent {
}

suspend fun addNewApp(appInfo: AppInfo?, proxyId: String = "", proxyName: String = "") {
if (appInfo == null) return
if (appInfo == null) {
Logger.e(LOG_TAG_PROXY, "AppInfo is null, cannot add to proxy")
return
}
val pam =
ProxyApplicationMapping(
appInfo.uid,
Expand All @@ -232,6 +235,7 @@ object ProxyManager : KoinComponent {
val pamTuple = ProxyAppMapTuple(appInfo.uid, appInfo.packageName, proxyId)
pamSet.add(pamTuple)
db.insert(pam)
Logger.i(LOG_TAG_PROXY, "Adding app for mapping: ${pam.appName}, ${pam.uid}")
}

private fun deleteFromCache(pam: ProxyApplicationMapping) {
Expand All @@ -242,15 +246,12 @@ object ProxyManager : KoinComponent {
}
}

suspend fun deleteApp(appInfo: AppInfo) {
return deleteApp(appInfo.uid, appInfo.packageName)
}

suspend fun deleteApp(appInfoTuple: FirewallManager.AppInfoTuple) {
return deleteApp(appInfoTuple.uid, appInfoTuple.packageName)
suspend fun deleteAppMappingsByUid(uid: Int) {
val m = pamSet.filter { it.uid == uid }
m.forEach { deleteApp(it.uid, it.packageName) }
}

suspend fun deleteApp(uid: Int, packageName: String) {
private suspend fun deleteApp(uid: Int, packageName: String) {
val pam = ProxyApplicationMapping(uid, packageName, "", "", false, "")
deleteFromCache(pam)
db.deleteApp(pam)
Expand All @@ -260,6 +261,7 @@ object ProxyManager : KoinComponent {
suspend fun clear() {
pamSet.clear()
db.deleteAll()
Logger.d(LOG_TAG_PROXY, "Deleting all apps for mapping")
}

fun isAnyAppSelected(proxyId: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ object WireguardManager : KoinComponent {
mappings.forEach {
val path = it.configPath
val config =
EncryptedFileManager.readWireguardConfig(applicationContext, path) ?: return@forEach
EncryptedFileManager.readWireguardConfig(applicationContext, path)
if (config == null) {
Logger.e(LOG_TAG_PROXY, "error loading wg config: $path, deleting...")
db.deleteConfig(it.id)
return@forEach
}
if (configs.none { i -> i.getId() == it.id }) {
val c =
Config.Builder()
Expand Down
3 changes: 3 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 @@ -98,6 +98,9 @@ object UIUtils {
Backend.END -> {
R.string.lbl_stopped
}
Backend.TNT -> {
R.string.status_waiting
}
else -> {
R.string.rt_filter_parent_selected
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.celzero.bravedns"
android:versionCode="38"
android:versionName="v055g"> <!-- 38(v055g) -->
android:versionCode="39"
android:versionName="v055h"> <!-- 39(v055h) -->

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
Expand Down
Loading

0 comments on commit 464c3b1

Please sign in to comment.