Merge branch 'master' into 1856-material-take-2
# Conflicts: # android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package com.mapbox.mapboxgl.http;
|
||||
|
||||
import com.mapbox.mapboxgl.constants.MapboxConstants;
|
||||
import com.squareup.okhttp.Call;
|
||||
import com.squareup.okhttp.Callback;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
|
||||
class HTTPContext {
|
||||
|
||||
private static final int CONNECTION_ERROR = 0;
|
||||
private static final int TEMPORARY_ERROR = 1;
|
||||
private static final int PERMANENT_ERROR = 2;
|
||||
|
||||
private static HTTPContext mInstance = null;
|
||||
|
||||
private OkHttpClient mClient;
|
||||
|
||||
private HTTPContext() {
|
||||
super();
|
||||
mClient = new OkHttpClient();
|
||||
}
|
||||
|
||||
public static HTTPContext getInstance() {
|
||||
if (mInstance == null) {
|
||||
mInstance = new HTTPContext();
|
||||
}
|
||||
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
public HTTPRequest createRequest(long nativePtr, String resourceUrl, String userAgent, String etag, String modified) {
|
||||
return new HTTPRequest(nativePtr, resourceUrl, userAgent, etag, modified);
|
||||
}
|
||||
|
||||
public class HTTPRequest implements Callback {
|
||||
private long mNativePtr = 0;
|
||||
|
||||
private Call mCall;
|
||||
private Request mRequest;
|
||||
|
||||
private native void nativeOnFailure(long nativePtr, int type, String message);
|
||||
private native void nativeOnResponse(long nativePtr, int code, String message, String etag, String modified, String cacheControl, String expires, byte[] body);
|
||||
|
||||
private HTTPRequest(long nativePtr, String resourceUrl, String userAgent, String etag, String modified) {
|
||||
mNativePtr = nativePtr;
|
||||
Request.Builder builder = new Request.Builder().url(resourceUrl).tag(resourceUrl.toLowerCase(MapboxConstants.MAPBOX_LOCALE)).addHeader("User-Agent", userAgent);
|
||||
if (etag.length() > 0) {
|
||||
builder = builder.addHeader("If-None-Match", etag);
|
||||
} else if (modified.length() > 0) {
|
||||
builder = builder.addHeader("If-Modified-Since", modified);
|
||||
}
|
||||
mRequest = builder.build();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
mCall = HTTPContext.getInstance().mClient.newCall(mRequest);
|
||||
mCall.enqueue(this);
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
mCall.cancel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Request request, IOException e) {
|
||||
int type = PERMANENT_ERROR;
|
||||
if ((e instanceof UnknownHostException) || (e instanceof SocketException) || (e instanceof ProtocolException) || (e instanceof SSLException)) {
|
||||
type = CONNECTION_ERROR;
|
||||
} else if ((e instanceof InterruptedIOException)) {
|
||||
type = TEMPORARY_ERROR;
|
||||
}
|
||||
nativeOnFailure(mNativePtr, type, e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Response response) throws IOException {
|
||||
byte[] body = response.body().bytes();
|
||||
response.body().close();
|
||||
|
||||
nativeOnResponse(mNativePtr, response.code(), response.message(), response.header("ETag"), response.header("Last-Modified"), response.header("Cache-Control"), response.header("Expires"), body);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package com.mapbox.mapboxgl.util;
|
||||
|
||||
import com.mapbox.mapboxgl.constants.MapboxConstants;
|
||||
import com.squareup.okhttp.Callback;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
|
||||
public class JavaFileSource {
|
||||
|
||||
private static JavaFileSource instance = null;
|
||||
|
||||
// Single reference to OkHttp for performance gains
|
||||
private OkHttpClient client;
|
||||
|
||||
/**
|
||||
* Private Constructor to support Singleton pattern
|
||||
*/
|
||||
private JavaFileSource() {
|
||||
super();
|
||||
client = new OkHttpClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the singleton instance of JavaFileSource
|
||||
* @return Reference to the Singleton Instance of JavaFileSource
|
||||
*/
|
||||
public static JavaFileSource getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new JavaFileSource();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an HTTP Request
|
||||
* @param resourceUrl URL to resource
|
||||
* @param callback Callback class
|
||||
*/
|
||||
public void request(final String resourceUrl, final Callback callback) {
|
||||
Request request = new Request.Builder().url(resourceUrl).tag(resourceUrl.toLowerCase(MapboxConstants.MAPBOX_LOCALE)).build();
|
||||
client.newCall(request).enqueue(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to cancel HTTP Request made
|
||||
* @param resourceUrl URL of request to cancel
|
||||
*/
|
||||
public void cancel(final String resourceUrl) {
|
||||
client.cancel(resourceUrl.toLowerCase(MapboxConstants.MAPBOX_LOCALE));
|
||||
}
|
||||
}
|
||||
@@ -191,6 +191,7 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
//
|
||||
|
||||
// Common initialization code goes here
|
||||
@TargetApi(16)
|
||||
private void initialize(Context context, AttributeSet attrs) {
|
||||
|
||||
// Save the context
|
||||
@@ -217,9 +218,12 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
|
||||
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
activityManager.getMemoryInfo(memoryInfo);
|
||||
long totalMemory = memoryInfo.totalMem;
|
||||
mNativeMapView = new NativeMapView(this, cachePath, dataPath, apkPath, mScreenDensity,availableProcessors, totalMemory);
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
||||
long totalMemory = memoryInfo.totalMem;
|
||||
mNativeMapView = new NativeMapView(this, cachePath, dataPath, apkPath, mScreenDensity, availableProcessors, totalMemory);
|
||||
} else {
|
||||
throw new RuntimeException("Need to implement totalMemory on pre-Jelly Bean devices.");
|
||||
}
|
||||
// Load the attributes
|
||||
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MapView, 0, 0);
|
||||
try {
|
||||
@@ -678,7 +682,7 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
}
|
||||
|
||||
public void onSizeChanged(int width, int height, int oldw, int oldh) {
|
||||
mNativeMapView.resizeView((int)(width / mScreenDensity), (int)(height / mScreenDensity));
|
||||
mNativeMapView.resizeView((int) (width / mScreenDensity), (int) (height / mScreenDensity));
|
||||
}
|
||||
|
||||
// This class handles SurfaceHolder callbacks
|
||||
|
||||
Reference in New Issue
Block a user