Backup/restore and clean service fixes and improvements

This commit is contained in:
Juha Heinanen
2026-06-02 08:36:44 +03:00
parent aabcd83177
commit 5128b12797
4 changed files with 44 additions and 47 deletions
@@ -2728,31 +2728,31 @@ class BaresipService: Service() {
private fun cleanService() { private fun cleanService() {
if (!isServiceClean) { if (!isServiceClean) {
try { if (hotSpotReceiverRegistered) {
if (hotSpotReceiverRegistered) { try {
unregisterReceiver(hotSpotReceiver) unregisterReceiver(hotSpotReceiver)
hotSpotReceiverRegistered = false } catch (_: IllegalArgumentException) {}
} hotSpotReceiverRegistered = false
} catch (_: IllegalArgumentException) {
Log.e(TAG, "hotSpotReceiver was not registered")
} }
if (bluetoothReceiverRegistered) { if (bluetoothReceiverRegistered) {
try { try {
unregisterReceiver(bluetoothReceiver) unregisterReceiver(bluetoothReceiver)
bluetoothReceiverRegistered = false
} catch (_: IllegalArgumentException) {} } catch (_: IllegalArgumentException) {}
bluetoothReceiverRegistered = false
} }
if (airplaneModeReceiverRegistered) { if (airplaneModeReceiverRegistered) {
try { try {
unregisterReceiver(airplaneModeReceiver) unregisterReceiver(airplaneModeReceiver)
airplaneModeReceiverRegistered = false
} catch (_: IllegalArgumentException) {} } catch (_: IllegalArgumentException) {}
airplaneModeReceiverRegistered = false
} }
if (telephonyCallbackRegistered) { if (telephonyCallbackRegistered) {
if (VERSION.SDK_INT >= 31) { if (VERSION.SDK_INT >= 31) {
telephonyManager.unregisterTelephonyCallback(telephonyCallback) try {
telephonyCallbackRegistered = false telephonyManager.unregisterTelephonyCallback(telephonyCallback)
} catch (_: Exception) {}
} }
telephonyCallbackRegistered = false
} }
val callps = ConnectionService.connections.keys.toList() val callps = ConnectionService.connections.keys.toList()
for (callp in callps) for (callp in callps)
@@ -2777,12 +2777,17 @@ class BaresipService: Service() {
proximityWakeLock.release() proximityWakeLock.release()
if (this::wifiLock.isInitialized) if (this::wifiLock.isInitialized)
wifiLock.release() wifiLock.release()
if (this::networkCallback.isInitialized) if (this::networkCallback.isInitialized) {
cm.unregisterNetworkCallback(networkCallback) try {
if (this::androidContactsObserver.isInitialized) cm.unregisterNetworkCallback(networkCallback)
contentResolver.unregisterContentObserver(androidContactsObserver) } catch (_: Exception) {}
if (this::bluetoothReceiver.isInitialized) }
unregisterReceiver(bluetoothReceiver) if (androidContactsObserverRegistered) {
try {
contentResolver.unregisterContentObserver(androidContactsObserver)
} catch (_: Exception) {}
androidContactsObserverRegistered = false
}
releaseAudioEffects() releaseAudioEffects()
isServiceClean = true isServiceClean = true
} }
@@ -52,9 +52,9 @@ sealed class Contact {
fun copy(): Contact { fun copy(): Contact {
val copy = when (this) { val copy = when (this) {
is BaresipContact -> is BaresipContact ->
BaresipContact(name, ArrayList(uris), email, color, id, favorite) BaresipContact(name, ArrayList(uris.map { it.copy() }), email, color, id, favorite)
is AndroidContact -> is AndroidContact ->
AndroidContact(name, ArrayList(uris), email, color, thumbnailUri, id, favorite) AndroidContact(name, ArrayList(uris.map { it.copy() }), email, color, thumbnailUri, id, favorite)
} }
if (this is BaresipContact) if (this is BaresipContact)
(copy as BaresipContact).avatarImage = this.avatarImage (copy as BaresipContact).avatarImage = this.avatarImage
@@ -282,9 +282,9 @@ sealed class Contact {
val uris = ArrayList<ContactUri>() val uris = ArrayList<ContactUri>()
for (uPart in urisPart.split(",")) { for (uPart in urisPart.split(",")) {
if (uPart.isEmpty()) continue if (uPart.isEmpty()) continue
if (uPart.contains("[") && uPart.contains("]")) { if (uPart.endsWith("]") && uPart.contains("[")) {
val uri = uPart.substringBefore("[") val uri = uPart.substringBeforeLast("[")
val label = uPart.substringAfter("[").substringBefore("]") val label = uPart.substringAfterLast("[").substringBeforeLast("]")
uris.add(ContactUri(uri, label)) uris.add(ContactUri(uri, label))
} else { } else {
uris.add(ContactUri(uPart, "")) uris.add(ContactUri(uPart, ""))
@@ -293,15 +293,9 @@ sealed class Contact {
val params = uriParams.substringAfter(">;") val params = uriParams.substringAfter(">;")
val email = Utils.paramValue(params, "email") val email = Utils.paramValue(params, "email")
val colorValue = Utils.paramValue(params, "color" ) val colorValue = Utils.paramValue(params, "color" )
val color: Int = if (colorValue != "") val color: Int = colorValue.toIntOrNull() ?: Utils.randomColor()
colorValue.toInt()
else
Utils.randomColor()
val idValue = Utils.paramValue(params, "id" ) val idValue = Utils.paramValue(params, "id" )
val id: Long = if (idValue != "") val id: Long = idValue.toLongOrNull() ?: (baseId + contactNo)
idValue.toLong()
else
baseId + contactNo
val favorite = Utils.paramValue(params, "favorite" ) == "yes" val favorite = Utils.paramValue(params, "favorite" ) == "yes"
// Log.d(TAG, "Restoring contact $name, $urisPart, $color, $id") // Log.d(TAG, "Restoring contact $name, $urisPart, $color, $id")
val contact = BaresipContact(name, uris, email, color, id, favorite) val contact = BaresipContact(name, uris, email, color, id, favorite)
@@ -2817,7 +2817,7 @@ private fun backup(ctx: Context, password: String) {
Utils.fileNameOfUri(ctx, downloadsOutputUri!!)) Utils.fileNameOfUri(ctx, downloadsOutputUri!!))
showAlert.value = true showAlert.value = true
Utils.deleteFile(File(zipFilePath)) Utils.deleteFile(File(zipFilePath))
downloadsOutputUri = null downloadsInputUri = null
} }
private fun restore(ctx: Context, password: String, onRestartApp: () -> Unit) { private fun restore(ctx: Context, password: String, onRestartApp: () -> Unit) {
@@ -2827,18 +2827,18 @@ private fun restore(ctx: Context, password: String, onRestartApp: () -> Unit) {
if (zipData == null) { if (zipData == null) {
alertTitle.value = ctx.getString(R.string.error) alertTitle.value = ctx.getString(R.string.error)
alertMessage.value = String.format(ctx.getString(R.string.restore_failed), alertMessage.value = String.format(ctx.getString(R.string.restore_failed),
Utils.fileNameOfUri(ctx, downloadsOutputUri!!)) Utils.fileNameOfUri(ctx, downloadsInputUri!!))
showAlert.value = true showAlert.value = true
downloadsOutputUri = null downloadsInputUri = null
return return
} }
if (!Utils.putFileContents(zipFilePath, zipData)) { if (!Utils.putFileContents(zipFilePath, zipData)) {
Log.w(TAG, "Failed to write zip file '$zipFile'") Log.w(TAG, "Failed to write zip file '$zipFile'")
alertTitle.value = ctx.getString(R.string.error) alertTitle.value = ctx.getString(R.string.error)
alertMessage.value = String.format(ctx.getString(R.string.restore_failed), alertMessage.value = String.format(ctx.getString(R.string.restore_failed),
Utils.fileNameOfUri(ctx, downloadsOutputUri!!)) Utils.fileNameOfUri(ctx, downloadsInputUri!!))
showAlert.value = true showAlert.value = true
downloadsOutputUri = null downloadsInputUri = null
return return
} }
if (!Utils.unZip(zipFilePath)) { if (!Utils.unZip(zipFilePath)) {
@@ -2850,7 +2850,7 @@ private fun restore(ctx: Context, password: String, onRestartApp: () -> Unit) {
BuildConfig.VERSION_NAME BuildConfig.VERSION_NAME
) )
showAlert.value = true showAlert.value = true
downloadsOutputUri = null downloadsInputUri = null
return return
} }
Utils.deleteFile(File(zipFilePath)) Utils.deleteFile(File(zipFilePath))
@@ -2876,6 +2876,6 @@ private fun restore(ctx: Context, password: String, onRestartApp: () -> Unit) {
} }
showDialog.value = true showDialog.value = true
downloadsOutputUri = null downloadsInputUri = null
} }
@@ -751,9 +751,9 @@ object Utils {
fun decryptFromUri(ctx: Context, uri: Uri, password: String): ByteArray? { fun decryptFromUri(ctx: Context, uri: Uri, password: String): ByteArray? {
var plainData: ByteArray? = null var plainData: ByteArray? = null
var stream: FileInputStream var stream: InputStream
try { try {
stream = (ctx.contentResolver.openInputStream(uri) as? FileInputStream) stream = ctx.contentResolver.openInputStream(uri)
?: return null ?: return null
} catch(e: Exception) { } catch(e: Exception) {
Log.w(TAG, "decryptFromUri could not open stream: $e") Log.w(TAG, "decryptFromUri could not open stream: $e")
@@ -764,19 +764,17 @@ object Utils {
val content = it.readObject() as ByteArray val content = it.readObject() as ByteArray
plainData = decrypt(content, password.toCharArray()) plainData = decrypt(content, password.toCharArray())
} }
stream.close()
} catch (e: Exception) { } catch (e: Exception) {
Log.w(TAG, "decryptFromUri as ByteArray failed: $e") Log.w(TAG, "decryptFromUri as ByteArray failed: $e")
stream.close()
try { try {
stream = ctx.contentResolver.openInputStream(uri) as FileInputStream ctx.contentResolver.openInputStream(uri)?.use { newStream ->
ObjectInputStream(stream).use { ObjectInputStream(newStream).use {
val obj = it.readObject() as Crypto val obj = it.readObject() as Crypto
plainData = decryptOld(obj, password.toCharArray()) plainData = decryptOld(obj, password.toCharArray())
}
} }
stream.close() } catch (e2: Exception) {
} catch (e: Exception) { Log.w(TAG, "decryptFromUri as Crypto failed: $e2")
Log.w(TAG, "decryptFromUri as Crypto failed: $e")
} }
} }
return plainData return plainData