summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/AccountsManager.java
blob: d086b3329eb7b666bda1f842f48e2d0b832cc71c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.mapbox.mapboxsdk;

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;

/**
 * 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. 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.
 *
 * Under the Mapbox Terms of Service, you may not use this code to access
 * Mapbox Mapping APIs other than through Mapbox SDKs.
 *
 * 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() {
    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() {
    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);
    }
  }

  @Nullable
  String getSkuToken() {
    if (isEnabled && 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);
  }
}