Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 990667b4 authored by Sayantan Roychowdhury's avatar Sayantan Roychowdhury Committed by Aayush Gupta
Browse files

PWA Player: Add PWA_ID to broadcast extras

parent 2caa345d
Loading
Loading
Loading
Loading
+19 −8
Original line number Diff line number Diff line
@@ -26,7 +26,8 @@ import android.content.Intent
 * This class is responsible for sending PWA install / removal status to all apps.
 * The sent intent contains following extras:
 * 1. SHORTCUT_ID - string shortcut id.
 * 2. URL - string url of the pwa.
 * 2. URL - string url of the pwa. This is to be set as Intent data to launch the PWA.
 * 3. PWA_ID - long type id of the PWA in PWA Player database. This id is needed to launch a PWA.
 *
 * An app should declare a broadcast receiver in the manifest as below:
 * ```
@@ -43,17 +44,27 @@ import android.content.Intent
 *     </intent-filter>
 *     </receiver>
 * ```
 * Example code of receiving a PWA broadcast and launching a PWA can be found here:
 * https://gitlab.e.foundation/SayantanRC/pwa-broadcast-and-launch-test
 *
 * This class will be able to manually resolve the eligible broadcast receivers in all apps
 * and send them the broadcast.
 */
object PWAStatusBroadcast {

    private fun broadcast(application: Application, action: String, shortcutId: String, url: String) {
    private fun broadcast(
        application: Application,
        action: String,
        shortcutId: String,
        url: String,
        pwaId: Long
    ) {
        try {
            val pm = application.packageManager
            val intent = Intent(action).apply {
                putExtra("SHORTCUT_ID", shortcutId)
                putExtra("URL", url)
                putExtra("PWA_ID", pwaId)
            }
            pm.queryBroadcastReceivers(intent, 0).forEach {
                val component = ComponentName(it.activityInfo.packageName, it.activityInfo.name)
@@ -65,18 +76,18 @@ object PWAStatusBroadcast {
        }
    }

    fun broadcastPwaAdded(context: Context, shortcutId: String, url: String) {
    fun broadcastPwaAdded(context: Context, shortcutId: String, url: String, pwaId: Long) {
        broadcast(context.applicationContext as Application,
            "foundation.e.pwaplayer.PWA_ADDED", shortcutId, url)
            "foundation.e.pwaplayer.PWA_ADDED", shortcutId, url, pwaId)
    }

    fun broadcastPwaChanged(context: Context, shortcutId: String, url: String) {
    fun broadcastPwaChanged(context: Context, shortcutId: String, url: String, pwaId: Long) {
        broadcast(context.applicationContext as Application,
            "foundation.e.pwaplayer.PWA_CHANGED", shortcutId, url)
            "foundation.e.pwaplayer.PWA_CHANGED", shortcutId, url, pwaId)
    }

    fun broadcastPwaRemoved(context: Context, shortcutId: String, url: String) {
    fun broadcastPwaRemoved(context: Context, shortcutId: String, url: String, pwaId: Long) {
        broadcast(context.applicationContext as Application,
            "foundation.e.pwaplayer.PWA_REMOVED", shortcutId, url)
            "foundation.e.pwaplayer.PWA_REMOVED", shortcutId, url, pwaId)
    }
}
 No newline at end of file
+8 −7
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ class PwaProvider : ContentProvider() {
                }
                val uri = ContentUris.withAppendedId(uri, id)
                notifyListeners(uri)
                PWAStatusBroadcast.broadcastPwaAdded(context, pwa.shortcutId, pwa.url)
                PWAStatusBroadcast.broadcastPwaAdded(context, pwa.shortcutId, pwa.url, id)
                return uri
            }
            else -> throw IllegalArgumentException("Invalid URI: Insert failed $uri")
@@ -120,7 +120,7 @@ class PwaProvider : ContentProvider() {
                val count = pwas.update(pwa)
                if (count > 0) {
                    notifyListeners(uri)
                    PWAStatusBroadcast.broadcastPwaChanged(context, pwa.shortcutId, pwa.url)
                    PWAStatusBroadcast.broadcastPwaChanged(context, pwa.shortcutId, pwa.url, pwa.id)
                }
                return count
            }
@@ -157,7 +157,7 @@ class PwaProvider : ContentProvider() {
                val count = pwas.delete(id)
                if (count > 0) {
                    notifyListeners(uri)
                    PWAStatusBroadcast.broadcastPwaRemoved(context, shortcutId, url)
                    PWAStatusBroadcast.broadcastPwaRemoved(context, shortcutId, url, id)
                }
                return count
            }
@@ -169,18 +169,19 @@ class PwaProvider : ContentProvider() {
                    /*
                     * Attempt to get PWA info to send via broadcast before deleting it.
                     */
                    val url: String = try {
                    val (url: String, id: Long) =  try {
                        val cursor = pwas.getById(selectionArgs[0])
                        cursor.moveToFirst()
                        cursor.mapToPwa().url
                        val pwa = cursor.mapToPwa()
                        Pair(pwa.url, pwa.id)
                    } catch (e: Exception) {
                        e.printStackTrace()
                        ""
                        Pair<String, Long>("", -1)
                    }
                    val count = pwas.delete(selectionArgs[0])
                    if (count > 0) {
                        notifyListeners(uri)
                        PWAStatusBroadcast.broadcastPwaRemoved(context, selectionArgs[0], url)
                        PWAStatusBroadcast.broadcastPwaRemoved(context, selectionArgs[0], url, id)
                    }
                    return count
                }