package org.mozilla.focus.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RawRes;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

public class HtmlLoader {

    /**
     * Load a given (html or css) resource file into a String. The input can contain tokens that will
     * be replaced with localised strings.
     *
     * @param substitutionTable A table of substitions, e.g. %shortMessage% -> "Error loading page..."
     *                          Can be null, in which case no substitutions will be made.
     * @return The file content, with all substitutions having being made.
     */
    public static String loadResourceFile(@NonNull final Context context,
                                           @NonNull final @RawRes int resourceID,
                                           @Nullable final Map<String, String> substitutionTable) {

        BufferedReader fileReader = null;

        try {
            final InputStream fileStream = context.getResources().openRawResource(resourceID);
            fileReader = new BufferedReader(new InputStreamReader(fileStream));

            final StringBuilder outputBuffer = new StringBuilder();

            String line;
            while ((line = fileReader.readLine()) != null) {
                if (substitutionTable != null) {
                    for (final Map.Entry<String, String> entry : substitutionTable.entrySet()) {
                        line = line.replace(entry.getKey(), entry.getValue());
                    }
                }

                outputBuffer.append(line);
            }

            return outputBuffer.toString();
        } catch (final IOException e) {
            throw new IllegalStateException("Unable to load error page data");
        } finally {
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {
                // There's pretty much nothing we can do here. It doesn't seem right to crash
                // just because we couldn't close a file?
            }
        }
    }
}

@@ -59,16 +59,63 @@
 ontext;%0A
+import android.support.annotation.DrawableRes;%0A
 import a
@@ -149,16 +149,16 @@
 onNull;%0A
-
 import a
@@ -234,16 +234,44 @@
 .RawRes;
+%0Aimport android.util.Base64;
 %0A%0Aimport
@@ -2337,32 +2337,32 @@
 ?%0A            %7D%0A
-
         %7D%0A    %7D%0A
@@ -2360,11 +2360,2171 @@
  %7D%0A    %7D
+%0A%0A    private final static byte%5B%5D pngHeader = new byte%5B%5D %7B -119, 80, 78, 71, 13, 10, 26, 10 %7D;%0A%0A    public static String loadPngAsDataURI(@NonNull final Context context,%0A                                          @NonNull final @DrawableRes int resourceID) %7B%0A%0A        // We are copying the approach BitmapFactory.decodeResource(Resources, int, Options)%0A        // uses - you are explicitly allowed to open Drawables, but the method has a @RawRes%0A        // annotation (despite officially supporting Drawables).%0A        //noinspection ResourceType%0A        final InputStream pngInputStream = context.getResources().openRawResource(resourceID);%0A        final BufferedReader reader = new BufferedReader(new InputStreamReader(pngInputStream));%0A%0A        final StringBuilder builder = new StringBuilder();%0A        builder.append(%22data:image/png;base64,%22);%0A%0A        try %7B%0A            // Base64 encodes 3 bytes at a time, make sure we have a multiple of 3 here%0A            // I don't know what a sensible chunk size is, let's just go with 300b.%0A            final byte%5B%5D data = new byte%5B3*100%5D;%0A            int bytesRead;%0A            boolean headerVerified = false;%0A%0A            while ((bytesRead = pngInputStream.read(data)) %3E 0) %7B%0A                // Sanity check: lets make sure this is still a png (i.e. make sure the build system%0A                // or Android haven't broken / change the image format).%0A                if (!headerVerified) %7B%0A                    if (bytesRead %3C 8) %7B%0A                        throw new IllegalStateException(%22Loaded drawable is improbably small%22);%0A                    %7D%0A%0A                    for (int i = 0; i %3C pngHeader.length; i++) %7B%0A                        if (data%5Bi%5D != pngHeader%5Bi%5D) %7B%0A                            throw new IllegalStateException(%22Invalid png detected%22);%0A                        %7D%0A                    %7D%0A                    headerVerified = true;%0A                %7D%0A%0A                builder.append(Base64.encodeToString(data, 0, bytesRead, 0));%0A            %7D%0A        %7D catch (IOException e) %7B%0A            throw new IllegalStateException(%22Unable to load png data%22);%0A        %7D%0A%0A        return  builder.toString();%0A    %7D
 %0A%7D%0A

Implement loadPngAsDataURI