Skip to content

Commit 6a62a4f

Browse files
Merge pull request #2410 from hussainmohd-a/main
Multiple fixes
2 parents bbf6752 + 09f6e76 commit 6a62a4f

File tree

12 files changed

+232
-75
lines changed

12 files changed

+232
-75
lines changed

app/src/full/java/com/celzero/bravedns/ui/activity/MiscSettingsActivity.kt

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Logger.LOG_TAG_UI
2121
import Logger.LOG_TAG_VPN
2222
import Logger.updateConfigLevel
2323
import android.Manifest
24+
import android.R.attr.theme
2425
import android.app.LocaleManager
2526
import android.content.ActivityNotFoundException
2627
import android.content.Context
@@ -88,10 +89,12 @@ import com.celzero.bravedns.util.Utilities.isAtleastT
8889
import com.celzero.bravedns.util.Utilities.isFdroidFlavour
8990
import com.celzero.bravedns.util.Utilities.showToastUiCentered
9091
import com.celzero.bravedns.util.handleFrostEffectIfNeeded
92+
import com.google.android.gms.common.wrappers.Wrappers.packageManager
9193
import com.google.android.material.dialog.MaterialAlertDialogBuilder
9294
import kotlinx.coroutines.Dispatchers
9395
import kotlinx.coroutines.launch
9496
import org.koin.android.ext.android.inject
97+
import org.koin.java.KoinJavaComponent.inject
9598
import org.xmlpull.v1.XmlPullParser
9699
import org.xmlpull.v1.XmlPullParserException
97100
import java.io.File
@@ -429,11 +432,40 @@ class MiscSettingsActivity : AppCompatActivity(R.layout.activity_misc_settings)
429432
intent.action = Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
430433
val uri = Uri.fromParts(SCHEME_PACKAGE, this.packageName, null)
431434
intent.data = uri
432-
storageActivityResultLauncher.launch(intent)
433-
} catch (_: Exception) {
434-
val intent = Intent()
435-
intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
436-
storageActivityResultLauncher.launch(intent)
435+
436+
// Check if there's an activity that can handle this intent
437+
if (intent.resolveActivity(packageManager) != null) {
438+
storageActivityResultLauncher.launch(intent)
439+
} else {
440+
// Fallback to general settings
441+
throw ActivityNotFoundException("No activity found for app-specific storage settings")
442+
}
443+
} catch (e: Exception) {
444+
Logger.e(LOG_TAG_VPN, "Error launching app-specific storage settings: ${e.message}")
445+
try {
446+
val intent = Intent()
447+
intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
448+
449+
// Check if there's an activity that can handle the fallback intent
450+
if (intent.resolveActivity(packageManager) != null) {
451+
storageActivityResultLauncher.launch(intent)
452+
} else {
453+
// No activity available to handle storage permission
454+
Logger.e(LOG_TAG_VPN, "No activity found to handle storage permission request")
455+
showToastUiCentered(
456+
this,
457+
getString(R.string.pcap_failure_toast),
458+
Toast.LENGTH_LONG
459+
)
460+
}
461+
} catch (fallbackException: Exception) {
462+
Logger.e(LOG_TAG_VPN, "Error launching storage settings: ${fallbackException.message}", fallbackException)
463+
showToastUiCentered(
464+
this,
465+
getString(R.string.pcap_failure_toast),
466+
Toast.LENGTH_LONG
467+
)
468+
}
437469
}
438470
} else {
439471
// below version 11
@@ -1275,7 +1307,33 @@ class MiscSettingsActivity : AppCompatActivity(R.layout.activity_misc_settings)
12751307
return
12761308
}
12771309

