From c4685214d8db34166213ffa373a16af1a99401a5 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 27 Jan 2015 21:23:50 +0100 Subject: o Adding 'recently seen' on BtDevice. Updated when scanning. o Removing BtScanResult, it was never used. o Getting MainActivity to listen on device property changed so the UI is properly updated. o Adding a status bar with color to indicate if the device is available, connected or not seen. --- .../java/io/trygvis/soilmoisture/MainActivity.java | 191 ++++++++++++++------- 1 file changed, 125 insertions(+), 66 deletions(-) (limited to 'app/src/main/java/io/trygvis/soilmoisture/MainActivity.java') diff --git a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java index e838f01..6996ef8 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java +++ b/app/src/main/java/io/trygvis/soilmoisture/MainActivity.java @@ -47,7 +47,7 @@ public class MainActivity extends ListActivity { private static final int REQUEST_ENABLE_BT = 1; private final BtActivitySupport btActivitySupport = new BtActivitySupport(this, REQUEST_ENABLE_BT); - private final SoilMoistureListener serviceListener = new MySoilMoistureListener(); + private final SoilMoistureListener serviceListener = new MainSoilMoistureListener(); private final MainActivity context = this; private DeviceListAdapter deviceList; @@ -56,7 +56,10 @@ public class MainActivity extends ListActivity { private ProgressDialog initializing; private boolean ready; - @Override + private int red; + private int yellow; + private int green; + protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, "onCreate"); Thread.setDefaultUncaughtExceptionHandler(EXCEPTION_HANDLER); @@ -92,8 +95,12 @@ public class MainActivity extends ListActivity { bindService(new Intent(this, DefaultSoilMoistureService.class), serviceConnection, BIND_AUTO_CREATE); - initializing = ProgressDialog. - show(this, "Initializing", "Connecting to Bluetooth system.", true); +// initializing = ProgressDialog. +// show(this, "Initializing", "Connecting to Bluetooth system.", true); + + green = getResources().getColor(R.color.green); + yellow = getResources().getColor(R.color.yellow); + red = getResources().getColor(R.color.red); setContentView(R.layout.main); } @@ -264,36 +271,126 @@ public class MainActivity extends ListActivity { Log.i(TAG, "onSensorClick, device=" + sensor.getDevice().getBtDevice().getId() + "/" + sensor.getIndex()); 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); } // ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- - static class DeviceItem { + class DeviceItem { + final SmDevice device; + final TextView statusBar; final TextView deviceName; final TextView deviceAddress; final TextView rssi; final TextView info; - DeviceItem(View view) { + DeviceItem(SmDevice device, View view) { + this.device = device; + this.statusBar = (TextView) view.findViewById(R.id.status_bar); this.deviceName = (TextView) view.findViewById(R.id.device_name); this.deviceAddress = (TextView) view.findViewById(R.id.device_address); this.rssi = (TextView) view.findViewById(R.id.device_rssi); this.info = (TextView) view.findViewById(R.id.device_info); } + + public void update() { + statusBar.setVisibility(deviceList.isGroupByDevice() ? View.VISIBLE : View.GONE); + statusBar.setBackgroundColor(statusColor(device)); + + if (device.getName() != null) { + deviceName.setText(device.getName()); + } else { + deviceName.setText(R.string.unknown_device); + } + String address = device.getBtDevice().getAddress(); + + if (!device.isProbed()) { + address += " not probed"; + } else if (device.isUseful()) { + address += " useful"; + } else { + address += " not useful"; + } + + address += ", connected=" + device.getBtDevice().connected(); + address += ", recentlySeen=" + device.getBtDevice().isRecentlySeen(); + + deviceAddress.setText(address); + + String rssi = getText(R.string.rssi) + ": " + + (device.getBtDevice().getRssi() != null ? valueOf(device.getBtDevice().getRssi()) : getText(R.string.unknown)); + rssi += ", device: " + device.toString(); + this.rssi.setText(rssi); + +// boolean useful = device.isUseful(); +// +// if (useful) { +// info.setText("Number of sensors: " + device.getSensors().size()); +// } else { +// info.setText(""); +// } + } } - static class SensorItem { + class SensorItem { + final SmDevice device; final SmSensor sensor; + final TextView statusBar; final TextView description; final ProgressBar sensorProgress; SensorItem(SmSensor sensor, View view) { this.sensor = sensor; + this.statusBar = (TextView) view.findViewById(R.id.status_bar); this.description = (TextView) view.findViewById(R.id.description); this.sensorProgress = (ProgressBar) view.findViewById(R.id.sensor_progress); sensorProgress.setMax(1024); + + device = sensor.getDevice(); + + view.setClickable(true); + view.setOnClickListener(v -> { + Log.i(TAG, "onClick, SmSensor: " + +// "position=" + position + ", " + + "sensor=" + sensor /*+ ", " + + "tag=" + v.getTag()*/); + onSensorClick(sensor); + }); + } + + public void update() { + statusBar.setVisibility(deviceList.isGroupByDevice() ? View.GONE : View.VISIBLE); + statusBar.setBackgroundColor(statusColor(device)); + + if (deviceList.isGroupByDevice()) { + statusBar.setVisibility(View.GONE); + } else { + statusBar.setVisibility(View.VISIBLE); + statusBar.setBackgroundColor(statusColor(device)); + } + + Integer value = sensor.getLastValue(); + String text = "Sensor " + sensor; + text += ", value: " + (value == null ? "Unknown" : value); + description.setText(text); + + sensorProgress.setProgress(value != null ? value : 0); + } + } + + private int statusColor(SmDevice device) { + if (device.getBtDevice().connected()) { + return green; + } else if (device.getBtDevice().isRecentlySeen()) { + return yellow; + } else { + return red; } } @@ -349,6 +446,7 @@ public class MainActivity extends ListActivity { } public void notifyDataSetChanged() { + Log.i(TAG, "notifyDataSetChanged"); dataSetObservable.notifyChanged(); } @@ -475,9 +573,9 @@ public class MainActivity extends ListActivity { //noinspection unchecked return getBtDeviceView((BtDevice) o, view); } else if (o instanceof SmDevice) { - return getSmDeviceView(position, (SmDevice) o, view); + return getSmDeviceView((SmDevice) o, view); } else if (o instanceof SmSensor) { - return getSoilSensorView(position, (SmSensor) o, view); + return getSoilSensorView((SmSensor) o, view); } throw new RuntimeException("Not implemented"); @@ -486,7 +584,7 @@ public class MainActivity extends ListActivity { private View getBtDeviceView(BtDevice device, View view) { if (view == null) { view = inflater.inflate(R.layout.fragment_device, null); - view.setTag(new DeviceItem(view)); + view.setTag(new DeviceItem(device.getTag(), view)); view.setOnLongClickListener(v -> MainActivity.this.onBtDeviceLongClick(device)); } @@ -507,79 +605,33 @@ public class MainActivity extends ListActivity { return view; } - private View getSmDeviceView(int position, SmDevice smDevice, View view) { + private View getSmDeviceView(SmDevice smDevice, View view) { if (view == null) { view = inflater.inflate(R.layout.fragment_device, null); - view.setTag(new DeviceItem(view)); + view.setTag(new DeviceItem(smDevice, view)); view.setOnClickListener(v -> onSmDeviceClick(smDevice)); view.setOnLongClickListener(v -> onSmDeviceLongClick(smDevice)); } - DeviceItem item = (DeviceItem) view.getTag(); - - if (smDevice.getName() != null) { - item.deviceName.setText(smDevice.getName()); - } else { - item.deviceName.setText(R.string.unknown_device); - } - String address = smDevice.getBtDevice().getAddress(); - - if (!smDevice.isProbed()) { - address += " not probed"; - } else if (smDevice.isUseful()) { - address += " useful"; - } else { - address += " not useful"; - } - - address += ", connected=" + smDevice.getBtDevice().connected(); - - item.deviceAddress.setText(address); - - String rssi = getText(R.string.rssi) + ": " + - (smDevice.getBtDevice().getRssi() != null ? valueOf(smDevice.getBtDevice().getRssi()) : getText(R.string.unknown)); - rssi += ", device: " + smDevice.toString(); - item.rssi.setText(rssi); - - boolean useful = smDevice.isUseful(); - - if (useful) { - item.info.setText("Number of sensors: " + smDevice.getSensors().size()); - } else { - item.info.setText(""); - } + ((DeviceItem) view.getTag()).update(); return view; } - private View getSoilSensorView(int position, SmSensor smSensor, View view) { + private View getSoilSensorView(SmSensor smSensor, View view) { if (view == null) { - view = inflater.inflate(R.layout.fragment_sensor, null); - view.setTag(new SensorItem(smSensor, view)); - view.setClickable(true); - view.setOnClickListener(v -> { - Log.i(TAG, "onClick, SmSensor: " + -// "position=" + position + ", " + - "sensor=" + smSensor /*+ ", " + - "tag=" + v.getTag()*/); - onSensorClick(smSensor); - }); + view = inflater.inflate(R.layout.fragment_main_sensor, null); + SensorItem item = new SensorItem(smSensor, view); + view.setTag(item); } - SensorItem item = (SensorItem) view.getTag(); - - Integer value = smSensor.getLastValue(); - String text = "Sensor " + smSensor; - text += ", value: " + (value == null ? "Unknown" : value); - item.description.setText(text); - - item.sensorProgress.setProgress(value != null ? value : 0); + ((SensorItem) view.getTag()).update(); return view; } } - private class MySoilMoistureListener extends SoilMoistureListener { + private class MainSoilMoistureListener extends SoilMoistureListener { @Override public void onToast(int id, int length) { CharSequence text = getText(id); @@ -605,7 +657,9 @@ public class MainActivity extends ListActivity { deviceList.notifyDataSetChanged(); startScan(); - initializing.dismiss(); + if (initializing != null) { + initializing.dismiss(); + } } } @@ -630,5 +684,10 @@ public class MainActivity extends ListActivity { public void onNewSample(String address, int sensor) { deviceList.notifyDataSetChanged(); } + + @Override + public void onDevicePropertyUpdated(String address) { + deviceList.notifyDataSetChanged(); + } } } -- cgit v1.2.3