<commit_before>package com.mapbox.reactnativemapboxgl;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.RequiresPermission;
import android.view.View;

import com.facebook.react.bridge.ReadableMap;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ReactNativeMapboxGLAnnotationFactory {

    static Drawable drawableFromDrawableName(View view, String drawableName) {
        Bitmap x;
        int resID = view.getResources().getIdentifier(drawableName, "drawable", view.getContext().getApplicationContext().getPackageName());
        x = BitmapFactory.decodeResource(view.getResources(), resID);
        return new BitmapDrawable(view.getResources(), x);
    }

    public static Drawable drawableFromUrl(View view, String url) throws IOException {
        Bitmap x;

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();

        x = BitmapFactory.decodeStream(input);
        return new BitmapDrawable(view.getResources(), x);
    }

    public static MarkerOptions markerFromJS(ReadableMap annotation, View view) {
        MarkerOptions marker = new MarkerOptions();

        double latitude = annotation.getArray("coordinates").getDouble(0);
        double longitude = annotation.getArray("coordinates").getDouble(1);
        LatLng markerCenter = new LatLng(latitude, longitude);
        marker.position(markerCenter);

        if (annotation.hasKey("title")) {
            String title = annotation.getString("title");
            marker.title(title);
        }

        if (annotation.hasKey("subtitle")) {
            String subtitle = annotation.getString("subtitle");
            marker.snippet(subtitle);
        }

        if (annotation.hasKey("annotationImage")) {
            ReadableMap annotationImage = annotation.getMap("annotationImage");
            String annotationURL = annotationImage.getString("url");
            try {
                Drawable image;
                if (annotationURL.startsWith("image!")) {
                    image = drawableFromDrawableName(view, annotationURL.replace("image!", ""));
                } else {
                    image = drawableFromUrl(view, annotationURL);
                }

                IconFactory iconFactory = IconFactory.getInstance(view.getContext());
                Icon icon;
                if (annotationImage.hasKey("height") && annotationImage.hasKey("width")) {
                    float scale = view.getResources().getDisplayMetrics().density;
                    int height = Math.round((float)annotationImage.getInt("height") * scale);
                    int width = Math.round((float)annotationImage.getInt("width") * scale);
                    icon = iconFactory.fromDrawable(image, width, height);
                } else {
                    icon = iconFactory.fromDrawable(image);
                }

                marker.icon(icon);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return marker;
    }
<commit_msg>Cache annotation images<commit_after>package com.mapbox.reactnativemapboxgl;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.RequiresPermission;
import android.view.View;

import com.facebook.react.bridge.ReadableMap;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class ReactNativeMapboxGLAnnotationFactory {

    static Drawable drawableFromDrawableName(Context context, String drawableName) {
        int resID = context.getResources().getIdentifier(drawableName, "drawable", context.getApplicationContext().getPackageName());
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
        return new BitmapDrawable(context.getResources(), bitmap);
    }

    public static Drawable drawableFromUrl(Context context, String url) throws IOException {
        Bitmap x;

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();

        x = BitmapFactory.decodeStream(input);
        return new BitmapDrawable(context.getResources(), x);
    }

    static Map<String, Drawable> drawableCache = new HashMap();

    public static Drawable drawableFromPath(Context context, String path) throws IOException {
        Drawable drawable = drawableCache.get(path);
        if (drawable != null) { return drawable; }

        if (path.startsWith("image!")) {
            drawable = drawableFromDrawableName(context, path.replace("image!", ""));
        } else {
            drawable = drawableFromUrl(context, path);
        }

        drawableCache.put(path, drawable);
        return drawable;
    }

    public static MarkerOptions markerFromJS(ReadableMap annotation, View view) {
        MarkerOptions marker = new MarkerOptions();

        double latitude = annotation.getArray("coordinates").getDouble(0);
        double longitude = annotation.getArray("coordinates").getDouble(1);
        LatLng markerCenter = new LatLng(latitude, longitude);
        marker.position(markerCenter);

        if (annotation.hasKey("title")) {
            String title = annotation.getString("title");
            marker.title(title);
        }

        if (annotation.hasKey("subtitle")) {
            String subtitle = annotation.getString("subtitle");
            marker.snippet(subtitle);
        }

        if (annotation.hasKey("annotationImage")) {
            ReadableMap annotationImage = annotation.getMap("annotationImage");
            String annotationURL = annotationImage.getString("url");
            try {
                Drawable image = drawableFromPath(view.getContext(), annotationURL);

                IconFactory iconFactory = IconFactory.getInstance(view.getContext());
                Icon icon;
                if (annotationImage.hasKey("height") && annotationImage.hasKey("width")) {
                    float scale = view.getResources().getDisplayMetrics().density;
                    int height = Math.round((float)annotationImage.getInt("height") * scale);
                    int width = Math.round((float)annotationImage.getInt("width") * scale);
                    icon = iconFactory.fromDrawable(image, width, height);
                } else {
                    icon = iconFactory.fromDrawable(image);
                }

                marker.icon(icon);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return marker;
    }

{'fraction_non_alphanumeric': 0.10325951729285893, 'fraction_numerical': 0.0004976362279173924, 'mean_word_length': 2.989578163771712, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3961769', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1999, 'n_words': 398}