diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-04 21:04:18 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-04 21:04:18 +0100 |
commit | 31fc64bec1c5286c27bdc1f683d037ae0e91418d (patch) | |
tree | cfc74e502e5499422f0cd11a1716bcb07977887a /app/src/main/java/io/trygvis/android | |
parent | 1b9a4defe73f3aa8c10aa4af49002f0cebd1c292 (diff) | |
download | io.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')
-rw-r--r-- | app/src/main/java/io/trygvis/android/bt/BtService.java | 5 | ||||
-rw-r--r-- | app/src/main/java/io/trygvis/android/bt/DefaultBtService.java | 34 |
2 files changed, 27 insertions, 12 deletions
diff --git a/app/src/main/java/io/trygvis/android/bt/BtService.java b/app/src/main/java/io/trygvis/android/bt/BtService.java index ca8bcd9..fb11027 100644 --- a/app/src/main/java/io/trygvis/android/bt/BtService.java +++ b/app/src/main/java/io/trygvis/android/bt/BtService.java @@ -4,9 +4,12 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.database.sqlite.SQLiteDatabase; import java.util.Collection; +import io.trygvis.android.Function; + public interface BtService<A extends BtDevice.BtDeviceWrapper<A>> { boolean initialize(BtDbIntegration<A> btDbIntegration); @@ -27,6 +30,8 @@ public interface BtService<A extends BtDevice.BtDeviceWrapper<A>> { Collection<A> getTags(); + <T> T runTx(Function<SQLiteDatabase, T> action); + public static interface BtDbIntegration<A extends BtDevice.BtDeviceWrapper<A>> { A createTag(BtDevice<A> a); } 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); |