mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
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
This commit is contained in:
@@ -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<NativeFunction<InitMediatorType>>(
|
||||
'InitMediatorDart',
|
||||
)
|
||||
.asFunction();
|
||||
|
||||
late final CloseMediatorDartType closeMediator = _lib
|
||||
.lookup<NativeFunction<CloseMediatorType>>('CloseMediator')
|
||||
.asFunction();
|
||||
|
||||
late final StartFastPairScanningDartType startFastPairScanning = _lib
|
||||
.lookup<NativeFunction<StartFastPairScanningType>>('StartScanDart')
|
||||
.asFunction();
|
||||
|
||||
late final InitializeApiDartType initializeApi = _lib
|
||||
.lookup<NativeFunction<InitializeApiType>>('Dart_InitializeApiDL')
|
||||
.asFunction();
|
||||
|
||||
late final AddNotificationControllerObserverDartType
|
||||
addNotificationControllerObserver = _lib
|
||||
.lookup<NativeFunction<AddNotificationControllerObserverType>>(
|
||||
'AddNotificationControllerObserverDart')
|
||||
.asFunction();
|
||||
|
||||
late final RemoveNotificationControllerObserverDartType
|
||||
removeNotificationControllerObserver = _lib
|
||||
.lookup<NativeFunction<RemoveNotificationControllerObserverType>>(
|
||||
'RemoveNotificationControllerObserverDart')
|
||||
.asFunction();
|
||||
|
||||
late final DiscoveryClickedDartType discoveryClicked = _lib
|
||||
.lookup<NativeFunction<DiscoveryClickedType>>('DiscoveryClickedDart')
|
||||
.asFunction();
|
||||
}
|
||||
@@ -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'],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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<Uint64> Function();
|
||||
typedef InitMediatorDartType = Pointer<Uint64> Function();
|
||||
|
||||
typedef CloseMediatorType = Void Function(Pointer<Uint64> /*service*/);
|
||||
typedef CloseMediatorDartType = void Function(Pointer<Uint64> /*service*/);
|
||||
|
||||
typedef StartFastPairScanningType = Void Function(Pointer<Uint64> /*service*/);
|
||||
typedef StartFastPairScanningDartType = void Function(
|
||||
Pointer<Uint64> /*service*/);
|
||||
|
||||
typedef AddNotificationControllerObserverType = Void Function(
|
||||
Pointer<Uint64> /*service*/,
|
||||
Int64 /* callback*/,
|
||||
);
|
||||
typedef AddNotificationControllerObserverDartType = void Function(
|
||||
Pointer<Uint64> /*service*/,
|
||||
int /* callback*/,
|
||||
);
|
||||
|
||||
typedef RemoveNotificationControllerObserverType = Void Function(
|
||||
Pointer<Uint64> /*service*/,
|
||||
Int64 /* callback*/,
|
||||
);
|
||||
typedef RemoveNotificationControllerObserverDartType = void Function(
|
||||
Pointer<Uint64> /*service*/,
|
||||
int /* callback*/,
|
||||
);
|
||||
|
||||
typedef DiscoveryClickedType = Void Function(
|
||||
Pointer<Uint64> /*service*/,
|
||||
Int64 /* callback*/,
|
||||
);
|
||||
typedef DiscoveryClickedDartType = void Function(
|
||||
Pointer<Uint64> /*service*/,
|
||||
int /* callback*/,
|
||||
);
|
||||
|
||||
typedef InitializeApiType = IntPtr Function(Pointer<Void>);
|
||||
typedef InitializeApiDartType = int Function(Pointer<Void>);
|
||||
|
||||
//callback type
|
||||
typedef OnMetadataChanged = void Function({
|
||||
required List<DeviceMetadata> deviceMetadata,
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user