-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade Mapzen Android SDK #822
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 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 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 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 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 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
33 changes: 8 additions & 25 deletions
33
app/src/main/kotlin/com/mapzen/erasermap/model/TileHttpHandler.kt
This file contains 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 |
---|---|---|
@@ -1,33 +1,16 @@ | ||
package com.mapzen.erasermap.model | ||
|
||
import android.app.Application | ||
import android.os.Build | ||
import com.mapzen.tangram.HttpHandler | ||
import com.squareup.okhttp.Callback | ||
import okhttp3.Callback | ||
import java.io.File | ||
|
||
public class TileHttpHandler(application: Application) : HttpHandler() { | ||
companion object { | ||
@JvmStatic val KITKAT = 19 | ||
} | ||
|
||
init { | ||
if (Build.VERSION.SDK_INT >= KITKAT) { | ||
val httpCache = File(application.externalCacheDir.absolutePath + "/tile_cache") | ||
setCache(httpCache, 30 * 1024 * 1024) | ||
} | ||
okRequestBuilder.addHeader(Http.HEADER_DNT, Http.VALUE_HEADER_DNT) | ||
} | ||
class TileHttpHandler : TmpHttpHandler { | ||
constructor() : super() {} | ||
|
||
var apiKey: String? = null | ||
|
||
override fun onRequest(url: String, callback: Callback): Boolean { | ||
val urlWithApiKey = url + "?api_key=" + apiKey | ||
return super.onRequest(urlWithApiKey, callback) | ||
} | ||
constructor(cacheDir: File, cacheSize: Long) : super(cacheDir, cacheSize) {} | ||
|
||
override fun onCancel(url: String) { | ||
val urlWithApiKey = url + "?api_key=" + apiKey | ||
super.onCancel(urlWithApiKey) | ||
override fun onRequest(url: String, headers: Map<String, String>, callback: Callback): Boolean { | ||
val emHeaders = mutableMapOf(Http.HEADER_DNT to Http.VALUE_HEADER_DNT) | ||
emHeaders.putAll(headers) | ||
return super.onRequest(url, emHeaders, callback) | ||
} | ||
} |
182 changes: 182 additions & 0 deletions
182
app/src/main/kotlin/com/mapzen/erasermap/model/TmpHttpHandler.java
This file contains 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,182 @@ | ||
package com.mapzen.erasermap.model; | ||
|
||
import com.mapzen.tangram.HttpHandler; | ||
|
||
import android.os.Build; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.Socket; | ||
import java.net.UnknownHostException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSocket; | ||
import javax.net.ssl.SSLSocketFactory; | ||
|
||
import okhttp3.Cache; | ||
import okhttp3.Call; | ||
import okhttp3.Callback; | ||
import okhttp3.ConnectionSpec; | ||
import okhttp3.Headers; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.TlsVersion; | ||
|
||
/** | ||
* Temporary TileHttpHandler super class until Tangram allows customizing headers. | ||
*/ | ||
public class TmpHttpHandler extends HttpHandler { | ||
|
||
private OkHttpClient okClient; | ||
|
||
/** | ||
* Enables TLS v1.2 when creating SSLSockets. | ||
* <p/> | ||
* For some reason, android supports TLS v1.2 from API 16, but enables it by | ||
* default only from API 20. | ||
* | ||
* @link https://developer.android.com/reference/javax/net/ssl/SSLSocket.html | ||
* @see SSLSocketFactory | ||
*/ | ||
private class Tls12SocketFactory extends SSLSocketFactory { | ||
private final String[] TLS_V12_ONLY = {"TLSv1.2"}; | ||
|
||
final SSLSocketFactory delegate; | ||
|
||
public Tls12SocketFactory(SSLSocketFactory base) { | ||
this.delegate = base; | ||
} | ||
|
||
@Override | ||
public String[] getDefaultCipherSuites() { | ||
return delegate.getDefaultCipherSuites(); | ||
} | ||
|
||
@Override | ||
public String[] getSupportedCipherSuites() { | ||
return delegate.getSupportedCipherSuites(); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws | ||
IOException { | ||
return patch(delegate.createSocket(s, host, port, autoClose)); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(String host, int port) throws IOException, UnknownHostException { | ||
return patch(delegate.createSocket(host, port)); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException { | ||
return patch(delegate.createSocket(host, port, localHost, localPort)); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(InetAddress host, int port) throws IOException { | ||
return patch(delegate.createSocket(host, port)); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { | ||
return patch(delegate.createSocket(address, port, localAddress, localPort)); | ||
} | ||
|
||
private Socket patch(Socket s) { | ||
if (s instanceof SSLSocket) { | ||
((SSLSocket) s).setEnabledProtocols(TLS_V12_ONLY); | ||
} | ||
return s; | ||
} | ||
} | ||
|
||
/** | ||
* Construct an {@code HttpHandler} with default options. | ||
*/ | ||
public TmpHttpHandler() { | ||
this(null, 0); | ||
} | ||
|
||
/** | ||
* Construct an {@code HttpHandler} with cache. | ||
* Cache map data in a directory with a specified size limit | ||
* @param directory Directory in which map data will be cached | ||
* @param maxSize Maximum size of data to cache, in bytes | ||
*/ | ||
public TmpHttpHandler(File directory, long maxSize) { | ||
OkHttpClient.Builder builder = new OkHttpClient.Builder() | ||
.connectTimeout(10, TimeUnit.SECONDS) | ||
.writeTimeout(10, TimeUnit.SECONDS) | ||
.readTimeout(30, TimeUnit.SECONDS); | ||
|
||
if (directory != null && maxSize > 0) { | ||
builder.cache(new Cache(directory, maxSize)); | ||
} | ||
|
||
if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 22) { | ||
try { | ||
SSLContext sc = SSLContext.getInstance("TLSv1.2"); | ||
sc.init(null, null, null); | ||
builder.sslSocketFactory(new Tls12SocketFactory(sc.getSocketFactory())); | ||
|
||
ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) | ||
.tlsVersions(TlsVersion.TLS_1_2) | ||
.build(); | ||
|
||
List<ConnectionSpec> specs = new ArrayList<>(); | ||
specs.add(cs); | ||
specs.add(ConnectionSpec.COMPATIBLE_TLS); | ||
specs.add(ConnectionSpec.CLEARTEXT); | ||
|
||
builder.connectionSpecs(specs); | ||
} catch (Exception exc) { | ||
android.util.Log.e("Tangram", "Error while setting TLS 1.2", exc); | ||
} | ||
} | ||
|
||
okClient = builder.build(); | ||
} | ||
|
||
/** | ||
* Begin an HTTP request | ||
* @param url URL for the requested resource | ||
* @param cb Callback for handling request result | ||
* @return true if request was successfully started | ||
*/ | ||
public boolean onRequest(String url, Map<String, String> headers, Callback cb) { | ||
Request request = new Request.Builder() | ||
.url(url) | ||
.headers(Headers.of(headers)) | ||
.build(); | ||
okClient.newCall(request).enqueue(cb); | ||
return true; | ||
} | ||
|
||
/** | ||
* Cancel an HTTP request | ||
* @param url URL of the request to be cancelled | ||
*/ | ||
public void onCancel(String url) { | ||
|
||
// check and cancel running call | ||
for (Call runningCall : okClient.dispatcher().runningCalls()) { | ||
if (runningCall.request().url().toString().equals(url)) { | ||
runningCall.cancel(); | ||
} | ||
} | ||
|
||
// check and cancel queued call | ||
for (Call queuedCall : okClient.dispatcher().queuedCalls()) { | ||
if (queuedCall.request().url().toString().equals(url)) { | ||
queuedCall.cancel(); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains 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 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
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.
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.
Maybe requires more changes (than we want with EM), but probably we should use sceneUpdate with sceneLoad for consistency with SDK usage.
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 is for interaction with Pelias so we need to append the api key. When mapzen/android#412 merges, we can use the new
MapzenSearchHttpRequestHandler
to append only the DNT header (the api key will be handled by the SDK)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.
Ohh, yes make sense. 👍