aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/io/trygvis/android/bt/DefaultBtService.java')
-rw-r--r--app/src/main/java/io/trygvis/android/bt/DefaultBtService.java109
1 files changed, 102 insertions, 7 deletions
diff --git a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
index 51d84af..5e13a1f 100644
--- a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
+++ b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
@@ -4,20 +4,28 @@ import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
+import android.content.ComponentName;
+import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
+import org.flywaydb.core.Flyway;
+import org.flywaydb.core.api.android.ContextHolder;
+
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Date;
import java.util.HashSet;
import java.util.Set;
-import io.trygvis.android.Function;
import io.trygvis.android.LocalBinder;
import io.trygvis.soilmoisture.R;
@@ -34,7 +42,7 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser
// State
// -----------------------------------------------------------------------
- private Function<BtDevice<A>, A> tagConstructor;
+ private BtDbIntegration<A> btDbIntegration;
private BluetoothManager bluetoothManager;
@@ -49,13 +57,13 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser
// -----------------------------------------------------------------------
@Override
- public boolean initialize(Function<BtDevice<A>, A> tagConstructor) {
+ public boolean initialize(BtDbIntegration<A> btDbIntegration) {
if (bluetoothManager != null) {
Log.i(TAG, "Already initialized");
return false;
}
- this.tagConstructor = tagConstructor;
+ this.btDbIntegration = btDbIntegration;
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
@@ -171,19 +179,106 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser
return binder;
}
+ @Override
+ public void onCreate() {
+ Bundle data;
+
+ try {
+ ComponentName myService = new ComponentName(this, this.getClass());
+ data = getPackageManager().getServiceInfo(myService, PackageManager.GET_META_DATA).metaData;
+ } catch (PackageManager.NameNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+
+ SQLiteDatabase db = openBtDevices();
+ String path;
+ try {
+ path = db.getPath();
+ } finally {
+ db.close();
+ }
+
+ {
+ ContextHolder.setContext(this);
+ Flyway flyway = new Flyway();
+ flyway.setDataSource("jdbc:sqlite:" + path, "", "");
+ flyway.setTable("schema_version_bt");
+ flyway.setLocations("db/migration/bt");
+
+ flyway.migrate();
+ }
+
+ String customMigrations = data.getString(getClass().getName() + ".migration", null);
+
+ if (customMigrations != null) {
+ ContextHolder.setContext(this);
+ Flyway flyway = new Flyway();
+ flyway.setDataSource("jdbc:sqlite:" + path, "", "");
+ flyway.setTable("schema_version_custom");
+ flyway.setLocations(customMigrations);
+
+ flyway.setBaselineOnMigrate(true);
+ flyway.migrate();
+ }
+ }
+
// -----------------------------------------------------------------------
// Stuff
// -----------------------------------------------------------------------
+ private SQLiteDatabase openBtDevices() {
+ return openOrCreateDatabase("bt-devices", MODE_ENABLE_WRITE_AHEAD_LOGGING, null);
+ }
+
private BtDevice<A> register(BluetoothDevice bluetoothDevice, Integer rssi, BtScanResult scanResult) {
- BtDevice<A> btDevice = findDevice(bluetoothDevice.getAddress());
+ String address = bluetoothDevice.getAddress();
+ BtDevice<A> btDevice = findDevice(address);
if (btDevice != null) {
return btDevice;
}
- Log.i(TAG, "New device: " + bluetoothDevice.getAddress());
- btDevice = new BtDevice<>(this, bluetoothDevice, tagConstructor, rssi, scanResult);
+ long now = System.currentTimeMillis();
+
+ SQLiteDatabase db = openBtDevices();
+ try {
+ db.beginTransaction();
+
+ Cursor cursor = db.query("bt_device", new String[]{"id", "first_seen"}, "address=?",
+ new String[]{address}, null, null, null);
+
+ long id;
+ Date firstSeen, lastSeen;
+ boolean seenBefore = cursor.moveToNext();
+
+ if (seenBefore) {
+ id = cursor.getLong(0);
+ firstSeen = new Date(cursor.getLong(1));
+ lastSeen = new Date(now);
+
+ ContentValues values = new ContentValues();
+ values.put("last_seen", now);
+ db.update("bt_device", values, "address=?", new String[]{address});
+ } else {
+ ContentValues values = new ContentValues();
+ values.put("address", address);
+ values.put("first_seen", now);
+ values.put("last_seen", now);
+ id = db.insert("bt_device", null, values);
+ firstSeen = lastSeen = new Date(now);
+ }
+
+ Log.i(TAG, "New device: " + address + ", seenBefore=" + seenBefore);
+ btDevice = new BtDevice<>(this, bluetoothDevice, btDbIntegration, id, rssi, scanResult,
+ seenBefore, firstSeen, lastSeen);
+
+ cursor.close();
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ db.close();
+ }
+
devices.add(btDevice);
sendBroadcast(createNewDevice(btDevice.getAddress()));