Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] new manifest flag to enable/disable sku tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
zugaldia authored and LukasPaczos committed Apr 24, 2019
1 parent c426df0 commit 71fd147
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,74 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.format.DateUtils;

import com.mapbox.android.accounts.v1.MapboxAccounts;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.log.Logger;

/**
* REMOVAL OR MODIFICATION OF THE FOLLOWING CODE VIOLATES THE MAPBOX TERMS
* OF SERVICE
* IF YOU USE THIS CODE WITH MAPBOX MAPPING API, REMOVAL OR MODIFICATION OF
* THE FOLLOWING CODE VIOLATES THE MAPBOX TERMS OF SERVICE.
*
* The following code is used to access Mapbox's Mapping APIs.
* The following code is used to access Mapbox's Mapping APIs. Removal or
* modification of this code when used with Mapbox's Mapping APIs can result
* in higher fees and/or termination of your account with Mapbox.
*
* Removal or modification of this code when used with Mapbox's Mapping APIs
* can result in termination of your agreement and/or your account with
* Mapbox.
* Under the Mapbox Terms of Service, you may not use this code to access
* Mapbox Mapping APIs other than through Mapbox SDKs.
*
* Using this code to access Mapbox Mapping APIs from outside the Mapbox Maps
* SDK also violates the Mapbox Terms of Service. On Android, Mapping APIs
* should be accessed using the methods documented at
* https://www.mapbox.com/android.
*
* You can access the Mapbox Terms of Service at https://www.mapbox.com/tos/
* The Android documentation to access Mapping APIs is available at
* https://www.mapbox.com/android and the Mapbox Terms of Service are
* available at https://www.mapbox.com/tos/.
*/
class AccountsManager {
private static final String TAG = "Mbgl-AccountsManager";

private static final String PREFERENCE_USER_ID = "com.mapbox.mapboxsdk.accounts.userid";
private static final String PREFERENCE_TIMESTAMP = "com.mapbox.mapboxsdk.accounts.timestamp";
private static final String PREFERENCE_SKU_TOKEN = "com.mapbox.mapboxsdk.accounts.skutoken";

private long timestamp;
@Nullable
private String skuToken;

private boolean isEnabled;

AccountsManager() {
String userId = validateUserId();
validateRotation(userId);
isEnabled = isSkuTokenEnabled();
if (isEnabled) {
String userId = validateUserId();
validateRotation(userId);
} else {
timestamp = 0L;
skuToken = null;
}
}

private boolean isSkuTokenEnabled() {
boolean value = MapboxConstants.DEFAULT_ENABLE_SKU_TOKEN;
try {
// Try getting a custom value from the app Manifest
ApplicationInfo appInfo = Mapbox.getApplicationContext().getPackageManager().getApplicationInfo(
Mapbox.getApplicationContext().getPackageName(),
PackageManager.GET_META_DATA);
if (appInfo.metaData != null) {
value = appInfo.metaData.getBoolean(
MapboxConstants.KEY_META_DATA_ENABLE_SKU_TOKEN,
MapboxConstants.DEFAULT_ENABLE_SKU_TOKEN
);
}
} catch (Exception exception) {
Logger.e(TAG, "Failed to read the package metadata: ", exception);
}

return value;
}

private String validateUserId() {
Expand All @@ -62,8 +95,9 @@ private void validateRotation(String userId) {
}
}

@Nullable
String getSkuToken() {
if (isExpired()) {
if (isEnabled && isExpired()) {
SharedPreferences sharedPreferences = getSharedPreferences();
String userId = sharedPreferences.getString(PREFERENCE_USER_ID, "");
skuToken = generateSkuToken(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static void setAccessToken(String accessToken) {
*
* @return the SKU token
*/
@Nullable
public static String getSkuToken() {
return INSTANCE.accounts.getSkuToken();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public class MapboxConstants {
*/
public static final boolean DEFAULT_MEASURE_TILE_DOWNLOAD_ON = false;

/**
* Key used to switch SKU token on/off in AndroidManifest.xml
*/
public static final String KEY_META_DATA_ENABLE_SKU_TOKEN = "com.mapbox.EnableSkuToken";

/**
* Default value for KEY_META_DATA_ENABLE_SKU_TOKEN (default is off)
*/
public static final boolean DEFAULT_ENABLE_SKU_TOKEN = false;

/**
* Unmeasured state
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public static String buildResourceUrl(@NonNull String host, String resourceUrl,
} else {
resourceUrl = resourceUrl + "&";
}
resourceUrl = resourceUrl + "events=true&sku=" + Mapbox.getSkuToken();
if (Mapbox.getSkuToken() != null) {
resourceUrl = resourceUrl + "events=true&sku=" + Mapbox.getSkuToken();
} else {
resourceUrl = resourceUrl + "events=true";
}
}
return resourceUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public TelemetryImpl() {
public void onAppUserTurnstileEvent() {
AppUserTurnstile turnstileEvent = new AppUserTurnstile(BuildConfig.MAPBOX_SDK_IDENTIFIER,
BuildConfig.MAPBOX_SDK_VERSION);
turnstileEvent.setSkuId(MapboxAccounts.SKU_ID_MAPS_MAUS);
if (Mapbox.getSkuToken() != null) {
turnstileEvent.setSkuId(MapboxAccounts.SKU_ID_MAPS_MAUS);
}
telemetry.push(turnstileEvent);
telemetry.push(MapEventFactory.buildMapLoadEvent(new PhoneState(appContext)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,11 @@
android:name="com.mapbox.MeasureTileDownloadOn"
android:value="false" />

<!-- Set value to true to have SKU tokens included in API requests -->
<meta-data
android:name="com.mapbox.EnableSkuToken"
android:value="true" />

</application>

</manifest>

0 comments on commit 71fd147

Please sign in to comment.