diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 4c30ab0f..24ed9136 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -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 } diff --git a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt index 31b8ef77..8a4fe05a 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Contact.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Contact.kt @@ -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() 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) diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainScreen.kt b/app/src/main/kotlin/com/tutpro/baresip/MainScreen.kt index 3c0941f9..1c92b5b2 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainScreen.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainScreen.kt @@ -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 } diff --git a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt index dc4456da..3eeb73bb 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt @@ -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