From 31fc64bec1c5286c27bdc1f683d037ae0e91418d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 4 Jan 2015 21:04:18 +0100 Subject: o Adding a method to run a transaction in the BtService's database. --- .../main/java/io/trygvis/android/bt/BtService.java | 5 ++++ .../io/trygvis/android/bt/DefaultBtService.java | 34 ++++++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) (limited to 'app/src/main/java/io/trygvis/android') 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> { boolean initialize(BtDbIntegration btDbIntegration); @@ -27,6 +30,8 @@ public interface BtService> { Collection getTags(); + T runTx(Function action); + public static interface BtDbIntegration> { A createTag(BtDevice 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> extends Ser return tags; } + @Override + public T runTx(Function 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> 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> 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); -- cgit v1.2.3