|
| 1 | +package io.sentry.react; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import com.facebook.react.bridge.ReadableMap; |
| 5 | +import io.sentry.ILogger; |
| 6 | +import io.sentry.SentryLevel; |
| 7 | +import io.sentry.android.core.AndroidLogger; |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.util.Map; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import org.json.JSONObject; |
| 14 | + |
| 15 | +public final class RNSentrySDK { |
| 16 | + private static final String CONFIGURATION_FILE = "sentry.options.json"; |
| 17 | + private static final String NAME = "RNSentrySDK"; |
| 18 | + |
| 19 | + private static final ILogger logger = new AndroidLogger(NAME); |
| 20 | + |
| 21 | + private RNSentrySDK() { |
| 22 | + throw new AssertionError("Utility class should not be instantiated"); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Start the Native Android SDK with the provided options |
| 27 | + * |
| 28 | + * @param context Android Context |
| 29 | + * @param options Map with options |
| 30 | + */ |
| 31 | + public static void init( |
| 32 | + @NotNull final Context context, @NotNull final Map<String, Object> options) { |
| 33 | + ReadableMap rnOptions = RNSentryMapConverter.mapToReadableMap(options); |
| 34 | + RNSentryStart.startWithOptions(context, rnOptions, null, logger); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Start the Native Android SDK with options from `sentry.options.json` configuration file |
| 39 | + * |
| 40 | + * @param context Android Context |
| 41 | + */ |
| 42 | + public static void init(@NotNull final Context context) { |
| 43 | + try { |
| 44 | + JSONObject jsonObject = getOptionsFromConfigurationFile(context); |
| 45 | + ReadableMap rnOptions = RNSentryMapConverter.jsonObjectToReadableMap(jsonObject); |
| 46 | + RNSentryStart.startWithOptions(context, rnOptions, null, logger); |
| 47 | + } catch (Exception e) { |
| 48 | + logger.log( |
| 49 | + SentryLevel.ERROR, "Failed to start Sentry with options from configuration file.", e); |
| 50 | + throw new RuntimeException(e); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static JSONObject getOptionsFromConfigurationFile(Context context) { |
| 55 | + try (InputStream inputStream = context.getAssets().open(CONFIGURATION_FILE); |
| 56 | + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { |
| 57 | + |
| 58 | + StringBuilder stringBuilder = new StringBuilder(); |
| 59 | + String line; |
| 60 | + while ((line = reader.readLine()) != null) { |
| 61 | + stringBuilder.append(line); |
| 62 | + } |
| 63 | + String configFileContent = stringBuilder.toString(); |
| 64 | + return new JSONObject(configFileContent); |
| 65 | + |
| 66 | + } catch (Exception e) { |
| 67 | + logger.log( |
| 68 | + SentryLevel.ERROR, |
| 69 | + "Failed to read configuration file. Please make sure " |
| 70 | + + CONFIGURATION_FILE |
| 71 | + + " exists in the root of your project.", |
| 72 | + e); |
| 73 | + return null; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments