Skip to content

Commit

Permalink
[android] migrate to new Android flutter plugin architecture (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun authored Feb 12, 2021
1 parent 37703aa commit 67f48b5
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 208 deletions.
35 changes: 29 additions & 6 deletions android/src/main/java/com/mapbox/mapboxgl/GlobalMethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mapbox.mapboxgl.models.OfflineRegionData;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
Expand All @@ -15,6 +19,7 @@
import java.io.InputStream;
import java.io.OutputStream;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;
Expand All @@ -23,10 +28,22 @@ class GlobalMethodHandler implements MethodChannel.MethodCallHandler {
private static final String TAG = GlobalMethodHandler.class.getSimpleName();
private static final String DATABASE_NAME = "mbgl-offline.db";
private static final int BUFFER_SIZE = 1024 * 2;
private final PluginRegistry.Registrar registrar;

GlobalMethodHandler(PluginRegistry.Registrar registrar) {
@Nullable
private PluginRegistry.Registrar registrar;
@Nullable
private FlutterPlugin.FlutterAssets flutterAssets;
@NonNull
private final Context context;

GlobalMethodHandler(@NonNull PluginRegistry.Registrar registrar) {
this.registrar = registrar;
this.context = registrar.activeContext();
}

GlobalMethodHandler(@NonNull Context context, @NonNull FlutterPlugin.FlutterAssets assets) {
this.context = context;
this.flutterAssets = assets;
}

@Override
Expand Down Expand Up @@ -58,7 +75,7 @@ public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
}

private void installOfflineMapTiles(String tilesDb) {
final File dest = new File(registrar.activeContext().getFilesDir(), DATABASE_NAME);
final File dest = new File(context.getFilesDir(), DATABASE_NAME);
try (InputStream input = openTilesDbFile(tilesDb);
OutputStream output = new FileOutputStream(dest)) {
copy(input, output);
Expand All @@ -71,7 +88,14 @@ private InputStream openTilesDbFile(String tilesDb) throws IOException {
if (tilesDb.startsWith("/")) { // Absolute path.
return new FileInputStream(new File(tilesDb));
} else {
final String assetKey = registrar.lookupKeyForAsset(tilesDb);
String assetKey;
if (registrar != null) {
assetKey = registrar.lookupKeyForAsset(tilesDb);
} else if(flutterAssets != null) {
assetKey = flutterAssets.getAssetFilePathByName(tilesDb);
} else {
throw new IllegalStateException();
}
return registrar.activeContext().getAssets().open(assetKey);
}
}
Expand All @@ -84,7 +108,7 @@ private String extractAccessToken(MethodCall methodCall, String fallbackValue) {
return fallbackValue;
}

private static int copy(InputStream input, OutputStream output) throws IOException {
private static void copy(InputStream input, OutputStream output) throws IOException {
final byte[] buffer = new byte[BUFFER_SIZE];
final BufferedInputStream in = new BufferedInputStream(input, BUFFER_SIZE);
final BufferedOutputStream out = new BufferedOutputStream(output, BUFFER_SIZE);
Expand All @@ -108,6 +132,5 @@ private static int copy(InputStream input, OutputStream output) throws IOExcepti
Log.e(TAG, e.getMessage(), e);
}
}
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.Style;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.PluginRegistry;

import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -30,9 +31,9 @@ class MapboxMapBuilder implements MapboxMapOptionsSink {
private String styleString = Style.MAPBOX_STREETS;

MapboxMapController build(
int id, Context context, AtomicInteger state, PluginRegistry.Registrar registrar, String accessToken) {
int id, Context context, BinaryMessenger messenger, MapboxMapsPlugin.LifecycleProvider lifecycleProvider, String accessToken) {
final MapboxMapController controller =
new MapboxMapController(id, context, state, registrar, options, accessToken, styleString);
new MapboxMapController(id, context, messenger, lifecycleProvider, options, accessToken, styleString);
controller.init();
controller.setMyLocationEnabled(myLocationEnabled);
controller.setMyLocationTrackingMode(myLocationTrackingMode);
Expand Down
Loading

0 comments on commit 67f48b5

Please sign in to comment.