aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-06-29 23:41:53 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-06-29 23:41:53 +0200
commitfce66be9d662d6d98d23191a3c623cae549a29b2 (patch)
treec41bf260c8ea1e6615ad6b22ad8dbeef9ee81810
parent21b5d60f6d0a4e3a26b8391bba9e78205b861bc6 (diff)
downloadio.trygvis.soilmoisture-android-fce66be9d662d6d98d23191a3c623cae549a29b2.tar.gz
io.trygvis.soilmoisture-android-fce66be9d662d6d98d23191a3c623cae549a29b2.tar.bz2
io.trygvis.soilmoisture-android-fce66be9d662d6d98d23191a3c623cae549a29b2.tar.xz
io.trygvis.soilmoisture-android-fce66be9d662d6d98d23191a3c623cae549a29b2.zip
o Supporting re-activation of the MainActivity. Must initialize the local data with the device list if it is already in the 'ready' state.
-rw-r--r--app/build.gradle2
-rw-r--r--app/src/main/AndroidManifest.xml5
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java18
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/MainActivity.java12
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/SmDevice.java8
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java4
6 files changed, 41 insertions, 8 deletions
diff --git a/app/build.gradle b/app/build.gradle
index 0b6ba45..08c21d1 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -90,9 +90,9 @@ dependencies {
compile('com.crashlytics.sdk.android:crashlytics:2.1.0@aar') {
transitive = true;
}
+ compile project(':bt')
compile 'org.sqldroid:sqldroid:1.0.3'
compile 'org.flywaydb:flyway-core:3.1'
- compile project(':bt')
}
/*
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 3edd865..1b1a23d 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
- xmlns:android="http://schemas.android.com/apk/res/android"
- package="io.trygvis.soilmoisture">
+ package="io.trygvis.soilmoisture"
+ xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Declare this required feature if you want to make the app available to BLE-capable
@@ -27,7 +27,6 @@
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
-
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
diff --git a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java
index 8f748a8..0becb24 100644
--- a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java
+++ b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java
@@ -63,6 +63,8 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
private BtService<SmDevice> btService;
+ private boolean ready;
+
@Override
public IBinder onBind(Intent intent) {
return binder;
@@ -76,10 +78,12 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
@SuppressWarnings("unchecked")
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
+ Log.i(TAG, "onServiceConnected, componentName=" + componentName.toShortString() + ", binder=" + service.toString());
+
btService = ((LocalBinder<BtService<SmDevice>>) service).getService();
- boolean ok = btService.initialize(DefaultSoilMoistureService.this::createTag);
+ ready = btService.initialize(DefaultSoilMoistureService.this::createTag);
- sendBroadcast(createReady(ok));
+ sendBroadcast(createReady(ready));
}
@Override
@@ -280,6 +284,11 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
// -----------------------------------------------------------------------
@Override
+ public List<SmDevice> getDevices() {
+ return getDevices(SmDevice.addressComparator);
+ }
+
+ @Override
public List<SmDevice> getDevices(Comparator<SmDevice> comparator) {
Set<SmDevice> devices = new TreeSet<>(comparator);
for (BtDevice<SmDevice> btDevice : btService.getDevices()) {
@@ -294,6 +303,11 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
}
@Override
+ public boolean isReady() {
+ return ready;
+ }
+
+ @Override
public boolean isScanning() {
return btService.isScanning();
}
diff --git a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java
index 984016a..017d207 100644
--- a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java
+++ b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java
@@ -83,6 +83,14 @@ public class MainActivity extends ListActivity {
Log.i(TAG, "onServiceConnected");
soilMoistureService = ((LocalBinder<SoilMoistureService>) service).getService();
registerReceiver(serviceListener, SoilMoistureListener.INTENT_FILTER);
+
+ if (soilMoistureService.isReady()) {
+ serviceListener.onReady(true);
+
+ for (SmDevice smDevice : soilMoistureService.getDevices()) {
+ serviceListener.onNewDevice(smDevice.getBtDevice().getAddress());
+ }
+ }
}
@Override
@@ -646,7 +654,8 @@ public class MainActivity extends ListActivity {
@Override
public void onReady(boolean ok) {
- if (!ok) {
+ ready = ok;
+ if (!ready) {
Toast.makeText(context,
"Could not initialize services.",
Toast.LENGTH_SHORT).
@@ -654,7 +663,6 @@ public class MainActivity extends ListActivity {
finish();
} else {
- ready = true;
deviceList = new DeviceListAdapter();
setListAdapter(deviceList);
diff --git a/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java b/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java
index e114f3c..f72020b 100644
--- a/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java
+++ b/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java
@@ -5,6 +5,7 @@ import android.util.Log;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
+import java.util.Comparator;
import java.util.List;
import io.trygvis.android.Optional;
@@ -35,6 +36,13 @@ class SmDevice {
private List<SmSensor> sensors = new ArrayList<>();
+ public static Comparator<SmDevice> addressComparator = new Comparator<SmDevice>() {
+ @Override
+ public int compare(SmDevice lhs, SmDevice rhs) {
+ return lhs.btDevice.getAddress().compareTo(rhs.btDevice.getAddress());
+ }
+ };
+
public SmDevice(DefaultSoilMoistureService smService, BtDevice<SmDevice> btDevice, long id) {
this.smService = smService;
this.btDevice = btDevice;
diff --git a/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java
index 40f4262..8b42c9e 100644
--- a/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java
+++ b/app/src/main/java/io/trygvis/soilmoisture/SoilMoistureService.java
@@ -9,12 +9,16 @@ import java.util.Comparator;
import java.util.List;
public interface SoilMoistureService {
+ List<SmDevice> getDevices();
+
List<SmDevice> getDevices(Comparator<SmDevice> comparator);
SmDevice getDevice(String address);
void probe(String address);
+ boolean isReady();
+
boolean isScanning();
boolean startScanning(long scanPeriod);