1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
package com.aerinder.test;
import java.io.File;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.AndroidRuntimeException;
import android.util.Log;
/**
*
* @author Aerinder
*
*/
public class DataBaseHelper extends SQLiteOpenHelper {
private static final boolean isLogged = Constantes.LOG_VISIBLE;
private static final String LOG_NAME = DataBaseHelper.class.getSimpleName();
private String dbPathToUse;
private final String dbName;
private static final int DATABASE_VERSION = 0;
private SQLiteDatabase mDatabase = null;
private boolean mIsInitializing = false;
public DataBaseHelper(Context context, String dbName) {
super(context, dbName, null, DATABASE_VERSION);
if(dbName == null)
throw new IllegalArgumentException("Base non sélectionné.");
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
if(isLogged)
Log.e(LOG_NAME, "Stockage externe (SD-Card) non montée");
throw new AndroidRuntimeException("Stockage externe (SD-Card) non montée");
}
this.dbName = dbName;
File appDbDir = Constantes.STOCKAGE_DB_PATH;
if (!appDbDir.exists()) {
appDbDir.mkdirs();
}
dbPathToUse = appDbDir + File.separator + dbName;
if(isLogged)
Log.d(LOG_NAME, useLocal + "->" + dbPathToUse);
}
/*
* (non-Javadoc)
*
* @see android.database.sqlite.SQLiteOpenHelper#getReadableDatabase()
*/
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
if (mDatabase != null && mDatabase.isOpen()) {
return mDatabase; // Base déjà ouverture pour le travail
}
if (mIsInitializing) {
throw new IllegalStateException("getReadableDatabase appelé en boucle");
}
try {
return getWritableDatabase();
} catch (SQLiteException e) {
if(isLogged)
Log.e(LOG_NAME, "Impossible d'ouvrir " + dbName + " pour l'écriture, tentative en mode lecture seule", e);
}
SQLiteDatabase db = null;
try {
mIsInitializing = true;
db = SQLiteDatabase.openDatabase(dbPathToUse, null, SQLiteDatabase.OPEN_READONLY);
if (db.getVersion() != DATABASE_VERSION) {
throw new SQLiteException("Impossible d'upgrader une base en lecture seule de la version " + db.getVersion() + " vers "
+ DATABASE_VERSION + ": " + dbPathToUse);
}
onOpen(db);
if(isLogged)
Log.w(LOG_NAME, "Ouverte " + dbName + " en lecture seule");
mDatabase = db;
return mDatabase;
} finally {
mIsInitializing = false;
if (db != null && db != mDatabase)
db.close();
}
}
/*
* (non-Javadoc)
*
* @see android.database.sqlite.SQLiteOpenHelper#getWritableDatabase()
*/
@Override
public synchronized SQLiteDatabase getWritableDatabase() {
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
return mDatabase; // Base déjà ouverte pour le travail
}
if (mIsInitializing) {
throw new IllegalStateException("getWritableDatabase appelé en boucle");
}
boolean success = false;
SQLiteDatabase db = null;
try {
mIsInitializing = true;
db = SQLiteDatabase.openOrCreateDatabase(dbPathToUse, null);
int version = db.getVersion();
if (version != DATABASE_VERSION) {
db.beginTransaction();
try {
if (version == 0) {
onCreate(db);
} else {
onUpgrade(db, version, DATABASE_VERSION);
}
db.setVersion(DATABASE_VERSION);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
onOpen(db);
success = true;
return db;
} finally {
mIsInitializing = false;
if (success) {
if (mDatabase != null) {
try {
mDatabase.close();
} catch (Exception e) {
}
}
mDatabase = db;
} else {
if (db != null)
db.close();
}
}
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldDbVersion, int newDbVersion) {
}
} |
Partager