Skip to content

Commit

Permalink
Listen to pokey broadcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Jan 8, 2025
1 parent 92d2135 commit b471fd2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
android:foregroundServiceType="remoteMessaging">
</service>

<receiver
android:name=".service.PokeyReceiver"
android:exported="true"
>
<intent-filter>
<action android:name="com.shared.NOSTR" />
</intent-filter>
</receiver>

<receiver
android:name=".service.BootBroadcastReceiver"
android:enabled="true"
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/greenart7c3/citrine/Citrine.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.greenart7c3.citrine

import android.annotation.SuppressLint
import android.app.Application
import android.content.ContentResolver
import android.content.IntentFilter
import android.os.Build
import android.util.Log
import com.greenart7c3.citrine.database.AppDatabase
import com.greenart7c3.citrine.okhttp.OkHttpWebSocket
import com.greenart7c3.citrine.server.OlderThan
import com.greenart7c3.citrine.server.Settings
import com.greenart7c3.citrine.service.LocalPreferences
import com.greenart7c3.citrine.service.PokeyReceiver
import com.vitorpamplona.ammolite.relays.NostrClient
import com.vitorpamplona.ammolite.relays.Relay
import com.vitorpamplona.quartz.events.Event
Expand All @@ -28,12 +32,26 @@ class Citrine : Application() {
val client: NostrClient = NostrClient(OkHttpWebSocket.Builder())
var isImportingEvents = false
var job: Job? = null
private val pokeyReceiver = PokeyReceiver()

@SuppressLint("UnspecifiedRegisterReceiverFlag")
override fun onCreate() {
super.onCreate()

instance = this
LocalPreferences.loadSettingsFromEncryptedStorage(this)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(
pokeyReceiver,
IntentFilter(PokeyReceiver.POKEY_ACTION),
RECEIVER_EXPORTED,
)
} else {
registerReceiver(
pokeyReceiver,
IntentFilter(PokeyReceiver.POKEY_ACTION),
)
}
}

fun contentResolverFn(): ContentResolver = contentResolver
Expand Down
60 changes: 60 additions & 0 deletions app/src/main/java/com/greenart7c3/citrine/service/PokeyReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (c) 2024 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.greenart7c3.citrine.service

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.greenart7c3.citrine.Citrine
import com.vitorpamplona.quartz.events.Event
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

class PokeyReceiver : BroadcastReceiver() {
companion object {
const val POKEY_ACTION = "com.shared.NOSTR"
}

private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())

override fun onReceive(
context: Context,
intent: Intent,
) {
if (intent.action == POKEY_ACTION) { // it's best practice to verify intent action before performing any operation
val eventStr = intent.getStringExtra("EVENT")
Log.d(Citrine.TAG, "New Pokey Notification Arrived $eventStr")

if (eventStr == null) return

scope.launch(Dispatchers.IO) {
try {
CustomWebSocketService.server?.innerProcessEvent(Event.fromJson(eventStr), null)
} catch (e: Exception) {
Log.e(Citrine.TAG, "Failed to parse Pokey Event", e)
}
}
}
}
}

0 comments on commit b471fd2

Please sign in to comment.