[android] user agent string

This commit is contained in:
Ivo van Dongen
2016-09-23 15:38:33 +02:00
committed by Ivo van Dongen
parent a126aad0b8
commit 2b05776f97
4 changed files with 56 additions and 7 deletions

View File

@@ -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'

View File

@@ -101,4 +101,12 @@ public class MapboxAccountManager {
return (activeNetwork != null && activeNetwork.isConnected());
}
/**
* Not public API
* @return the Application Context
*/
public Context getApplicationContext() {
return applicationContext;
}
}

View File

@@ -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 "";
}
}
}

View File

@@ -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);
}