This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] Introduce AccountsManager to support SKU tokens in API requ…
…ests (#14404)
- Loading branch information
Showing
10 changed files
with
174 additions
and
9 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
112 changes: 112 additions & 0 deletions
112
platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/AccountsManager.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,112 @@ | ||
package com.mapbox.mapboxsdk; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.support.annotation.NonNull; | ||
import android.text.TextUtils; | ||
import android.text.format.DateUtils; | ||
|
||
import com.mapbox.android.accounts.v1.MapboxAccounts; | ||
import com.mapbox.mapboxsdk.constants.MapboxConstants; | ||
|
||
/** | ||
* REMOVAL OR MODIFICATION OF THE FOLLOWING CODE VIOLATES THE MAPBOX TERMS | ||
* OF SERVICE | ||
* | ||
* 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 termination of your agreement and/or your account with | ||
* Mapbox. | ||
* | ||
* 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/ | ||
*/ | ||
class 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; | ||
private String skuToken; | ||
|
||
AccountsManager() { | ||
String userId = validateUserId(); | ||
validateRotation(userId); | ||
} | ||
|
||
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) { | ||
SharedPreferences sharedPreferences = getSharedPreferences(); | ||
timestamp = sharedPreferences.getLong(PREFERENCE_TIMESTAMP, 0L); | ||
skuToken = sharedPreferences.getString(PREFERENCE_SKU_TOKEN, ""); | ||
if (timestamp == 0L || TextUtils.isEmpty(skuToken)) { | ||
skuToken = generateSkuToken(userId); | ||
timestamp = persistRotation(skuToken); | ||
} | ||
} | ||
|
||
String getSkuToken() { | ||
if (isExpired()) { | ||
SharedPreferences sharedPreferences = getSharedPreferences(); | ||
String userId = sharedPreferences.getString(PREFERENCE_USER_ID, ""); | ||
skuToken = generateSkuToken(userId); | ||
timestamp = persistRotation(skuToken); | ||
} | ||
|
||
return skuToken; | ||
} | ||
|
||
private boolean isExpired() { | ||
return isExpired(getNow(), timestamp); | ||
} | ||
|
||
static boolean isExpired(long now, long then) { | ||
return ((now - then) > DateUtils.HOUR_IN_MILLIS); | ||
} | ||
|
||
private long persistRotation(String skuToken) { | ||
long now = getNow(); | ||
SharedPreferences.Editor editor = getSharedPreferences().edit(); | ||
editor.putLong(PREFERENCE_TIMESTAMP, now); | ||
editor.putString(PREFERENCE_SKU_TOKEN, skuToken); | ||
editor.apply(); | ||
return now; | ||
} | ||
|
||
@NonNull | ||
private SharedPreferences getSharedPreferences() { | ||
return Mapbox.getApplicationContext() | ||
.getSharedPreferences(MapboxConstants.MAPBOX_SHARED_PREFERENCES, Context.MODE_PRIVATE); | ||
} | ||
|
||
static long getNow() { | ||
return System.currentTimeMillis(); | ||
} | ||
|
||
@NonNull | ||
private String generateUserId() { | ||
return MapboxAccounts.obtainEndUserId(); | ||
} | ||
|
||
@NonNull | ||
private String generateSkuToken(String userId) { | ||
return MapboxAccounts.obtainMapsSkuUserToken(userId); | ||
} | ||
} |
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
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
24 changes: 24 additions & 0 deletions
24
...rm/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/AccountsManagerTest.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,24 @@ | ||
package com.mapbox.mapboxsdk; | ||
|
||
import android.text.format.DateUtils; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class AccountsManagerTest { | ||
@Test | ||
public void testIsExpired() { | ||
long now = AccountsManager.getNow(); | ||
|
||
long defaultValue = 0L; | ||
long tooOld = now - DateUtils.HOUR_IN_MILLIS - 1; | ||
long futureValue = now + 1; | ||
long immediatePast = now - 1; | ||
|
||
Assert.assertTrue(AccountsManager.isExpired(now, defaultValue)); | ||
Assert.assertTrue(AccountsManager.isExpired(now, tooOld)); | ||
|
||
Assert.assertFalse(AccountsManager.isExpired(now, futureValue)); | ||
Assert.assertFalse(AccountsManager.isExpired(now, immediatePast)); | ||
} | ||
} |
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