Skip to content

Commit

Permalink
Merge branch 'main' into feature/gnss_satellites_in_view
Browse files Browse the repository at this point in the history
# Conflicts:
#	geolocator_android/CHANGELOG.md
  • Loading branch information
TimHoogstrate committed Nov 1, 2023
2 parents 655ca9f + c994ce6 commit 43301e6
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 41 deletions.
2 changes: 1 addition & 1 deletion geolocator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies:
sdk: flutter

geolocator_platform_interface: ^4.1.0
geolocator_android: ^4.4.0
geolocator_android: ^4.5.0
geolocator_apple: ^2.3.0
geolocator_web: ^2.2.0
geolocator_windows: ^0.2.2
Expand Down
6 changes: 5 additions & 1 deletion geolocator_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## 4.4.0
## 4.5.0

* Creates `AndroidPosition`, a child class of `Position` with Android specific properties.
* Adds the `satelliteCount` and `satellitesUsedInFix` to `AndroidPosition`.

## 4.4.0

- Adds `color` to `ForegroundNotificationConfig` to set the color of the notification icon.

## 4.3.3

- Removes deprecated support for Android V1 embedding from the JAVA code base. Note that the geolocator's Flutter version restrictions already don't support V1 embedding anymore.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

Expand All @@ -24,10 +25,10 @@ public class BackgroundNotification {
private NotificationCompat.Builder builder;

public BackgroundNotification(
Context context,
String channelId ,
Integer notificationId,
ForegroundNotificationOptions options
@NonNull Context context,
@NonNull String channelId ,
@NonNull Integer notificationId,
ForegroundNotificationOptions options
) {
this.context = context;
this.notificationId = notificationId;
Expand Down Expand Up @@ -88,6 +89,12 @@ private void updateNotification(
.setContentIntent(buildBringToFrontIntent())
.setOngoing(options.isSetOngoing());

@Nullable final Integer notificationColor = options.getColor();
if (notificationColor != null) {
builder = builder
.setColor(notificationColor);
}

if (notify) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId, builder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,69 @@ public class ForegroundNotificationOptions {
private final String notificationText;
@NonNull
private final AndroidIconResource notificationIcon;
@NonNull
private final boolean enableWifiLock;
@NonNull
private final boolean enableWakeLock;

@NonNull
private final boolean setOngoing;
@Nullable
private final Integer color;


public static ForegroundNotificationOptions parseArguments(@Nullable Map<String, Object> arguments) {
if (arguments == null) {
return null;
}

final AndroidIconResource notificationIcon = AndroidIconResource.parseArguments((Map<String, Object>)arguments.get("notificationIcon"));
final AndroidIconResource notificationIcon = AndroidIconResource.parseArguments((Map<String, Object>)arguments.get("notificationIcon"));
final String notificationTitle = (String) arguments.get("notificationTitle");
final String notificationText = (String) arguments.get("notificationText");
final Boolean enableWifiLock = (Boolean) arguments.get("enableWifiLock");
final Boolean enableWakeLock = (Boolean) arguments.get("enableWakeLock");
final Boolean setOngoing = (Boolean) arguments.get("setOngoing");

@Nullable Integer color = null;
final Object colorObject = arguments.get("color");
if (colorObject != null) {
color = ((Number) colorObject).intValue();
}

return new ForegroundNotificationOptions(
notificationTitle,
notificationText,
notificationIcon,
enableWifiLock,
enableWakeLock,
setOngoing);
setOngoing,
color);
}

private ForegroundNotificationOptions(String notificationTitle, String notificationText, AndroidIconResource notificationIcon, boolean enableWifiLock, boolean enableWakeLock, boolean setOngoing) {
private ForegroundNotificationOptions(
@NonNull String notificationTitle,
@NonNull String notificationText,
@NonNull AndroidIconResource notificationIcon,
boolean enableWifiLock,
boolean enableWakeLock,
boolean setOngoing,
@Nullable Integer color) {
this.notificationTitle = notificationTitle;
this.notificationText = notificationText;
this.notificationIcon = notificationIcon;
this.enableWifiLock = enableWifiLock;
this.enableWakeLock = enableWakeLock;
this.setOngoing = setOngoing;
this.color = color;
}

@NonNull
public String getNotificationTitle() {
return notificationTitle;
}

@NonNull
public String getNotificationText() {
return notificationText;
}

@NonNull
public AndroidIconResource getNotificationIcon() {
return notificationIcon;
}
Expand All @@ -78,4 +93,7 @@ public boolean isSetOngoing() {
return setOngoing;
}

@Nullable public Integer getColor() {
return color;
}
}
12 changes: 7 additions & 5 deletions geolocator_android/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main() {
runApp(const GeolocatorWidget());
}

/// Example [Widget] showing the functionalities of the geolocator plugin
/// Example [Widget] showing the functionalities of the geolocator plugin.
class GeolocatorWidget extends StatefulWidget {
/// Creates a new GeolocatorWidget.
const GeolocatorWidget({Key? key}) : super(key: key);
Expand Down Expand Up @@ -297,13 +297,15 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
forceLocationManager: false,
useMSLAltitude: true,
foregroundNotificationConfig: const ForegroundNotificationConfig(
// Explain to the user why we are showing this notification.
notificationText:
"Example app will continue to receive your location even when you aren't using it",
//Explain to the user why we are showing this notification
// Tell the user what we are doing.
notificationTitle: "Running in Background",
//Tell the user what we are doing
enableWakeLock:
false, //Keep the system awake to receive background location information.
// Keep the system awake to receive background location information.
enableWakeLock: false,
// Give the notification an amber color.
color: Colors.amber,
),
);
final positionStream = geolocatorAndroid.getPositionStream(
Expand Down
47 changes: 38 additions & 9 deletions geolocator_android/lib/src/types/foreground_settings.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import 'dart:ui';

/// Uniquely identifies an Android resource.
class AndroidResource {
/// The name of the desired resource.
final String name;

/// Optional default resource type to find, if "type/" is not included in the name. Can be null to require an explicit type.
/// Optional default resource type to find, if "type/" is not included in the name.
///
/// Can be null to require an explicit type.
final String defType;

/// Uniquely identifies an Android resource.
const AndroidResource({required this.name, this.defType = 'drawable'});
const AndroidResource({
required this.name,
this.defType = 'drawable',
});

/// Returns a JSON representation of this class.
Map<String, dynamic> toJson() {
Expand Down Expand Up @@ -51,16 +58,36 @@ class ForegroundNotificationConfig {
/// the user cannot dismiss it
final bool setOngoing;

/// Accent color (an ARGB integer like the constants in Color) to be applied
/// by the standard Style templates when presenting this notification.
///
/// The current template design constructs a colorful header image by
/// overlaying the icon image (stenciled in white) atop a field of this color.
/// Alpha components are ignored.
///
/// If this color is null, a system default color is used.
final Color? color;

/// Creates an Android specific configuration for the [FlutterBackground] plugin.
///
/// [notificationTitle] is the title used for the foreground service notification.
/// [notificationText] is the body used for the foreground service notification.
/// [notificationTitle] is the title used for the foreground service
/// notification.
/// [notificationText] is the body used for the foreground service
/// notification.
/// [notificationIcon] must be a drawable resource.
/// E. g. if the icon with name "background_icon" is in the "drawable" resource folder,
/// it should be of value `AndroidResource(name: 'background_icon', defType: 'drawable').
/// E. g. if the icon with name "background_icon" is in the "drawable"
/// resource folder, it should be of value
/// `AndroidResource(name: 'background_icon', defType: 'drawable').
/// [enableWifiLock] indicates wether or not a WifiLock is acquired, when the
/// background execution is started. This allows the application to keep the
/// Wi-Fi radio awake, even when the user has not used the device in a while.
/// [enableWakeLock] indicates wether or not a Wakelock is acquired, when the
/// background execution is started. If this is false then the system can
/// still sleep and all location events will be received at once when the
/// system wakes up again.
/// [setOngoing] indicates wether or not the displayed notification is
/// persistent and the user cannot dismiss it.
/// [color] is the accent color that is applied to the [notificationIcon].
const ForegroundNotificationConfig({
required this.notificationTitle,
required this.notificationText,
Expand All @@ -69,17 +96,19 @@ class ForegroundNotificationConfig {
this.enableWifiLock = false,
this.enableWakeLock = false,
this.setOngoing = false,
this.color,
});

/// Returns a JSON representation of this class.
Map<String, dynamic> toJson() {
return {
'enableWakeLock': enableWakeLock,
'enableWifiLock': enableWifiLock,
'notificationTitle': notificationTitle,
'notificationText': notificationText,
'notificationIcon': notificationIcon.toJson(),
'enableWifiLock': enableWifiLock,
'enableWakeLock': enableWakeLock,
'notificationText': notificationText,
'setOngoing': setOngoing,
'color': color?.value,
};
}
}
2 changes: 1 addition & 1 deletion geolocator_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: geolocator_android
description: Geolocation plugin for Flutter. This plugin provides the Android implementation for the geolocator.
repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator_android
issue_tracker: https://github.com/baseflow/flutter-geolocator/issues?q=is%3Aissue+is%3Aopen
version: 4.4.0
version: 4.5.0

environment:
sdk: ">=2.15.0 <4.0.0"
Expand Down
66 changes: 53 additions & 13 deletions geolocator_android/test/geolocator_android_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:async/async.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:geolocator_android/geolocator_android.dart';
Expand Down Expand Up @@ -1435,19 +1436,25 @@ void main() {
() async {
// Arrange
final settings = AndroidSettings(
accuracy: LocationAccuracy.best,
distanceFilter: 5,
forceLocationManager: false,
intervalDuration: const Duration(seconds: 1),
timeLimit: const Duration(seconds: 1),
useMSLAltitude: false,
foregroundNotificationConfig: const ForegroundNotificationConfig(
notificationText: 'text',
notificationTitle: 'title',
enableWakeLock: false,
enableWifiLock: false,
notificationIcon:
AndroidResource(name: 'name', defType: 'defType')));
accuracy: LocationAccuracy.best,
distanceFilter: 5,
forceLocationManager: false,
intervalDuration: const Duration(seconds: 1),
timeLimit: const Duration(seconds: 1),
useMSLAltitude: false,
foregroundNotificationConfig: const ForegroundNotificationConfig(
color: Colors.amber,
enableWakeLock: false,
enableWifiLock: false,
notificationIcon: AndroidResource(
name: 'name',
defType: 'defType',
),
notificationText: 'text',
notificationTitle: 'title',
setOngoing: true,
),
);

// Act
final jsonMap = settings.toJson();
Expand All @@ -1473,6 +1480,39 @@ void main() {
jsonMap['useMSLAltitude'],
settings.useMSLAltitude,
);
expect(
jsonMap['foregroundNotificationConfig']['enableWakeLock'],
settings.foregroundNotificationConfig!.enableWakeLock,
);
expect(
jsonMap['foregroundNotificationConfig']['enableWifiLock'],
settings.foregroundNotificationConfig!.enableWifiLock,
);
expect(
jsonMap['foregroundNotificationConfig']['notificationIcon']['name'],
settings.foregroundNotificationConfig!.notificationIcon.name,
);
expect(
jsonMap['foregroundNotificationConfig']['notificationIcon']
['defType'],
settings.foregroundNotificationConfig!.notificationIcon.defType,
);
expect(
jsonMap['foregroundNotificationConfig']['notificationText'],
settings.foregroundNotificationConfig!.notificationText,
);
expect(
jsonMap['foregroundNotificationConfig']['notificationTitle'],
settings.foregroundNotificationConfig!.notificationTitle,
);
expect(
jsonMap['foregroundNotificationConfig']['setOngoing'],
settings.foregroundNotificationConfig!.setOngoing,
);
expect(
jsonMap['foregroundNotificationConfig']['color'],
settings.foregroundNotificationConfig!.color!.value,
);
});

test('Should receive false if an error occurred', () async {
Expand Down

0 comments on commit 43301e6

Please sign in to comment.