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

[android] Introduce AccountsManager to support SKU tokens in API requests #14404

Merged
merged 16 commits into from
Apr 18, 2019

Conversation

zugaldia
Copy link
Member

@zugaldia zugaldia commented Apr 11, 2019

This PR proposes a new AccountsManager object, instantiated within the regular Mapbox object, that manages the state and rotation of SKU tokens. SKU tokens are added to Mapbox API requests only.

It relies on Android's SharedPreferences for storage, and proposes a centralized MapboxConstants.MAPBOX_SHARED_PREFERENCES to be shared with FileSource values.

Before being able to merge this PR we need to add two extra dependencies:

  • Updated Telemetry SDK supporting SKU token mode for turnstile events.
  • MapboxAccounts library to generate the content of the SKU tokens.

/cc: @friedbunny

@zugaldia zugaldia added this to the release-mojito milestone Apr 11, 2019
@zugaldia zugaldia added the Android Mapbox Maps SDK for Android label Apr 11, 2019
@zugaldia
Copy link
Member Author

@tobrun Per chat, while this PR isn't still ready to be merged (it requires updating some dependencies), the rest of the code is ready for a first review.

return now;
}

private @NonNull SharedPreferences getSharedPreferences() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotations should be placed before private keyword, couple more occurrences below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, it wasn't caught by lint or checkstyle -- I'll update it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically it's correct and not aware of any lint rules enforcing this though I think it's a best practice.

}

static long getNow() {
return Calendar.getInstance(MapboxConstants.MAPBOX_LOCALE).getTimeInMillis();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use System.currentTimeMillis() for this use-case. Calendar adds overhead and relies on that same api. This algorithm doesn't need to be localized, we only need a reference point in time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method will eventually be removed to use the underlying accounts library which, in turn, uses System.currentTimeMillis(). I'll still change it here for the time being.

@@ -87,6 +90,11 @@ public static void setAccessToken(String accessToken) {
FileSource.getInstance(getApplicationContext()).setAccessToken(accessToken);
}

public static String getSkuToken() {
validateMapbox();
return INSTANCE.accounts.getSkuToken();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a developer doesn't provide an access token, eg. when using their own tile endpoint, this will throw a null pointer exception when the SDK tries to make a request.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is only called for Mapbox API URLs so it's safe to remove the validation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that part, :shipit:

@@ -87,6 +90,11 @@ public static void setAccessToken(String accessToken) {
FileSource.getInstance(getApplicationContext()).setAccessToken(accessToken);
}

public static String getSkuToken() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit javadoc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

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

static final long ONE_HOUR_MILLIS = 60 * 60 * 1_000L;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit could be DateUtils.HOUR_IN_MILLIS instead

@andrlee
Copy link
Contributor

andrlee commented Apr 16, 2019

@zugaldia @tobrun folks, telem release with changes to turnstile event has been published on maven and github: https://github.com/mapbox/mapbox-events-android/releases/tag/telem-4.4.0

@LukasPaczos
Copy link
Member

@tobrun This should be ready for a final review.

I've rebased on top of #14309, added the turnstile parameter and replaced placeholders with the concrete implementation, @zugaldia could you verify that the changes are correct?

@LukasPaczos LukasPaczos marked this pull request as ready for review April 18, 2019 15:04
@@ -87,6 +90,11 @@ public static void setAccessToken(String accessToken) {
FileSource.getInstance(getApplicationContext()).setAccessToken(accessToken);
}

public static String getSkuToken() {
validateMapbox();
return INSTANCE.accounts.getSkuToken();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that part, :shipit:

@zugaldia zugaldia merged commit d0a1526 into master Apr 18, 2019
@zugaldia zugaldia deleted the az-sku-token branch April 18, 2019 20:29
tobrun added a commit that referenced this pull request May 14, 2019
tobrun added a commit that referenced this pull request May 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android beta blocker Blocks the next beta release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants