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.java34
1 files changed, 22 insertions, 12 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 3c14c1f..205421b 100644
--- a/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
+++ b/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
@@ -26,6 +26,7 @@ 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;
@@ -160,6 +161,23 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser
return tags;
}
+ @Override
+ public <T> T runTx(Function<SQLiteDatabase, T> action) {
+ SQLiteDatabase db = openBtDevices();
+ try {
+ db.beginTransaction();
+
+ T value = action.apply(db);
+
+ db.setTransactionSuccessful();
+
+ return value;
+ } finally {
+ db.endTransaction();
+ db.close();
+ }
+ }
+
// -----------------------------------------------------------------------
// Scanning
// -----------------------------------------------------------------------
@@ -244,10 +262,7 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser
long now = System.currentTimeMillis();
- SQLiteDatabase db = openBtDevices();
- try {
- db.beginTransaction();
-
+ btDevice = runTx(db -> {
Cursor cursor = db.query("bt_device", new String[]{"id", "first_seen"}, "address=?",
new String[]{address}, null, null, null);
@@ -273,15 +288,10 @@ public class DefaultBtService<A extends BtDevice.BtDeviceWrapper<A>> extends Ser
}
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();
- }
+ return new BtDevice<>(this, bluetoothDevice, btDbIntegration, id, rssi, scanResult,
+ seenBefore, firstSeen, lastSeen);
+ });
devices.add(btDevice);