2018-06-25 18:35:46 +02:00
|
|
|
/* Copyright (C) 2016-2018 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/>. */
|
2016-05-16 23:00:04 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
2016-05-15 00:09:34 +02:00
|
|
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
|
2016-05-16 23:00:04 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
2016-05-17 00:51:00 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
|
2016-05-16 23:00:04 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
2016-05-15 00:09:34 +02:00
|
|
|
|
|
|
|
/**
|
2018-03-22 22:47:40 +01: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() {
|
|
|
|
}
|
|
|
|
|
2017-04-27 07:52:11 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 00:07:50 +02:00
|
|
|
@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();
|
2016-05-16 23:00:04 +02:00
|
|
|
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()
|
2017-04-27 07:52:11 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-16 23:00:04 +02:00
|
|
|
@Override
|
|
|
|
public DaoSession getDaoSession() {
|
2016-06-14 20:13:08 +02:00
|
|
|
ensureValid();
|
2016-05-16 23:00:04 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|