[android] #3907 - Initial refactoring to support on / off capability for Telemetry Services

This commit is contained in:
Brad Leege
2016-02-11 18:01:44 -08:00
parent cd951e2a77
commit 23bde2141e
5 changed files with 95 additions and 16 deletions

View File

@@ -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";
}

View File

@@ -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;

View File

@@ -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
// ============

View File

@@ -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");