diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2016-09-23 15:38:33 +0200 |
---|---|---|
committer | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2016-09-29 18:40:10 +0200 |
commit | 2b05776f978c7759824bce1c4fc89d67cc00d332 (patch) | |
tree | 0675523b7b7be8dc8129771cb89fd449495a9061 /platform | |
parent | a126aad0b805d84d6cbfe59311bfbd1d6e2ba6f1 (diff) | |
download | qtlocation-mapboxgl-2b05776f978c7759824bce1c4fc89d67cc00d332.tar.gz |
[android] user agent string
Diffstat (limited to 'platform')
4 files changed, 56 insertions, 7 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/build.gradle b/platform/android/MapboxGLAndroidSDK/build.gradle index 11fbcec88c..3386bd81aa 100644 --- a/platform/android/MapboxGLAndroidSDK/build.gradle +++ b/platform/android/MapboxGLAndroidSDK/build.gradle @@ -41,6 +41,9 @@ android { defaultConfig { minSdkVersion Integer.parseInt(project.ANDROID_MIN_SDK) targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION) + buildConfigField "String", "MAPBOX_EVENTS_USER_AGENT_BASE", String.format("\"MapboxEventsAndroid/%s\"", project.VERSION_NAME) + buildConfigField "String", "MAPBOX_VERSION_STRING", String.format("\"Mapbox/%s\"", project.VERSION_NAME) + buildConfigField "String", "GIT_REVISION_SHORT", String.format("\"%s\"", getGitRevision()) } sourceSets { @@ -64,12 +67,10 @@ android { buildTypes { debug { jniDebuggable true - buildConfigField "String", "MAPBOX_EVENTS_USER_AGENT_BASE", new StringBuilder().append("\"").append("MapboxEventsAndroid/").append(project.VERSION_NAME).append("\"").toString() } release { jniDebuggable false - buildConfigField "String", "MAPBOX_EVENTS_USER_AGENT_BASE", new StringBuilder().append("\"").append("MapboxEventsAndroid/").append(project.VERSION_NAME).append("\"").toString() consumerProguardFiles 'proguard-rules.pro' } } @@ -157,6 +158,12 @@ def getRepositoryPassword() { (hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "") } +def getGitRevision() { + def cmd = "git rev-parse --short HEAD" + def proc = cmd.execute() + def ref = proc.text.trim() + return ref +} task apklib(type: Zip) { appendix = extension = 'apklib' diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapboxAccountManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapboxAccountManager.java index fa53807306..0d192d302d 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapboxAccountManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/MapboxAccountManager.java @@ -101,4 +101,12 @@ public class MapboxAccountManager { return (activeNetwork != null && activeNetwork.isConnected()); } + /** + * Not public API + * @return the Application Context + */ + public Context getApplicationContext() { + return applicationContext; + } + } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java index 8c818c2f52..430b37adb8 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java @@ -1,8 +1,13 @@ package com.mapbox.mapboxsdk.http; + +import android.content.Context; +import android.content.pm.PackageInfo; +import android.os.Build; import android.text.TextUtils; import android.util.Log; +import com.mapbox.mapboxsdk.BuildConfig; import com.mapbox.mapboxsdk.MapboxAccountManager; import com.mapbox.mapboxsdk.constants.MapboxConstants; @@ -22,11 +27,13 @@ import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import okhttp3.internal.Util; class HTTPRequest implements Callback { private static OkHttpClient mClient = new OkHttpClient(); - private final String LOG_TAG = HTTPRequest.class.getName(); + private static final String LOG_TAG = HTTPRequest.class.getName(); + private String USER_AGENT_STRING = null; private static final int CONNECTION_ERROR = 0; private static final int TEMPORARY_ERROR = 1; @@ -45,7 +52,7 @@ class HTTPRequest implements Callback { private native void nativeOnResponse(int code, String etag, String modified, String cacheControl, String expires, String retryAfter, String xRateLimitReset, byte[] body); - private HTTPRequest(long nativePtr, String resourceUrl, String userAgent, String etag, String modified) { + private HTTPRequest(long nativePtr, String resourceUrl, String etag, String modified) { mNativePtr = nativePtr; try { @@ -65,7 +72,10 @@ class HTTPRequest implements Callback { resourceUrl = resourceUrl + "events=true"; } - Request.Builder builder = new Request.Builder().url(resourceUrl).tag(resourceUrl.toLowerCase(MapboxConstants.MAPBOX_LOCALE)).addHeader("User-Agent", userAgent); + Request.Builder builder = new Request.Builder() + .url(resourceUrl) + .tag(resourceUrl.toLowerCase(MapboxConstants.MAPBOX_LOCALE)) + .addHeader("User-Agent", getUserAgent()); if (etag.length() > 0) { builder = builder.addHeader("If-None-Match", etag); } else if (modified.length() > 0) { @@ -165,4 +175,29 @@ class HTTPRequest implements Callback { } mLock.unlock(); } + + private String getUserAgent() { + if (USER_AGENT_STRING == null) { + return USER_AGENT_STRING = Util.toHumanReadableAscii( + String.format("%s %s (%s) Android/%s (%s)", + getApplicationIdentifier(), + BuildConfig.MAPBOX_VERSION_STRING, + BuildConfig.GIT_REVISION_SHORT, + Build.VERSION.SDK_INT, + Build.CPU_ABI) + ); + } else { + return USER_AGENT_STRING; + } + } + + private String getApplicationIdentifier() { + try { + Context context = MapboxAccountManager.getInstance().getApplicationContext(); + PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); + return String.format("%s/%s (%s)", context.getPackageName(), packageInfo.versionName, packageInfo.versionCode); + } catch (Exception e) { + return ""; + } + } } diff --git a/platform/android/src/http_file_source.cpp b/platform/android/src/http_file_source.cpp index 00b61aa107..ed4d81391f 100644 --- a/platform/android/src/http_file_source.cpp +++ b/platform/android/src/http_file_source.cpp @@ -83,12 +83,11 @@ HTTPRequest::HTTPRequest(jni::JNIEnv& env, const Resource& resource_, FileSource jni::UniqueLocalFrame frame = jni::PushLocalFrame(env, 10); static auto constructor = - javaClass.GetConstructor<jni::jlong, jni::String, jni::String, jni::String, jni::String>(env); + javaClass.GetConstructor<jni::jlong, jni::String, jni::String, jni::String>(env); javaRequest = javaClass.New(env, constructor, reinterpret_cast<jlong>(this), jni::Make<jni::String>(env, resource.url), - jni::Make<jni::String>(env, "MapboxGL/1.0"), jni::Make<jni::String>(env, etagStr), jni::Make<jni::String>(env, modifiedStr)).NewGlobalRef(env); } |