From de45d755173fcf0ba985c381308b28c69389c900 Mon Sep 17 00:00:00 2001
From: Florian <florian.taurer@gmx.at>
Date: Thu, 15 Mar 2018 18:03:25 +0100
Subject: [PATCH] Activate alert also when opening the app in blocking state
 (because of preventive blocking). But check if already blocking before trying
 to block again... Otherwise microphone already blocked... By us.

---
 .../at/ac/fhstp/sonicontrol/MainActivity.java | 43 +++++++++++++++++--
 1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/MainActivity.java b/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/MainActivity.java
index eea9d05e..85ad1ebf 100644
--- a/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/MainActivity.java
+++ b/SoniControl/app/src/main/java/at/ac/fhstp/sonicontrol/MainActivity.java
@@ -442,7 +442,9 @@ public class MainActivity extends BaseActivity implements Scan.DetectionListener
         preventiveSpoof = settings.getBoolean(ConfigConstants.SETTING_PREVENTIVE_SPOOFING, ConfigConstants.SETTING_PREVENTIVE_SPOOFING_DEFAULT);
         if(preventiveSpoof) {
             NotificationHelper.activateSpoofingStatusNotification(getApplicationContext());
-            usedBlockingMethod = locationFinder.blockMicOrSpoof();
+            if (usedBlockingMethod == null) {
+                usedBlockingMethod = locationFinder.blockMicOrSpoof();
+            }
         }
 
         boolean gpsEnabled = settings.getBoolean(ConfigConstants.SETTING_GPS, ConfigConstants.SETTING_GPS_DEFAULT);
@@ -732,19 +734,54 @@ public class MainActivity extends BaseActivity implements Scan.DetectionListener
             state = StateEnum.ON_HOLD;
         }
 
+        Intent intent = getIntent();
         switch (state) {
             case ON_HOLD:
                 NotificationHelper.activateOnHoldStatusNotification(getApplicationContext());
                 setGUIStateStopped();
                 break;
             case JAMMING:
-                NotificationHelper.activateSpoofingStatusNotification(getApplicationContext());
                 setGUIStateStarted();
+                NotificationHelper.activateSpoofingStatusNotification(getApplicationContext());
+
+                if (intent.hasExtra(ConfigConstants.EXTRA_TECHNOLOGY_DETECTED)) {
+                    //TODO: We might use directly the "lastDetectedTechnology", not using Extras ?
+
+                    Technology technology = (Technology) intent.getExtras().get(ConfigConstants.EXTRA_TECHNOLOGY_DETECTED);
+                    if (technology != null) {
+                        activateAlert(technology);
+                        intent.removeExtra(ConfigConstants.EXTRA_TECHNOLOGY_DETECTED);
+                    }
+                }
+                else if (detectionPendingIntent != null) {
+                    if (sigType != null) {
+                        activateAlert(sigType);
+                    }
+                    else {
+                        // in case the Activity was destroyed
+                        String storedTechnology = sp.getString("lastDetectedTechnology", null);
+                        if (storedTechnology != null) {
+                            Technology lastDetectedTechnology = null;
+                            try {
+                                lastDetectedTechnology = Technology.fromString(storedTechnology);
+                            }
+                            catch (IllegalArgumentException e) {
+                                //Log.d(TAG, "onResume: " + e.getMessage());
+                            }
+                            if (lastDetectedTechnology != null) {
+                                activateAlert(lastDetectedTechnology);
+                            }
+                            else {
+                                //Log.d(TAG, "onResume: Technology not stored correctly ?");
+                            }
+                        }
+                    }
+                }
+
                 break;
             case SCANNING:
                 setGUIStateStarted();
 
-                Intent intent = getIntent();
                 if (intent.hasExtra(ConfigConstants.EXTRA_TECHNOLOGY_DETECTED)) {
                     //TODO: We might use directly the "lastDetectedTechnology", not using Extras ?
 
-- 
GitLab