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

AccountManager: write user ID synchronously, keep it in memory. #145

Merged
merged 2 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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