diff options
author | Brad Leege <bleege@gmail.com> | 2016-02-11 18:01:44 -0800 |
---|---|---|
committer | Brad Leege <bleege@gmail.com> | 2016-02-11 18:34:52 -0800 |
commit | 23bde2141ebb045b101ca104c91b09a052e7879a (patch) | |
tree | 71c64af7773b8c2b25a86aa0d0211dc1b2ffcf02 | |
parent | cd951e2a77758bab6686d0cd37ca3e776db9642c (diff) | |
download | qtlocation-mapboxgl-23bde2141ebb045b101ca104c91b09a052e7879a.tar.gz |
[android] #3907 - Initial refactoring to support on / off capability for Telemetry Services
5 files changed, 95 insertions, 16 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java index 9ff39000e6..a68e89fb74 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java @@ -104,4 +104,5 @@ public class MapboxConstants { public static final String MAPBOX_SHARED_PREFERENCES_FILE = "MapboxSharedPreferences"; public static final String MAPBOX_SHARED_PREFERENCE_KEY_VENDORID = "mapboxVendorId"; + public static final String MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_ENABLED = "mapboxTelemetryEnabled"; } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 488f1dd49a..51ce47df8d 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -79,6 +79,7 @@ import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.layers.CustomLayer; import com.mapbox.mapboxsdk.location.LocationServices; +import com.mapbox.mapboxsdk.telemetry.MapboxEventManager; import com.mapbox.mapboxsdk.telemetry.TelemetryService; import com.mapbox.mapboxsdk.utils.ApiAccess; import java.lang.annotation.Retention; @@ -368,12 +369,8 @@ public class MapView extends FrameLayout { // Force a check for Telemetry validateTelemetryServiceConfigured(); - // Start Telemetry Service - Intent telemetryService = new Intent(getContext(), TelemetryService.class); - getContext().startService(telemetryService); - - // Start PASSIVE Telemetry - LocationServices.getLocationServices(getContext()).toggleGPS(false); + // Start Telemetry (authorization determined in initial MapboxEventManager constructor) + MapboxEventManager.getMapboxEventManager(getContext()).isTelemetryEnabled(); } // Force a check for an access token @@ -2848,9 +2845,11 @@ public class MapView extends FrameLayout { private static class AttributionOnClickListener implements View.OnClickListener, DialogInterface.OnClickListener { private static final int ATTRIBUTION_INDEX_IMPROVE_THIS_MAP = 2; + private static final int ATTRIBUTION_INDEX_TELEMETRY_SETTINGS = 3; private MapView mMapView; public AttributionOnClickListener(MapView mapView) { + super(); mMapView = mapView; } @@ -2869,6 +2868,19 @@ public class MapView extends FrameLayout { @Override public void onClick(DialogInterface dialog, int which) { Context context = ((Dialog) dialog).getContext(); + if (which == ATTRIBUTION_INDEX_TELEMETRY_SETTINGS) { + + int array = R.array.attribution_telemetry_options; + if (MapboxEventManager.getMapboxEventManager(context).isTelemetryEnabled()) { + array = R.array.attribution_telemetry_options_already_participating; + } + String[] items = context.getResources().getStringArray(array); + AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AttributionAlertDialogStyle); + builder.setTitle(R.string.attributionsDialogTitle); + builder.setAdapter(new ArrayAdapter<>(context, R.layout.attribution_list_item, items), this); + builder.show(); + return; + } String url = context.getResources().getStringArray(R.array.attribution_links)[which]; if (which == ATTRIBUTION_INDEX_IMPROVE_THIS_MAP) { LatLng latLng = mMapView.getMapboxMap().getCameraPosition().target; 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 d9d39a2087..da85bebb55 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 @@ -24,6 +24,7 @@ import android.util.DisplayMetrics; import android.util.Log; import android.view.WindowManager; import com.mapbox.mapboxsdk.constants.MapboxConstants; +import com.mapbox.mapboxsdk.location.LocationServices; import com.mapbox.mapboxsdk.utils.ApiAccess; import org.json.JSONArray; import org.json.JSONObject; @@ -51,6 +52,8 @@ public class MapboxEventManager { private static MapboxEventManager mapboxEventManager = null; + private boolean telemetryEnabled; + private final Vector<Hashtable<String, Object>> events = new Vector<>(); private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US); @@ -90,11 +93,15 @@ public class MapboxEventManager { Log.w(TAG, "Error getting Encryption Algorithm: " + e); } - // Load / Create Vendor Id SharedPreferences prefs = context.getSharedPreferences(MapboxConstants.MAPBOX_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + + // Determine if Telemetry Should Be Enabled + setTelemetryEnabled(prefs.getBoolean(MapboxConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_ENABLED, true)); + + // Load / Create Vendor Id if (prefs.contains(MapboxConstants.MAPBOX_SHARED_PREFERENCE_KEY_VENDORID)) { mapboxVendorId = prefs.getString(MapboxConstants.MAPBOX_SHARED_PREFERENCE_KEY_VENDORID, "Default Value"); - Log.i(TAG, "Found Vendor Id = " + mapboxVendorId); + Log.d(TAG, "Found Vendor Id = " + mapboxVendorId); } else { String vendorId = UUID.randomUUID().toString(); vendorId = encodeString(vendorId); @@ -102,7 +109,7 @@ public class MapboxEventManager { editor.putString(MapboxConstants.MAPBOX_SHARED_PREFERENCE_KEY_VENDORID, vendorId); editor.apply(); editor.commit(); - Log.i(TAG, "Set New Vendor Id = " + vendorId); + Log.d(TAG, "Set New Vendor Id = " + vendorId); } // Create Initial Session Id @@ -140,12 +147,8 @@ public class MapboxEventManager { } // Register for battery updates - IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); - batteryStatus = context.registerReceiver(null, ifilter); - - // Manage Timer Flush - timer = new Timer(); - timer.schedule(new FlushEventsTimerTask(), 1, flushDelayInMillis); + IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + batteryStatus = context.registerReceiver(null, iFilter); } /** @@ -160,6 +163,53 @@ public class MapboxEventManager { return mapboxEventManager; } + public boolean isTelemetryEnabled() { + return telemetryEnabled; + } + + /** + * Enables / Disables Telemetry + * @param telemetryEnabled True to start telemetry, false to stop it + */ + public void setTelemetryEnabled(boolean telemetryEnabled) { + if (this.telemetryEnabled == telemetryEnabled) { + Log.i(TAG, "no need to start / stop telemetry as it's already in that state."); + return; + } + + if (telemetryEnabled) { + Log.i(TAG, "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).isGPSEnabled()) { + LocationServices.getLocationServices(context).toggleGPS(false); + } + + // Manage Timer Flush + timer = new Timer(); + timer.schedule(new FlushEventsTimerTask(), 1, flushDelayInMillis); + } else { + Log.i(TAG, "Shutting Telemetry Down"); + // Shut It Down + context.stopService(new Intent(context, TelemetryService.class)); + + if (timer != null) { + timer.cancel(); + timer = null; + } + } + + // Persist + this.telemetryEnabled = telemetryEnabled; + SharedPreferences prefs = context.getSharedPreferences(MapboxConstants.MAPBOX_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = prefs.edit(); + editor.putBoolean(MapboxConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_ENABLED, telemetryEnabled); + editor.apply(); + editor.commit(); + } + /** * Adds a Location Event to the system for processing * @param location Location event @@ -400,7 +450,7 @@ public class MapboxEventManager { .post(body) .build(); Response response = client.newCall(request).execute(); - Log.d(TAG, "Response Code from Mapbox Events Server: " + response.code() + " for " + events.size() + " events sent in."); + Log.i(TAG, "Response Code from Mapbox Events Server: " + response.code() + " for " + events.size() + " events sent in."); // Reset Events // ============ 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 a4965b0e45..56006dadf6 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.os.AsyncTask; import android.os.IBinder; import android.os.PowerManager; import android.support.annotation.Nullable; +import android.util.Log; public class TelemetryService extends Service { @@ -50,6 +51,8 @@ public class TelemetryService extends Service { public void onCreate() { super.onCreate(); + Log.i(TAG, "onCreate() called"); + // Enable Location Listening for lifecycle of app IntentFilter filter = new IntentFilter(TelemetryLocationReceiver.INTENT_STRING); telemetryLocationReceiver = new TelemetryLocationReceiver(); @@ -125,6 +128,8 @@ public class TelemetryService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { + Log.i(TAG, "onStartCommand() called"); + // Start WakeLock to keep Location Data working when device sleeps PowerManager mgr = (PowerManager)getSystemService(Context.POWER_SERVICE); telemetryWakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TelemetryWakeLock"); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/res/values/arrays.xml b/platform/android/MapboxGLAndroidSDK/src/main/res/values/arrays.xml index 677a4994d8..d5d26d09d2 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/res/values/arrays.xml +++ b/platform/android/MapboxGLAndroidSDK/src/main/res/values/arrays.xml @@ -6,10 +6,21 @@ <item>Improve this map</item> <item>Telemetry Settings</item> </array> + <array name="attribution_telemetry_options"> + <item>Tell Me More</item> + <item>Don\'t Participate</item> + <item>Participate</item> + </array> + <array name="attribution_telemetry_options_already_participating"> + <item>Tell Me More</item> + <item>Stop Participating</item> + <item>Keep Participating</item> + </array> <!-- If editing this array update MapView.ATTRIBUTION_INDEX_IMPROVE_THIS_MAP --> <array name="attribution_links" formatted="false" translatable="false"> <item>https://www.mapbox.com/about/maps/</item> <item>http://www.openstreetmap.org/about/</item> <item>https://www.mapbox.com/map-feedback/#/%1$f/%2$f/%3$d</item> + <item>https://www.mapbox.com/telemetry/</item> </array> </resources> |