diff --git a/app/src/main/java/foundation/e/pwaplayer/broadcast/PWAStatusBroadcast.kt b/app/src/main/java/foundation/e/pwaplayer/broadcast/PWAStatusBroadcast.kt index 950f29098ad23550435c11e2e079f78c60b6a9ea..848faef5ba12c5372c42d0626f1c8f55c1292897 100644 --- a/app/src/main/java/foundation/e/pwaplayer/broadcast/PWAStatusBroadcast.kt +++ b/app/src/main/java/foundation/e/pwaplayer/broadcast/PWAStatusBroadcast.kt @@ -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 * * * ``` + * 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 diff --git a/app/src/main/java/foundation/e/pwaplayer/provider/PwaProvider.kt b/app/src/main/java/foundation/e/pwaplayer/provider/PwaProvider.kt index 8b2879262c29072d0899ba32d5c81b5962df5cff..be49af00690d4697fabe5fd9fdfb93a3a7d5bf97 100644 --- a/app/src/main/java/foundation/e/pwaplayer/provider/PwaProvider.kt +++ b/app/src/main/java/foundation/e/pwaplayer/provider/PwaProvider.kt @@ -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("", -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 }