Add Network Callback

This commit is contained in:
S1m 2021-11-24 01:12:11 +01:00
parent 85b838acfd
commit 4d39c5b2fd
4 changed files with 43 additions and 4 deletions

View File

@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.unifiedpush.distributor.nextpush" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

View File

@ -43,13 +43,13 @@ class RegisterBroadcastReceiver : BroadcastReceiver() {
ACTION_UNREGISTER ->{
Log.i(TAG,"UNREGISTER")
val connectorToken = intent.getStringExtra(EXTRA_TOKEN)?: ""
val application = intent.getStringExtra(EXTRA_APPLICATION)?: return
val application = getDb(context!!).getPackageName(connectorToken)
if (application.isBlank()) {
return
}
if (connectorToken !in delQueue) {
delQueue.add(connectorToken)
sendUnregistered(context!!.applicationContext, connectorToken)
sendUnregistered(context.applicationContext, connectorToken)
ApiUtils().deleteApp(context.applicationContext, connectorToken) {
val db = getDb(context.applicationContext)
db.unregisterApp(connectorToken)

View File

@ -33,6 +33,15 @@ fun createForegroundNotification(context: Context): Notification {
notificationManager.createNotificationChannel(channel)
}
val notificationIntent = Intent(context, MainActivity::class.java)
notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
val intent = PendingIntent.getActivity(
context, 0,
notificationIntent, 0
)
val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder(
context,
notificationChannelId
@ -44,6 +53,7 @@ fun createForegroundNotification(context: Context): Notification {
.setSmallIcon(R.drawable.ic_launcher_notification)
.setTicker(context.getString(R.string.listening_notif_ticker))
.setPriority(Notification.PRIORITY_LOW) // for under android 26 compatibility
.setContentIntent(intent)
.build()
}
@ -65,7 +75,7 @@ fun createWarningNotification(context: Context) {
notificationManager.createNotificationChannel(channel)
}
val notificationIntent: Intent = Intent(context, MainActivity::class.java)
val notificationIntent = Intent(context, MainActivity::class.java)
notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP

View File

@ -16,7 +16,15 @@ import com.nextcloud.android.sso.ui.UiExceptionManager
import org.unifiedpush.distributor.nextpush.api.ApiUtils
import org.unifiedpush.distributor.nextpush.account.ssoAccount
import android.net.Network
import android.net.ConnectivityManager
import android.net.ConnectivityManager.NetworkCallback
import java.lang.Exception
private const val TAG = "StartService"
var isServiceStarted = false
fun startListener(context: Context){
Log.d(TAG, "Starting the Listener")
@ -30,7 +38,6 @@ fun startListener(context: Context){
class StartService: Service(){
private var isServiceStarted = false
private var wakeLock: PowerManager.WakeLock? = null
private var api = ApiUtils()
@ -46,6 +53,7 @@ class StartService: Service(){
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
registerNetworkCallback()
startService()
// by returning this we make sure the service is restarted if the system kills the service
return START_STICKY
@ -77,5 +85,25 @@ class StartService: Service(){
api.sync(this)
}
private fun registerNetworkCallback() {
try {
val connectivityManager =
this.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.registerDefaultNetworkCallback(object : NetworkCallback() {
override fun onAvailable(network: Network) {
Log.d(TAG, "Network is CONNECTED")
startService()
}
override fun onLost(network: Network) {
Log.d(TAG, "Network is DISCONNECTED")
}
})
} catch (e: Exception) {
e.printStackTrace()
}
}
}