From 54e1a530757cb67eb8c18930ca57a200365c60ec Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sun, 12 Apr 2020 11:52:14 +0300 Subject: [PATCH] - Made backup/restore to work also on Android 10 (API level 29) - Suppressed some warnings. --- app/src/main/AndroidManifest.xml | 5 +- .../com/tutpro/baresip/BaresipService.kt | 117 ++++++++++-------- .../kotlin/com/tutpro/baresip/CallHistory.kt | 1 + .../kotlin/com/tutpro/baresip/ChatActivity.kt | 4 +- .../main/kotlin/com/tutpro/baresip/Message.kt | 1 + 5 files changed, 71 insertions(+), 57 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 709b4249..899a8385 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ @@ -34,7 +35,9 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher" android:supportsRtl="true" - android:theme="@style/AppTheme"> + android:requestLegacyExternalStorage="true" + android:theme="@style/AppTheme" + tools:targetApi="q"> = 23) && (network == cm.activeNetwork)) || - ((Build.VERSION.SDK_INT < 23) && - (cm.activeNetworkInfo.toString() == - cm.getNetworkInfo(network).toString()))) { - Log.i(LOG_TAG, "Active network $network@$interfaceName " + - " is available: $linkProperties") - activeNetwork = "$network" - if (isServiceRunning) { - Utils.updateLinkProperties(linkProperties) + val linkProps = cm.getLinkProperties(network) + if (linkProps != null) { + val interfaceName = linkProps.interfaceName!! + if (((Build.VERSION.SDK_INT >= 23) && (network == cm.activeNetwork)) || + ((Build.VERSION.SDK_INT < 23) && + (cm.activeNetworkInfo.toString() == + cm.getNetworkInfo(network).toString()))) { + Log.i(LOG_TAG, "Active network $network@$interfaceName " + + " is available: $linkProps") + activeNetwork = "$network" + if (isServiceRunning) { + Utils.updateLinkProperties(linkProps) + } else { + dnsServers = linkProps.dnsServers + linkAddresses = linkProps.linkAddresses + } } else { - dnsServers = linkProperties.dnsServers - linkAddresses = linkProperties.linkAddresses + Log.i(LOG_TAG, "Non-active network $network@$interfaceName " + + " is available: $linkProps") } - } else { - Log.i(LOG_TAG, "Non-active network $network@$interfaceName " + - " is available: $linkProperties") } } @@ -124,12 +126,14 @@ class BaresipService: Service() { } } if (newNetwork != null) { - val interfaceName = cm.getLinkProperties(newNetwork).interfaceName!! - val linkProperties = cm.getLinkProperties(newNetwork) - Log.d(LOG_TAG, "Updating new active network " + - "$newNetwork@$interfaceName link properties: $linkProperties") - activeNetwork = "$newNetwork" - Utils.updateLinkProperties(cm.getLinkProperties(newNetwork)) + val linkProps = cm.getLinkProperties(newNetwork) + if (linkProps != null) { + val interfaceName = linkProps.interfaceName + Log.d(LOG_TAG, "Updating new active network " + + "$newNetwork@$interfaceName link properties: $linkProps") + activeNetwork = "$newNetwork" + Utils.updateLinkProperties(linkProps) + } } } else { Log.d(LOG_TAG, "Network '$network' is lost") @@ -138,23 +142,26 @@ class BaresipService: Service() { override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) { super.onLinkPropertiesChanged(network, linkProperties) - val interfaceName = cm.getLinkProperties(network).interfaceName!! - if (((Build.VERSION.SDK_INT >= 23) && (network == cm.activeNetwork)) || - ((Build.VERSION.SDK_INT < 23) && - (cm.activeNetworkInfo.toString() == - cm.getNetworkInfo(network).toString()))) { - Log.d(LOG_TAG, "Active network $network@$interfaceName " + - " link properties changed: $linkProperties") - activeNetwork = "$network" - if (isServiceRunning) { - Utils.updateLinkProperties(linkProperties) + val linkProps = cm.getLinkProperties(network) + if (linkProps != null) { + val interfaceName = linkProps.interfaceName + if (((Build.VERSION.SDK_INT >= 23) && (network == cm.activeNetwork)) || + ((Build.VERSION.SDK_INT < 23) && + (cm.activeNetworkInfo.toString() == + cm.getNetworkInfo(network).toString()))) { + Log.d(LOG_TAG, "Active network $network@$interfaceName " + + " link properties changed: $linkProperties") + activeNetwork = "$network" + if (isServiceRunning) { + Utils.updateLinkProperties(linkProperties) + } else { + dnsServers = linkProperties.dnsServers + linkAddresses = linkProperties.linkAddresses + } } else { - dnsServers = linkProperties.dnsServers - linkAddresses = linkProperties.linkAddresses + Log.d(LOG_TAG, "Network $network@$interfaceName " + + " link properties changed: $linkProperties") } - } else { - Log.d(LOG_TAG, "Network $network@$interfaceName " + - " link properties changed: $linkProperties") } } } @@ -331,7 +338,7 @@ class BaresipService: Service() { } "Call Reject" -> { - val callp = intent!!.getStringExtra("callp") + val callp = intent!!.getStringExtra("callp")!! val call = Call.find(callp) if (call == null) { Log.w(LOG_TAG, "onStartCommand did not find call $callp") @@ -348,7 +355,7 @@ class BaresipService: Service() { } "Transfer Show", "Transfer Accept" -> { - val uap = intent!!.getStringExtra("uap") + val uap = intent!!.getStringExtra("uap")!! val ua = UserAgent.find(uap) if (ua == null) { Log.w(LOG_TAG, "onStartCommand did not find ua $uap") @@ -365,7 +372,7 @@ class BaresipService: Service() { } "Transfer Deny" -> { - val callp = intent!!.getStringExtra("callp") + val callp = intent!!.getStringExtra("callp")!! val call = Call.find(callp) if (call == null) Log.w(LOG_TAG, "onStartCommand did not find call $callp") @@ -386,24 +393,24 @@ class BaresipService: Service() { } "Message Save" -> { - val uap = intent!!.getStringExtra("uap") + val uap = intent!!.getStringExtra("uap")!! val ua = UserAgent.find(uap) if (ua == null) Log.w(LOG_TAG, "onStartCommand did not find UA $uap") else ChatsActivity.saveUaMessage(ua.account.aor, - intent.getStringExtra("time").toLong()) + intent.getStringExtra("time")!!.toLong()) nm.cancel(MESSAGE_NOTIFICATION_ID) } "Message Delete" -> { - val uap = intent!!.getStringExtra("uap") + val uap = intent!!.getStringExtra("uap")!! val ua = UserAgent.find(uap) if (ua == null) Log.w(LOG_TAG, "onStartCommand did not find UA $uap") else ChatsActivity.deleteUaMessage(ua.account.aor, - intent.getStringExtra("time").toLong()) + intent.getStringExtra("time")!!.toLong()) nm.cancel(MESSAGE_NOTIFICATION_ID) } @@ -517,18 +524,20 @@ class BaresipService: Service() { if (Build.VERSION.SDK_INT >= 23) { val activeNetwork = cm.activeNetwork if (activeNetwork != null) { - val dnsServers = cm.getLinkProperties(activeNetwork).dnsServers - Log.d(LOG_TAG, "Updating DNS Servers = $dnsServers") - if (Config.updateDnsServers(dnsServers) != 0) { - Log.w(LOG_TAG, "Failed to update DNS servers '$dnsServers'") - } else { - Api.net_dns_debug() - if (!ua.registrationFailed) { - ua.registrationFailed = true - Api.ua_register(uap) + val linkProps = cm.getLinkProperties(activeNetwork) + if (linkProps != null) { + val dnsServers = linkProps.dnsServers + Log.d(LOG_TAG, "Updating DNS Servers = $dnsServers") + if (Config.updateDnsServers(dnsServers) != 0) { + Log.w(LOG_TAG, "Failed to update DNS servers '$dnsServers'") + } else { + Api.net_dns_debug() + if (!ua.registrationFailed) { + ua.registrationFailed = true + Api.ua_register(uap) + } } } - } else { Log.d(LOG_TAG, "No active network!") } diff --git a/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt b/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt index 16946270..58795063 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/CallHistory.kt @@ -62,6 +62,7 @@ class CallHistory(val aor: String, val peerURI: String, val direction: String, try { val fis = FileInputStream(file) val ois = ObjectInputStream(fis) + @Suppress("UNCHECKED_CAST") BaresipService.callHistory = ois.readObject() as ArrayList ois.close() fis.close() diff --git a/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt index 13e4c920..3ce1c719 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt @@ -156,8 +156,8 @@ class ChatActivity : AppCompatActivity() { messageResponseReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { handleMessageResponse(intent.getIntExtra("response code", 0), - intent.getStringExtra("response reason"), - intent.getStringExtra("time")) + intent.getStringExtra("response reason")!!, + intent.getStringExtra("time")!!) } } LocalBroadcastManager.getInstance(this).registerReceiver(messageResponseReceiver, diff --git a/app/src/main/kotlin/com/tutpro/baresip/Message.kt b/app/src/main/kotlin/com/tutpro/baresip/Message.kt index 05e31dc3..8b9bcb31 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Message.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Message.kt @@ -57,6 +57,7 @@ class Message(val aor: String, val peerUri: String, val message: String, val tim try { val fis = FileInputStream(file) val ois = ObjectInputStream(fis) + @Suppress("UNCHECKED_CAST") BaresipService.messages = ois.readObject() as ArrayList ois.close() fis.close()