From b5f1added45f9fb6513eebb73f7a8bc819bf59c1 Mon Sep 17 00:00:00 2001 From: hai007 Date: Thu, 15 Jun 2023 11:42:49 -0700 Subject: [PATCH] Adding components for binding the C++ libraries with flutter app - Branch class Listener from google3/location/nearby/cpp/sharing/clients/dart/platform/lib/listener.dart for further use of listening the change from notification controller. - Create Device Metadata to store the device information relating to UI showing. - Create binding class to bind C++ libraries to dart language. PiperOrigin-RevId: 540644654 --- fastpair/dart/binding/lib/bindings.dart | 55 ++++++++++++++++ .../dart/binding/lib/device_metadata.dart | 36 +++++++++++ fastpair/dart/binding/lib/ffi_types.dart | 62 +++++++++++++++++++ fastpair/dart/binding/lib/listener.dart | 36 +++++++++++ 4 files changed, 189 insertions(+) create mode 100644 fastpair/dart/binding/lib/bindings.dart create mode 100644 fastpair/dart/binding/lib/device_metadata.dart create mode 100644 fastpair/dart/binding/lib/ffi_types.dart create mode 100644 fastpair/dart/binding/lib/listener.dart diff --git a/fastpair/dart/binding/lib/bindings.dart b/fastpair/dart/binding/lib/bindings.dart new file mode 100644 index 00000000..2171d00f --- /dev/null +++ b/fastpair/dart/binding/lib/bindings.dart @@ -0,0 +1,55 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import 'dart:ffi'; + +import 'ffi_types.dart'; + +class FastpairBinding { + static final _lib = DynamicLibrary.open('fastpair_dart.dll'); + + late final InitMediatorDartType initMediator = _lib + .lookup>( + 'InitMediatorDart', + ) + .asFunction(); + + late final CloseMediatorDartType closeMediator = _lib + .lookup>('CloseMediator') + .asFunction(); + + late final StartFastPairScanningDartType startFastPairScanning = _lib + .lookup>('StartScanDart') + .asFunction(); + + late final InitializeApiDartType initializeApi = _lib + .lookup>('Dart_InitializeApiDL') + .asFunction(); + + late final AddNotificationControllerObserverDartType + addNotificationControllerObserver = _lib + .lookup>( + 'AddNotificationControllerObserverDart') + .asFunction(); + + late final RemoveNotificationControllerObserverDartType + removeNotificationControllerObserver = _lib + .lookup>( + 'RemoveNotificationControllerObserverDart') + .asFunction(); + + late final DiscoveryClickedDartType discoveryClicked = _lib + .lookup>('DiscoveryClickedDart') + .asFunction(); +} diff --git a/fastpair/dart/binding/lib/device_metadata.dart b/fastpair/dart/binding/lib/device_metadata.dart new file mode 100644 index 00000000..d1c2810f --- /dev/null +++ b/fastpair/dart/binding/lib/device_metadata.dart @@ -0,0 +1,36 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Detail information of a observed device. Compared to the device information +// get from GetObservedDeviceResponse, this class only store the device +// information that is related to UI showing. +class DeviceMetadata { + String id; + String name; + String imageUrl; + + DeviceMetadata({ + required this.id, + required this.name, + required this.imageUrl, + }); + + factory DeviceMetadata.fromJson(dynamic data) { + return DeviceMetadata( + id: data['id'], + name: data['name'] == '' ? null : data['name'], + imageUrl: data['imageUrl'] == '' ? null : data['imageUrl'], + ); + } +} diff --git a/fastpair/dart/binding/lib/ffi_types.dart b/fastpair/dart/binding/lib/ffi_types.dart new file mode 100644 index 00000000..f92b402a --- /dev/null +++ b/fastpair/dart/binding/lib/ffi_types.dart @@ -0,0 +1,62 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import 'dart:ffi'; + +import 'device_metadata.dart'; + +typedef InitMediatorType = Pointer Function(); +typedef InitMediatorDartType = Pointer Function(); + +typedef CloseMediatorType = Void Function(Pointer /*service*/); +typedef CloseMediatorDartType = void Function(Pointer /*service*/); + +typedef StartFastPairScanningType = Void Function(Pointer /*service*/); +typedef StartFastPairScanningDartType = void Function( + Pointer /*service*/); + +typedef AddNotificationControllerObserverType = Void Function( + Pointer /*service*/, + Int64 /* callback*/, +); +typedef AddNotificationControllerObserverDartType = void Function( + Pointer /*service*/, + int /* callback*/, +); + +typedef RemoveNotificationControllerObserverType = Void Function( + Pointer /*service*/, + Int64 /* callback*/, +); +typedef RemoveNotificationControllerObserverDartType = void Function( + Pointer /*service*/, + int /* callback*/, +); + +typedef DiscoveryClickedType = Void Function( + Pointer /*service*/, + Int64 /* callback*/, +); +typedef DiscoveryClickedDartType = void Function( + Pointer /*service*/, + int /* callback*/, +); + +typedef InitializeApiType = IntPtr Function(Pointer); +typedef InitializeApiDartType = int Function(Pointer); + +//callback type +typedef OnMetadataChanged = void Function({ + required List deviceMetadata, +}); diff --git a/fastpair/dart/binding/lib/listener.dart b/fastpair/dart/binding/lib/listener.dart new file mode 100644 index 00000000..a264ecb7 --- /dev/null +++ b/fastpair/dart/binding/lib/listener.dart @@ -0,0 +1,36 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import 'dart:ffi'; +import 'dart:isolate'; + +import 'package:mobile.flutter.contrib.disposable/disposable.dart'; + +/// A convenience class for listening to a [ReceivePort]. +/// +/// This can be used to listen to ongoing updates from the c++ layer. +class Listener with AutoDisposerMixin { + Listener() { + autoDisposeCustom(_port.close); + autoDisposeCustom(_subscription.cancel); + } + + final _port = ReceivePort(); + late final _subscription = _port.listen(null); + + int get port => _port.sendPort.nativePort; + + void onData(void Function(dynamic data)? handleData) => + _subscription.onData(handleData); +}