1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-11 19:27:50 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/LockHandler.java

117 lines
3.4 KiB
Java
Raw Normal View History

2019-02-13 20:43:30 +01:00
/* Copyright (C) 2016-2019 Andreas Shimokawa, Carsten Pfeiffer, Taavi Eomäe
2017-03-10 14:53:19 +01:00
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
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.entities.DaoMaster;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
2016-05-15 00:09:34 +02:00
/**
* Provides low-level 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, DaoMaster.OpenHelper helper) {
2016-06-14 20:13:08 +02:00
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()
GBApplication.app().setupDatabase();
2016-05-17 00:51:00 +02:00
}
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
}
}