5c41474e6a
Schedule reconnects with idle-tolerant alarms, keep a watchdog and wake lock, and restart after task removal so delivery continues while the app is closed. Request battery exemption, stop retrying on expired sessions, persist connection diagnostics, and expose the status in Settings.
194 lines
6.7 KiB
Dart
194 lines
6.7 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
|
||
import '../notification_service/notification_service_bridge.dart';
|
||
import '../notification_service/notification_service_controller.dart';
|
||
import 'settings_group.dart';
|
||
import 'settings_row.dart';
|
||
import 'settings_theme.dart';
|
||
|
||
const deviceDeliveryTitle = 'Notifications in Pipelines';
|
||
const deviceDeliverySwitchLabel = 'Receive notifications in Pipelines';
|
||
const deviceDeliveryNote =
|
||
'Pipelines keeps its own connection to the server and shows its '
|
||
'notifications, so the ntfy app is no longer needed. Android shows a '
|
||
'quiet "Pipelines is connected" notification while it runs.';
|
||
const deviceDeliveryBatteryLabel = 'Allow background delivery';
|
||
const deviceDeliveryNotificationsLabel = 'Allow notifications';
|
||
const deviceDeliveryWarningTitle = 'Delivery can pause while idle';
|
||
const deviceDeliveryWarningText =
|
||
'Android may pause Pipelines in the background, so notifications can '
|
||
'arrive late. Allow background delivery above.';
|
||
const deviceDeliverySamsungNote =
|
||
'Samsung phones: also add Pipelines under Settings › Battery › '
|
||
'Background usage limits › Never sleeping apps.';
|
||
const deviceDeliveryLogTitle = 'Connection log';
|
||
const deviceDeliveryLogEmpty = 'No connection events yet';
|
||
|
||
abstract final class DeviceDeliveryKeys {
|
||
static const toggle = ValueKey<String>('settings.deviceDelivery.toggle');
|
||
static const status = ValueKey<String>('settings.deviceDelivery.status');
|
||
static const battery = ValueKey<String>('settings.deviceDelivery.battery');
|
||
static const batteryWarning = ValueKey<String>(
|
||
'settings.deviceDelivery.batteryWarning',
|
||
);
|
||
static const log = ValueKey<String>('settings.deviceDelivery.log');
|
||
static const permission = ValueKey<String>(
|
||
'settings.deviceDelivery.permission',
|
||
);
|
||
static const error = ValueKey<String>('settings.deviceDelivery.error');
|
||
}
|
||
|
||
/// Android background delivery in Settings: the switch, the service's live
|
||
/// status, and the two system grants it relies on (notification permission and
|
||
/// the battery-optimisation exemption). Absent where there is no service.
|
||
class SettingsDeviceDeliverySection extends StatefulWidget {
|
||
const SettingsDeviceDeliverySection({
|
||
super.key,
|
||
required this.bridge,
|
||
this.controller,
|
||
this.requestNotificationPermission,
|
||
});
|
||
|
||
final NotificationServiceBridge bridge;
|
||
final NotificationServiceController? controller;
|
||
final Future<void> Function()? requestNotificationPermission;
|
||
|
||
@override
|
||
State<SettingsDeviceDeliverySection> createState() =>
|
||
_SettingsDeviceDeliverySectionState();
|
||
}
|
||
|
||
class _SettingsDeviceDeliverySectionState
|
||
extends State<SettingsDeviceDeliverySection> {
|
||
late final NotificationServiceController _controller =
|
||
widget.controller ??
|
||
NotificationServiceController(
|
||
widget.bridge,
|
||
requestNotificationPermission: widget.requestNotificationPermission,
|
||
);
|
||
bool get _ownsController => widget.controller == null;
|
||
|
||
// System prompts (permission, battery) return by resuming the app.
|
||
late final AppLifecycleListener _lifecycle = AppLifecycleListener(
|
||
onResume: () => unawaited(_controller.refresh()),
|
||
);
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
_lifecycle;
|
||
unawaited(_controller.open());
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_lifecycle.dispose();
|
||
if (_ownsController) _controller.dispose();
|
||
super.dispose();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) => ListenableBuilder(
|
||
listenable: _controller,
|
||
builder: (context, _) => _body(context),
|
||
);
|
||
|
||
Widget _body(BuildContext context) {
|
||
final status = _controller.status;
|
||
final enabled = status?.enabled ?? false;
|
||
final onChanged = status == null || _controller.busy
|
||
? null
|
||
: (bool value) => unawaited(_controller.setEnabled(value));
|
||
return SettingsGroup(
|
||
title: deviceDeliveryTitle,
|
||
footer: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
children: <Widget>[
|
||
const Text(deviceDeliveryNote),
|
||
if (_controller.error.isNotEmpty)
|
||
Text(
|
||
_controller.error,
|
||
key: DeviceDeliveryKeys.error,
|
||
style: TextStyle(color: settingsErrorColor(context)),
|
||
),
|
||
],
|
||
),
|
||
children: <Widget>[
|
||
SwitchListTile(
|
||
key: DeviceDeliveryKeys.toggle,
|
||
title: const Text(deviceDeliverySwitchLabel),
|
||
value: enabled,
|
||
onChanged: onChanged,
|
||
),
|
||
SettingsRow(
|
||
key: DeviceDeliveryKeys.status,
|
||
title: const Text('Status'),
|
||
subtitle: Text(status == null ? 'Loading…' : status.label),
|
||
),
|
||
if (status != null && enabled && !status.notificationsAllowed)
|
||
SettingsRow(
|
||
key: DeviceDeliveryKeys.permission,
|
||
title: const Text(deviceDeliveryNotificationsLabel),
|
||
subtitle: const Text('Android is blocking Pipelines notifications'),
|
||
action: true,
|
||
enabled: !_controller.busy,
|
||
onTap: () => unawaited(_controller.allowNotifications()),
|
||
),
|
||
if (status != null && enabled)
|
||
SettingsRow(
|
||
key: DeviceDeliveryKeys.battery,
|
||
title: const Text(deviceDeliveryBatteryLabel),
|
||
subtitle: Text(
|
||
status.batteryExempt
|
||
? 'Allowed: battery optimisation will not pause delivery'
|
||
: 'Keeps notifications instant while the phone is idle',
|
||
),
|
||
action: !status.batteryExempt,
|
||
enabled: !status.batteryExempt && !_controller.busy,
|
||
onTap: () => unawaited(_controller.allowBackgroundDelivery()),
|
||
),
|
||
if (status != null && enabled && !status.batteryExempt)
|
||
SettingsRow(
|
||
key: DeviceDeliveryKeys.batteryWarning,
|
||
leading: Icon(
|
||
Icons.warning_amber_rounded,
|
||
color: settingsErrorColor(context),
|
||
),
|
||
title: const Text(deviceDeliveryWarningTitle),
|
||
subtitle: Text(
|
||
'$deviceDeliveryWarningText\n$deviceDeliverySamsungNote',
|
||
),
|
||
),
|
||
if (status != null)
|
||
ExpansionTile(
|
||
key: DeviceDeliveryKeys.log,
|
||
title: const Text(deviceDeliveryLogTitle),
|
||
children: _logRows(),
|
||
),
|
||
],
|
||
);
|
||
}
|
||
|
||
List<Widget> _logRows() {
|
||
final events = _controller.events;
|
||
if (events.isEmpty) {
|
||
return const <Widget>[ListTile(title: Text(deviceDeliveryLogEmpty))];
|
||
}
|
||
return [
|
||
for (final event in events)
|
||
ListTile(
|
||
dense: true,
|
||
title: Text(event.title),
|
||
subtitle: Text(
|
||
[
|
||
event.timeLabel,
|
||
if (event.detail != null) event.detail!,
|
||
].join('\n'),
|
||
),
|
||
),
|
||
];
|
||
}
|
||
}
|