[android] #2462 refactored ApiAccess token setup

This commit is contained in:
Tobrun
2015-10-08 10:06:33 +02:00
committed by Brad Leege
parent bd41ae1deb
commit fd45a206ec
6 changed files with 52 additions and 35 deletions

View File

@@ -7,4 +7,7 @@ public class MapboxConstants {
// Default Locale for data processing (ex: String.toLowerCase(MAPBOX_LOCALE, "foo"))
public static final Locale MAPBOX_LOCALE = Locale.US;
// Key used to store access token in AndroidManifest.xml
public static final String KEY_META_DATA_MANIFEST = "com.mapbox.AccessToken";
}

View File

@@ -0,0 +1,44 @@
package com.mapbox.mapboxsdk.utils;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ApiAccess {
public static String getToken(@NonNull Context context) {
String accessToken = getReleaseToken(context);
if (TextUtils.isEmpty(accessToken)) {
accessToken = getDevelopmentToken(context);
}
return accessToken;
}
private static String getReleaseToken(@NonNull Context context){
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
return appInfo.metaData.getString(MapboxConstants.KEY_META_DATA_MANIFEST);
} catch (Exception e) {
return null;
}
}
private static String getDevelopmentToken(@NonNull Context context) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(R.raw.token)));
return reader.readLine();
}catch (IOException e){
return null;
}
}
}

View File

@@ -38,7 +38,7 @@
android:value="9724157045ff7d083492c6d9ae03e60e8609d461" />
<meta-data
android:name="com.mapbox.accessToken"
android:name="com.mapbox.AccessToken"
android:value="" />
</application>

View File

@@ -12,6 +12,7 @@ import android.widget.TextView;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import com.mapbox.mapboxsdk.views.MapView;
public class InfoWindowAdapterActivity extends AppCompatActivity {
@@ -35,7 +36,7 @@ public class InfoWindowAdapterActivity extends AppCompatActivity {
}
mMapView = (MapView) findViewById(R.id.secondMapView);
mMapView.setAccessToken(Util.getAccessToken(this));
mMapView.setAccessToken(ApiAccess.getToken(this));
mMapView.onCreate(savedInstanceState);
mMapView.setStyleUrl(MapView.StyleUrls.MAPBOX_STREETS);
mMapView.setInfoWindowAdapter(new MapView.InfoWindowAdapter() {

View File

@@ -30,6 +30,7 @@ import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import com.mapbox.mapboxsdk.views.MapView;
import io.fabric.sdk.android.Fabric;
@@ -100,7 +101,7 @@ public class MainActivity extends AppCompatActivity {
}
mMapView = (MapView) findViewById(R.id.mainMapView);
mMapView.setAccessToken(Util.getAccessToken(this));
mMapView.setAccessToken(ApiAccess.getToken(this));
mMapView.onCreate(savedInstanceState);
mMapView.setOnFpsChangedListener(new MyOnFpsChangedListener());

View File

@@ -63,36 +63,4 @@ public class Util {
}
return sb.toString();
}
// TODO move this to SDK
public static String getAccessToken(Context context) {
String accessToken = getReleaseAccessToken(context);
if (TextUtils.isEmpty(accessToken)) {
accessToken = getDevelopmentAccessToken(context);
}
// TODO add validation + throw exception
return accessToken;
}
// TODO move this to SDK
private static String getReleaseAccessToken(Context context){
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
return appInfo.metaData.getString("com.mapbox.accessToken");
} catch (Exception e) {
Log.e(TAG, "Failed to load AccessToken: " + e.getMessage());
return null;
}
}
// TODO move this to SDK
private static String getDevelopmentAccessToken(Context context) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(R.raw.token)));
return reader.readLine();
}catch (IOException e){
return null;
}
}
}