1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-15 22:49:26 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/LockHandler.java

102 lines
2.7 KiB
Java
Raw Normal View History

package nodomain.freeyourgadget.gadgetbridge;
2016-05-15 00:09:34 +02:00
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
2016-05-17 00:51:00 +02:00
import nodomain.freeyourgadget.gadgetbridge.database.DBOpenHelper;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
2016-05-15 00:09:34 +02:00
/**
2016-06-14 20:13:08 +02:00
* Provides lowlevel access to the database.
2016-05-15 00:09:34 +02:00
*/
public class LockHandler implements DBHandler {
2016-06-14 20:13:08 +02:00
private DaoMaster daoMaster = null;
private DaoSession session = null;
private SQLiteOpenHelper helper = null;
2016-05-15 00:09:34 +02:00
2016-06-14 20:13:08 +02:00
public LockHandler() {
}
public void init(DaoMaster daoMaster, DBOpenHelper helper) {
if (isValid()) {
throw new IllegalStateException("DB must be closed before initializing it again");
}
if (daoMaster == null) {
throw new IllegalArgumentException("daoMaster must not be null");
}
if (helper == null) {
throw new IllegalArgumentException("helper must not be null");
}
2016-05-17 00:51:00 +02:00
this.daoMaster = daoMaster;
this.helper = helper;
2016-06-16 21:54:53 +02:00
2016-05-17 00:51:00 +02:00
session = daoMaster.newSession();
2016-06-14 20:13:08 +02:00
if (session == null) {
throw new RuntimeException("Unable to create database session");
}
}
@Override
public DaoMaster getDaoMaster() {
return daoMaster;
}
2016-06-14 20:13:08 +02:00
private boolean isValid() {
return daoMaster != null;
}
private void ensureValid() {
if (!isValid()) {
throw new IllegalStateException("LockHandler is not in a valid state");
}
2016-05-15 00:09:34 +02:00
}
@Override
public void close() {
2016-06-14 20:13:08 +02:00
ensureValid();
GBApplication.releaseDB();
}
@Override
2016-05-17 00:51:00 +02:00
public synchronized void openDb() {
if (session != null) {
throw new IllegalStateException("session must be null");
}
2016-06-14 20:13:08 +02:00
// this will create completely new db instances and in turn update this handler through #init()
2016-05-17 00:51:00 +02:00
GBApplication.setupDatabase(GBApplication.getContext());
}
2016-05-15 00:09:34 +02:00
2016-05-17 00:51:00 +02:00
@Override
public synchronized void closeDb() {
if (session == null) {
throw new IllegalStateException("session must not be null");
}
session.clear();
session.getDatabase().close();
session = null;
2016-06-14 20:13:08 +02:00
helper = null;
daoMaster = null;
2016-05-15 00:09:34 +02:00
}
@Override
public SQLiteOpenHelper getHelper() {
2016-06-14 20:13:08 +02:00
ensureValid();
2016-05-17 00:51:00 +02:00
return helper;
2016-05-15 00:09:34 +02:00
}
@Override
public DaoSession getDaoSession() {
2016-06-14 20:13:08 +02:00
ensureValid();
return session;
}
2016-05-15 00:09:34 +02:00
@Override
2016-05-17 00:51:00 +02:00
public SQLiteDatabase getDatabase() {
2016-06-14 20:13:08 +02:00
ensureValid();
2016-05-17 00:51:00 +02:00
return daoMaster.getDatabase();
2016-05-15 00:09:34 +02:00
}
}