diff options
author | Pablo Guardiola <guardiola31337@gmail.com> | 2017-05-12 12:34:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-12 12:34:03 +0200 |
commit | 85cb04be22e8d996d3d3447a252ed936eb6f3659 (patch) | |
tree | fd07d95c2d8c4918e42bc400d5057fe0ede9824f /platform | |
parent | 4d6f54553d277c0af24a0b8ff03d2dd7e9284ca2 (diff) | |
download | qtlocation-mapboxgl-85cb04be22e8d996d3d3447a252ed936eb6f3659.tar.gz |
[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
Diffstat (limited to 'platform')
3 files changed, 59 insertions, 6 deletions
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 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + - " <string name=\"mapbox_access_token\">" + "$System.env.MAPBOX_ACCESS_TOKEN" + "</string>\n" + + " <string name=\"mapbox_access_token\">" + mapboxAccessToken + "</string>\n" + "</resources>" - - 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 { + + /** + * <p> + * Returns the Mapbox access token set in the app resources. + * </p> + * 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; + } + } +} |