This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[google_maps_flutter] Add support to request map renderer for Android #6619
Merged
auto-submit
merged 4 commits into
flutter:main
from
CodemateLtd:google_maps_flutter_preferred_renderer
Dec 7, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
975f6cc
[google_maps_flutter] support to request a specific map renderer for …
jokerttu 44f33b1
[google_maps_flutter] Minor fixes to comments and error messages
jokerttu 65a5c17
Merge branch 'main' into google_maps_flutter_preferred_renderer
jokerttu c637317
[google_maps_flutter] fix linting issues
jokerttu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
packages/google_maps_flutter/google_maps_flutter_android/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...ter_android/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapInitializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.googlemaps; | ||
|
|
||
| import android.content.Context; | ||
| import androidx.annotation.VisibleForTesting; | ||
| import com.google.android.gms.maps.MapsInitializer; | ||
| import com.google.android.gms.maps.MapsInitializer.Renderer; | ||
| import com.google.android.gms.maps.OnMapsSdkInitializedCallback; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugin.common.MethodCall; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
|
|
||
| /** GoogleMaps initializer used to initialize the Google Maps SDK with preferred settings. */ | ||
| final class GoogleMapInitializer | ||
| implements OnMapsSdkInitializedCallback, MethodChannel.MethodCallHandler { | ||
| private final MethodChannel methodChannel; | ||
| private final Context context; | ||
| private static MethodChannel.Result initializationResult; | ||
| private boolean rendererInitialized = false; | ||
|
|
||
| GoogleMapInitializer(Context context, BinaryMessenger binaryMessenger) { | ||
| this.context = context; | ||
|
|
||
| methodChannel = | ||
| new MethodChannel(binaryMessenger, "plugins.flutter.dev/google_maps_android_initializer"); | ||
| methodChannel.setMethodCallHandler(this); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMethodCall(MethodCall call, MethodChannel.Result result) { | ||
| switch (call.method) { | ||
| case "initializer#preferRenderer": | ||
| { | ||
| String preferredRenderer = (String) call.argument("value"); | ||
| initializeWithPreferredRenderer(preferredRenderer, result); | ||
| break; | ||
| } | ||
| default: | ||
| result.notImplemented(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Initializes map renderer to with preferred renderer type. Renderer can be initialized only once | ||
| * per application context. | ||
| * | ||
| * <p>Supported renderer types are "latest", "legacy" and "default". | ||
| */ | ||
| private void initializeWithPreferredRenderer( | ||
| String preferredRenderer, MethodChannel.Result result) { | ||
| if (rendererInitialized || initializationResult != null) { | ||
| result.error( | ||
| "Renderer already initialized", "Renderer initialization called multiple times", null); | ||
| } else { | ||
| initializationResult = result; | ||
| switch (preferredRenderer) { | ||
| case "latest": | ||
| initializeWithRendererRequest(Renderer.LATEST); | ||
| break; | ||
| case "legacy": | ||
| initializeWithRendererRequest(Renderer.LEGACY); | ||
| break; | ||
| case "default": | ||
| initializeWithRendererRequest(null); | ||
| break; | ||
| default: | ||
| initializationResult.error( | ||
| "Invalid renderer type", | ||
| "Renderer initialization called with invalid renderer type", | ||
| null); | ||
| initializationResult = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Initializes map renderer to with preferred renderer type. | ||
| * | ||
| * <p>This method is visible for testing purposes only and should never be used outside this | ||
| * class. | ||
| */ | ||
| @VisibleForTesting | ||
| public void initializeWithRendererRequest(MapsInitializer.Renderer renderer) { | ||
| MapsInitializer.initialize(context, renderer, this); | ||
| } | ||
|
|
||
| /** Is called by Google Maps SDK to determine which version of the renderer was initialized. */ | ||
| @Override | ||
| public void onMapsSdkInitialized(MapsInitializer.Renderer renderer) { | ||
| rendererInitialized = true; | ||
| if (initializationResult != null) { | ||
| switch (renderer) { | ||
| case LATEST: | ||
| initializationResult.success("latest"); | ||
| break; | ||
| case LEGACY: | ||
| initializationResult.success("legacy"); | ||
| break; | ||
| default: | ||
| initializationResult.error( | ||
| "Unknown renderer type", "Initialized with unknown renderer type", null); | ||
| } | ||
| initializationResult = null; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...android/android/src/test/java/io/flutter/plugins/googlemaps/GoogleMapInitializerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.googlemaps; | ||
|
|
||
| import static org.mockito.Mockito.any; | ||
| import static org.mockito.Mockito.doNothing; | ||
| import static org.mockito.Mockito.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| import android.content.Context; | ||
| import android.os.Build; | ||
| import androidx.test.core.app.ApplicationProvider; | ||
| import com.google.android.gms.maps.MapsInitializer.Renderer; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugin.common.MethodCall; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
| import java.util.HashMap; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockitoAnnotations; | ||
| import org.robolectric.RobolectricTestRunner; | ||
| import org.robolectric.annotation.Config; | ||
|
|
||
| @RunWith(RobolectricTestRunner.class) | ||
| @Config(sdk = Build.VERSION_CODES.P) | ||
| public class GoogleMapInitializerTest { | ||
| private GoogleMapInitializer googleMapInitializer; | ||
|
|
||
| @Mock BinaryMessenger mockMessenger; | ||
|
|
||
| @Before | ||
| public void before() { | ||
| MockitoAnnotations.openMocks(this); | ||
| Context context = ApplicationProvider.getApplicationContext(); | ||
| googleMapInitializer = spy(new GoogleMapInitializer(context, mockMessenger)); | ||
| } | ||
|
|
||
| @Test | ||
| public void initializer_OnMapsSdkInitializedWithLatestRenderer() { | ||
| doNothing().when(googleMapInitializer).initializeWithRendererRequest(Renderer.LATEST); | ||
| MethodChannel.Result result = mock(MethodChannel.Result.class); | ||
| googleMapInitializer.onMethodCall( | ||
| new MethodCall( | ||
| "initializer#preferRenderer", | ||
| new HashMap<String, Object>() { | ||
| { | ||
| put("value", "latest"); | ||
| } | ||
| }), | ||
| result); | ||
| googleMapInitializer.onMapsSdkInitialized(Renderer.LATEST); | ||
| verify(result, times(1)).success("latest"); | ||
| verify(result, never()).error(any(), any(), any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void initializer_OnMapsSdkInitializedWithLegacyRenderer() { | ||
| doNothing().when(googleMapInitializer).initializeWithRendererRequest(Renderer.LEGACY); | ||
| MethodChannel.Result result = mock(MethodChannel.Result.class); | ||
| googleMapInitializer.onMethodCall( | ||
| new MethodCall( | ||
| "initializer#preferRenderer", | ||
| new HashMap<String, Object>() { | ||
| { | ||
| put("value", "legacy"); | ||
| } | ||
| }), | ||
| result); | ||
| googleMapInitializer.onMapsSdkInitialized(Renderer.LEGACY); | ||
| verify(result, times(1)).success("legacy"); | ||
| verify(result, never()).error(any(), any(), any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void initializer_onMethodCallWithUnknownRenderer() { | ||
| doNothing().when(googleMapInitializer).initializeWithRendererRequest(Renderer.LEGACY); | ||
| MethodChannel.Result result = mock(MethodChannel.Result.class); | ||
| googleMapInitializer.onMethodCall( | ||
| new MethodCall( | ||
| "initializer#preferRenderer", | ||
| new HashMap<String, Object>() { | ||
| { | ||
| put("value", "wrong_renderer"); | ||
| } | ||
| }), | ||
| result); | ||
| verify(result, never()).success(any()); | ||
| verify(result, times(1)).error(eq("Invalid renderer type"), any(), any()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...ps_flutter/google_maps_flutter_android/example/integration_test/latest_renderer_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:google_maps_flutter_android/google_maps_flutter_android.dart'; | ||
| import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'; | ||
| import 'package:integration_test/integration_test.dart'; | ||
|
|
||
| import 'google_maps_tests.dart' show googleMapsTests; | ||
|
|
||
| void main() { | ||
| late AndroidMapRenderer initializedRenderer; | ||
| IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| setUpAll(() async { | ||
| final GoogleMapsFlutterAndroid instance = | ||
| GoogleMapsFlutterPlatform.instance as GoogleMapsFlutterAndroid; | ||
| initializedRenderer = | ||
| await instance.initializeWithRenderer(AndroidMapRenderer.latest); | ||
| }); | ||
|
|
||
| testWidgets('initialized with latest renderer', (WidgetTester _) async { | ||
| expect(initializedRenderer, AndroidMapRenderer.latest); | ||
| }); | ||
|
|
||
| testWidgets('throws PlatformException on multiple renderer initializations', | ||
| (WidgetTester _) async { | ||
| final GoogleMapsFlutterAndroid instance = | ||
| GoogleMapsFlutterPlatform.instance as GoogleMapsFlutterAndroid; | ||
| expect( | ||
| () async => instance.initializeWithRenderer(AndroidMapRenderer.latest), | ||
| throwsA(isA<PlatformException>().having((PlatformException e) => e.code, | ||
| 'code', 'Renderer already initialized'))); | ||
| }); | ||
|
|
||
| // Run tests. | ||
| googleMapsTests(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is done, as otherwise test fails with latest renderer, as bounds with zero area is given at start.