Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #5004 - Allowing MapboxAccountManager and MapView to both i…
Browse files Browse the repository at this point in the history
…nitialize telemetry depending on developer implementation
  • Loading branch information
bleege committed May 13, 2016
1 parent bddc9fc commit 9ce8ca4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
package com.mapbox.mapboxsdk;

import android.content.Context;
import com.mapbox.mapboxsdk.telemetry.MapboxEventManager;

public class MapboxAccountManager {

private static MapboxAccountManager mapboxAccountManager = null;

private final String accessToken;
private final Context applicationContext;

/**
* MapboxAccountManager should NOT be instantiated directly.
* Use @see MapboxAccountManager#getInstance() instead.
* @param applicationContext
* @param accessToken Mapbox Access Token
*/
private MapboxAccountManager(String accessToken) {
private MapboxAccountManager(Context applicationContext, String accessToken) {
super();
this.applicationContext = applicationContext.getApplicationContext();
this.accessToken = accessToken;
}

/**
* Primary entry point to Mapbox for implementing developers.
* Must be configured in either Application.onCreate() or Launch Activity.onCreate()
*
* @param context Context used to get Application Context
* @param accessToken Mapbox Access Token. You can get one on the Mapbox Web site.
* @return MapboxAccountManager instance for app
*/
public static MapboxAccountManager start(String accessToken) {
public static MapboxAccountManager start(Context context, String accessToken) {
if (mapboxAccountManager == null) {
mapboxAccountManager = new MapboxAccountManager(accessToken);
mapboxAccountManager = new MapboxAccountManager(context, accessToken);
}
MapboxEventManager eventManager = MapboxEventManager.getMapboxEventManager();
eventManager.initialize(mapboxAccountManager.applicationContext, mapboxAccountManager.accessToken);
return mapboxAccountManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,6 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
//noinspection ResourceType
trackingSettings.setMyBearingTrackingMode(savedInstanceState.getInt(MapboxConstants.STATE_MY_BEARING_TRACKING_MODE, MyBearingTracking.NONE));
} else if (savedInstanceState == null) {
// Force a check for Telemetry
validateTelemetryServiceConfigured();

// Start Telemetry (authorization determined in initial MapboxEventManager constructor)
MapboxEventManager eventManager = MapboxEventManager.getMapboxEventManager();
eventManager.initialize(getContext(), getAccessToken());
Expand Down Expand Up @@ -889,24 +886,6 @@ private void validateAccessToken(String accessToken) {
}
}

// Checks that TelemetryService has been configured by developer
private void validateTelemetryServiceConfigured() {
try {
// Check Implementing app's AndroidManifest.xml
PackageInfo packageInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), PackageManager.GET_SERVICES);
if (packageInfo.services != null) {
for (ServiceInfo service : packageInfo.services) {
if (TextUtils.equals("com.mapbox.mapboxsdk.telemetry.TelemetryService", service.name)) {
return;
}
}
}
} catch (Exception e) {
Log.w(MapboxConstants.TAG, "Error checking for Telemetry Service Config: " + e);
}
throw new TelemetryServiceNotConfiguredException();
}

//
// Projection
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.Icon;
Expand All @@ -33,7 +32,6 @@
import com.mapbox.mapboxsdk.layers.CustomLayer;
import com.mapbox.mapboxsdk.location.LocationListener;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.content.res.Configuration;
import android.location.Location;
import android.net.ConnectivityManager;
Expand All @@ -26,6 +27,7 @@
import com.mapbox.mapboxsdk.BuildConfig;
import com.mapbox.mapboxsdk.constants.GeoConstants;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.TelemetryServiceNotConfiguredException;
import com.mapbox.mapboxsdk.location.LocationServices;
import com.mapbox.mapboxsdk.utils.MathUtils;
import org.json.JSONArray;
Expand Down Expand Up @@ -58,6 +60,7 @@ public class MapboxEventManager {

private static MapboxEventManager mapboxEventManager = null;

private boolean initialized = false;
private boolean telemetryEnabled;

private final Vector<Hashtable<String, Object>> events = new Vector<>();
Expand Down Expand Up @@ -104,9 +107,17 @@ private MapboxEventManager() {
* @param accessToken The accessToken to load MapView
*/
public void initialize(@NonNull Context context, @NonNull String accessToken) {

if (initialized) {
Log.d(TAG, "Mapbox Telemetry has already been initialized.");
return;
}

this.context = context.getApplicationContext();
this.accessToken = accessToken;

validateTelemetryServiceConfigured();

// Setup Message Digest
try {
messageDigest = MessageDigest.getInstance("SHA-1");
Expand Down Expand Up @@ -171,6 +182,8 @@ public void initialize(@NonNull Context context, @NonNull String accessToken) {
// Register for battery updates
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
batteryStatus = context.registerReceiver(null, iFilter);

initialized = true;
}

/**
Expand All @@ -185,6 +198,24 @@ public static MapboxEventManager getMapboxEventManager() {
return mapboxEventManager;
}

// Checks that TelemetryService has been configured by developer
private void validateTelemetryServiceConfigured() {
try {
// Check Implementing app's AndroidManifest.xml
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SERVICES);
if (packageInfo.services != null) {
for (ServiceInfo service : packageInfo.services) {
if (TextUtils.equals("com.mapbox.mapboxsdk.telemetry.TelemetryService", service.name)) {
return;
}
}
}
} catch (Exception e) {
Log.w(MapboxConstants.TAG, "Error checking for Telemetry Service Config: " + e);
}
throw new TelemetryServiceNotConfiguredException();
}

public static String generateCreateDate() {
return dateFormat.format(new Date());
}
Expand Down

0 comments on commit 9ce8ca4

Please sign in to comment.