1278-
notificationPermissionResult.launch(Manifest.permission.POST_NOTIFICATIONS)
1310+
try {
1311+
notificationPermissionResult.launch(Manifest.permission.POST_NOTIFICATIONS)
1312+
} catch (e: ActivityNotFoundException) {
1313+
Logger.e(
1314+
LOG_TAG_VPN,
1315+
"ActivityNotFoundException while requesting notification permission: ${e.message}",
1316+
e
1317+
)
1318+
showToastUiCentered(
1319+
this,
1320+
getString(R.string.blocklist_update_check_failure),
1321+
Toast.LENGTH_SHORT
1322+
)
1323+
// Fallback: try opening notification settings directly
1324+
invokeAndroidNotificationSetting()
1325+
} catch (e: Exception) {
1326+
Logger.e(
1327+
LOG_TAG_VPN,
1328+
"Exception while requesting notification permission: ${e.message}",
1329+
e
1330+
)
1331+
showToastUiCentered(
1332+
this,
1333+
getString(R.string.blocklist_update_check_failure),
1334+
Toast.LENGTH_SHORT
1335+
)
1336+
}
12791337
}
12801338

12811339
private fun invokeAndroidNotificationSetting() {

app/src/full/java/com/celzero/bravedns/ui/activity/ProxySettingsActivity.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ class ProxySettingsActivity : AppCompatActivity(R.layout.fragment_proxy_configur
252252
}
253253
io {
254254
val endpoint = appConfig.getHttpProxyDetails()
255+
if (endpoint == null) {
256+
uiCtx {
257+
showToastUiCentered(this, getString(R.string.blocklist_update_check_failure), Toast.LENGTH_SHORT)
258+
}
259+
return@io
260+
}
255261
val packageName = endpoint.proxyAppName
256262
val app = FirewallManager.getAppInfoByPackage(packageName)
257263
val m = ProxyManager.ProxyMode.get(endpoint.proxyMode)
@@ -388,6 +394,12 @@ class ProxySettingsActivity : AppCompatActivity(R.layout.fragment_proxy_configur
388394

389395
io {
390396
val endpoint = appConfig.getHttpProxyDetails()
397+
if (endpoint == null) {
398+
uiCtx {
399+
showToastUiCentered(this, getString(R.string.blocklist_update_check_failure), Toast.LENGTH_SHORT)
400+
}
401+
return@io
402+
}
391403
val m = ProxyManager.ProxyMode.get(endpoint.proxyMode) ?: return@io
392404

393405
// only update below ui if its custom http proxy

app/src/full/java/com/celzero/bravedns/ui/activity/WgMainActivity.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package com.celzero.bravedns.ui.activity
1717

1818
import Logger
1919
import Logger.LOG_TAG_PROXY
20+
import android.content.ActivityNotFoundException
2021
import android.content.Context
2122
import android.content.Intent
2223
import android.content.res.ColorStateList
@@ -331,15 +332,31 @@ class WgMainActivity :
331332
}
332333
}
333334
b.importFab.setOnClickListener {
334-
tunnelFileImportResultLauncher.launch(IMPORT_LAUNCH_INPUT)
335+
try {
336+
tunnelFileImportResultLauncher.launch(IMPORT_LAUNCH_INPUT)
337+
} catch (e: ActivityNotFoundException) {
338+
Logger.e(LOG_TAG_PROXY, "err; anf; while launching file import: ${e.message}", e)
339+
Utilities.showToastUiCentered(this, getString(R.string.blocklist_update_check_failure), Toast.LENGTH_SHORT)
340+
} catch (e: Exception) {
341+
Logger.e(LOG_TAG_PROXY, "err while launching file import: ${e.message}", e)
342+
Utilities.showToastUiCentered(this, getString(R.string.blocklist_update_check_failure), Toast.LENGTH_SHORT)
343+
}
335344
}
336345
b.qrCodeFab.setOnClickListener {
337-
qrImportResultLauncher.launch(
338-
ScanOptions()
339-
.setOrientationLocked(false)
340-
.setBeepEnabled(false)
341-
.setPrompt(resources.getString(R.string.lbl_qr_code))
342-
)
346+
try {
347+
qrImportResultLauncher.launch(
348+
ScanOptions()
349+
.setOrientationLocked(false)
350+
.setBeepEnabled(false)
351+
.setPrompt(resources.getString(R.string.lbl_qr_code))
352+
)
353+
} catch (e: ActivityNotFoundException) {
354+
Logger.e(LOG_TAG_PROXY, "err; anf while launching QR scanner: ${e.message}", e)
355+
Utilities.showToastUiCentered(this, getString(R.string.blocklist_update_check_failure), Toast.LENGTH_SHORT)
356+
} catch (e: Exception) {
357+
Logger.e(LOG_TAG_PROXY, "err while launching QR scanner: ${e.message}", e)
358+
Utilities.showToastUiCentered(this, getString(R.string.blocklist_update_check_failure), Toast.LENGTH_SHORT)
359+
}
343360
}
344361
b.createFab.setOnClickListener { openTunnelEditorActivity() }
345362

app/src/full/java/com/celzero/bravedns/ui/bottomsheet/BackupRestoreBottomSheet.kt

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,37 @@ class BackupRestoreBottomSheet : BottomSheetDialogFragment() {
150150
}
151151

152152
private fun backup() {
153-
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
154-
// have complete access to the physical location returned as part of result
155-
intent.addCategory(Intent.CATEGORY_OPENABLE)
156-
intent.type = INTENT_TYPE_OCTET
157-
val sdf = SimpleDateFormat(BACKUP_FILE_NAME_DATETIME, Locale.ROOT)
158-
// filename format (Rethink_version_DATA_FORMAT.bk)
159-
val version = getVersionName().replace(' ', '_')
160-
val zipFileName: String = BACKUP_FILE_NAME + version + sdf.format(Date()) + BACKUP_FILE_EXTN
161-
162-
intent.putExtra(Intent.EXTRA_TITLE, zipFileName)
163-
164-
backupActivityResult.launch(intent)
153+
try {
154+
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
155+
// have complete access to the physical location returned as part of result
156+
intent.addCategory(Intent.CATEGORY_OPENABLE)
157+
intent.type = INTENT_TYPE_OCTET
158+
val sdf = SimpleDateFormat(BACKUP_FILE_NAME_DATETIME, Locale.ROOT)
159+
// filename format (Rethink_version_DATA_FORMAT.bk)
160+
val version = getVersionName().replace(' ', '_')
161+
val zipFileName: String = BACKUP_FILE_NAME + version + sdf.format(Date()) + BACKUP_FILE_EXTN
162+
163+
intent.putExtra(Intent.EXTRA_TITLE, zipFileName)
164+
165+
// Check if there's an activity that can handle this intent
166+
if (intent.resolveActivity(requireContext().packageManager) != null) {
167+
backupActivityResult.launch(intent)
168+
} else {
169+
Logger.e(LOG_TAG_BACKUP_RESTORE, "No activity found to handle CREATE_DOCUMENT intent")
170+
Utilities.showToastUiCentered(
171+
requireContext(),
172+
getString(R.string.brbs_backup_dialog_failure_message),
173+
Toast.LENGTH_LONG
174+
)
175+
}
176+
} catch (e: Exception) {
177+
Logger.e(LOG_TAG_BACKUP_RESTORE, "err opening file picker for backup: ${e.message}")
178+
Utilities.showToastUiCentered(
179+
requireContext(),
180+
getString(R.string.brbs_backup_dialog_failure_message),
181+
Toast.LENGTH_LONG
182+
)
183+
}
165184
}
166185

167186
private fun observeBackupWorker() {

app/src/main/java/com/celzero/bravedns/data/AppConfig.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,11 @@ internal constructor(
490490
return customSocks5Endpoint!!
491491
}
492492

493-
suspend fun getHttpProxyDetails(): ProxyEndpoint {
493+
suspend fun getHttpProxyDetails(): ProxyEndpoint? {
494494
if (customHttpEndpoint == null) {
495495
customHttpEndpoint = proxyEndpointRepository.getHttpProxyDetails()
496496
}
497-
return customHttpEndpoint!!
497+
return customHttpEndpoint
498498
}
499499

500500
suspend fun getConnectedOrbotProxy(): ProxyEndpoint? {
@@ -504,11 +504,11 @@ internal constructor(
504504
return orbotEndpoint
505505
}
506506

507-
suspend fun getOrbotSocks5Endpoint(): ProxyEndpoint {
507+
suspend fun getOrbotSocks5Endpoint(): ProxyEndpoint? {
508508
return proxyEndpointRepository.getOrbotSocks5Endpoint()
509509
}
510510

511-
suspend fun getOrbotHttpEndpoint(): ProxyEndpoint {
511+
suspend fun getOrbotHttpEndpoint(): ProxyEndpoint? {
512512
return proxyEndpointRepository.getOrbotHttpEndpoint()
513513
}
514514

app/src/main/java/com/celzero/bravedns/database/ProxyEndpointDAO.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,24 @@ interface ProxyEndpointDAO {
4949
fun getConnectedProxyLiveData(): LiveData<ProxyEndpoint?>
5050

5151
@Query("select * from ProxyEndpoint where proxyMode = 0") // 0 for Custom SOCKS5
52-
fun getCustomSocks5Endpoint(): ProxyEndpoint?
52+
suspend fun getCustomSocks5Endpoint(): ProxyEndpoint?
5353

5454
@Query("select * from ProxyEndpoint where isSelected = 1 and proxyMode = 0")
55-
fun getConnectedSocks5Proxy(): ProxyEndpoint?
55+
suspend fun getConnectedSocks5Proxy(): ProxyEndpoint?
5656

5757
@Query("select * from ProxyEndpoint where proxyMode = 1") // 1 for Custom HTTP
58-
fun getHttpProxyDetails(): ProxyEndpoint
58+
suspend fun getHttpProxyDetails(): ProxyEndpoint?
5959

6060
@Query("select * from ProxyEndpoint where proxyMode = 1 and isSelected = 1")
61-
fun getConnectedHttpProxy(): ProxyEndpoint?
61+
suspend fun getConnectedHttpProxy(): ProxyEndpoint?
6262

6363

6464
@Query("select * from ProxyEndpoint where isSelected = 1 and (proxyMode = 2 or proxyMode = 3)")
65-
fun getConnectedOrbotProxy(): ProxyEndpoint
65+
suspend fun getConnectedOrbotProxy(): ProxyEndpoint?
6666

6767
@Query("select * from ProxyEndpoint where proxyMode = 2") // 2 for Orbot SOCKS5
68-
fun getOrbotSocks5Endpoint(): ProxyEndpoint
68+
suspend fun getOrbotSocks5Endpoint(): ProxyEndpoint?
6969

7070
@Query("select * from ProxyEndpoint where proxyMode = 3") // 3 for Orbot HTTP
71-
fun getOrbotHttpEndpoint(): ProxyEndpoint
71+
suspend fun getOrbotHttpEndpoint(): ProxyEndpoint?
7272
}

app/src/main/java/com/celzero/bravedns/database/ProxyEndpointRepository.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ class ProxyEndpointRepository(private val proxyEndpointDAO: ProxyEndpointDAO) {
4444
return proxyEndpointDAO.getCustomSocks5Endpoint()
4545
}
4646

47-
suspend fun getHttpProxyDetails(): ProxyEndpoint {
47+
suspend fun getHttpProxyDetails(): ProxyEndpoint? {
4848
return proxyEndpointDAO.getHttpProxyDetails()
4949
}
5050

51-
suspend fun getOrbotSocks5Endpoint(): ProxyEndpoint {
51+
suspend fun getOrbotSocks5Endpoint(): ProxyEndpoint? {
5252
return proxyEndpointDAO.getOrbotSocks5Endpoint()
5353
}
5454

55-
suspend fun getOrbotHttpEndpoint(): ProxyEndpoint {
55+
suspend fun getOrbotHttpEndpoint(): ProxyEndpoint? {
5656
return proxyEndpointDAO.getOrbotHttpEndpoint()
5757
}
5858

@@ -64,7 +64,7 @@ class ProxyEndpointRepository(private val proxyEndpointDAO: ProxyEndpointDAO) {
6464
return proxyEndpointDAO.getConnectedProxyLiveData()
6565
}
6666

67-
suspend fun getConnectedOrbotProxy(): ProxyEndpoint {
67+
suspend fun getConnectedOrbotProxy(): ProxyEndpoint? {
6868
return proxyEndpointDAO.getConnectedOrbotProxy()
6969
}
7070
}

0 commit comments

Comments
 (0)