package de.koffeinsucht.faketag;

import android.content.Intent;
import android.nfc.cardemulation.HostApduService;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

public class CardEmuService extends HostApduService {

    private HashMap<String, String> cmdMap = null;
    private boolean initialized = false;

    @Override
    public byte[]  processCommandApdu(byte[] apdu, Bundle extras) {
        // is initialized?
        if (!initialized) {
            parseJSONFile();
            initialized = true;
        }

        // get apdu bytes from cmdMap
        byte[] bytes = cmdMap.get(apdu.toString()).getBytes();
        if (bytes != null) {
            return bytes;
        }
        else { // command not supported by json file
            return new byte[]{0x68,0x00}; // => "The request function is not supported by the card."
        }
    }

    @Override
    public void onDeactivated(int reason) {

    }

    public void parseJSONFile() {
        try
        {
            //Load File
            InputStream jsonReader = this.getResources().openRawResource(R.raw.example);
            int size = jsonReader.available();

            byte[] buffer = new byte[size];
            jsonReader.read(buffer);
            jsonReader.close();
            String json = new String(buffer, "UTF-8");

            JSONArray jsonArray = new JSONArray(json); //jsonBuilder.toString());
            for (int index = 0; index < jsonArray.length(); index++) {
                //add values to map
                cmdMap.put(jsonArray.getJSONObject(index).getString("request"), jsonArray.getJSONObject(index).getString("response"));
            }
        } catch (FileNotFoundException e) {
            Log.e("jsonFile", "file not found");
        } catch (IOException e) {
            Log.e("jsonFile", "ioerror");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}

@@ -31,39 +31,8 @@
 g;%0A%0A
-import android.content.Intent;%0A
 impo
@@ -103,16 +103,44 @@
 Bundle;%0A
+import android.util.Base64;%0A
 import a
@@ -1692,18 +1692,38 @@
     
-cmdMap.put
+byte%5B%5D request = Base64.decode
 (jso
@@ -1776,59 +1776,209 @@
 %22), 
-jsonArray.getJSONObject(index).getString(%22response%22
+Base64.DEFAULT);%0A                byte%5B%5D response = Base64.decode(jsonArray.getJSONObject(index).getString(%22response%22), Base64.DEFAULT);%0A                cmdMap.put(request.toString(), response.toString(
 ));%0A

add decoding of base64