com.modestmaps.providers.connection.TileBaseHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.modestmaps.providers.connection.TileBaseHelper.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.modestmaps.providers.connection;

import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import com.modestmaps.InteractiveMap;
import com.modestmaps.mapextends.MapPApplet;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import processing.core.PApplet;
//import static processing.core.PApplet.map;

/**
 *
 * @author jedi-Knight
 */
public class TileBaseHelper {

    /**TODO: storage path and dbname**/
    public static String DBNAME = "KTMHS.mbtiles";
    private String DBPATH = "/sdcard/";
    //private static final String TABLE_NAME = "images";
    private Context context;
    //PApplet p = (PApplet) context;
    //System.out.println("1. openDatabase()..");
    //        private SQLiteDatabase tileDB = SQLiteDatabase.openDatabase(DBPATH+DBNAME, null,
    //                                                    SQLiteDatabase.OPEN_READWRITE);

    private SQLiteDatabase tileDB = null;

    private String serverADD;

    /*If using MBTiles generated by Tilemill, do not change these: */
    private String tableName = "images";
    private String columnName = "tile_data";
    /****/

    BitmapFactory.Options bmgOptions;

    PApplet p;

    public InteractiveMap map = null;

    Bitmap bmg;

    private long totalBytes = 10485760;
    private long downloadedBytes = 0;

    //InteractiveMap p.map = null;

    //constructor  
    public TileBaseHelper(PApplet context, String serverADD) {
        //                              tileDB.setLockingEnabled(true);
        this.context = context;
        this.serverADD = serverADD;
        bmgOptions = new BitmapFactory.Options();
        this.p = context;
        //this.bmgOptions.inPurgeable = true;
        //bmgOptions.inJustDecodeBounds = true;
        bmgOptions.inTempStorage = new byte[] { 4 };
        System.out.println("1. TileBaseHelper instantiated..");
        //TileDBOpenHelper tiledbopenHelper = new TileDBOpenHelper(this.context);
        //                 try {
        //                      this.createDatabase();
        //                 }
        //                 catch (IOException e) {
        //                      System.out.println("ERRRRRROOORRRR!!");//throw new Error("Failed to create Seinfeld database");
        //                                          }
    }

