Apply a confirmed age restriction to Android push wakes (#9925)

Follow-up to [#9804 handle Android push
wakes](https://github.com/block/buzz/pull/9804), from its review: a
confirmed age restriction now applies to Android push wakes, as it
already does on iOS.

## What it does

- A new native `AgeRestriction` holds a confirmed restriction for the
life of the app process. Like the iOS restriction session, nothing is
saved: a later launch starts unrestricted and the age check runs again.
- The age gate's purge (`purgeAgeRestrictedNotifications`) now runs on
Android. It sets the restriction and cancels every notification Buzz has
posted. Restore (`restoreAgeRestrictedNotifications`) clears it. Both
were iOS-only before.
- On Android the restriction is released only when an age check in the
current engine finishes without restricting (allowed, or failing open).
The process can outlive the engine, for example after Back from the
restricted screen, and a reopened app starts its age state as allowed,
so releasing on that first build would lift a restriction the process
still holds before the new check answers. A retired check releases
nothing. iOS keeps restoring from the push bootstrap as before.
- The purge starts as soon as the age check confirms a restriction, not
when the restricted screen first draws. The check can answer while Buzz
is in the background, where Flutter draws no frames until the user
returns, so before this wakes kept posting until then. This also closes
the same gap on iOS. The restricted screen still retries a failed purge,
and a retry that a resume has replaced stays quiet, so a late failure
can't purge again.
- While the restriction holds, a push wake is skipped before it fetches
anything, the same way as when notifications are turned off.
- Every post from a wake (conversations and the generic "New activity")
checks the restriction under the same lock the purge takes. So a wake
already running when the restriction arrives posts nothing after the
purge, and a post already in progress finishes before the purge cancels
it.

## Decisions

- **Process lifetime, as on iOS.** Wakes run in the app's process, so an
in-memory flag covers every wake while the confirming process lives. A
wake that starts a fresh process (after the app was killed) is not
restricted until the app runs its check again. That matches iOS, where
the restriction ends with the process holding its lock.
- **No release-config change here.** `BUZZ_AGE_GATING_ENABLED` is still
set only for iOS builds, and no build sets the Firebase config yet.
Adding either to an Android build is a separate release change.

### Validation

Added tests:
[`AgeRestrictionTest.kt`](https://github.com/block/buzz/blob/android-push-age-gate/mobile/android/app/src/test/kotlin/xyz/block/buzz/mobile/AgeRestrictionTest.kt)
covers restrict, release and a post that is in progress when the
restriction arrives.
[`push_bridge_test.dart`](https://github.com/block/buzz/blob/android-push-age-gate/mobile/test/shared/push/push_bridge_test.dart)
now covers the purge and restore calls on iOS and Android. The age
gate's outcome table now runs on Android and counts releases: none while
the check is pending, one after an adult, error, malformed or timed-out
result, none after a minor.


[`age_signal_provider_test.dart`](https://github.com/block/buzz/blob/android-push-age-gate/mobile/test/features/age_gate/age_signal_provider_test.dart)'s
upper-bound table now also checks that a confirmed restriction starts
the purge with no widget or frame involved.
[`age_gate_app_test.dart`](https://github.com/block/buzz/blob/android-push-age-gate/mobile/test/features/age_gate/age_gate_app_test.dart)'s
purge retry test now uses the real notifier, with a resume while the
first purge is still running.

On a Pixel 9a (Android 16), a debug build with a local test relay. A
temporary debug hook called the native restrict and release directly,
because the phone was locked and Flutter doesn't draw frames behind the
lock screen:

1. Allowed: a wake posted the new message.
2. Restrict: the posted notification was removed.
3. While restricted: a wake logged `wake ignored foreground=false
restricted=true` and posted nothing.
4. Release: the next wake posted both the message that arrived during
the restriction and the new one.

The Dart side calling purge and restore is covered by the bridge test
above.

## Regressions

None expected: without age gating enabled, the restriction is never set
on Android.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
This commit is contained in:
Tom Brow
2026-11-24 22:14:05 -08:00
committed by GitHub
parent 9bddaa1b64
commit 2f1be86525
12 changed files with 289 additions and 64 deletions

View File

@@ -0,0 +1,36 @@
package xyz.block.buzz.mobile
/**
* A confirmed age restriction, as push wakes see it.
*
* Like the iOS restriction session, it lives only as long as the process that
* confirmed it, and nothing is saved: a later launch starts unrestricted and
* checks again. Wakes run in the same process as the app, so while it holds
* no wake posts anything.
*/
internal object AgeRestriction {
private val lock = Any()
// Guarded by lock.
private var restricted = false
/** Whether a wake should be skipped without fetching anything. */
val isRestricted: Boolean
get() = synchronized(lock) { restricted }
/**
* Restricts, then removes what is already posted with [cancelAll]. Posting
* goes through [unlessRestricted], so nothing a wake posts outlives this.
*/
fun restrict(cancelAll: () -> Unit) = synchronized(lock) {
restricted = true
cancelAll()
}
fun release() = synchronized(lock) { restricted = false }
/** Runs [post] unless restricted, and never during [restrict]. */
fun unlessRestricted(post: () -> Unit) = synchronized(lock) {
if (!restricted) post()
}
}

View File

@@ -155,7 +155,7 @@ class MainActivity : FlutterFragmentActivity() {
identityFilePlugin = IdentityFilePlugin(this, flutterEngine.dartExecutor.binaryMessenger)
browserAuthPlugin = BrowserAuthPlugin(this, flutterEngine.dartExecutor.binaryMessenger)
hapticsPlugin = HapticsPlugin(this, flutterEngine.dartExecutor.binaryMessenger)
pushNotificationOpenPlugin = PushNotificationOpenPlugin(flutterEngine.dartExecutor.binaryMessenger)
pushNotificationOpenPlugin = PushNotificationOpenPlugin(this, flutterEngine.dartExecutor.binaryMessenger)
.also { it.onLaunch(intent) }
huddleMediaPlugin = HuddleMediaPlugin(

View File

@@ -1,6 +1,8 @@
package xyz.block.buzz.mobile
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationManagerCompat
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel
@@ -10,8 +12,11 @@ import io.flutter.plugin.common.MethodChannel
*
* A tap that starts the app is held until Dart asks for it, since its handler
* isn't installed yet. A tap while the app runs is delivered at once.
*
* The same channel carries the age gate's purge and restore, as on iOS.
*/
internal class PushNotificationOpenPlugin(messenger: BinaryMessenger) {
internal class PushNotificationOpenPlugin(context: Context, messenger: BinaryMessenger) {
private val notifications = NotificationManagerCompat.from(context.applicationContext)
private val channel = MethodChannel(messenger, "buzz/push")
private var pending: Map<String, String>? = null
@@ -22,6 +27,14 @@ internal class PushNotificationOpenPlugin(messenger: BinaryMessenger) {
result.success(pending)
pending = null
}
"purgeAgeRestrictedNotifications" -> {
AgeRestriction.restrict(notifications::cancelAll)
result.success(null)
}
"restoreAgeRestrictedNotifications" -> {
AgeRestriction.release()
result.success(null)
}
else -> result.notImplemented()
}
}

View File

@@ -71,7 +71,10 @@ internal object PushWake {
// Declined permission costs nothing else: nothing is fetched, so
// nothing is recorded as shown, and a later grant sees new messages.
val foreground = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)
if (foreground || !NotificationManagerCompat.from(context).areNotificationsEnabled()) {
// A confirmed age restriction skips the wake the same way, and also
// keeps a run already going from posting (see WakeNotifier). The log
// leaves it out: the system log must not reveal the age decision.
if (foreground || AgeRestriction.isRestricted || !NotificationManagerCompat.from(context).areNotificationsEnabled()) {
Log.i(TAG, "wake ignored foreground=$foreground")
handled.countDown()
return

View File

@@ -181,7 +181,7 @@ internal class WakeNotifier(private val context: Context) {
if (silent) NotificationCompat.GROUP_ALERT_SUMMARY else NotificationCompat.GROUP_ALERT_ALL,
)
.build()
manager.notify(tag, CONVERSATION_ID, notification)
AgeRestriction.unlessRestricted { manager.notify(tag, CONVERSATION_ID, notification) }
}
}
@@ -193,18 +193,15 @@ internal class WakeNotifier(private val context: Context) {
?: throw IllegalStateException("No launch intent"),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
manager.notify(
GENERIC_TAG,
GENERIC_ID,
NotificationCompat.Builder(context, OTHER_CHANNEL)
.setSmallIcon(R.drawable.ic_stat_buzz)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.notification_new_activity))
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setAutoCancel(true)
.setContentIntent(tap)
.build(),
)
val notification = NotificationCompat.Builder(context, OTHER_CHANNEL)
.setSmallIcon(R.drawable.ic_stat_buzz)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(context.getString(R.string.notification_new_activity))
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setAutoCancel(true)
.setContentIntent(tap)
.build()
AgeRestriction.unlessRestricted { manager.notify(GENERIC_TAG, GENERIC_ID, notification) }
}
fun cancelGeneric() = manager.cancel(GENERIC_TAG, GENERIC_ID)

View File

@@ -0,0 +1,56 @@
package xyz.block.buzz.mobile
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class AgeRestrictionTest {
@AfterTest
fun release() = AgeRestriction.release()
@Test
fun restrictionCancelsPostedNotificationsAndBlocksPostingUntilReleased() {
val posted = mutableListOf<String>()
AgeRestriction.unlessRestricted { posted += "before" }
AgeRestriction.restrict { posted.clear() }
AgeRestriction.unlessRestricted { posted += "restricted" }
assertTrue(AgeRestriction.isRestricted)
assertEquals(emptyList(), posted)
AgeRestriction.release()
AgeRestriction.unlessRestricted { posted += "released" }
assertFalse(AgeRestriction.isRestricted)
assertEquals(listOf("released"), posted)
}
@Test
fun postingInProgressFinishesBeforeRestrictionCancels() {
val events = mutableListOf<String>()
val posting = java.util.concurrent.CountDownLatch(1)
val restricting = Thread {
posting.await()
AgeRestriction.restrict { events += "cancelAll" }
}
restricting.start()
AgeRestriction.unlessRestricted {
posting.countDown()
// Post only once the restricting thread waits for this one, or has
// finished because nothing made it wait.
val deadline = System.nanoTime() + 5_000_000_000L
while (restricting.state != Thread.State.BLOCKED && restricting.isAlive) {
check(System.nanoTime() < deadline) { "restrict neither waited nor finished" }
Thread.yield()
}
events += "post"
}
restricting.join()
assertEquals(listOf("post", "cancelAll"), events)
}
}