Skip to content

Commit

Permalink
Avoid storing context in global objects
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Oct 11, 2023
1 parent 9a05079 commit 59873a5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class UnifiedPushSettingsViewModel(private val application: Application) : ViewM
EXECUTOR.enqueue {
UnifiedPush.saveDistributor(application, it)
UnifiedPush.registerApp(application)
UnifiedPushHelper.initializeMollySocketLinkedDevice()
UnifiedPushHelper.initializeMollySocketLinkedDevice(ApplicationContext.getInstance())
processNewStatus()
}
// Do not enable if there is no distributor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package im.molly.unifiedpush.device

import android.content.Context
import im.molly.unifiedpush.model.MollyDevice
import org.signal.core.util.logging.Log
import org.signal.libsignal.protocol.util.KeyHelper
Expand All @@ -20,10 +21,9 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress
import java.io.IOException
import java.nio.charset.Charset

class MollySocketLinkedDevice {
class MollySocketLinkedDevice(val context: Context) {
private val TAG = Log.tag(MollySocketLinkedDevice::class.java)
private val DEVICE_NAME = "MollySocket"
private val context = ApplicationDependencies.getApplication()

var device: MollyDevice? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class UnifiedPushRefreshJob private constructor(parameters: Parameters) : BaseJo
@Throws(Exception::class)
public override fun onRun() {
Log.d(TAG, "Running the refresh job")
UnifiedPushHelper.checkDistributorPresence()
UnifiedPushHelper.checkDistributorPresence(context)
if (UnifiedPushHelper.isUnifiedPushAvailable() && !SignalStore.unifiedpush().airGaped) {
Log.i(TAG, "Reregistering to MollySocket...")
when (val status = MollySocketRequest.registerToMollySocketServer()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package im.molly.unifiedpush.util

import android.content.Context
import im.molly.unifiedpush.device.MollySocketLinkedDevice
import im.molly.unifiedpush.model.UnifiedPushStatus
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.unifiedpush.android.connector.UnifiedPush

object UnifiedPushHelper {
private val TAG = Log.tag(UnifiedPushHelper::class.java)
private val context = ApplicationDependencies.getApplication()

// return false if the initialization failed
fun initializeMollySocketLinkedDevice(): Boolean {
fun initializeMollySocketLinkedDevice(context: Context): Boolean {
if (SignalStore.account().isRegistered) {
Log.d(TAG, "Initializing UnifiedPush")
MollySocketLinkedDevice().device ?: run {
MollySocketLinkedDevice(context).device ?: run {
Log.w(TAG, "Can't initialize the linked device for MollySocket")
return false
}
Expand All @@ -37,7 +36,7 @@ object UnifiedPushHelper {
return SignalStore.account().fcmEnabled || isUnifiedPushAvailable()
}

fun checkDistributorPresence() {
fun checkDistributorPresence(context: Context) {
if (UnifiedPush.getDistributor(context).isEmpty()) {
SignalStore.unifiedpush().endpoint = null
}
Expand Down

0 comments on commit 59873a5

Please sign in to comment.