[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
This commit is contained in:
Pablo Guardiola
2017-05-12 12:34:03 +02:00
committed by GitHub
parent 4d6f54553d
commit 85cb04be22
3 changed files with 59 additions and 6 deletions

View File

@@ -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)
}
}

View File

@@ -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() {

View File

@@ -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;
}
}
}