    public Bitmap getTile(String tileQ) throws OutOfMemoryError {
        //bmg.recycle();
        byte[] tile = { -119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 1, 0, 0, 0, 1, 0, 8, 3,
                0, 0, 0, 107, -84, 88, 84, 0, 0, 0, 25, 116, 69, 88, 116, 83, 111, 102, 116, 119, 97, 114, 101, 0,
                65, 100, 111, 98, 101, 32, 73, 109, 97, 103, 101, 82, 101, 97, 100, 121, 113, -55, 101, 60, 0, 0, 3,
                32, 105, 84, 88, 116, 88, 77, 76, 58, 99, 111, 109, 46, 97, 100, 111, 98, 101, 46, 120, 109, 112, 0,
                0, 0, 0, 0, 60, 63, 120, 112, 97, 99, 107, 101, 116, 32, 98, 101, 103, 105, 110, 61, 34, -17, -69,
                -65, 34, 32, 105, 100, 61, 34, 87, 53, 77, 48, 77, 112, 67, 101, 104, 105, 72, 122, 114, 101, 83,
                122, 78, 84, 99, 122, 107, 99, 57, 100, 34, 63, 62, 32, 60, 120, 58, 120, 109, 112, 109, 101, 116,
                97, 32, 120, 109, 108, 110, 115, 58, 120, 61, 34, 97, 100, 111, 98, 101, 58, 110, 115, 58, 109, 101,
                116, 97, 47, 34, 32, 120, 58, 120, 109, 112, 116, 107, 61, 34, 65, 100, 111, 98, 101, 32, 88, 77,
                80, 32, 67, 111, 114, 101, 32, 53, 46, 48, 45, 99, 48, 54, 48, 32, 54, 49, 46, 49, 51, 52, 55, 55,
                55, 44, 32, 50, 48, 49, 48, 47, 48, 50, 47, 49, 50, 45, 49, 55, 58, 51, 50, 58, 48, 48, 32, 32, 32,
                32, 32, 32, 32, 32, 34, 62, 32, 60, 114, 100, 102, 58, 82, 68, 70, 32, 120, 109, 108, 110, 115, 58,
                114, 100, 102, 61, 34, 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111, 114,
                103, 47, 49, 57, 57, 57, 47, 48, 50, 47, 50, 50, 45, 114, 100, 102, 45, 115, 121, 110, 116, 97, 120,
                45, 110, 115, 35, 34, 62, 32, 60, 114, 100, 102, 58, 68, 101, 115, 99, 114, 105, 112, 116, 105, 111,
                110, 32, 114, 100, 102, 58, 97, 98, 111, 117, 116, 61, 34, 34, 32, 120, 109, 108, 110, 115, 58, 120,
                109, 112, 61, 34, 104, 116, 116, 112, 58, 47, 47, 110, 115, 46, 97, 100, 111, 98, 101, 46, 99, 111,
                109, 47, 120, 97, 112, 47, 49, 46, 48, 47, 34, 32, 120, 109, 108, 110, 115, 58, 120, 109, 112, 77,
                77, 61, 34, 104, 116, 116, 112, 58, 47, 47, 110, 115, 46, 97, 100, 111, 98, 101, 46, 99, 111, 109,
                47, 120, 97, 112, 47, 49, 46, 48, 47, 109, 109, 47, 34, 32, 120, 109, 108, 110, 115, 58, 115, 116,
                82, 101, 102, 61, 34, 104, 116, 116, 112, 58, 47, 47, 110, 115, 46, 97, 100, 111, 98, 101, 46, 99,
                111, 109, 47, 120, 97, 112, 47, 49, 46, 48, 47, 115, 84, 121, 112, 101, 47, 82, 101, 115, 111, 117,
                114, 99, 101, 82, 101, 102, 35, 34, 32, 120, 109, 112, 58, 67, 114, 101, 97, 116, 111, 114, 84, 111,
                111, 108, 61, 34, 65, 100, 111, 98, 101, 32, 80, 104, 111, 116, 111, 115, 104, 111, 112, 32, 67, 83,
                53, 32, 87, 105, 110, 100, 111, 119, 115, 34, 32, 120, 109, 112, 77, 77, 58, 73, 110, 115, 116, 97,
                110, 99, 101, 73, 68, 61, 34, 120, 109, 112, 46, 105, 105, 100, 58, 70, 49, 69, 65, 51, 53, 51, 69,
                56, 54, 55, 68, 49, 49, 69, 51, 66, 48, 57, 48, 68, 49, 48, 56, 50, 49, 57, 67, 67, 68, 56, 50, 34,
                32, 120, 109, 112, 77, 77, 58, 68, 111, 99, 117, 109, 101, 110, 116, 73, 68, 61, 34, 120, 109, 112,
                46, 100, 105, 100, 58, 70, 49, 69, 65, 51, 53, 51, 70, 56, 54, 55, 68, 49, 49, 69, 51, 66, 48, 57,
                48, 68, 49, 48, 56, 50, 49, 57, 67, 67, 68, 56, 50, 34, 62, 32, 60, 120, 109, 112, 77, 77, 58, 68,
                101, 114, 105, 118, 101, 100, 70, 114, 111, 109, 32, 115, 116, 82, 101, 102, 58, 105, 110, 115, 116,
                97, 110, 99, 101, 73, 68, 61, 34, 120, 109, 112, 46, 105, 105, 100, 58, 70, 49, 69, 65, 51, 53, 51,
                67, 56, 54, 55, 68, 49, 49, 69, 51, 66, 48, 57, 48, 68, 49, 48, 56, 50, 49, 57, 67, 67, 68, 56, 50,
                34, 32, 115, 116, 82, 101, 102, 58, 100, 111, 99, 117, 109, 101, 110, 116, 73, 68, 61, 34, 120, 109,
                112, 46, 100, 105, 100, 58, 70, 49, 69, 65, 51, 53, 51, 68, 56, 54, 55, 68, 49, 49, 69, 51, 66, 48,
                57, 48, 68, 49, 48, 56, 50, 49, 57, 67, 67, 68, 56, 50, 34, 47, 62, 32, 60, 47, 114, 100, 102, 58,
                68, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 62, 32, 60, 47, 114, 100, 102, 58, 82, 68, 70,
                62, 32, 60, 47, 120, 58, 120, 109, 112, 109, 101, 116, 97, 62, 32, 60, 63, 120, 112, 97, 99, 107,
                101, 116, 32, 101, 110, 100, 61, 34, 114, 34, 63, 62, -55, 39, 49, -69, 0, 0, 0, 18, 80, 76, 84, 69,
                -52, -52, -52, 102, 102, 102, -103, -103, -103, 51, 51, 51, 0, 0, 0, 75, 75, 75, -112, 33, 7, 1, 0,
                0, 4, -76, 73, 68, 65, 84, 120, -38, -20, -101, -19, -126, -85, 42, 12, 69, 77, -128, -9, 127, -27,
                91, 69, -27, 43, 80, -67, -75, -75, 61, -77, -42, -113, 25, -37, 42, -122, 45, 4, -48, -19, 52, 1,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, -64, 105, -44, 123, -1, -93, -95, 63, 34, 119, -27, -57, -109, 21, -41, -33, 23, -64,
                -53, -1, 22, -64, -81, -5, -1, -72, 0, -2, -49, 11, 32, -89, 4, -112, 16, -37, -3, -75, 93, 32, 43,
                -11, -61, 2, 56, -105, 5, 127, -92, 34, -34, 55, -95, 94, 32, -128, 81, -22, -89, 4, -112, 44, 15,
                -2, 69, 1, 124, 126, -18, -65, 41, -128, -92, -16, -77, -118, -120, -101, -121, -56, 80, 69, -27,
                -4, -122, -53, 42, -82, -51, 113, -39, -56, 18, -26, 47, -126, 52, 93, 62, -37, -83, 44, -11, -29,
                2, -52, -25, -105, 74, 0, -39, 67, 10, -89, 4, 72, -57, 105, -11, 69, -82, -92, 84, 21, -66, 91,
                -128, 20, -1, -74, -79, 68, 40, 18, -81, 102, 113, -27, 68, -106, -21, -7, -64, 18, 96, -7, 81, 117,
                57, 110, -17, 28, -113, -67, 67, -39, 69, -26, -76, 35, -53, -73, -79, -12, -78, -44, -49, 11, 48,
                -123, -19, 66, 103, 21, -110, 84, -127, 110, 111, -83, 5, -48, -3, -73, 45, -77, -122, -67, -96,
                -78, 7, -92, -29, -11, -2, 28, -112, -99, 62, 125, -108, -84, -75, -22, 81, 1, -78, -97, 92, -36,
                124, -110, 29, 101, -105, -9, 102, 1, 36, -43, 32, -5, -76, -43, 36, 28, 20, 64, -77, 93, 53, 106,
                -8, 68, 0, -35, 123, -40, -51, 2, 108, -41, 124, -17, 17, 82, 36, -84, -125, 2, 72, 94, -119, 88,
                -75, 71, 81, 78, 15, -100, -1, 118, 1, -42, 106, -58, -65, 110, -72, 64, -22, 11, 48, 55, -5, -99,
                -67, 81, -52, 105, -47, -100, -5, -82, -71, -1, 59, 4, 88, 91, -6, 46, -125, -10, -89, 40, 125, 1,
                124, 69, 90, 109, -44, -107, -45, 80, -19, 117, -65, 0, -79, 14, 111, 16, 96, 29, -12, -61, 84, -49,
                3, -126, -20, -19, -28, 27, 4, -120, 67, -95, -77, 5, -104, 14, 11, 96, -89, 58, 87, -34, 117, 72,
                -93, -20, -12, 69, 2, 44, -101, -82, -100, 22, -100, 76, -126, -82, 91, -119, 80, -52, -89, -78, 36,
                -5, 77, 2, -52, 99, -78, 123, 105, 24, -108, -30, 58, -9, -50, 51, -103, -85, -81, 47, 16, 96, -82,
                -86, -37, -89, 3, -95, -41, 0, -14, 89, -46, 96, 34, 116, 76, 0, -55, 5, -112, -69, 5, -48, 61, 115,
                -55, -82, -128, -76, -127, -71, -44, -100, 107, 1, 66, -77, -73, -42, 51, -66, -94, 67, -120, -49,
                71, 33, 119, -73, 0, 75, -4, 62, 109, -83, -117, -95, -48, 78, 95, -25, -5, 72, -26, 98, -56, -7,
                109, 81, 19, -30, 113, -15, -109, 43, -101, -122, -82, -91, -5, -67, -55, 21, -91, -34, 39, -64,
                -28, -115, 101, 109, 27, -110, -37, -65, 31, 45, -121, -45, 60, -88, 45, 102, -37, -53, 105, -70,
                31, -25, 58, 103, -5, -88, 0, 50, -70, -79, 81, -59, 47, -93, 27, 34, -34, -83, 119, 64, 52, -104,
                -59, 44, 19, -95, 121, -105, 108, -10, 44, -2, -82, 60, 0, 0, 0, 0, 0, 0, 0, 0, -16, 111, 115, -34,
                -50, -13, -61, 38, -64, -93, -43, -47, -31, 61, -7, -53, 4, -48, -77, -73, -2, 55, -1, -124, 94, -7,
                -48, -64, -86, -114, 31, 86, -15, 50, 1, -4, -39, -126, 16, -32, 50, 1, 74, 119, -21, 103, -69, 64,
                58, -9, -23, 46, -112, -37, 55, 94, -42, 94, -17, 74, -126, -81, 92, 61, 4, 64, -128, 43, 4, -80,
                -52, -99, 90, 60, -58, -52, -86, 104, -8, 90, -11, -119, 127, 118, 123, -8, -93, 91, -105, -17, 121,
                96, 11, 37, -21, -110, -94, -85, 108, 62, -38, -55, -5, 5, 8, -123, -121, -91, 52, 122, 54, -106,
                30, 29, -6, 103, -43, -27, -33, 13, 61, -80, -103, 0, 109, 73, -117, 0, -82, -116, -21, 34, 1, 90,
                119, -85, -60, -73, 4, 42, 127, 80, -49, -41, -86, 35, -1, -84, 108, -113, -125, 99, -82, 31, 122,
                96, 43, 37, -53, -110, 102, 1, 102, 47, 70, 124, -76, -8, -26, 28, -80, 6, 87, -8, 97, -105, -10,
                111, -6, 90, 117, -32, -97, 109, -100, -76, 35, 15, -84, -26, 6, 98, -85, -92, -72, 41, -83, -125,
                -24, 106, 1, 86, -47, 119, -97, -126, 22, 62, -47, 94, 18, -84, -100, 33, -101, -75, -42, -74, 53,
                88, 30, 88, -83, 92, -103, 69, 73, 86, 92, 111, 19, -64, -70, 34, 79, 5, -80, -116, 67, -3, 1, -62,
                -14, -64, 106, -27, -53, -51, 75, 50, -29, 122, -4, -24, -118, -1, 87, 15, -125, -66, 122, -25, -55,
                -10, -75, 106, -33, 63, 27, 90, -1, 72, -99, -65, 12, 1, 76, 39, -82, 21, -41, -57, 5, -80, 125,
                -83, -38, -9, -49, 58, -53, -47, -48, -11, -64, 110, -89, 49, -99, -72, 95, 33, -64, -26, 104, 81,
                75, 0, -53, 62, 122, -50, 3, 107, 123, -55, -78, 5, -49, -3, 2, -104, -66, -42, 51, 2, 12, 61, -80,
                63, 33, 64, -21, 107, -19, 10, 48, -75, -26, -66, -79, 7, -74, 43, 64, -73, 11, 108, -81, -48, -68,
                -4, 42, -51, 25, 1, 106, 95, 107, -106, 4, 27, -1, 108, -19, 34, 29, 123, 96, -77, 36, 24, -114, 37,
                -63, -53, 56, 39, 64, 53, 28, 14, -122, 65, 105, 15, -44, -25, 2, 116, -121, -63, -73, 10, 32, -81,
                10, 96, -6, 103, -85, 124, 49, -10, -64, -86, 113, -108, -44, 115, -111, -86, 11, 76, -59, -1, 87,
                -42, 67, -18, -56, 48, 88, -49, -30, -102, 11, 87, -5, 103, -59, -97, -16, -64, -106, 47, -41, 77,
                -10, 116, 60, -113, 75, -46, -92, -3, -59, 36, 104, -72, 91, 45, 1, 76, 95, 107, 58, -62, -14, -49,
                -122, 98, 49, 52, -10, -64, -90, 83, 27, 37, 89, 113, 109, 67, -78, 127, -67, 79, -76, -18, 86, 67,
                -128, -114, -81, 85, -121, -2, -39, 114, 97, 59, -12, -64, -102, -53, -31, 81, -41, -12, -87, -13,
                -67, 124, 91, 86, 14, 8, -48, -15, -75, -22, 19, -1, 108, -100, -8, 57, 73, 19, -95, -114, 7, -42,
                -72, 33, 34, -26, 89, -78, -72, 36, -1, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, -65, -59, 127, 2, 12, 0, 35, -109, 40, -115, 21, -105, 28, -78, 0, 0, 0, 0, 73, 69, 78, 68, -82,
                66, 96, -126 };
        System.out.println("byte array tile[] initialized..now loading default nullTile.png..");

        System.out.println("nullTile.png loaded as byte array");
        System.out.println("tileQ: " + tileQ + " !!");
        //TODO: catch if tileQ = null
        if (tileQ != null) {

            Cursor cursor = this.tileDB.query(tableName, new String[] { columnName }, tileQ, null, null, null,
                    null);

            cursor.moveToFirst();
            //TODO: this todo note should be movoed to readme.txt:
            //try to limit the number of tiles pre-loaded into memory by InteractiveMapProvider..
            try {
                tile = cursor.getBlob(0);
            } catch (Exception e) {
                System.out.println(
                        "null cursor returned..no such tile found in db..returning nulltile..\n" + e.getMessage());
            }
            cursor.close();

        }
        //ByteArrayInputStream bmgIS = new ByteArrayInputStream(tile);
        bmg = null;
        //if(Runtime.getRuntime().freeMemory()>24576){
        //                          try{
        bmg = BitmapFactory.decodeByteArray(tile, 0, tile.length, bmgOptions);
        //                          }catch(OutOfMemoryError e){
        ////                            p.map = null;
        //                              map = null;
        //                              bmg.recycle();
        //                              System.out.println("decodeByteArray out of memory..please wait..");
        //                          }

        tile = null;

        return bmg;
    }

