aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-01-04 21:04:18 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-01-04 21:04:18 +0100
commit31fc64bec1c5286c27bdc1f683d037ae0e91418d (patch)
treecfc74e502e5499422f0cd11a1716bcb07977887a /app/src/main/java/io/trygvis/android/bt/DefaultBtService.java
parent1b9a4defe73f3aa8c10aa4af49002f0cebd1c292 (diff)
downloadio.trygvis.soilmoisture-android-31fc64bec1c5286c27bdc1f683d037ae0e91418d.tar.gz
io.trygvis.soilmoisture-android-31fc64bec1c5286c27bdc1f683d037ae0e91418d.tar.bz2
io.trygvis.soilmoisture-android-31fc64bec1c5286c27bdc1f683d037ae0e91418d.tar.xz
io.trygvis.soilmoisture-android-31fc64bec1c5286c27bdc1f683d037ae0e91418d.zip
o Adding a method to run a transaction in the BtService's database.
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);