/* Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 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 http://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 'package:flutter/services.dart'; import 'hwlocation.dart'; import 'location.dart'; import 'location_availability.dart'; import 'location_callback.dart'; import 'location_request.dart'; import 'location_result.dart'; import 'location_settings_request.dart'; import 'location_settings_states.dart'; import 'navigation_request.dart'; import 'navigation_result.dart'; class FusedLocationProviderClient { static FusedLocationProviderClient _instance; final MethodChannel _methodChannel; final EventChannel _eventChannel; final Map _callbacks; Stream _onLocationData; FusedLocationProviderClient._create( this._methodChannel, this._eventChannel, this._callbacks, ) { _methodChannel.setMethodCallHandler(_methodCallHandler); } factory FusedLocationProviderClient() { if (_instance == null) { _instance = FusedLocationProviderClient._create( const MethodChannel( 'com.huawei.flutter.location/fusedlocation_methodchannel'), const EventChannel( 'com.huawei.flutter.location/fusedlocation_eventchannel'), {}, ); } return _instance; } Future _methodCallHandler(MethodCall methodCall) async { switch (methodCall.method) { case 'onLocationResult': _callbacks[methodCall.arguments['callbackId']].onLocationResult( LocationResult.fromMap(methodCall.arguments['locationResult'])); break; case 'onLocationAvailability': _callbacks[methodCall.arguments['callbackId']].onLocationAvailability( LocationAvailability.fromMap( methodCall.arguments['locationAvailability'])); break; default: break; } } Future checkLocationSettings( LocationSettingsRequest locationSettingsRequest) async { return LocationSettingsStates.fromMap( await _methodChannel.invokeMapMethod( 'checkLocationSettings', locationSettingsRequest.toMap())); } Future getLastLocation() async { return Location.fromMap(await _methodChannel .invokeMapMethod('getLastLocation')); } Future getLastLocationWithAddress( LocationRequest locationRequest) async { return HWLocation.fromMap( await _methodChannel.invokeMapMethod( 'getLastLocationWithAddress', locationRequest.toMap())); } Future getLocationAvailability() async { return LocationAvailability.fromMap(await _methodChannel .invokeMapMethod('getLocationAvailability')); } Future setMockMode(bool mockMode) async { return _methodChannel.invokeMethod('setMockMode', mockMode); } Future setMockLocation(Location location) async { return _methodChannel.invokeMethod( 'setMockLocation', location.toMap()); } Future requestLocationUpdates(LocationRequest locationRequest) async { return _methodChannel.invokeMethod( 'requestLocationUpdates', locationRequest.toMap()); } Future requestLocationUpdatesCb(LocationRequest locationRequest, LocationCallback locationCallback) async { int callbackId = await _methodChannel.invokeMethod( 'requestLocationUpdatesCb', locationRequest.toMap()); _callbacks.putIfAbsent(callbackId, () => locationCallback); return callbackId; } Future requestLocationUpdatesExCb(LocationRequest locationRequest, LocationCallback locationCallback) async { int callbackId = await _methodChannel.invokeMethod( 'requestLocationUpdatesExCb', (locationRequest..priority = LocationRequest.PRIORITY_HD_ACCURACY) .toMap()); _callbacks.putIfAbsent(callbackId, () => locationCallback); return callbackId; } Future removeLocationUpdates(int requestCode) async { return _methodChannel.invokeMethod( 'removeLocationUpdates', requestCode); } Future removeLocationUpdatesCb(int callbackId) async { await _methodChannel.invokeMethod( 'removeLocationUpdatesCb', callbackId); _callbacks.remove(callbackId); } Future getNavigationContextState( NavigationRequest navigationRequest) async { return NavigationResult.fromMap( await _methodChannel.invokeMapMethod( 'getNavigationContextState', navigationRequest.toMap())); } Stream get onLocationData { if (_onLocationData == null) { _onLocationData = _eventChannel .receiveBroadcastStream() .map((event) => Location.fromMap(event)); } return _onLocationData; } }