Skip to content

Commit 84cc74a

Browse files
authored
Add setter for session rotation time (#544)
* split constant into value and units * add setter for sessionIdRotationTime * add experimental annotation to setSessionIdRotationTime and fix checkstyle issues
1 parent 5e11335 commit 84cc74a

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/MapboxTelemetry.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.os.BatteryManager;
1414
import android.os.Build;
1515
import android.os.Handler;
16+
import android.support.annotation.IntRange;
1617
import android.support.annotation.NonNull;
1718
import android.support.v4.content.LocalBroadcastManager;
1819
import android.text.TextUtils;
@@ -72,6 +73,7 @@ public class MapboxTelemetry implements Callback, LocationEngineListener {
7273
private Boolean telemetryEnabled = null;
7374
protected CopyOnWriteArrayList<TelemetryListener> telemetryListeners;
7475
private Hashtable<String, Object> customTurnstileEvent = null;
76+
private int sessionIdRotationTime = TelemetryConstants.DEFAULT_SESSION_ID_ROTATION_HOURS;
7577

7678
/**
7779
* Private constructor for configuring the single instance per app.
@@ -157,6 +159,14 @@ public void setCustomTurnstileEvent(Hashtable<String, Object> customTurnstileEve
157159
this.customTurnstileEvent = customTurnstileEvent;
158160
}
159161

162+
// For internal use only
163+
// This is an experimental API. Experimental APIs are quickly evolving and
164+
// might change or be removed in minor versions.
165+
@Experimental
166+
public void setSessionIdRotationTime(@IntRange(from = 1, to = 24) int sessionIdRotationTime) {
167+
this.sessionIdRotationTime = sessionIdRotationTime; // in hours
168+
}
169+
160170
/**
161171
* Checks that TelemetryService has been configured by developer
162172
*/
@@ -219,7 +229,7 @@ private void checkStagingServerInformation() {
219229
// Set new client information (if needed)
220230
if (!TextUtils.isEmpty(stagingURL) && !TextUtils.isEmpty(stagingAccessToken)) {
221231
Log.w(LOG_TAG, String.format("Using staging server '%s' with access token '%s'.",
222-
stagingURL, stagingAccessToken));
232+
stagingURL, stagingAccessToken));
223233
client.setEventsEndpoint(stagingURL);
224234
client.setAccessToken(stagingAccessToken);
225235
client.setStagingEnvironment(true);
@@ -234,7 +244,7 @@ private void setUserAgent() {
234244
// Append app identifier to user agent if present
235245
String appIdentifier = TelemetryUtils.getApplicationIdentifier(context);
236246
String fullUserAgent = TextUtils.isEmpty(appIdentifier) ? userAgent : Util.toHumanReadableAscii(
237-
String.format(TelemetryConstants.DEFAULT_LOCALE, "%s %s", appIdentifier, userAgent));
247+
String.format(TelemetryConstants.DEFAULT_LOCALE, "%s %s", appIdentifier, userAgent));
238248
client.setUserAgent(fullUserAgent);
239249
}
240250

@@ -244,7 +254,7 @@ private void setUserAgent() {
244254
private void rotateSessionId() {
245255
long timeSinceLastSet = System.currentTimeMillis() - mapboxSessionIdLastSet;
246256
if ((TextUtils.isEmpty(mapboxSessionId))
247-
|| (timeSinceLastSet > TelemetryConstants.SESSION_ID_ROTATION_MS)) {
257+
|| (timeSinceLastSet > sessionIdRotationTime * TelemetryConstants.ONE_HOUR_IN_MS)) {
248258
mapboxSessionId = TelemetryUtils.buildUUID();
249259
mapboxSessionIdLastSet = System.currentTimeMillis();
250260
}
@@ -660,7 +670,7 @@ private void shutdownTelemetry() {
660670
context.stopService(new Intent(context, TelemetryService.class));
661671
if (locationEngine == null) {
662672
Log.e(LOG_TAG, String.format(
663-
"Shutdown error: Location Engine instance wasn't set up (initialized: %b).", initialized));
673+
"Shutdown error: Location Engine instance wasn't set up (initialized: %b).", initialized));
664674
} else {
665675
locationEngine.removeLocationEngineListener(this);
666676
locationEngine.removeLocationUpdates();

mapbox/libandroid-telemetry/src/main/java/com/mapbox/services/android/telemetry/constants/TelemetryConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public class TelemetryConstants {
2020
public static final long FLUSH_PERIOD_MS = 3 * 60 * 1_000; // 3 minutes
2121
public static final int FLUSH_EVENTS_CAP = 180; // 180 seconds or 180 events
2222

23-
public static final int SESSION_ID_ROTATION_MS = 24 * 60 * 60 * 1_000; // 24 hours
23+
public static final int ONE_HOUR_IN_MS = 60 * 60 * 1_000;
24+
public static final int DEFAULT_SESSION_ID_ROTATION_HOURS = 24;
2425

2526
/**
2627
* Key used to store CN endpoint settings in AndroidManifest.xml

0 commit comments

Comments
 (0)