Skip to content

Commit

Permalink
Add option to request ping from mollysocket
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g authored and valldrac committed Nov 12, 2024
1 parent d2cc5c7 commit e5ead24
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@
<string name="UnifiedPushSettingsFragment__unknown">Unknown</string>
<string name="UnifiedPushSettingsFragment__server_url">Server URL</string>
<string name="UnifiedPushSettingsFragment__no_server_url_summary">Please provide the MollySocket server URL</string>
<string name="UnifiedPushSettingsFragment__test">Test</string>
<string name="UnifiedPushSettingsFragment__test_summary">Request a test notification from MollySocket</string>
<string name="UnifiedPushSettingsFragment__test_toast">A test notification should be shown in a few moments</string>
<string name="UnifiedPushSettingsFragment__no_device">No device</string>
<string name="UnifiedPushSettingsFragment__no_endpoint">No endpoint</string>
<string name="UnifiedPushSettingsFragment__status_summary_disabled">Disabled</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.res.Resources
import android.text.InputType
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import im.molly.unifiedpush.model.UnifiedPushStatus
Expand Down Expand Up @@ -95,6 +96,15 @@ class UnifiedPushSettingsFragment : DSLSettingsFragment(R.string.NotificationDel
iconEnd = getMollySocketUrlIcon(state),
onClick = { urlDialog(state) },
)

clickPref(
title = DSLSettingsText.from(getString(R.string.UnifiedPushSettingsFragment__test)),
summary = DSLSettingsText.from(getString(R.string.UnifiedPushSettingsFragment__test_summary)),
onClick = {
viewModel.pingMollySocket()
Toast.makeText(context, getString(R.string.UnifiedPushSettingsFragment__test_toast), Toast.LENGTH_SHORT).show()
},
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import im.molly.unifiedpush.jobs.UnifiedPushRefreshJob
import im.molly.unifiedpush.model.UnifiedPushStatus
import im.molly.unifiedpush.model.saveStatus
import im.molly.unifiedpush.util.MollySocketRequest
import org.signal.core.util.concurrent.SignalExecutors
import org.signal.core.util.logging.Log
Expand Down Expand Up @@ -114,6 +115,12 @@ class UnifiedPushSettingsViewModel(private val application: Application) : ViewM
}
}

fun pingMollySocket() {
EXECUTOR.enqueue {
MollySocketRequest.registerToMollySocketServer(true).saveStatus()
}
}

private fun processNewStatus() {
refresh()
AppDependencies.jobManager.add(UnifiedPushRefreshJob())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class UnifiedPushRefreshJob private constructor(parameters: Parameters) : BaseJo
UnifiedPushStatus.INTERNAL_ERROR -> {
Log.i(TAG, "Registering to MollySocket...")
SignalStore.unifiedpush.pending = false
val msStatus = MollySocketRequest.registerToMollySocketServer()
val msStatus = MollySocketRequest.registerToMollySocketServer(true)
msStatus.saveStatus()
when (msStatus) {
RegistrationStatus.INTERNAL_ERROR -> Log.d(TAG, "An error occurred while trying to re-register with MollySocket.")
Expand All @@ -120,7 +120,7 @@ class UnifiedPushRefreshJob private constructor(parameters: Parameters) : BaseJo

UnifiedPushStatus.OK -> {
Log.i(TAG, "Registering again to MollySocket...")
when (val msStatus = MollySocketRequest.registerToMollySocketServer()) {
when (val msStatus = MollySocketRequest.registerToMollySocketServer(false)) {
RegistrationStatus.INTERNAL_ERROR -> Log.d(TAG, "An error occurred while trying to re-register with MollySocket. It may be a bad connection: ignore it.")
RegistrationStatus.OK -> Log.d(TAG, "Successfully re-registered to MollySocket")
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ data class ConnectionData(
@JsonProperty("device_id") val device_id: Int,
@JsonProperty("password") val password: String,
@JsonProperty("endpoint") val endpoint: String,
@JsonProperty("ping") val ping: Boolean,
)

object MollySocketRequest {
Expand Down Expand Up @@ -68,15 +69,16 @@ object MollySocketRequest {
return true
}

fun registerToMollySocketServer(): RegistrationStatus {
fun registerToMollySocketServer(ping: Boolean): RegistrationStatus {
try {
val data = SignalStore.unifiedpush.device?.let {
val endpoint = SignalStore.unifiedpush.endpoint ?: return RegistrationStatus.NO_ENDPOINT
ConnectionData(
uuid = it.uuid,
device_id = it.deviceId,
password = it.password,
endpoint = endpoint
endpoint = endpoint,
ping = ping
)
} ?: return RegistrationStatus.NO_DEVICE

Expand Down

0 comments on commit e5ead24

Please sign in to comment.