Skip to content

Commit

Permalink
Add notifications related to UnifiedPush events
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Jan 29, 2023
1 parent 2e16703 commit d821abf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class UnifiedPushSettingsViewModel(private val application: Application) : ViewM
} ?: return
} else {
UnifiedPush.unregisterApp(application)
SignalStore.unifiedpush().airGaped = false
SignalStore.unifiedpush().mollySocketUrl = null
processNewStatus()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

package im.molly.unifiedpush.jobs

import im.molly.unifiedpush.model.RegistrationStatus
import im.molly.unifiedpush.model.saveStatus
import im.molly.unifiedpush.util.MollySocketRequest
import im.molly.unifiedpush.util.UnifiedPushHelper
import im.molly.unifiedpush.util.UnifiedPushNotificationBuilder
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.jobmanager.Data
Expand Down Expand Up @@ -46,12 +48,9 @@ class UnifiedPushRefreshJob private constructor(parameters: Parameters) : BaseJo
Log.w(TAG, "The registration status has changed!")
status.saveStatus()
ApplicationContext.getInstance().initializeFcmCheck()
// TODO: alert user
UnifiedPushNotificationBuilder(context).setNotificationMollySocketRegistrationChanged()
}
}
} else {
ApplicationContext.getInstance().initializeFcmCheck()
// TODO: alert user
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import im.molly.unifiedpush.model.UnifiedPushStatus
import im.molly.unifiedpush.model.saveStatus
import im.molly.unifiedpush.util.MollySocketRequest
import im.molly.unifiedpush.util.UnifiedPushHelper
import im.molly.unifiedpush.util.UnifiedPushNotificationBuilder
import org.greenrobot.eventbus.EventBus
import org.signal.core.util.concurrent.SignalExecutors
import org.signal.core.util.logging.Log
Expand All @@ -32,18 +33,23 @@ class UnifiedPushReceiver : MessagingReceiver() {
SignalStore.unifiedpush().endpoint = endpoint
when (SignalStore.unifiedpush().status) {
UnifiedPushStatus.AIR_GAPED -> {
// TODO: alert if air gaped and endpoint changes
EventBus.getDefault().post(UnifiedPushRegistrationEvent)
UnifiedPushNotificationBuilder(context).setNotificationEndpointChangedAirGaped()
}
UnifiedPushStatus.OK -> {
EXECUTOR.enqueue {
MollySocketRequest.registerToMollySocketServer().saveStatus()
EventBus.getDefault().post(UnifiedPushRegistrationEvent)
UnifiedPushNotificationBuilder(context).setNotificationEndpointChangedError()
}
}
in listOf(
UnifiedPushStatus.INTERNAL_ERROR,
UnifiedPushStatus.MISSING_ENDPOINT,
UnifiedPushStatus.OK,
) -> {
EXECUTOR.enqueue {
MollySocketRequest.registerToMollySocketServer().saveStatus()
EventBus.getDefault().post(UnifiedPushRegistrationEvent)
// TODO: alert if status changes from Ok to something else
}
}
else -> {
Expand All @@ -55,7 +61,7 @@ class UnifiedPushReceiver : MessagingReceiver() {

override fun onRegistrationFailed(context: Context, instance: String) {
// called when the registration is not possible, eg. no network
// TODO: alert user the registration has failed
UnifiedPushNotificationBuilder(context).setNotificationRegistrationFailed()
}

override fun onUnregistered(context: Context, instance: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package im.molly.unifiedpush.util

import android.app.Notification
import android.app.NotificationManager
import android.content.Context
import androidx.core.app.NotificationCompat
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.notifications.NotificationChannels

class UnifiedPushNotificationBuilder(val context: Context) {

private val NOTIFICATION_ID_UNIFIEDPUSH = 51215

private val builder: NotificationCompat.Builder = NotificationCompat.Builder(context, NotificationChannels.getInstance().APP_ALERTS)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.UnifiedPushNotificationBuilder__title))
.setContentIntent(null)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)

private fun getNotification(content: String): Notification {
return builder.setContentText(content).setStyle(
NotificationCompat.BigTextStyle()
.bigText(content)
).build()
}

fun setNotificationMollySocketRegistrationChanged() {
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
.notify(NOTIFICATION_ID_UNIFIEDPUSH, getNotification(context.getString(R.string.UnifiedPushNotificationBuilder__mollysocket_registration_changed)))
}

fun setNotificationEndpointChangedAirGaped() {
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
.notify(NOTIFICATION_ID_UNIFIEDPUSH, getNotification(context.getString(R.string.UnifiedPushNotificationBuilder__endpoint_changed_airgaped)))
}

fun setNotificationEndpointChangedError() {
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
.notify(NOTIFICATION_ID_UNIFIEDPUSH, getNotification(context.getString(R.string.UnifiedPushNotificationBuilder__endpoint_changed_error)))
}

fun setNotificationRegistrationFailed() {
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
.notify(NOTIFICATION_ID_UNIFIEDPUSH, getNotification(context.getString(R.string.UnifiedPushNotificationBuilder__registration_failed)))
}
}
5 changes: 5 additions & 0 deletions app/src/unifiedpush/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@
<string name="UnifiedPushSettingsFragment__status_summary_unknown_error">Unknown error</string>
<string name="UnifiedPushSettingsFragment__strategy_summary">The websocket strategy may drain a little more battery but reduces the number of requests sent to the provider.</string>
<string name="UnifiedPushSettingsFragment__air_gaped_summary">Enable if your MollySocket server can\'t be reach on the Internet. You will have to register the connection manually</string>
<string name="UnifiedPushNotificationBuilder__title">UnifiedPush</string>
<string name="UnifiedPushNotificationBuilder__mollysocket_registration_changed">Your registration on MollySocket isn\'t valid anymore. Try to remove the linked device and register again.</string>
<string name="UnifiedPushNotificationBuilder__endpoint_changed_airgaped">Your UnifiedPush endpoint has changed. You must update your connection on MollySocket.</string>
<string name="UnifiedPushNotificationBuilder__endpoint_changed_error">An error occurred while changing your UnifiedPush endpoint. Try to register again to MollySocket.</string>
<string name="UnifiedPushNotificationBuilder__registration_failed">Your UnifiedPush distributor refused the registration. You may not have any connection or a requirement is missing for your distributor.</string>
</resources>

0 comments on commit d821abf

Please sign in to comment.