diff --git a/.idea/modules.xml b/.idea/modules.xml index 25bb056..1e8560f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + diff --git a/app/src/main/java/esel/esel/esel/Esel.java b/app/src/main/java/esel/esel/esel/Esel.java index 639059e..15c9b95 100644 --- a/app/src/main/java/esel/esel/esel/Esel.java +++ b/app/src/main/java/esel/esel/esel/Esel.java @@ -6,6 +6,8 @@ import esel.esel.esel.receivers.KeepAliveReceiver; import esel.esel.esel.receivers.ReadReceiver; +import esel.esel.esel.util.SP; + /** * Created by adrian on 04/08/17. */ @@ -23,8 +25,12 @@ public void onCreate() { super.onCreate(); sInstance = this; sResources = getResources(); - startReadReceiver(); - startKeepAliveService(); + + boolean use_patched_es = SP.getBoolean("use_patched_es", false); + if (use_patched_es) { + startKeepAliveService(); + startReadReceiver(); + } } public static Esel getsInstance() { @@ -48,23 +54,27 @@ public synchronized void startReadReceiver() { public synchronized void stopReadReceiver() { - if (readReceiver != null) + if (readReceiver != null) { readReceiver.cancelAlarm(this); readReceiver = null; + } } - private void startKeepAliveService() { + public synchronized void startKeepAliveService() { if (keepAliveReceiver == null) { keepAliveReceiver = new KeepAliveReceiver(); keepAliveReceiver.setAlarm(this); + } else { + keepAliveReceiver.setAlarm(this); } } - public void stopKeepAliveService() { - if (keepAliveReceiver != null) + public synchronized void stopKeepAliveService() { + if (keepAliveReceiver != null) { keepAliveReceiver.cancelAlarm(this); + keepAliveReceiver = null; + } } - } diff --git a/app/src/main/java/esel/esel/esel/MainActivity.java b/app/src/main/java/esel/esel/esel/MainActivity.java index 23d5593..15be272 100644 --- a/app/src/main/java/esel/esel/esel/MainActivity.java +++ b/app/src/main/java/esel/esel/esel/MainActivity.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -56,6 +57,27 @@ public void onClick(View view) { .setAction("Action", null).show(); } });*/ + + SharedPreferences.OnSharedPreferenceChangeListener settingsListener = new SharedPreferences.OnSharedPreferenceChangeListener() { + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (key.equals("use_patched_es")){ + if (SP.getBoolean("use_patched_es", false)){ + Esel.getsInstance().startKeepAliveService(); + EselLog.LogV(TAG, "START patched_es keepAlive recievers"); + Esel.getsInstance().startReadReceiver(); + EselLog.LogV(TAG, "START patched_es Read recievers"); + } else { + Esel.getsInstance().stopReadReceiver(); + EselLog.LogV(TAG, "STOP patched_es Read recievers"); + Esel.getsInstance().stopKeepAliveService(); + EselLog.LogV(TAG, "STOP patched_es keepAlive recievers"); + } + } + } + }; + SP.sharedPreferences.registerOnSharedPreferenceChangeListener(settingsListener); + buttonReadValue.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { diff --git a/app/src/main/java/esel/esel/esel/datareader/EsNotificationListener.java b/app/src/main/java/esel/esel/esel/datareader/EsNotificationListener.java index 18af204..53bdbd4 100644 --- a/app/src/main/java/esel/esel/esel/datareader/EsNotificationListener.java +++ b/app/src/main/java/esel/esel/esel/datareader/EsNotificationListener.java @@ -9,6 +9,7 @@ import java.util.List; import esel.esel.esel.util.SP; +import esel.esel.esel.receivers.ReadReceiver; /** * Created by bernhard on 24-01-18. @@ -16,6 +17,7 @@ public class EsNotificationListener extends NotificationListenerService { private static List lastReadings = new ArrayList(); + private static final ReadReceiver rr = new ReadReceiver(); @Override public void onNotificationPosted(StatusBarNotification sbn) { @@ -37,6 +39,10 @@ public void onNotificationPosted(StatusBarNotification sbn) { } catch (NumberFormatException err) { err.printStackTrace(); } + + long lastReadingTime = SP.getLong("lastReadingTime", System.currentTimeMillis()); + rr.broadcastData(null, lastReadingTime, true); + } } @@ -80,7 +86,7 @@ public static SGV generateSGV(Notification notification ,int record){ SGV oldSgv = lastReadings.get(lastReadings.size() - 1); long lastreadingtime = oldSgv.timestamp; // SP.getLong("lastreadingtime_nl",timestamp); int lastreadingvalue = oldSgv.raw; //SP.getInt("lastreadingvalue_nl",value); - if (value == lastreadingvalue && (lastreadingtime + (five_min * 1.05)) > timestamp ) { // no new value + if (value == lastreadingvalue && (lastreadingtime + (five_min * 1.1)) > timestamp ) { // no new value // 5 min 30 secs grace time return null; } } diff --git a/app/src/main/java/esel/esel/esel/util/SP.java b/app/src/main/java/esel/esel/esel/util/SP.java index cf8b4f3..2042855 100644 --- a/app/src/main/java/esel/esel/esel/util/SP.java +++ b/app/src/main/java/esel/esel/esel/util/SP.java @@ -11,7 +11,7 @@ */ public class SP { - static SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Esel.getsInstance().getApplicationContext()); + static public SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Esel.getsInstance().getApplicationContext()); static public boolean contains(String key) { return sharedPreferences.contains(key);