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

View File

@ -2728,31 +2728,31 @@ class BaresipService: Service() {
private fun cleanService() {
if (!isServiceClean) {
try {
if (hotSpotReceiverRegistered) {
if (hotSpotReceiverRegistered) {
try {
unregisterReceiver(hotSpotReceiver)
hotSpotReceiverRegistered = false
}
} catch (_: IllegalArgumentException) {
Log.e(TAG, "hotSpotReceiver was not registered")
} catch (_: IllegalArgumentException) {}
hotSpotReceiverRegistered = false
}
if (bluetoothReceiverRegistered) {
try {
unregisterReceiver(bluetoothReceiver)
bluetoothReceiverRegistered = false
} catch (_: IllegalArgumentException) {}
bluetoothReceiverRegistered = false
}
if (airplaneModeReceiverRegistered) {
try {
unregisterReceiver(airplaneModeReceiver)
airplaneModeReceiverRegistered = false
} catch (_: IllegalArgumentException) {}
airplaneModeReceiverRegistered = false
}
if (telephonyCallbackRegistered) {
if (VERSION.SDK_INT >= 31) {
telephonyManager.unregisterTelephonyCallback(telephonyCallback)
telephonyCallbackRegistered = false
try {
telephonyManager.unregisterTelephonyCallback(telephonyCallback)
} catch (_: Exception) {}
}
telephonyCallbackRegistered = false
}
val callps = ConnectionService.connections.keys.toList()
for (callp in callps)
@ -2777,12 +2777,17 @@ class BaresipService: Service() {
proximityWakeLock.release()
if (this::wifiLock.isInitialized)
wifiLock.release()
if (this::networkCallback.isInitialized)
cm.unregisterNetworkCallback(networkCallback)
if (this::androidContactsObserver.isInitialized)
contentResolver.unregisterContentObserver(androidContactsObserver)
if (this::bluetoothReceiver.isInitialized)
unregisterReceiver(bluetoothReceiver)
if (this::networkCallback.isInitialized) {
try {
cm.unregisterNetworkCallback(networkCallback)
} catch (_: Exception) {}
}
if (androidContactsObserverRegistered) {
try {
contentResolver.unregisterContentObserver(androidContactsObserver)
} catch (_: Exception) {}
androidContactsObserverRegistered = false
}
releaseAudioEffects()
isServiceClean = true
}

View File

@ -52,9 +52,9 @@ sealed class Contact {
fun copy(): Contact {
val copy = when (this) {
is BaresipContact ->
BaresipContact(name, ArrayList(uris), email, color, id, favorite)
BaresipContact(name, ArrayList(uris.map { it.copy() }), email, color, id, favorite)
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)
(copy as BaresipContact).avatarImage = this.avatarImage
@ -282,9 +282,9 @@ sealed class Contact {
val uris = ArrayList<ContactUri>()
for (uPart in urisPart.split(",")) {
if (uPart.isEmpty()) continue
if (uPart.contains("[") && uPart.contains("]")) {
val uri = uPart.substringBefore("[")
val label = uPart.substringAfter("[").substringBefore("]")
if (uPart.endsWith("]") && uPart.contains("[")) {
val uri = uPart.substringBeforeLast("[")
val label = uPart.substringAfterLast("[").substringBeforeLast("]")
uris.add(ContactUri(uri, label))
} else {
uris.add(ContactUri(uPart, ""))
@ -293,15 +293,9 @@ sealed class Contact {
val params = uriParams.substringAfter(">;")
val email = Utils.paramValue(params, "email")
val colorValue = Utils.paramValue(params, "color" )
val color: Int = if (colorValue != "")
colorValue.toInt()
else
Utils.randomColor()
val color: Int = colorValue.toIntOrNull() ?: Utils.randomColor()
val idValue = Utils.paramValue(params, "id" )
val id: Long = if (idValue != "")
idValue.toLong()
else
baseId + contactNo
val id: Long = idValue.toLongOrNull() ?: (baseId + contactNo)
val favorite = Utils.paramValue(params, "favorite" ) == "yes"
// Log.d(TAG, "Restoring contact $name, $urisPart, $color, $id")
val contact = BaresipContact(name, uris, email, color, id, favorite)

View File

@ -2817,7 +2817,7 @@ private fun backup(ctx: Context, password: String) {
Utils.fileNameOfUri(ctx, downloadsOutputUri!!))
showAlert.value = true
Utils.deleteFile(File(zipFilePath))
downloadsOutputUri = null
downloadsInputUri = null
}
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) {
alertTitle.value = ctx.getString(R.string.error)
alertMessage.value = String.format(ctx.getString(R.string.restore_failed),
Utils.fileNameOfUri(ctx, downloadsOutputUri!!))
Utils.fileNameOfUri(ctx, downloadsInputUri!!))
showAlert.value = true
downloadsOutputUri = null
downloadsInputUri = null
return
}
if (!Utils.putFileContents(zipFilePath, zipData)) {
Log.w(TAG, "Failed to write zip file '$zipFile'")
alertTitle.value = ctx.getString(R.string.error)
alertMessage.value = String.format(ctx.getString(R.string.restore_failed),
Utils.fileNameOfUri(ctx, downloadsOutputUri!!))
Utils.fileNameOfUri(ctx, downloadsInputUri!!))
showAlert.value = true
downloadsOutputUri = null
downloadsInputUri = null
return
}
if (!Utils.unZip(zipFilePath)) {
@ -2850,7 +2850,7 @@ private fun restore(ctx: Context, password: String, onRestartApp: () -> Unit) {
BuildConfig.VERSION_NAME
)
showAlert.value = true
downloadsOutputUri = null
downloadsInputUri = null
return
}
Utils.deleteFile(File(zipFilePath))
@ -2876,6 +2876,6 @@ private fun restore(ctx: Context, password: String, onRestartApp: () -> Unit) {
}
showDialog.value = true
downloadsOutputUri = null
downloadsInputUri = null
}

View File

@ -751,9 +751,9 @@ object Utils {
fun decryptFromUri(ctx: Context, uri: Uri, password: String): ByteArray? {
var plainData: ByteArray? = null
var stream: FileInputStream
var stream: InputStream
try {
stream = (ctx.contentResolver.openInputStream(uri) as? FileInputStream)
stream = ctx.contentResolver.openInputStream(uri)
?: return null
} catch(e: Exception) {
Log.w(TAG, "decryptFromUri could not open stream: $e")
@ -764,19 +764,17 @@ object Utils {
val content = it.readObject() as ByteArray
plainData = decrypt(content, password.toCharArray())
}
stream.close()
} catch (e: Exception) {
Log.w(TAG, "decryptFromUri as ByteArray failed: $e")
stream.close()
try {
stream = ctx.contentResolver.openInputStream(uri) as FileInputStream
ObjectInputStream(stream).use {
val obj = it.readObject() as Crypto
plainData = decryptOld(obj, password.toCharArray())
ctx.contentResolver.openInputStream(uri)?.use { newStream ->
ObjectInputStream(newStream).use {
val obj = it.readObject() as Crypto
plainData = decryptOld(obj, password.toCharArray())
}
}
stream.close()
} catch (e: Exception) {
Log.w(TAG, "decryptFromUri as Crypto failed: $e")
} catch (e2: Exception) {
Log.w(TAG, "decryptFromUri as Crypto failed: $e2")
}
}
return plainData