blob: 4f968c3a135a0eaafbdc3f9c5536e02520a18992 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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.support.annotation.Nullable;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
/**
* {@code ApiAccess} provides a method to load the Mapbox access token.
*/
public final class ApiAccess {
/**
* <p>
* Returns the Mapbox access token set in the app resources.
* </p>
* It will first search the application manifest for a {@link MapboxConstants#KEY_META_DATA_MANIFEST}
* meta-data value. If not found it will then attempt to load the access token from the
* {@code res/raw/token.txt} 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.
* @see MapboxConstants#KEY_META_DATA_MANIFEST
*/
public static String getToken(@NonNull Context context) {
try {
// read out AndroidManifest
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
String token = appInfo.metaData.getString(MapboxConstants.KEY_META_DATA_MANIFEST);
if (token == null || token.isEmpty()) {
throw new IllegalArgumentException();
}
return token;
} catch (Exception e) {
// 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;
}
}
}
|