Skip to content

Commit 197d976

Browse files
authored
[android_alarm_manager] Migrated android_alarm_manager to support null safety #75233 (flutter#3499)
Migrated android_alarm_manager to support null safety. Fixes flutter/flutter#75233
1 parent 99d599c commit 197d976

File tree

9 files changed

+34
-41
lines changed

9 files changed

+34
-41
lines changed

packages/android_alarm_manager/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.0-nullsafety
2+
3+
* Migrate to null safety.
4+
15
## 0.4.5+20
26

37
* Update the example app: remove the deprecated `RaisedButton` and `FlatButton` widgets.

packages/android_alarm_manager/README.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
A Flutter plugin for accessing the Android AlarmManager service, and running
66
Dart code in the background when alarms fire.
77

8-
**Please set your constraint to `android_alarm_manager: '>=0.4.y+x <2.0.0'`**
9-
10-
## Backward compatible 1.0.0 version is coming
11-
The plugin has reached a stable API, we guarantee that version `1.0.0` will be backward compatible with `0.4.y+z`.
12-
Please use `android_alarm_manager: '>=0.4.y+x <2.0.0'` as your dependency constraint to allow a smoother ecosystem migration.
13-
For more details see: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
14-
158
## Getting Started
169

1710
After importing this plugin to your project as usual, add the following to your
@@ -109,17 +102,12 @@ Which must be reflected in the application's `AndroidManifest.xml`. E.g.:
109102
**Note:** Not calling `AlarmService.setPluginRegistrant` will result in an exception being
110103
thrown when an alarm eventually fires.
111104

112-
### Flutter Android Embedding V2 (Flutter Version >= 1.12)
105+
### Flutter Android Embedding V2
113106

114107
For the Flutter Android Embedding V2, plugins are registered with the background
115108
isolate via reflection so `AlarmService.setPluginRegistrant` does not need to be
116109
called.
117110

