From 38261e570ca355d79c8d8f18c3f31a03e0e604a3 Mon Sep 17 00:00:00 2001 From: sim Date: Fri, 14 Jan 2022 00:09:57 +0100 Subject: [PATCH 1/7] Fix typo in Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 99f9afa..3f0c362 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ UnifiedPush provider for Nextcloud - android application height="80">](https://f-droid.org/packages/org.unifiedpush.distributor.nextpush/) ## Usage -This application require the [server application](https://github.com/UP-NextPush/server-app) to be intalled on the server and the [Nextcloud application](https://apps.nextcloud.com/apps/android_nextcloud_app) on the mobile phone. +This application require the [server application](https://github.com/UP-NextPush/server-app) to be installed on the server and the [Nextcloud application](https://apps.nextcloud.com/apps/android_nextcloud_app) on the mobile phone. ## Credit This application has been inspired by [Nextcloud Push Notifier](https://gitlab.com/Nextcloud-Push/nextcloud-push-notifier) From e8ce207517a8d16d7536129c748789073b41a97b Mon Sep 17 00:00:00 2001 From: sim Date: Fri, 14 Jan 2022 00:21:49 +0100 Subject: [PATCH 2/7] Change notification wording --- .../distributor/nextpush/services/NotificationUtils.kt | 6 +++--- app/src/main/res/values/strings.xml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt index 75ce478..e3e0145 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt @@ -28,7 +28,7 @@ fun createForegroundNotification(context: Context): Notification { appName, NotificationManager.IMPORTANCE_LOW ).let { - it.description = context.getString(R.string.listening_notif_description) + it.description = context.getString(R.string.foreground_notif_description) it } notificationManager.createNotificationChannel(channel) @@ -50,9 +50,9 @@ fun createForegroundNotification(context: Context): Notification { return builder .setContentTitle(context.getString(R.string.app_name)) - .setContentText(context.getString(R.string.listening_notif_description)) + .setContentText(context.getString(R.string.foreground_notif_description)) .setSmallIcon(R.drawable.ic_launcher_notification) - .setTicker(context.getString(R.string.listening_notif_ticker)) + .setTicker(context.getString(R.string.foreground_notif_ticker)) .setPriority(Notification.PRIORITY_LOW) // for under android 26 compatibility .setContentIntent(intent) .setOngoing(true) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1d775b1..2c92f6b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,7 +5,7 @@ Registered applications - Listening for incoming notifications + Notification to run in the foreground Login You are not connected to Nextcloud yet Logout @@ -18,7 +18,7 @@ Restart Service NextPush is disconnected Warning - Listening + Foreground Nextcloud Files is not installed on your device.\nPlease install it market://details?id=com.nextcloud.client Market From f42589dd9cbc25113aa32bccf09642728dc9a8fa Mon Sep 17 00:00:00 2001 From: sim Date: Fri, 14 Jan 2022 01:16:19 +0100 Subject: [PATCH 3/7] Improve Wake Lock --- .../nextpush/services/SSEListener.kt | 16 ++++++++++-- .../nextpush/services/StartService.kt | 25 +++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt index 65e8898..2556f40 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt @@ -22,6 +22,11 @@ class SSEListener (val context: Context) : EventSourceListener() { override fun onOpen(eventSource: EventSource, response: Response) { deleteWarningNotification(context) nFails = 0 + wakeLock?.let { + while (it.isHeld) { + it.release() + } + } try { Log.d(TAG, "onOpen: " + response.code) } catch (e: Exception) { @@ -31,6 +36,9 @@ class SSEListener (val context: Context) : EventSourceListener() { override fun onEvent(eventSource: EventSource, id: String?, type: String?, data: String) { Log.d(TAG, "New SSE message event=$type message=$data") + wakeLock?.let { + it.acquire() + } when (type) { "warning" -> Log.d(TAG, "Warning event received.") "ping" -> Log.d(TAG, "SSE ping received.") @@ -53,18 +61,22 @@ class SSEListener (val context: Context) : EventSourceListener() { db.unregisterApp(connectorToken) } } + wakeLock?.let { + if (it.isHeld) { + it.release() + } + } } override fun onClosed(eventSource: EventSource) { Log.d(TAG, "onClosed: $eventSource") - isServiceStarted = false + nFails += 1 createWarningNotification(context) startListener(context) } override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) { Log.d(TAG, "onFailure") - isServiceStarted = false nFails += 1 if (nFails > 1) createWarningNotification(context) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt index 1a96542..dabe9c5 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt @@ -24,8 +24,10 @@ import org.unifiedpush.distributor.nextpush.api.apiSync import java.lang.Exception private const val TAG = "StartService" +const val WAKE_LOCK_TAG = "NextPush:StartService:lock" var isServiceStarted = false var nFails = 0 +var wakeLock: PowerManager.WakeLock? = null fun startListener(context: Context){ Log.d(TAG, "Starting the Listener") @@ -39,7 +41,6 @@ fun startListener(context: Context){ class StartService: Service(){ - private var wakeLock: PowerManager.WakeLock? = null private var isCallbackRegistered = false override fun onBind(intent: Intent?): IBinder? { @@ -65,12 +66,26 @@ class StartService: Service(){ override fun onDestroy() { Log.d(TAG, "Destroyed") + if (isServiceStarted) { + startListener(this) + } else { + stopService() + } + } + + private fun stopService() { + Log.d(TAG, "Stopping Service") apiDestroy() - super.onDestroy() + wakeLock?.let { + while (it.isHeld) { + it.release() + } + } + stopSelf() } private fun startService() { - if (isServiceStarted) return + if (isServiceStarted && nFails == 0) return isServiceStarted = true try { @@ -83,7 +98,7 @@ class StartService: Service(){ // we need this lock so our service gets not affected by Doze Mode wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).run { - newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EndlessService::lock").apply { + newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG).apply { acquire() } } @@ -98,7 +113,7 @@ class StartService: Service(){ connectivityManager.registerDefaultNetworkCallback(object : NetworkCallback() { override fun onAvailable(network: Network) { Log.d(TAG, "Network is CONNECTED") - startService() + startListener(this@StartService) } override fun onLost(network: Network) { From 1ab3eaa83a2387bb0b866af334372e9984a8cce4 Mon Sep 17 00:00:00 2001 From: sim Date: Fri, 14 Jan 2022 01:48:07 +0100 Subject: [PATCH 4/7] Add RegistrationFailed if the connector token is used by another app if nextpush is not connected --- .../nextpush/account/AccountUtils.kt | 7 ++-- .../nextpush/activities/MainActivity.kt | 2 +- .../nextpush/distributor/DistributorUtils.kt | 27 ++++++++++++++- .../receivers/RegisterBroadcastReceiver.kt | 33 ++++++++++++++++--- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt index 67d1bd3..796b303 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt @@ -65,11 +65,14 @@ fun nextcloudAppNotInstalledDialog(context: Context) { builder.show() } -fun isConnected(context: Context) : Boolean { +fun isConnected(context: Context, showDialog: Boolean = false) : Boolean { try { ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context) } catch (e: NextcloudFilesAppAccountNotFoundException) { - nextcloudAppNotInstalledDialog(context) + if (showDialog) { + nextcloudAppNotInstalledDialog(context) + } + return false } catch (e: NoCurrentAccountSelectedException) { Log.d(TAG,"Device is not connected") return false diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt index 0a55a86..ebc35c8 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt @@ -46,7 +46,7 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(findViewById(R.id.toolbar)) - if (isConnected(this)) { + if (isConnected(this, showDialog = true)) { showMain() } else { findViewById