[android] #5004 - Introducing MapboxAccountManager

This commit is contained in:
Brad Leege
2016-05-11 11:01:18 -05:00
parent 0d5d2a923b
commit 635993b4b6
30 changed files with 156 additions and 71 deletions

View File

@@ -0,0 +1,74 @@
package com.mapbox.mapboxsdk;
import android.content.Context;
import android.text.TextUtils;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException;
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 Context used to get ApplicationContext
* @param accessToken Mapbox Access Token
*/
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(Context context, String accessToken) {
if (mapboxAccountManager == null) {
mapboxAccountManager = new MapboxAccountManager(context, accessToken);
}
MapboxEventManager eventManager = MapboxEventManager.getMapboxEventManager();
eventManager.initialize(mapboxAccountManager.applicationContext, mapboxAccountManager.accessToken);
return mapboxAccountManager;
}
/**
* Internal Use Only
* Get an instance of MapboxAccountManager configured with the app's Access Token
*
* @return MapboxAccountManager instance for app. May be NULL if not configured yet.
*/
public static MapboxAccountManager getInstance() {
return mapboxAccountManager;
}
/**
* Access Token for this application
* @return Mapbox Access Token
*/
public String getAccessToken() {
return accessToken;
}
/**
* Runtime validation of Access Token
*
* @param accessToken Access Token to check
* @throws InvalidAccessTokenException
*/
public static void validateAccessToken(String accessToken) throws InvalidAccessTokenException {
if (TextUtils.isEmpty(accessToken) || (!accessToken.toLowerCase(MapboxConstants.MAPBOX_LOCALE).startsWith("pk.") && !accessToken.toLowerCase(MapboxConstants.MAPBOX_LOCALE).startsWith("sk."))) {
throw new InvalidAccessTokenException();
}
}
}

View File