    /* For MBTiles generated by TileMill: */
    public String getTileQ(String coordQ) {
        String TileQ = null;
        Cursor cursor = this.tileDB.query("map", new String[] { "tile_id" }, coordQ, null, null, null, null);
        cursor.moveToFirst();
        try {
            TileQ = "tile_id = '" + cursor.getString(0) + "'";
            cursor.close();
        } catch (CursorIndexOutOfBoundsException e) {
            System.out.println("No tiles at this location!! coordQ: " + coordQ);
        }
        return TileQ;
    }
    /****/

    //cache tiles from server

    /**
     *
     * @return
     * @throws java.io.IOException
     */
    public boolean createDatabase() throws JSONException {
        if (!checkDatabase()) {

            try {
                //byte[] b = new byte[1024];
                new URL(serverADD + "?k=fileSize").openStream().read();

                System.out.println("1. instantiating TileDBOpenHelper as tiledopenHelper..");
                System.out.println("passing context: " + this.context);
                TileDBOpenHelper tiledbopenHelper = new TileDBOpenHelper(this.context);

                System.out.println("2. getting WritableDatabase from tiledbopenHelper..");
                this.tileDB = tiledbopenHelper.getWritableDatabase();
            } catch (SQLiteException e) {
                System.out.println(
                        "could not tiledbopenHelper.getWriteableDatabase(): " + e.getStackTrace().toString());
            } catch (MalformedURLException e) {
                System.out.println(e);
            } catch (IOException e) {
                System.out.println("Error copying database..check network connection!!");
                return false;
            }
            System.out.println("3. got WritableDatabase from tiledbopenHelper..!!!!");

            try {
                System.out.println("4. copying database..");
                copyDatabase();
            } catch (IOException e) {
                System.out.println("Error copying database..check network connection!!");
                return false;
            }

        }
        return true;
    }

