package edu.mines.alterego;

import android.content.res.AssetManager;
import android.util.Log;

import com.google.android.gms.maps.model.TileProvider;
import com.google.android.gms.maps.model.Tile;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;

public class CustomMapTileProvider implements TileProvider {
    private static final int TILE_WIDTH = 256;
    private static final int TILE_HEIGHT = 256;
    private static final int BUFFER_SIZE = 16 * 1024;

    private AssetManager mAssets;

    public CustomMapTileProvider(AssetManager assets) {
        mAssets = assets;
    }

    @Override
    public Tile getTile(int x, int y, int zoom) {
        byte[] image = readTileImage(x, y, zoom);
        return image == null ? null : new Tile(TILE_WIDTH, TILE_HEIGHT, image);
    }

    private byte[] readTileImage(int x, int y, int zoom) {
        InputStream in = null;
        ByteArrayOutputStream buffer = null;

        try {
            in = mAssets.open(getTileFilename(x, y, zoom));
            buffer = new ByteArrayOutputStream();

            int nRead;
            byte[] data = new byte[BUFFER_SIZE];

            while ((nRead = in.read(data, 0, BUFFER_SIZE)) != -1) {
                buffer.write(data, 0, nRead);
            }
            buffer.flush();

            return buffer.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        } finally {
            if (in != null) try { in.close(); } catch (Exception ignored) {}
            if (buffer != null) try { buffer.close(); } catch (Exception ignored) {}
        }
    }

    private String getTileFilename(int x, int y, int zoom) {
        //return "map/" + zoom + '/' + x + '/' + y + ".png";
        return "kittens.png";
    }
}

@@ -285,16 +285,39 @@
 ception;
+%0Aimport java.lang.Math;
 %0A%0Apublic
@@ -1416,32 +1416,34 @@
 ) %7B%0A            
+//
 e.printStackTrac
@@ -1439,32 +1439,32 @@
 ntStackTrace();%0A
-
             retu
@@ -1523,16 +1523,18 @@
         
+//
 e.printS
@@ -1843,50 +1843,259 @@
     
-//return %22map/%22 + zoom + '/' + x + '/' + y
+Log.d(%22AlterEgo::CustomMapTileProvider%22, %22Getting tile at zoom=%22 + zoom + %22 x=%22 + x + %22 y=%22 + y);%0A        //return %22MapQuest/%22 + zoom + '/' + x + '/' + y + %22.jpg%22;%0A        return %22MapMiddleEarth/%22 + zoom + '/' + x + '/' + ((int) Math.pow(2, zoom) - y)
  + %22
@@ -2101,32 +2101,34 @@
 %22.png%22;%0A        
+//
 return %22kittens.
@@ -2129,20 +2129,56 @@
 ttens.png%22;%0A
+        //return %22MiddleEarth.jpg%22;%0A
     %7D%0A%7D%0A

Make change to comply with MapTiler's output format (x-y coordination) in vectorization form