PageRenderTime 48ms CodeModel.GetById 36ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/v3.2/nimbits-android/src/com/nimbits/android/database/DatabaseHelperImpl.java

http://nimbits-server.googlecode.com/
Java | 121 lines | 84 code | 34 blank | 3 comment | 5 complexity | 6acff47c7ecd452107deaa362c75f3e2 MD5 | raw file
  1package com.nimbits.android.database;
  2
  3import android.content.Context;
  4import android.content.res.AssetManager;
  5import android.database.Cursor;
  6import android.database.sqlite.SQLiteDatabase;
  7import android.database.sqlite.SQLiteOpenHelper;
  8import com.nimbits.client.model.Const;
  9
 10import java.io.FileOutputStream;
 11import java.io.IOException;
 12import java.io.InputStream;
 13import java.io.OutputStream;
 14import java.util.Locale;
 15
 16public class DatabaseHelperImpl extends SQLiteOpenHelper implements DatabaseHelper {
 17
 18    //The Android's default system path of your application database.
 19
 20
 21   // private SQLiteDatabase myDataBase;
 22    private final Context myContext;
 23
 24    public DatabaseHelperImpl(Context context) {
 25
 26        super(context, Const.ANDROID_DB_NAME, null, 1);
 27        this.myContext = context;
 28    }
 29
 30    boolean dbFileExists() {
 31        java.io.File file = new java.io.File(Const.ANDROID_DB_PATH + Const.ANDROID_DB_NAME);
 32        return file.exists();
 33    }
 34
 35    @Override
 36    public boolean checkDatabase()   {
 37        if (! dbFileExists()) {
 38            try {
 39                dbCreate();
 40                return true;
 41            } catch (IOException e) {
 42                return false;
 43            }
 44        }
 45        else {
 46            return true;
 47        }
 48
 49    }
 50
 51
 52
 53
 54
 55
 56    @Override
 57    public boolean isDatabaseEmpty() {
 58        SQLiteDatabase db1 = getDB(false);
 59        final Cursor c = db1.query(Const.ANDROID_TABLE_LEVEL_TWO_DISPLAY, new String[] {"_id", Const.ANDROID_COL_CATEGORY, Const.ANDROID_COL_DESCRIPTION, Const.ANDROID_COL_DISPLAY_TYPE},  null , null, null, null, Const.ANDROID_COL_DISPLAY_TYPE);
 60        final boolean retVal = c.isAfterLast();
 61        c.close();
 62        db1.close();
 63        return retVal;
 64    }
 65
 66
 67
 68    void dbCreate() throws IOException {
 69        final AssetManager assetManager = myContext.getAssets();
 70        this.getWritableDatabase();
 71        final InputStream myInput =assetManager.open(Const.ANDROID_DB_NAME);
 72        final String outFileName = Const.ANDROID_DB_PATH + Const.ANDROID_DB_NAME;
 73        final OutputStream myOutput = new FileOutputStream(outFileName);
 74        final byte[] buffer = new byte[1024];
 75        int length;
 76        while ((length = myInput.read(buffer))>0){
 77            myOutput.write(buffer, 0, length);
 78        }
 79
 80        myOutput.flush();
 81        myOutput.close();
 82        myInput.close();
 83
 84
 85    }
 86
 87    public SQLiteDatabase getDB(boolean writable)  {
 88       // DatabaseHelperImpl myDbHelper = new DatabaseHelperImpl(c);
 89        SQLiteDatabase db1;
 90        if (writable) {
 91            db1 = getWritableDatabase();
 92        }
 93        else {
 94            db1 = getReadableDatabase();
 95        }
 96        db1.setVersion(1);
 97        db1.setLocale(Locale.getDefault());
 98        db1.setLockingEnabled(false);
 99        return db1;
100    }
101
102    @Override
103    public synchronized void close() {
104
105        super.close();
106
107    }
108
109    @Override
110    public void onCreate(SQLiteDatabase db) {
111
112    }
113
114    @Override
115    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
116
117    }
118
119
120
121}