aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-01-30 23:51:53 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-01-30 23:51:53 +0100
commit61bdbbdc3f81b89ce1679075664fe50012ccd78d (patch)
tree97c4fa0190406812cb5159433fc398c950f1da3c
parent991620666551c1eaf655a0aa0b1fe08d2bc19681 (diff)
downloadio.trygvis.soilmoisture-android-61bdbbdc3f81b89ce1679075664fe50012ccd78d.tar.gz
io.trygvis.soilmoisture-android-61bdbbdc3f81b89ce1679075664fe50012ccd78d.tar.bz2
io.trygvis.soilmoisture-android-61bdbbdc3f81b89ce1679075664fe50012ccd78d.tar.xz
io.trygvis.soilmoisture-android-61bdbbdc3f81b89ce1679075664fe50012ccd78d.zip
o Activating the sensor view.
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java3
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/MainActivity.java14
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/SensorActivity.java115
-rw-r--r--app/src/main/java/io/trygvis/soilmoisture/SmSensor.java14
-rw-r--r--app/src/main/res/layout/fragment_sensor.xml41
-rw-r--r--app/src/main/res/values/strings.xml2
6 files changed, 170 insertions, 19 deletions
diff --git a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java
index 2cf85d9..8f748a8 100644
--- a/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java
+++ b/app/src/main/java/io/trygvis/soilmoisture/DefaultSoilMoistureService.java
@@ -405,6 +405,9 @@ public class DefaultSoilMoistureService extends Service implements SoilMoistureS
sensor.getDevice().getBtDevice().withConnection(sequence);
}
+ public void readWarningValue(SmSensor sensor) {
+ }
+
// -----------------------------------------------------------------------
// Event creation and dispatching
// -----------------------------------------------------------------------
diff --git a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java
index 6996ef8..984016a 100644
--- a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java
+++ b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java
@@ -103,6 +103,10 @@ public class MainActivity extends ListActivity {
red = getResources().getColor(R.color.red);
setContentView(R.layout.main);
+
+ // This will be replaced quite fast, but when resuming it needs to be here.
+ deviceList = new DeviceListAdapter();
+ setListAdapter(deviceList);
}
@Override
@@ -270,12 +274,12 @@ public class MainActivity extends ListActivity {
private void onSensorClick(SmSensor sensor) {
Log.i(TAG, "onSensorClick, device=" + sensor.getDevice().getBtDevice().getId() + "/" + sensor.getIndex());
- sensor.readCurrentValue();
+// sensor.readCurrentValue();
-// Intent intent = new Intent(this, SensorActivity.class);
-// intent.putExtra(SensorActivity.EXTRA_ADDRESS, sensor.getDevice().getBtDevice().getAddress());
-// intent.putExtra(SensorActivity.EXTRA_NUMBER, sensor.getIndex());
-// startActivity(intent);
+ Intent intent = new Intent(this, SensorActivity.class);
+ intent.putExtra(SensorActivity.EXTRA_ADDRESS, sensor.getDevice().getBtDevice().getAddress());
+ intent.putExtra(SensorActivity.EXTRA_NUMBER, sensor.getIndex());
+ startActivity(intent);
}
// -----------------------------------------------------------------------
diff --git a/app/src/main/java/io/trygvis/soilmoisture/SensorActivity.java b/app/src/main/java/io/trygvis/soilmoisture/SensorActivity.java
index a5b68d8..f818054 100644
--- a/app/src/main/java/io/trygvis/soilmoisture/SensorActivity.java
+++ b/app/src/main/java/io/trygvis/soilmoisture/SensorActivity.java
@@ -5,6 +5,7 @@ import android.app.Activity;
import android.app.Fragment;
import android.content.ComponentName;
import android.content.Context;
+import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
@@ -14,6 +15,8 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
+import android.widget.Button;
+import android.widget.TextView;
import android.widget.Toast;
import io.trygvis.android.LocalBinder;
@@ -38,18 +41,19 @@ public class SensorActivity extends Activity {
private SmSensor sensor;
@Override
- protected void onCreate(Bundle savedInstanceState) {
+ protected void onCreate(Bundle savedInstanceState) { Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor);
+
+ address = getIntent().getStringExtra(EXTRA_ADDRESS);
+ number = getIntent().getIntExtra(EXTRA_NUMBER, -1);
+
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
- .add(R.id.container, new PlaceholderFragment())
+ .add(R.id.container, new SensorFragment())
.commit();
}
- address = getIntent().getStringExtra(EXTRA_ADDRESS);
- number = getIntent().getIntExtra(EXTRA_NUMBER, -1);
-
ActionBar actionBar = getActionBar();
if (actionBar != null) {
@@ -74,6 +78,9 @@ public class SensorActivity extends Activity {
}
sensor = o.get();
+ // Schedule an updated reading of the relevant values.
+ refreshSensorValues();
+
runOnUiThread(SensorActivity.this::initializeView);
}
@@ -83,6 +90,46 @@ public class SensorActivity extends Activity {
soilMoistureService = null;
}
};
+
+ bindService(new Intent(this, DefaultSoilMoistureService.class), serviceConnection, BIND_AUTO_CREATE);
+ }
+
+ @Override
+ protected void onDestroy() {
+ super.onDestroy();
+
+ if (serviceConnection != null) {
+ sensor = null;
+ device = null;
+ unbindService(serviceConnection);
+ }
+ }
+
+ @Override
+ protected void onResume() {
+ Log.i(TAG, "onResume");
+
+ super.onResume();
+
+ registerReceiver(serviceListener, SoilMoistureListener.INTENT_FILTER);
+ }
+
+ @Override
+ protected void onPause() {
+ Log.i(TAG, "onPause");
+
+ super.onPause();
+ unregisterReceiver(serviceListener);
+ }
+
+ private void refreshSensorValues() {
+ if (sensor == null) {
+ Log.d(TAG, "refreshSensorValues: not connected to service");
+ return;
+ }
+
+ sensor.readCurrentValue();
+ sensor.readWarningValue();
}
public void initializeView() {
@@ -114,20 +161,72 @@ public class SensorActivity extends Activity {
/**
* A placeholder fragment containing a simple view.
*/
- public static class PlaceholderFragment extends Fragment {
+ public static class SensorFragment extends Fragment {
+ private SensorActivity sensorActivity;
+ private TextView currentValue;
+ private TextView warningValue;
+
+ @Override
+ public void onAttach(Activity activity) {
+ Log.i(TAG, "onAttach");
+ super.onAttach(activity);
- public PlaceholderFragment() {
+ sensorActivity = (SensorActivity) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sensor, container, false);
+
+ currentValue = (TextView) rootView.findViewById(R.id.current_value);
+ warningValue = (TextView) rootView.findViewById(R.id.warning_value);
+
+ Button button = (Button) rootView.findViewById(R.id.button_refresh);
+
+ button.setOnClickListener(v -> sensorActivity.refreshSensorValues());
+
+ update();
+
return rootView;
}
- }
+ public void update() {
+ SmSensor sensor = sensorActivity.sensor;
+
+ if (sensor == null) {
+ return;
+ }
+
+ Integer v = sensor.getLastValue();
+ currentValue.setText(v != null ? v.toString() : "");
+
+ Integer w = sensor.getWarningValue();
+ warningValue.setText(w != null ? w.toString() : "");
+ }
+ }
private class SensorSoilMoistureListener extends SoilMoistureListener {
+ @Override
+ public void onNewSample(String address, int sensor) {
+ if(!address.equals(SensorActivity.this.address) || sensor != SensorActivity.this.number) {
+ return;
+ }
+
+ SensorFragment sensorFragment = (SensorFragment) getFragmentManager().findFragmentById(R.id.container);
+ sensorFragment.update();
+ }
+
+ @Override
+ public void onDevicePropertyUpdated(String address) {
+ Log.i(TAG, "onDevicePropertyUpdated");
+
+ if (!address.equals(SensorActivity.this.address)) {
+ return;
+ }
+
+ SensorFragment sensorFragment = (SensorFragment) getFragmentManager().findFragmentById(R.id.container);
+ sensorFragment.update();
+ }
}
}
diff --git a/app/src/main/java/io/trygvis/soilmoisture/SmSensor.java b/app/src/main/java/io/trygvis/soilmoisture/SmSensor.java
index 3c6cac3..935bf36 100644
--- a/app/src/main/java/io/trygvis/soilmoisture/SmSensor.java
+++ b/app/src/main/java/io/trygvis/soilmoisture/SmSensor.java
@@ -15,6 +15,8 @@ class SmSensor {
private Integer lastValue;
+ private Integer warningValue;
+
SmSensor(SmDevice device, long id, int index) {
this.device = device;
this.id = id;
@@ -46,6 +48,14 @@ class SmSensor {
return lastValue;
}
+ public Integer getWarningValue() {
+ return warningValue;
+ }
+
+ public void setWarningValue(Integer warningValue) {
+ this.warningValue = warningValue;
+ }
+
public Date getTimestamp() {
return timestamp;
}
@@ -69,4 +79,8 @@ class SmSensor {
this.timestamp = timestamp;
this.lastValue = lastValue;
}
+
+ public void readWarningValue() {
+ device.smService.readWarningValue(this);
+ }
}
diff --git a/app/src/main/res/layout/fragment_sensor.xml b/app/src/main/res/layout/fragment_sensor.xml
index 4d911ca..47aa61f 100644
--- a/app/src/main/res/layout/fragment_sensor.xml
+++ b/app/src/main/res/layout/fragment_sensor.xml
@@ -6,18 +6,47 @@
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context="io.trygvis.soilmoisture.SensorActivity$PlaceholderFragment">
+ tools:context="io.trygvis.soilmoisture.SensorActivity.SensorFragment">
- <TextView
- android:text="@string/hello_world"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
+ <GridLayout
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:layout_alignParentTop="true"
+ android:layout_alignParentStart="true"
+ android:layout_above="@+id/button_refresh">
+
+ <TextView
+ android:text="@string/value"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"/>
+
+ <TextView
+ android:text="@string/warning_value"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_row="1"
+ android:layout_column="0"/>
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:id="@+id/current_value"
+ android:layout_row="0"
+ android:layout_column="1"/>
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:id="@+id/warning_value"
+ android:layout_row="1"
+ android:layout_column="1"/>
+ </GridLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/refresh"
- android:id="@+id/button"
+ android:id="@+id/button_refresh"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"/>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index d1934f0..30ae8df 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -38,5 +38,7 @@
<string name="no">No</string>
<string name="error_could_not_read_value">Could not read value</string>
<string name="title_activity_sensor">Sensor</string>
+ <string name="value">Value</string>
+ <string name="warning_value">Warning value</string>
</resources>