@@ -81,7 +81,6 @@ public class MapboxConstants {
public static final String STATE_ZOOM_CONTROLS_ENABLED = "zoomControlsEnabled";
public static final String STATE_DEBUG_ACTIVE = "debugActive";
public static final String STATE_STYLE_URL = "styleUrl";
public static final String STATE_ACCESS_TOKEN = "accessToken";
public static final String STATE_MY_LOCATION_ENABLED = "myLocationEnabled";
public static final String STATE_MY_LOCATION_TRACKING_MODE = "myLocationTracking";
public static final String STATE_MY_BEARING_TRACKING_MODE = "myBearingTracking";

View File

@@ -10,9 +10,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
@@ -60,6 +58,7 @@ import android.widget.ZoomButtonsController;
import com.almeros.android.multitouch.gesturedetectors.RotateGestureDetector;
import com.almeros.android.multitouch.gesturedetectors.ShoveGestureDetector;
import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector;
import com.mapbox.mapboxsdk.MapboxAccountManager;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.Icon;
@@ -75,8 +74,6 @@ import com.mapbox.mapboxsdk.constants.MyBearingTracking;
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.exceptions.IconBitmapChangedException;
import com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException;
import com.mapbox.mapboxsdk.exceptions.TelemetryServiceNotConfiguredException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.layers.CustomLayer;
@@ -88,7 +85,6 @@ import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
import com.mapbox.mapboxsdk.telemetry.MapboxEvent;
import com.mapbox.mapboxsdk.telemetry.MapboxEventManager;
import com.mapbox.mapboxsdk.utils.ColorUtils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.nio.ByteBuffer;
@@ -113,7 +109,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
* <strong>Warning:</strong> Please note that you are responsible for getting permission to use the map data,
* and for ensuring your use adheres to the relevant terms of use.
*
* @see MapView#setAccessToken(String)
*/
public class MapView extends FrameLayout {
@@ -258,7 +253,13 @@ public class MapView extends FrameLayout {
mMapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(position));
}
String accessToken = options.getAccessToken();
String accessToken = null;
if (MapboxAccountManager.getInstance() != null) {
accessToken = MapboxAccountManager.getInstance().getAccessToken();
} else {
accessToken = options.getAccessToken();
}
String style = options.getStyle();
if (!TextUtils.isEmpty(accessToken)) {
mMapboxMap.setAccessToken(accessToken);
@@ -355,7 +356,7 @@ public class MapView extends FrameLayout {
@UiThread
public void onCreate(@Nullable Bundle savedInstanceState) {
// Force a check for an access token
validateAccessToken(getAccessToken());
MapboxAccountManager.validateAccessToken(getAccessToken());
if (savedInstanceState != null && savedInstanceState.getBoolean(MapboxConstants.STATE_HAS_SAVED_STATE)) {
@@ -402,7 +403,6 @@ public class MapView extends FrameLayout {
mMapboxMap.setDebugActive(savedInstanceState.getBoolean(MapboxConstants.STATE_DEBUG_ACTIVE));
mMapboxMap.setStyleUrl(savedInstanceState.getString(MapboxConstants.STATE_STYLE_URL));
setAccessToken(savedInstanceState.getString(MapboxConstants.STATE_ACCESS_TOKEN));
// User location
try {
@@ -418,10 +418,8 @@ public class MapView extends FrameLayout {
//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)
Log.i(MapView.class.getCanonicalName(), "MapView start Telemetry...");
MapboxEventManager eventManager = MapboxEventManager.getMapboxEventManager();
eventManager.initialize(getContext(), getAccessToken());
}
@@ -473,7 +471,6 @@ public class MapView extends FrameLayout {
outState.putParcelable(MapboxConstants.STATE_CAMERA_POSITION, mMapboxMap.getCameraPosition());
outState.putBoolean(MapboxConstants.STATE_DEBUG_ACTIVE, mMapboxMap.isDebugActive());
outState.putString(MapboxConstants.STATE_STYLE_URL, mStyleInitializer.getStyle());
outState.putString(MapboxConstants.STATE_ACCESS_TOKEN, mMapboxMap.getAccessToken());
outState.putBoolean(MapboxConstants.STATE_MY_LOCATION_ENABLED, mMapboxMap.isMyLocationEnabled());
// TrackingSettings
@@ -837,6 +834,10 @@ public class MapView extends FrameLayout {
//
/**
* <p>
* DEPRECATED @see MapboxAccountManager#start(String)
* </p>
*
* <p>
* Sets the current Mapbox access token used to load map styles and tiles.
* </p>
@@ -848,6 +849,7 @@ public class MapView extends FrameLayout {
* @param accessToken Your public Mapbox access token.
* @see MapView#onCreate(Bundle)
*/
@Deprecated
@UiThread
public void setAccessToken(@NonNull String accessToken) {
if (mDestroyed) {
@@ -857,15 +859,20 @@ public class MapView extends FrameLayout {
if (!TextUtils.isEmpty(accessToken)) {
accessToken = accessToken.trim();
}
validateAccessToken(accessToken);
MapboxAccountManager.validateAccessToken(accessToken);
mNativeMapView.setAccessToken(accessToken);
}
/**
* <p>
* DEPRECATED @see MapboxAccountManager#getAccessToken()
* </p>
*
* Returns the current Mapbox access token used to load map styles and tiles.
*
* @return The current Mapbox access token.
*/
@Deprecated
@UiThread
@Nullable
public String getAccessToken() {
@@ -875,31 +882,6 @@ public class MapView extends FrameLayout {
return mNativeMapView.getAccessToken();
}
// Checks if the given token is valid
private void validateAccessToken(String accessToken) {
if (TextUtils.isEmpty(accessToken) || (!accessToken.startsWith("pk.") && !accessToken.startsWith("sk."))) {
throw new InvalidAccessTokenException();
}
}
// 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
//

View File

@@ -11,7 +11,6 @@ import android.support.v4.util.LongSparseArray;
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;
@@ -33,7 +32,6 @@ import com.mapbox.mapboxsdk.geometry.LatLng;
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;
@@ -583,6 +581,10 @@ public class MapboxMap {
//
/**
* <p>
* DEPRECATED @see MapboxAccountManager#start(String)
* </p>
*
* <p>
* Sets the current Mapbox access token used to load map styles and tiles.
* </p>
@@ -590,16 +592,22 @@ public class MapboxMap {
* @param accessToken Your public Mapbox access token.
* @see MapView#setAccessToken(String)
*/
@Deprecated
@UiThread
public void setAccessToken(@NonNull String accessToken) {
mMapView.setAccessToken(accessToken);
}
/**
* <p>
* DEPRECATED @see MapboxAccountManager#getAccessToken()
* </p>
*
* Returns the current Mapbox access token used to load map styles and tiles.
*
* @return The current Mapbox access token.
*/
@Deprecated
@UiThread
@Nullable
public String getAccessToken() {

View File

@@ -13,12 +13,10 @@ import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.utils.ColorUtils;
import java.util.Arrays;
/**
@@ -74,6 +72,7 @@ public class MapboxMapOptions implements Parcelable {
private int myLocationAccuracyAlpha;
private String style;
@Deprecated
private String accessToken;
/**
@@ -217,11 +216,15 @@ public class MapboxMapOptions implements Parcelable {
}
/**
* <p>
* DEPRECATED @see MapboxAccountManager#start(String)
* </p>
* Specifies the accesstoken associated with a map view.
*
* @param accessToken Token to be used to access the service
* @return This
*/
@Deprecated
public MapboxMapOptions accessToken(String accessToken) {
this.accessToken = accessToken;
return this;
@@ -595,10 +598,14 @@ public class MapboxMapOptions implements Parcelable {
}
/**
* <p>
* DEPRECATED @see MapboxAccountManager#start(String)
* </p>
* Get the current configured access token for a map view.
*
* @return Access token to be used.
*/
@Deprecated
public String getAccessToken() {
return accessToken;
}

View File

@@ -8,6 +8,7 @@ import android.content.SharedPreferences;
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;
@@ -26,6 +27,7 @@ import android.view.WindowManager;
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;
@@ -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<>();
@@ -104,9 +107,19 @@ public class MapboxEventManager {
* @param accessToken The accessToken to load MapView
*/
public void initialize(@NonNull Context context, @NonNull String accessToken) {
Log.i(TAG, "Telemetry initialize() called...");
if (initialized) {
Log.i(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");
@@ -117,6 +130,7 @@ public class MapboxEventManager {
SharedPreferences prefs = context.getSharedPreferences(MapboxConstants.MAPBOX_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
// Determine if Telemetry Should Be Enabled
Log.i(TAG, "Right before Telemetry set enabled in initialized()");
setTelemetryEnabled(prefs.getBoolean(MapboxConstants.MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_ENABLED, true));
// Load / Create Vendor Id
@@ -171,6 +185,8 @@ public class MapboxEventManager {
// Register for battery updates
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
batteryStatus = context.registerReceiver(null, iFilter);
initialized = true;
}
/**
@@ -185,6 +201,24 @@ public class MapboxEventManager {
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());
}
@@ -198,6 +232,7 @@ public class MapboxEventManager {
* @param telemetryEnabled True to start telemetry, false to stop it
*/
public void setTelemetryEnabled(boolean telemetryEnabled) {
Log.i(TAG, "setTelemetryEnabled(); this.telemetryEnabled = " + this.telemetryEnabled + "; telemetryEnabled = " + telemetryEnabled);
if (this.telemetryEnabled == telemetryEnabled) {
Log.d(TAG, "No need to start / stop telemetry as it's already in that state.");
return;