From 4336b4743178a373b365a468cae4b9c5d17ad978 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 29 Apr 2019 11:36:30 +0200 Subject: [PATCH 1/3] NPE fixes from Firebase --- .../activities/PreferencesActivity.java | 2 +- .../activities/InsightPairingActivity.java | 19 ++++++++++------ .../plugins/treatments/TreatmentService.java | 5 +++-- .../androidaps/queue/CommandQueue.java | 7 ++++-- .../androidaps/queue/QueueThread.java | 22 ++++++++++--------- app/src/test/java/info/AAPSMocker.java | 1 + 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java index aea7bcfca6..ef2639e9ac 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java @@ -93,7 +93,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre } else if (editTextPref.getText() != null) { ((EditTextPreference) pref).setDialogMessage(editTextPref.getDialogMessage()); pref.setSummary(editTextPref.getText()); - } else if (pref.getKey().contains("smscommunicator_allowednumbers") && TextUtils.isEmpty(editTextPref.getText().trim())) { + } else if (pref.getKey().contains("smscommunicator_allowednumbers") && (editTextPref.getText() == null || TextUtils.isEmpty(editTextPref.getText().trim()))) { pref.setSummary(MainApp.gs(R.string.smscommunicator_allowednumbers_summary)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java index 7ea52ed271..c165dd140e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java @@ -160,12 +160,14 @@ public class InsightPairingActivity extends AppCompatActivity implements Insight private void startBLScan() { if (!scanning) { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - if (!bluetoothAdapter.isEnabled()) bluetoothAdapter.enable(); - IntentFilter intentFilter = new IntentFilter(); - intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); - intentFilter.addAction(BluetoothDevice.ACTION_FOUND); - registerReceiver(broadcastReceiver, intentFilter); - bluetoothAdapter.startDiscovery(); + if (bluetoothAdapter != null) { + if (!bluetoothAdapter.isEnabled()) bluetoothAdapter.enable(); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + intentFilter.addAction(BluetoothDevice.ACTION_FOUND); + registerReceiver(broadcastReceiver, intentFilter); + bluetoothAdapter.startDiscovery(); + } scanning = true; } } @@ -173,7 +175,10 @@ public class InsightPairingActivity extends AppCompatActivity implements Insight private void stopBLScan() { if (scanning) { unregisterReceiver(broadcastReceiver); - BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); + BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + if (bluetoothAdapter != null) { + bluetoothAdapter.cancelDiscovery(); + } scanning = false; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java index 7bb4774911..502b7f38b7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java @@ -190,8 +190,9 @@ public class TreatmentService extends OrmLiteBaseService { } // prepare task for execution in 1 sec // cancel waiting task to prevent sending multiple posts - if (callback.getPost() != null) - callback.getPost().cancel(false); + ScheduledFuture scheduledFuture = callback.getPost(); + if (scheduledFuture != null) + scheduledFuture.cancel(false); Runnable task = new PostRunnable(); final int sec = 1; callback.setPost(eventWorker.schedule(task, sec, TimeUnit.SECONDS)); diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index e4fbacc43c..daf8239788 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -12,6 +12,8 @@ import org.slf4j.LoggerFactory; import java.util.LinkedList; +import javax.annotation.Nullable; + import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; @@ -126,6 +128,7 @@ public class CommandQueue { queue.add(command); } + @Nullable synchronized void pickup() { performing = queue.poll(); } @@ -143,11 +146,11 @@ public class CommandQueue { return queue.size(); } - public Command performing() { + Command performing() { return performing; } - public void resetPerforming() { + void resetPerforming() { performing = null; } diff --git a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java index 0962952221..a7e106c042 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java @@ -141,15 +141,17 @@ public class QueueThread extends Thread { // Pickup 1st command and set performing variable if (queue.size() > 0) { queue.pickup(); - if (L.isEnabled(L.PUMPQUEUE)) - log.debug("performing " + queue.performing().status()); - MainApp.bus().post(new EventQueueChanged()); - queue.performing().execute(); - queue.resetPerforming(); - MainApp.bus().post(new EventQueueChanged()); - lastCommandTime = System.currentTimeMillis(); - SystemClock.sleep(100); - continue; + if (queue.performing() != null) { + if (L.isEnabled(L.PUMPQUEUE)) + log.debug("performing " + queue.performing().status()); + MainApp.bus().post(new EventQueueChanged()); + queue.performing().execute(); + queue.resetPerforming(); + MainApp.bus().post(new EventQueueChanged()); + lastCommandTime = System.currentTimeMillis(); + SystemClock.sleep(100); + continue; + } } } @@ -173,7 +175,7 @@ public class QueueThread extends Thread { } } } finally { - if (mWakeLock != null) + if (mWakeLock != null && mWakeLock.isHeld()) mWakeLock.release(); if (L.isEnabled(L.PUMPQUEUE)) log.debug("thread end"); diff --git a/app/src/test/java/info/AAPSMocker.java b/app/src/test/java/info/AAPSMocker.java index 50bd20d198..79e0f89996 100644 --- a/app/src/test/java/info/AAPSMocker.java +++ b/app/src/test/java/info/AAPSMocker.java @@ -150,6 +150,7 @@ public class AAPSMocker { when(MainApp.gs(R.string.pumpsuspended)).thenReturn("Pump suspended"); when(MainApp.gs(R.string.cob)).thenReturn("COB"); when(MainApp.gs(R.string.value_unavailable_short)).thenReturn("n/a"); + when(MainApp.gs(R.string.pumpNotInitialized)).thenReturn("Pump not initialized!"); } public static MainApp mockMainApp() { From 6c7a18067fa3ba80859eb0711b50fe986c2669c6 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 1 May 2019 22:59:48 +0200 Subject: [PATCH 2/3] remove misplaced @Nullable --- .../main/java/info/nightscout/androidaps/queue/CommandQueue.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index daf8239788..7928435ed0 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -128,7 +128,6 @@ public class CommandQueue { queue.add(command); } - @Nullable synchronized void pickup() { performing = queue.poll(); } From a303d71d253f3c8d8e11a585598fafc22f01aec1 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 2 May 2019 21:51:44 +0200 Subject: [PATCH 3/3] scanning = false if no adapter present --- .../plugins/pump/insight/activities/InsightPairingActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java index c165dd140e..13fd698236 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java @@ -167,8 +167,8 @@ public class InsightPairingActivity extends AppCompatActivity implements Insight intentFilter.addAction(BluetoothDevice.ACTION_FOUND); registerReceiver(broadcastReceiver, intentFilter); bluetoothAdapter.startDiscovery(); + scanning = true; } - scanning = true; } }