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

Commit

Permalink
AccountManager: write user ID synchronously, keep it in memory. (#145)
Browse files Browse the repository at this point in the history
* AccountManager: write user ID synchronously, keep it in memory.
* Unit tests: run AccountsManagerTest with Robolectric.
  • Loading branch information
Igor Tarasov authored and tobrun committed Jan 22, 2020
1 parent d27a18c commit 350ad46
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class AccountsManager {
private static final String PREFERENCE_TIMESTAMP = "com.mapbox.mapboxsdk.accounts.timestamp";

private SharedPreferences sharedPreferences;
private String userId;
private String skuToken;
private long timestamp;
private boolean isManaged;
Expand All @@ -57,8 +58,7 @@ class AccountsManager {
private void initialize() {
retrieveSkuTokenAndTimestamp();
if (isManaged) {
String userId = validateUserId();
validateRotation(userId);
validateRotation();
}
}

Expand Down Expand Up @@ -92,32 +92,17 @@ private void retrieveSkuTokenAndTimestamp() {
timestamp = sharedPreferences.getLong(PREFERENCE_TIMESTAMP, 0L);
}

private String validateUserId() {
SharedPreferences sharedPreferences = getSharedPreferences();
String userId = sharedPreferences.getString(PREFERENCE_USER_ID, "");
if (TextUtils.isEmpty(userId)) {
userId = generateUserId();
SharedPreferences.Editor editor = getSharedPreferences().edit();
editor.putString(PREFERENCE_USER_ID, userId);
editor.apply();
}

return userId;
}

private void validateRotation(String userId) {
private void validateRotation() {
if (TextUtils.isEmpty(skuToken) || timestamp == 0L) {
skuToken = generateSkuToken(userId);
skuToken = generateSkuToken(getUserId());
timestamp = persistRotation(skuToken);
}
}

String getSkuToken() {
if (isManaged) {
if (isExpired()) {
SharedPreferences sharedPreferences = getSharedPreferences();
String userId = sharedPreferences.getString(PREFERENCE_USER_ID, "");
skuToken = generateSkuToken(userId);
skuToken = generateSkuToken(getUserId());
timestamp = persistRotation(skuToken);
}
} else {
Expand Down Expand Up @@ -158,6 +143,26 @@ static long getNow() {
return System.currentTimeMillis();
}

private synchronized String getUserId() {
if (!TextUtils.isEmpty(userId)) {
return userId;
}

SharedPreferences sharedPreferences = getSharedPreferences();
userId = sharedPreferences.getString(PREFERENCE_USER_ID, "");

if (TextUtils.isEmpty(userId)) {
userId = generateUserId();
SharedPreferences.Editor editor = getSharedPreferences().edit();
editor.putString(PREFERENCE_USER_ID, userId);
if (!editor.commit()) {
Logger.e(TAG, "Failed to save user id.");
}
}

return userId;
}

@NonNull
private String generateUserId() {
return MapboxAccounts.obtainEndUserId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.text.format.DateUtils;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static com.mapbox.mapboxsdk.constants.MapboxConstants.KEY_PREFERENCE_SKU_TOKEN;
import static org.junit.Assert.assertEquals;
Expand All @@ -13,6 +15,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(RobolectricTestRunner.class)
public class AccountsManagerTest {
@Test
public void testIsExpired() {
Expand Down

0 comments on commit 350ad46

Please sign in to comment.