Skip to content

Commit

Permalink
Merge pull request #1459 from hussainmohd-a/v055k
Browse files Browse the repository at this point in the history
V055k
  • Loading branch information
hussainmohd-a authored May 24, 2024
2 parents c663833 + f73100e commit 7bd0fbb
Show file tree
Hide file tree
Showing 25 changed files with 368 additions and 126 deletions.
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ dependencies {
fullImplementation 'com.github.kirich1409:viewbindingpropertydelegate-noreflection:1.5.9'

// from: https://jitpack.io/#celzero/firestack
download 'com.github.celzero:firestack:d92f398622@aar'
implementation 'com.github.celzero:firestack:d92f398622@aar'
download 'com.github.celzero:firestack:177d6a51d8@aar'
implementation 'com.github.celzero:firestack:177d6a51d8@aar'

// Work manager
implementation('androidx.work:work-runtime-ktx:2.9.0') {
Expand Down Expand Up @@ -289,6 +289,8 @@ dependencies {
// barcode scanner for wireguard
fullImplementation 'com.journeyapps:zxing-android-embedded:4.3.0'

// only using firebase crashlytics experimentally for stability tracking, only in play variant
// not in fdroid or website
playImplementation 'com.google.firebase:firebase-crashlytics:19.0.0'
playImplementation 'com.google.firebase:firebase-crashlytics-ndk:19.0.0'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
}

companion object {
private const val ONE_SEC = 1000L
private const val ONE_SEC = 1500L

private val DIFF_CALLBACK =
object : DiffUtil.ItemCallback<WgConfigFiles>() {
Expand Down Expand Up @@ -192,7 +192,7 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
b.oneWgCheck.isChecked = true
b.interfaceAppsCount.visibility = View.VISIBLE
b.interfaceAppsCount.text = context.getString(R.string.one_wg_apps_added)
val status: String
var status: String
val handShakeTime = getHandshakeTime(stats)
if (statusId != null) {
var resId = UIUtils.getProxyStatusStringRes(statusId)
Expand All @@ -206,8 +206,6 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
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) {
b.interfaceDetailCard.strokeColor =
fetchColor(context, R.attr.chipTextNeutral)
Expand All @@ -225,6 +223,20 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
handShakeTime
)
}

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 =
context
.getString(R.string.dns_connected)
.replaceFirstChar(Char::titlecase)
}
}
} else {
b.interfaceDetailCard.strokeColor = fetchColor(context, R.attr.chipTextNegative)
b.interfaceDetailCard.strokeWidth = 2
Expand Down Expand Up @@ -328,10 +340,12 @@ class OneWgConfigAdapter(private val context: Context, private val listener: Dns
}
} else {
config.oneWireGuard = false
b.oneWgCheck.isChecked = false
WireguardManager.updateOneWireGuardConfig(config.id, owg = false)
WireguardManager.disableConfig(config.toImmutable())
uiCtx { listener.onDnsStatusChanged() }
uiCtx {
b.oneWgCheck.isChecked = false
listener.onDnsStatusChanged()
}
}
}
}
Expand Down
18 changes: 15 additions & 3 deletions app/src/full/java/com/celzero/bravedns/adapter/WgConfigAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class WgConfigAdapter(private val context: Context) :
private var lifecycleOwner: LifecycleOwner? = null

