From 85cb04be22e8d996d3d3447a252ed936eb6f3659 Mon Sep 17 00:00:00 2001 From: Pablo Guardiola Date: Fri, 12 May 2017 12:34:03 +0200 Subject: [android] Fix gradle developer config creation task with discussed suggestions (#8897) * fix gradle developer config creation task with discussed suggestions * fix access token not set error message --- .../MapboxGLAndroidSDKTestApp/gradle-config.gradle | 11 ++++--- .../mapboxsdk/testapp/MapboxApplication.java | 17 +++++++++- .../mapbox/mapboxsdk/testapp/utils/TokenUtils.java | 37 ++++++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/TokenUtils.java diff --git a/platform/android/MapboxGLAndroidSDKTestApp/gradle-config.gradle b/platform/android/MapboxGLAndroidSDKTestApp/gradle-config.gradle index 1068e5e69e..8346806633 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/gradle-config.gradle +++ b/platform/android/MapboxGLAndroidSDKTestApp/gradle-config.gradle @@ -5,14 +5,15 @@ task accessToken { def tokenFile = new File("${projectDir}/src/main/res/values/developer-config.xml") if (!tokenFile.exists()) { + String mapboxAccessToken = "$System.env.MAPBOX_ACCESS_TOKEN" + if (mapboxAccessToken == "null") { + System.out.println("You should set the MAPBOX_ACCESS_TOKEN environment variable.") + mapboxAccessToken = "YOUR_MAPBOX_ACCESS_TOKEN_GOES_HERE" + } String tokenFileContents = "\n" + "\n" + - " " + "$System.env.MAPBOX_ACCESS_TOKEN" + "\n" + + " " + mapboxAccessToken + "\n" + "" - - if (tokenFileContents == null) { - throw new InvalidUserDataException("You must set the MAPBOX_ACCESS_TOKEN environment variable.") - } tokenFile.write(tokenFileContents) } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java index e344343627..deee312bb3 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java @@ -2,8 +2,11 @@ package com.mapbox.mapboxsdk.testapp; import android.app.Application; import android.os.StrictMode; +import android.text.TextUtils; +import android.util.Log; import com.mapbox.mapboxsdk.Mapbox; +import com.mapbox.mapboxsdk.testapp.utils.TokenUtils; import com.squareup.leakcanary.LeakCanary; import timber.log.Timber; @@ -18,6 +21,13 @@ import static timber.log.Timber.DebugTree; */ public class MapboxApplication extends Application { + private static final String LOG_TAG = MapboxApplication.class.getSimpleName(); + private static final String DEFAULT_MAPBOX_ACCESS_TOKEN = "YOUR_MAPBOX_ACCESS_TOKEN_GOES_HERE"; + private static final String ACCESS_TOKEN_NOT_SET_MESSAGE = "In order to run the Test App you need to set a valid " + + "access token. During development, you can set the MAPBOX_ACCESS_TOKEN environment variable for the SDK to " + + "automatically include it in the Test App. Otherwise, you can manually include it in the " + + "res/values/developer-config.xml file in the MapboxGLAndroidSDKTestApp folder."; + @Override public void onCreate() { super.onCreate(); @@ -43,7 +53,12 @@ public class MapboxApplication extends Application { .penaltyDeath() .build()); - Mapbox.getInstance(getApplicationContext(), getString(R.string.mapbox_access_token)); + String mapboxAccessToken = TokenUtils.getMapboxAccessToken(getApplicationContext()); + if (TextUtils.isEmpty(mapboxAccessToken) || mapboxAccessToken.equals(DEFAULT_MAPBOX_ACCESS_TOKEN)) { + Log.w(LOG_TAG, ACCESS_TOKEN_NOT_SET_MESSAGE); + } + + Mapbox.getInstance(getApplicationContext(), mapboxAccessToken); } private void initializeLogger() { diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/TokenUtils.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/TokenUtils.java new file mode 100644 index 0000000000..e08fdb9154 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/TokenUtils.java @@ -0,0 +1,37 @@ +package com.mapbox.mapboxsdk.testapp.utils; + + +import android.content.Context; +import android.support.annotation.NonNull; + +import com.mapbox.mapboxsdk.Mapbox; + +public class TokenUtils { + + /** + *

+ * Returns the Mapbox access token set in the app resources. + *

+ * It will first search for a token in the Mapbox object. If not found it + * will then attempt to load the access token from the + * {@code res/values/dev.xml} development file. + * + * @param context The {@link Context} of the {@link android.app.Activity} or {@link android.app.Fragment}. + * @return The Mapbox access token or null if not found. + */ + public static String getMapboxAccessToken(@NonNull Context context) { + try { + // Read out AndroidManifest + String token = Mapbox.getAccessToken(); + if (token == null || token.isEmpty()) { + throw new IllegalArgumentException(); + } + return token; + } catch (Exception exception) { + // Use fallback on string resource, used for development + int tokenResId = context.getResources() + .getIdentifier("mapbox_access_token", "string", context.getPackageName()); + return tokenResId != 0 ? context.getString(tokenResId) : null; + } + } +} -- cgit v1.2.1