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

@@ -290,15 +290,21 @@ void main() {
await tester.pump();
expect(snapshotRestorations, 2);
},
// iOS restores from the push bootstrap, retrying a failure.
variant: TargetPlatformVariant.only(TargetPlatform.iOS),
);
for (final outcome in ['minor', 'adult', 'error', 'malformed', 'timeout']) {
testWidgets('normal app and push start before native $outcome result', (
tester,
) async {
// On Android only a check that finishes allowed releases the native
// restriction, which a live process may still hold from an earlier
// engine; the first, still-pending build releases nothing.
final response = Completer<Object?>();
var requests = 0;
var restrictions = 0;
var restores = 0;
final relaySession = _CountingRelaySessionNotifier();
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(ageSignalChannel, (_) {
@@ -317,7 +323,7 @@ void main() {
AgeSignalNotifier(requestTimeout: const Duration(seconds: 1)),
),
ageAllowedNotificationRestorerProvider.overrideWithValue(
() async {},
() async => restores++,
),
ageRestrictedNotificationPurgerProvider.overrideWithValue(() async {
restrictions++;
@@ -332,6 +338,7 @@ void main() {
expect(find.byType(BuzzPushBootstrap), findsOneWidget);
expect(relaySession.builds, 1);
expect(requests, 1);
expect(restores, 0);
if (outcome == 'timeout') {
await tester.pump(const Duration(seconds: 2));
@@ -350,6 +357,7 @@ void main() {
await tester.pump();
final restricted = outcome == 'minor';
expect(restrictions, restricted ? 1 : 0);
expect(restores, restricted ? 0 : 1);
expect(
find.byType(AgeRestrictionPage),
restricted ? findsOneWidget : findsNothing,
@@ -361,7 +369,7 @@ void main() {
);
await tester.pumpWidget(const SizedBox.shrink());
await tester.pump();
});
}, variant: TargetPlatformVariant.only(TargetPlatform.android));
}
testWidgets(
@@ -392,31 +400,68 @@ void main() {
testWidgets('retries a failed restricted notification purge', (tester) async {
var purges = 0;
final firstPurge = Completer<void>();
final container = ProviderContainer(
overrides: [
ageSignalProvider.overrideWith(
() => AgeSignalNotifier(
requestSignal: () async => {'status': 'signal', 'ageUpper': 17},
),
),
communityListProvider.overrideWith(
() => _UnavailableCommunityListNotifier(),
),
ageAllowedNotificationRestorerProvider.overrideWithValue(() async {}),
ageRestrictedNotificationPurgerProvider.overrideWithValue(() {
purges += 1;
return switch (purges) {
1 => firstPurge.future,
2 => Future.error(
StateError('injected notification purge failure'),
),
_ => Future.value(),
};
}),
ageRestrictedNotificationMaintenanceScheduleProvider.overrideWithValue(
(_) => () {},
),
ageSignalPushSnapshotRetryWaitProvider.overrideWithValue((_) async {}),
],
);
addTearDown(container.dispose);
await container.read(ageSignalProvider.notifier).request();
expect(container.read(ageSignalProvider), AgeSignalState.restricted);
await tester.pumpWidget(
ProviderScope(
overrides: [
ageSignalProvider.overrideWith(() => _BlockingAgeSignalNotifier()),
communityListProvider.overrideWith(
() => _UnavailableCommunityListNotifier(),
),
ageRestrictedNotificationPurgerProvider.overrideWithValue(() async {
purges += 1;
if (purges == 1) {
throw StateError('injected notification purge failure');
}
}),
ageSignalPushSnapshotRetryWaitProvider.overrideWithValue(
(_) async {},
),
],
UncontrolledProviderScope(
container: container,
child: const AgeSignalPushBootstrap(child: SizedBox()),
),
);
await tester.pump();
expect(purges, 1);
// A resume while the confirmed purge is still running retires the effect
// waiting on it. The new effect purges, fails once and retries.
for (final state in [
AppLifecycleState.inactive,
AppLifecycleState.hidden,
AppLifecycleState.paused,
AppLifecycleState.hidden,
AppLifecycleState.inactive,
AppLifecycleState.resumed,
]) {
tester.binding.handleAppLifecycleStateChanged(state);
}
await tester.pump();
await tester.pump();
expect(purges, 3);
firstPurge.completeError(StateError('injected notification purge failure'));
await tester.pump();
await tester.pump();
expect(purges, 2);
expect(purges, 3);
});
testWidgets(

View File

@@ -12,10 +12,16 @@ void main() {
.setMockMethodCallHandler(ageSignalChannel, null);
});
var purges = 0;
setUp(() => purges = 0);
ProviderContainer enabledContainer({AgeSignalNotifier Function()? create}) {
final container = ProviderContainer(
overrides: [
ageSignalProvider.overrideWith(create ?? AgeSignalNotifier.new),
ageRestrictedNotificationPurgerProvider.overrideWithValue(() async {
purges += 1;
}),
],
);
addTearDown(container.dispose);
@@ -52,12 +58,14 @@ void main() {
final container = enabledContainer();
expect(container.read(ageSignalProvider), AgeSignalState.allowed);
await container.read(ageSignalProvider.notifier).request();
final restricted = upper >= 0 && upper < 18;
expect(
container.read(ageSignalProvider),
upper >= 0 && upper < 18
? AgeSignalState.restricted
: AgeSignalState.allowed,
restricted ? AgeSignalState.restricted : AgeSignalState.allowed,
);
// With no widget or frame at all: an answer that arrives in the
// background must stop wakes from posting at once.
expect(purges, restricted ? 1 : 0);
});
}

View File

@@ -61,17 +61,27 @@ void main() {
},
);
test('purges notifications delivered before age restriction', () async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(_channel, (call) async {
expect(call.method, 'purgeAgeRestrictedNotifications');
expect(call.arguments, isNull);
return null;
});
for (final platform in [TargetPlatform.iOS, TargetPlatform.android]) {
test('purges and restores age-restricted notifications on '
'${platform.name}', () async {
debugDefaultTargetPlatformOverride = platform;
final methods = <String>[];
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(_channel, (call) async {
methods.add(call.method);
expect(call.arguments, isNull);
return null;
});
await purgeAgeRestrictedBuzzNotifications();
});
await purgeAgeRestrictedBuzzNotifications();
await restoreAgeRestrictedBuzzNotifications();
expect(methods, [
'purgeAgeRestrictedNotifications',
'restoreAgeRestrictedNotifications',
]);
});
}
test('reads and exposes persisted endpoint grants on iOS', () async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;