    //Check if our database already exists
    private boolean checkDatabase() {
        System.out.println("1. checkDatabase()..");
        SQLiteDatabase checkableDatabase = null;
        boolean localDatabaseExists = false;
        //String checkableDatabase = null; //dummy null String..
        System.out.println("1. null checkableDatabase created..!!");
        try {
            checkableDatabase = SQLiteDatabase.openDatabase(DBPATH + DBNAME, null, SQLiteDatabase.OPEN_READONLY);

        } catch (Exception e) {
            System.out.println("1. database doesnt exist/is corrupt..so copy from server.."); //our database doesn't exist, so we'll return false below.
            System.out.println("Copying from: " + serverADD);
            localDatabaseExists = false;
            //checkableDatabase.close();
            checkableDatabase = null;
        }
        if (checkableDatabase != null) {
            localDatabaseExists = true;
            System.out.println("1. huh?? database exists?? " + localDatabaseExists);
            checkableDatabase.close();
        }

        System.out.println("2. returning checkableDatabase().." + localDatabaseExists);
        return localDatabaseExists;
    }

    //Copy our database from the Application's assets
    //over the empty DB for use
    private void copyDatabase() throws IOException, JSONException {

        System.out.println("1. getting mbTiles url..");

        downloadedBytes = new File(DBPATH + DBNAME).length();
        System.out.println("TileBaseHelper.downloadedBytes: " + downloadedBytes);
        byte[] b = new byte[1024];
        new URL(serverADD + "?k=fileSize").openStream().read(b);
        System.out.println("TileBaseHelper: " + new String(b));
        totalBytes = new JSONObject(new String(b)).getLong("fileSize");
        System.out.println("TileBaseHelper.totalBytes: " + totalBytes);

        if (downloadedBytes == totalBytes) {
            System.out.println("Download Complete !!");
            return;
        }

        URL tileDBURL = new URL(serverADD + "?k=i&i=" + downloadedBytes);

        System.out.println("2. opening http connection..");
        //HttpURLConnection tileDBConnection = (HttpURLConnection) tileDBURL.openConnection();

        System.out.println("3. I/O Stream..");
        InputStream tileDBIS = null;
        //                            try{
        tileDBIS = tileDBURL.openStream();

        System.out.println("4. input stream made..!! totalBytes: " + totalBytes);

        //                                try{  
        OutputStream tileDBOS = new FileOutputStream(DBPATH + DBNAME);

        System.out.println("5. output stream made..!!");
        // 
        System.out.println("6. copying mbTiles DB..");
        byte[] buffer = new byte[1024];
        int length;
        while ((length = tileDBIS.read(buffer)) > 0) {
            //                try{
            tileDBOS.write(buffer, 0, length);
            downloadedBytes += buffer.length;
            //                                    }catch (IOException e){
            //                                            System.out.println("Error writing to DB file");
            ////                                    }catch (Exception e){
            //                                            System.out.println("Error writing to DB file");
            //                                    }
        }

        System.out.println("7. mbTiles DB..copied!!");
        System.out.println("8. flushing buffers and closing I/O Stream..");

        tileDBOS.flush();
        tileDBOS.close();
        tileDBIS.close();

        /**/
        tileDB.close();
        /**/

        //                                }catch (IOException e){
        //                                    System.out.println("SDCard write access!!");
        //                                }
        //                            }catch(IOException e){
        System.out.println("Not connected to the Internet!! OR does mbtiles file exist on server??");
        //                            }

    }

