Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsd…
Browse files Browse the repository at this point in the history
…k/maps/MapView.java
  • Loading branch information
tobrun committed Jul 13, 2018
1 parent f7e2fe5 commit 6eb9e10
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.drawable.ColorDrawable;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -126,6 +127,11 @@ protected void initialize(@NonNull final Context context, @NonNull final MapboxM
// in IDE layout editor, just return
return;
}

// hide surface until map is fully loaded #10990
setForeground(new ColorDrawable(options.getForegroundLoadColor()));
mapCallback.addOnMapReadyCallback(new InitialStyleLoadedCallback(this));

mapboxMapOptions = options;

// inflate view
Expand Down Expand Up @@ -283,11 +289,13 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
}

private void initialiseDrawingSurface(MapboxMapOptions options) {
String localFontFamily = options.getLocalIdeographFontFamily();
int foregroundLoadColor = options.getForegroundLoadColor();
if (options.getTextureMode()) {
TextureView textureView = new TextureView(getContext());
String localFontFamily = options.getLocalIdeographFontFamily();
boolean translucentSurface = options.getTranslucentTextureSurface();
mapRenderer = new TextureViewMapRenderer(getContext(), textureView, localFontFamily, translucentSurface) {
mapRenderer = new TextureViewMapRenderer(getContext(),
textureView, localFontFamily, translucentSurface, foregroundLoadColor) {
@Override
protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.onSurfaceCreated();
Expand All @@ -299,7 +307,7 @@ protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
} else {
GLSurfaceView glSurfaceView = new GLSurfaceView(getContext());
glSurfaceView.setZOrderMediaOverlay(mapboxMapOptions.getRenderSurfaceOnTop());
mapRenderer = new GLSurfaceViewMapRenderer(getContext(), glSurfaceView, options.getLocalIdeographFontFamily()) {
mapRenderer = new GLSurfaceViewMapRenderer(getContext(), glSurfaceView, localFontFamily, foregroundLoadColor) {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.onSurfaceCreated();
Expand Down Expand Up @@ -981,6 +989,23 @@ public void onFocalPointChanged(PointF pointF) {
}
}

private static class InitialStyleLoadedCallback implements OnMapReadyCallback {

private WeakReference<MapView> weakReference;

InitialStyleLoadedCallback(MapView mapView) {
this.weakReference = new WeakReference<>(mapView);
}

@Override
public void onMapReady(MapboxMap mapboxMap) {
MapView mapView = weakReference.get();
if (mapView != null && !mapView.isDestroyed()) {
mapView.setForeground(null);
}
}
}

private class GesturesManagerInteractionListener implements MapboxMap.OnGesturesManagerInteractionListener {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
public class MapboxMapOptions implements Parcelable {

private static final int LIGHT_GRAY = 0xFFF0E9E1; // RGB(240, 233, 225))
private static final float FOUR_DP = 4f;
private static final float NINETY_TWO_DP = 92f;
private static final int UNDEFINED_COLOR = -1;
Expand Down Expand Up @@ -73,6 +74,9 @@ public class MapboxMapOptions implements Parcelable {
private boolean textureMode;
private boolean translucentTextureSurface;

@ColorInt
private int foregroundLoadColor;

private String style;

private float pixelRatio;
Expand Down Expand Up @@ -124,6 +128,7 @@ private MapboxMapOptions(Parcel in) {
zMediaOverlay = in.readByte() != 0;
localIdeographFontFamily = in.readString();
pixelRatio = in.readFloat();
foregroundLoadColor = in.readInt();
}

/**
Expand Down Expand Up @@ -209,7 +214,6 @@ public static MapboxMapOptions createFromAttributes(@NonNull Context context, @N
FOUR_DP * pxlRatio)),
(int) (typedArray.getDimension(R.styleable.mapbox_MapView_mapbox_uiAttributionMarginBottom,
FOUR_DP * pxlRatio))});

mapboxMapOptions.textureMode(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_renderTextureMode, false));
mapboxMapOptions.translucentTextureSurface(
Expand All @@ -222,6 +226,9 @@ public static MapboxMapOptions createFromAttributes(@NonNull Context context, @N
typedArray.getString(R.styleable.mapbox_MapView_mapbox_localIdeographFontFamily));
mapboxMapOptions.pixelRatio(
typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_pixelRatio, 0));
mapboxMapOptions.foregroundLoadColor(
typedArray.getInt(R.styleable.mapbox_MapView_mapbox_foregroundLoadColor, LIGHT_GRAY)
);
} finally {
typedArray.recycle();
}
Expand Down Expand Up @@ -521,6 +528,17 @@ public MapboxMapOptions translucentTextureSurface(boolean translucentTextureSurf
return this;
}

/**
* Set the MapView foreground color that is used when the map surface is being created.
*
* @param loadColor the color to show during map creation
* @return This
*/
public MapboxMapOptions foregroundLoadColor(@ColorInt int loadColor) {
this.foregroundLoadColor = loadColor;
return this;
}

/**
* Enable tile pre-fetching. Loads tiles at a lower zoom-level to pre-render
* a low resolution preview while more detailed tiles are loaded.
Expand Down Expand Up @@ -819,6 +837,16 @@ public boolean getTranslucentTextureSurface() {
return translucentTextureSurface;
}

/**
* Returns the current configured foreground color that is used during map creation.
*
* @return the load color
*/
@ColorInt
public int getForegroundLoadColor() {
return foregroundLoadColor;
}

/**
* Returns the font-family for locally overriding generation of glyphs in the
* &#x27;CJK Unified Ideographs&#x27; and &#x27;Hangul Syllables&#x27; ranges.
Expand Down Expand Up @@ -892,6 +920,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (zMediaOverlay ? 1 : 0));
dest.writeString(localIdeographFontFamily);
dest.writeFloat(pixelRatio);
dest.writeInt(foregroundLoadColor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.mapboxsdk.utils.ColorUtils;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
Expand All @@ -21,13 +22,19 @@ public abstract class MapRenderer implements MapRendererScheduler {
// Holds the pointer to the native peer after initialisation
private long nativePtr = 0;

private MapboxMap.OnFpsChangedListener onFpsChangedListener;
// The color shown when the map style is loading
private final float[] foregroundRgbaColor;

public MapRenderer(Context context, String localIdeographFontFamily) {
private MapboxMap.OnFpsChangedListener onFpsChangedListener;

public MapRenderer(Context context, String localIdeographFontFamily, int foregroundLoadColor) {
FileSource fileSource = FileSource.getInstance(context);
float pixelRatio = context.getResources().getDisplayMetrics().density;
String programCacheDir = context.getCacheDir().getAbsolutePath();

// convert android int color to GL rgba, range 0-1
foregroundRgbaColor = ColorUtils.colorToGlRgbaArray(foregroundLoadColor);

// Initialise native peer
nativeInitialize(this, fileSource, pixelRatio, programCacheDir, localIdeographFontFamily);
}
Expand Down Expand Up @@ -63,30 +70,17 @@ protected void onSurfaceCreated(GL10 gl, EGLConfig config) {

@CallSuper
protected void onSurfaceChanged(GL10 gl, int width, int height) {
if (width < 0) {
throw new IllegalArgumentException("fbWidth cannot be negative.");
}

if (height < 0) {
throw new IllegalArgumentException("fbHeight cannot be negative.");
}

if (width > 65535) {
throw new IllegalArgumentException(
"fbWidth cannot be greater than 65535.");
}

if (height > 65535) {
throw new IllegalArgumentException(
"fbHeight cannot be greater than 65535.");
}

gl.glViewport(0, 0, width, height);
nativeOnSurfaceChanged(width, height);
}

@CallSuper
protected void onDrawFrame(GL10 gl) {
// clear color with the foreground load color #10990
// on low end devices there is a race condition between
// when the gl surface is loaded and the onMapReady is called
gl.glClearColor(foregroundRgbaColor[0], foregroundRgbaColor[1], foregroundRgbaColor[2], foregroundRgbaColor[3]);

nativeRender();

if (onFpsChangedListener != null) {
Expand Down Expand Up @@ -138,5 +132,4 @@ private void updateFps() {
frames = 0;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.opengl.GLSurfaceView;

import android.support.annotation.ColorInt;
import com.mapbox.mapboxsdk.maps.renderer.MapRenderer;
import com.mapbox.mapboxsdk.maps.renderer.egl.EGLConfigChooser;

Expand All @@ -21,8 +22,11 @@ public class GLSurfaceViewMapRenderer extends MapRenderer implements GLSurfaceVi

private final GLSurfaceView glSurfaceView;

public GLSurfaceViewMapRenderer(Context context, GLSurfaceView glSurfaceView, String localIdeographFontFamily) {
super(context, localIdeographFontFamily);
public GLSurfaceViewMapRenderer(Context context,
GLSurfaceView glSurfaceView,
String localIdeographFontFamily,
@ColorInt int clearColor) {
super(context, localIdeographFontFamily, clearColor);
this.glSurfaceView = glSurfaceView;
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setEGLConfigChooser(new EGLConfigChooser());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.mapboxsdk.maps.renderer.textureview;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.view.TextureView;

Expand Down Expand Up @@ -30,8 +31,9 @@ public class TextureViewMapRenderer extends MapRenderer {
public TextureViewMapRenderer(@NonNull Context context,
@NonNull TextureView textureView,
String localIdeographFontFamily,
boolean translucentSurface) {
super(context, localIdeographFontFamily);
boolean translucentSurface,
@ColorInt int foregroundLoadColor) {
super(context, localIdeographFontFamily, foregroundLoadColor);
this.translucentSurface = translucentSurface;
renderThread = new TextureViewRenderThread(textureView, this);
renderThread.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

import com.mapbox.mapboxsdk.style.expressions.Expression;

import java.text.DecimalFormat;
import java.util.Locale;

/**
* Constructs paint/layout properties for Layers
*
Expand Down Expand Up @@ -2404,11 +2401,11 @@ public static PropertyValue<Expression> textOptional(Expression value) {
*
* @param color Android color int
* @return String rgba color
* @deprecated use {@link com.mapbox.mapboxsdk.utils.ColorUtils#colorToRgbaString(int)} instead
*/
@Deprecated
public static String colorToRgbaString(@ColorInt int color) {
String alpha = new DecimalFormat("#.###").format(((float)((color >> 24) & 0xFF)) / 255.0f);
return String.format(Locale.US, "rgba(%d, %d, %d, %s)",
(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, alpha);
return ColorUtils.colorToRgbaString(color);
}

/**
Expand All @@ -2419,8 +2416,10 @@ public static String colorToRgbaString(@ColorInt int color) {
*
* @param color Android color int
* @return int rgba array
* @deprecated use {@link com.mapbox.mapboxsdk.utils.ColorUtils#colorToRgbaArray(int)} instead
*/
@Deprecated
public static float[] colorToRgbaArray(@ColorInt int color) {
return new float[] {(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, ((color >> 24) & 0xFF) / 255.0f};
return ColorUtils.colorToRgbaArray(color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ package com.mapbox.mapboxsdk.style.layers;
import android.support.annotation.ColorInt;

import com.mapbox.mapboxsdk.style.expressions.Expression;

import java.text.DecimalFormat;
import java.util.Locale;
import com.mapbox.mapboxsdk.utils.ColorUtils;

/**
* Constructs paint/layout properties for Layers
Expand Down Expand Up @@ -94,11 +92,11 @@ public class PropertyFactory {
*
* @param color Android color int
* @return String rgba color
* @deprecated use {@link com.mapbox.mapboxsdk.utils.ColorUtils#colorToRgbaString(int)} instead
*/
@Deprecated
public static String colorToRgbaString(@ColorInt int color) {
String alpha = new DecimalFormat("#.###").format(((float)((color >> 24) & 0xFF)) / 255.0f);
return String.format(Locale.US, "rgba(%d, %d, %d, %s)",
(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, alpha);
return ColorUtils.colorToRgbaString(color);
}

/**
Expand All @@ -109,8 +107,10 @@ public class PropertyFactory {
*
* @param color Android color int
* @return int rgba array
* @deprecated use {@link com.mapbox.mapboxsdk.utils.ColorUtils#colorToRgbaArray(int)} instead
*/
@Deprecated
public static float[] colorToRgbaArray(@ColorInt int color) {
return new float[] {(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, ((color >> 24) & 0xFF) / 255.0f};
return ColorUtils.colorToRgbaArray(color);
}
}
Loading

0 comments on commit 6eb9e10

Please sign in to comment.