companion object {
private const val ONE_SEC_MS = 1000L
private const val ONE_SEC_MS = 1500L
private val DIFF_CALLBACK =
object : DiffUtil.ItemCallback<WgConfigFiles>() {

Expand Down Expand Up @@ -280,7 +280,7 @@ class WgConfigAdapter(private val context: Context) :
b.interfaceDetailCard.strokeWidth = 2
b.interfaceStatus.visibility = View.VISIBLE
b.interfaceConfigStatus.visibility = View.VISIBLE
val status: String
var status: String
b.interfaceActiveLayout.visibility = View.VISIBLE
val time = getUpTime(stats)
val rxtx = getRxTx(stats)
Expand Down Expand Up @@ -311,7 +311,6 @@ class WgConfigAdapter(private val context: Context) :
b.interfaceDetailCard.strokeColor =
UIUtils.fetchColor(context, R.attr.accentGood)
}
cancelJobIfAny(config.id)
} else if (
statusId == Backend.TUP ||
statusId == Backend.TZZ ||
Expand All @@ -333,6 +332,19 @@ class WgConfigAdapter(private val context: Context) :
handShakeTime
)
}
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 =
context
.getString(R.string.dns_connected)
.replaceFirstChar(Char::titlecase)
}
}
} else {
b.interfaceDetailCard.strokeColor =
UIUtils.fetchColor(context, R.attr.accentBad)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class LocalBlocklistCoordinator(val context: Context, workerParams: WorkerParame
val elapsedMs = SystemClock.elapsedRealtime() - startMs
downloadedMB += bytesToMB(bytesRead)
val progress =
if (contentLength == Long.MAX_VALUE) 0
if (contentLength == Long.MAX_VALUE || expectedMB == 0.0) 0
else (downloadedMB * 100 / expectedMB).toInt()
if (elapsedMs >= progressJumpsMs) {
updateProgress(context, progress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class AppInfoActivity : AppCompatActivity(R.layout.activity_app_details) {
}

private fun init() {
val rethinkPkgName = this.packageName
io {
val appInfo = FirewallManager.getAppInfoByUid(uid)
// case: app is uninstalled but still available in RethinkDNS database
Expand All @@ -127,7 +128,7 @@ class AppInfoActivity : AppCompatActivity(R.layout.activity_app_details) {
)

// do not show the firewall status if the app is Rethink
if (appInfo.packageName == this.packageName) {
if (appInfo.packageName == rethinkPkgName) {
b.aadFirewallStatus.visibility = View.GONE
hideFirewallStatusUi()
hideDomainBlockUi()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ class MiscSettingsActivity : AppCompatActivity(R.layout.activity_misc_settings)
}

private fun invokeImportExport() {
if (this.isFinishing || this.isDestroyed) {
Logger.w(LOG_TAG_UI, "err opening bkup btmsheet, activity is destroyed")
return
}

val bottomSheetFragment = BackupRestoreBottomSheet()
bottomSheetFragment.show(this.supportFragmentManager, bottomSheetFragment.tag)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ import com.celzero.bravedns.R
import com.celzero.bravedns.adapter.AppWiseIpsAdapter
import com.celzero.bravedns.adapter.DomainRulesBtmSheetAdapter
import com.celzero.bravedns.databinding.BottomSheetAppConnectionsBinding
import com.celzero.bravedns.service.FirewallManager
import com.celzero.bravedns.service.IpRulesManager
import com.celzero.bravedns.service.PersistentState
import com.celzero.bravedns.util.Constants.Companion.INVALID_UID
import com.celzero.bravedns.util.CustomLinearLayoutManager
import com.celzero.bravedns.util.Themes.Companion.getBottomsheetCurrentTheme
import com.celzero.bravedns.util.UIUtils.updateHtmlEncodedText
import com.celzero.bravedns.util.Utilities
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -115,6 +117,8 @@ class AppIpRulesBottomSheet : BottomSheetDialogFragment() {
this.dismiss()
return
}

updateAppDetails()
b.bsacIpAddressTv.text = ipAddress

b.bsacIpRuleTxt.text = updateHtmlEncodedText(getString(R.string.bsct_block_ip))
Expand All @@ -123,6 +127,46 @@ class AppIpRulesBottomSheet : BottomSheetDialogFragment() {
setupRecycler()
}

private fun updateAppDetails() {
if (uid == -1) return

io {
val appNames = FirewallManager.getAppNamesByUid(uid)
if (appNames.isEmpty()) {
uiCtx { handleNonApp() }
return@io
}
val pkgName = FirewallManager.getPackageNameByAppName(appNames[0])

val appCount = appNames.count()
uiCtx {
if (appCount >= 1) {
b.bsacAppName.text =
if (appCount >= 2) {
getString(
R.string.ctbs_app_other_apps,
appNames[0],
appCount.minus(1).toString()
)
} else {
appNames[0]
}
if (pkgName == null) return@uiCtx
b.bsacAppIcon.setImageDrawable(Utilities.getIcon(requireContext(), pkgName))
} else {
// apps which are not available in cache are treated as non app.
// TODO: check packageManager#getApplicationInfo() for appInfo
handleNonApp()
}
}
}
}

private fun handleNonApp() {
b.bsacAppName.visibility = View.GONE
b.bsacAppIcon.visibility = View.GONE
}

private fun setupRecycler() {
if (domains.isEmpty()) {
b.bsacDomainLl.visibility = View.GONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ import com.celzero.bravedns.util.Utilities
import com.celzero.bravedns.util.Utilities.delay
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.koin.android.ext.android.inject
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.concurrent.TimeUnit
import org.koin.android.ext.android.inject

class BackupRestoreBottomSheet : BottomSheetDialogFragment() {
private var _binding: ActivityBackupRestoreBinding? = null
Expand Down Expand Up @@ -204,13 +204,22 @@ class BackupRestoreBottomSheet : BottomSheetDialogFragment() {
}

private fun restore() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "*/*"
val mimeTypes = arrayOf(INTENT_TYPE_OCTET, INTENT_TYPE_XZIP)
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)

restoreActivityResult.launch(intent)
try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "*/*"
val mimeTypes = arrayOf(INTENT_TYPE_OCTET, INTENT_TYPE_XZIP)
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)

restoreActivityResult.launch(intent)
} catch (e: Exception) {
Logger.e(LOG_TAG_BACKUP_RESTORE, "err opening file picker: ${e.message}")
Utilities.showToastUiCentered(
requireContext(),
getString(R.string.blocklist_update_check_failure),
Toast.LENGTH_SHORT
)
}
}

private fun result() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,16 @@ class HomeScreenFragment : Fragment(R.layout.fragment_home_screen) {
builder.setMessage(R.string.hsf_vpn_dialog_message)
builder.setCancelable(false)
builder.setPositiveButton(R.string.lbl_proceed) { _, _ ->
startForResult.launch(prepareVpnIntent)
try {
startForResult.launch(prepareVpnIntent)
} catch (e: ActivityNotFoundException) {
Logger.e(LOG_TAG_VPN, "Activity not found to start VPN service", e)
showToastUiCentered(
requireContext(),
getString(R.string.hsf_vpn_prepare_failure),
Toast.LENGTH_LONG
)
}
}

builder.setNegativeButton(R.string.lbl_cancel) { _, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ class SummaryStatisticsFragment : Fragment(R.layout.fragment_summary_statistics)
}

private fun calculatePercentage(value: Long, maxValue: Long): Int {
if (maxValue == 0L) return 0

return (value * 100 / maxValue).toInt()
}

Expand Down
1 change: 1 addition & 0 deletions app/src/full/res/layout/bottom_sheet_app_connections.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/trust_icon"
android:textIsSelectable="true"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textColor="?attr/primaryTextColor"
Expand Down
1 change: 1 addition & 0 deletions app/src/full/res/layout/domain_item_bottom_sheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textColor="?attr/primaryTextColor"
android:textIsSelectable="true"
android:textSize="@dimen/large_font_text_view" />

<androidx.appcompat.widget.AppCompatImageView
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/celzero/bravedns/data/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal constructor(

private const val ORBOT_DNS = "Orbot"

const val FALLBACK_DNS = "8.8.4.4, 2001:4860:4860::8844"
const val FALLBACK_DNS = "8.8.4.4,2001:4860:4860::8844"
}

init {
Expand Down
Loading

0 comments on commit 7bd0fbb

Please sign in to comment.