android: small cleanups - DB close in finally, single profile reload

- HomeViewModel closes the profile database in finally blocks and relies
  on the VPN_PROFILES_CHANGED broadcast instead of reloading twice
- READ_EXTERNAL_STORAGE only up to API 32; drop a dead settings flag

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DenozordecandClaude Opus 5.5 committed 2026-10-10 09:20:28 +07:00
1 parent 7abac87140
commit 66688031cd
3 files changed
+27 -11

No files matched your search

@@ -39,7 +39,8 @@
<!-- required to claim update ownership (setRequestUpdateOwnership) so our
own updates keep being applied silently on Android 14+ -->
<uses-permission android:name="android.permission.ENFORCE_UPDATE_OWNERSHIP" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- Required to reliably schedule NAT keepalives/rekeyings via the AlarmManager.
@@ -380,12 +380,18 @@ class HomeViewModel(app: Application) : AndroidViewModel(app), VpnStateService.V
viewModelScope.launch(Dispatchers.IO) {
val source = VpnProfileSource(getApplication())
source.open()
source.deleteVpnProfile(profile)
source.close()
try
{
source.deleteVpnProfile(profile)
}
finally
{
source.close()
}
val intent = Intent(Constants.VPN_PROFILES_CHANGED)
intent.putExtra(Constants.VPN_PROFILES_MULTIPLE, arrayOf(profile.getUUID().toString()))
/* the broadcast reaches profilesChanged, which reloads the list */
LocalBroadcastManager.getInstance(getApplication()).sendBroadcast(intent)
reloadProfiles()
}
}
@@ -395,11 +401,16 @@ class HomeViewModel(app: Application) : AndroidViewModel(app), VpnStateService.V
viewModelScope.launch(Dispatchers.IO) {
val source = VpnProfileSource(getApplication())
source.open()
source.insertProfile(profile)
source.close()
try
{
source.insertProfile(profile)
}
finally
{
source.close()
}
val intent = Intent(Constants.VPN_PROFILES_CHANGED)
LocalBroadcastManager.getInstance(getApplication()).sendBroadcast(intent)
reloadProfiles()
}
}
@@ -434,8 +445,14 @@ class HomeViewModel(app: Application) : AndroidViewModel(app), VpnStateService.V
viewModelScope.launch(Dispatchers.IO) {
val source = VpnProfileSource(getApplication())
source.open()
val all = source.allVpnProfiles.sortedBy { it.name?.lowercase() ?: "" }
source.close()
val all = try
{
source.allVpnProfiles.sortedBy { it.name?.lowercase() ?: "" }
}
finally
{
source.close()
}
_ui.update { state ->
val selected = all.firstOrNull { it.getUUID() == state.selected?.getUUID() }
?: all.firstOrNull { it.getUUID() == state.activeProfile?.getUUID() }
@@ -240,7 +240,6 @@ fun SettingsScreen(profiles: List<VpnProfile>, showTitle: Boolean = true)
ShxSection(title = stringResource(R.string.settings_page_appearance))
{
ThemeModeRow()
var addedRow = false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
{
var dynamicColors by remember { ShxThemeSettings.load(context); mutableStateOf(ShxThemeSettings.useDynamicColors.value) }
@@ -254,7 +253,6 @@ fun SettingsScreen(profiles: List<VpnProfile>, showTitle: Boolean = true)
ShxThemeSettings.setUseDynamicColors(context, it)
},
)
addedRow = true
}
}
}