Skip to content

Commit

Permalink
Merge pull request #742 from hussainmohd-a/v053m
Browse files Browse the repository at this point in the history
v053n
  • Loading branch information
hussainmohd-a authored Jan 12, 2023
2 parents b10f667 + 547e0e2 commit 62e9bbb
Show file tree
Hide file tree
Showing 20 changed files with 293 additions and 284 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ android {
applicationId "com.celzero.bravedns"
minSdkVersion 23
targetSdkVersion 33
versionCode 24 // for version name 053m
versionName "053m"
versionCode 25 // for version name 053n
versionName "053n"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ class AppConnectionAdapter(val context: Context, val uid: Int) :
null // don't check for port as adding rule from this screen port is null
)
updateStatusUi(ipRuleStatus)
if (!appConnection.dnsQuery.isNullOrEmpty()) {
if (!appConnection.appOrDnsName.isNullOrEmpty()) {
b.acdDomainName.visibility = View.VISIBLE
b.acdDomainName.text = beautifyDomainString(appConnection.dnsQuery)
b.acdDomainName.text = beautifyDomainString(appConnection.appOrDnsName)
} else {
b.acdDomainName.visibility = View.GONE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,15 @@ class SummaryStatisticsAdapter(
}
SummaryStatisticsFragment.SummaryStatisticsType.MOST_CONTACTED_DOMAINS -> {
itemBinding.ssFlag.text = appConnection.flag
val query = appConnection.dnsQuery?.dropLast(1)
val query = appConnection.appOrDnsName?.dropLast(1)
if (query == null) {
hideFavIcon()
showFlag()
return
}

// no need to check in glide cache if the value is available in failed cache
if (
FavIconDownloader.isUrlAvailableInFailedCache(query) !=
null
) {
if (FavIconDownloader.isUrlAvailableInFailedCache(query) != null) {
hideFavIcon()
showFlag()
} else {
Expand Down Expand Up @@ -199,20 +196,18 @@ class SummaryStatisticsAdapter(
private fun setName(appConnection: AppConnection) {
when (type) {
SummaryStatisticsFragment.SummaryStatisticsType.MOST_CONNECTED_APPS -> {
val appInfo = FirewallManager.getAppInfoByUid(appConnection.uid)
itemBinding.ssName.text = appInfo?.appName
itemBinding.ssName.text = getAppName(appConnection)
}
SummaryStatisticsFragment.SummaryStatisticsType.MOST_BLOCKED_APPS -> {
val appInfo = FirewallManager.getAppInfoByUid(appConnection.uid)
itemBinding.ssName.text = appInfo?.appName
itemBinding.ssName.text = getAppName(appConnection)
}
SummaryStatisticsFragment.SummaryStatisticsType.MOST_CONTACTED_DOMAINS -> {
// remove the trailing dot
itemBinding.ssName.text = appConnection.dnsQuery?.dropLast(1)
itemBinding.ssName.text = appConnection.appOrDnsName?.dropLast(1)
}
SummaryStatisticsFragment.SummaryStatisticsType.MOST_BLOCKED_DOMAINS -> {
// remove the trailing dot
itemBinding.ssName.text = appConnection.dnsQuery?.dropLast(1)
itemBinding.ssName.text = appConnection.appOrDnsName?.dropLast(1)
}
SummaryStatisticsFragment.SummaryStatisticsType.MOST_CONTACTED_IPS -> {
itemBinding.ssName.text = appConnection.ipAddress
Expand All @@ -223,6 +218,22 @@ class SummaryStatisticsAdapter(
}
}

private fun getAppName(appConnection: AppConnection): String? {
return if (appConnection.appOrDnsName.isNullOrEmpty()) {
val appInfo = FirewallManager.getAppInfoByUid(appConnection.uid)
if (appInfo?.appName.isNullOrEmpty()) {
context.getString(
R.string.network_log_app_name_unnamed,
"($appConnection.uid)"
)
} else {
appInfo?.appName
}
} else {
appConnection.appOrDnsName
}
}

private fun setProgress(count: Int, isBlocked: Boolean = false) {
val percentage = calculatePercentage(count)
if (isBlocked) {
Expand Down Expand Up @@ -286,7 +297,7 @@ class SummaryStatisticsAdapter(
startActivity(
isDns = true,
DnsDetailActivity.Tabs.LOGS.screen,
appConnection.dnsQuery
appConnection.appOrDnsName
)
} else {
Utilities.showToastUiCentered(
Expand Down
11 changes: 6 additions & 5 deletions app/src/full/java/com/celzero/bravedns/ui/DnsDetailActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class DnsDetailActivity : AppCompatActivity(R.layout.activity_dns_detail) {
private val persistentState by inject<PersistentState>()

enum class Tabs(val screen: Int) {
LOGS(0),
CONFIGURE(1);
LOGS(1),
CONFIGURE(0);

companion object {
fun getCount(): Int {
Expand All @@ -51,7 +51,8 @@ class DnsDetailActivity : AppCompatActivity(R.layout.activity_dns_detail) {
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(getCurrentTheme(isDarkThemeOn(), persistentState.theme))
super.onCreate(savedInstanceState)
fragmentIndex = intent.getIntExtra(Constants.VIEW_PAGER_SCREEN_TO_LOAD, 0)
fragmentIndex =
intent.getIntExtra(Constants.VIEW_PAGER_SCREEN_TO_LOAD, Tabs.CONFIGURE.screen)
searchParam = intent.getStringExtra(Constants.SEARCH_QUERY) ?: ""
init()
}
Expand All @@ -62,8 +63,8 @@ class DnsDetailActivity : AppCompatActivity(R.layout.activity_dns_detail) {
object : FragmentStateAdapter(this) {
override fun createFragment(position: Int): Fragment {
return when (position) {
Tabs.LOGS.screen -> DnsLogFragment.newInstance(searchParam)
Tabs.CONFIGURE.screen -> DnsConfigureFragment.newInstance()
Tabs.LOGS.screen -> DnsLogFragment.newInstance(searchParam)
else -> DnsConfigureFragment.newInstance()
}
}
Expand All @@ -76,8 +77,8 @@ class DnsDetailActivity : AppCompatActivity(R.layout.activity_dns_detail) {
TabLayoutMediator(b.dnsDetailActTabLayout, b.dnsDetailActViewpager) { tab, position ->
tab.text =
when (position) {
Tabs.LOGS.screen -> getString(R.string.dns_act_log)
Tabs.CONFIGURE.screen -> getString(R.string.dns_act_configure_tab)
Tabs.LOGS.screen -> getString(R.string.dns_act_log)
else -> getString(R.string.dns_act_configure_tab)
}
}
Expand Down
14 changes: 13 additions & 1 deletion app/src/full/java/com/celzero/bravedns/ui/HomeScreenActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.celzero.bravedns.ui

import android.app.UiModeManager
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -103,7 +104,8 @@ class HomeScreenActivity : AppCompatActivity(R.layout.activity_home_screen) {
setTheme(getCurrentTheme(isDarkThemeOn(), persistentState.theme))
super.onCreate(savedInstanceState)

if (persistentState.firstTimeLaunch) {
// do not launch on board activity when app is running on TV
if (persistentState.firstTimeLaunch && !isAppRunningOnTv()) {
launchOnboardActivity()
return
}
Expand Down Expand Up @@ -135,6 +137,16 @@ class HomeScreenActivity : AppCompatActivity(R.layout.activity_home_screen) {
}
}

// check if app running on TV
private fun isAppRunningOnTv(): Boolean {
return try {
val uiModeManager: UiModeManager = getSystemService(UI_MODE_SERVICE) as UiModeManager
uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION
} catch (ignored: Exception) {
false
}
}

private fun biometricPrompt() {
// ref: https://developer.android.com/training/sign-in/biometric-auth
executor = ContextCompat.getMainExecutor(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,11 +973,6 @@ class HomeScreenFragment : Fragment(R.layout.fragment_home_screen) {
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
if (it) {
Log.i(LoggerConstants.LOG_TAG_UI, "User accepted notification permission")
showToastUiCentered(
requireContext(),
"User accepted the permission",
Toast.LENGTH_SHORT
)
} else {
persistentState.shouldRequestNotificationPermission = false
Log.w(LoggerConstants.LOG_TAG_UI, "User rejected notification permission")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ class HomeScreenSettingBottomSheet : BottomSheetDialogFragment() {
private fun updateUptime() {
val uptimeMs = VpnController.uptimeMs()
val protocols = VpnController.protocols()
val netType = VpnController.netType()
val now = System.currentTimeMillis()
// returns a string describing 'time' as a time relative to 'now'
val t =
Expand All @@ -228,7 +229,7 @@ class HomeScreenSettingBottomSheet : BottomSheetDialogFragment() {
getString(R.string.hsf_downtime, t)
} else {
b.bsHomeScreenAppUptime.visibility = View.VISIBLE
getString(R.string.hsf_uptime, t, protocols)
getString(R.string.hsf_uptime, t, protocols, netType)
}
}

Expand Down
5 changes: 2 additions & 3 deletions app/src/full/java/com/celzero/bravedns/ui/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ class SettingsFragment : Fragment(R.layout.fragment_settings_screen) {
}
// for app locale (default system/user selected locale)
if (isAtleastT()) {
val currentAppLocales: LocaleList =
requireContext().getSystemService(LocaleManager::class.java).applicationLocales
b.settingsLocaleDesc.text = currentAppLocales[0].displayName
val currentAppLocales: LocaleList = requireContext().getSystemService(LocaleManager::class.java).applicationLocales
b.settingsLocaleDesc.text = currentAppLocales[0]?.displayName ?: getString(R.string.settings_locale_desc)
} else {
b.settingsLocaleDesc.text =
AppCompatDelegate.getApplicationLocales().get(0)?.displayName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class SummaryStatisticsViewModel(
val getAllowedAppNetworkActivity =
Transformations.switchMap(networkActivity) { _ ->
Pager(PagingConfig(Constants.LIVEDATA_PAGE_SIZE)) {
// use dnsQuery as appName
connectionTrackerDAO.getAllowedAppNetworkActivity()
}
.liveData
Expand All @@ -53,6 +54,7 @@ class SummaryStatisticsViewModel(
val getBlockedAppNetworkActivity =
Transformations.switchMap(networkActivity) { _ ->
Pager(PagingConfig(Constants.LIVEDATA_PAGE_SIZE)) {
// use dnsQuery as appName
connectionTrackerDAO.getBlockedAppNetworkActivity()
}
.liveData
Expand Down
2 changes: 1 addition & 1 deletion app/src/full/res/layout/bottom_sheet_home_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
android:alpha="0.75"
android:gravity="center"
android:textColor="?attr/primaryTextColor"
android:textSize="@dimen/default_font_text_view" />
android:textSize="@dimen/small_font_text_view" />

</RelativeLayout>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ data class AppConnection(
val count: Int,
val flag: String,
val blocked: Boolean,
val dnsQuery: String?
val appOrDnsName: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ interface ConnectionTrackerDAO {
fun getBlockedConnections(query: String): PagingSource<Int, ConnectionTracker>

@Query(
"select uid as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 0 as blocked, GROUP_CONCAT(DISTINCT dnsQuery) as dnsQuery from ConnectionTracker where uid = :uid group by ipAddress order by count desc"
"select uid as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 0 as blocked, GROUP_CONCAT(DISTINCT dnsQuery) as appOrDnsName from ConnectionTracker where uid = :uid group by ipAddress order by count desc"
)
fun getLogsForApp(uid: Int): PagingSource<Int, AppConnection>

@Query(
"select uid as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 0 as blocked, GROUP_CONCAT(DISTINCT dnsQuery) as dnsQuery from ConnectionTracker where uid = :uid and ipAddress like :ipAddress group by ipAddress order by count desc"
"select uid as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 0 as blocked, GROUP_CONCAT(DISTINCT dnsQuery) as appOrDnsName from ConnectionTracker where uid = :uid and ipAddress like :ipAddress group by ipAddress order by count desc"
)
fun getLogsForAppFiltered(uid: Int, ipAddress: String): PagingSource<Int, AppConnection>

Expand Down Expand Up @@ -84,42 +84,42 @@ interface ConnectionTrackerDAO {
): PagingSource<Int, ConnectionTracker>

@Query(
"select uid as uid, '' as ipAddress, 0 as port, count(uid) as count, 0 as flag, 0 as blocked, '' as dnsQuery from ConnectionTracker where isBlocked = 0 group by uid order by count desc LIMIT 7"
"select uid as uid, '' as ipAddress, 0 as port, count(uid) as count, 0 as flag, 0 as blocked, appName as appOrDnsName from ConnectionTracker where isBlocked = 0 group by uid, appName order by count desc LIMIT 7"
)
fun getAllowedAppNetworkActivity(): PagingSource<Int, AppConnection>

@Query(
"select uid as uid, '' as ipAddress, 0 as port, count(uid) as count, 0 as flag, 0 as blocked, '' as dnsQuery from ConnectionTracker where isBlocked = 0 group by uid order by count desc"
"select uid as uid, '' as ipAddress, 0 as port, count(uid) as count, 0 as flag, 0 as blocked, appName as appOrDnsName from ConnectionTracker where isBlocked = 0 group by uid, appName order by count desc"
)
fun getAllAllowedAppNetworkActivity(): PagingSource<Int, AppConnection>

@Query(
"select uid as uid, '' as ipAddress, 0 as port, count(uid) as count, 0 as flag, 1 as blocked, '' as dnsQuery from ConnectionTracker where isBlocked = 1 group by uid order by count desc LIMIT 7"
"select uid as uid, '' as ipAddress, 0 as port, count(uid) as count, 0 as flag, 1 as blocked, appName as appOrDnsName from ConnectionTracker where isBlocked = 1 group by uid, appName order by count desc LIMIT 7"
)
fun getBlockedAppNetworkActivity(): PagingSource<Int, AppConnection>

@Query(
"select uid as uid, '' as ipAddress, 0 as port, count(uid) as count, 0 as flag, 1 as blocked, '' as dnsQuery from ConnectionTracker where isBlocked = 1 group by uid order by count desc"
"select uid as uid, '' as ipAddress, 0 as port, count(uid) as count, 0 as flag, 1 as blocked, appName as appOrDnsName from ConnectionTracker where isBlocked = 1 group by uid, appName order by count desc"
)
fun getAllBlockedAppNetworkActivity(): PagingSource<Int, AppConnection>

@Query(
"select 0 as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 0 as blocked, '' as dnsQuery from ConnectionTracker where isBlocked = 0 group by ipAddress, flag order by count desc LIMIT 7"
"select 0 as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 0 as blocked, '' as appOrDnsName from ConnectionTracker where isBlocked = 0 group by ipAddress order by count desc LIMIT 7"
)
fun getMostContactedIps(): PagingSource<Int, AppConnection>

@Query(
"select 0 as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 0 as blocked, '' as dnsQuery from ConnectionTracker where isBlocked = 0 group by ipAddress, flag order by count desc"
"select 0 as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 0 as blocked, '' as appOrDnsName from ConnectionTracker where isBlocked = 0 group by ipAddress order by count desc"
)
fun getAllContactedIps(): PagingSource<Int, AppConnection>

@Query(
"select 0 as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 1 as blocked, '' as dnsQuery from ConnectionTracker where isBlocked = 1 group by ipAddress, flag order by count desc LIMIT 7"
"select 0 as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 1 as blocked, '' as appOrDnsName from ConnectionTracker where isBlocked = 1 group by ipAddress order by count desc LIMIT 7"
)
fun getMostBlockedIps(): PagingSource<Int, AppConnection>

@Query(
"select 0 as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 1 as blocked, '' as dnsQuery from ConnectionTracker where isBlocked = 1 group by ipAddress, flag order by count desc"
"select 0 as uid, ipAddress as ipAddress, port as port, count(ipAddress) as count, flag, 1 as blocked, '' as appOrDnsName from ConnectionTracker where isBlocked = 1 group by ipAddress order by count desc"
)
fun getAllBlockedIps(): PagingSource<Int, AppConnection>

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/celzero/bravedns/database/DnsLogDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ interface DnsLogDAO {
@Query("delete from DNSLogs where time < :date") fun purgeDnsLogsByDate(date: Long)

@Query(
"select 0 as uid, '' as ipAddress, 0 as port, count(queryStr) as count, flag, 0 as blocked, queryStr as dnsQuery from DNSLogs where isBlocked = 0 and status = 'COMPLETE' and response != 'NXDOMAIN' group by queryStr order by count desc LIMIT 7"
"select 0 as uid, '' as ipAddress, 0 as port, count(queryStr) as count, flag, 0 as blocked, queryStr as appOrDnsName from DNSLogs where isBlocked = 0 and status = 'COMPLETE' and response != 'NXDOMAIN' group by queryStr order by count desc LIMIT 7"
)
fun getMostContactedDomains(): PagingSource<Int, AppConnection>

@Query(
"select 0 as uid, '' as ipAddress, 0 as port, count(queryStr) as count, flag, 0 as blocked, queryStr as dnsQuery from DNSLogs where isBlocked = 0 and status = 'COMPLETE' and response != 'NXDOMAIN' group by queryStr order by count desc"
"select 0 as uid, '' as ipAddress, 0 as port, count(queryStr) as count, flag, 0 as blocked, queryStr as appOrDnsName from DNSLogs where isBlocked = 0 and status = 'COMPLETE' and response != 'NXDOMAIN' group by queryStr order by count desc"
)
fun getAllContactedDomains(): PagingSource<Int, AppConnection>

@Query(
"select 0 as uid, '' as ipAddress, 0 as port, count(queryStr) as count, flag, 1 as blocked, queryStr as dnsQuery from DNSLogs where isBlocked = 1 group by queryStr order by count desc LIMIT 7"
"select 0 as uid, '' as ipAddress, 0 as port, count(queryStr) as count, flag, 1 as blocked, queryStr as appOrDnsName from DNSLogs where isBlocked = 1 group by queryStr order by count desc LIMIT 7"
)
fun getMostBlockedDomains(): PagingSource<Int, AppConnection>

@Query(
"select 0 as uid, '' as ipAddress, 0 as port, count(queryStr) as count, flag, 1 as blocked, queryStr as dnsQuery from DNSLogs where isBlocked = 1 group by queryStr order by count desc"
"select 0 as uid, '' as ipAddress, 0 as port, count(queryStr) as count, flag, 1 as blocked, queryStr as appOrDnsName from DNSLogs where isBlocked = 1 group by queryStr order by count desc"
)
fun getAllBlockedDomains(): PagingSource<Int, AppConnection>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class ConnectionMonitor(context: Context, networkListener: NetworkListener) :
val ipv4Net: Set<Network>,
val ipv6Net: Set<Network>,
val useActive: Boolean,
var `isActiveNetworkMetered`: Boolean,
var isActiveNetworkMetered: Boolean,
var lastUpdated: Long
)

Expand Down
Loading

0 comments on commit 62e9bbb

Please sign in to comment.