-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spec: mapbox/mobile-telemetry#423 Add new User agent with header X-Mapbox-User-Agent in addition to current user agent. The new user agent is constructed from host app and Mapbox libraries version information. Update the version for android-sdk-versions-plugin. Update copyright year to 2018-2019 in LICENSE.MD Change publishing license from "The Apache Software License, Version 2.0" to "The MIT License" for consistency. * check params and init client before setting base url * change setBaseUrl() to return boolean fix: typo displacemnt => displacement (#438)
- Loading branch information
Showing
16 changed files
with
319 additions
and
55 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ POM_SCM_URL=https://github.com/mapbox/mapbox-events-android | |
POM_SCM_CONNECTION=scm:[email protected]:mapbox/mapbox-events-android.git | ||
POM_SCM_DEV_CONNECTION=scm:[email protected]:mapbox/mapbox-events-android.git | ||
|
||
POM_LICENCE_NAME=The Apache Software License, Version 2.0 | ||
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt | ||
POM_LICENCE_NAME=The MIT License | ||
POM_LICENCE_URL=https://opensource.org/licenses/MIT | ||
POM_LICENCE_DIST=repo | ||
|
||
POM_DEVELOPER_ID=mapbox | ||
|
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
46 changes: 46 additions & 0 deletions
46
libcore/src/androidTest/java/com/mapbox/android/core/UserAgentSDKInfoTest.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,46 @@ | ||
package com.mapbox.android.core; | ||
|
||
import android.content.Context; | ||
import android.support.test.InstrumentationRegistry; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Locale; | ||
|
||
public class UserAgentSDKInfoTest { | ||
|
||
private static final String SDK_UA_FORMAT = "%s/%s (%s; %s)"; | ||
private static final String SDK_UA_VERSION_CODE_FORMAT = "v%d"; | ||
private static final Locale LOCALE_DEFAULT = Locale.US; | ||
private static final String NAME = "libcore"; | ||
|
||
@Test | ||
public void testSDKInformation() { | ||
Context context = InstrumentationRegistry.getContext(); | ||
String packageName = context.getPackageName().replace(".test", ""); | ||
String versionCode = String.format(LOCALE_DEFAULT, SDK_UA_VERSION_CODE_FORMAT, BuildConfig.VERSION_CODE); | ||
String sdkInfo = MapboxSdkInfoForUserAgentGenerator.getInstance(context.getAssets()) | ||
.getMapboxSdkIdentifiersForUserAgent(context.getAssets()); | ||
Assert.assertEquals(String.format(Locale.US, SDK_UA_FORMAT, NAME, BuildConfig.VERSION_NAME, | ||
packageName, versionCode), sdkInfo); | ||
} | ||
|
||
@Test | ||
public void testUserAgentSdkInfo() { | ||
Context context = InstrumentationRegistry.getContext(); | ||
String sdkInfo = MapboxSdkInfoForUserAgentGenerator.getInstance(context.getAssets()) | ||
.getSdkInfoForUserAgent(); | ||
String packageName = context.getPackageName().replace(".test", ""); | ||
String versionCode = String.format(LOCALE_DEFAULT, SDK_UA_VERSION_CODE_FORMAT, BuildConfig.VERSION_CODE); | ||
Assert.assertEquals(String.format(Locale.US, SDK_UA_FORMAT, NAME, BuildConfig.VERSION_NAME, | ||
packageName, versionCode), sdkInfo); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testSDKInformationInUserAgentWithNullContext() { | ||
MapboxSdkInfoForUserAgentGenerator.getInstance(null) | ||
.getSdkInfoForUserAgent(); | ||
|
||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
libcore/src/main/java/com/mapbox/android/core/MapboxSdkInfoForUserAgentGenerator.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,88 @@ | ||
package com.mapbox.android.core; | ||
|
||
import android.content.res.AssetManager; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.RestrictTo; | ||
import android.support.annotation.VisibleForTesting; | ||
import android.util.Log; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Generator that reads(from assets/sdk_versions folder) and constructs Mapbox SDK versions for user agent. | ||
* Generates strings in format for each Mapbox library in the host app and concatenates them seperated by spaces. | ||
* <p> User agent format for Mapbox SDK : {SDK Name}/{Version} ({packageName}; {versionCode}) </p> | ||
*/ | ||
public class MapboxSdkInfoForUserAgentGenerator { | ||
|
||
private static MapboxSdkInfoForUserAgentGenerator userAgentGenerator; | ||
|
||
private String sdkInfoForUserAgent; | ||
private static final Object lock = new Object(); | ||
private static final Locale DEFAULT_LOCALE = Locale.US; | ||
private static final String USER_AGENT_SDK_VERSION_FORMAT = " %s (%s%s)"; | ||
private static final String MAPBOX_IDENTIFIER = "mapbox"; | ||
private static final String EMPTY_STRING = ""; | ||
private static final String SDK_VERSIONS_FOLDER = "sdk_versions"; | ||
private static final String LOG_TAG = "MapboxUAGenerator"; | ||
|
||
private MapboxSdkInfoForUserAgentGenerator(AssetManager assetManager) { | ||
this.sdkInfoForUserAgent = getMapboxSdkIdentifiersForUserAgent(assetManager); | ||
} | ||
|
||
public static MapboxSdkInfoForUserAgentGenerator getInstance(@NonNull AssetManager assetManager) { | ||
if (userAgentGenerator == null) { | ||
synchronized (lock) { | ||
userAgentGenerator = new MapboxSdkInfoForUserAgentGenerator(assetManager); | ||
} | ||
} | ||
return userAgentGenerator; | ||
} | ||
|
||
@VisibleForTesting | ||
@RestrictTo(RestrictTo.Scope.LIBRARY) | ||
String getMapboxSdkIdentifiersForUserAgent(@NonNull AssetManager assetManager) { | ||
StringBuilder stringBuilder = new StringBuilder(EMPTY_STRING); | ||
try { | ||
String[] files = assetManager.list(SDK_VERSIONS_FOLDER); | ||
if (files != null) { | ||
for (String fileName : files) { | ||
if (fileName.contains(MAPBOX_IDENTIFIER)) { | ||
InputStream inputStream = null; | ||
try { | ||
inputStream = assetManager.open(SDK_VERSIONS_FOLDER + File.separator + fileName); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | ||
String nameAndVersion = reader.readLine(); | ||
nameAndVersion = nameAndVersion != null ? nameAndVersion : EMPTY_STRING; | ||
StringBuilder sdkSubInfo = new StringBuilder(EMPTY_STRING); | ||
String subInfo; | ||
while ((subInfo = reader.readLine()) != null) { | ||
sdkSubInfo.append("; "); | ||
sdkSubInfo.append(subInfo); | ||
} | ||
reader.close(); | ||
stringBuilder.append(String.format(DEFAULT_LOCALE, USER_AGENT_SDK_VERSION_FORMAT, | ||
nameAndVersion, fileName, sdkSubInfo.toString())); | ||
} catch (IOException exception) { | ||
Log.e(LOG_TAG, exception.toString()); | ||
} finally { | ||
FileUtils.closeQuietly(inputStream); | ||
} | ||
} | ||
} | ||
} | ||
} catch (IOException exception) { | ||
Log.e(LOG_TAG, exception.toString()); | ||
} | ||
return stringBuilder.toString().trim(); | ||
} | ||
|
||
public String getSdkInfoForUserAgent() { | ||
return sdkInfoForUserAgent; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
libcore/src/test/java/com/mapbox/android/core/UserAgentSDKInfoHandleExceptionsTest.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,31 @@ | ||
package com.mapbox.android.core; | ||
|
||
import android.content.res.AssetManager; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class UserAgentSDKInfoHandleExceptionsTest { | ||
|
||
@Test | ||
public void testSDKInformationCatchOpenAssetsIOException() throws Exception { | ||
AssetManager assetManager = mock(AssetManager.class); | ||
when(assetManager.list(anyString())).thenThrow(IOException.class); | ||
MapboxSdkInfoForUserAgentGenerator.getInstance(assetManager) | ||
.getSdkInfoForUserAgent(); | ||
} | ||
|
||
@Test | ||
public void testSDKInformationReadInputStremIOException() throws IOException { | ||
AssetManager assetManager = mock(AssetManager.class); | ||
when(assetManager.list(anyString())).thenReturn(new String[] {"com.mapbox.android.core"}); | ||
when(assetManager.open(anyString())).thenThrow(IOException.class); | ||
MapboxSdkInfoForUserAgentGenerator.getInstance(assetManager) | ||
.getSdkInfoForUserAgent(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
libtelemetry/src/androidTest/java/com/mapbox/android/telemetry/TestReformedUserAgent.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,28 @@ | ||
package com.mapbox.android.telemetry; | ||
|
||
import android.content.Context; | ||
import android.support.test.InstrumentationRegistry; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TestReformedUserAgent { | ||
|
||
private static final String CORE_PACKAGE = "com.mapbox.android.core"; | ||
private static final String TELEMETRY_PACKAGE = "com.mapbox.android.telemetry"; | ||
|
||
@Test | ||
public void testReformedUserAgent() { | ||
Context context = InstrumentationRegistry.getContext(); | ||
String reformedUserAgent = TelemetryUtils.createReformedFullUserAgent(context); | ||
Assert.assertTrue(reformedUserAgent.contains(context.getPackageName())); | ||
Assert.assertTrue(reformedUserAgent.contains(CORE_PACKAGE)); | ||
Assert.assertTrue(reformedUserAgent.contains(TELEMETRY_PACKAGE)); | ||
Assert.assertFalse(reformedUserAgent.contains("null")); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testReformedUserAgentForNullContext() { | ||
TelemetryUtils.createReformedFullUserAgent(null); | ||
} | ||
} |
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
Oops, something went wrong.