(fix): mobile notification showing "Connected" if the device is offline
This commit is contained in:
parent
d07202386f
commit
1b17158ad5
2 changed files with 36 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
<uses-permission android:name="android.permission.CAMERA" />
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||||
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
|
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,30 @@ import android.app.Service
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.ServiceInfo
|
import android.content.pm.ServiceInfo
|
||||||
|
import android.net.ConnectivityManager
|
||||||
|
import android.net.Network
|
||||||
|
import android.net.NetworkCapabilities
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
|
|
||||||
class MtpForegroundService : Service() {
|
class MtpForegroundService : Service() {
|
||||||
private var started = false
|
private var started = false
|
||||||
|
private val networkCallback = object : ConnectivityManager.NetworkCallback() {
|
||||||
|
override fun onAvailable(network: Network) = refreshNotification()
|
||||||
|
override fun onLost(network: Network) = refreshNotification()
|
||||||
|
override fun onCapabilitiesChanged(network: Network, capabilities: NetworkCapabilities) =
|
||||||
|
refreshNotification()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent?): IBinder? = null
|
override fun onBind(intent: Intent?): IBinder? = null
|
||||||
|
|
||||||
|
override fun onCreate() {
|
||||||
|
super.onCreate()
|
||||||
|
getSystemService(ConnectivityManager::class.java)
|
||||||
|
.registerDefaultNetworkCallback(networkCallback)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
if (intent?.action == ACTION_STOP) {
|
if (intent?.action == ACTION_STOP) {
|
||||||
MtpSecureStore.setEnabled(this, false)
|
MtpSecureStore.setEnabled(this, false)
|
||||||
|
|
@ -28,7 +43,8 @@ class MtpForegroundService : Service() {
|
||||||
if (started) return START_STICKY
|
if (started) return START_STICKY
|
||||||
|
|
||||||
createChannel(this)
|
createChannel(this)
|
||||||
val notification = buildNotification(this, "Connecting")
|
connectionStatus = "Connecting"
|
||||||
|
val notification = buildNotification(this, displayedStatus(this))
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||||
startForeground(
|
startForeground(
|
||||||
NOTIFICATION_ID,
|
NOTIFICATION_ID,
|
||||||
|
|
@ -65,20 +81,37 @@ class MtpForegroundService : Service() {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
|
getSystemService(ConnectivityManager::class.java).unregisterNetworkCallback(networkCallback)
|
||||||
if (!MtpSecureStore.isEnabled(this)) NativeMtpBridge.nativeStop()
|
if (!MtpSecureStore.isEnabled(this)) NativeMtpBridge.nativeStop()
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun refreshNotification() {
|
||||||
|
updateNotification(this, connectionStatus)
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val CHANNEL_ID = "tensamin-connection"
|
private const val CHANNEL_ID = "tensamin-connection"
|
||||||
private const val NOTIFICATION_ID = 2201
|
private const val NOTIFICATION_ID = 2201
|
||||||
private const val ACTION_STOP = "net.tensamin.client.STOP_MTP"
|
private const val ACTION_STOP = "net.tensamin.client.STOP_MTP"
|
||||||
|
@Volatile private var connectionStatus = "Connecting"
|
||||||
|
|
||||||
fun updateNotification(context: Context, status: String) {
|
fun updateNotification(context: Context, status: String) {
|
||||||
if (!MtpSecureStore.isEnabled(context)) return
|
if (!MtpSecureStore.isEnabled(context)) return
|
||||||
|
connectionStatus = status
|
||||||
createChannel(context)
|
createChannel(context)
|
||||||
context.getSystemService(NotificationManager::class.java)
|
context.getSystemService(NotificationManager::class.java)
|
||||||
.notify(NOTIFICATION_ID, buildNotification(context, status))
|
.notify(NOTIFICATION_ID, buildNotification(context, displayedStatus(context)))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun displayedStatus(context: Context): String {
|
||||||
|
val connectivity = context.getSystemService(ConnectivityManager::class.java)
|
||||||
|
val network = connectivity.activeNetwork ?: return "No network"
|
||||||
|
val capabilities = connectivity.getNetworkCapabilities(network) ?: return "No network"
|
||||||
|
return if (
|
||||||
|
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
|
||||||
|
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
||||||
|
) connectionStatus else "No network"
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createChannel(context: Context) {
|
private fun createChannel(context: Context) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue