Skip to content

Commit

Permalink
Use EventBus for unifiedpush registration events
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Jan 29, 2023
1 parent 34bd34d commit 2e16703
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import android.text.InputType
import android.widget.EditText
import android.widget.FrameLayout
import androidx.lifecycle.ViewModelProvider
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import im.molly.unifiedpush.model.FetchStrategy
import im.molly.unifiedpush.model.UnifiedPushStatus
import im.molly.unifiedpush.model.toFetchStrategy
import im.molly.unifiedpush.model.toInt
import org.greenrobot.eventbus.EventBus
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.components.settings.DSLConfiguration
import org.thoughtcrime.securesms.components.settings.DSLSettingsFragment
Expand All @@ -24,7 +24,6 @@ import org.thoughtcrime.securesms.util.adapter.mapping.MappingAdapter
class UnifiedPushSettingsFragment : DSLSettingsFragment(R.string.NotificationsSettingsFragment__unifiedpush) {

private lateinit var viewModel: UnifiedPushSettingsViewModel
private var broadcastReceiverRegistered = false

override fun bindAdapter(adapter: MappingAdapter) {
val factory = UnifiedPushSettingsViewModel.Factory(requireActivity().application)
Expand All @@ -34,27 +33,15 @@ class UnifiedPushSettingsFragment : DSLSettingsFragment(R.string.NotificationsSe
viewModel.state.observe(viewLifecycleOwner) {
adapter.submitList(getConfiguration(it).toMappingModelList())
}
if (!broadcastReceiverRegistered) {
broadcastReceiverRegistered = true
LocalBroadcastManager.getInstance(requireContext())
.registerReceiver(viewModel.broadcastReceiver, viewModel.intentFilter)
}
}

override fun onResume() {
if (!broadcastReceiverRegistered) {
broadcastReceiverRegistered = true
LocalBroadcastManager.getInstance(requireContext())
.registerReceiver(viewModel.broadcastReceiver, viewModel.intentFilter)
}
super.onResume()
override fun onStart() {
super.onStart()
EventBus.getDefault().register(viewModel)
}

override fun onPause() {
LocalBroadcastManager.getInstance(requireContext())
.unregisterReceiver(viewModel.broadcastReceiver)
broadcastReceiverRegistered = false
super.onPause()
override fun onStop() {
EventBus.getDefault().unregister(viewModel)
super.onStop()
}

private fun getConfiguration(state: UnifiedPushSettingsState): DSLConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package im.molly.unifiedpush.components.settings.app.notifications

import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import im.molly.unifiedpush.events.UnifiedPushRegistrationEvent
import im.molly.unifiedpush.model.FetchStrategy
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 org.greenrobot.eventbus.Subscribe
import org.signal.core.util.concurrent.SignalExecutors
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.ApplicationContext
Expand All @@ -24,8 +22,6 @@ import org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor
import org.thoughtcrime.securesms.util.livedata.Store
import org.unifiedpush.android.connector.UnifiedPush

val BROADCAST_NEW_ENDPOINT = "UnifiedPushSettingsViewModel.new_endpoint"

class UnifiedPushSettingsViewModel(private val application: Application) : ViewModel() {

private val TAG = Log.tag(UnifiedPushSettingsViewModel::class.java)
Expand All @@ -34,14 +30,10 @@ class UnifiedPushSettingsViewModel(private val application: Application) : ViewM
private val EXECUTOR = SerialMonoLifoExecutor(SignalExecutors.UNBOUNDED)

val state: LiveData<UnifiedPushSettingsState> = store.stateLiveData
val intentFilter = IntentFilter(BROADCAST_NEW_ENDPOINT)

val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
BROADCAST_NEW_ENDPOINT -> processNewStatus()
}
}
@Subscribe
fun onNewEndpoint(e: UnifiedPushRegistrationEvent) {
processNewStatus()
}

private fun getState(): UnifiedPushSettingsState {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package im.molly.unifiedpush.events

object UnifiedPushRegistrationEvent
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package im.molly.unifiedpush.receiver

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.PowerManager
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import im.molly.unifiedpush.components.settings.app.notifications.BROADCAST_NEW_ENDPOINT
import im.molly.unifiedpush.events.UnifiedPushRegistrationEvent
import im.molly.unifiedpush.model.FetchStrategy
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 org.greenrobot.eventbus.EventBus
import org.signal.core.util.concurrent.SignalExecutors
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
Expand All @@ -34,7 +33,7 @@ class UnifiedPushReceiver : MessagingReceiver() {
when (SignalStore.unifiedpush().status) {
UnifiedPushStatus.AIR_GAPED -> {
// TODO: alert if air gaped and endpoint changes
LocalBroadcastManager.getInstance(context).sendBroadcast(Intent().apply { action = BROADCAST_NEW_ENDPOINT })
EventBus.getDefault().post(UnifiedPushRegistrationEvent)
}
in listOf(
UnifiedPushStatus.INTERNAL_ERROR,
Expand All @@ -43,12 +42,12 @@ class UnifiedPushReceiver : MessagingReceiver() {
) -> {
EXECUTOR.enqueue {
MollySocketRequest.registerToMollySocketServer().saveStatus()
LocalBroadcastManager.getInstance(context).sendBroadcast(Intent().apply { action = BROADCAST_NEW_ENDPOINT })
EventBus.getDefault().post(UnifiedPushRegistrationEvent)
// TODO: alert if status changes from Ok to something else
}
}
else -> {
LocalBroadcastManager.getInstance(context).sendBroadcast(Intent().apply { action = BROADCAST_NEW_ENDPOINT })
EventBus.getDefault().post(UnifiedPushRegistrationEvent)
}
}
}
Expand All @@ -63,6 +62,7 @@ class UnifiedPushReceiver : MessagingReceiver() {
// called when this application is unregistered from receiving push messages
// isPushAvailable becomes false => The websocket starts
SignalStore.unifiedpush().endpoint = null
EventBus.getDefault().post(UnifiedPushRegistrationEvent)
}

override fun onMessage(context: Context, message: ByteArray, instance: String) {
Expand Down

0 comments on commit 2e16703

Please sign in to comment.