diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2017-01-10 17:26:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-10 17:26:06 +0100 |
commit | f18c0b8d1e4db52c49f7b5980902b4273b69dc33 (patch) | |
tree | 053bd9528be4bf6fceca4a96a1f79cf30bb42c9a | |
parent | 92dff9ee93a9b77afdb701621aa1bca392392a03 (diff) | |
download | qtlocation-mapboxgl-f18c0b8d1e4db52c49f7b5980902b4273b69dc33.tar.gz |
[android] - cleanup telem, disabled logging, exponential backoff, local broadcast (#7605)
4 files changed, 65 insertions, 29 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationServices.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationServices.java index a1ed5d5b00..aaf08c8b55 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationServices.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationServices.java @@ -8,6 +8,7 @@ import android.location.Location; import android.location.LocationManager; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; +import android.support.v4.content.LocalBroadcastManager; import timber.log.Timber; @@ -153,7 +154,7 @@ public class LocationServices implements LostApiClient.ConnectionCallbacks, // Update the Telemetry Receiver Intent locIntent = new Intent(TelemetryLocationReceiver.INTENT_STRING); locIntent.putExtra(LocationManager.KEY_LOCATION_CHANGED, location); - context.sendBroadcast(locIntent); + LocalBroadcastManager.getInstance(context.getApplicationContext()).sendBroadcast(locIntent); } /** diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/MapboxEventManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/MapboxEventManager.java index 9e607746c9..4a0ef248b7 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/MapboxEventManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/MapboxEventManager.java @@ -117,10 +117,10 @@ public class MapboxEventManager { */ public void initialize(@NonNull Context context, @NonNull String accessToken) { - Timber.i("Telemetry initialize() called..."); + //Timber.i("Telemetry initialize() called..."); if (initialized) { - Timber.i("Mapbox Telemetry has already been initialized."); + //Timber.i("Mapbox Telemetry has already been initialized."); return; } @@ -143,7 +143,7 @@ public class MapboxEventManager { Context.MODE_PRIVATE); // Determine if Telemetry Should Be Enabled - Timber.i("Right before Telemetry set enabled in initialized()"); + ///Timber.i("Right before Telemetry set enabled in initialized()"); setTelemetryEnabled(prefs.getBoolean(MapboxConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_ENABLED, true)); // Load / Create Vendor Id @@ -171,7 +171,7 @@ public class MapboxEventManager { String stagingAccessToken = appInfo.metaData.getString(MapboxConstants.KEY_META_DATA_STAGING_ACCESS_TOKEN); if (TextUtils.isEmpty(stagingURL) || TextUtils.isEmpty(stagingAccessToken)) { - Timber.d("Looking in SharedPreferences for Staging Credentials"); + //Timber.d("Looking in SharedPreferences for Staging Credentials"); stagingURL = prefs.getString(MapboxConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_URL, null); stagingAccessToken = prefs.getString( MapboxConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_ACCESS_TOKEN, null); @@ -191,7 +191,7 @@ public class MapboxEventManager { } } catch (Exception exception) { - Timber.e("Error Trying to load Staging Credentials: ", exception); + //Timber.e("Error Trying to load Staging Credentials: ", exception); } // Register for battery updates @@ -227,7 +227,7 @@ public class MapboxEventManager { } } } catch (Exception exception) { - Timber.w("Error checking for Telemetry Service Config: ", exception); + //Timber.w("Error checking for Telemetry Service Config: ", exception); } throw new TelemetryServiceNotConfiguredException(); } @@ -246,41 +246,45 @@ public class MapboxEventManager { * @param telemetryEnabled True to start telemetry, false to stop it */ public void setTelemetryEnabled(boolean telemetryEnabled) { - Timber.i("this.telemetryEnabled = " + this.telemetryEnabled + "; telemetryEnabled = " + telemetryEnabled); + //Timber.i("this.telemetryEnabled = " + this.telemetryEnabled + "; telemetryEnabled = " + telemetryEnabled); if (this.telemetryEnabled == telemetryEnabled) { - Timber.d("No need to start / stop telemetry as it's already in that state."); + //Timber.d("No need to start / stop telemetry as it's already in that state."); return; } if (telemetryEnabled) { - Timber.d("Starting Telemetry Up!"); + //Timber.d("Starting Telemetry Up!"); // Start It Up context.startService(new Intent(context, TelemetryService.class)); // Make sure Ambient Mode is started at a minimum if (LocationServices.getLocationServices(context).areLocationPermissionsGranted()) { - Timber.i("Permissions are good, see if GPS is enabled and if not then setup Ambient."); + //Timber.i("Permissions are good, see if GPS is enabled and if not then setup Ambient."); if (LocationServices.getLocationServices(context).isGpsEnabled()) { LocationServices.getLocationServices(context).toggleGPS(false); } } else { // Start timer that checks for Permissions - Timber.i("Permissions are not good. Need to do some looping to check on stuff."); + //Timber.i("Permissions are not good. Need to do some looping to check on stuff."); final Handler permsHandler = new Handler(); Runnable runnable = new Runnable() { + + private final ExponentialBackoffCounter exponentialBackoffCounter = new ExponentialBackoffCounter(); + @Override public void run() { if (LocationServices.getLocationServices(context).areLocationPermissionsGranted()) { - Timber.i("Permissions finally granted, so starting Ambient if GPS isn't already enabled"); + //Timber.i("Permissions finally granted, so starting Ambient if GPS isn't already enabled"); // Start Ambient if (LocationServices.getLocationServices(context).isGpsEnabled()) { LocationServices.getLocationServices(context).toggleGPS(false); } } else { // Restart Handler - Timber.i("Permissions not granted yet... let's try again in 30 seconds"); - permsHandler.postDelayed(this, 1000 * 30); + long nextWaitTime = exponentialBackoffCounter.getNextCount(); + //Timber.i("Permissions not granted yet... let's try again in " + nextWaitTime/1000 + " seconds"); + permsHandler.postDelayed(this, nextWaitTime); } } }; @@ -291,7 +295,7 @@ public class MapboxEventManager { timer = new Timer(); timer.schedule(new FlushEventsTimerTask(), flushDelayInitialInMillis, flushDelayInMillis); } else { - Timber.d("Shutting Telemetry Down"); + //Timber.d("Shutting Telemetry Down"); // Shut It Down events.removeAllElements(); context.stopService(new Intent(context, TelemetryService.class)); @@ -318,7 +322,7 @@ public class MapboxEventManager { * NOTE: Permission set to package private to enable only telemetry code to use this. */ void flushEventsQueueImmediately() { - Timber.i("flushEventsQueueImmediately() called..."); + //Timber.i("flushEventsQueueImmediately() called..."); new FlushTheEventsTask().execute(); } @@ -333,7 +337,7 @@ public class MapboxEventManager { } events.add(event); if (events.size() == FLUSH_EVENTS_CAP) { - Timber.d("eventsSize == flushCap so send data."); + //Timber.d("eventsSize == flushCap so send data."); flushEventsQueueImmediately(); } } @@ -433,7 +437,7 @@ public class MapboxEventManager { eventWithAttributes.put(MapboxEvent.ATTRIBUTE_CELLULAR_NETWORK_TYPE, getCellularNetworkType()); eventWithAttributes.put(MapboxEvent.ATTRIBUTE_WIFI, getConnectedToWifi()); } else { - Timber.w("This is not an event type in the Events Data Model."); + //Timber.w("This is not an event type in the Events Data Model."); return; } @@ -455,7 +459,7 @@ public class MapboxEventManager { // Send to Server Immediately flushEventsQueueImmediately(); - Timber.d("turnstile event pushed."); + //Timber.d("turnstile event pushed."); } /** @@ -481,7 +485,7 @@ public class MapboxEventManager { return hex; } } catch (Exception exception) { - Timber.w("Error encoding string, will return in original form.", exception); + //Timber.w("Error encoding string, will return in original form.", exception); } return string; } @@ -620,7 +624,7 @@ public class MapboxEventManager { status = true; } } catch (Exception exception) { - Timber.w("Error getting Wifi Connection Status: ", exception); + //Timber.w("Error getting Wifi Connection Status: ", exception); status = false; } } @@ -638,13 +642,13 @@ public class MapboxEventManager { protected Void doInBackground(Void... voids) { if (events.isEmpty()) { - Timber.d("No events in the queue to send so returning."); + //Timber.d("No events in the queue to send so returning."); return null; } // Check for NetworkConnectivity if (!Mapbox.isConnected()) { - Timber.w("Not connected to network, so empty events cache and return without attempting to send events"); + //Timber.w("Not connected to network, so empty events cache and return without attempting to send events"); // Make sure that events don't pile up when Offline // and thus impact available memory over time. events.removeAllElements(); @@ -768,11 +772,10 @@ public class MapboxEventManager { .post(body) .build(); response = client.newCall(request).execute(); - Timber.d("response code = " + response.code() + " for events " + events.size()); + //Timber.d("response code = " + response.code() + " for events " + events.size()); } catch (Exception exception) { Timber.e("FlushTheEventsTask borked: ", exception); - exception.printStackTrace(); } finally { if (response != null && response.body() != null) { response.body().close(); @@ -811,4 +814,15 @@ public class MapboxEventManager { return ""; } } + + static class ExponentialBackoffCounter { + + private static final long BASE_TIME = 30000 /*30 seconds*/; + private int attempt; + + long getNextCount() { + attempt++; + return attempt * BASE_TIME; + } + } }
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/TelemetryService.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/TelemetryService.java index a965591f75..c667c1199d 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/TelemetryService.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/telemetry/TelemetryService.java @@ -8,6 +8,7 @@ import android.content.pm.ServiceInfo; import android.os.AsyncTask; import android.os.IBinder; import android.support.annotation.Nullable; +import android.support.v4.content.LocalBroadcastManager; import timber.log.Timber; @@ -55,9 +56,10 @@ public class TelemetryService extends Service { Timber.i("onCreate() called"); // Enable Location Listening for lifecycle of app - IntentFilter filter = new IntentFilter(TelemetryLocationReceiver.INTENT_STRING); - telemetryLocationReceiver = new TelemetryLocationReceiver(); - registerReceiver(telemetryLocationReceiver, filter); + LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver( + new TelemetryLocationReceiver(), + new IntentFilter(TelemetryLocationReceiver.INTENT_STRING) + ); } /** diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/telemetry/ExponentialBackOffTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/telemetry/ExponentialBackOffTest.java new file mode 100644 index 0000000000..50ce19c050 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/telemetry/ExponentialBackOffTest.java @@ -0,0 +1,19 @@ +package com.mapbox.mapboxsdk.telemetry; + +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; + +public class ExponentialBackOffTest { + + @Test + public void testExponentialBackOff() { + MapboxEventManager.ExponentialBackoffCounter counter = new MapboxEventManager.ExponentialBackoffCounter(); + assertEquals(30000, counter.getNextCount()); + assertEquals(60000, counter.getNextCount()); + assertEquals(90000, counter.getNextCount()); + assertEquals(120000, counter.getNextCount()); + assertEquals(150000, counter.getNextCount()); + assertEquals(180000, counter.getNextCount()); + } +}
\ No newline at end of file |