    public void openDatabase() throws SQLException {
        System.out.println("1. openDatabase()..");
        tileDB = SQLiteDatabase.openDatabase(DBPATH + DBNAME, null, SQLiteDatabase.OPEN_READWRITE);

    }

    //is closeDatabase() mthod necessary??
    public void closeDatabase() {
        tileDB.close();
    }

    /* getters and setters */

    //sqlite
    public void setTileTableAs(String tableName) {
        this.tableName = tableName;
    }

    public void setTileColumnAs(String columnName) {
        this.columnName = columnName;
    }

    //fileIO
    public void setDBPathAs(String dbPath) {
        this.DBPATH = dbPath;
    }

    //download progress
    public long getDownloadedBytes() {
        return downloadedBytes;
    }

    public long getTotalBytes() {
        return totalBytes;
    }

    /****/
    /*garbage collector*/
    public void clearBitmap() {
        //                            bmg.recycle();
        //                            Bitmap bmg = null;
    }

    private static class TileDBOpenHelper extends SQLiteOpenHelper {
        //                            SQLiteDatabase tileDB;
        TileDBOpenHelper(Context context) {
            super(context, DBNAME, null, 1);

            System.out.println("TileDBOpenHelper instantiated with context" + context.toString());
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            //                                            db = getWritableDatabase();
            //                                            tileDB = db;
            System.out.println("TileDBOpenHelper onCreate()..");
            // if something goes wrong using foods.db asset, revert to creating a simplified version for demo
            //db.execSQL("create table " + TABLE_NAME + "(id integer primary key, type_id integer, name text)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            //                 db.execSQL("drop table if exists " + TABLE_NAME);
            //                 onCreate(db);
        }
    }
}