118-
**NOTE: this plugin is not completely compatible with the V2 embedding on
119-
Flutter versions < 1.12 as the background isolate will not automatically
120-
register plugins. This can be resolved by running `flutter upgrade` to upgrade
121-
to the latest Flutter version.**
122-
123111
For help getting started with Flutter, view our online
124112
[documentation](https://flutter.dev/).
125113

packages/android_alarm_manager/example/integration_test/android_alarm_manager_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
// @dart=2.9
6+
57
import 'dart:async';
68
import 'dart:io';
79
import 'package:android_alarm_manager_example/main.dart' as app;

packages/android_alarm_manager/example/lib/main.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const String isolateName = 'isolate';
2222
final ReceivePort port = ReceivePort();
2323

2424
/// Global [SharedPreferences] object.
25-
SharedPreferences prefs;
25+
late SharedPreferences prefs;
2626

2727
Future<void> main() async {
2828
// TODO(bkonyi): uncomment
@@ -54,7 +54,7 @@ class AlarmManagerExampleApp extends StatelessWidget {
5454
}
5555

5656
class _AlarmHomePage extends StatefulWidget {
57-
_AlarmHomePage({Key key, this.title}) : super(key: key);
57+
_AlarmHomePage({Key? key, required this.title}) : super(key: key);
5858
final String title;
5959

6060
@override
@@ -86,15 +86,15 @@ class _AlarmHomePageState extends State<_AlarmHomePage> {
8686
}
8787

8888
// The background
89-
static SendPort uiSendPort;
89+
static SendPort? uiSendPort;
9090

9191
// The callback for our alarm
9292
static Future<void> callback() async {
9393
print('Alarm fired!');
9494

9595
// Get the previous cached count and increment it.
9696
final prefs = await SharedPreferences.getInstance();
97-
int currentCount = prefs.getInt(countKey);
97+
int currentCount = prefs.getInt(countKey) ?? 0;
9898
await prefs.setInt(countKey, currentCount + 1);
9999

100100
// This will be null if we're running in the background.
@@ -140,7 +140,7 @@ class _AlarmHomePageState extends State<_AlarmHomePage> {
140140
await AndroidAlarmManager.oneShot(
141141
const Duration(seconds: 5),
142142
// Ensure we have a unique alarm ID.
143-
Random().nextInt(pow(2, 31)),
143+
Random().nextInt(pow(2, 31).toInt()),
144144
callback,
145145
exact: true,
146146
wakeup: true,

packages/android_alarm_manager/example/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ dependencies:
1111
# The example app is bundled with the plugin so we use a path dependency on
1212
# the parent directory to use the current plugin's version.
1313
path: ../
14-
shared_preferences: ^0.5.6
14+
shared_preferences: ^2.0.0-nullsafety
1515
integration_test:
1616
path: ../../integration_test
17-
path_provider: ^1.3.1
17+
path_provider: ^2.0.0-nullsafety
1818

1919
dev_dependencies:
2020
espresso: ^0.0.1+3
@@ -28,5 +28,5 @@ flutter:
2828
uses-material-design: true
2929

3030
environment:
31-
sdk: ">=2.1.0 <3.0.0"
31+
sdk: '>=2.12.0-0 <3.0.0'
3232
flutter: ">=1.12.13+hotfix.5"

packages/android_alarm_manager/example/test_driver/integration_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
// @dart=2.9
6+
57
import 'dart:async';
68
import 'dart:convert';
79
import 'dart:io';

packages/android_alarm_manager/lib/android_alarm_manager.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void _alarmManagerCallbackDispatcher() {
3131

3232
// PluginUtilities.getCallbackFromHandle performs a lookup based on the
3333
// callback handle and returns a tear-off of the original callback.
34-
final Function closure = PluginUtilities.getCallbackFromHandle(handle);
34+
final Function? closure = PluginUtilities.getCallbackFromHandle(handle);
3535

3636
if (closure == null) {
3737
print('Fatal: could not find callback');
@@ -56,7 +56,7 @@ void _alarmManagerCallbackDispatcher() {
5656
// A lambda that returns the current instant in the form of a [DateTime].
5757
typedef DateTime _Now();
5858
// A lambda that gets the handle for the given [callback].
59-
typedef CallbackHandle _GetCallbackHandle(Function callback);
59+
typedef CallbackHandle? _GetCallbackHandle(Function callback);
6060

6161
/// A Flutter plugin for registering Dart callbacks with the Android
6262
/// AlarmManager service.
@@ -77,7 +77,7 @@ class AndroidAlarmManager {
7777
/// the plugin.
7878
@visibleForTesting
7979
static void setTestOverides(
80-
{_Now now, _GetCallbackHandle getCallbackHandle}) {
80+
{_Now? now, _GetCallbackHandle? getCallbackHandle}) {
8181
_now = (now ?? _now);
8282
_getCallbackHandle = (getCallbackHandle ?? _getCallbackHandle);
8383
}
@@ -88,12 +88,12 @@ class AndroidAlarmManager {
8888
/// Returns a [Future] that resolves to `true` on success and `false` on
8989
/// failure.
9090
static Future<bool> initialize() async {
91-
final CallbackHandle handle =
91+
final CallbackHandle? handle =
9292
_getCallbackHandle(_alarmManagerCallbackDispatcher);
9393
if (handle == null) {
9494
return false;
9595
}
96-
final bool r = await _channel.invokeMethod<bool>(
96+
final bool? r = await _channel.invokeMethod<bool>(
9797
'AlarmService.start', <dynamic>[handle.toRawHandle()]);
9898
return r ?? false;
9999
}
@@ -207,11 +207,11 @@ class AndroidAlarmManager {
207207
assert(callback is Function() || callback is Function(int));
208208
assert(id.bitLength < 32);
209209
final int startMillis = time.millisecondsSinceEpoch;
210-
final CallbackHandle handle = _getCallbackHandle(callback);
210+
final CallbackHandle? handle = _getCallbackHandle(callback);
211211
if (handle == null) {
212212
return false;
213213
}
214-
final bool r =
214+
final bool? r =
215215
await _channel.invokeMethod<bool>('Alarm.oneShotAt', <dynamic>[
216216
id,
217217
alarmClock,
@@ -222,7 +222,7 @@ class AndroidAlarmManager {
222222
rescheduleOnReboot,
223223
handle.toRawHandle(),
224224
]);
225-
return (r == null) ? false : r;
225+
return r ?? false;
226226
}
227227

228228
/// Schedules a repeating timer to run `callback` with period `duration`.
@@ -262,7 +262,7 @@ class AndroidAlarmManager {
262262
Duration duration,
263263
int id,
264264
Function callback, {
265-
DateTime startAt,
265+
DateTime? startAt,
266266
bool exact = false,
267267
bool wakeup = false,
268268
bool rescheduleOnReboot = false,
@@ -274,11 +274,11 @@ class AndroidAlarmManager {
274274
final int period = duration.inMilliseconds;
275275
final int first =
276276
startAt != null ? startAt.millisecondsSinceEpoch : now + period;
277-
final CallbackHandle handle = _getCallbackHandle(callback);
277+
final CallbackHandle? handle = _getCallbackHandle(callback);
278278
if (handle == null) {
279279
return false;
280280
}
281-
final bool r = await _channel.invokeMethod<bool>(
281+
final bool? r = await _channel.invokeMethod<bool>(
282282
'Alarm.periodic', <dynamic>[
283283
id,
284284
exact,
@@ -288,7 +288,7 @@ class AndroidAlarmManager {
288288
rescheduleOnReboot,
289289
handle.toRawHandle()
290290
]);
291-
return (r == null) ? false : r;
291+
return r ?? false;
292292
}
293293

294294
/// Cancels a timer.
@@ -299,8 +299,8 @@ class AndroidAlarmManager {
299299
/// Returns a [Future] that resolves to `true` on success and `false` on
300300
/// failure.
301301
static Future<bool> cancel(int id) async {
302-
final bool r =
302+
final bool? r =
303303
await _channel.invokeMethod<bool>('Alarm.cancel', <dynamic>[id]);
304-
return (r == null) ? false : r;
304+
return r ?? false;
305305
}
306306
}

packages/android_alarm_manager/pubspec.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
name: android_alarm_manager
22
description: Flutter plugin for accessing the Android AlarmManager service, and
33
running Dart code in the background when alarms fire.
4-
# 0.4.y+z is compatible with 1.0.0, if you land a breaking change bump
5-
# the version to 2.0.0.
6-
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
7-
version: 0.4.5+20
4+
version: 2.0.0-nullsafety
85
homepage: https://github.com/flutter/plugins/tree/master/packages/android_alarm_manager
96

107
dependencies:
@@ -24,5 +21,5 @@ flutter:
2421
pluginClass: AndroidAlarmManagerPlugin
2522

2623
environment:
27-
sdk: ">=2.1.0 <3.0.0"
24+
sdk: '>=2.12.0-0 <3.0.0'
2825
flutter: ">=1.12.13+hotfix.5"

script/nnbd_plugins.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# null-safe is available on stable.
66

77
readonly NNBD_PLUGINS_LIST=(
8+
"android_alarm_manager"
89
"android_intent"
910
"battery"
1011
"camera"
@@ -36,7 +37,6 @@ readonly NNBD_PLUGINS_LIST=(
3637
# building the all plugins app. This list should be kept empty.
3738

3839
readonly NON_NNBD_PLUGINS_LIST=(
39-
# "android_alarm_manager"
4040
"camera"
4141
# "google_maps_flutter"
4242
# "image_picker"

0 commit comments

Comments
 (0)