diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000..d15d2440fd --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,48 @@ +This document speciffy hints and good practices for source code contributions. + +AndroidAPS is community effort and all contributions are welcome! If you wish help us improving AndroidAPS - please read and try to adhere to +this guidelines, to make the development and process of change aproval as smooth as possible :) + +General rules +============= + +* There are plenty of ways you can help, some of them are listed on wiki: + https://androidaps.readthedocs.io/en/latest/EN/Getting-Started/How-can-I-help.html +* If you wish to help with documentation or translating: + https://androidaps.readthedocs.io/en/latest/EN/translations.html + +Development guidelines +====================== + +Coding convetions +----------------- +1. Use Android Studio with default indents (4 chars, use spaces) +2. Use autoformat feature CTRL-ALT-L in every changed file before commit + +Commiting Changes / Pull Requests +--------------------------------- + +1. Make fork of repository on github +2. Create separate branch for each feature, branch from most recent dev +3. Commit all changes to your fork +4. When ready, rebase on top of dev and make pull request to main repo + +Naming Conventions for Pull Requests / Branches +----------------------------------------------- + +TODO + +Translations +------------ + +* If possible, always use Android translation mechanism (with strings.xml and @strings/id) instead of hardcoded texts +* Provide only English strings - all other languages will be crowd translated via Crowdn https://translations.androidaps.org/ + +Hints +----- + +* Start small, it is easier to review smaller changes that affect fewer parts of code +* Take a look into Issues list (https://github.com/MilosKozak/AndroidAPS/issues) - maybe there is somthing you can fix or implement +* For new features, make sure there is Issue to track progress and have on-topic discussion +* Reach out to community, discuss idea on Gitter (https://gitter.im/MilosKozak/AndroidAPS) +* Speak with other developers to minimise merge conflicts. Find out who worked, working or plan to work on speciffic issue or part of app diff --git a/app/build.gradle b/app/build.gradle index 8b2c15a042..858fe354fc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -109,7 +109,7 @@ android { targetSdkVersion 28 multiDexEnabled true versionCode 1500 - version "2.4-dev-g" + version "2.6-dev" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"' @@ -240,8 +240,6 @@ dependencies { implementation 'androidx.percentlayout:percentlayout:1.0.0' implementation "com.wdullaer:materialdatetimepicker:2.3.0" - // Otto bus will be replaced by rx - implementation "com.squareup:otto:1.3.7" implementation "io.reactivex.rxjava2:rxandroid:2.1.1" implementation "com.j256.ormlite:ormlite-core:${ormLiteVersion}" @@ -286,9 +284,6 @@ dependencies { testImplementation("com.google.truth:truth:0.39") { exclude group: "com.google.guava", module: "guava" } - testImplementation("org.robolectric:robolectric:4.2.1") { - exclude group: "com.google.guava", module: "guava" - } testImplementation "org.skyscreamer:jsonassert:1.5.0" testImplementation "org.hamcrest:hamcrest-all:1.3" /* diff --git a/app/src/main/java/com/squareup/otto/LoggingBus.java b/app/src/main/java/com/squareup/otto/LoggingBus.java deleted file mode 100644 index d9758a9a24..0000000000 --- a/app/src/main/java/com/squareup/otto/LoggingBus.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.squareup.otto; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.ConcurrentModificationException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import info.nightscout.androidaps.events.Event; -import info.nightscout.androidaps.logging.L; - -/** - * Logs events has they're being posted to and dispatched from the event bus. - *

- * A summary of event-receiver calls that occurred so far is logged - * after 10s (after startup) and then again every 60s. - */ -public class LoggingBus extends Bus { - private static Logger log = LoggerFactory.getLogger(L.EVENTS); - - private static long everyMinute = System.currentTimeMillis() + 10 * 1000; - private Map> event2Receiver = new HashMap<>(); - - public LoggingBus(ThreadEnforcer enforcer) { - super(enforcer); - } - - @Override - public void post(Object event) { - if (event instanceof DeadEvent) { - log.debug("Event has no receiver: " + ((DeadEvent) event).event + ", source: " + ((DeadEvent) event).source); - return; - } - - if (!(event instanceof Event)) { - log.error("Posted event not an event class: " + event.getClass()); - } - - log.debug("<<< " + event); - try { - StackTraceElement caller = new Throwable().getStackTrace()[1]; - String className = caller.getClassName(); - className = className.substring(className.lastIndexOf(".") + 1); - log.debug(" source: " + className + "." + caller.getMethodName() + ":" + caller.getLineNumber()); - } catch (RuntimeException e) { - log.debug(" source: "); - } - - try { - super.post(event); - } catch (IllegalStateException ignored) { - } - } - - @Override - protected void dispatch(Object event, EventHandler wrapper) { - try { - log.debug(">>> " + event); - Field methodField = wrapper.getClass().getDeclaredField("method"); - methodField.setAccessible(true); - Method targetMethod = (Method) methodField.get(wrapper); - String className = targetMethod.getDeclaringClass().getSimpleName(); - String methodName = targetMethod.getName(); - String receiverMethod = className + "." + methodName; - log.debug(" receiver: " + receiverMethod); - - String key = event.getClass().getSimpleName(); - if (!event2Receiver.containsKey(key)) event2Receiver.put(key, new HashSet()); - event2Receiver.get(key).add(receiverMethod); - } catch (ReflectiveOperationException e) { - log.debug(" receiver: "); - } - - try { - if (everyMinute < System.currentTimeMillis()) { - log.debug("***************** Event -> receiver pairings seen so far ****************"); - for (Map.Entry> stringSetEntry : event2Receiver.entrySet()) { - log.debug(" " + stringSetEntry.getKey()); - for (String s : stringSetEntry.getValue()) { - log.debug(" -> " + s); - } - } - log.debug("*************************************************************************"); - everyMinute = System.currentTimeMillis() + 60 * 1000; - } - } catch (ConcurrentModificationException ignored) { - } - - super.dispatch(event, wrapper); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/Config.java b/app/src/main/java/info/nightscout/androidaps/Config.java index ae0e14c9b6..bb967cdeb6 100644 --- a/app/src/main/java/info/nightscout/androidaps/Config.java +++ b/app/src/main/java/info/nightscout/androidaps/Config.java @@ -12,13 +12,4 @@ public class Config { public static final boolean PUMPCONTROL = BuildConfig.FLAVOR.equals("pumpcontrol"); public static final boolean PUMPDRIVERS = BuildConfig.FLAVOR.equals("full") || BuildConfig.FLAVOR.equals("pumpcontrol"); - - public static final boolean ACTION = !NSCLIENT; - public static final boolean MDI = !NSCLIENT; - public static final boolean OTHERPROFILES = !NSCLIENT; - public static final boolean SAFETY = !NSCLIENT; - - public static final boolean SMSCOMMUNICATORENABLED = !NSCLIENT; - - } diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 83dab8130d..64c2656086 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -6,7 +6,6 @@ import android.content.pm.PackageManager; import android.graphics.Rect; import android.os.Bundle; import android.os.PersistableBundle; -import android.os.PowerManager; import android.text.SpannableString; import android.text.method.LinkMovementMethod; import android.text.util.Linkify; @@ -21,6 +20,7 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AlertDialog; @@ -33,7 +33,6 @@ import com.google.android.material.navigation.NavigationView; import com.google.android.material.tabs.TabLayout; import com.joanzapata.iconify.Iconify; import com.joanzapata.iconify.fonts.FontAwesomeModule; -import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,16 +44,16 @@ import info.nightscout.androidaps.activities.PreferencesActivity; import info.nightscout.androidaps.activities.SingleFragmentActivity; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.events.EventAppExit; -import info.nightscout.androidaps.events.EventFeatureRunning; import info.nightscout.androidaps.events.EventPreferenceChange; -import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.androidaps.events.EventRebuildTabs; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; +import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt; import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus; -import info.nightscout.androidaps.plugins.general.versionChecker.VersionCheckerUtilsKt; import info.nightscout.androidaps.setupwizard.SetupWizardActivity; import info.nightscout.androidaps.tabs.TabPageAdapter; import info.nightscout.androidaps.utils.AndroidPermission; @@ -63,11 +62,12 @@ import info.nightscout.androidaps.utils.LocaleHelper; import info.nightscout.androidaps.utils.OKDialog; import info.nightscout.androidaps.utils.PasswordProtection; import info.nightscout.androidaps.utils.SP; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; public class MainActivity extends NoSplashAppCompatActivity { private static Logger log = LoggerFactory.getLogger(L.CORE); - - protected PowerManager.WakeLock mWakeLock; + private CompositeDisposable disposable = new CompositeDisposable(); private ActionBarDrawerToggle actionBarDrawerToggle; @@ -77,11 +77,8 @@ public class MainActivity extends NoSplashAppCompatActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (L.isEnabled(L.CORE)) - log.debug("onCreate"); - Iconify.with(new FontAwesomeModule()); - LocaleHelper.onCreate(this, "en"); + LocaleHelper.INSTANCE.update(getApplicationContext()); setContentView(R.layout.activity_main); setSupportActionBar(findViewById(R.id.toolbar)); @@ -95,14 +92,10 @@ public class MainActivity extends NoSplashAppCompatActivity { actionBarDrawerToggle.syncState(); // initialize screen wake lock - onEventPreferenceChange(new EventPreferenceChange(R.string.key_keep_screen_on)); + processPreferenceChange(new EventPreferenceChange(R.string.key_keep_screen_on)); doMigrations(); - registerBus(); - setupTabs(); - setupViews(false); - final ViewPager viewPager = findViewById(R.id.pager); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override @@ -124,6 +117,43 @@ public class MainActivity extends NoSplashAppCompatActivity { VersionCheckerUtilsKt.triggerCheckVersion(); FabricPrivacy.setUserStats(); + + setupTabs(); + setupViews(); + + disposable.add(RxBus.INSTANCE + .toObservable(EventRebuildTabs.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + LocaleHelper.INSTANCE.update(getApplicationContext()); + if (event.getRecreate()) { + recreate(); + } else { + setupTabs(); + setupViews(); + } + setWakeLock(); + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(this::processPreferenceChange, FabricPrivacy::logException) + ); + + if (!SP.getBoolean(R.string.key_setupwizard_processed, false)) { + Intent intent = new Intent(this, SetupWizardActivity.class); + startActivity(intent); + } else { + checkEula(); + } + + AndroidPermission.notifyForStoragePermission(this); + AndroidPermission.notifyForBatteryOptimizationPermission(this); + if (Config.PUMPDRIVERS) { + AndroidPermission.notifyForLocationPermissions(this); + AndroidPermission.notifyForSMSPermissions(this); + } } private void checkPluginPreferences(ViewPager viewPager) { @@ -139,86 +169,29 @@ public class MainActivity extends NoSplashAppCompatActivity { actionBarDrawerToggle.syncState(); } - @Override - protected void onResume() { - super.onResume(); - - if (L.isEnabled(L.CORE)) - log.debug("onResume"); - - if (!SP.getBoolean(R.string.key_setupwizard_processed, false)) { - Intent intent = new Intent(this, SetupWizardActivity.class); - startActivity(intent); - } else { - checkEula(); - } - - AndroidPermission.notifyForStoragePermission(this); - AndroidPermission.notifyForBatteryOptimizationPermission(this); - if (Config.PUMPDRIVERS) { - AndroidPermission.notifyForLocationPermissions(this); - AndroidPermission.notifyForSMSPermissions(this); - } - - MainApp.bus().post(new EventFeatureRunning(EventFeatureRunning.Feature.MAIN)); - } - @Override public void onDestroy() { - if (L.isEnabled(L.CORE)) - log.debug("onDestroy"); - if (mWakeLock != null) - if (mWakeLock.isHeld()) - mWakeLock.release(); super.onDestroy(); + disposable.clear(); } - @Subscribe - public void onEventPreferenceChange(final EventPreferenceChange ev) { - if (ev.isChanged(R.string.key_keep_screen_on)) { - boolean keepScreenOn = SP.getBoolean(R.string.key_keep_screen_on, false); - final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); - if (keepScreenOn) { - mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "AndroidAPS:MainActivity_onEventPreferenceChange"); - if (!mWakeLock.isHeld()) - mWakeLock.acquire(); - } else { - if (mWakeLock != null && mWakeLock.isHeld()) - mWakeLock.release(); - } - } + private void setWakeLock() { + boolean keepScreenOn = SP.getBoolean(R.string.key_keep_screen_on, false); + if (keepScreenOn) + getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + else + getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } - @Subscribe - public void onStatusEvent(final EventRefreshGui ev) { - String lang = SP.getString(R.string.key_language, "en"); - LocaleHelper.setLocale(getApplicationContext(), lang); - runOnUiThread(() -> { - if (ev.recreate) { - recreate(); - } else { - try { // activity may be destroyed - setupTabs(); - setupViews(false); - } catch (IllegalStateException e) { - log.error("Unhandled exception", e); - } - } - - boolean keepScreenOn = Config.NSCLIENT && SP.getBoolean(R.string.key_keep_screen_on, false); - if (keepScreenOn) - getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - else - getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - }); + public void processPreferenceChange(final EventPreferenceChange ev) { + if (ev.isChanged(R.string.key_keep_screen_on)) + setWakeLock(); } - private void setupViews(boolean switchToLast) { + private void setupViews() { TabPageAdapter pageAdapter = new TabPageAdapter(getSupportFragmentManager(), this); NavigationView navigationView = findViewById(R.id.navigation_view); - navigationView.setNavigationItemSelectedListener(menuItem -> { - return true; - }); + navigationView.setNavigationItemSelectedListener(menuItem -> true); Menu menu = navigationView.getMenu(); menu.clear(); for (PluginBase p : MainApp.getPluginsList()) { @@ -237,8 +210,8 @@ public class MainActivity extends NoSplashAppCompatActivity { } ViewPager mPager = findViewById(R.id.pager); mPager.setAdapter(pageAdapter); - if (switchToLast) - mPager.setCurrentItem(pageAdapter.getCount() - 1, false); + //if (switchToLast) + // mPager.setCurrentItem(pageAdapter.getCount() - 1, false); checkPluginPreferences(mPager); } @@ -264,15 +237,6 @@ public class MainActivity extends NoSplashAppCompatActivity { } } - private void registerBus() { - try { - MainApp.bus().unregister(this); - } catch (RuntimeException x) { - // Ignore - } - MainApp.bus().register(this); - } - private void checkEula() { //SP.removeBoolean(R.string.key_i_understand); boolean IUnderstand = SP.getBoolean(R.string.key_i_understand, false); @@ -289,10 +253,10 @@ public class MainActivity extends NoSplashAppCompatActivity { // guarantee that the unreachable threshold is at least 30 and of type String // Added in 1.57 at 21.01.2018 - Integer unreachable_threshold = SP.getInt(R.string.key_pump_unreachable_threshold, 30); + int unreachable_threshold = SP.getInt(R.string.key_pump_unreachable_threshold, 30); SP.remove(R.string.key_pump_unreachable_threshold); if (unreachable_threshold < 30) unreachable_threshold = 30; - SP.putString(R.string.key_pump_unreachable_threshold, unreachable_threshold.toString()); + SP.putString(R.string.key_pump_unreachable_threshold, Integer.toString(unreachable_threshold)); } @@ -308,19 +272,16 @@ public class MainActivity extends NoSplashAppCompatActivity { String message = "Target range is changed in current version.\n\nIt's not taken from preferences but from profile.\n\n!!! REVIEW YOUR SETTINGS !!!"; message += "\n\nOld settings: " + oldRange; message += "\nProfile settings: " + newRange; - OKDialog.show(this, "Target range change", message, new Runnable() { - @Override - public void run() { - SP.remove("openapsma_min_bg"); - SP.remove("openapsma_max_bg"); - SP.remove("openapsma_target_bg"); - } + OKDialog.show(this, "Target range change", message, () -> { + SP.remove("openapsma_min_bg"); + SP.remove("openapsma_max_bg"); + SP.remove("openapsma_target_bg"); }); } } @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (permissions.length != 0) { if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) { @@ -405,7 +366,7 @@ public class MainActivity extends NoSplashAppCompatActivity { case R.id.nav_exit: log.debug("Exiting"); MainApp.instance().stopKeepAliveService(); - MainApp.bus().post(new EventAppExit()); + RxBus.INSTANCE.send(new EventAppExit()); MainApp.closeDbHelper(); finish(); System.runFinalization(); diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 4137e2e1b1..785aa84c38 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -6,16 +6,12 @@ import android.content.IntentFilter; import android.content.res.Resources; import android.os.SystemClock; -import androidx.annotation.Nullable; import androidx.annotation.PluralsRes; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import com.crashlytics.android.Crashlytics; import com.google.firebase.analytics.FirebaseAnalytics; import com.j256.ormlite.android.apptools.OpenHelperManager; -import com.squareup.otto.Bus; -import com.squareup.otto.LoggingBus; -import com.squareup.otto.ThreadEnforcer; import net.danlew.android.joda.JodaTimeAndroid; @@ -39,7 +35,9 @@ import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.constraints.dstHelper.DstHelperPlugin; import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.constraints.safety.SafetyPlugin; +import info.nightscout.androidaps.plugins.constraints.signatureVerifier.SignatureVerifierPlugin; import info.nightscout.androidaps.plugins.constraints.storage.StorageConstraintPlugin; +import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerPlugin; import info.nightscout.androidaps.plugins.general.actions.ActionsPlugin; import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin; import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin; @@ -52,9 +50,7 @@ import info.nightscout.androidaps.plugins.general.nsclient.receivers.AckAlarmRec import info.nightscout.androidaps.plugins.general.nsclient.receivers.DBAccessReceiver; import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin; import info.nightscout.androidaps.plugins.general.persistentNotification.PersistentNotificationPlugin; -import info.nightscout.androidaps.plugins.general.signatureVerifier.SignatureVerifier; import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin; -import info.nightscout.androidaps.plugins.general.versionChecker.VersionCheckerPlugin; import info.nightscout.androidaps.plugins.general.wear.WearPlugin; import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin; import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin; @@ -92,16 +88,16 @@ import info.nightscout.androidaps.receivers.NSAlarmReceiver; import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver; import info.nightscout.androidaps.services.Intents; import info.nightscout.androidaps.utils.FabricPrivacy; +import info.nightscout.androidaps.utils.LocaleHelper; import io.fabric.sdk.android.Fabric; -import static info.nightscout.androidaps.plugins.general.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion; +import static info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion; public class MainApp extends Application { private static Logger log = LoggerFactory.getLogger(L.CORE); private static KeepAliveReceiver keepAliveReceiver; - private static Bus sBus; private static MainApp sInstance; public static Resources sResources; @@ -129,10 +125,18 @@ public class MainApp extends Application { log.debug("onCreate"); sInstance = this; sResources = getResources(); + LocaleHelper.INSTANCE.update(this); sConstraintsChecker = new ConstraintChecker(); sDatabaseHelper = OpenHelperManager.getHelper(sInstance, DatabaseHelper.class); - Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> log.error("Uncaught exception crashing app", ex)); + Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> { + if (ex instanceof InternalError) { + // usually the app trying to spawn a thread while being killed + return; + } + + log.error("Uncaught exception crashing app", ex); + }); try { if (FabricPrivacy.fabricEnabled()) { @@ -157,8 +161,6 @@ public class MainApp extends Application { engineeringMode = engineeringModeSemaphore.exists() && engineeringModeSemaphore.isFile(); devBranch = BuildConfig.VERSION.contains("-") || BuildConfig.VERSION.matches(".*[a-zA-Z]+.*"); - sBus = L.isEnabled(L.EVENTS) && devBranch ? new LoggingBus(ThreadEnforcer.ANY) : new Bus(ThreadEnforcer.ANY); - registerLocalBroadcastReceiver(); //trigger here to see the new version on app start after an update @@ -170,7 +172,7 @@ public class MainApp extends Application { // Register all tabs in app here pluginsList.add(OverviewPlugin.INSTANCE); pluginsList.add(IobCobCalculatorPlugin.getPlugin()); - if (Config.ACTION) pluginsList.add(ActionsPlugin.INSTANCE); + if (!Config.NSCLIENT) pluginsList.add(ActionsPlugin.INSTANCE); pluginsList.add(InsulinOrefRapidActingPlugin.getPlugin()); pluginsList.add(InsulinOrefUltraRapidActingPlugin.getPlugin()); pluginsList.add(InsulinOrefFreePeakPlugin.getPlugin()); @@ -183,24 +185,23 @@ public class MainApp extends Application { if (Config.PUMPDRIVERS) pluginsList.add(DanaRv2Plugin.getPlugin()); if (Config.PUMPDRIVERS) pluginsList.add(DanaRSPlugin.getPlugin()); if (Config.PUMPDRIVERS) pluginsList.add(LocalInsightPlugin.getPlugin()); - pluginsList.add(CareportalPlugin.getPlugin()); if (Config.PUMPDRIVERS) pluginsList.add(ComboPlugin.getPlugin()); - if (Config.PUMPDRIVERS && engineeringMode) - pluginsList.add(MedtronicPumpPlugin.getPlugin()); - if (Config.MDI) pluginsList.add(MDIPlugin.getPlugin()); + if (Config.PUMPDRIVERS) pluginsList.add(MedtronicPumpPlugin.getPlugin()); + if (!Config.NSCLIENT) pluginsList.add(MDIPlugin.getPlugin()); pluginsList.add(VirtualPumpPlugin.getPlugin()); + pluginsList.add(CareportalPlugin.getPlugin()); if (Config.APS) pluginsList.add(LoopPlugin.getPlugin()); if (Config.APS) pluginsList.add(OpenAPSMAPlugin.getPlugin()); if (Config.APS) pluginsList.add(OpenAPSAMAPlugin.getPlugin()); if (Config.APS) pluginsList.add(OpenAPSSMBPlugin.getPlugin()); pluginsList.add(NSProfilePlugin.getPlugin()); - if (Config.OTHERPROFILES) pluginsList.add(SimpleProfilePlugin.getPlugin()); - if (Config.OTHERPROFILES) pluginsList.add(LocalProfilePlugin.getPlugin()); + if (!Config.NSCLIENT) pluginsList.add(SimpleProfilePlugin.getPlugin()); + if (!Config.NSCLIENT) pluginsList.add(LocalProfilePlugin.getPlugin()); pluginsList.add(TreatmentsPlugin.getPlugin()); - if (Config.SAFETY) pluginsList.add(SafetyPlugin.getPlugin()); - if (Config.SAFETY) pluginsList.add(VersionCheckerPlugin.INSTANCE); - if (Config.SAFETY) pluginsList.add(StorageConstraintPlugin.getPlugin()); - if (Config.SAFETY) pluginsList.add(SignatureVerifier.getPlugin()); + if (!Config.NSCLIENT) pluginsList.add(SafetyPlugin.getPlugin()); + if (!Config.NSCLIENT) pluginsList.add(VersionCheckerPlugin.INSTANCE); + if (Config.APS) pluginsList.add(StorageConstraintPlugin.getPlugin()); + if (Config.APS) pluginsList.add(SignatureVerifierPlugin.getPlugin()); if (Config.APS) pluginsList.add(ObjectivesPlugin.INSTANCE); pluginsList.add(SourceXdripPlugin.getPlugin()); pluginsList.add(SourceNSClientPlugin.getPlugin()); @@ -210,7 +211,7 @@ public class MainApp extends Application { pluginsList.add(SourcePoctechPlugin.getPlugin()); pluginsList.add(SourceTomatoPlugin.getPlugin()); pluginsList.add(SourceEversensePlugin.getPlugin()); - if (Config.SMSCOMMUNICATORENABLED) pluginsList.add(SmsCommunicatorPlugin.getPlugin()); + if (!Config.NSCLIENT) pluginsList.add(SmsCommunicatorPlugin.INSTANCE); pluginsList.add(FoodPlugin.getPlugin()); pluginsList.add(WearPlugin.initPlugin(this)); @@ -285,10 +286,6 @@ public class MainApp extends Application { KeepAliveReceiver.cancelAlarm(this); } - public static Bus bus() { - return sBus; - } - public static String gs(int id) { return sResources.getString(id); } @@ -390,19 +387,6 @@ public class MainApp extends Application { return newList; } - @Nullable - public static T getSpecificPlugin(Class pluginClass) { - if (pluginsList != null) { - for (PluginBase p : pluginsList) { - if (pluginClass.isAssignableFrom(p.getClass())) - return (T) p; - } - } else { - log.error("pluginsList=null"); - } - return null; - } - public static boolean isEngineeringModeOrRelease() { if (!Config.APS) return true; diff --git a/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java index cd969a53a1..79f3fbf4d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java @@ -16,7 +16,6 @@ import androidx.appcompat.widget.PopupMenu; import androidx.core.content.res.ResourcesCompat; import com.jjoe64.graphview.GraphView; -import com.squareup.otto.Subscribe; import com.wdullaer.materialdatetimepicker.date.DatePickerDialog; import org.slf4j.Logger; @@ -30,6 +29,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.events.EventCustomCalculationFinished; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.overview.OverviewFragment; @@ -39,12 +39,15 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorP import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCalculationProgress; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; public class HistoryBrowseActivity extends NoSplashActivity { private static Logger log = LoggerFactory.getLogger(HistoryBrowseActivity.class); - + private CompositeDisposable disposable = new CompositeDisposable(); ImageButton chartButton; @@ -165,14 +168,33 @@ public class HistoryBrowseActivity extends NoSplashActivity { @Override public void onPause() { super.onPause(); - MainApp.bus().unregister(this); + disposable.clear(); iobCobCalculatorPlugin.stopCalculation("onPause"); } @Override public void onResume() { super.onResume(); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + if (event.getCause() == eventCustomCalculationFinished) { + log.debug("EventAutosensCalculationFinished"); + synchronized (HistoryBrowseActivity.this) { + updateGUI("EventAutosensCalculationFinished"); + } + } + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventIobCalculationProgress.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + if (iobCalculationProgressView != null) + iobCalculationProgressView.setText(event.getProgress()); + }, FabricPrivacy::logException) + ); // set start of current day Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); @@ -193,26 +215,6 @@ public class HistoryBrowseActivity extends NoSplashActivity { iobCobCalculatorPlugin.runCalculation(from, end, true, false, eventCustomCalculationFinished); } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished e) { - if (e.getCause() == eventCustomCalculationFinished) { - log.debug("EventAutosensCalculationFinished"); - runOnUiThread(() -> { - synchronized (HistoryBrowseActivity.this) { - updateGUI("EventAutosensCalculationFinished"); - } - }); - } - } - - @Subscribe - public void onStatusEvent(final EventIobCalculationProgress e) { - runOnUiThread(() -> { - if (iobCalculationProgressView != null) - iobCalculationProgressView.setText(e.progress); - }); - } - void updateGUI(String from) { log.debug("updateGUI from: " + from); 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 5096a82250..0e93a47151 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java @@ -9,27 +9,28 @@ import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceGroup; import android.preference.PreferenceManager; -import android.preference.PreferenceScreen; -import android.text.TextUtils; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.events.EventPreferenceChange; -import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.androidaps.events.EventRebuildTabs; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin; -import info.nightscout.androidaps.plugins.constraints.safety.SafetyPlugin; -import info.nightscout.androidaps.plugins.general.tidepool.TidepoolPlugin; -import info.nightscout.androidaps.plugins.general.tidepool.comm.TidepoolUploader; -import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; -import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; import info.nightscout.androidaps.plugins.aps.openAPSAMA.OpenAPSAMAPlugin; import info.nightscout.androidaps.plugins.aps.openAPSMA.OpenAPSMAPlugin; import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin; +import info.nightscout.androidaps.plugins.bus.RxBus; +import info.nightscout.androidaps.plugins.constraints.safety.SafetyPlugin; +import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin; +import info.nightscout.androidaps.plugins.general.careportal.CareportalPlugin; +import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; +import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin; +import info.nightscout.androidaps.plugins.general.tidepool.TidepoolPlugin; +import info.nightscout.androidaps.plugins.general.wear.WearPlugin; +import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin; +import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin; import info.nightscout.androidaps.plugins.pump.combo.ComboPlugin; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin; import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin; @@ -42,14 +43,9 @@ import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref0Plugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin; -import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin; -import info.nightscout.androidaps.plugins.general.wear.WearPlugin; -import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin; import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin; -import info.nightscout.androidaps.utils.LocaleHelper; import info.nightscout.androidaps.utils.OKDialog; import info.nightscout.androidaps.utils.SP; -import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin; public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { MyPreferenceFragment myPreferenceFragment; @@ -68,22 +64,19 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - MainApp.bus().post(new EventPreferenceChange(key)); RxBus.INSTANCE.send(new EventPreferenceChange(key)); if (key.equals("language")) { - String lang = sharedPreferences.getString("language", "en"); - LocaleHelper.setLocale(getApplicationContext(), lang); - MainApp.bus().post(new EventRefreshGui(true)); + RxBus.INSTANCE.send(new EventRebuildTabs(true)); //recreate() does not update language so better close settings finish(); } if (key.equals("short_tabtitles")) { - MainApp.bus().post(new EventRefreshGui()); + RxBus.INSTANCE.send(new EventRebuildTabs()); } if (key.equals(MainApp.gs(R.string.key_openapsama_useautosens)) && SP.getBoolean(R.string.key_openapsama_useautosens, false)) { OKDialog.show(this, MainApp.gs(R.string.configbuilder_sensitivity), MainApp.gs(R.string.sensitivity_warning), null); } - updatePrefSummary(myPreferenceFragment.getPreference(key)); + updatePrefSummary(myPreferenceFragment.findPreference(key)); } private static void updatePrefSummary(Preference pref) { @@ -95,13 +88,13 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre EditTextPreference editTextPref = (EditTextPreference) pref; if (pref.getKey().contains("password") || pref.getKey().contains("secret")) { pref.setSummary("******"); - } else if (pref.getKey().equals(MainApp.gs(R.string.key_danars_name))) { - pref.setSummary(SP.getString(R.string.key_danars_name, "")); } else if (editTextPref.getText() != null) { ((EditTextPreference) pref).setDialogMessage(editTextPref.getDialogMessage()); pref.setSummary(editTextPref.getText()); - } else if (pref.getKey().contains("smscommunicator_allowednumbers") && (editTextPref.getText() == null || TextUtils.isEmpty(editTextPref.getText().trim()))) { - pref.setSummary(MainApp.gs(R.string.smscommunicator_allowednumbers_summary)); + } else { + for (PluginBase plugin : MainApp.getPluginsList()) { + plugin.updatePreferenceSummary(pref); + } } } } @@ -191,7 +184,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre addPreferencesFromResourceIfEnabled(NSClientPlugin.getPlugin(), PluginType.GENERAL); addPreferencesFromResourceIfEnabled(TidepoolPlugin.INSTANCE, PluginType.GENERAL); - addPreferencesFromResourceIfEnabled(SmsCommunicatorPlugin.getPlugin(), PluginType.GENERAL); + addPreferencesFromResourceIfEnabled(SmsCommunicatorPlugin.INSTANCE, PluginType.GENERAL); addPreferencesFromResourceIfEnabled(AutomationPlugin.INSTANCE, PluginType.GENERAL); addPreferencesFromResource(R.xml.pref_others); @@ -201,26 +194,11 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre addPreferencesFromResourceIfEnabled(StatuslinePlugin.getPlugin(), PluginType.GENERAL); } - if (Config.NSCLIENT) { - PreferenceScreen scrnAdvancedSettings = (PreferenceScreen) findPreference(getString(R.string.key_advancedsettings)); - if (scrnAdvancedSettings != null) { - scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_statuslights_res_warning))); - scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_statuslights_res_critical))); - scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_statuslights_bat_warning))); - scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_statuslights_bat_critical))); - scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_show_statuslights))); - scrnAdvancedSettings.removePreference(getPreference(getString(R.string.key_show_statuslights_extended))); - } - } - initSummary(getPreferenceScreen()); - final Preference tidepoolTestLogin = findPreference(MainApp.gs(R.string.key_tidepool_test_login)); - if (tidepoolTestLogin != null) - tidepoolTestLogin.setOnPreferenceClickListener(preference -> { - TidepoolUploader.INSTANCE.testLogin(getActivity()); - return false; - }); + for (PluginBase plugin : MainApp.getPluginsList()) { + plugin.preprocessPreferences(this); + } } @Override @@ -228,9 +206,5 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre super.onSaveInstanceState(outState); outState.putInt("id", id); } - - public Preference getPreference(String key) { - return findPreference(key); - } } } diff --git a/app/src/main/java/info/nightscout/androidaps/activities/TDDStatsActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/TDDStatsActivity.java index 487def0241..24cdceada6 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/TDDStatsActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/TDDStatsActivity.java @@ -1,10 +1,8 @@ package info.nightscout.androidaps.activities; -import android.app.Activity; import android.graphics.Color; import android.graphics.Rect; import android.os.Bundle; -import androidx.recyclerview.widget.LinearLayoutManager; import android.text.TextUtils; import android.view.KeyEvent; import android.view.MotionEvent; @@ -18,7 +16,7 @@ import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.recyclerview.widget.LinearLayoutManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +37,7 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.TDD; import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin; @@ -49,11 +48,15 @@ import info.nightscout.androidaps.plugins.pump.danaRv2.DanaRv2Plugin; import info.nightscout.androidaps.plugins.pump.insight.LocalInsightPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SafeParse; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; public class TDDStatsActivity extends NoSplashActivity { private static Logger log = LoggerFactory.getLogger(TDDStatsActivity.class); + private CompositeDisposable disposable = new CompositeDisposable(); TextView statusView, statsMessage, totalBaseBasal2; EditText totalBaseBasal; @@ -74,13 +77,25 @@ public class TDDStatsActivity extends NoSplashActivity { @Override protected void onResume() { super.onResume(); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventPumpStatusChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> statusView.setText(event.getStatus()), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventDanaRSyncStatus.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + log.debug("EventDanaRSyncStatus: " + event.getMessage()); + statusView.setText(event.getMessage()); + }, FabricPrivacy::logException) + ); } @Override protected void onPause() { super.onPause(); - MainApp.bus().unregister(this); + disposable.clear(); } @Override @@ -239,7 +254,7 @@ public class TDDStatsActivity extends NoSplashActivity { statsMessage.setText(MainApp.gs(R.string.danar_stats_warning_Message)); } }); - ConfigBuilderPlugin.getPlugin().getCommandQueue().loadTDDs( new Callback() { + ConfigBuilderPlugin.getPlugin().getCommandQueue().loadTDDs(new Callback() { @Override public void run() { loadDataFromDB(); @@ -399,7 +414,7 @@ public class TDDStatsActivity extends NoSplashActivity { //cumulative TDDs for (TDD record : historyList) { - if(!historyList.isEmpty() && df.format(new Date(record.date)).equals(df.format(new Date()))) { + if (!historyList.isEmpty() && df.format(new Date(record.date)).equals(df.format(new Date()))) { //Today should not be included continue; } @@ -448,7 +463,7 @@ public class TDDStatsActivity extends NoSplashActivity { tl.setBackgroundColor(Color.TRANSPARENT); } - if(!historyList.isEmpty() && df.format(new Date(historyList.get(0).date)).equals(df.format(new Date()))) { + if (!historyList.isEmpty() && df.format(new Date(historyList.get(0).date)).equals(df.format(new Date()))) { //Today should not be included historyList.remove(0); } @@ -519,42 +534,17 @@ public class TDDStatsActivity extends NoSplashActivity { } } - @Subscribe - public void onStatusEvent(final EventDanaRSyncStatus s) { - log.debug("EventDanaRSyncStatus: " + s.message); - runOnUiThread( - new Runnable() { - @Override - public void run() { - statusView.setText(s.message); - } - }); - } - - @Subscribe - public void onStatusEvent(final EventPumpStatusChanged c) { - runOnUiThread( - new Runnable() { - @Override - public void run() { - statusView.setText(c.textStatus()); - } - } - ); - } - - public static boolean isOldData(List historyList) { Object activePump = ConfigBuilderPlugin.getPlugin().getActivePump(); - PumpInterface dana = MainApp.getSpecificPlugin(DanaRPlugin.class); - PumpInterface danaRS = MainApp.getSpecificPlugin(DanaRSPlugin.class); - PumpInterface danaV2 = MainApp.getSpecificPlugin(DanaRv2Plugin.class); - PumpInterface danaKorean = MainApp.getSpecificPlugin(DanaRKoreanPlugin.class); - PumpInterface insight = MainApp.getSpecificPlugin(LocalInsightPlugin.class); + PumpInterface dana = DanaRPlugin.getPlugin(); + PumpInterface danaRS = DanaRSPlugin.getPlugin(); + PumpInterface danaV2 = DanaRv2Plugin.getPlugin(); + PumpInterface danaKorean = DanaRKoreanPlugin.getPlugin(); + PumpInterface insight = LocalInsightPlugin.getPlugin(); boolean startsYesterday = activePump == dana || activePump == danaRS || activePump == danaV2 || activePump == danaKorean || activePump == insight; DateFormat df = new SimpleDateFormat("dd.MM."); - return (historyList.size() < 3 || !(df.format(new Date(historyList.get(0).date)).equals(df.format(new Date(System.currentTimeMillis() - (startsYesterday?1000 * 60 * 60 * 24:0)))))); + return (historyList.size() < 3 || !(df.format(new Date(historyList.get(0).date)).equals(df.format(new Date(System.currentTimeMillis() - (startsYesterday ? 1000 * 60 * 60 * 24 : 0)))))); } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java index 40fadff684..1c33e8f1be 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java +++ b/app/src/main/java/info/nightscout/androidaps/db/CareportalEvent.java @@ -100,8 +100,8 @@ public class CareportalEvent implements DataPointWithLabelInterface, Interval { String hours = " " + MainApp.gs(R.string.hours) + " "; if (useShortText) { - days = "d"; - hours = "h"; + days = MainApp.gs(R.string.shortday); + hours = MainApp.gs(R.string.shorthour); } return diff.get(TimeUnit.DAYS) + days + diff.get(TimeUnit.HOURS) + hours; diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 9fa3ab9929..d8926fc4c1 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -243,7 +243,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { new java.util.TimerTask() { @Override public void run() { - MainApp.bus().post(new EventRefreshOverview("resetDatabases")); + RxBus.INSTANCE.send(new EventRefreshOverview("resetDatabases")); } }, 3000 @@ -412,7 +412,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void run() { if (L.isEnabled(L.DATABASE)) log.debug("Firing EventNewBg"); - MainApp.bus().post(new EventNewBG(bgReading)); RxBus.INSTANCE.send(new EventNewBG(bgReading)); scheduledBgPost = null; } @@ -738,7 +737,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void run() { if (L.isEnabled(L.DATABASE)) log.debug("Firing EventTempTargetChange"); - MainApp.bus().post(new EventTempTargetChange()); + RxBus.INSTANCE.send(new EventTempTargetChange()); scheduledTemTargetPost = null; } } @@ -1035,10 +1034,10 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void run() { if (L.isEnabled(L.DATABASE)) log.debug("Firing EventTempBasalChange"); - MainApp.bus().post(new EventReloadTempBasalData()); - MainApp.bus().post(new EventTempBasalChange()); + RxBus.INSTANCE.send(new EventReloadTempBasalData()); + RxBus.INSTANCE.send(new EventTempBasalChange()); if (earliestDataChange != null) - MainApp.bus().post(new EventNewHistoryData(earliestDataChange)); + RxBus.INSTANCE.send(new EventNewHistoryData(earliestDataChange)); earliestDataChange = null; scheduledTemBasalsPost = null; } @@ -1371,9 +1370,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void run() { if (L.isEnabled(L.DATABASE)) log.debug("Firing EventExtendedBolusChange"); - MainApp.bus().post(new EventReloadTreatmentData(new EventExtendedBolusChange())); + RxBus.INSTANCE.send(new EventReloadTreatmentData(new EventExtendedBolusChange())); if (earliestDataChange != null) - MainApp.bus().post(new EventNewHistoryData(earliestDataChange)); + RxBus.INSTANCE.send(new EventNewHistoryData(earliestDataChange)); earliestDataChange = null; scheduledExtendedBolusPost = null; } @@ -1577,7 +1576,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void run() { if (L.isEnabled(L.DATABASE)) log.debug("Firing scheduleCareportalEventChange"); - MainApp.bus().post(new EventCareportalEventChange()); + RxBus.INSTANCE.send(new EventCareportalEventChange()); scheduledCareportalEventPost = null; } } @@ -1720,8 +1719,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void run() { if (L.isEnabled(L.DATABASE)) log.debug("Firing EventProfileNeedsUpdate"); - MainApp.bus().post(new EventReloadProfileSwitchData()); - MainApp.bus().post(new EventProfileNeedsUpdate()); + RxBus.INSTANCE.send(new EventReloadProfileSwitchData()); + RxBus.INSTANCE.send(new EventProfileNeedsUpdate()); scheduledProfileSwitchEventPost = null; } } diff --git a/app/src/main/java/info/nightscout/androidaps/events/Event.java b/app/src/main/java/info/nightscout/androidaps/events/Event.java deleted file mode 100644 index 864d55d6f7..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/Event.java +++ /dev/null @@ -1,16 +0,0 @@ -package info.nightscout.androidaps.events; - -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; - -/** Base class for all events posted on the event bus. */ -public abstract class Event { - static { - ReflectionToStringBuilder.setDefaultStyle(ToStringStyle.SHORT_PREFIX_STYLE); - } - - @Override - public String toString() { - return ReflectionToStringBuilder.toString(this); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/Event.kt b/app/src/main/java/info/nightscout/androidaps/events/Event.kt new file mode 100644 index 0000000000..a44f65e836 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/Event.kt @@ -0,0 +1,18 @@ +package info.nightscout.androidaps.events + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder +import org.apache.commons.lang3.builder.ToStringStyle + +/** Base class for all events posted on the event bus. */ +abstract class Event { + + override fun toString(): String { + return ReflectionToStringBuilder.toString(this) + } + + companion object { + init { + ReflectionToStringBuilder.setDefaultStyle(ToStringStyle.SHORT_PREFIX_STYLE) + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.java deleted file mode 100644 index 2dfbf9ae35..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.java +++ /dev/null @@ -1,5 +0,0 @@ -package info.nightscout.androidaps.events; - -/** Base class for events to update the UI, mostly a specific tab. */ -public class EventAcceptOpenLoopChange extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.kt new file mode 100644 index 0000000000..552564edfc --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventAcceptOpenLoopChange : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventAppExit.java b/app/src/main/java/info/nightscout/androidaps/events/EventAppExit.java deleted file mode 100644 index 9ce91a9a39..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventAppExit.java +++ /dev/null @@ -1,7 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 07.07.2016. - */ -public class EventAppExit extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventAppExit.kt b/app/src/main/java/info/nightscout/androidaps/events/EventAppExit.kt new file mode 100644 index 0000000000..640b586f5f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventAppExit.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventAppExit : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.java b/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.java deleted file mode 100644 index 17262cfb85..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 23.01.2018. - */ - -public class EventAppInitialized extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.kt b/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.kt new file mode 100644 index 0000000000..293f9698f2 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventAppInitialized : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.java b/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.java deleted file mode 100644 index cb727758bb..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.java +++ /dev/null @@ -1,21 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by adrian on 07/02/17. - */ - -public class EventBolusRequested extends Event { - private double amount; - - public EventBolusRequested (double amount){ - this.amount = amount; - } - - public double getAmount() { - return amount; - } - - public void setAmount(double amount) { - this.amount = amount; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.kt b/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.kt new file mode 100644 index 0000000000..a528ef1656 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventBolusRequested(var amount: Double) : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.java deleted file mode 100644 index 9b47ed39cb..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 25.05.2017. - */ - -public class EventCareportalEventChange extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.kt new file mode 100644 index 0000000000..e7d52d86c0 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventCareportalEventChange : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.java deleted file mode 100644 index ad5f558fe8..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 17.02.2017. - */ - -public class EventConfigBuilderChange extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.kt new file mode 100644 index 0000000000..b674374fad --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventConfigBuilderChange : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCustomActionsChanged.java b/app/src/main/java/info/nightscout/androidaps/events/EventCustomActionsChanged.java deleted file mode 100644 index 81d1f60271..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventCustomActionsChanged.java +++ /dev/null @@ -1,4 +0,0 @@ -package info.nightscout.androidaps.events; - -public class EventCustomActionsChanged extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCustomActionsChanged.kt b/app/src/main/java/info/nightscout/androidaps/events/EventCustomActionsChanged.kt new file mode 100644 index 0000000000..d75bf612ce --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventCustomActionsChanged.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventCustomActionsChanged : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java b/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java deleted file mode 100644 index e52761dc58..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 13.02.2018. - */ - -public class EventCustomCalculationFinished extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.kt b/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.kt new file mode 100644 index 0000000000..f6092b395d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventCustomCalculationFinished : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventExtendedBolusChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventExtendedBolusChange.java deleted file mode 100644 index 8881b0ecc1..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventExtendedBolusChange.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 15.05.2017. - */ - -public class EventExtendedBolusChange extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventExtendedBolusChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventExtendedBolusChange.kt new file mode 100644 index 0000000000..4ed0ca5ffe --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventExtendedBolusChange.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventExtendedBolusChange : EventLoop() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventFeatureRunning.java b/app/src/main/java/info/nightscout/androidaps/events/EventFeatureRunning.java deleted file mode 100644 index 0d07cd6c61..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventFeatureRunning.java +++ /dev/null @@ -1,36 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by jamorham on 07/02/2018. - * - * Event to indicate that an app feature is being used, for example bolus wizard being opened - * - * The purpose this has been created for is to enable opportunistic connection to the pump - * so that it is already connected before the user wishes to enact a pump function - * - */ - -public class EventFeatureRunning extends Event { - - private Feature feature = Feature.UNKNOWN; - - public EventFeatureRunning() { - } - - public EventFeatureRunning(Feature feature) { - this.feature = feature; - } - - public Feature getFeature() { - return feature; - } - - public enum Feature { - UNKNOWN, - MAIN, - WIZARD, - - JUST_ADD_MORE_HERE - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventFoodDatabaseChanged.java b/app/src/main/java/info/nightscout/androidaps/events/EventFoodDatabaseChanged.java deleted file mode 100644 index 48e6be3a14..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventFoodDatabaseChanged.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 20.09.2017. - */ - -public class EventFoodDatabaseChanged extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventFoodDatabaseChanged.kt b/app/src/main/java/info/nightscout/androidaps/events/EventFoodDatabaseChanged.kt new file mode 100644 index 0000000000..83402c3cb6 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventFoodDatabaseChanged.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventFoodDatabaseChanged : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.java b/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.java deleted file mode 100644 index f2bef1d3d0..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 13.12.2016. - */ - -public class EventInitializationChanged extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.kt b/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.kt new file mode 100644 index 0000000000..33ab0062c5 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventInitializationChanged : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.java deleted file mode 100644 index 265ce2998c..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.java +++ /dev/null @@ -1,11 +0,0 @@ -package info.nightscout.androidaps.events; - -import android.location.Location; - -public class EventLocationChange extends Event { - public Location location; - - public EventLocationChange(Location location) { - this.location = location; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.kt new file mode 100644 index 0000000000..fee6c9f800 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.events + +import android.location.Location + +class EventLocationChange(var location: Location) : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventLoop.java b/app/src/main/java/info/nightscout/androidaps/events/EventLoop.java deleted file mode 100644 index d694d52537..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventLoop.java +++ /dev/null @@ -1,5 +0,0 @@ -package info.nightscout.androidaps.events; - -/** Supeclass for all events concerned with input or output into or from the LoopPlugin. */ -public abstract class EventLoop extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventLoop.kt b/app/src/main/java/info/nightscout/androidaps/events/EventLoop.kt new file mode 100644 index 0000000000..dd28e2323b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventLoop.kt @@ -0,0 +1,4 @@ +package info.nightscout.androidaps.events + +/** Supeclass for all events concerned with input or output into or from the LoopPlugin. */ +abstract class EventLoop : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.java deleted file mode 100644 index 546d6f8624..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.java +++ /dev/null @@ -1,17 +0,0 @@ -package info.nightscout.androidaps.events; - - -import info.nightscout.androidaps.utils.StringUtils; - -public class EventNetworkChange extends Event { - - public boolean mobileConnected = false; - public boolean wifiConnected = false; - - public String ssid = ""; - public boolean roaming = false; - - public String getSsid() { - return StringUtils.removeSurroundingQuotes(ssid); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.kt new file mode 100644 index 0000000000..62c8bdd13e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.kt @@ -0,0 +1,16 @@ +package info.nightscout.androidaps.events + +import info.nightscout.androidaps.utils.StringUtils + +class EventNetworkChange : Event() { + + var mobileConnected = false + var wifiConnected = false + + var ssid = "" + var roaming = false + + fun connectedSsid(): String { + return StringUtils.removeSurroundingQuotes(ssid) + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.java b/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.java deleted file mode 100644 index db3498bed7..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.java +++ /dev/null @@ -1,17 +0,0 @@ -package info.nightscout.androidaps.events; - -import androidx.annotation.Nullable; - -import info.nightscout.androidaps.db.BgReading; - -/** - * Created by mike on 05.06.2016. - */ -public class EventNewBG extends EventLoop { - @Nullable - public final BgReading bgReading; - - public EventNewBG(BgReading bgReading) { - this.bgReading = bgReading; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.kt b/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.kt new file mode 100644 index 0000000000..08c05407c9 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.events + +import info.nightscout.androidaps.db.BgReading + +class EventNewBG(val bgReading: BgReading?) : EventLoop() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java b/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java deleted file mode 100644 index f26a310b6b..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java +++ /dev/null @@ -1,7 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 04.06.2016. - */ -public class EventNewBasalProfile extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.kt b/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.kt new file mode 100644 index 0000000000..2ffa5a9724 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventNewBasalProfile : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNsFood.java b/app/src/main/java/info/nightscout/androidaps/events/EventNsFood.java deleted file mode 100644 index 90b6f5681b..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventNsFood.java +++ /dev/null @@ -1,35 +0,0 @@ -package info.nightscout.androidaps.events; - -import android.os.Bundle; - -/** - * Event which is published with data fetched from NightScout specific for the - * Food-class. - * - * Payload is the from NS retrieved JSON-String which should be handled by all - * subscriber. - */ - -public class EventNsFood extends Event { - - public static final int ADD = 0; - public static final int UPDATE = 1; - public static final int REMOVE = 2; - - private final int mode; - - private final Bundle payload; - - public EventNsFood(int mode, Bundle payload) { - this.mode = mode; - this.payload = payload; - } - - public int getMode() { - return mode; - } - - public Bundle getPayload() { - return payload; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNsFood.kt b/app/src/main/java/info/nightscout/androidaps/events/EventNsFood.kt new file mode 100644 index 0000000000..2f34e76c85 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventNsFood.kt @@ -0,0 +1,19 @@ +package info.nightscout.androidaps.events + +import android.os.Bundle + +/** + * Event which is published with data fetched from NightScout specific for the + * Food-class. + * + * Payload is the from NS retrieved JSON-String which should be handled by all + * subscriber. + */ + +class EventNsFood(val mode: Int, val payload: Bundle) : Event() { + companion object { + val ADD = 0 + val UPDATE = 1 + val REMOVE = 2 + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNsTreatment.java b/app/src/main/java/info/nightscout/androidaps/events/EventNsTreatment.java deleted file mode 100644 index 2c5ba6c9c0..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventNsTreatment.java +++ /dev/null @@ -1,36 +0,0 @@ -package info.nightscout.androidaps.events; - -import org.json.JSONObject; - - -/** - * Event which is published with data fetched from NightScout specific for the - * Treatment-class. - *

- * Payload is the from NS retrieved JSON-String which should be handled by all - * subscriber. - */ - -public class EventNsTreatment extends Event { - - public static final int ADD = 0; - public static final int UPDATE = 1; - public static final int REMOVE = 2; - - private final int mode; - - private final JSONObject payload; - - public EventNsTreatment(int mode, JSONObject payload) { - this.mode = mode; - this.payload = payload; - } - - public int getMode() { - return mode; - } - - public JSONObject getPayload() { - return payload; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNsTreatment.kt b/app/src/main/java/info/nightscout/androidaps/events/EventNsTreatment.kt new file mode 100644 index 0000000000..149894c221 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventNsTreatment.kt @@ -0,0 +1,21 @@ +package info.nightscout.androidaps.events + +import org.json.JSONObject + + +/** + * Event which is published with data fetched from NightScout specific for the + * Treatment-class. + * + * + * Payload is the from NS retrieved JSON-String which should be handled by all + * subscriber. + */ + +class EventNsTreatment(val mode: Int, val payload: JSONObject) : Event() { + companion object { + val ADD = 0 + val UPDATE = 1 + val REMOVE = 2 + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventPreferenceChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventPreferenceChange.java deleted file mode 100644 index f23d4e802a..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventPreferenceChange.java +++ /dev/null @@ -1,25 +0,0 @@ -package info.nightscout.androidaps.events; - -import info.nightscout.androidaps.MainApp; - -/** - * Created by mike on 19.06.2016. - */ -public class EventPreferenceChange extends Event { - public String changedKey; - public EventPreferenceChange(String key) { - changedKey = key; - } - - public EventPreferenceChange(int resourceID) { - changedKey = MainApp.gs(resourceID); - } - - public boolean isChanged(int id) { - return changedKey.equals(MainApp.gs(id)); - } - - public boolean isChanged(String id) { - return changedKey.equals(id); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventPreferenceChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventPreferenceChange.kt new file mode 100644 index 0000000000..d224d75df1 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventPreferenceChange.kt @@ -0,0 +1,19 @@ +package info.nightscout.androidaps.events + +import info.nightscout.androidaps.MainApp + +class EventPreferenceChange : Event { + private var changedKey: String? = null + + constructor(key: String) { + changedKey = key + } + + constructor(resourceID: Int) { + changedKey = MainApp.gs(resourceID) + } + + fun isChanged(id: Int): Boolean { + return changedKey == MainApp.gs(id) + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventProfileNeedsUpdate.java b/app/src/main/java/info/nightscout/androidaps/events/EventProfileNeedsUpdate.java deleted file mode 100644 index 9e3f3b08c7..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventProfileNeedsUpdate.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 02.06.2017. - */ - -public class EventProfileNeedsUpdate extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventProfileNeedsUpdate.kt b/app/src/main/java/info/nightscout/androidaps/events/EventProfileNeedsUpdate.kt new file mode 100644 index 0000000000..2baf1db945 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventProfileNeedsUpdate.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventProfileNeedsUpdate : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventProfileStoreChanged.java b/app/src/main/java/info/nightscout/androidaps/events/EventProfileStoreChanged.java deleted file mode 100644 index 0b2d933c12..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventProfileStoreChanged.java +++ /dev/null @@ -1,4 +0,0 @@ -package info.nightscout.androidaps.events; - -public class EventProfileStoreChanged extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventProfileStoreChanged.kt b/app/src/main/java/info/nightscout/androidaps/events/EventProfileStoreChanged.kt new file mode 100644 index 0000000000..0e839ca2d3 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventProfileStoreChanged.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventProfileStoreChanged : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventPumpStatusChanged.java b/app/src/main/java/info/nightscout/androidaps/events/EventPumpStatusChanged.java deleted file mode 100644 index 6729a4e703..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventPumpStatusChanged.java +++ /dev/null @@ -1,63 +0,0 @@ -package info.nightscout.androidaps.events; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; - -/** - * Created by mike on 19.02.2017. - */ - -public class EventPumpStatusChanged extends Event { - public static final int CONNECTING = 0; - public static final int CONNECTED = 1; - public static final int HANDSHAKING = 2; - public static final int PERFORMING = 3; - public static final int DISCONNECTING = 4; - public static final int DISCONNECTED = 5; - - public int sStatus = DISCONNECTED; - public int sSecondsElapsed = 0; - public String sPerfomingAction = ""; - - public static String error = ""; - - public EventPumpStatusChanged(int status) { - sStatus = status; - sSecondsElapsed = 0; - error = ""; - } - - public EventPumpStatusChanged(int status, int secondsElapsed) { - sStatus = status; - sSecondsElapsed = secondsElapsed; - error = ""; - } - - public EventPumpStatusChanged(int status, String error) { - sStatus = status; - sSecondsElapsed = 0; - this.error = error; - } - - public EventPumpStatusChanged(String action) { - sStatus = PERFORMING; - sSecondsElapsed = 0; - sPerfomingAction = action; - } - - public String textStatus() { - if (sStatus == CONNECTING) - return String.format(MainApp.gs(R.string.danar_history_connectingfor), sSecondsElapsed); - else if (sStatus == HANDSHAKING) - return MainApp.gs(R.string.handshaking); - else if (sStatus == CONNECTED) - return MainApp.gs(R.string.connected); - else if (sStatus == PERFORMING) - return sPerfomingAction; - else if (sStatus == DISCONNECTING) - return MainApp.gs(R.string.disconnecting); - else if (sStatus == DISCONNECTED) - return ""; - return ""; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventPumpStatusChanged.kt b/app/src/main/java/info/nightscout/androidaps/events/EventPumpStatusChanged.kt new file mode 100644 index 0000000000..3d25bc1ca5 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventPumpStatusChanged.kt @@ -0,0 +1,62 @@ +package info.nightscout.androidaps.events + +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R + +class EventPumpStatusChanged : EventStatus { + + enum class Status { + CONNECTING, + CONNECTED, + HANDSHAKING, + PERFORMING, + DISCONNECTING, + DISCONNECTED + } + + var sStatus: Status = Status.DISCONNECTED + var sSecondsElapsed = 0 + var sPerfomingAction = "" + var error = "" + + constructor(status: Status) { + sStatus = status + sSecondsElapsed = 0 + error = "" + } + + constructor(status: Status, secondsElapsed: Int) { + sStatus = status + sSecondsElapsed = secondsElapsed + error = "" + } + + constructor(status: Status, error: String) { + sStatus = status + sSecondsElapsed = 0 + this.error = error + } + + constructor(action: String) { + sStatus = Status.PERFORMING + sSecondsElapsed = 0 + sPerfomingAction = action + } + + // status for startup wizard + override fun getStatus(): String { + if (sStatus == Status.CONNECTING) + return String.format(MainApp.gs(R.string.danar_history_connectingfor), sSecondsElapsed) + else if (sStatus == Status.HANDSHAKING) + return MainApp.gs(R.string.handshaking) + else if (sStatus == Status.CONNECTED) + return MainApp.gs(R.string.connected) + else if (sStatus == Status.PERFORMING) + return sPerfomingAction + else if (sStatus == Status.DISCONNECTING) + return MainApp.gs(R.string.disconnecting) + else if (sStatus == Status.DISCONNECTED) + return "" + return "" + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventRebuildTabs.kt b/app/src/main/java/info/nightscout/androidaps/events/EventRebuildTabs.kt new file mode 100644 index 0000000000..aa0db3467a --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventRebuildTabs.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventRebuildTabs @JvmOverloads constructor(var recreate: Boolean = false) : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventRefreshGui.java b/app/src/main/java/info/nightscout/androidaps/events/EventRefreshGui.java deleted file mode 100644 index 390ad8ea4f..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventRefreshGui.java +++ /dev/null @@ -1,14 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 13.06.2016. - */ -public class EventRefreshGui extends Event { - public boolean recreate = false; - public EventRefreshGui(boolean recreate) { - this.recreate = recreate; - } - public EventRefreshGui(){ - this(false); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventRefreshOverview.java b/app/src/main/java/info/nightscout/androidaps/events/EventRefreshOverview.java deleted file mode 100644 index 2ba78fa9ec..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventRefreshOverview.java +++ /dev/null @@ -1,13 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 16.06.2017. - */ - -public class EventRefreshOverview extends Event { - public String from; - - public EventRefreshOverview(String from) { - this.from = from; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventRefreshOverview.kt b/app/src/main/java/info/nightscout/androidaps/events/EventRefreshOverview.kt new file mode 100644 index 0000000000..533a25dd40 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventRefreshOverview.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventRefreshOverview(var from: String) : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventReloadProfileSwitchData.java b/app/src/main/java/info/nightscout/androidaps/events/EventReloadProfileSwitchData.java deleted file mode 100644 index 212e8856d9..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventReloadProfileSwitchData.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 12.06.2017. - */ - -public class EventReloadProfileSwitchData extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventReloadProfileSwitchData.kt b/app/src/main/java/info/nightscout/androidaps/events/EventReloadProfileSwitchData.kt new file mode 100644 index 0000000000..6f6d848b5e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventReloadProfileSwitchData.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventReloadProfileSwitchData : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventReloadTempBasalData.java b/app/src/main/java/info/nightscout/androidaps/events/EventReloadTempBasalData.java deleted file mode 100644 index 80125cbb4a..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventReloadTempBasalData.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 29.05.2017. - */ - -public class EventReloadTempBasalData extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventReloadTempBasalData.kt b/app/src/main/java/info/nightscout/androidaps/events/EventReloadTempBasalData.kt new file mode 100644 index 0000000000..fa8f720896 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventReloadTempBasalData.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventReloadTempBasalData : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventReloadTreatmentData.java b/app/src/main/java/info/nightscout/androidaps/events/EventReloadTreatmentData.java deleted file mode 100644 index 0ba9b95ad7..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventReloadTreatmentData.java +++ /dev/null @@ -1,13 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 29.05.2017. - */ - -public class EventReloadTreatmentData extends Event { - public Object next; - - public EventReloadTreatmentData(Object next) { - this.next = next; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventReloadTreatmentData.kt b/app/src/main/java/info/nightscout/androidaps/events/EventReloadTreatmentData.kt new file mode 100644 index 0000000000..1f8b2938b9 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventReloadTreatmentData.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventReloadTreatmentData(var next: Event) : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventStatus.kt b/app/src/main/java/info/nightscout/androidaps/events/EventStatus.kt new file mode 100644 index 0000000000..193c3b1fdb --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventStatus.kt @@ -0,0 +1,6 @@ +package info.nightscout.androidaps.events + +// pass string to startup wizard +abstract class EventStatus :Event() { + abstract fun getStatus() : String +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventTempBasalChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventTempBasalChange.java deleted file mode 100644 index 73660bb00e..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventTempBasalChange.java +++ /dev/null @@ -1,7 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 05.06.2016. - */ -public class EventTempBasalChange extends EventLoop { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventTempBasalChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventTempBasalChange.kt new file mode 100644 index 0000000000..3f3ecf732e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventTempBasalChange.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventTempBasalChange : EventLoop() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventTempTargetChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventTempTargetChange.java deleted file mode 100644 index 4e3bf5c5f8..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventTempTargetChange.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 13.01.2017. - */ - -public class EventTempTargetChange extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventTempTargetChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventTempTargetChange.kt new file mode 100644 index 0000000000..c108d6589c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventTempTargetChange.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventTempTargetChange : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventTreatmentChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventTreatmentChange.java deleted file mode 100644 index e1d9d527e1..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventTreatmentChange.java +++ /dev/null @@ -1,17 +0,0 @@ -package info.nightscout.androidaps.events; - -import androidx.annotation.Nullable; - -import info.nightscout.androidaps.plugins.treatments.Treatment; - -/** - * Created by mike on 04.06.2016. - */ -public class EventTreatmentChange extends EventLoop { - @Nullable - public final Treatment treatment; - - public EventTreatmentChange(Treatment treatment) { - this.treatment = treatment; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventTreatmentChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventTreatmentChange.kt new file mode 100644 index 0000000000..9cbc9d1563 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventTreatmentChange.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.events + +import info.nightscout.androidaps.plugins.treatments.Treatment + +class EventTreatmentChange(val treatment: Treatment?) : EventLoop() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventUpdateGui.java b/app/src/main/java/info/nightscout/androidaps/events/EventUpdateGui.java deleted file mode 100644 index 3471d2e851..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventUpdateGui.java +++ /dev/null @@ -1,5 +0,0 @@ -package info.nightscout.androidaps.events; - -/** Base class for events to update the UI, mostly a specific tab. */ -public abstract class EventUpdateGui extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventUpdateGui.kt b/app/src/main/java/info/nightscout/androidaps/events/EventUpdateGui.kt new file mode 100644 index 0000000000..cc21e784b9 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventUpdateGui.kt @@ -0,0 +1,4 @@ +package info.nightscout.androidaps.events + +/** Base class for events to update the UI, mostly a specific tab. */ +abstract class EventUpdateGui : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java index 7b1d0c6175..8de88e1e0e 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java @@ -1,24 +1,26 @@ package info.nightscout.androidaps.interfaces; import android.os.SystemClock; +import android.preference.Preference; +import android.preference.PreferenceFragment; import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.FragmentActivity; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.events.EventConfigBuilderChange; -import info.nightscout.androidaps.events.EventRefreshGui; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.configBuilder.EventConfigBuilderUpdateGui; -import info.nightscout.androidaps.utils.SP; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventConfigBuilderChange; +import info.nightscout.androidaps.events.EventRebuildTabs; import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.configBuilder.EventConfigBuilderUpdateGui; import info.nightscout.androidaps.queue.CommandQueue; +import info.nightscout.androidaps.utils.SP; /** * Created by mike on 09.06.2016. @@ -81,8 +83,8 @@ public abstract class PluginBase { setFragmentVisible(type, enabled); ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(this, getType()); ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxEnabled"); - MainApp.bus().post(new EventRefreshGui()); - MainApp.bus().post(new EventConfigBuilderChange()); + RxBus.INSTANCE.send(new EventRebuildTabs()); + RxBus.INSTANCE.send(new EventConfigBuilderChange()); RxBus.INSTANCE.send(new EventConfigBuilderUpdateGui()); ConfigBuilderPlugin.getPlugin().logPluginStatus(); } @@ -216,4 +218,10 @@ public abstract class PluginBase { protected void onStateChange(PluginType type, State oldState, State newState) { } + + public void preprocessPreferences(@NotNull final PreferenceFragment preferenceFragment) { + } + + public void updatePreferenceSummary(@NotNull final Preference pref) { + } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/APSResult.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/APSResult.java index b3b0c091d5..7e568af2d9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/APSResult.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/APSResult.java @@ -247,7 +247,7 @@ public class APSResult { } } } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return array; } @@ -280,7 +280,7 @@ public class APSResult { } } } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return latest; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java index c9444acb33..f339bd3771 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java @@ -14,8 +14,6 @@ import android.os.SystemClock; import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,6 +50,7 @@ import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; +import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperActivity; import info.nightscout.androidaps.plugins.general.wear.ActionStringHandler; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin; @@ -62,12 +61,15 @@ import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; import info.nightscout.androidaps.utils.ToastUtils; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Created by mike on 05.08.2016. */ public class LoopPlugin extends PluginBase { private static Logger log = LoggerFactory.getLogger(L.APS); + private CompositeDisposable disposable = new CompositeDisposable(); private static final String CHANNEL_ID = "AndroidAPS-Openloop"; @@ -116,9 +118,39 @@ public class LoopPlugin extends PluginBase { @Override protected void onStart() { - MainApp.bus().register(this); createNotificationChannel(); super.onStart(); + disposable.add(RxBus.INSTANCE + .toObservable(EventTempTargetChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + invoke("EventTempTargetChange", true); + }, FabricPrivacy::logException) + ); + /** + * This method is triggered once autosens calculation has completed, so the LoopPlugin + * has current data to work with. However, autosens calculation can be triggered by multiple + * sources and currently only a new BG should trigger a loop run. Hence we return early if + * the event causing the calculation is not EventNewBg. + *

+ */ + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + // Autosens calculation not triggered by a new BG + if (!(event.getCause() instanceof EventNewBG)) return; + + BgReading bgReading = DatabaseHelper.actualBg(); + // BG outdated + if (bgReading == null) return; + // already looped with that value + if (bgReading.date <= lastBgTriggeredRun) return; + + lastBgTriggeredRun = bgReading.date; + invoke("AutosenseCalculation for " + bgReading, true); + }, FabricPrivacy::logException) + ); } private void createNotificationChannel() { @@ -135,8 +167,8 @@ public class LoopPlugin extends PluginBase { @Override protected void onStop() { + disposable.clear(); super.onStop(); - MainApp.bus().unregister(this); } @Override @@ -145,43 +177,10 @@ public class LoopPlugin extends PluginBase { return pump == null || pump.getPumpDescription().isTempBasalCapable; } - /** - * This method is triggered once autosens calculation has completed, so the LoopPlugin - * has current data to work with. However, autosens calculation can be triggered by multiple - * sources and currently only a new BG should trigger a loop run. Hence we return early if - * the event causing the calculation is not EventNewBg. - *

- */ - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - if (!(ev.getCause() instanceof EventNewBG)) { - // Autosens calculation not triggered by a new BG - return; - } - BgReading bgReading = DatabaseHelper.actualBg(); - if (bgReading == null) { - // BG outdated - return; - } - if (bgReading.date <= lastBgTriggeredRun) { - // already looped with that value - return; - } - - lastBgTriggeredRun = bgReading.date; - invoke("AutosenseCalculation for " + bgReading, true); - } - public long suspendedTo() { return loopSuspendedTill; } - @Subscribe - public void onStatusEvent(final EventTempTargetChange ev) { - new Thread(() -> invoke("EventTempTargetChange", true)).start(); - } - - public void suspendTo(long endTime) { loopSuspendedTill = endTime; isSuperBolus = false; @@ -439,7 +438,7 @@ public class LoopPlugin extends PluginBase { (NotificationManager) MainApp.instance().getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(Constants.notificationID, builder.build()); - MainApp.bus().post(new EventNewOpenLoopNotification()); + RxBus.INSTANCE.send(new EventNewOpenLoopNotification()); // Send to Wear ActionStringHandler.handleInitiate("changeRequest"); @@ -472,7 +471,7 @@ public class LoopPlugin extends PluginBase { NSUpload.uploadDeviceStatus(); SP.incInt(R.string.key_ObjectivesmanualEnacts); } - MainApp.bus().post(new EventAcceptOpenLoopChange()); + RxBus.INSTANCE.send(new EventAcceptOpenLoopChange()); } }); FabricPrivacy.getInstance().logCustom("AcceptTemp"); @@ -647,7 +646,12 @@ public class LoopPlugin extends PluginBase { @Override public void run() { if (!result.success) { - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.tempbasaldeliveryerror)); + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.boluserror); + i.putExtra("status", result.comment); + i.putExtra("title", MainApp.gs(R.string.tempbasaldeliveryerror)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); } } }); @@ -656,7 +660,12 @@ public class LoopPlugin extends PluginBase { @Override public void run() { if (!result.success) { - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.tempbasaldeliveryerror)); + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.boluserror); + i.putExtra("status", result.comment); + i.putExtra("title", MainApp.gs(R.string.tempbasaldeliveryerror)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); } } }); @@ -667,7 +676,12 @@ public class LoopPlugin extends PluginBase { @Override public void run() { if (!result.success) { - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.extendedbolusdeliveryerror)); + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.boluserror); + i.putExtra("status", result.comment); + i.putExtra("title", MainApp.gs(R.string.extendedbolusdeliveryerror)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); } } }); @@ -681,7 +695,12 @@ public class LoopPlugin extends PluginBase { @Override public void run() { if (!result.success) { - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.tempbasaldeliveryerror)); + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.boluserror); + i.putExtra("status", result.comment); + i.putExtra("title", MainApp.gs(R.string.tempbasaldeliveryerror)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); } } }); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/events/EventNewOpenLoopNotification.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/events/EventNewOpenLoopNotification.java deleted file mode 100644 index 6a0a4dc0cf..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/events/EventNewOpenLoopNotification.java +++ /dev/null @@ -1,9 +0,0 @@ -package info.nightscout.androidaps.plugins.aps.loop.events; - -import info.nightscout.androidaps.events.Event; - -/** - * Created by mike on 07.08.2016. - */ -public class EventNewOpenLoopNotification extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/events/EventNewOpenLoopNotification.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/events/EventNewOpenLoopNotification.kt new file mode 100644 index 0000000000..2933318dd6 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/events/EventNewOpenLoopNotification.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.aps.loop.events + +import info.nightscout.androidaps.events.Event + +class EventNewOpenLoopNotification : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAPlugin.java index ef31ad599e..65db6e440c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAPlugin.java @@ -164,7 +164,7 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface { return; if (!HardLimits.checkOnlyHardLimits(Profile.toMgdl(profile.getIsf(), units), "sens", HardLimits.MINISF, HardLimits.MAXISF)) return; - if (!HardLimits.checkOnlyHardLimits(profile.getMaxDailyBasal(), "max_daily_basal", 0.05, HardLimits.maxBasal())) + if (!HardLimits.checkOnlyHardLimits(profile.getMaxDailyBasal(), "max_daily_basal", 0.02, HardLimits.maxBasal())) return; if (!HardLimits.checkOnlyHardLimits(pump.getBaseBasalRate(), "current_basal", 0.01, HardLimits.maxBasal())) return; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/OpenAPSMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/OpenAPSMAPlugin.java index 899c0151d8..8cac8b87cc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/OpenAPSMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSMA/OpenAPSMAPlugin.java @@ -162,7 +162,7 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface { return; if (!checkOnlyHardLimits(Profile.toMgdl(profile.getIsf(), units), "sens", HardLimits.MINISF, HardLimits.MAXISF)) return; - if (!checkOnlyHardLimits(profile.getMaxDailyBasal(), "max_daily_basal", 0.05, HardLimits.maxBasal())) + if (!checkOnlyHardLimits(profile.getMaxDailyBasal(), "max_daily_basal", 0.02, HardLimits.maxBasal())) return; if (!checkOnlyHardLimits(pump.getBaseBasalRate(), "current_basal", 0.01, HardLimits.maxBasal())) return; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBPlugin.java index d410eba291..c27a3f2059 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBPlugin.java @@ -170,7 +170,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface, Constr return; if (!checkOnlyHardLimits(Profile.toMgdl(profile.getIsf(), units), "sens", HardLimits.MINISF, HardLimits.MAXISF)) return; - if (!checkOnlyHardLimits(profile.getMaxDailyBasal(), "max_daily_basal", 0.05, HardLimits.maxBasal())) + if (!checkOnlyHardLimits(profile.getMaxDailyBasal(), "max_daily_basal", 0.02, HardLimits.maxBasal())) return; if (!checkOnlyHardLimits(pump.getBaseBasalRate(), "current_basal", 0.01, HardLimits.maxBasal())) return; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/common/SubscriberFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/common/SubscriberFragment.java deleted file mode 100644 index 4e6f07671d..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/common/SubscriberFragment.java +++ /dev/null @@ -1,22 +0,0 @@ -package info.nightscout.androidaps.plugins.common; - -import androidx.fragment.app.Fragment; - -import info.nightscout.androidaps.MainApp; - -abstract public class SubscriberFragment extends Fragment { - @Override - public void onPause() { - super.onPause(); - MainApp.bus().unregister(this); - } - - @Override - public void onResume() { - super.onResume(); - MainApp.bus().register(this); - updateGUI(); - } - - protected abstract void updateGUI(); -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java index b73c535147..f43513fbf5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java @@ -20,6 +20,7 @@ import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.SensitivityInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.insulin.InsulinOrefRapidActingPlugin; import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin; import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin; @@ -67,14 +68,12 @@ public class ConfigBuilderPlugin extends PluginBase { @Override protected void onStart() { - MainApp.bus().register(this); super.onStart(); } @Override protected void onStop() { super.onStop(); - MainApp.bus().unregister(this); } @@ -83,7 +82,7 @@ public class ConfigBuilderPlugin extends PluginBase { upgradeSettings(); loadSettings(); setAlwaysEnabledPluginsEnabled(); - MainApp.bus().post(new EventAppInitialized()); + RxBus.INSTANCE.send(new EventAppInitialized()); } private void setAlwaysEnabledPluginsEnabled() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/PluginViewHolder.kt b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/PluginViewHolder.kt index ddb5356f70..86e0f39437 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/PluginViewHolder.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/PluginViewHolder.kt @@ -3,12 +3,12 @@ package info.nightscout.androidaps.plugins.configBuilder import android.content.Intent import android.view.View import android.widget.* -import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.activities.PreferencesActivity -import info.nightscout.androidaps.events.EventRefreshGui +import info.nightscout.androidaps.events.EventRebuildTabs import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.PluginType +import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.utils.PasswordProtection class PluginViewHolder internal constructor(private val fragment: ConfigBuilderFragment, @@ -34,7 +34,7 @@ class PluginViewHolder internal constructor(private val fragment: ConfigBuilderF pluginVisibility.setOnClickListener { plugin.setFragmentVisible(pluginType, pluginVisibility.isChecked) ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxVisible") - MainApp.bus().post(EventRefreshGui()) + RxBus.send(EventRebuildTabs()) ConfigBuilderPlugin.getPlugin().logPluginStatus() } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java index b9276a5ac3..91c20b095a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java @@ -2,10 +2,10 @@ package info.nightscout.androidaps.plugins.configBuilder; import android.content.Intent; import android.os.Bundle; + import androidx.annotation.Nullable; import com.google.firebase.analytics.FirebaseAnalytics; -import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,14 +23,19 @@ import info.nightscout.androidaps.events.EventProfileNeedsUpdate; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperActivity; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; +import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; public class ProfileFunctions { private static Logger log = LoggerFactory.getLogger(L.PROFILE); + private CompositeDisposable disposable = new CompositeDisposable(); private static ProfileFunctions profileFunctions = null; @@ -44,61 +49,69 @@ public class ProfileFunctions { ProfileFunctions.getInstance(); // register to bus at start } - ProfileFunctions() { - MainApp.bus().register(this); - } - - @Subscribe - public void onProfileSwitch(EventProfileNeedsUpdate ignored) { - if (L.isEnabled(L.PROFILE)) - log.debug("onProfileSwitch"); - ConfigBuilderPlugin.getPlugin().getCommandQueue().setProfile(getProfile(), new Callback() { - @Override - public void run() { - if (!result.success) { - Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); - i.putExtra("soundid", R.raw.boluserror); - i.putExtra("status", result.comment); - i.putExtra("title", MainApp.gs(R.string.failedupdatebasalprofile)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - if (result.enacted) - MainApp.bus().post(new EventNewBasalProfile()); - } - }); + private ProfileFunctions() { + disposable.add(RxBus.INSTANCE + .toObservable(EventProfileNeedsUpdate.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (L.isEnabled(L.PROFILE)) + log.debug("onProfileSwitch"); + ConfigBuilderPlugin.getPlugin().getCommandQueue().setProfile(getProfile(), new Callback() { + @Override + public void run() { + if (!result.success) { + Intent i = new Intent(MainApp.instance(), ErrorHelperActivity.class); + i.putExtra("soundid", R.raw.boluserror); + i.putExtra("status", result.comment); + i.putExtra("title", MainApp.gs(R.string.failedupdatebasalprofile)); + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + MainApp.instance().startActivity(i); + } + if (result.enacted) + RxBus.INSTANCE.send(new EventNewBasalProfile()); + } + }); + }, FabricPrivacy::logException) + ); } public String getProfileName() { - return getProfileName(System.currentTimeMillis()); + return getProfileName(System.currentTimeMillis(), true, false); } public String getProfileName(boolean customized) { - return getProfileName(System.currentTimeMillis(), customized); + return getProfileName(System.currentTimeMillis(), customized, false); } - public String getProfileName(long time) { - return getProfileName(time, true); + public String getProfileNameWithDuration() { + return getProfileName(System.currentTimeMillis(), true, true); } - public String getProfileName(long time, boolean customized) { + public String getProfileName(long time, boolean customized, boolean showRemainingTime) { + String profileName = MainApp.gs(R.string.noprofileselected); + TreatmentsInterface activeTreatments = TreatmentsPlugin.getPlugin(); ProfileInterface activeProfile = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface(); ProfileSwitch profileSwitch = activeTreatments.getProfileSwitchFromHistory(time); if (profileSwitch != null) { if (profileSwitch.profileJson != null) { - return customized ? profileSwitch.getCustomizedName() : profileSwitch.profileName; + profileName = customized ? profileSwitch.getCustomizedName() : profileSwitch.profileName; } else { ProfileStore profileStore = activeProfile.getProfile(); if (profileStore != null) { Profile profile = profileStore.getSpecificProfile(profileSwitch.profileName); if (profile != null) - return profileSwitch.profileName; + profileName = profileSwitch.profileName; } } + + if (showRemainingTime && profileSwitch.durationInMinutes != 0) { + profileName += DateUtil.untilString(profileSwitch.originalEnd()); + } + return profileName; } - return MainApp.gs(R.string.noprofileselected); + return profileName; } public boolean isProfileValid(String from) { @@ -171,7 +184,7 @@ public class ProfileFunctions { profileSwitch = new ProfileSwitch(); profileSwitch.date = System.currentTimeMillis(); profileSwitch.source = Source.USER; - profileSwitch.profileName = getInstance().getProfileName(System.currentTimeMillis(), false); + profileSwitch.profileName = getInstance().getProfileName(System.currentTimeMillis(), false, false); profileSwitch.profileJson = getInstance().getProfile().getData().toString(); profileSwitch.profilePlugin = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getClass().getName(); profileSwitch.durationInMinutes = duration; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/dstHelper/DstHelperPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/dstHelper/DstHelperPlugin.java index e026d6589d..828fa1b86f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/dstHelper/DstHelperPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/dstHelper/DstHelperPlugin.java @@ -22,7 +22,7 @@ import info.nightscout.androidaps.plugins.general.overview.notifications.Notific public class DstHelperPlugin extends PluginBase implements ConstraintsInterface { public static final int DISABLE_TIMEFRAME_HOURS = -3; - public static final int WARN_PRIOR_TIMEFRAME_HOURS = 24; + public static final int WARN_PRIOR_TIMEFRAME_HOURS = 12; private static Logger log = LoggerFactory.getLogger(L.CONSTRAINTS); static DstHelperPlugin plugin = null; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesFragment.kt index cd6e6dff4c..5edb199c9a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesFragment.kt @@ -51,7 +51,6 @@ class ObjectivesFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - objectives_recyclerview.layoutManager = LinearLayoutManager(view.context) objectives_recyclerview.adapter = objectivesAdapter objectives_fake.setOnClickListener { updateGUI() } @@ -105,17 +104,14 @@ class ObjectivesFragment : Fragment() { for (i in 0 until ObjectivesPlugin.objectives.size) { val objective = ObjectivesPlugin.objectives[i] if (!objective.isStarted || !objective.isAccomplished) { - val smoothScroller = object : LinearSmoothScroller(context!!) { - override fun getVerticalSnapPreference(): Int { - return SNAP_TO_START - } - - override fun calculateTimeForScrolling(dx: Int): Int { - return super.calculateTimeForScrolling(dx) * 4 + context?.let { + val smoothScroller = object : LinearSmoothScroller(it) { + override fun getVerticalSnapPreference(): Int = SNAP_TO_START + override fun calculateTimeForScrolling(dx: Int): Int = super.calculateTimeForScrolling(dx) * 4 } + smoothScroller.targetPosition = i + objectives_recyclerview.layoutManager?.startSmoothScroll(smoothScroller) } - smoothScroller.targetPosition = i - objectives_recyclerview.layoutManager?.startSmoothScroll(smoothScroller) break } } @@ -208,49 +204,63 @@ class ObjectivesFragment : Fragment() { holder.accomplished.setTextColor(-0x3e3e3f) holder.verify.setOnClickListener { holder.verify.visibility = View.INVISIBLE - SntpClient.ntpTime(object : SntpClient.Callback() { - override fun run() { - activity?.runOnUiThread { - holder.verify.visibility = View.VISIBLE - log.debug("NTP time: $time System time: ${DateUtil.now()}") - if (!networkConnected) { - ToastUtils.showToastInUiThread(context, R.string.notconnected) - } else if (success) { - if (objective.isCompleted(time)) { - objective.accomplishedOn = time - notifyDataSetChanged() - scrollToCurrentObjective() - startUpdateTimer() + NetworkChangeReceiver.grabNetworkStatus(context) + if (objectives_fake.isChecked) { + objective.accomplishedOn = DateUtil.now() + scrollToCurrentObjective() + startUpdateTimer() + RxBus.send(EventObjectivesUpdateGui()) + } else + SntpClient.ntpTime(object : SntpClient.Callback() { + override fun run() { + activity?.runOnUiThread { + holder.verify.visibility = View.VISIBLE + log.debug("NTP time: $time System time: ${DateUtil.now()}") + if (!networkConnected) { + ToastUtils.showToastInUiThread(context, R.string.notconnected) + } else if (success) { + if (objective.isCompleted(time)) { + objective.accomplishedOn = time + scrollToCurrentObjective() + startUpdateTimer() + RxBus.send(EventObjectivesUpdateGui()) + } else { + ToastUtils.showToastInUiThread(context, R.string.requirementnotmet) + } } else { - ToastUtils.showToastInUiThread(context, R.string.requirementnotmet) + ToastUtils.showToastInUiThread(context, R.string.failedretrievetime) } - } else { - ToastUtils.showToastInUiThread(context, R.string.failedretrievetime) } } - } - }, NetworkChangeReceiver.isConnected()) + }, NetworkChangeReceiver.isConnected()) } holder.start.setOnClickListener { holder.start.visibility = View.INVISIBLE - SntpClient.ntpTime(object : SntpClient.Callback() { - override fun run() { - activity?.runOnUiThread { - holder.start.visibility = View.VISIBLE - log.debug("NTP time: $time System time: ${DateUtil.now()}") - if (!networkConnected) { - ToastUtils.showToastInUiThread(context, R.string.notconnected) - } else if (success) { - objective.startedOn = time - notifyDataSetChanged() - scrollToCurrentObjective() - startUpdateTimer() - } else { - ToastUtils.showToastInUiThread(context, R.string.failedretrievetime) + NetworkChangeReceiver.grabNetworkStatus(context) + if (objectives_fake.isChecked) { + objective.startedOn = DateUtil.now() + scrollToCurrentObjective() + startUpdateTimer() + RxBus.send(EventObjectivesUpdateGui()) + } else + SntpClient.ntpTime(object : SntpClient.Callback() { + override fun run() { + activity?.runOnUiThread { + holder.start.visibility = View.VISIBLE + log.debug("NTP time: $time System time: ${DateUtil.now()}") + if (!networkConnected) { + ToastUtils.showToastInUiThread(context, R.string.notconnected) + } else if (success) { + objective.startedOn = time + scrollToCurrentObjective() + startUpdateTimer() + RxBus.send(EventObjectivesUpdateGui()) + } else { + ToastUtils.showToastInUiThread(context, R.string.failedretrievetime) + } } } - } - }, NetworkChangeReceiver.isConnected()) + }, NetworkChangeReceiver.isConnected()) } holder.revert.setOnClickListener { objective.accomplishedOn = 0 @@ -259,8 +269,8 @@ class ObjectivesFragment : Fragment() { val prevObj = ObjectivesPlugin.objectives[position - 1] prevObj.accomplishedOn = 0 } - notifyDataSetChanged() scrollToCurrentObjective() + RxBus.send(EventObjectivesUpdateGui()) } if (objective.hasSpecialInput && !objective.isAccomplished && objective.isStarted) { // generate random request code if none exists @@ -274,7 +284,7 @@ class ObjectivesFragment : Fragment() { holder.enterButton.setOnClickListener { val input = holder.input.text.toString() objective.specialAction(activity, input) - notifyDataSetChanged() + RxBus.send(EventObjectivesUpdateGui()) } } else { holder.enterButton.visibility = View.GONE diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPlugin.kt index 7913e73426..5882f84d61 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesPlugin.kt @@ -21,8 +21,8 @@ import java.util.* object ObjectivesPlugin : PluginBase(PluginDescription() .mainType(PluginType.CONSTRAINTS) .fragmentClass(ObjectivesFragment::class.qualifiedName) - .alwaysEnabled(!Config.NSCLIENT) - .showInList(!Config.NSCLIENT) + .alwaysEnabled(Config.APS) + .showInList(Config.APS) .pluginName(R.string.objectives) .shortName(R.string.objectives_shortname) .description(R.string.description_objectives)), ConstraintsInterface { @@ -105,7 +105,7 @@ object ObjectivesPlugin : PluginBase(PluginDescription() val requestCode = SP.getString(R.string.key_objectives_request_code, "") var url = SP.getString(R.string.key_nsclientinternal_url, "").toLowerCase() if (!url.endsWith("\"")) url = "$url/" - val hashNS = Hashing.sha1().hashString(url + BuildConfig.APPLICATION_ID + "/" + requestCode, Charsets.UTF_8).toString() + @Suppress("DEPRECATION") val hashNS = Hashing.sha1().hashString(url + BuildConfig.APPLICATION_ID + "/" + requestCode, Charsets.UTF_8).toString() if (request.equals(hashNS.substring(0, 10), ignoreCase = true)) { SP.putLong("Objectives_" + "openloop" + "_started", DateUtil.now()) SP.putLong("Objectives_" + "openloop" + "_accomplished", DateUtil.now()) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt index 30ab4b500b..1428652be5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/activities/ObjectivesExamDialog.kt @@ -57,12 +57,11 @@ class ObjectivesExamDialog : DialogFragment() { objectives_exam_question.setText(task.question) // Options objectives_exam_options.removeAllViews() - for (o in task.options) { - val option: Option = o as Option; - val cb = option.generate(context) + task.options.forEach { + val cb = it.generate(context) if (task.answered) { cb.isEnabled = false - if (option.isCorrect) + if (it.isCorrect) cb.isChecked = true } objectives_exam_options.addView(cb) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java index b126f93673..c882a921f5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java @@ -230,7 +230,7 @@ public abstract class Objective { return question; } - public List getOptions() { + public List getOptions() { return options; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective2.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective2.java index 0a101dd975..017ad33a8c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective2.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective2.java @@ -37,6 +37,7 @@ public class Objective2 extends Objective { ); tasks.add(new ExamTask(R.string.pumpdisconnect_label, R.string.pumpdisconnect_label,"pumpdisconnect") .option(new Option(R.string.pumpdisconnect_letknow, true)) + .option(new Option(R.string.pumpdisconnect_suspend, false)) .option(new Option(R.string.pumpdisconnect_dontchnage, false)) .hint(new Hint(R.string.pumpdisconnect_hint1)) ); @@ -45,6 +46,7 @@ public class Objective2 extends Objective { .option(new Option(R.string.objectives_storeelsewhere, true)) .option(new Option(R.string.objectives_doexportonstart, false)) .option(new Option(R.string.objectives_doexportafterchange, true)) + .option(new Option(R.string.objectives_doexportafterobjective, true)) .option(new Option(R.string.objectives_doexportafterfirtssettings, true)) .hint(new Hint(R.string.objectives_hint1)) .hint(new Hint(R.string.objectives_hint2)) @@ -65,6 +67,7 @@ public class Objective2 extends Objective { .option(new Option(R.string.exercise_switchprofileabove100, false)) .option(new Option(R.string.exercise_stoploop, false)) .option(new Option(R.string.exercise_doitbeforestart, true)) + .option(new Option(R.string.exercise_afterstart, true)) .hint(new Hint(R.string.exercise_hint1)) ); tasks.add(new ExamTask(R.string.suspendloop_label, R.string.suspendloop_doigetinsulin,"suspendloop") @@ -196,6 +199,11 @@ public class Objective2 extends Objective { .hint(new Hint(R.string.profileswitchtime_hint1)) ); + tasks.add(new ExamTask(R.string.other_medication_label, R.string.other_medication_text,"otherMedicationWarning") + .option(new Option(R.string.yes, true)) + .option(new Option(R.string.no, false)) + ); + for (Task task : tasks) Collections.shuffle(((ExamTask)task).options); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/signatureVerifier/SignatureVerifier.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/signatureVerifier/SignatureVerifierPlugin.java similarity index 73% rename from app/src/main/java/info/nightscout/androidaps/plugins/general/signatureVerifier/SignatureVerifier.java rename to app/src/main/java/info/nightscout/androidaps/plugins/constraints/signatureVerifier/SignatureVerifierPlugin.java index b0616ec35e..9c9e38bb46 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/signatureVerifier/SignatureVerifier.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/signatureVerifier/SignatureVerifierPlugin.java @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.general.signatureVerifier; +package info.nightscout.androidaps.plugins.constraints.signatureVerifier; import android.content.pm.PackageManager; import android.content.pm.Signature; @@ -42,23 +42,23 @@ import info.nightscout.androidaps.utils.SP; * In case someone decides to leak a ready-to-use APK nonetheless, we can still disable it. * Self-compiled APKs with privately held certificates cannot and will not be disabled. */ -public class SignatureVerifier extends PluginBase implements ConstraintsInterface { +public class SignatureVerifierPlugin extends PluginBase implements ConstraintsInterface { private static final String REVOKED_CERTS_URL = "https://raw.githubusercontent.com/MilosKozak/AndroidAPS/master/app/src/main/assets/revoked_certs.txt"; private static final long UPDATE_INTERVAL = TimeUnit.DAYS.toMillis(1); - private static SignatureVerifier plugin = new SignatureVerifier(); + private static SignatureVerifierPlugin plugin = new SignatureVerifierPlugin(); private Logger log = LoggerFactory.getLogger(L.CORE); private final Object $lock = new Object[0]; private File revokedCertsFile; private List revokedCerts; - public static SignatureVerifier getPlugin() { + public static SignatureVerifierPlugin getPlugin() { return plugin; } - private SignatureVerifier() { + private SignatureVerifierPlugin() { super(new PluginDescription() .mainType(PluginType.CONSTRAINTS) .neverVisible(true) @@ -124,14 +124,52 @@ public class SignatureVerifier extends PluginBase implements ConstraintsInterfac } } } - } catch (PackageManager.NameNotFoundException e) { - log.error("Error in SignatureVerifier", e); - } catch (NoSuchAlgorithmException e) { - log.error("Error in SignatureVerifier", e); + } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) { + log.error("Error in SignatureVerifierPlugin", e); } return false; } + public List shortHashes() { + List hashes = new ArrayList<>(); + try { + Signature[] signatures = MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), PackageManager.GET_SIGNATURES).signatures; + if (signatures != null) { + for (Signature signature : signatures) { + MessageDigest digest = MessageDigest.getInstance("SHA256"); + byte[] fingerprint = digest.digest(signature.toByteArray()); + String hash = Hex.toHexString(fingerprint); + log.debug("Found signature: " + hash); + log.debug("Found signature (short): " + singleCharMap(fingerprint)); + hashes.add(singleCharMap(fingerprint)); + } + } + } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) { + log.error("Error in SignatureVerifierPlugin", e); + } + return hashes; + } + + String map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"§$%&/()=?,.-;:_<>|°^`´\\@€*'#+~{}[]¿¡áéíóúàèìòùöäü`ÁÉÍÓÚÀÈÌÒÙÖÄÜßÆÇÊËÎÏԌ۟æçêëîïôœûÿĆČĐŠŽćđšžñΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡ\u03A2ΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρςστυφχψωϨϩϪϫϬϭϮϯϰϱϲϳϴϵ϶ϷϸϹϺϻϼϽϾϿЀЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗ"; + + private String singleCharMap(byte[] array) { + StringBuilder sb = new StringBuilder(); + for (byte b : array) { + sb.append(map.charAt(b & 0xFF)); + } + return sb.toString(); + } + + public String singleCharUnMap(String shortHash) { + byte[] array = new byte[shortHash.length()]; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i != 0) sb.append(":"); + sb.append(String.format("%02X", 0xFF & map.charAt(map.indexOf(shortHash.charAt(i))))); + } + return sb.toString(); + } + private boolean shouldDownloadCerts() { return System.currentTimeMillis() - SP.getLong(R.string.key_last_revoked_certs_check, 0L) >= UPDATE_INTERVAL; } @@ -153,7 +191,7 @@ public class SignatureVerifier extends PluginBase implements ConstraintsInterfac this.revokedCerts = parseRevokedCertsFile(revokedCerts); } } catch (IOException e) { - log.error("Error in SignatureVerifier", e); + log.error("Error in SignatureVerifierPlugin", e); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerPlugin.kt similarity index 97% rename from app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerPlugin.kt rename to app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerPlugin.kt index bf67426787..ee267c689e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerPlugin.kt @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.general.versionChecker +package info.nightscout.androidaps.plugins.constraints.versionChecker import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtils.kt similarity index 69% rename from app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt rename to app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtils.kt index 7961b19e83..ac9724608f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtils.kt @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.general.versionChecker +package info.nightscout.androidaps.plugins.constraints.versionChecker import android.content.Context import android.net.ConnectivityManager @@ -15,21 +15,14 @@ import java.io.IOException import java.net.URL import java.util.concurrent.TimeUnit - // check network connection fun isConnected(): Boolean { val connMgr = MainApp.instance().applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager return connMgr.activeNetworkInfo?.isConnected ?: false } -fun findVersion(file :String?): String? { - val regex = "(.*)version(.*)\"(((\\d+)\\.)+(\\d+))\"(.*)".toRegex() - return file?.lines()?.filter { regex.matches(it) }?.mapNotNull { regex.matchEntire(it)?.groupValues?.getOrNull(3) }?.firstOrNull() -} - private val log = LoggerFactory.getLogger(L.CORE) - fun triggerCheckVersion() { if (!SP.contains(R.string.key_last_time_this_version_detected)) { @@ -56,13 +49,34 @@ private fun checkVersion() = if (isConnected()) { log.debug("Github master version no checked. No connectivity") fun compareWithCurrentVersion(newVersion: String?, currentVersion: String) { - val comparison: Int? = newVersion?.versionStrip()?.compareTo(currentVersion.versionStrip()) - when { - comparison == null -> onVersionNotDetectable() - comparison == 0 -> onSameVersionDetected() - comparison > 0 -> onNewVersionDetected(currentVersion = currentVersion, newVersion = newVersion) - else -> onOlderVersionDetected() + + val newVersionElements = newVersion.toNumberList() + val currentVersionElements = currentVersion.toNumberList() + + if (newVersionElements == null || newVersionElements.isEmpty()) { + onVersionNotDetectable() + return } + + if (currentVersionElements == null || currentVersionElements.isEmpty()) { + // current version scrambled?! + onNewVersionDetected(currentVersion, newVersion) + return + } + + newVersionElements.take(3).forEachIndexed { i, newElem -> + val currElem: Int = currentVersionElements.getOrNull(i) + ?: return onNewVersionDetected(currentVersion, newVersion) + + (newElem - currElem).let { + when { + it > 0 -> return onNewVersionDetected(currentVersion, newVersion) + it < 0 -> return onOlderVersionDetected() + it == 0 -> Unit + } + } + } + onSameVersionDetected() } private fun onOlderVersionDetected() { @@ -75,7 +89,7 @@ fun onSameVersionDetected() { } fun onVersionNotDetectable() { - log.debug("fetch failed, ignore and smartcast to non-null") + log.debug("fetch failed") } fun onNewVersionDetected(currentVersion: String, newVersion: String?) { @@ -88,14 +102,25 @@ fun onNewVersionDetected(currentVersion: String, newVersion: String?) { } } +@Deprecated(replaceWith = ReplaceWith("numericVersionPart()"), message = "Will not work if RCs have another index number in it.") fun String.versionStrip() = this.mapNotNull { when (it) { in '0'..'9' -> it - '.' -> it - else -> null + '.' -> it + else -> null } }.joinToString(separator = "") +fun String.numericVersionPart(): String = + "(((\\d+)\\.)+(\\d+))(\\D(.*))?".toRegex().matchEntire(this)?.groupValues?.getOrNull(1) ?: "" + +fun String?.toNumberList() = + this?.numericVersionPart().takeIf { !it.isNullOrBlank() }?.split(".")?.map { it.toInt() } + +fun findVersion(file: String?): String? { + val regex = "(.*)version(.*)\"(((\\d+)\\.)+(\\d+))\"(.*)".toRegex() + return file?.lines()?.filter { regex.matches(it) }?.mapNotNull { regex.matchEntire(it)?.groupValues?.getOrNull(3) }?.firstOrNull() +} val CHECK_EVERY = TimeUnit.DAYS.toMillis(1) val WARN_EVERY = TimeUnit.DAYS.toMillis(1) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.java deleted file mode 100644 index 579700be4c..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.java +++ /dev/null @@ -1,329 +0,0 @@ -package info.nightscout.androidaps.plugins.general.actions; - - -import android.app.Activity; -import android.content.Intent; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.LinearLayout; - -import androidx.fragment.app.FragmentManager; - -import com.squareup.otto.Subscribe; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.activities.HistoryBrowseActivity; -import info.nightscout.androidaps.activities.TDDStatsActivity; -import info.nightscout.androidaps.db.ExtendedBolus; -import info.nightscout.androidaps.db.TemporaryBasal; -import info.nightscout.androidaps.events.EventCustomActionsChanged; -import info.nightscout.androidaps.events.EventExtendedBolusChange; -import info.nightscout.androidaps.events.EventInitializationChanged; -import info.nightscout.androidaps.events.EventRefreshOverview; -import info.nightscout.androidaps.events.EventTempBasalChange; -import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.actions.defs.CustomAction; -import info.nightscout.androidaps.plugins.general.actions.dialogs.FillDialog; -import info.nightscout.androidaps.plugins.general.actions.dialogs.NewExtendedBolusDialog; -import info.nightscout.androidaps.plugins.general.actions.dialogs.NewTempBasalDialog; -import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment; -import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; -import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow; -import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.utils.SP; -import info.nightscout.androidaps.utils.SingleClickButton; - -public class ActionsFragment extends SubscriberFragment implements View.OnClickListener { - - private View actionsFragmentView; - private SingleClickButton profileSwitch; - private SingleClickButton tempTarget; - private SingleClickButton extendedBolus; - private SingleClickButton extendedBolusCancel; - private SingleClickButton tempBasal; - private SingleClickButton tempBasalCancel; - private SingleClickButton fill; - private SingleClickButton tddStats; - private SingleClickButton history; - - private Map pumpCustomActions = new HashMap<>(); - private List pumpCustomButtons = new ArrayList<>(); - - public ActionsFragment() { - super(); - } - - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.actions_fragment, container, false); - - profileSwitch = view.findViewById(R.id.actions_profileswitch); - tempTarget = view.findViewById(R.id.actions_temptarget); - extendedBolus = view.findViewById(R.id.actions_extendedbolus); - extendedBolusCancel = view.findViewById(R.id.actions_extendedbolus_cancel); - tempBasal = view.findViewById(R.id.actions_settempbasal); - tempBasalCancel = view.findViewById(R.id.actions_canceltempbasal); - fill = view.findViewById(R.id.actions_fill); - tddStats = view.findViewById(R.id.actions_tddstats); - history = view.findViewById(R.id.actions_historybrowser); - - profileSwitch.setOnClickListener(this); - tempTarget.setOnClickListener(this); - extendedBolus.setOnClickListener(this); - extendedBolusCancel.setOnClickListener(this); - tempBasal.setOnClickListener(this); - tempBasalCancel.setOnClickListener(this); - fill.setOnClickListener(this); - history.setOnClickListener(this); - tddStats.setOnClickListener(this); - - actionsFragmentView = view; - - updateGUI(); - SP.putBoolean(R.string.key_objectiveuseactions, true); - return view; - } - - @Subscribe - public void onStatusEvent(final EventInitializationChanged ev) { - updateGUI(); - } - - @Subscribe - public void onStatusEvent(final EventRefreshOverview ev) { - updateGUI(); - } - - @Subscribe - public void onStatusEvent(final EventExtendedBolusChange ev) { - updateGUI(); - } - - @Subscribe - public void onStatusEvent(final EventTempBasalChange ev) { - updateGUI(); - } - - @Subscribe - public void onStatusEvent(final EventCustomActionsChanged ev) { - updateGUI(); - } - - @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - if (ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null && ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getProfile() != null) { - profileSwitch.setVisibility(View.VISIBLE); - } else { - profileSwitch.setVisibility(View.GONE); - } - - if (ProfileFunctions.getInstance().getProfile() == null) { - tempTarget.setVisibility(View.GONE); - extendedBolus.setVisibility(View.GONE); - extendedBolusCancel.setVisibility(View.GONE); - tempBasal.setVisibility(View.GONE); - tempBasalCancel.setVisibility(View.GONE); - fill.setVisibility(View.GONE); - return; - } - - final PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); - final boolean basalprofileEnabled = MainApp.isEngineeringModeOrRelease() - && pump.getPumpDescription().isSetBasalProfileCapable; - - if (!basalprofileEnabled || !pump.isInitialized() || pump.isSuspended()) - profileSwitch.setVisibility(View.GONE); - else - profileSwitch.setVisibility(View.VISIBLE); - - if (!pump.getPumpDescription().isExtendedBolusCapable || !pump.isInitialized() || pump.isSuspended() || pump.isFakingTempsByExtendedBoluses()) { - extendedBolus.setVisibility(View.GONE); - extendedBolusCancel.setVisibility(View.GONE); - } else { - ExtendedBolus activeExtendedBolus = TreatmentsPlugin.getPlugin().getExtendedBolusFromHistory(System.currentTimeMillis()); - if (activeExtendedBolus != null) { - extendedBolus.setVisibility(View.GONE); - extendedBolusCancel.setVisibility(View.VISIBLE); - extendedBolusCancel.setText(MainApp.gs(R.string.cancel) + " " + activeExtendedBolus.toString()); - } else { - extendedBolus.setVisibility(View.VISIBLE); - extendedBolusCancel.setVisibility(View.GONE); - } - } - - - if (!pump.getPumpDescription().isTempBasalCapable || !pump.isInitialized() || pump.isSuspended()) { - tempBasal.setVisibility(View.GONE); - tempBasalCancel.setVisibility(View.GONE); - } else { - final TemporaryBasal activeTemp = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(System.currentTimeMillis()); - if (activeTemp != null) { - tempBasal.setVisibility(View.GONE); - tempBasalCancel.setVisibility(View.VISIBLE); - tempBasalCancel.setText(MainApp.gs(R.string.cancel) + " " + activeTemp.toStringShort()); - } else { - tempBasal.setVisibility(View.VISIBLE); - tempBasalCancel.setVisibility(View.GONE); - } - } - - if (!pump.getPumpDescription().isRefillingCapable || !pump.isInitialized() || pump.isSuspended()) - fill.setVisibility(View.GONE); - else - fill.setVisibility(View.VISIBLE); - - if (!Config.APS) - tempTarget.setVisibility(View.GONE); - else - tempTarget.setVisibility(View.VISIBLE); - - if (!pump.getPumpDescription().supportsTDDs) - tddStats.setVisibility(View.GONE); - else - tddStats.setVisibility(View.VISIBLE); - - checkPumpCustomActions(); - - } - }); - } - - - View.OnClickListener pumpCustomActionsListener = v -> { - - SingleClickButton btn = (SingleClickButton) v; - - CustomAction customAction = this.pumpCustomActions.get(btn.getText().toString()); - - ConfigBuilderPlugin.getPlugin().getActivePump().executeCustomAction(customAction.getCustomActionType()); - - }; - - - private void checkPumpCustomActions() { - - PumpInterface activePump = ConfigBuilderPlugin.getPlugin().getActivePump(); - - removePumpCustomActions(); - - if (activePump == null) { - return; - } - - List customActions = activePump.getCustomActions(); - - if (customActions != null && customActions.size() > 0) { - - LinearLayout ll = actionsFragmentView.findViewById(R.id.action_buttons_layout); - - for (CustomAction customAction : customActions) { - - if (!customAction.isEnabled()) - continue; - - SingleClickButton btn = new SingleClickButton(getContext(), null, android.R.attr.buttonStyle); - btn.setText(MainApp.gs(customAction.getName())); - - LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f); - layoutParams.setMargins(20, 8, 20, 8); // 10,3,10,3 - - btn.setLayoutParams(layoutParams); - btn.setOnClickListener(pumpCustomActionsListener); - - Drawable top = getResources().getDrawable(customAction.getIconResourceId()); - btn.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null); - - ll.addView(btn); - - this.pumpCustomActions.put(MainApp.gs(customAction.getName()), customAction); - this.pumpCustomButtons.add(btn); - } - } - } - - - private void removePumpCustomActions() { - - if (pumpCustomActions.size() == 0) - return; - - LinearLayout ll = actionsFragmentView.findViewById(R.id.action_buttons_layout); - - for (SingleClickButton customButton : pumpCustomButtons) { - ll.removeView(customButton); - } - - pumpCustomButtons.clear(); - } - - - @Override - public void onClick(View view) { - FragmentManager manager = getFragmentManager(); - switch (view.getId()) { - case R.id.actions_profileswitch: - NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); - final OptionsToShow profileswitch = CareportalFragment.PROFILESWITCH; - profileswitch.executeProfileSwitch = true; - newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); - newDialog.show(manager, "NewNSTreatmentDialog"); - break; - case R.id.actions_temptarget: - NewNSTreatmentDialog newTTDialog = new NewNSTreatmentDialog(); - final OptionsToShow temptarget = CareportalFragment.TEMPTARGET; - temptarget.executeTempTarget = true; - newTTDialog.setOptions(temptarget, R.string.careportal_temporarytarget); - newTTDialog.show(manager, "NewNSTreatmentDialog"); - break; - case R.id.actions_extendedbolus: - NewExtendedBolusDialog newExtendedDialog = new NewExtendedBolusDialog(); - newExtendedDialog.show(manager, "NewExtendedDialog"); - break; - case R.id.actions_extendedbolus_cancel: - if (TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) { - ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelExtended(null); - } - break; - case R.id.actions_canceltempbasal: - if (TreatmentsPlugin.getPlugin().isTempBasalInProgress()) { - ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, null); - } - break; - case R.id.actions_settempbasal: - NewTempBasalDialog newTempDialog = new NewTempBasalDialog(); - newTempDialog.show(manager, "NewTempDialog"); - break; - case R.id.actions_fill: - FillDialog fillDialog = new FillDialog(); - fillDialog.show(manager, "FillDialog"); - break; - case R.id.actions_historybrowser: - startActivity(new Intent(getContext(), HistoryBrowseActivity.class)); - break; - case R.id.actions_tddstats: - startActivity(new Intent(getContext(), TDDStatsActivity.class)); - break; - } - } -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt new file mode 100644 index 0000000000..dfc06e6156 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsFragment.kt @@ -0,0 +1,242 @@ +package info.nightscout.androidaps.plugins.general.actions + + +import android.content.Intent +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.LinearLayout +import androidx.fragment.app.Fragment +import info.nightscout.androidaps.Config +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.HistoryBrowseActivity +import info.nightscout.androidaps.activities.TDDStatsActivity +import info.nightscout.androidaps.events.* +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.actions.defs.CustomAction +import info.nightscout.androidaps.plugins.general.actions.dialogs.FillDialog +import info.nightscout.androidaps.plugins.general.actions.dialogs.NewExtendedBolusDialog +import info.nightscout.androidaps.plugins.general.actions.dialogs.NewTempBasalDialog +import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment +import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.queue.Callback +import info.nightscout.androidaps.utils.* +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.CompositeDisposable +import kotlinx.android.synthetic.main.actions_fragment.* +import java.util.* + +class ActionsFragment : Fragment() { + + private var disposable: CompositeDisposable = CompositeDisposable() + + private val pumpCustomActions = HashMap() + private val pumpCustomButtons = ArrayList() + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + return inflater.inflate(R.layout.actions_fragment, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + actions_profileswitch.setOnClickListener { + val newDialog = NewNSTreatmentDialog() + val profileSwitch = CareportalFragment.PROFILESWITCH + profileSwitch.executeProfileSwitch = true + newDialog.setOptions(profileSwitch, R.string.careportal_profileswitch) + fragmentManager?.let { newDialog.show(it, "NewNSTreatmentDialog") } + } + actions_temptarget.setOnClickListener { + val newTTDialog = NewNSTreatmentDialog() + val temptarget = CareportalFragment.TEMPTARGET + temptarget.executeTempTarget = true + newTTDialog.setOptions(temptarget, R.string.careportal_temporarytarget) + fragmentManager?.let { newTTDialog.show(it, "NewNSTreatmentDialog") } + } + actions_extendedbolus.setOnClickListener { + fragmentManager?.let { NewExtendedBolusDialog().show(it, "NewExtendedDialog") } + } + actions_extendedbolus_cancel.setOnClickListener { + if (TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress) { + ConfigBuilderPlugin.getPlugin().commandQueue.cancelExtended(object : Callback() { + override fun run() { + if (!result.success) + ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.extendedbolusdeliveryerror)) + } + }) + } + } + actions_settempbasal.setOnClickListener { fragmentManager?.let { NewTempBasalDialog().show(it, "NewTempDialog") } } + actions_canceltempbasal.setOnClickListener { + if (TreatmentsPlugin.getPlugin().isTempBasalInProgress) { + ConfigBuilderPlugin.getPlugin().commandQueue.cancelTempBasal(true, object : Callback() { + override fun run() { + if (!result.success) + ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.tempbasaldeliveryerror)) + } + }) + } + } + actions_fill.setOnClickListener { fragmentManager?.let { FillDialog().show(it, "FillDialog") } } + actions_historybrowser.setOnClickListener { startActivity(Intent(context, HistoryBrowseActivity::class.java)) } + actions_tddstats.setOnClickListener { startActivity(Intent(context, TDDStatsActivity::class.java)) } + + SP.putBoolean(R.string.key_objectiveuseactions, true) + } + + @Synchronized + override fun onResume() { + super.onResume() + disposable += RxBus + .toObservable(EventInitializationChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + updateGui() + }, { + FabricPrivacy.logException(it) + }) + disposable += RxBus + .toObservable(EventRefreshOverview::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + updateGui() + }, { + FabricPrivacy.logException(it) + }) + disposable += RxBus + .toObservable(EventExtendedBolusChange::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + updateGui() + }, { + FabricPrivacy.logException(it) + }) + disposable += RxBus + .toObservable(EventTempBasalChange::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + updateGui() + }, { + FabricPrivacy.logException(it) + }) + disposable += RxBus + .toObservable(EventCustomActionsChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + updateGui() + }, { + FabricPrivacy.logException(it) + }) + updateGui() + } + + @Synchronized + override fun onPause() { + super.onPause() + disposable.clear() + } + + @Synchronized + fun updateGui() { + actions_profileswitch?.visibility = + if (ConfigBuilderPlugin.getPlugin().activeProfileInterface?.profile != null) View.VISIBLE + else View.GONE + + if (ProfileFunctions.getInstance().profile == null) { + actions_temptarget?.visibility = View.GONE + actions_extendedbolus?.visibility = View.GONE + actions_extendedbolus_cancel?.visibility = View.GONE + actions_settempbasal?.visibility = View.GONE + actions_canceltempbasal?.visibility = View.GONE + actions_fill?.visibility = View.GONE + return + } + + val pump = ConfigBuilderPlugin.getPlugin().activePump ?: return + val basalProfileEnabled = MainApp.isEngineeringModeOrRelease() && pump.pumpDescription.isSetBasalProfileCapable + + actions_profileswitch?.visibility = if (!basalProfileEnabled || !pump.isInitialized || pump.isSuspended) View.GONE else View.VISIBLE + + if (!pump.pumpDescription.isExtendedBolusCapable || !pump.isInitialized || pump.isSuspended || pump.isFakingTempsByExtendedBoluses || Config.APS) { + actions_extendedbolus?.visibility = View.GONE + actions_extendedbolus_cancel?.visibility = View.GONE + } else { + val activeExtendedBolus = TreatmentsPlugin.getPlugin().getExtendedBolusFromHistory(System.currentTimeMillis()) + if (activeExtendedBolus != null) { + actions_extendedbolus?.visibility = View.GONE + actions_extendedbolus_cancel?.visibility = View.VISIBLE + actions_extendedbolus_cancel?.text = MainApp.gs(R.string.cancel) + " " + activeExtendedBolus.toString() + } else { + actions_extendedbolus?.visibility = View.VISIBLE + actions_extendedbolus_cancel?.visibility = View.GONE + } + } + + if (!pump.pumpDescription.isTempBasalCapable || !pump.isInitialized || pump.isSuspended) { + actions_settempbasal?.visibility = View.GONE + actions_canceltempbasal?.visibility = View.GONE + } else { + val activeTemp = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(System.currentTimeMillis()) + if (activeTemp != null) { + actions_settempbasal?.visibility = View.GONE + actions_canceltempbasal?.visibility = View.VISIBLE + actions_canceltempbasal?.text = MainApp.gs(R.string.cancel) + " " + activeTemp.toStringShort() + } else { + actions_settempbasal?.visibility = View.VISIBLE + actions_canceltempbasal?.visibility = View.GONE + } + } + + actions_fill?.visibility = + if (!pump.pumpDescription.isRefillingCapable || !pump.isInitialized || pump.isSuspended) View.GONE + else View.VISIBLE + + actions_temptarget?.visibility = if (!Config.APS) View.GONE else View.VISIBLE + actions_tddstats?.visibility = if (!pump.pumpDescription.supportsTDDs) View.GONE else View.VISIBLE + checkPumpCustomActions() + } + + private fun checkPumpCustomActions() { + val activePump = ConfigBuilderPlugin.getPlugin().activePump ?: return + val customActions = activePump.customActions ?: return + removePumpCustomActions() + + for (customAction in customActions) { + if (!customAction.isEnabled) continue + + val btn = SingleClickButton(context, null, android.R.attr.buttonStyle) + btn.text = MainApp.gs(customAction.name) + + val layoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f) + layoutParams.setMargins(20, 8, 20, 8) // 10,3,10,3 + + btn.layoutParams = layoutParams + btn.setOnClickListener { v -> + val b = v as SingleClickButton + val action = this.pumpCustomActions[b.text.toString()] + ConfigBuilderPlugin.getPlugin().activePump!!.executeCustomAction(action!!.customActionType) + } + + val top = resources.getDrawable(customAction.iconResourceId) + btn.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null) + + action_buttons_layout?.addView(btn) + + this.pumpCustomActions[MainApp.gs(customAction.name)] = customAction + this.pumpCustomButtons.add(btn) + } + } + + private fun removePumpCustomActions() { + for (customButton in pumpCustomButtons) action_buttons_layout?.removeView(customButton) + pumpCustomButtons.clear() + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsPlugin.kt index 086c75a157..04afb826a2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/ActionsPlugin.kt @@ -7,7 +7,7 @@ import info.nightscout.androidaps.interfaces.PluginType object ActionsPlugin : PluginBase(PluginDescription() .mainType(PluginType.GENERAL) - .fragmentClass(ActionsFragment::class.java.name) + .fragmentClass(ActionsFragment::class.qualifiedName) .pluginName(R.string.actions) .shortName(R.string.actions_shortname) .description(R.string.description_actions)) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationEvent.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationEvent.java index 6eb3be382b..4f7bb62c74 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationEvent.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationEvent.java @@ -10,8 +10,11 @@ import java.util.List; import info.nightscout.androidaps.plugins.general.automation.actions.Action; import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class AutomationEvent { + private static final Logger log = LoggerFactory.getLogger(AutomationEvent.class); private Trigger trigger = new TriggerConnector(); private List actions = new ArrayList<>(); @@ -74,7 +77,7 @@ public class AutomationEvent { } o.put("actions", array); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -91,7 +94,7 @@ public class AutomationEvent { actions.add(Action.instantiate(new JSONObject(array.getString(i)))); } } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.kt index 8b6ffb1720..d1135c21ed 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.kt @@ -29,7 +29,7 @@ class AutomationFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - eventListAdapter = EventListAdapter(AutomationPlugin.automationEvents, fragmentManager) + eventListAdapter = EventListAdapter(AutomationPlugin.automationEvents, fragmentManager, activity) automation_eventListView.layoutManager = LinearLayoutManager(context) automation_eventListView.adapter = eventListAdapter @@ -74,14 +74,11 @@ class AutomationFragment : Fragment() { @Synchronized private fun updateGui() { - if (eventListAdapter == null) return eventListAdapter?.notifyDataSetChanged() val sb = StringBuilder() - for (l in AutomationPlugin.executionLog.reversed()) { - sb.append(l) - sb.append("\n") - } - automation_logView.text = sb.toString() + for (l in AutomationPlugin.executionLog.reversed()) + sb.append(l).append("\n") + automation_logView?.text = sb.toString() } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt index 02f5bf5e87..8b78ae6458 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt @@ -13,6 +13,7 @@ import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.PluginDescription import info.nightscout.androidaps.interfaces.PluginType import info.nightscout.androidaps.logging.L +import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.automation.actions.* import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationDataChanged @@ -160,11 +161,16 @@ object AutomationPlugin : PluginBase(PluginDescription() private fun processActions() { if (!isEnabled(PluginType.GENERAL)) return + if (LoopPlugin.getPlugin().isSuspended) { + if (L.isEnabled(L.AUTOMATION)) + log.debug("Loop deactivated") + return + } if (L.isEnabled(L.AUTOMATION)) log.debug("processActions") for (event in automationEvents) { - if (event.isEnabled() && event.trigger.shouldRun() && event.preconditions.shouldRun()) { + if (event.isEnabled && event.trigger.shouldRun() && event.preconditions.shouldRun()) { val actions = event.actions for (action in actions) { action.doAction(object : Callback() { @@ -221,7 +227,8 @@ object AutomationPlugin : PluginBase(PluginDescription() TriggerWifiSsid(), TriggerLocation(), TriggerAutosensValue(), - TriggerBolusAgo() + TriggerBolusAgo(), + TriggerPumpLastConnection() ) } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/EventListAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/EventListAdapter.java index ec1cac23d6..08079a9110 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/EventListAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/EventListAdapter.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.automation; +import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; @@ -26,14 +27,17 @@ import info.nightscout.androidaps.plugins.general.automation.actions.Action; import info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog; import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationDataChanged; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector; +import info.nightscout.androidaps.utils.OKDialog; class EventListAdapter extends RecyclerView.Adapter { - private final List mEventList; - private final FragmentManager mFragmentManager; + private final List eventList; + private final FragmentManager fragmentManager; + private final Activity activity; - EventListAdapter(List events, FragmentManager fragmentManager) { - this.mEventList = events; - this.mFragmentManager = fragmentManager; + EventListAdapter(List events, FragmentManager fragmentManager, Activity activity) { + this.eventList = events; + this.fragmentManager = fragmentManager; + this.activity = activity; } @NonNull @@ -52,7 +56,7 @@ class EventListAdapter extends RecyclerView.Adapter @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { - final AutomationEvent event = mEventList.get(position); + final AutomationEvent event = eventList.get(position); holder.eventTitle.setText(event.getTitle()); holder.enabled.setChecked(event.isEnabled()); holder.iconLayout.removeAllViews(); @@ -82,16 +86,18 @@ class EventListAdapter extends RecyclerView.Adapter } // enabled event - holder.enabled.setOnCheckedChangeListener((buttonView, isChecked) -> { - event.setEnabled(isChecked); + holder.enabled.setOnClickListener(v -> { + event.setEnabled((holder.enabled.isChecked())); RxBus.INSTANCE.send(new EventAutomationDataChanged()); }); // remove event - holder.iconTrash.setOnClickListener(v -> { - mEventList.remove(event); - RxBus.INSTANCE.send(new EventAutomationDataChanged()); - }); + holder.iconTrash.setOnClickListener(v -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.removerecord) + " " + event.getTitle(), () -> { + eventList.remove(event); + RxBus.INSTANCE.send(new EventAutomationDataChanged()); + }) + ); // edit event holder.rootLayout.setOnClickListener(v -> { @@ -101,14 +107,14 @@ class EventListAdapter extends RecyclerView.Adapter args.putString("event", event.toJSON()); args.putInt("position", position); dialog.setArguments(args); - if (mFragmentManager != null) - dialog.show(mFragmentManager, "EditEventDialog"); + if (fragmentManager != null) + dialog.show(fragmentManager, "EditEventDialog"); }); } @Override public int getItemCount() { - return mEventList.size(); + return eventList.size(); } static class ViewHolder extends RecyclerView.ViewHolder { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.java index 796f023f1d..ee4564fd6f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.java @@ -11,6 +11,8 @@ import javax.annotation.Nullable; import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger; import info.nightscout.androidaps.queue.Callback; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /* Action ideas: @@ -44,6 +46,7 @@ import info.nightscout.androidaps.queue.Callback; public abstract class Action { + private static final Logger log = LoggerFactory.getLogger(Action.class); public Trigger precondition = null; @@ -65,7 +68,7 @@ public abstract class Action { try { o.put("type", this.getClass().getName()); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -84,7 +87,7 @@ public abstract class Action { Class clazz = Class.forName(type); return ((Action) clazz.newInstance()).fromJSON(data != null ? data.toString() : ""); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return null; } @@ -98,7 +101,7 @@ public abstract class Action { fromJSON(data.toString()); } } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspend.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspend.java index bd221d8f81..39adbcac27 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspend.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionLoopSuspend.java @@ -18,8 +18,12 @@ import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithE import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.JsonHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ActionLoopSuspend extends Action { + private static final Logger log = LoggerFactory.getLogger(ActionLoopSuspend.class); + public InputDuration minutes = new InputDuration(0, InputDuration.TimeUnit.MINUTES); @Override @@ -59,7 +63,7 @@ public class ActionLoopSuspend extends Action { o.put("type", this.getClass().getName()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -70,7 +74,7 @@ public class ActionLoopSuspend extends Action { JSONObject o = new JSONObject(data); minutes.setMinutes(JsonHelper.safeGetInt(o, "minutes")); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionNotification.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionNotification.java index 1f7ad543c2..7bd501b460 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionNotification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionNotification.java @@ -20,8 +20,12 @@ import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotifi import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.JsonHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ActionNotification extends Action { + private static final Logger log = LoggerFactory.getLogger(ActionNotification.class); + public InputString text = new InputString(); @Override @@ -59,7 +63,7 @@ public class ActionNotification extends Action { o.put("type", this.getClass().getName()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -70,7 +74,7 @@ public class ActionNotification extends Action { JSONObject o = new JSONObject(data); text.setValue(JsonHelper.safeGetString(o, "text")); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitch.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitch.java index 50fbb786db..8a4762bd08 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitch.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitch.java @@ -92,7 +92,7 @@ public class ActionProfileSwitch extends Action { data.put("profileToSwitchTo", inputProfileName.getValue()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -105,7 +105,7 @@ public class ActionProfileSwitch extends Action { profileName = JsonHelper.safeGetString(d, "profileToSwitchTo"); inputProfileName.setValue(profileName); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java index 20b6abb081..23b54640a5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchPercent.java @@ -19,8 +19,12 @@ import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuil import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerProfilePercent; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.JsonHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ActionProfileSwitchPercent extends Action { + private static final Logger log = LoggerFactory.getLogger(ActionProfileSwitchPercent.class); + InputPercent pct = new InputPercent(); InputDuration duration = new InputDuration(0, InputDuration.TimeUnit.MINUTES); @@ -71,7 +75,7 @@ public class ActionProfileSwitchPercent extends Action { data.put("durationInMinutes", duration.getMinutes()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -83,7 +87,7 @@ public class ActionProfileSwitchPercent extends Action { pct.setValue(JsonHelper.safeGetInt(d, "percentage")); duration.setMinutes(JsonHelper.safeGetInt(d, "durationInMinutes")); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionSendSMS.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionSendSMS.java index f8afafca75..fa9140e5f9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionSendSMS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionSendSMS.java @@ -21,6 +21,7 @@ import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.JsonHelper; public class ActionSendSMS extends Action { + private static final Logger log = LoggerFactory.getLogger(ActionSendSMS.class); public InputString text = new InputString(); @@ -36,7 +37,7 @@ public class ActionSendSMS extends Action { @Override public void doAction(Callback callback) { - boolean result = SmsCommunicatorPlugin.getPlugin().sendNotificationToAllNumbers(text.getValue()); + boolean result = SmsCommunicatorPlugin.INSTANCE.sendNotificationToAllNumbers(text.getValue()); if (callback != null) callback.result(new PumpEnactResult().success(result).comment(result ? R.string.ok : R.string.danar_error)).run(); @@ -56,7 +57,7 @@ public class ActionSendSMS extends Action { o.put("type", this.getClass().getName()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -67,7 +68,7 @@ public class ActionSendSMS extends Action { JSONObject o = new JSONObject(data); text.setValue(JsonHelper.safeGetString(o, "text")); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java index 659bb508eb..b0ef645069 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.java @@ -24,8 +24,12 @@ import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.JsonHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ActionStartTempTarget extends Action { + private static final Logger log = LoggerFactory.getLogger(ActionStartTempTarget.class); + String reason = ""; InputTempTarget value = new InputTempTarget(); InputDuration duration = new InputDuration(0, InputDuration.TimeUnit.MINUTES); @@ -93,7 +97,7 @@ public class ActionStartTempTarget extends Action { data.put("durationInMinutes", duration.getMinutes()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -107,7 +111,7 @@ public class ActionStartTempTarget extends Action { value.setValue(JsonHelper.safeGetDouble(d, "value")); duration.setMinutes(JsonHelper.safeGetInt(d, "durationInMinutes")); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTarget.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTarget.java index de251c499e..abf354bc9c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTarget.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStopTempTarget.java @@ -14,8 +14,12 @@ import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.JsonHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ActionStopTempTarget extends Action { + private static final Logger log = LoggerFactory.getLogger(ActionStopTempTarget.class); + String reason = ""; private TempTarget tempTarget; @@ -54,7 +58,7 @@ public class ActionStopTempTarget extends Action { data.put("reason", reason); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -65,7 +69,7 @@ public class ActionStopTempTarget extends Action { JSONObject d = new JSONObject(data); reason = JsonHelper.safeGetString(d, "reason"); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java index f6ba6b6f67..69ac67bf07 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java @@ -13,10 +13,13 @@ import com.google.common.base.Optional; import org.json.JSONException; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.annotation.Nullable; public abstract class Trigger { + private static final Logger log = LoggerFactory.getLogger(Trigger.class); TriggerConnector connector = null; long lastRun; @@ -56,7 +59,7 @@ public abstract class Trigger { try { return instantiate(new JSONObject(json)); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return null; } @@ -69,7 +72,7 @@ public abstract class Trigger { Class clazz = Class.forName(type); return ((Trigger) clazz.newInstance()).fromJSON(data.toString()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return null; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.java index da850b187d..ce0a8f63bb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerAutosensValue.java @@ -89,7 +89,7 @@ public class TriggerAutosensValue extends Trigger { data.put("comparator", comparator.getValue().toString()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -102,7 +102,7 @@ public class TriggerAutosensValue extends Trigger { lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java index 1636377bf2..3733859b96 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java @@ -104,7 +104,7 @@ public class TriggerBg extends Trigger { data.put("units", bg.getUnits()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -118,7 +118,7 @@ public class TriggerBg extends Trigger { lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.java index 0ad9f75ab2..0a038112e2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBolusAgo.java @@ -86,7 +86,7 @@ public class TriggerBolusAgo extends Trigger { data.put("comparator", comparator.getValue().toString()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -99,7 +99,7 @@ public class TriggerBolusAgo extends Trigger { lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOB.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOB.java index 4ddeb4299a..bf17dc72d1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOB.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerCOB.java @@ -87,7 +87,7 @@ public class TriggerCOB extends Trigger { data.put("comparator", comparator.getValue().toString()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -100,7 +100,7 @@ public class TriggerCOB extends Trigger { lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java index ae4bd35ad5..549925528a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java @@ -162,7 +162,7 @@ public class TriggerConnector extends Trigger { data.put("triggerList", array); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -179,7 +179,7 @@ public class TriggerConnector extends Trigger { add(newItem); } } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java index a3939a60b7..eee649307b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerDelta.java @@ -129,7 +129,7 @@ public class TriggerDelta extends Trigger { data.put("comparator", comparator.getValue().toString()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -144,7 +144,7 @@ public class TriggerDelta extends Trigger { lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.java index c4003af861..a3935d7cec 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerIob.java @@ -82,7 +82,7 @@ public class TriggerIob extends Trigger { data.put("comparator", comparator.getValue().toString()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -95,7 +95,7 @@ public class TriggerIob extends Trigger { lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocation.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocation.java index 4772d29112..19965cfc9a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocation.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocation.java @@ -93,7 +93,7 @@ public class TriggerLocation extends Trigger { data.put("lastRun", lastRun); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -108,7 +108,7 @@ public class TriggerLocation extends Trigger { name.setValue(JsonHelper.safeGetString(d, "name")); lastRun = JsonHelper.safeGetLong(d, "lastRun"); } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.java index d86bc35cb8..e4c0ae4346 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerProfilePercent.java @@ -88,7 +88,7 @@ public class TriggerProfilePercent extends Trigger { data.put("comparator", comparator.getValue().toString()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -101,7 +101,7 @@ public class TriggerProfilePercent extends Trigger { lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnection.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnection.java new file mode 100644 index 0000000000..ff8bb335f9 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerPumpLastConnection.java @@ -0,0 +1,146 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers; + +import android.widget.LinearLayout; + +import androidx.fragment.app.FragmentManager; + +import com.google.common.base.Optional; + +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; +import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration; +import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; +import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; +import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel; +import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.JsonHelper; +import info.nightscout.androidaps.utils.T; + +public class TriggerPumpLastConnection extends Trigger { + private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); + private InputDuration minutesAgo = new InputDuration(0, InputDuration.TimeUnit.MINUTES); + private Comparator comparator = new Comparator(); + + public TriggerPumpLastConnection() { + super(); + } + + private TriggerPumpLastConnection(TriggerPumpLastConnection triggerPumpLastConnection) { + super(); + minutesAgo = new InputDuration(triggerPumpLastConnection.minutesAgo); + lastRun = triggerPumpLastConnection.lastRun; + comparator = new Comparator(triggerPumpLastConnection.comparator); + } + + public double getValue() { + return minutesAgo.getValue(); + } + + public Comparator getComparator() { + return comparator; + } + + @Override + public synchronized boolean shouldRun() { + + if (lastRun > DateUtil.now() - T.mins(5).msecs()) + return false; + long lastConnection = ConfigBuilderPlugin.getPlugin().getActivePump().lastDataTime(); + + if (lastConnection == 0 && comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) + return true; + + double minutesAgo = (double) (DateUtil.now() - lastConnection) / (60 * 1000); + if (L.isEnabled(L.AUTOMATION)) + log.debug("Last connection min ago: " + minutesAgo); + + boolean doRun = comparator.getValue().check((minutesAgo), getValue()); + if (doRun) { + if (L.isEnabled(L.AUTOMATION)) + log.debug("Ready for execution: " + friendlyDescription()); + return true; + } + return false; + } + + @Override + public synchronized String toJSON() { + JSONObject o = new JSONObject(); + try { + o.put("type", TriggerPumpLastConnection.class.getName()); + JSONObject data = new JSONObject(); + data.put("minutesAgo", getValue()); + data.put("lastRun", lastRun); + data.put("comparator", comparator.getValue().toString()); + o.put("data", data); + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + return o.toString(); + } + + @Override + Trigger fromJSON(String data) { + try { + JSONObject d = new JSONObject(data); + minutesAgo.setMinutes(JsonHelper.safeGetInt(d, "minutesAgo")); + lastRun = JsonHelper.safeGetLong(d, "lastRun"); + comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); + } catch (Exception e) { + log.error("Unhandled exception", e); + } + return this; + } + + @Override + public int friendlyName() { + return R.string.automation_trigger_pump_last_connection_label; + } + + @Override + public String friendlyDescription() { + return MainApp.gs(R.string.automation_trigger_pump_last_connection_compared, MainApp.gs(comparator.getValue().getStringRes()), (int) getValue()); + } + + @Override + public Optional icon() { + return Optional.of(R.drawable.remove); + } + + @Override + public Trigger duplicate() { + return new TriggerPumpLastConnection(this); + } + + TriggerPumpLastConnection setValue(int requestedValue) { + this.minutesAgo.setMinutes(requestedValue); + return this; + } + + TriggerPumpLastConnection lastRun(long lastRun) { + this.lastRun = lastRun; + return this; + } + + TriggerPumpLastConnection comparator(Comparator.Compare compare) { + this.comparator = new Comparator().setValue(compare); + return this; + } + + @Override + public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { + new LayoutBuilder() + .add(new StaticLabel(R.string.automation_trigger_pump_last_connection_label)) + .add(comparator) + .add(new LabelWithElement(MainApp.gs(R.string.automation_trigger_pump_last_connection_description) + ": ", "", minutesAgo)) + .build(root); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.java index a145253b36..89c8d17085 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.java @@ -163,7 +163,7 @@ public class TriggerRecurringTime extends Trigger { object.put("type", TriggerRecurringTime.class.getName()); object.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return object.toString(); } @@ -181,7 +181,7 @@ public class TriggerRecurringTime extends Trigger { minute = JsonHelper.safeGetInt(o, "minute"); validTo = JsonHelper.safeGetLong(o, "validTo"); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.java index 378652ed2a..f45efdf4a4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTarget.java @@ -74,7 +74,7 @@ public class TriggerTempTarget extends Trigger { data.put("comparator", comparator.getValue().toString()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -86,7 +86,7 @@ public class TriggerTempTarget extends Trigger { lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(ComparatorExists.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java index 261783b99c..08f9a65aa7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java @@ -65,7 +65,7 @@ public class TriggerTime extends Trigger { object.put("type", TriggerTime.class.getName()); object.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return object.toString(); } @@ -78,7 +78,7 @@ public class TriggerTime extends Trigger { lastRun = JsonHelper.safeGetLong(o, "lastRun"); runAt = JsonHelper.safeGetLong(o, "runAt"); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.java index 8755c3559c..1d03871620 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.java @@ -93,7 +93,7 @@ public class TriggerTimeRange extends Trigger { object.put("type", TriggerTimeRange.class.getName()); object.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } log.debug(object.toString()); return object.toString(); @@ -108,7 +108,7 @@ public class TriggerTimeRange extends Trigger { start = JsonHelper.safeGetInt(o, "start"); end = JsonHelper.safeGetInt(o, "end"); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java index 84094095d5..dd2c34bcb8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java @@ -59,13 +59,13 @@ public class TriggerWifiSsid extends Trigger { if (lastRun > DateUtil.now() - T.mins(5).msecs()) return false; - if (!eventNetworkChange.wifiConnected && comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) { + if (!eventNetworkChange.getWifiConnected() && comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); return true; } - boolean doRun = eventNetworkChange.wifiConnected && comparator.getValue().check(eventNetworkChange.getSsid(), getValue()); + boolean doRun = eventNetworkChange.getWifiConnected() && comparator.getValue().check(eventNetworkChange.connectedSsid(), getValue()); if (doRun) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); @@ -85,7 +85,7 @@ public class TriggerWifiSsid extends Trigger { data.put("comparator", comparator.getValue().toString()); o.put("data", data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return o.toString(); } @@ -98,7 +98,7 @@ public class TriggerWifiSsid extends Trigger { lastRun = JsonHelper.safeGetLong(d, "lastRun"); comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator"))); } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return this; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalFragment.java index c7f6e1bf64..42ae7fd6fc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalFragment.java @@ -4,14 +4,14 @@ package info.nightscout.androidaps.plugins.general.careportal; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; -import androidx.fragment.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,19 +22,18 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.events.EventCareportalEventChange; -import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus; import info.nightscout.androidaps.plugins.general.overview.OverviewFragment; -import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.FabricPrivacy; -import info.nightscout.androidaps.utils.SP; -import info.nightscout.androidaps.utils.SetWarnColor; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; -public class CareportalFragment extends SubscriberFragment implements View.OnClickListener { +public class CareportalFragment extends Fragment implements View.OnClickListener { private static Logger log = LoggerFactory.getLogger(CareportalFragment.class); + private CompositeDisposable disposable = new CompositeDisposable(); TextView iage; TextView cage; @@ -71,59 +70,69 @@ public class CareportalFragment extends SubscriberFragment implements View.OnCli @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - try { - View view = inflater.inflate(R.layout.careportal_fragment, container, false); + View view = inflater.inflate(R.layout.careportal_fragment, container, false); - view.findViewById(R.id.careportal_bgcheck).setOnClickListener(this); - view.findViewById(R.id.careportal_announcement).setOnClickListener(this); - view.findViewById(R.id.careportal_cgmsensorinsert).setOnClickListener(this); - view.findViewById(R.id.careportal_cgmsensorstart).setOnClickListener(this); - view.findViewById(R.id.careportal_combobolus).setOnClickListener(this); - view.findViewById(R.id.careportal_correctionbolus).setOnClickListener(this); - view.findViewById(R.id.careportal_carbscorrection).setOnClickListener(this); - view.findViewById(R.id.careportal_exercise).setOnClickListener(this); - view.findViewById(R.id.careportal_insulincartridgechange).setOnClickListener(this); - view.findViewById(R.id.careportal_pumpbatterychange).setOnClickListener(this); - view.findViewById(R.id.careportal_mealbolus).setOnClickListener(this); - view.findViewById(R.id.careportal_note).setOnClickListener(this); - view.findViewById(R.id.careportal_profileswitch).setOnClickListener(this); - view.findViewById(R.id.careportal_pumpsitechange).setOnClickListener(this); - view.findViewById(R.id.careportal_question).setOnClickListener(this); - view.findViewById(R.id.careportal_snackbolus).setOnClickListener(this); - view.findViewById(R.id.careportal_tempbasalend).setOnClickListener(this); - view.findViewById(R.id.careportal_tempbasalstart).setOnClickListener(this); - view.findViewById(R.id.careportal_openapsoffline).setOnClickListener(this); - view.findViewById(R.id.careportal_temporarytarget).setOnClickListener(this); + view.findViewById(R.id.careportal_bgcheck).setOnClickListener(this); + view.findViewById(R.id.careportal_announcement).setOnClickListener(this); + view.findViewById(R.id.careportal_cgmsensorinsert).setOnClickListener(this); + view.findViewById(R.id.careportal_cgmsensorstart).setOnClickListener(this); + view.findViewById(R.id.careportal_combobolus).setOnClickListener(this); + view.findViewById(R.id.careportal_correctionbolus).setOnClickListener(this); + view.findViewById(R.id.careportal_carbscorrection).setOnClickListener(this); + view.findViewById(R.id.careportal_exercise).setOnClickListener(this); + view.findViewById(R.id.careportal_insulincartridgechange).setOnClickListener(this); + view.findViewById(R.id.careportal_pumpbatterychange).setOnClickListener(this); + view.findViewById(R.id.careportal_mealbolus).setOnClickListener(this); + view.findViewById(R.id.careportal_note).setOnClickListener(this); + view.findViewById(R.id.careportal_profileswitch).setOnClickListener(this); + view.findViewById(R.id.careportal_pumpsitechange).setOnClickListener(this); + view.findViewById(R.id.careportal_question).setOnClickListener(this); + view.findViewById(R.id.careportal_snackbolus).setOnClickListener(this); + view.findViewById(R.id.careportal_tempbasalend).setOnClickListener(this); + view.findViewById(R.id.careportal_tempbasalstart).setOnClickListener(this); + view.findViewById(R.id.careportal_openapsoffline).setOnClickListener(this); + view.findViewById(R.id.careportal_temporarytarget).setOnClickListener(this); - iage = view.findViewById(R.id.careportal_insulinage); - cage = view.findViewById(R.id.careportal_canulaage); - sage = view.findViewById(R.id.careportal_sensorage); - pbage = view.findViewById(R.id.careportal_pbage); + iage = view.findViewById(R.id.careportal_insulinage); + cage = view.findViewById(R.id.careportal_canulaage); + sage = view.findViewById(R.id.careportal_sensorage); + pbage = view.findViewById(R.id.careportal_pbage); - statsLayout = view.findViewById(R.id.careportal_stats); + statsLayout = view.findViewById(R.id.careportal_stats); - noProfileView = view.findViewById(R.id.profileview_noprofile); - butonsLayout = view.findViewById(R.id.careportal_buttons); + noProfileView = view.findViewById(R.id.profileview_noprofile); + butonsLayout = view.findViewById(R.id.careportal_buttons); - ProfileStore profileStore = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null ? ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getProfile() : null; - if (profileStore == null) { - noProfileView.setVisibility(View.VISIBLE); - butonsLayout.setVisibility(View.GONE); - } else { - noProfileView.setVisibility(View.GONE); - butonsLayout.setVisibility(View.VISIBLE); - } - - if (Config.NSCLIENT) - statsLayout.setVisibility(View.GONE); // visible on overview - - updateGUI(); - return view; - } catch (Exception e) { - FabricPrivacy.logException(e); + ProfileStore profileStore = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null ? ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getProfile() : null; + if (profileStore == null) { + noProfileView.setVisibility(View.VISIBLE); + butonsLayout.setVisibility(View.GONE); + } else { + noProfileView.setVisibility(View.GONE); + butonsLayout.setVisibility(View.VISIBLE); } - return null; + if (Config.NSCLIENT) + statsLayout.setVisibility(View.GONE); // visible on overview + + return view; + } + + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventCareportalEventChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI(), FabricPrivacy::logException) + ); + updateGUI(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); } @Override @@ -203,12 +212,6 @@ public class CareportalFragment extends SubscriberFragment implements View.OnCli newDialog.show(manager, "NewNSTreatmentDialog"); } - @Subscribe - public void onStatusEvent(final EventCareportalEventChange c) { - updateGUI(); - } - - @Override protected void updateGUI() { Activity activity = getActivity(); updateAge(activity, sage, iage, cage, pbage); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.java index 66727f0f7f..997c6b0ba9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.java @@ -1,12 +1,8 @@ package info.nightscout.androidaps.plugins.general.food; -import android.app.Activity; import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; -import androidx.appcompat.app.AlertDialog; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; @@ -18,7 +14,10 @@ import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,17 +30,20 @@ import java.util.Set; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventFoodDatabaseChanged; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; -import info.nightscout.androidaps.utils.FabricPrivacy; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SpinnerHelper; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; /** * Created by mike on 16.10.2017. */ -public class FoodFragment extends SubscriberFragment { +public class FoodFragment extends Fragment { private static Logger log = LoggerFactory.getLogger(FoodFragment.class); + private CompositeDisposable disposable = new CompositeDisposable(); EditText filter; ImageView clearFilter; @@ -59,93 +61,96 @@ public class FoodFragment extends SubscriberFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - try { - View view = inflater.inflate(R.layout.food_fragment, container, false); - filter = (EditText) view.findViewById(R.id.food_filter); - clearFilter = (ImageView) view.findViewById(R.id.food_clearfilter); - category = new SpinnerHelper(view.findViewById(R.id.food_category)); - subcategory = new SpinnerHelper(view.findViewById(R.id.food_subcategory)); - recyclerView = (RecyclerView) view.findViewById(R.id.food_recyclerview); - recyclerView.setHasFixedSize(true); - LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); - recyclerView.setLayoutManager(llm); + View view = inflater.inflate(R.layout.food_fragment, container, false); + filter = (EditText) view.findViewById(R.id.food_filter); + clearFilter = (ImageView) view.findViewById(R.id.food_clearfilter); + category = new SpinnerHelper(view.findViewById(R.id.food_category)); + subcategory = new SpinnerHelper(view.findViewById(R.id.food_subcategory)); + recyclerView = (RecyclerView) view.findViewById(R.id.food_recyclerview); + recyclerView.setHasFixedSize(true); + LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); + recyclerView.setLayoutManager(llm); - clearFilter.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - filter.setText(""); - category.setSelection(0); - subcategory.setSelection(0); - filterData(); - } - }); + clearFilter.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + filter.setText(""); + category.setSelection(0); + subcategory.setSelection(0); + filterData(); + } + }); - category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - fillSubcategories(); - filterData(); - } + category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + fillSubcategories(); + filterData(); + } - @Override - public void onNothingSelected(AdapterView parent) { - fillSubcategories(); - filterData(); - } - }); + @Override + public void onNothingSelected(AdapterView parent) { + fillSubcategories(); + filterData(); + } + }); - subcategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - filterData(); - } + subcategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + filterData(); + } - @Override - public void onNothingSelected(AdapterView parent) { - filterData(); - } - }); + @Override + public void onNothingSelected(AdapterView parent) { + filterData(); + } + }); - filter.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } + filter.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - filterData(); - } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + filterData(); + } - @Override - public void afterTextChanged(Editable s) { - } - }); + @Override + public void afterTextChanged(Editable s) { + } + }); - RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp - .getSpecificPlugin(FoodPlugin.class).getService().getFoodData()); - recyclerView.setAdapter(adapter); + RecyclerViewAdapter adapter = new RecyclerViewAdapter(FoodPlugin.getPlugin().getService().getFoodData()); + recyclerView.setAdapter(adapter); - loadData(); - fillCategories(); - fillSubcategories(); - filterData(); - return view; - } catch (Exception e) { - FabricPrivacy.logException(e); - } - - return null; + loadData(); + fillCategories(); + fillSubcategories(); + filterData(); + return view; } - @Subscribe - @SuppressWarnings("unused") - public void onStatusEvent(final EventFoodDatabaseChanged ev) { - loadData(); - filterData(); + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventFoodDatabaseChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + updateGui(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); } void loadData() { - unfiltered = MainApp.getSpecificPlugin(FoodPlugin.class).getService().getFoodData(); + unfiltered = FoodPlugin.getPlugin().getService().getFoodData(); } void fillCategories() { @@ -207,19 +212,11 @@ public class FoodFragment extends SubscriberFragment { filtered.add(f); } - updateGUI(); + updateGui(); } - @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - recyclerView.swapAdapter(new FoodFragment.RecyclerViewAdapter(filtered), true); - } - }); + protected void updateGui() { + recyclerView.swapAdapter(new FoodFragment.RecyclerViewAdapter(filtered), true); } public class RecyclerViewAdapter extends RecyclerView.Adapter { @@ -299,7 +296,7 @@ public class FoodFragment extends SubscriberFragment { if (_id != null && !_id.equals("")) { NSUpload.removeFoodFromNS(_id); } - MainApp.getSpecificPlugin(FoodPlugin.class).getService().delete(food); + FoodPlugin.getPlugin().getService().delete(food); } }); builder.setNegativeButton(MainApp.gs(R.string.cancel), null); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodService.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodService.java index 3856ecfc22..065c74eabb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodService.java @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.general.food; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; + import androidx.annotation.Nullable; import com.j256.ormlite.android.apptools.OpenHelperManager; @@ -11,7 +12,6 @@ import com.j256.ormlite.dao.Dao; import com.j256.ormlite.dao.DaoManager; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; -import com.squareup.otto.Subscribe; import org.json.JSONArray; import org.json.JSONException; @@ -34,6 +34,10 @@ import info.nightscout.androidaps.events.Event; import info.nightscout.androidaps.events.EventFoodDatabaseChanged; import info.nightscout.androidaps.events.EventNsFood; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; +import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Created by mike on 24.09.2017. @@ -41,6 +45,7 @@ import info.nightscout.androidaps.logging.L; public class FoodService extends OrmLiteBaseService { private Logger log = LoggerFactory.getLogger(L.DATAFOOD); + private CompositeDisposable disposable = new CompositeDisposable(); private static final ScheduledExecutorService foodEventWorker = Executors.newSingleThreadScheduledExecutor(); private static ScheduledFuture scheduledFoodEventPost = null; @@ -48,7 +53,34 @@ public class FoodService extends OrmLiteBaseService { public FoodService() { onCreate(); dbInitialize(); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventNsFood.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + int mode = event.getMode(); + Bundle payload = event.getPayload(); + + try { + if (payload.containsKey("food")) { + JSONObject json = new JSONObject(payload.getString("food")); + if (mode == EventNsFood.Companion.getADD() || mode == EventNsFood.Companion.getUPDATE()) + this.createFoodFromJsonIfNotExists(json); + else + this.deleteNS(json); + } + + if (payload.containsKey("foods")) { + JSONArray array = new JSONArray(payload.getString("foods")); + if (mode == EventNsFood.Companion.getADD() || mode == EventNsFood.Companion.getUPDATE()) + this.createFoodFromJsonIfNotExists(array); + else + this.deleteNS(array); + } + } catch (JSONException e) { + log.error("Unhandled Exception", e); + } + }, FabricPrivacy::logException) + ); } /** @@ -79,34 +111,6 @@ public class FoodService extends OrmLiteBaseService { return null; } - @Subscribe - public void handleNsEvent(EventNsFood event) { - int mode = event.getMode(); - Bundle payload = event.getPayload(); - - try { - if (payload.containsKey("food")) { - JSONObject json = new JSONObject(payload.getString("food")); - if (mode == EventNsFood.ADD || mode == EventNsFood.UPDATE) { - this.createFoodFromJsonIfNotExists(json); - } else { - this.deleteNS(json); - } - } - - if (payload.containsKey("foods")) { - JSONArray array = new JSONArray(payload.getString("foods")); - if (mode == EventNsFood.ADD || mode == EventNsFood.UPDATE) { - this.createFoodFromJsonIfNotExists(array); - } else { - this.deleteNS(array); - } - } - } catch (JSONException e) { - log.error("Unhandled Exception", e); - } - } - @Override public void onCreate() { super.onCreate(); @@ -162,7 +166,7 @@ public class FoodService extends OrmLiteBaseService { public void run() { if (L.isEnabled(L.DATAFOOD)) log.debug("Firing EventFoodChange"); - MainApp.bus().post(event); + RxBus.INSTANCE.send(event); callback.setPost(null); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.java index ea56115347..7a801a44c0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefs.java @@ -28,6 +28,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.utils.OKDialog; import info.nightscout.androidaps.utils.ToastUtils; @@ -136,7 +137,7 @@ public class ImportExportPrefs { OKDialog.show(context, MainApp.gs(R.string.setting_imported), MainApp.gs(R.string.restartingapp), () -> { log.debug("Exiting"); MainApp.instance().stopKeepAliveService(); - MainApp.bus().post(new EventAppExit()); + RxBus.INSTANCE.send(new EventAppExit()); MainApp.closeDbHelper(); if (context instanceof Activity) { ((Activity)context).finish(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientFragment.java index 50674c5a94..112eb9c951 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientFragment.java @@ -1,7 +1,6 @@ package info.nightscout.androidaps.plugins.general.nsclient; -import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; @@ -17,18 +16,22 @@ import android.widget.CompoundButton; import android.widget.ScrollView; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.fragment.app.Fragment; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientNewLog; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientUpdateGUI; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; + +public class NSClientFragment extends Fragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener { + private CompositeDisposable disposable = new CompositeDisposable(); -public class NSClientFragment extends SubscriberFragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener { private TextView logTextView; private TextView queueTextView; private TextView urlTextView; @@ -45,51 +48,61 @@ public class NSClientFragment extends SubscriberFragment implements View.OnClick @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - try { - View view = inflater.inflate(R.layout.nsclientinternal_fragment, container, false); + View view = inflater.inflate(R.layout.nsclientinternal_fragment, container, false); - logScrollview = (ScrollView) view.findViewById(R.id.nsclientinternal_logscrollview); - autoscrollCheckbox = (CheckBox) view.findViewById(R.id.nsclientinternal_autoscroll); - autoscrollCheckbox.setChecked(NSClientPlugin.getPlugin().autoscroll); - autoscrollCheckbox.setOnCheckedChangeListener(this); - pausedCheckbox = (CheckBox) view.findViewById(R.id.nsclientinternal_paused); - pausedCheckbox.setChecked(NSClientPlugin.getPlugin().paused); - pausedCheckbox.setOnCheckedChangeListener(this); - logTextView = (TextView) view.findViewById(R.id.nsclientinternal_log); - queueTextView = (TextView) view.findViewById(R.id.nsclientinternal_queue); - urlTextView = (TextView) view.findViewById(R.id.nsclientinternal_url); - statusTextView = (TextView) view.findViewById(R.id.nsclientinternal_status); + logScrollview = (ScrollView) view.findViewById(R.id.nsclientinternal_logscrollview); + autoscrollCheckbox = (CheckBox) view.findViewById(R.id.nsclientinternal_autoscroll); + autoscrollCheckbox.setChecked(NSClientPlugin.getPlugin().autoscroll); + autoscrollCheckbox.setOnCheckedChangeListener(this); + pausedCheckbox = (CheckBox) view.findViewById(R.id.nsclientinternal_paused); + pausedCheckbox.setChecked(NSClientPlugin.getPlugin().paused); + pausedCheckbox.setOnCheckedChangeListener(this); + logTextView = (TextView) view.findViewById(R.id.nsclientinternal_log); + queueTextView = (TextView) view.findViewById(R.id.nsclientinternal_queue); + urlTextView = (TextView) view.findViewById(R.id.nsclientinternal_url); + statusTextView = (TextView) view.findViewById(R.id.nsclientinternal_status); - clearlog = (TextView) view.findViewById(R.id.nsclientinternal_clearlog); - clearlog.setOnClickListener(this); - clearlog.setPaintFlags(clearlog.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); - restart = (TextView) view.findViewById(R.id.nsclientinternal_restart); - restart.setOnClickListener(this); - restart.setPaintFlags(restart.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); - delivernow = (TextView) view.findViewById(R.id.nsclientinternal_delivernow); - delivernow.setOnClickListener(this); - delivernow.setPaintFlags(delivernow.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); - clearqueue = (TextView) view.findViewById(R.id.nsclientinternal_clearqueue); - clearqueue.setOnClickListener(this); - clearqueue.setPaintFlags(clearqueue.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); - showqueue = (TextView) view.findViewById(R.id.nsclientinternal_showqueue); - showqueue.setOnClickListener(this); - showqueue.setPaintFlags(showqueue.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + clearlog = (TextView) view.findViewById(R.id.nsclientinternal_clearlog); + clearlog.setOnClickListener(this); + clearlog.setPaintFlags(clearlog.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + restart = (TextView) view.findViewById(R.id.nsclientinternal_restart); + restart.setOnClickListener(this); + restart.setPaintFlags(restart.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + delivernow = (TextView) view.findViewById(R.id.nsclientinternal_delivernow); + delivernow.setOnClickListener(this); + delivernow.setPaintFlags(delivernow.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + clearqueue = (TextView) view.findViewById(R.id.nsclientinternal_clearqueue); + clearqueue.setOnClickListener(this); + clearqueue.setPaintFlags(clearqueue.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + showqueue = (TextView) view.findViewById(R.id.nsclientinternal_showqueue); + showqueue.setOnClickListener(this); + showqueue.setPaintFlags(showqueue.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); - updateGUI(); - return view; - } catch (Exception e) { - FabricPrivacy.logException(e); - } + return view; + } - return null; + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventNSClientUpdateGUI.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + updateGui(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.nsclientinternal_restart: - MainApp.bus().post(new EventNSClientRestart()); + RxBus.INSTANCE.send(new EventNSClientRestart()); FabricPrivacy.getInstance().logCustom("NSClientRestart"); break; case R.id.nsclientinternal_delivernow: @@ -108,7 +121,7 @@ public class NSClientFragment extends SubscriberFragment implements View.OnClick builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { UploadQueue.clearQueue(); - updateGUI(); + updateGui(); FabricPrivacy.getInstance().logCustom("NSClientClearQueue"); } }); @@ -116,7 +129,7 @@ public class NSClientFragment extends SubscriberFragment implements View.OnClick builder.show(); break; case R.id.nsclientinternal_showqueue: - MainApp.bus().post(new EventNSClientNewLog("QUEUE", NSClientPlugin.getPlugin().queue().textList())); + RxBus.INSTANCE.send(new EventNSClientNewLog("QUEUE", NSClientPlugin.getPlugin().queue().textList())); break; } } @@ -126,38 +139,28 @@ public class NSClientFragment extends SubscriberFragment implements View.OnClick switch (buttonView.getId()) { case R.id.nsclientinternal_paused: NSClientPlugin.getPlugin().pause(isChecked); - updateGUI(); + updateGui(); FabricPrivacy.getInstance().logCustom("NSClientPause"); break; case R.id.nsclientinternal_autoscroll: SP.putBoolean(R.string.key_nsclientinternal_autoscroll, isChecked); NSClientPlugin.getPlugin().autoscroll = isChecked; - updateGUI(); + updateGui(); break; } } - @Subscribe - public void onStatusEvent(final EventNSClientUpdateGUI ev) { - updateGUI(); - } - - @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - NSClientPlugin.getPlugin().updateLog(); - pausedCheckbox.setChecked(SP.getBoolean(R.string.key_nsclientinternal_paused, false)); - logTextView.setText(NSClientPlugin.getPlugin().textLog); - if (NSClientPlugin.getPlugin().autoscroll) { - logScrollview.fullScroll(ScrollView.FOCUS_DOWN); - } - urlTextView.setText(NSClientPlugin.getPlugin().url()); - Spanned queuetext = Html.fromHtml(MainApp.gs(R.string.queue) + " " + UploadQueue.size() + ""); - queueTextView.setText(queuetext); - statusTextView.setText(NSClientPlugin.getPlugin().status); - }); + protected void updateGui() { + NSClientPlugin.getPlugin().updateLog(); + pausedCheckbox.setChecked(SP.getBoolean(R.string.key_nsclientinternal_paused, false)); + logTextView.setText(NSClientPlugin.getPlugin().textLog); + if (NSClientPlugin.getPlugin().autoscroll) { + logScrollview.fullScroll(ScrollView.FOCUS_DOWN); + } + urlTextView.setText(NSClientPlugin.getPlugin().url()); + Spanned queuetext = Html.fromHtml(MainApp.gs(R.string.queue) + " " + UploadQueue.size() + ""); + queueTextView.setText(queuetext); + statusTextView.setText(NSClientPlugin.getPlugin().status); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientPlugin.java index 4b6072ee1e..037f91b3f0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientPlugin.java @@ -7,11 +7,12 @@ import android.content.ServiceConnection; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; +import android.preference.PreferenceFragment; +import android.preference.PreferenceScreen; import android.text.Html; import android.text.Spanned; -import com.squareup.otto.Subscribe; - +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,7 +23,6 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventChargingState; import info.nightscout.androidaps.events.EventNetworkChange; @@ -31,15 +31,20 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientNewLog; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientUpdateGUI; import info.nightscout.androidaps.plugins.general.nsclient.services.NSClientService; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.ToastUtils; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; public class NSClientPlugin extends PluginBase { private Logger log = LoggerFactory.getLogger(L.NSCLIENT); + private CompositeDisposable disposable = new CompositeDisposable(); static NSClientPlugin nsClientPlugin; @@ -87,7 +92,7 @@ public class NSClientPlugin extends PluginBase { } nsClientReceiverDelegate = - new NsClientReceiverDelegate(MainApp.instance().getApplicationContext(), MainApp.bus()); + new NsClientReceiverDelegate(); } public boolean isAllowed() { @@ -97,41 +102,79 @@ public class NSClientPlugin extends PluginBase { @Override protected void onStart() { - MainApp.bus().register(this); Context context = MainApp.instance().getApplicationContext(); Intent intent = new Intent(context, NSClientService.class); context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); super.onStart(); - nsClientReceiverDelegate.registerReceivers(); + nsClientReceiverDelegate.grabReceiversState(); + disposable.add(RxBus.INSTANCE + .toObservable(EventNSClientStatus.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + status = event.getStatus(); + RxBus.INSTANCE.send(new EventNSClientUpdateGUI()); + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventNetworkChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> nsClientReceiverDelegate.onStatusEvent(event), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> nsClientReceiverDelegate.onStatusEvent(event), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (nsClientService != null) { + MainApp.instance().getApplicationContext().unbindService(mConnection); + } + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventNSClientNewLog.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + addToLog(event); + if (L.isEnabled(L.NSCLIENT)) + log.debug(event.getAction() + " " + event.getLogText()); + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventChargingState.class) + .observeOn(Schedulers.io()) + .subscribe(event -> nsClientReceiverDelegate.onStatusEvent(event), FabricPrivacy::logException) + ); } @Override protected void onStop() { - MainApp.bus().unregister(this); - Context context = MainApp.instance().getApplicationContext(); - context.unbindService(mConnection); - - nsClientReceiverDelegate.unregisterReceivers(); + MainApp.instance().getApplicationContext().unbindService(mConnection); + disposable.clear(); super.onStop(); } - @Subscribe - public void onStatusEvent(EventPreferenceChange ev) { - nsClientReceiverDelegate.onStatusEvent(ev); - } + @Override + public void preprocessPreferences(@NotNull PreferenceFragment preferenceFragment) { + super.preprocessPreferences(preferenceFragment); - @Subscribe - public void onStatusEvent(final EventChargingState ev) { - nsClientReceiverDelegate.onStatusEvent(ev); + if (Config.NSCLIENT) { + PreferenceScreen scrnAdvancedSettings = (PreferenceScreen) preferenceFragment.findPreference(MainApp.gs(R.string.key_advancedsettings)); + if (scrnAdvancedSettings != null) { + scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_statuslights_res_warning))); + scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_statuslights_res_critical))); + scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_statuslights_bat_warning))); + scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_statuslights_bat_critical))); + scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_show_statuslights))); + scrnAdvancedSettings.removePreference(preferenceFragment.findPreference(MainApp.gs(R.string.key_show_statuslights_extended))); + } + } } - @Subscribe - public void onStatusEvent(final EventNetworkChange ev) { - nsClientReceiverDelegate.onStatusEvent(ev); - } - - private ServiceConnection mConnection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { @@ -149,33 +192,12 @@ public class NSClientPlugin extends PluginBase { } }; - @Subscribe - public void onStatusEvent(final EventAppExit ignored) { - if (nsClientService != null) { - MainApp.instance().getApplicationContext().unbindService(mConnection); - nsClientReceiverDelegate.unregisterReceivers(); - } - } - - @Subscribe - public void onStatusEvent(final EventNSClientNewLog ev) { - addToLog(ev); - if (L.isEnabled(L.NSCLIENT)) - log.debug(ev.action + " " + ev.logText); - } - - @Subscribe - public void onStatusEvent(final EventNSClientStatus ev) { - status = ev.status; - MainApp.bus().post(new EventNSClientUpdateGUI()); - } - synchronized void clearLog() { handler.post(() -> { synchronized (listLog) { listLog.clear(); } - MainApp.bus().post(new EventNSClientUpdateGUI()); + RxBus.INSTANCE.send(new EventNSClientUpdateGUI()); }); } @@ -188,7 +210,7 @@ public class NSClientPlugin extends PluginBase { listLog.remove(0); } } - MainApp.bus().post(new EventNSClientUpdateGUI()); + RxBus.INSTANCE.send(new EventNSClientUpdateGUI()); }); } @@ -214,7 +236,6 @@ public class NSClientPlugin extends PluginBase { public void pause(boolean newState) { SP.putBoolean(R.string.key_nsclientinternal_paused, newState); paused = newState; - MainApp.bus().post(new EventPreferenceChange(R.string.key_nsclientinternal_paused)); RxBus.INSTANCE.send(new EventPreferenceChange(R.string.key_nsclientinternal_paused)); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java index 881593b32a..f5582b71a2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java @@ -594,7 +594,7 @@ public class NSUpload { } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java index 072ad6cd56..fd05b133f5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java @@ -6,74 +6,45 @@ import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.wifi.WifiManager; -import com.squareup.otto.Bus; - import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.events.EventChargingState; import info.nightscout.androidaps.events.EventNetworkChange; import info.nightscout.androidaps.events.EventPreferenceChange; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.receivers.ChargingStateReceiver; import info.nightscout.androidaps.receivers.NetworkChangeReceiver; import info.nightscout.androidaps.utils.SP; class NsClientReceiverDelegate { - private final Context context; - private final Bus bus; - - private NetworkChangeReceiver networkChangeReceiver = new NetworkChangeReceiver(); - private ChargingStateReceiver chargingStateReceiver = new ChargingStateReceiver(); - private boolean allowedChargingState = true; private boolean allowedNetworkState = true; boolean allowed = true; - NsClientReceiverDelegate(Context context, Bus bus) { - this.context = context; - this.bus = bus; - } - - void registerReceivers() { + void grabReceiversState() { Context context = MainApp.instance().getApplicationContext(); - // register NetworkChangeReceiver --> https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html - // Nougat is not providing Connectivity-Action anymore ;-( - context.registerReceiver(networkChangeReceiver, - new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); - context.registerReceiver(networkChangeReceiver, - new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)); - EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(context); - if (event != null) - bus.post(event); + EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus(context); + if (event != null) RxBus.INSTANCE.send(event); - context.registerReceiver(chargingStateReceiver, - new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); + EventChargingState eventChargingState = ChargingStateReceiver.grabChargingState(context); + if (eventChargingState != null) RxBus.INSTANCE.send(eventChargingState); - EventChargingState eventChargingState = chargingStateReceiver.grabChargingState(context); - if (eventChargingState != null) - bus.post(eventChargingState); - - } - - void unregisterReceivers() { - context.unregisterReceiver(networkChangeReceiver); - context.unregisterReceiver(chargingStateReceiver); } void onStatusEvent(EventPreferenceChange ev) { if (ev.isChanged(R.string.key_ns_wifionly) || ev.isChanged(R.string.key_ns_wifi_ssids) || ev.isChanged(R.string.key_ns_allowroaming) - ) { - EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext()); + ) { + EventNetworkChange event = NetworkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext()); if (event != null) - bus.post(event); + RxBus.INSTANCE.send(event); } else if (ev.isChanged(R.string.key_ns_chargingonly)) { - EventChargingState event = chargingStateReceiver.grabChargingState(MainApp.instance().getApplicationContext()); + EventChargingState event = ChargingStateReceiver.grabChargingState(MainApp.instance().getApplicationContext()); if (event != null) - bus.post(event); + RxBus.INSTANCE.send(event); } } @@ -95,18 +66,16 @@ class NsClientReceiverDelegate { } } - void processStateChange() { + private void processStateChange() { boolean newAllowedState = allowedChargingState && allowedNetworkState; if (newAllowedState != allowed) { allowed = newAllowedState; - bus.post(new EventPreferenceChange(R.string.key_nsclientinternal_paused)); RxBus.INSTANCE.send(new EventPreferenceChange(R.string.key_nsclientinternal_paused)); } } boolean calculateStatus(final EventChargingState ev) { boolean chargingOnly = SP.getBoolean(R.string.key_ns_chargingonly, false); - boolean newAllowedState = true; if (!ev.isCharging() && chargingOnly) { @@ -123,19 +92,17 @@ class NsClientReceiverDelegate { boolean newAllowedState = true; - if (ev.wifiConnected) { + if (ev.getWifiConnected()) { if (!allowedSSIDs.trim().isEmpty() && - (!allowedSSIDs.contains(ev.getSsid()) && !allowedSSIDs.contains(ev.ssid))) { + (!allowedSSIDs.contains(ev.connectedSsid()) && !allowedSSIDs.contains(ev.getSsid()))) { newAllowedState = false; } } else { - if ((!allowRoaming && ev.roaming) || wifiOnly) { + if ((!allowRoaming && ev.getRoaming()) || wifiOnly) { newAllowedState = false; } } - return newAllowedState; } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSAddAck.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSAddAck.java index ead373d61d..01d1f26c33 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSAddAck.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSAddAck.java @@ -5,9 +5,9 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.Event; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart; import io.socket.client.Ack; @@ -33,7 +33,7 @@ public class NSAddAck extends Event implements Ack { nsClientID = response.getString("NSCLIENT_ID"); } } - MainApp.bus().post(this); + RxBus.INSTANCE.send(this); return; } catch (Exception e) { log.error("Unhandled exception", e); @@ -44,7 +44,7 @@ public class NSAddAck extends Event implements Ack { if (response.has("result")) { _id = null; if (response.getString("result").contains("Not")) { - MainApp.bus().post(new EventNSClientRestart()); + RxBus.INSTANCE.send(new EventNSClientRestart()); return; } if (L.isEnabled(L.NSCLIENT)) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSAuthAck.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSAuthAck.java index 62c1cf7f5d..754ff6684b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSAuthAck.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSAuthAck.java @@ -2,13 +2,10 @@ package info.nightscout.androidaps.plugins.general.nsclient.acks; import org.json.JSONObject; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.Event; +import info.nightscout.androidaps.plugins.bus.RxBus; import io.socket.client.Ack; -/** - * Created by mike on 02.01.2016. - */ public class NSAuthAck extends Event implements Ack{ public boolean read = false; public boolean write = false; @@ -19,6 +16,6 @@ public class NSAuthAck extends Event implements Ack{ read = response.optBoolean("read"); write = response.optBoolean("write"); write_treatment = response.optBoolean("write_treatment"); - MainApp.bus().post(this); + RxBus.INSTANCE.send(this); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSUpdateAck.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSUpdateAck.java index 68ce3d9a37..2a4022980d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSUpdateAck.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/acks/NSUpdateAck.java @@ -5,9 +5,9 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.Event; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import io.socket.client.Ack; /** @@ -28,7 +28,7 @@ public class NSUpdateAck extends Event implements Ack { result = true; log.debug("Internal error: Missing _id returned on dbUpdate ack"); } - MainApp.bus().post(this); + RxBus.INSTANCE.send(this); } catch (JSONException e) { log.error("Unhandled exception", e); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientNewLog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientNewLog.java deleted file mode 100644 index 121085cb94..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientNewLog.java +++ /dev/null @@ -1,33 +0,0 @@ -package info.nightscout.androidaps.plugins.general.nsclient.events; - -import java.text.SimpleDateFormat; -import java.util.Date; - -import info.nightscout.androidaps.events.Event; - -/** - * Created by mike on 15.02.2017. - */ - -public class EventNSClientNewLog extends Event { - public Date date = new Date(); - public String action; - public String logText; - public EventNSClientNewLog(String action, String logText) { - this.action = action; - this.logText = logText; - } - - SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); - - public StringBuilder toPreparedHtml() { - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append(timeFormat.format(date)); - stringBuilder.append(" "); - stringBuilder.append(action); - stringBuilder.append(" "); - stringBuilder.append(logText); - stringBuilder.append("
"); - return stringBuilder; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientNewLog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientNewLog.kt new file mode 100644 index 0000000000..38d6bcfd0f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientNewLog.kt @@ -0,0 +1,22 @@ +package info.nightscout.androidaps.plugins.general.nsclient.events + +import info.nightscout.androidaps.events.Event +import java.text.SimpleDateFormat +import java.util.* + +class EventNSClientNewLog(var action: String, var logText: String) : Event() { + var date = Date() + + private var timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault()) + + fun toPreparedHtml(): StringBuilder { + val stringBuilder = StringBuilder() + stringBuilder.append(timeFormat.format(date)) + stringBuilder.append(" ") + stringBuilder.append(action) + stringBuilder.append(" ") + stringBuilder.append(logText) + stringBuilder.append("
") + return stringBuilder + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientRestart.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientRestart.java deleted file mode 100644 index b5481ce741..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientRestart.java +++ /dev/null @@ -1,10 +0,0 @@ -package info.nightscout.androidaps.plugins.general.nsclient.events; - -import info.nightscout.androidaps.events.Event; - -/** - * Created by mike on 15.02.2017. - */ - -public class EventNSClientRestart extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientRestart.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientRestart.kt new file mode 100644 index 0000000000..49333b6840 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientRestart.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.general.nsclient.events + +import info.nightscout.androidaps.events.Event + +class EventNSClientRestart : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientStatus.java deleted file mode 100644 index c3bf74321c..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientStatus.java +++ /dev/null @@ -1,18 +0,0 @@ -package info.nightscout.androidaps.plugins.general.nsclient.events; - -import info.nightscout.androidaps.events.Event; - -/** - * Created by mike on 02.01.2016. - */ -public class EventNSClientStatus extends Event { - public String status = ""; - - public EventNSClientStatus(String status) { - this.status = status; - } - - public EventNSClientStatus() { - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientStatus.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientStatus.kt new file mode 100644 index 0000000000..4d0c9dcd6e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientStatus.kt @@ -0,0 +1,7 @@ +package info.nightscout.androidaps.plugins.general.nsclient.events + +import info.nightscout.androidaps.events.EventStatus + +class EventNSClientStatus(var text: String) : EventStatus() { + override fun getStatus(): String = text +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientUpdateGUI.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientUpdateGUI.java deleted file mode 100644 index 60d74a249d..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientUpdateGUI.java +++ /dev/null @@ -1,10 +0,0 @@ -package info.nightscout.androidaps.plugins.general.nsclient.events; - -import info.nightscout.androidaps.events.EventUpdateGui; - -/** - * Created by mike on 17.02.2017. - */ - -public class EventNSClientUpdateGUI extends EventUpdateGui { -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientUpdateGUI.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientUpdateGUI.kt new file mode 100644 index 0000000000..891e61895c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/events/EventNSClientUpdateGUI.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.general.nsclient.events + +import info.nightscout.androidaps.events.EventUpdateGui + +class EventNSClientUpdateGUI : EventUpdateGui() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/receivers/AckAlarmReceiver.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/receivers/AckAlarmReceiver.java index 7185072692..1c46716d69 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/receivers/AckAlarmReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/receivers/AckAlarmReceiver.java @@ -27,7 +27,7 @@ public class AckAlarmReceiver extends BroadcastReceiver { PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, AckAlarmReceiver.class.getSimpleName()); - NSClientPlugin nsClientPlugin = MainApp.getSpecificPlugin(NSClientPlugin.class); + NSClientPlugin nsClientPlugin = NSClientPlugin.getPlugin(); if (!nsClientPlugin.isEnabled(PluginType.GENERAL)) { return; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/receivers/DBAccessReceiver.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/receivers/DBAccessReceiver.java index 4d8b7eba2a..4838c178ef 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/receivers/DBAccessReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/receivers/DBAccessReceiver.java @@ -118,7 +118,7 @@ public class DBAccessReceiver extends BroadcastReceiver { } public boolean shouldUpload() { - NSClientPlugin nsClientPlugin = MainApp.getSpecificPlugin(NSClientPlugin.class); + NSClientPlugin nsClientPlugin = NSClientPlugin.getPlugin(); return nsClientPlugin.isEnabled(PluginType.GENERAL) && !SP.getBoolean(R.string.key_ns_noupload, false); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java index 98ac099851..e1c57cc7c2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java @@ -13,7 +13,6 @@ import android.os.SystemClock; import com.google.common.base.Charsets; import com.google.common.hash.Hashing; import com.j256.ormlite.dao.CloseableIterator; -import com.squareup.otto.Subscribe; import org.json.JSONArray; import org.json.JSONException; @@ -69,12 +68,15 @@ import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; import io.socket.client.IO; import io.socket.client.Socket; import io.socket.emitter.Emitter; public class NSClientService extends Service { private static Logger log = LoggerFactory.getLogger(L.NSCLIENT); + private CompositeDisposable disposable = new CompositeDisposable(); static public PowerManager.WakeLock mWakeLock; private IBinder mBinder = new NSClientService.LocalBinder(); @@ -113,7 +115,6 @@ public class NSClientService extends Service { private int WATCHDOG_MAXCONNECTIONS = 5; public NSClientService() { - registerBus(); if (handler == null) { HandlerThread handlerThread = new HandlerThread(NSClientService.class.getSimpleName() + "Handler"); handlerThread.start(); @@ -129,14 +130,115 @@ public class NSClientService extends Service { public void onCreate() { super.onCreate(); mWakeLock.acquire(); + disposable.add(RxBus.INSTANCE + .toObservable(EventConfigBuilderChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (nsEnabled != NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL)) { + latestDateInReceivedData = 0; + destroy(); + initialize(); + } + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (event.isChanged(R.string.key_nsclientinternal_url) || + event.isChanged(R.string.key_nsclientinternal_api_secret) || + event.isChanged(R.string.key_nsclientinternal_paused) + ) { + latestDateInReceivedData = 0; + destroy(); + initialize(); + } + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (L.isEnabled(L.NSCLIENT)) + log.debug("EventAppExit received"); + destroy(); + stopSelf(); + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventNSClientRestart.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + latestDateInReceivedData = 0; + restart(); + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(NSAuthAck.class) + .observeOn(Schedulers.io()) + .subscribe(event -> processAuthAck(event), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(NSUpdateAck.class) + .observeOn(Schedulers.io()) + .subscribe(event -> processUpdateAck(event), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(NSAddAck.class) + .observeOn(Schedulers.io()) + .subscribe(event -> processAddAck(event), FabricPrivacy::logException) + ); } @Override public void onDestroy() { super.onDestroy(); + disposable.clear(); if (mWakeLock.isHeld()) mWakeLock.release(); } + public void processAddAck(NSAddAck ack) { + if (ack.nsClientID != null) { + uploadQueue.removeID(ack.json); + RxBus.INSTANCE.send(new EventNSClientNewLog("DBADD", "Acked " + ack.nsClientID)); + } else { + RxBus.INSTANCE.send(new EventNSClientNewLog("ERROR", "DBADD Unknown response")); + } + } + + public void processUpdateAck(NSUpdateAck ack) { + if (ack.result) { + uploadQueue.removeID(ack.action, ack._id); + RxBus.INSTANCE.send(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked " + ack._id)); + } else { + RxBus.INSTANCE.send(new EventNSClientNewLog("ERROR", "DBUPDATE/DBREMOVE Unknown response")); + } + } + + public void processAuthAck(NSAuthAck ack) { + String connectionStatus = "Authenticated ("; + if (ack.read) connectionStatus += "R"; + if (ack.write) connectionStatus += "W"; + if (ack.write_treatment) connectionStatus += "T"; + connectionStatus += ')'; + isConnected = true; + hasWriteAuth = ack.write && ack.write_treatment; + RxBus.INSTANCE.send(new EventNSClientStatus(connectionStatus)); + RxBus.INSTANCE.send(new EventNSClientNewLog("AUTH", connectionStatus)); + if (!ack.write) { + RxBus.INSTANCE.send(new EventNSClientNewLog("ERROR", "Write permission not granted !!!!")); + } + if (!ack.write_treatment) { + RxBus.INSTANCE.send(new EventNSClientNewLog("ERROR", "Write treatment permission not granted !!!!")); + } + if (!hasWriteAuth) { + Notification noperm = new Notification(Notification.NSCLIENT_NO_WRITE_PERMISSION, MainApp.gs(R.string.nowritepermission), Notification.URGENT); + RxBus.INSTANCE.send(new EventNewNotification(noperm)); + } else { + RxBus.INSTANCE.send(new EventDismissNotification(Notification.NSCLIENT_NO_WRITE_PERMISSION)); + } + } + public class LocalBinder extends Binder { public NSClientService getServiceInstance() { return NSClientService.this; @@ -154,52 +256,6 @@ public class NSClientService extends Service { return START_STICKY; } - private void registerBus() { - try { - MainApp.bus().unregister(this); - } catch (RuntimeException x) { - // Ignore - } - MainApp.bus().register(this); - } - - @Subscribe - public void onStatusEvent(EventAppExit event) { - if (L.isEnabled(L.NSCLIENT)) - log.debug("EventAppExit received"); - - destroy(); - - stopSelf(); - } - - @Subscribe - public void onStatusEvent(EventPreferenceChange ev) { - if (ev.isChanged(R.string.key_nsclientinternal_url) || - ev.isChanged(R.string.key_nsclientinternal_api_secret) || - ev.isChanged(R.string.key_nsclientinternal_paused) - ) { - latestDateInReceivedData = 0; - destroy(); - initialize(); - } - } - - @Subscribe - public void onStatusEvent(EventConfigBuilderChange ev) { - if (nsEnabled != MainApp.getSpecificPlugin(NSClientPlugin.class).isEnabled(PluginType.GENERAL)) { - latestDateInReceivedData = 0; - destroy(); - initialize(); - } - } - - @Subscribe - public void onStatusEvent(final EventNSClientRestart ev) { - latestDateInReceivedData = 0; - restart(); - } - public void initialize() { dataCounter = 0; @@ -208,19 +264,19 @@ public class NSClientService extends Service { if (!nsAPISecret.equals("")) nsAPIhashCode = Hashing.sha1().hashString(nsAPISecret, Charsets.UTF_8).toString(); - MainApp.bus().post(new EventNSClientStatus("Initializing")); - if (!MainApp.getSpecificPlugin(NSClientPlugin.class).isAllowed()) { - MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "not allowed")); - MainApp.bus().post(new EventNSClientStatus("Not allowed")); - } else if (MainApp.getSpecificPlugin(NSClientPlugin.class).paused) { - MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "paused")); - MainApp.bus().post(new EventNSClientStatus("Paused")); + RxBus.INSTANCE.send(new EventNSClientStatus("Initializing")); + if (!NSClientPlugin.getPlugin().isAllowed()) { + RxBus.INSTANCE.send(new EventNSClientNewLog("NSCLIENT", "not allowed")); + RxBus.INSTANCE.send(new EventNSClientStatus("Not allowed")); + } else if (NSClientPlugin.getPlugin().paused) { + RxBus.INSTANCE.send(new EventNSClientNewLog("NSCLIENT", "paused")); + RxBus.INSTANCE.send(new EventNSClientStatus("Paused")); } else if (!nsEnabled) { - MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "disabled")); - MainApp.bus().post(new EventNSClientStatus("Disabled")); + RxBus.INSTANCE.send(new EventNSClientNewLog("NSCLIENT", "disabled")); + RxBus.INSTANCE.send(new EventNSClientStatus("Disabled")); } else if (!nsURL.equals("")) { try { - MainApp.bus().post(new EventNSClientStatus("Connecting ...")); + RxBus.INSTANCE.send(new EventNSClientStatus("Connecting ...")); IO.Options opt = new IO.Options(); opt.forceNew = true; opt.reconnection = true; @@ -228,7 +284,7 @@ public class NSClientService extends Service { mSocket.on(Socket.EVENT_CONNECT, onConnect); mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect); mSocket.on(Socket.EVENT_PING, onPing); - MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "do connect")); + RxBus.INSTANCE.send(new EventNSClientNewLog("NSCLIENT", "do connect")); mSocket.connect(); mSocket.on("dataUpdate", onDataUpdate); mSocket.on("announcement", onAnnouncement); @@ -236,12 +292,12 @@ public class NSClientService extends Service { mSocket.on("urgent_alarm", onUrgentAlarm); mSocket.on("clear_alarm", onClearAlarm); } catch (URISyntaxException | RuntimeException e) { - MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "Wrong URL syntax")); - MainApp.bus().post(new EventNSClientStatus("Wrong URL syntax")); + RxBus.INSTANCE.send(new EventNSClientNewLog("NSCLIENT", "Wrong URL syntax")); + RxBus.INSTANCE.send(new EventNSClientStatus("Wrong URL syntax")); } } else { - MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "No NS URL specified")); - MainApp.bus().post(new EventNSClientStatus("Not configured")); + RxBus.INSTANCE.send(new EventNSClientNewLog("NSCLIENT", "No NS URL specified")); + RxBus.INSTANCE.send(new EventNSClientStatus("Not configured")); } } @@ -250,7 +306,7 @@ public class NSClientService extends Service { public void call(Object... args) { connectCounter++; String socketId = mSocket != null ? mSocket.id() : "NULL"; - MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "connect #" + connectCounter + " event. ID: " + socketId)); + RxBus.INSTANCE.send(new EventNSClientNewLog("NSCLIENT", "connect #" + connectCounter + " event. ID: " + socketId)); if (mSocket != null) sendAuthMessage(new NSAuthAck()); watchdog(); @@ -267,16 +323,16 @@ public class NSClientService extends Service { reconnections.remove(r); } } - MainApp.bus().post(new EventNSClientNewLog("WATCHDOG", "connections in last " + WATCHDOG_INTERVAL_MINUTES + " mins: " + reconnections.size() + "/" + WATCHDOG_MAXCONNECTIONS)); + RxBus.INSTANCE.send(new EventNSClientNewLog("WATCHDOG", "connections in last " + WATCHDOG_INTERVAL_MINUTES + " mins: " + reconnections.size() + "/" + WATCHDOG_MAXCONNECTIONS)); if (reconnections.size() >= WATCHDOG_MAXCONNECTIONS) { Notification n = new Notification(Notification.NSMALFUNCTION, MainApp.gs(R.string.nsmalfunction), Notification.URGENT); RxBus.INSTANCE.send(new EventNewNotification(n)); - MainApp.bus().post(new EventNSClientNewLog("WATCHDOG", "pausing for " + WATCHDOG_RECONNECT_IN + " mins")); + RxBus.INSTANCE.send(new EventNSClientNewLog("WATCHDOG", "pausing for " + WATCHDOG_RECONNECT_IN + " mins")); NSClientPlugin.getPlugin().pause(true); - MainApp.bus().post(new EventNSClientUpdateGUI()); + RxBus.INSTANCE.send(new EventNSClientUpdateGUI()); new Thread(() -> { SystemClock.sleep(T.mins(WATCHDOG_RECONNECT_IN).msecs()); - MainApp.bus().post(new EventNSClientNewLog("WATCHDOG", "reenabling NSClient")); + RxBus.INSTANCE.send(new EventNSClientNewLog("WATCHDOG", "reenabling NSClient")); NSClientPlugin.getPlugin().pause(false); }).start(); } @@ -288,7 +344,7 @@ public class NSClientService extends Service { public void call(Object... args) { if (L.isEnabled(L.NSCLIENT)) log.debug("disconnect reason: {}", args); - MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "disconnect event")); + RxBus.INSTANCE.send(new EventNSClientNewLog("NSCLIENT", "disconnect event")); } }; @@ -303,7 +359,7 @@ public class NSClientService extends Service { mSocket.off("urgent_alarm"); mSocket.off("clear_alarm"); - MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "destroy")); + RxBus.INSTANCE.send(new EventNSClientNewLog("NSCLIENT", "destroy")); isConnected = false; hasWriteAuth = false; mSocket.disconnect(); @@ -324,38 +380,13 @@ public class NSClientService extends Service { log.error("Unhandled exception", e); return; } - MainApp.bus().post(new EventNSClientNewLog("AUTH", "requesting auth")); + RxBus.INSTANCE.send(new EventNSClientNewLog("AUTH", "requesting auth")); if (mSocket != null) mSocket.emit("authorize", authMessage, ack); } - @Subscribe - public void onStatusEvent(NSAuthAck ack) { - String connectionStatus = "Authenticated ("; - if (ack.read) connectionStatus += "R"; - if (ack.write) connectionStatus += "W"; - if (ack.write_treatment) connectionStatus += "T"; - connectionStatus += ')'; - isConnected = true; - hasWriteAuth = ack.write && ack.write_treatment; - MainApp.bus().post(new EventNSClientStatus(connectionStatus)); - MainApp.bus().post(new EventNSClientNewLog("AUTH", connectionStatus)); - if (!ack.write) { - MainApp.bus().post(new EventNSClientNewLog("ERROR", "Write permission not granted !!!!")); - } - if (!ack.write_treatment) { - MainApp.bus().post(new EventNSClientNewLog("ERROR", "Write treatment permission not granted !!!!")); - } - if (!hasWriteAuth) { - Notification noperm = new Notification(Notification.NSCLIENT_NO_WRITE_PERMISSION, MainApp.gs(R.string.nowritepermission), Notification.URGENT); - RxBus.INSTANCE.send(new EventNewNotification(noperm)); - } else { - RxBus.INSTANCE.send(new EventDismissNotification(Notification.NSCLIENT_NO_WRITE_PERMISSION)); - } - } - public void readPreferences() { - nsEnabled = MainApp.getSpecificPlugin(NSClientPlugin.class).isEnabled(PluginType.GENERAL); + nsEnabled = NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL); nsURL = SP.getString(R.string.key_nsclientinternal_url, ""); nsAPISecret = SP.getString(R.string.key_nsclientinternal_api_secret, ""); nsDevice = SP.getString("careportal_enteredby", ""); @@ -364,7 +395,7 @@ public class NSClientService extends Service { private Emitter.Listener onPing = new Emitter.Listener() { @Override public void call(final Object... args) { - MainApp.bus().post(new EventNSClientNewLog("PING", "received")); + RxBus.INSTANCE.send(new EventNSClientNewLog("PING", "received")); // send data if there is something waiting resend("Ping received"); } @@ -393,7 +424,7 @@ public class NSClientService extends Service { return; } try { - MainApp.bus().post(new EventNSClientNewLog("ANNOUNCEMENT", JsonHelper.safeGetString(data, "message", "received"))); + RxBus.INSTANCE.send(new EventNSClientNewLog("ANNOUNCEMENT", JsonHelper.safeGetString(data, "message", "received"))); } catch (Exception e) { FabricPrivacy.logException(e); log.error("Unhandled exception", e); @@ -420,7 +451,7 @@ public class NSClientService extends Service { */ @Override public void call(final Object... args) { - MainApp.bus().post(new EventNSClientNewLog("ALARM", "received")); + RxBus.INSTANCE.send(new EventNSClientNewLog("ALARM", "received")); JSONObject data; try { data = (JSONObject) args[0]; @@ -459,7 +490,7 @@ public class NSClientService extends Service { log.error("Unhandled exception", e); return; } - MainApp.bus().post(new EventNSClientNewLog("URGENTALARM", "received")); + RxBus.INSTANCE.send(new EventNSClientNewLog("URGENTALARM", "received")); BroadcastUrgentAlarm.handleUrgentAlarm(data, getApplicationContext()); if (L.isEnabled(L.NSCLIENT)) log.debug(data.toString()); @@ -485,7 +516,7 @@ public class NSClientService extends Service { log.error("Unhandled exception", e); return; } - MainApp.bus().post(new EventNSClientNewLog("CLEARALARM", "received")); + RxBus.INSTANCE.send(new EventNSClientNewLog("CLEARALARM", "received")); BroadcastClearAlarm.handleClearAlarm(data, getApplicationContext()); if (L.isEnabled(L.NSCLIENT)) log.debug(data.toString()); @@ -510,7 +541,7 @@ public class NSClientService extends Service { // delta means only increment/changes are comming boolean isDelta = data.has("delta"); boolean isFull = !isDelta; - MainApp.bus().post(new EventNSClientNewLog("DATA", "Data packet #" + dataCounter++ + (isDelta ? " delta" : " full"))); + RxBus.INSTANCE.send(new EventNSClientNewLog("DATA", "Data packet #" + dataCounter++ + (isDelta ? " delta" : " full"))); if (data.has("profiles")) { JSONArray profiles = data.getJSONArray("profiles"); @@ -518,7 +549,7 @@ public class NSClientService extends Service { JSONObject profile = (JSONObject) profiles.get(profiles.length() - 1); profileStore = new ProfileStore(profile); broadcastProfile = true; - MainApp.bus().post(new EventNSClientNewLog("PROFILE", "profile received")); + RxBus.INSTANCE.send(new EventNSClientNewLog("PROFILE", "profile received")); } } @@ -528,7 +559,7 @@ public class NSClientService extends Service { if (!status.has("versionNum")) { if (status.getInt("versionNum") < Config.SUPPORTEDNSVERSION) { - MainApp.bus().post(new EventNSClientNewLog("ERROR", "Unsupported Nightscout version !!!!")); + RxBus.INSTANCE.send(new EventNSClientNewLog("ERROR", "Unsupported Nightscout version !!!!")); } } else { nightscoutVersionName = nsSettingsStatus.getVersion(); @@ -553,13 +584,13 @@ public class NSClientService extends Service { } */ } else if (!isDelta) { - MainApp.bus().post(new EventNSClientNewLog("ERROR", "Unsupported Nightscout version !!!!")); + RxBus.INSTANCE.send(new EventNSClientNewLog("ERROR", "Unsupported Nightscout version !!!!")); } // If new profile received or change detected broadcast it if (broadcastProfile && profileStore != null) { BroadcastProfile.handleNewTreatment(profileStore, MainApp.instance().getApplicationContext(), isDelta); - MainApp.bus().post(new EventNSClientNewLog("PROFILE", "broadcasting")); + RxBus.INSTANCE.send(new EventNSClientNewLog("PROFILE", "broadcasting")); } if (data.has("treatments")) { @@ -568,7 +599,7 @@ public class NSClientService extends Service { JSONArray updatedTreatments = new JSONArray(); JSONArray addedTreatments = new JSONArray(); if (treatments.length() > 0) - MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + treatments.length() + " treatments")); + RxBus.INSTANCE.send(new EventNSClientNewLog("DATA", "received " + treatments.length() + " treatments")); for (Integer index = 0; index < treatments.length(); index++) { JSONObject jsonTreatment = treatments.getJSONObject(index); NSTreatment treatment = new NSTreatment(jsonTreatment); @@ -602,7 +633,7 @@ public class NSClientService extends Service { if (data.has("devicestatus")) { JSONArray devicestatuses = data.getJSONArray("devicestatus"); if (devicestatuses.length() > 0) { - MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + devicestatuses.length() + " devicestatuses")); + RxBus.INSTANCE.send(new EventNSClientNewLog("DATA", "received " + devicestatuses.length() + " devicestatuses")); for (Integer index = 0; index < devicestatuses.length(); index++) { JSONObject jsonStatus = devicestatuses.getJSONObject(index); // remove from upload queue if Ack is failing @@ -617,7 +648,7 @@ public class NSClientService extends Service { JSONArray updatedFoods = new JSONArray(); JSONArray addedFoods = new JSONArray(); if (foods.length() > 0) - MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + foods.length() + " foods")); + RxBus.INSTANCE.send(new EventNSClientNewLog("DATA", "received " + foods.length() + " foods")); for (Integer index = 0; index < foods.length(); index++) { JSONObject jsonFood = foods.getJSONObject(index); @@ -647,7 +678,7 @@ public class NSClientService extends Service { if (data.has("mbgs")) { JSONArray mbgs = data.getJSONArray("mbgs"); if (mbgs.length() > 0) - MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + mbgs.length() + " mbgs")); + RxBus.INSTANCE.send(new EventNSClientNewLog("DATA", "received " + mbgs.length() + " mbgs")); for (Integer index = 0; index < mbgs.length(); index++) { JSONObject jsonMbg = mbgs.getJSONObject(index); // remove from upload queue if Ack is failing @@ -658,7 +689,7 @@ public class NSClientService extends Service { if (data.has("cals")) { JSONArray cals = data.getJSONArray("cals"); if (cals.length() > 0) - MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + cals.length() + " cals")); + RxBus.INSTANCE.send(new EventNSClientNewLog("DATA", "received " + cals.length() + " cals")); // Retreive actual calibration for (Integer index = 0; index < cals.length(); index++) { // remove from upload queue if Ack is failing @@ -669,10 +700,10 @@ public class NSClientService extends Service { if (data.has("sgvs")) { JSONArray sgvs = data.getJSONArray("sgvs"); if (sgvs.length() > 0) - MainApp.bus().post(new EventNSClientNewLog("DATA", "received " + sgvs.length() + " sgvs")); + RxBus.INSTANCE.send(new EventNSClientNewLog("DATA", "received " + sgvs.length() + " sgvs")); for (Integer index = 0; index < sgvs.length(); index++) { JSONObject jsonSgv = sgvs.getJSONObject(index); - // MainApp.bus().post(new EventNSClientNewLog("DATA", "svg " + sgvs.getJSONObject(index).toString()); + // RxBus.INSTANCE.send(new EventNSClientNewLog("DATA", "svg " + sgvs.getJSONObject(index).toString()); NSSgv sgv = new NSSgv(jsonSgv); // Handle new sgv here // remove from upload queue if Ack is failing @@ -691,11 +722,11 @@ public class NSClientService extends Service { } BroadcastSgvs.handleNewSgv(sgvs, MainApp.instance().getApplicationContext(), isDelta); } - MainApp.bus().post(new EventNSClientNewLog("LAST", DateUtil.dateAndTimeString(latestDateInReceivedData))); + RxBus.INSTANCE.send(new EventNSClientNewLog("LAST", DateUtil.dateAndTimeString(latestDateInReceivedData))); } catch (JSONException e) { log.error("Unhandled exception", e); } - //MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "onDataUpdate end"); + //RxBus.INSTANCE.send(new EventNSClientNewLog("NSCLIENT", "onDataUpdate end"); } finally { if (wakeLock.isHeld()) wakeLock.release(); } @@ -713,7 +744,7 @@ public class NSClientService extends Service { message.put("_id", dbr._id); message.put("data", new JSONObject(dbr.data)); mSocket.emit("dbUpdate", message, ack); - MainApp.bus().post(new EventNSClientNewLog("DBUPDATE " + dbr.collection, "Sent " + dbr._id)); + RxBus.INSTANCE.send(new EventNSClientNewLog("DBUPDATE " + dbr.collection, "Sent " + dbr._id)); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -727,7 +758,7 @@ public class NSClientService extends Service { message.put("_id", dbr._id); message.put("data", new JSONObject(dbr.data)); mSocket.emit("dbUpdateUnset", message, ack); - MainApp.bus().post(new EventNSClientNewLog("DBUPDATEUNSET " + dbr.collection, "Sent " + dbr._id)); + RxBus.INSTANCE.send(new EventNSClientNewLog("DBUPDATEUNSET " + dbr.collection, "Sent " + dbr._id)); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -740,22 +771,12 @@ public class NSClientService extends Service { message.put("collection", dbr.collection); message.put("_id", dbr._id); mSocket.emit("dbRemove", message, ack); - MainApp.bus().post(new EventNSClientNewLog("DBREMOVE " + dbr.collection, "Sent " + dbr._id)); + RxBus.INSTANCE.send(new EventNSClientNewLog("DBREMOVE " + dbr.collection, "Sent " + dbr._id)); } catch (JSONException e) { log.error("Unhandled exception", e); } } - @Subscribe - public void onStatusEvent(NSUpdateAck ack) { - if (ack.result) { - uploadQueue.removeID(ack.action, ack._id); - MainApp.bus().post(new EventNSClientNewLog("DBUPDATE/DBREMOVE", "Acked " + ack._id)); - } else { - MainApp.bus().post(new EventNSClientNewLog("ERROR", "DBUPDATE/DBREMOVE Unknown response")); - } - } - public void dbAdd(DbRequest dbr, NSAddAck ack) { try { if (!isConnected || !hasWriteAuth) return; @@ -763,7 +784,7 @@ public class NSClientService extends Service { message.put("collection", dbr.collection); message.put("data", new JSONObject(dbr.data)); mSocket.emit("dbAdd", message, ack); - MainApp.bus().post(new EventNSClientNewLog("DBADD " + dbr.collection, "Sent " + dbr.nsClientID)); + RxBus.INSTANCE.send(new EventNSClientNewLog("DBADD " + dbr.collection, "Sent " + dbr.nsClientID)); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -772,17 +793,7 @@ public class NSClientService extends Service { public void sendAlarmAck(AlarmAck alarmAck) { if (!isConnected || !hasWriteAuth) return; mSocket.emit("ack", alarmAck.level, alarmAck.group, alarmAck.silenceTime); - MainApp.bus().post(new EventNSClientNewLog("ALARMACK ", alarmAck.level + " " + alarmAck.group + " " + alarmAck.silenceTime)); - } - - @Subscribe - public void onStatusEvent(NSAddAck ack) { - if (ack.nsClientID != null) { - uploadQueue.removeID(ack.json); - MainApp.bus().post(new EventNSClientNewLog("DBADD", "Acked " + ack.nsClientID)); - } else { - MainApp.bus().post(new EventNSClientNewLog("ERROR", "DBADD Unknown response")); - } + RxBus.INSTANCE.send(new EventNSClientNewLog("ALARMACK ", alarmAck.level + " " + alarmAck.group + " " + alarmAck.silenceTime)); } public void resend(final String reason) { @@ -803,7 +814,7 @@ public class NSClientService extends Service { } lastResendTime = System.currentTimeMillis(); - MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend started: " + reason)); + RxBus.INSTANCE.send(new EventNSClientNewLog("QUEUE", "Resend started: " + reason)); CloseableIterator iterator = null; int maxcount = 30; @@ -834,7 +845,7 @@ public class NSClientService extends Service { log.error("Unhandled exception", e); } - MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend ended: " + reason)); + RxBus.INSTANCE.send(new EventNSClientNewLog("QUEUE", "Resend ended: " + reason)); } }); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java index b7d7cae4a0..046290500d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java @@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.general.overview; import android.annotation.SuppressLint; import android.app.Activity; import android.app.NotificationManager; - import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; @@ -12,16 +11,6 @@ import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.os.Handler; - -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentActivity; -import androidx.fragment.app.FragmentManager; -import androidx.core.content.res.ResourcesCompat; -import androidx.appcompat.app.AlertDialog; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.appcompat.widget.PopupMenu; -import androidx.recyclerview.widget.RecyclerView; - import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.util.DisplayMetrics; @@ -36,8 +25,16 @@ import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.widget.PopupMenu; +import androidx.core.content.res.ResourcesCompat; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + import com.jjoe64.graphview.GraphView; -import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -83,6 +80,7 @@ import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.aps.loop.APSResult; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; import info.nightscout.androidaps.plugins.aps.loop.events.EventNewOpenLoopNotification; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment; @@ -113,18 +111,23 @@ import info.nightscout.androidaps.utils.BolusWizard; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.DefaultValueHelper; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.OKDialog; import info.nightscout.androidaps.utils.Profiler; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SingleClickButton; import info.nightscout.androidaps.utils.T; import info.nightscout.androidaps.utils.ToastUtils; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; import static info.nightscout.androidaps.utils.DateUtil.now; public class OverviewFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener { private static Logger log = LoggerFactory.getLogger(L.OVERVIEW); + private CompositeDisposable disposable = new CompositeDisposable(); + TextView timeView; TextView bgView; TextView arrowView; @@ -340,6 +343,117 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, return view; } + @Override + public void onPause() { + super.onPause(); + disposable.clear(); + sLoopHandler.removeCallbacksAndMessages(null); + unregisterForContextMenu(apsModeView); + unregisterForContextMenu(activeProfileView); + unregisterForContextMenu(tempTargetView); + } + + @Override + public void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventRefreshOverview.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(eventOpenAPSUpdateGui -> scheduleUpdateGUI(eventOpenAPSUpdateGui.getFrom()), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventExtendedBolusChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventExtendedBolusChange"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventTempBasalChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventTempBasalChange"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventTreatmentChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventTreatmentChange"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventTempTargetChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventTempTargetChange"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAcceptOpenLoopChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventAcceptOpenLoopChange"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventCareportalEventChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventCareportalEventChange"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventInitializationChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventInitializationChanged"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventAutosensCalculationFinished"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventProfileNeedsUpdate.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventProfileNeedsUpdate"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventPreferenceChange"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventNewOpenLoopNotification.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventNewOpenLoopNotification"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventPumpStatusChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updatePumpStatus(event.getStatus()), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventIobCalculationProgress.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + if (iobCalculationProgressView != null) + iobCalculationProgressView.setText(event.getProgress()); + }, + FabricPrivacy::logException + )); + sRefreshLoop = () -> { + scheduleUpdateGUI("refreshLoop"); + sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L); + }; + sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L); + registerForContextMenu(apsModeView); + registerForContextMenu(activeProfileView); + registerForContextMenu(tempTargetView); + updateGUI("onResume"); + } + private void setupChartMenu(View view) { chartButton = (ImageButton) view.findViewById(R.id.overview_chartMenuButton); chartButton.setOnClickListener(v -> { @@ -539,8 +653,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } } - private void showSuspendtPump(ContextMenu menu, - PumpDescription pumpDescription) { + private void showSuspendtPump(ContextMenu menu, PumpDescription pumpDescription) { if (pumpDescription.tempDurationStep15mAllowed) menu.add(MainApp.gs(R.string.disconnectpumpfor15m)); if (pumpDescription.tempDurationStep30mAllowed) @@ -846,108 +959,6 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } } - @Override - public void onPause() { - super.onPause(); - MainApp.bus().unregister(this); - sLoopHandler.removeCallbacksAndMessages(null); - unregisterForContextMenu(apsModeView); - unregisterForContextMenu(activeProfileView); - unregisterForContextMenu(tempTargetView); - } - - @Override - public void onResume() { - super.onResume(); - MainApp.bus().register(this); - sRefreshLoop = () -> { - scheduleUpdateGUI("refreshLoop"); - sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L); - }; - sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L); - registerForContextMenu(apsModeView); - registerForContextMenu(activeProfileView); - registerForContextMenu(tempTargetView); - updateGUI("onResume"); - } - - @Subscribe - public void onStatusEvent(final EventInitializationChanged ev) { - scheduleUpdateGUI("EventInitializationChanged"); - } - - @Subscribe - public void onStatusEvent(final EventPreferenceChange ev) { - scheduleUpdateGUI("EventPreferenceChange"); - } - - @Subscribe - public void onStatusEvent(final EventRefreshOverview ev) { - scheduleUpdateGUI(ev.from); - } - - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - scheduleUpdateGUI("EventAutosensCalculationFinished"); - } - - @Subscribe - public void onStatusEvent(final EventTreatmentChange ev) { - scheduleUpdateGUI("EventTreatmentChange"); - } - - @Subscribe - public void onStatusEvent(final EventCareportalEventChange ev) { - scheduleUpdateGUI("EventCareportalEventChange"); - } - - @Subscribe - public void onStatusEvent(final EventTempBasalChange ev) { - scheduleUpdateGUI("EventTempBasalChange"); - } - - @Subscribe - public void onStatusEvent(final EventExtendedBolusChange ev) { - scheduleUpdateGUI("EventExtendedBolusChange"); - } - - @Subscribe - public void onStatusEvent(final EventNewOpenLoopNotification ev) { - scheduleUpdateGUI("EventNewOpenLoopNotification"); - } - - @Subscribe - public void onStatusEvent(final EventAcceptOpenLoopChange ev) { - scheduleUpdateGUI("EventAcceptOpenLoopChange"); - } - - @Subscribe - public void onStatusEvent(final EventTempTargetChange ev) { - scheduleUpdateGUI("EventTempTargetChange"); - } - - @Subscribe - public void onStatusEvent(final EventProfileNeedsUpdate ev) { - scheduleUpdateGUI("EventProfileNeedsUpdate"); - } - - @Subscribe - public void onStatusEvent(final EventPumpStatusChanged s) { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> updatePumpStatus(s.textStatus())); - } - - @Subscribe - public void onStatusEvent(final EventIobCalculationProgress e) { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - if (iobCalculationProgressView != null) - iobCalculationProgressView.setText(e.progress); - }); - } - private void hideTempRecommendation() { Activity activity = getActivity(); if (activity != null) @@ -1135,7 +1146,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } // **** Calibration & CGM buttons **** - boolean xDripIsBgSource = MainApp.getSpecificPlugin(SourceXdripPlugin.class) != null && MainApp.getSpecificPlugin(SourceXdripPlugin.class).isEnabled(PluginType.BGSOURCE); + boolean xDripIsBgSource = SourceXdripPlugin.getPlugin().isEnabled(PluginType.BGSOURCE); boolean dexcomIsSource = SourceDexcomPlugin.INSTANCE.isEnabled(PluginType.BGSOURCE); boolean bgAvailable = DatabaseHelper.actualBg() != null; if (calibrationButton != null) { @@ -1212,7 +1223,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, extendedBolusView.setVisibility(View.VISIBLE); } - activeProfileView.setText(ProfileFunctions.getInstance().getProfileName()); + activeProfileView.setText(ProfileFunctions.getInstance().getProfileNameWithDuration()); if (profile.getPercentage() != 100 || profile.getTimeshift() != 0) { activeProfileView.setBackgroundColor(MainApp.gc(R.color.ribbonWarning)); activeProfileView.setTextColor(MainApp.gc(R.color.ribbonTextWarning)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt index 0c5b3b4370..cece359f22 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewPlugin.kt @@ -45,7 +45,7 @@ object OverviewPlugin : PluginBase(PluginDescription() .observeOn(Schedulers.io()) .subscribe({ n -> if (notificationStore.add(n.notification)) - MainApp.bus().post(EventRefreshOverview("EventNewNotification")) + RxBus.send(EventRefreshOverview("EventNewNotification")) }, { FabricPrivacy.logException(it) }) @@ -54,7 +54,7 @@ object OverviewPlugin : PluginBase(PluginDescription() .observeOn(Schedulers.io()) .subscribe({ n -> if (notificationStore.remove(n.id)) - MainApp.bus().post(EventRefreshOverview("EventDismissNotification")) + RxBus.send(EventRefreshOverview("EventDismissNotification")) }, { FabricPrivacy.logException(it) }) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/BolusProgressDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/BolusProgressDialog.java index 04da9761a3..645f6d2b19 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/BolusProgressDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/BolusProgressDialog.java @@ -14,8 +14,6 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.fragment.app.DialogFragment; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,12 +21,18 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusprogressIfRunning; +import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; +import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; public class BolusProgressDialog extends DialogFragment implements View.OnClickListener { private static Logger log = LoggerFactory.getLogger(L.UI); + private CompositeDisposable disposable = new CompositeDisposable(); + Button stopButton; TextView statusView; TextView stopPressedView; @@ -91,11 +95,34 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL if (L.isEnabled(L.UI)) log.debug("onResume running"); } - try { - MainApp.bus().register(this); - } catch (IllegalArgumentException e) { - log.error("Already registered"); - } + disposable.add(RxBus.INSTANCE + .toObservable(EventPumpStatusChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> statusView.setText(event.getStatus()), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventDismissBolusProgressIfRunning.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + if (L.isEnabled(L.UI)) log.debug("EventDismissBolusProgressIfRunning"); + if (BolusProgressDialog.running) dismiss(); + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventOverviewBolusProgress.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + if (L.isEnabled(L.UI)) + log.debug("Status: " + event.getStatus() + " Percent: " + event.getPercent()); + statusView.setText(event.getStatus()); + progressBar.setProgress(event.getPercent()); + if (event.getPercent() == 100) { + stopButton.setVisibility(View.INVISIBLE); + scheduleDismiss(); + } + state = event.getStatus(); + }, FabricPrivacy::logException) + ); } @Override @@ -121,11 +148,7 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL log.debug("onPause"); running = false; super.onPause(); - try { - MainApp.bus().unregister(this); - } catch (IllegalArgumentException e) { - log.error("Already unregistered"); - } + disposable.clear(); } @Override @@ -149,43 +172,6 @@ public class BolusProgressDialog extends DialogFragment implements View.OnClickL } } - @Subscribe - public void onStatusEvent(final EventOverviewBolusProgress ev) { - Activity activity = getActivity(); - if (activity != null) { - activity.runOnUiThread(() -> { - if (L.isEnabled(L.UI)) - log.debug("Status: " + ev.status + " Percent: " + ev.percent); - statusView.setText(ev.status); - progressBar.setProgress(ev.percent); - if (ev.percent == 100) { - stopButton.setVisibility(View.INVISIBLE); - scheduleDismiss(); - } - }); - } - state = ev.status; - } - - @Subscribe - public void onStatusEvent(final EventDismissBolusprogressIfRunning ev) { - if (L.isEnabled(L.UI)) - log.debug("EventDismissBolusprogressIfRunning"); - if (BolusProgressDialog.running) { - dismiss(); - } - } - - @Subscribe - public void onStatusEvent(final EventPumpStatusChanged c) { - if (L.isEnabled(L.UI)) - log.debug("EventPumpStatusChanged"); - Activity activity = getActivity(); - if (activity != null) { - activity.runOnUiThread(() -> statusView.setText(c.textStatus())); - } - } - private void scheduleDismiss() { if (L.isEnabled(L.UI)) log.debug("scheduleDismiss"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt index 1b52e5f3ff..efe800bef0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt @@ -6,7 +6,7 @@ import android.text.Editable import android.text.TextWatcher import android.view.* import android.widget.AdapterView -import android.widget.AdapterView.* +import android.widget.AdapterView.OnItemSelectedListener import android.widget.ArrayAdapter import android.widget.CompoundButton import androidx.fragment.app.DialogFragment @@ -14,8 +14,8 @@ import info.nightscout.androidaps.Constants import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.data.Profile +import info.nightscout.androidaps.db.BgReading import info.nightscout.androidaps.db.DatabaseHelper -import info.nightscout.androidaps.events.EventFeatureRunning import info.nightscout.androidaps.interfaces.Constraint import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin @@ -26,11 +26,11 @@ import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.utils.* import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable -import kotlinx.android.synthetic.main.okcancel.* import kotlinx.android.synthetic.main.overview_wizard_dialog.* import org.slf4j.LoggerFactory import java.text.DecimalFormat import java.util.* +import kotlin.math.abs class WizardDialog : DialogFragment() { private val log = LoggerFactory.getLogger(WizardDialog::class.java) @@ -58,22 +58,22 @@ class WizardDialog : DialogFragment() { this.parentContext = context } + override fun onStart() { + super.onStart() + dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + } + override fun onDetach() { super.onDetach() this.parentContext = null } - override fun onResume() { - super.onResume() - MainApp.bus().post(EventFeatureRunning(EventFeatureRunning.Feature.WIZARD)) - } - override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) - savedInstanceState.putDouble("treatments_wizard_bginput", treatments_wizard_bginput.value) - savedInstanceState.putDouble("treatments_wizard_carbsinput", treatments_wizard_carbsinput.value) - savedInstanceState.putDouble("treatments_wizard_correctioninput", treatments_wizard_correctioninput.value) - savedInstanceState.putDouble("treatments_wizard_carbtimeinput", treatments_wizard_carbtimeinput.value) + savedInstanceState.putDouble("treatments_wizard_bg_input", treatments_wizard_bg_input.value) + savedInstanceState.putDouble("treatments_wizard_carbs_input", treatments_wizard_carbs_input.value) + savedInstanceState.putDouble("treatments_wizard_correction_input", treatments_wizard_correction_input.value) + savedInstanceState.putDouble("treatments_wizard_carb_time_input", treatments_wizard_carb_time_input.value) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, @@ -95,25 +95,26 @@ class WizardDialog : DialogFragment() { val maxCarbs = MainApp.getConstraintChecker().maxCarbsAllowed.value() val maxCorrection = MainApp.getConstraintChecker().maxBolusAllowed.value() - treatments_wizard_bginput.setParams(savedInstanceState?.getDouble("treatments_wizard_bginput") + treatments_wizard_bg_input.setParams(savedInstanceState?.getDouble("treatments_wizard_bg_input") ?: 0.0, 0.0, 500.0, 0.1, DecimalFormat("0.0"), false, ok, textWatcher) - treatments_wizard_carbsinput.setParams(savedInstanceState?.getDouble("treatments_wizard_carbsinput") + treatments_wizard_carbs_input.setParams(savedInstanceState?.getDouble("treatments_wizard_carbs_input") ?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, ok, textWatcher) - val bolusstep = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription?.bolusStep + val bolusStep = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription?.bolusStep ?: 0.1 - treatments_wizard_correctioninput.setParams(savedInstanceState?.getDouble("treatments_wizard_correctioninput") - ?: 0.0, -maxCorrection, maxCorrection, bolusstep, DecimalFormatter.pumpSupportedBolusFormat(), false, ok, textWatcher) - treatments_wizard_carbtimeinput.setParams(savedInstanceState?.getDouble("treatments_wizard_carbtimeinput") + treatments_wizard_correction_input.setParams(savedInstanceState?.getDouble("treatments_wizard_correction_input") + ?: 0.0, -maxCorrection, maxCorrection, bolusStep, DecimalFormatter.pumpSupportedBolusFormat(), false, ok, textWatcher) + treatments_wizard_carb_time_input.setParams(savedInstanceState?.getDouble("treatments_wizard_carb_time_input") ?: 0.0, -60.0, 60.0, 5.0, DecimalFormat("0"), false, ok, textWatcher) initDialog() - treatments_wizard_percent_used.text = SP.getInt(R.string.key_boluswizard_percentage, 100).toString() + "%" + treatments_wizard_percent_used.text = MainApp.gs(R.string.format_percent, SP.getInt(R.string.key_boluswizard_percentage, 100)) // ok button ok.setOnClickListener { if (okClicked) { log.debug("guarding: ok already clicked") } else { okClicked = true + calculateInsulin() parentContext?.let { context -> wizard?.confirmAndExecute(context) } @@ -130,6 +131,18 @@ class WizardDialog : DialogFragment() { treatments_wizard_bolusiobcheckbox.setOnCheckedChangeListener { buttonView, _ -> onCheckedChanged(buttonView) } treatments_wizard_bgtrendcheckbox.setOnCheckedChangeListener { buttonView, _ -> onCheckedChanged(buttonView) } treatments_wizard_sbcheckbox.setOnCheckedChangeListener { buttonView, _ -> onCheckedChanged(buttonView) } + + val showCalc = SP.getBoolean(MainApp.gs(R.string.key_wizard_calculation_visible), false) + treatments_wizard_delimiter.visibility = if (showCalc) View.VISIBLE else View.GONE + treatments_wizard_resulttable.visibility = if (showCalc) View.VISIBLE else View.GONE + treatments_wizard_calculationcheckbox.isChecked = showCalc + treatments_wizard_calculationcheckbox.setOnCheckedChangeListener { _, isChecked -> + run { + SP.putBoolean(MainApp.gs(R.string.key_wizard_calculation_visible), isChecked) + treatments_wizard_delimiter.visibility = if (isChecked) View.VISIBLE else View.GONE + treatments_wizard_resulttable.visibility = if (isChecked) View.VISIBLE else View.GONE + } + } // profile spinner treatments_wizard_profile.onItemSelectedListener = object : OnItemSelectedListener { override fun onNothingSelected(parent: AdapterView<*>?) { @@ -160,7 +173,7 @@ class WizardDialog : DialogFragment() { disposable.clear() } - fun onCheckedChanged(buttonView: CompoundButton) { + private fun onCheckedChanged(buttonView: CompoundButton) { saveCheckedStates() treatments_wizard_ttcheckbox.isEnabled = treatments_wizard_bgcheckbox.isChecked && TreatmentsPlugin.getPlugin().tempTargetFromHistory != null if (buttonView.id == treatments_wizard_cobcheckbox.id) @@ -212,17 +225,17 @@ class WizardDialog : DialogFragment() { val units = profile.units treatments_wizard_bgunits.text = units if (units == Constants.MGDL) - treatments_wizard_bginput.setStep(1.0) + treatments_wizard_bg_input.setStep(1.0) else - treatments_wizard_bginput.setStep(0.1) + treatments_wizard_bg_input.setStep(0.1) // Set BG if not old val lastBg = DatabaseHelper.actualBg() if (lastBg != null) { - treatments_wizard_bginput.value = lastBg.valueToUnits(units) + treatments_wizard_bg_input.value = lastBg.valueToUnits(units) } else { - treatments_wizard_bginput.value = 0.0 + treatments_wizard_bg_input.value = 0.0 } treatments_wizard_ttcheckbox.isEnabled = TreatmentsPlugin.getPlugin().tempTargetFromHistory != null @@ -255,37 +268,29 @@ class WizardDialog : DialogFragment() { if (specificProfile == null) return // Entered values - var c_bg = SafeParse.stringToDouble(treatments_wizard_bginput.text) - val c_carbs = SafeParse.stringToInt(treatments_wizard_carbsinput.text) - var c_correction = SafeParse.stringToDouble(treatments_wizard_correctioninput.text) - val corrAfterConstraint = c_correction - if (c_correction > 0) - c_correction = MainApp.getConstraintChecker().applyBolusConstraints(Constraint(c_correction)).value() - if (Math.abs(c_correction - corrAfterConstraint) > 0.01) { // c_correction != corrAfterConstraint doesn't work - treatments_wizard_correctioninput.value = 0.0 - ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.bolusconstraintapplied)) - return - } - val carbsAfterConstraint = MainApp.getConstraintChecker().applyCarbsConstraints(Constraint(c_carbs)).value() - if (Math.abs(c_carbs - carbsAfterConstraint) > 0.01) { - treatments_wizard_carbsinput.value = 0.0 + var bg = SafeParse.stringToDouble(treatments_wizard_bg_input.text) + val carbs = SafeParse.stringToInt(treatments_wizard_carbs_input.text) + val correction = SafeParse.stringToDouble(treatments_wizard_correction_input.text) + val carbsAfterConstraint = MainApp.getConstraintChecker().applyCarbsConstraints(Constraint(carbs)).value() + if (abs(carbs - carbsAfterConstraint) > 0.01) { + treatments_wizard_carbs_input.value = 0.0 ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.carbsconstraintapplied)) return } - c_bg = if (treatments_wizard_bgcheckbox.isChecked) c_bg else 0.0 + bg = if (treatments_wizard_bgcheckbox.isChecked) bg else 0.0 val tempTarget = if (treatments_wizard_ttcheckbox.isChecked) TreatmentsPlugin.getPlugin().tempTargetFromHistory else null // COB - var c_cob = 0.0 + var cob = 0.0 if (treatments_wizard_cobcheckbox.isChecked) { val cobInfo = IobCobCalculatorPlugin.getPlugin().getCobInfo(false, "Wizard COB") - cobInfo.displayCob?.let { c_cob = it } + cobInfo.displayCob?.let { cob = it } } - val carbTime = SafeParse.stringToInt(treatments_wizard_carbtimeinput.text) + val carbTime = SafeParse.stringToInt(treatments_wizard_carb_time_input.text) - wizard = BolusWizard(specificProfile, profileName, tempTarget, carbsAfterConstraint, c_cob, c_bg, corrAfterConstraint, + wizard = BolusWizard(specificProfile, profileName, tempTarget, carbsAfterConstraint, cob, bg, correction, SP.getInt(R.string.key_boluswizard_percentage, 100).toDouble(), treatments_wizard_bgcheckbox.isChecked, treatments_wizard_cobcheckbox.isChecked, @@ -297,10 +302,10 @@ class WizardDialog : DialogFragment() { treatment_wizard_notes.text.toString(), carbTime) wizard?.let { wizard -> - treatments_wizard_bg.text = c_bg.toString() + " ISF: " + DecimalFormatter.to1Decimal(wizard.sens) + treatments_wizard_bg.text = String.format(MainApp.gs(R.string.format_bg_isf), BgReading().value(Profile.toMgdl(bg, specificProfile.units)).valueToUnitsToString(specificProfile.units), wizard.sens) treatments_wizard_bginsulin.text = StringUtils.formatInsulin(wizard.insulinFromBG) - treatments_wizard_carbs.text = DecimalFormatter.to0Decimal(c_carbs.toDouble()) + "g IC: " + DecimalFormatter.to1Decimal(wizard.ic) + treatments_wizard_carbs.text = String.format(MainApp.gs(R.string.format_carbs_ic), carbs.toDouble(), wizard.ic) treatments_wizard_carbsinsulin.text = StringUtils.formatInsulin(wizard.insulinFromCarbs) treatments_wizard_bolusiobinsulin.text = StringUtils.formatInsulin(wizard.insulinFromBolusIOB) @@ -324,7 +329,7 @@ class WizardDialog : DialogFragment() { // COB if (treatments_wizard_cobcheckbox.isChecked) { - treatments_wizard_cob.text = DecimalFormatter.to2Decimal(c_cob) + "g IC: " + DecimalFormatter.to1Decimal(wizard.ic) + treatments_wizard_cob.text = String.format(MainApp.gs(R.string.format_cob_ic), cob, wizard.ic) treatments_wizard_cobinsulin.text = StringUtils.formatInsulin(wizard.insulinFromCOB) } else { treatments_wizard_cob.text = "" @@ -332,12 +337,12 @@ class WizardDialog : DialogFragment() { } if (wizard.calculatedTotalInsulin > 0.0 || carbsAfterConstraint > 0.0) { - val insulinText = if (wizard.calculatedTotalInsulin > 0.0) DecimalFormatter.toPumpSupportedBolus(wizard.calculatedTotalInsulin) + "U" else "" - val carbsText = if (carbsAfterConstraint > 0.0) DecimalFormatter.to0Decimal(carbsAfterConstraint.toDouble()) + "g" else "" - treatments_wizard_total.text = MainApp.gs(R.string.result) + ": " + insulinText + " " + carbsText + val insulinText = if (wizard.calculatedTotalInsulin > 0.0) MainApp.gs(R.string.formatinsulinunits, wizard.calculatedTotalInsulin) else "" + val carbsText = if (carbsAfterConstraint > 0.0) MainApp.gs(R.string.format_carbs, carbsAfterConstraint) else "" + treatments_wizard_total.text = MainApp.gs(R.string.result_insulin_carbs, insulinText, carbsText) ok.visibility = View.VISIBLE } else { - treatments_wizard_total.text = MainApp.gs(R.string.missing) + " " + DecimalFormatter.to0Decimal(wizard.carbsEquivalent) + "g" + treatments_wizard_total.text = MainApp.gs(R.string.missing_carbs, wizard.carbsEquivalent.toInt()) ok.visibility = View.INVISIBLE } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventDismissBolusProgressIfRunning.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventDismissBolusProgressIfRunning.kt new file mode 100644 index 0000000000..53ab699d9c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventDismissBolusProgressIfRunning.kt @@ -0,0 +1,6 @@ +package info.nightscout.androidaps.plugins.general.overview.events + +import info.nightscout.androidaps.data.PumpEnactResult +import info.nightscout.androidaps.events.Event + +class EventDismissBolusProgressIfRunning(val result: PumpEnactResult?) : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventDismissBolusprogressIfRunning.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventDismissBolusprogressIfRunning.java deleted file mode 100644 index 2022a4d74b..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventDismissBolusprogressIfRunning.java +++ /dev/null @@ -1,16 +0,0 @@ -package info.nightscout.androidaps.plugins.general.overview.events; - -import info.nightscout.androidaps.data.PumpEnactResult; -import info.nightscout.androidaps.events.Event; - -/** - * Created by adrian on 20/02/17. - */ - -public class EventDismissBolusprogressIfRunning extends Event { - public final PumpEnactResult result; - - public EventDismissBolusprogressIfRunning(PumpEnactResult result) { - this.result = result; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventOverviewBolusProgress.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventOverviewBolusProgress.java deleted file mode 100644 index 3a7c50faac..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventOverviewBolusProgress.java +++ /dev/null @@ -1,27 +0,0 @@ -package info.nightscout.androidaps.plugins.general.overview.events; - -import info.nightscout.androidaps.plugins.treatments.Treatment; -import info.nightscout.androidaps.events.Event; - -public class EventOverviewBolusProgress extends Event { - public String status = ""; - public Treatment t = null; - public int percent = 0; - public int bolusId; - private static EventOverviewBolusProgress eventOverviewBolusProgress = null; - - public EventOverviewBolusProgress() { - } - - public boolean isSMB(){ - return (t != null) && t.isSMB; - } - - public static EventOverviewBolusProgress getInstance() { - if(eventOverviewBolusProgress == null) { - eventOverviewBolusProgress = new EventOverviewBolusProgress(); - } - return eventOverviewBolusProgress; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventOverviewBolusProgress.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventOverviewBolusProgress.kt new file mode 100644 index 0000000000..52b62790da --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/events/EventOverviewBolusProgress.kt @@ -0,0 +1,12 @@ +package info.nightscout.androidaps.plugins.general.overview.events + +import info.nightscout.androidaps.plugins.treatments.Treatment +import info.nightscout.androidaps.events.Event + +object EventOverviewBolusProgress : Event() { + var status = "" + var t: Treatment? = null + var percent = 0 + + fun isSMB(): Boolean = t?.isSMB ?: false +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.java index f46e5184cd..9685445ce0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.java @@ -172,7 +172,7 @@ public class NotificationStore { removeExpired(); unSnooze(); if (store.size() > 0) { - NotificationRecyclerViewAdapter adapter = new NotificationRecyclerViewAdapter(store); + NotificationRecyclerViewAdapter adapter = new NotificationRecyclerViewAdapter(cloneStore()); notificationsView.setAdapter(adapter); notificationsView.setVisibility(View.VISIBLE); } else { @@ -180,4 +180,9 @@ public class NotificationStore { } } + private synchronized List cloneStore() { + List clone = new ArrayList<>(store.size()); + clone.addAll(store); + return clone; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.java index 13310d70a8..007298ac76 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/DummyService.java @@ -1,26 +1,27 @@ package info.nightscout.androidaps.plugins.general.persistentNotification; -import android.app.Notification; import android.app.Service; import android.content.Intent; import android.os.IBinder; import androidx.annotation.Nullable; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; +import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Keeps AndroidAPS in foreground state, so it won't be terminated by Android nor get restricted by the background execution limits */ public class DummyService extends Service { private static Logger log = LoggerFactory.getLogger(L.CORE); + private CompositeDisposable disposable = new CompositeDisposable(); @Nullable @Override @@ -28,6 +29,30 @@ public class DummyService extends Service { return null; } + @Override + public void onCreate() { + super.onCreate(); + // TODO: I guess this was moved here in order to adhere to the 5 seconds rule to call "startForeground" after a Service was called as Foreground service? + // As onCreate() is not called every time a service is started, copied to onStartCommand(). + startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().getLastNotification()); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (L.isEnabled(L.PUMP)) log.debug("EventAppExit received"); + stopSelf(); + }, FabricPrivacy::logException) + ); + } + + @Override + public void onDestroy() { + if (L.isEnabled(L.CORE)) log.debug("onDestroy"); + disposable.clear(); + super.onDestroy(); + stopForeground(true); + } + @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); @@ -35,28 +60,4 @@ public class DummyService extends Service { return START_STICKY; } - @Subscribe - public void onStatusEvent(EventAppExit event) { - if (L.isEnabled(L.CORE)) - log.debug("EventAppExit received"); - - stopSelf(); - } - - @Override - public void onCreate() { - super.onCreate(); - // TODO: I guess this was moved here in order to adhere to the 5 seconds rule to call "startForeground" after a Service was called as Foreground service? - // As onCreate() is not called every time a service is started, copied to onStartCommand(). - startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().getLastNotification()); - MainApp.bus().register(this); - } - - @Override - public void onDestroy() { - if (L.isEnabled(L.CORE)) - log.debug("onDestroy"); - MainApp.bus().unregister(this); - stopForeground(true); - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java index 655435f400..fdee040682 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java @@ -15,10 +15,6 @@ import androidx.core.app.NotificationCompat; import androidx.core.app.RemoteInput; import androidx.core.app.TaskStackBuilder; -import com.squareup.otto.Subscribe; - -import javax.annotation.Nonnull; - import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; @@ -37,6 +33,7 @@ import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; @@ -44,6 +41,9 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorP import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Created by adrian on 23/12/16. @@ -51,6 +51,8 @@ import info.nightscout.androidaps.utils.DecimalFormatter; public class PersistentNotificationPlugin extends PluginBase { + private CompositeDisposable disposable = new CompositeDisposable(); + private static PersistentNotificationPlugin plugin; private Notification notification; @@ -91,7 +93,54 @@ public class PersistentNotificationPlugin extends PluginBase { protected void onStart() { super.onStart(); createNotificationChannel(); // make sure channels exist before triggering updates through the bus - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventRefreshOverview.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventExtendedBolusChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventTempBasalChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventTreatmentChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventInitializationChanged.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventNewBasalProfile.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); triggerNotificationUpdate(true); } @@ -109,7 +158,7 @@ public class PersistentNotificationPlugin extends PluginBase { @Override protected void onStop() { - MainApp.bus().unregister(this); + disposable.clear(); MainApp.instance().stopService(new Intent(MainApp.instance(), DummyService.class)); super.onStop(); } @@ -285,46 +334,4 @@ public class PersistentNotificationPlugin extends PluginBase { throw new IllegalStateException("Notification is null"); } } - - - @Subscribe - public void onStatusEvent(final EventPreferenceChange ev) { - triggerNotificationUpdate(false); - } - - @Subscribe - public void onStatusEvent(final EventTreatmentChange ev) { - triggerNotificationUpdate(false); - } - - @Subscribe - public void onStatusEvent(final EventTempBasalChange ev) { - triggerNotificationUpdate(false); - } - - @Subscribe - public void onStatusEvent(final EventExtendedBolusChange ev) { - triggerNotificationUpdate(false); - } - - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - triggerNotificationUpdate(false); - } - - @Subscribe - public void onStatusEvent(final EventNewBasalProfile ev) { - triggerNotificationUpdate(false); - } - - @Subscribe - public void onStatusEvent(final EventInitializationChanged ev) { - triggerNotificationUpdate(false); - } - - @Subscribe - public void onStatusEvent(final EventRefreshOverview ev) { - triggerNotificationUpdate(false); - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/AuthRequest.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/AuthRequest.java deleted file mode 100644 index 4b05072eb1..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/AuthRequest.java +++ /dev/null @@ -1,59 +0,0 @@ -package info.nightscout.androidaps.plugins.general.smsCommunicator; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.utils.DateUtil; - -class AuthRequest { - private static Logger log = LoggerFactory.getLogger(L.SMS); - - Sms requester; - String confirmCode; - private Runnable action; - - private long date; - - private boolean processed; - private SmsCommunicatorPlugin plugin; - - AuthRequest(SmsCommunicatorPlugin plugin, Sms requester, String requestText, String confirmCode, SmsAction action) { - this.requester = requester; - this.confirmCode = confirmCode; - this.action = action; - this.plugin = plugin; - - this.date = DateUtil.now(); - - plugin.sendSMS(new Sms(requester.phoneNumber, requestText)); - } - - void action(String codeReceived) { - if (processed) { - if (L.isEnabled(L.SMS)) - log.debug("Already processed"); - return; - } - if (!confirmCode.equals(codeReceived)) { - processed = true; - if (L.isEnabled(L.SMS)) - log.debug("Wrong code"); - plugin.sendSMS(new Sms(requester.phoneNumber, R.string.sms_wrongcode)); - return; - } - if (DateUtil.now() - date < Constants.SMS_CONFIRM_TIMEOUT) { - processed = true; - if (L.isEnabled(L.SMS)) - log.debug("Processing confirmed SMS: " + requester.text); - if (action != null) - action.run(); - return; - } - if (L.isEnabled(L.SMS)) - log.debug("Timed out SMS: " + requester.text); - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/AuthRequest.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/AuthRequest.kt new file mode 100644 index 0000000000..48e816928f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/AuthRequest.kt @@ -0,0 +1,38 @@ +package info.nightscout.androidaps.plugins.general.smsCommunicator + +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.R +import info.nightscout.androidaps.logging.L +import info.nightscout.androidaps.utils.DateUtil +import org.slf4j.LoggerFactory + +class AuthRequest internal constructor(val plugin: SmsCommunicatorPlugin, var requester: Sms, requestText: String, var confirmCode: String, val action: SmsAction) { + private val log = LoggerFactory.getLogger(L.SMS) + + private val date = DateUtil.now() + private var processed = false + + init { + plugin.sendSMS(Sms(requester.phoneNumber, requestText)) + } + + fun action(codeReceived: String) { + if (processed) { + if (L.isEnabled(L.SMS)) log.debug("Already processed") + return + } + if (confirmCode != codeReceived) { + processed = true + if (L.isEnabled(L.SMS)) log.debug("Wrong code") + plugin.sendSMS(Sms(requester.phoneNumber, R.string.sms_wrongcode)) + return + } + if (DateUtil.now() - date < Constants.SMS_CONFIRM_TIMEOUT) { + processed = true + if (L.isEnabled(L.SMS)) log.debug("Processing confirmed SMS: " + requester.text) + action.run() + return + } + if (L.isEnabled(L.SMS)) log.debug("Timed out SMS: " + requester.text) + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/Sms.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/Sms.java deleted file mode 100644 index 2eedfa0a51..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/Sms.java +++ /dev/null @@ -1,42 +0,0 @@ -package info.nightscout.androidaps.plugins.general.smsCommunicator; - -import android.telephony.SmsMessage; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.utils.DateUtil; - -class Sms { - String phoneNumber; - String text; - long date; - boolean received = false; - boolean sent = false; - boolean processed = false; - boolean ignored = false; - - Sms(SmsMessage message) { - phoneNumber = message.getOriginatingAddress(); - text = message.getMessageBody(); - date = message.getTimestampMillis(); - received = true; - } - - Sms(String phoneNumber, String text) { - this.phoneNumber = phoneNumber; - this.text = text; - this.date = DateUtil.now(); - sent = true; - } - - Sms(String phoneNumber, int textId) { - this.phoneNumber = phoneNumber; - this.text = MainApp.gs(textId); - this.date = DateUtil.now(); - sent = true; - } - - public String toString() { - return "SMS from " + phoneNumber + ": " + text; - } -} - diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/Sms.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/Sms.kt new file mode 100644 index 0000000000..868620be2e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/Sms.kt @@ -0,0 +1,40 @@ +package info.nightscout.androidaps.plugins.general.smsCommunicator + +import android.telephony.SmsMessage +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.utils.DateUtil + +class Sms { + var phoneNumber: String + var text: String + var date: Long + var received = false + var sent = false + var processed = false + var ignored = false + + internal constructor(message: SmsMessage) { + phoneNumber = message.originatingAddress ?: "" + text = message.messageBody + date = message.timestampMillis + received = true + } + + internal constructor(phoneNumber: String, text: String) { + this.phoneNumber = phoneNumber + this.text = text + date = DateUtil.now() + sent = true + } + + internal constructor(phoneNumber: String, textId: Int) { + this.phoneNumber = phoneNumber + text = MainApp.gs(textId) + date = DateUtil.now() + sent = true + } + + override fun toString(): String { + return "SMS from $phoneNumber: $text" + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsAction.java deleted file mode 100644 index 6b5d5b8747..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsAction.java +++ /dev/null @@ -1,33 +0,0 @@ -package info.nightscout.androidaps.plugins.general.smsCommunicator; - -abstract class SmsAction implements Runnable { - Double aDouble; - Integer anInteger; - Integer secondInteger; - String aString; - - SmsAction() {} - - SmsAction(Double aDouble) { - this.aDouble = aDouble; - } - - SmsAction(Double aDouble, Integer secondInteger) { - this.aDouble = aDouble; - this.secondInteger = secondInteger; - } - - SmsAction(String aString, Integer secondInteger) { - this.aString = aString; - this.secondInteger = secondInteger; - } - - SmsAction(Integer anInteger) { - this.anInteger = anInteger; - } - - SmsAction(Integer anInteger, Integer secondInteger) { - this.anInteger = anInteger; - this.secondInteger = secondInteger; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsAction.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsAction.kt new file mode 100644 index 0000000000..98c892d918 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsAction.kt @@ -0,0 +1,68 @@ +package info.nightscout.androidaps.plugins.general.smsCommunicator + +abstract class SmsAction : Runnable { + var aDouble: Double? = null + var anInteger: Int? = null + var secondInteger: Int? = null + var secondLong: Long? = null + var aString: String? = null + + internal constructor() + internal constructor(aDouble: Double) { + this.aDouble = aDouble + } + + internal constructor(aDouble: Double, secondInteger: Int) { + this.aDouble = aDouble + this.secondInteger = secondInteger + } + + internal constructor(aString: String, secondInteger: Int) { + this.aString = aString + this.secondInteger = secondInteger + } + + internal constructor(anInteger: Int) { + this.anInteger = anInteger + } + + internal constructor(anInteger: Int, secondInteger: Int) { + this.anInteger = anInteger + this.secondInteger = secondInteger + } + + internal constructor(anInteger: Int, secondLong: Long) { + this.anInteger = anInteger + this.secondLong = secondLong + } + + fun aDouble(): Double { + return aDouble?.let { + aDouble + } ?: throw IllegalStateException() + } + + fun anInteger(): Int { + return anInteger?.let { + anInteger + } ?: throw IllegalStateException() + } + + fun secondInteger(): Int { + return secondInteger?.let { + secondInteger + } ?: throw IllegalStateException() + } + + fun secondLong(): Long { + return secondLong?.let { + secondLong + } ?: throw IllegalStateException() + } + + fun aString(): String { + return aString?.let { + aString + } ?: throw IllegalStateException() + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorFragment.java deleted file mode 100644 index d1c4adf5fe..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorFragment.java +++ /dev/null @@ -1,73 +0,0 @@ -package info.nightscout.androidaps.plugins.general.smsCommunicator; - - -import android.app.Activity; -import android.os.Bundle; -import android.text.Html; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import com.squareup.otto.Subscribe; - -import java.util.Collections; -import java.util.Comparator; - -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; -import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSmsCommunicatorUpdateGui; -import info.nightscout.androidaps.utils.DateUtil; - -public class SmsCommunicatorFragment extends SubscriberFragment { - TextView logView; - - public SmsCommunicatorFragment() { - super(); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.smscommunicator_fragment, container, false); - - logView = (TextView) view.findViewById(R.id.smscommunicator_log); - - return view; - } - - @Subscribe - public void onStatusEvent(final EventSmsCommunicatorUpdateGui ev) { - updateGUI(); - } - - @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - class CustomComparator implements Comparator { - public int compare(Sms object1, Sms object2) { - return (int) (object1.date - object2.date); - } - } - Collections.sort(SmsCommunicatorPlugin.getPlugin().messages, new CustomComparator()); - int messagesToShow = 40; - - int start = Math.max(0, SmsCommunicatorPlugin.getPlugin().messages.size() - messagesToShow); - - String logText = ""; - for (int x = start; x < SmsCommunicatorPlugin.getPlugin().messages.size(); x++) { - Sms sms = SmsCommunicatorPlugin.getPlugin().messages.get(x); - if (sms.ignored) { - logText += DateUtil.timeString(sms.date) + " <<< " + "░ " + sms.phoneNumber + " " + sms.text + "
"; - } else if (sms.received) { - logText += DateUtil.timeString(sms.date) + " <<< " + (sms.processed ? "● " : "○ ") + sms.phoneNumber + " " + sms.text + "
"; - } else if (sms.sent) { - logText += DateUtil.timeString(sms.date) + " >>> " + (sms.processed ? "● " : "○ ") + sms.phoneNumber + " " + sms.text + "
"; - } - } - logView.setText(Html.fromHtml(logText)); - }); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorFragment.kt new file mode 100644 index 0000000000..90ec078931 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorFragment.kt @@ -0,0 +1,70 @@ +package info.nightscout.androidaps.plugins.general.smsCommunicator + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.bus.RxBus.toObservable +import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSmsCommunicatorUpdateGui +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.FabricPrivacy +import info.nightscout.androidaps.utils.HtmlHelper +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.CompositeDisposable +import kotlinx.android.synthetic.main.smscommunicator_fragment.* +import java.util.* +import kotlin.math.max + +class SmsCommunicatorFragment : Fragment() { + private val disposable = CompositeDisposable() + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + return inflater.inflate(R.layout.smscommunicator_fragment, container, false) + } + + @Synchronized + override fun onResume() { + super.onResume() + disposable.add(toObservable(EventSmsCommunicatorUpdateGui::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGui() }) { FabricPrivacy.logException(it) } + ) + updateGui() + } + + @Synchronized + override fun onPause() { + super.onPause() + disposable.clear() + } + + fun updateGui() { + class CustomComparator : Comparator { + override fun compare(object1: Sms, object2: Sms): Int { + return (object1.date - object2.date).toInt() + } + } + Collections.sort(SmsCommunicatorPlugin.messages, CustomComparator()) + val messagesToShow = 40 + val start = max(0, SmsCommunicatorPlugin.messages.size - messagesToShow) + var logText = "" + for (x in start until SmsCommunicatorPlugin.messages.size) { + val sms = SmsCommunicatorPlugin.messages[x] + when { + sms.ignored -> { + logText += DateUtil.timeString(sms.date) + " <<< " + "░ " + sms.phoneNumber + " " + sms.text + "
" + } + sms.received -> { + logText += DateUtil.timeString(sms.date) + " <<< " + (if (sms.processed) "● " else "○ ") + sms.phoneNumber + " " + sms.text + "
" + } + sms.sent -> { + logText += DateUtil.timeString(sms.date) + " >>> " + (if (sms.processed) "● " else "○ ") + sms.phoneNumber + " " + sms.text + "
" + } + } + } + smscommunicator_log?.text = HtmlHelper.fromHtml(logText) + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java deleted file mode 100644 index d1494e6033..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.java +++ /dev/null @@ -1,807 +0,0 @@ -package info.nightscout.androidaps.plugins.general.smsCommunicator; - -import android.content.Intent; -import android.os.Bundle; -import android.telephony.SmsManager; -import android.telephony.SmsMessage; - -import com.squareup.otto.Subscribe; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.text.Normalizer; -import java.util.ArrayList; -import java.util.List; - -import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.DetailedBolusInfo; -import info.nightscout.androidaps.data.IobTotal; -import info.nightscout.androidaps.data.Profile; -import info.nightscout.androidaps.data.ProfileStore; -import info.nightscout.androidaps.db.BgReading; -import info.nightscout.androidaps.db.DatabaseHelper; -import info.nightscout.androidaps.db.Source; -import info.nightscout.androidaps.events.EventPreferenceChange; -import info.nightscout.androidaps.events.EventRefreshOverview; -import info.nightscout.androidaps.interfaces.Constraint; -import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.PluginDescription; -import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.interfaces.ProfileInterface; -import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; -import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart; -import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; -import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; -import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSmsCommunicatorUpdateGui; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.CobInfo; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; -import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.queue.Callback; -import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.DecimalFormatter; -import info.nightscout.androidaps.utils.SP; -import info.nightscout.androidaps.utils.SafeParse; -import info.nightscout.androidaps.utils.XdripCalibrations; - -/** - * Created by mike on 05.08.2016. - */ -public class SmsCommunicatorPlugin extends PluginBase { - private static Logger log = LoggerFactory.getLogger(L.SMS); - - private static SmsCommunicatorPlugin smsCommunicatorPlugin; - - public static SmsCommunicatorPlugin getPlugin() { - - if (smsCommunicatorPlugin == null) { - smsCommunicatorPlugin = new SmsCommunicatorPlugin(); - } - return smsCommunicatorPlugin; - } - - List allowedNumbers = new ArrayList<>(); - - AuthRequest messageToConfirm = null; - - long lastRemoteBolusTime = 0; - - ArrayList messages = new ArrayList<>(); - - SmsCommunicatorPlugin() { - super(new PluginDescription() - .mainType(PluginType.GENERAL) - .fragmentClass(SmsCommunicatorFragment.class.getName()) - .pluginName(R.string.smscommunicator) - .shortName(R.string.smscommunicator_shortname) - .preferencesId(R.xml.pref_smscommunicator) - .description(R.string.description_sms_communicator) - ); - processSettings(null); - } - - @Override - protected void onStart() { - MainApp.bus().register(this); - super.onStart(); - } - - @Override - protected void onStop() { - MainApp.bus().unregister(this); - super.onStop(); - } - - @Subscribe - public void processSettings(final EventPreferenceChange ev) { - if (ev == null || ev.isChanged(R.string.key_smscommunicator_allowednumbers)) { - String settings = SP.getString(R.string.key_smscommunicator_allowednumbers, ""); - - allowedNumbers.clear(); - String[] substrings = settings.split(";"); - for (String number : substrings) { - String cleaned = number.replaceAll("\\s+", ""); - allowedNumbers.add(cleaned); - log.debug("Found allowed number: " + cleaned); - } - } - } - - boolean isCommand(String command, String number) { - switch (command.toUpperCase()) { - case "BG": - case "LOOP": - case "TREATMENTS": - case "NSCLIENT": - case "PUMP": - case "BASAL": - case "BOLUS": - case "EXTENDED": - case "CAL": - case "PROFILE": - return true; - } - if (messageToConfirm != null && messageToConfirm.requester.phoneNumber.equals(number)) - return true; - return false; - } - - boolean isAllowedNumber(String number) { - for (String num : allowedNumbers) { - if (num.equals(number)) return true; - } - return false; - } - - public void handleNewData(Intent intent) { - Bundle bundle = intent.getExtras(); - if (bundle == null) return; - - Object[] pdus = (Object[]) bundle.get("pdus"); - if (pdus != null) { - // For every SMS message received - for (Object pdu : pdus) { - SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu); - processSms(new Sms(message)); - } - } - } - - void processSms(final Sms receivedSms) { - if (!isEnabled(PluginType.GENERAL)) { - log.debug("Ignoring SMS. Plugin disabled."); - return; - } - if (!isAllowedNumber(receivedSms.phoneNumber)) { - log.debug("Ignoring SMS from: " + receivedSms.phoneNumber + ". Sender not allowed"); - receivedSms.ignored = true; - messages.add(receivedSms); - MainApp.bus().post(new EventSmsCommunicatorUpdateGui()); - return; - } - - messages.add(receivedSms); - log.debug(receivedSms.toString()); - - String[] splitted = receivedSms.text.split("\\s+"); - boolean remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false); - - if (splitted.length > 0 && isCommand(splitted[0].toUpperCase(), receivedSms.phoneNumber)) { - switch (splitted[0].toUpperCase()) { - case "BG": - processBG(splitted, receivedSms); - break; - case "LOOP": - if (!remoteCommandsAllowed) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)); - else if (splitted.length == 2 || splitted.length == 3) - processLOOP(splitted, receivedSms); - else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - break; - case "TREATMENTS": - if (splitted.length == 2) - processTREATMENTS(splitted, receivedSms); - else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - break; - case "NSCLIENT": - if (splitted.length == 2) - processNSCLIENT(splitted, receivedSms); - else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - break; - case "PUMP": - processPUMP(splitted, receivedSms); - break; - case "PROFILE": - if (!remoteCommandsAllowed) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)); - else if (splitted.length == 2 || splitted.length == 3) - processPROFILE(splitted, receivedSms); - else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - break; - case "BASAL": - if (!remoteCommandsAllowed) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)); - else if (splitted.length == 2 || splitted.length == 3) - processBASAL(splitted, receivedSms); - else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - break; - case "EXTENDED": - if (!remoteCommandsAllowed) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)); - else if (splitted.length == 2 || splitted.length == 3) - processEXTENDED(splitted, receivedSms); - else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - break; - case "BOLUS": - if (!remoteCommandsAllowed) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)); - else if (splitted.length == 2 && DateUtil.now() - lastRemoteBolusTime < Constants.remoteBolusMinDistance) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotebolusnotallowed)); - else if (splitted.length == 2 && ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended()) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.pumpsuspended)); - else if (splitted.length == 2) - processBOLUS(splitted, receivedSms); - else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - break; - case "CAL": - if (!remoteCommandsAllowed) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)); - else if (splitted.length == 2) - processCAL(splitted, receivedSms); - else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - break; - default: // expect passCode here - if (messageToConfirm != null && messageToConfirm.requester.phoneNumber.equals(receivedSms.phoneNumber)) { - messageToConfirm.action(splitted[0]); - messageToConfirm = null; - } else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_unknowncommand)); - break; - } - } - - MainApp.bus().post(new EventSmsCommunicatorUpdateGui()); - } - - @SuppressWarnings("unused") - private void processBG(String[] splitted, Sms receivedSms) { - BgReading actualBG = DatabaseHelper.actualBg(); - BgReading lastBG = DatabaseHelper.lastBg(); - - String reply = ""; - - String units = ProfileFunctions.getInstance().getProfileUnits(); - - if (actualBG != null) { - reply = MainApp.gs(R.string.sms_actualbg) + " " + actualBG.valueToUnitsToString(units) + ", "; - } else if (lastBG != null) { - Long agoMsec = System.currentTimeMillis() - lastBG.date; - int agoMin = (int) (agoMsec / 60d / 1000d); - reply = MainApp.gs(R.string.sms_lastbg) + " " + lastBG.valueToUnitsToString(units) + " " + String.format(MainApp.gs(R.string.sms_minago), agoMin) + ", "; - } - GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); - if (glucoseStatus != null) - reply += MainApp.gs(R.string.sms_delta) + " " + Profile.toUnitsString(glucoseStatus.delta, glucoseStatus.delta * Constants.MGDL_TO_MMOLL, units) + " " + units + ", "; - - TreatmentsPlugin.getPlugin().updateTotalIOBTreatments(); - IobTotal bolusIob = TreatmentsPlugin.getPlugin().getLastCalculationTreatments().round(); - TreatmentsPlugin.getPlugin().updateTotalIOBTempBasals(); - IobTotal basalIob = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals().round(); - - String cobText = MainApp.gs(R.string.value_unavailable_short); - CobInfo cobInfo = IobCobCalculatorPlugin.getPlugin().getCobInfo(false, "SMS COB"); - - reply += MainApp.gs(R.string.sms_iob) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U (" - + MainApp.gs(R.string.sms_bolus) + " " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " - + MainApp.gs(R.string.sms_basal) + " " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U), " - + MainApp.gs(R.string.cob) + ": " + cobInfo.generateCOBString(); - - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - receivedSms.processed = true; - } - - private void processLOOP(String[] splitted, Sms receivedSms) { - String reply; - switch (splitted[1].toUpperCase()) { - case "DISABLE": - case "STOP": - LoopPlugin loopPlugin = MainApp.getSpecificPlugin(LoopPlugin.class); - if (loopPlugin != null && loopPlugin.isEnabled(PluginType.LOOP)) { - loopPlugin.setPluginEnabled(PluginType.LOOP, false); - ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() { - @Override - public void run() { - MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_STOP")); - String reply = MainApp.gs(R.string.smscommunicator_loophasbeendisabled) + " " + - MainApp.gs(result.success ? R.string.smscommunicator_tempbasalcanceled : R.string.smscommunicator_tempbasalcancelfailed); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - }); - } else { - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopisdisabled)); - } - receivedSms.processed = true; - break; - case "ENABLE": - case "START": - loopPlugin = MainApp.getSpecificPlugin(LoopPlugin.class); - if (loopPlugin != null && !loopPlugin.isEnabled(PluginType.LOOP)) { - loopPlugin.setPluginEnabled(PluginType.LOOP, true); - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_loophasbeenenabled)); - MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_START")); - } else { - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopisenabled)); - } - receivedSms.processed = true; - break; - case "STATUS": - loopPlugin = MainApp.getSpecificPlugin(LoopPlugin.class); - if (loopPlugin != null) { - if (loopPlugin.isEnabled(PluginType.LOOP)) { - if (loopPlugin.isSuspended()) - reply = String.format(MainApp.gs(R.string.loopsuspendedfor), loopPlugin.minutesToEndOfSuspend()); - else - reply = MainApp.gs(R.string.smscommunicator_loopisenabled); - } else { - reply = MainApp.gs(R.string.smscommunicator_loopisdisabled); - } - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - receivedSms.processed = true; - break; - case "RESUME": - LoopPlugin.getPlugin().suspendTo(0); - MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_RESUME")); - NSUpload.uploadOpenAPSOffline(0); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopresumed)); - break; - case "SUSPEND": - int duration = 0; - if (splitted.length == 3) - duration = SafeParse.stringToInt(splitted[2]); - duration = Math.max(0, duration); - duration = Math.min(180, duration); - if (duration == 0) { - receivedSms.processed = true; - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_wrongduration)); - return; - } else { - String passCode = generatePasscode(); - reply = String.format(MainApp.gs(R.string.smscommunicator_suspendreplywithcode), duration, passCode); - receivedSms.processed = true; - messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(duration) { - @Override - public void run() { - ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() { - @Override - public void run() { - if (result.success) { - LoopPlugin.getPlugin().suspendTo(System.currentTimeMillis() + anInteger * 60L * 1000); - NSUpload.uploadOpenAPSOffline(anInteger * 60); - MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_SUSPENDED")); - String reply = MainApp.gs(R.string.smscommunicator_loopsuspended) + " " + - MainApp.gs(result.success ? R.string.smscommunicator_tempbasalcanceled : R.string.smscommunicator_tempbasalcancelfailed); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); - } else { - String reply = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - } - }); - - } - }); - } - break; - default: - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - break; - } - } - - private void processTREATMENTS(String[] splitted, Sms receivedSms) { - if (splitted[1].toUpperCase().equals("REFRESH")) { - TreatmentsPlugin.getPlugin().getService().resetTreatments(); - MainApp.bus().post(new EventNSClientRestart()); - sendSMS(new Sms(receivedSms.phoneNumber, "TREATMENTS REFRESH SENT")); - receivedSms.processed = true; - } else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - } - - private void processNSCLIENT(String[] splitted, Sms receivedSms) { - if (splitted[1].toUpperCase().equals("RESTART")) { - MainApp.bus().post(new EventNSClientRestart()); - sendSMS(new Sms(receivedSms.phoneNumber, "NSCLIENT RESTART SENT")); - receivedSms.processed = true; - } else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - } - - @SuppressWarnings("unused") - private void processPUMP(String[] splitted, Sms receivedSms) { - ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("SMS", new Callback() { - @Override - public void run() { - PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); - if (result.success) { - if (pump != null) { - String reply = pump.shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - } else { - String reply = MainApp.gs(R.string.readstatusfailed); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - } - }); - receivedSms.processed = true; - } - - private void processPROFILE(String[] splitted, Sms receivedSms) { - // load profiles - ProfileInterface anInterface = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface(); - if (anInterface == null) { - sendSMS(new Sms(receivedSms.phoneNumber, R.string.notconfigured)); - receivedSms.processed = true; - return; - } - ProfileStore store = anInterface.getProfile(); - if (store == null) { - sendSMS(new Sms(receivedSms.phoneNumber, R.string.notconfigured)); - receivedSms.processed = true; - return; - } - final ArrayList list = store.getProfileList(); - - if (splitted[1].toUpperCase().equals("STATUS")) { - sendSMS(new Sms(receivedSms.phoneNumber, ProfileFunctions.getInstance().getProfileName())); - } else if (splitted[1].toUpperCase().equals("LIST")) { - if (list.isEmpty()) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.invalidprofile)); - else { - String reply = ""; - for (int i = 0; i < list.size(); i++) { - if (i > 0) - reply += "\n"; - reply += (i + 1) + ". "; - reply += list.get(i); - } - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - } else { - - int pindex = SafeParse.stringToInt(splitted[1]); - int percentage = 100; - if (splitted.length > 2) - percentage = SafeParse.stringToInt(splitted[2]); - - if (pindex > list.size()) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - else if (percentage == 0) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - else if (pindex == 0) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - else { - final Profile profile = store.getSpecificProfile((String) list.get(pindex - 1)); - if (profile == null) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.noprofile)); - else { - String passCode = generatePasscode(); - String reply = String.format(MainApp.gs(R.string.smscommunicator_profilereplywithcode), list.get(pindex - 1), percentage, passCode); - receivedSms.processed = true; - int finalPercentage = percentage; - messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction((String) list.get(pindex - 1), finalPercentage) { - @Override - public void run() { - ProfileFunctions.doProfileSwitch(store, (String) list.get(pindex - 1), 0, finalPercentage, 0); - sendSMS(new Sms(receivedSms.phoneNumber, R.string.profileswitchcreated)); - } - }); - } - } - } - receivedSms.processed = true; - } - - private void processBASAL(String[] splitted, Sms receivedSms) { - if (splitted[1].toUpperCase().equals("CANCEL") || splitted[1].toUpperCase().equals("STOP")) { - String passCode = generatePasscode(); - String reply = String.format(MainApp.gs(R.string.smscommunicator_basalstopreplywithcode), passCode); - receivedSms.processed = true; - messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction() { - @Override - public void run() { - ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() { - @Override - public void run() { - if (result.success) { - String reply = MainApp.gs(R.string.smscommunicator_tempbasalcanceled); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); - } else { - String reply = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - } - }); - } - }); - } else if (splitted[1].endsWith("%")) { - int tempBasalPct = SafeParse.stringToInt(StringUtils.removeEnd(splitted[1], "%")); - int duration = 30; - if (splitted.length > 2) - duration = SafeParse.stringToInt(splitted[2]); - final Profile profile = ProfileFunctions.getInstance().getProfile(); - - if (profile == null) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.noprofile)); - else if (tempBasalPct == 0 && !splitted[1].equals("0%")) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - else if (duration == 0) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - else { - tempBasalPct = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(tempBasalPct), profile).value(); - String passCode = generatePasscode(); - String reply = String.format(MainApp.gs(R.string.smscommunicator_basalpctreplywithcode), tempBasalPct, duration, passCode); - receivedSms.processed = true; - messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(tempBasalPct, duration) { - @Override - public void run() { - ConfigBuilderPlugin.getPlugin().getCommandQueue().tempBasalPercent(anInteger, secondInteger, true, profile, new Callback() { - @Override - public void run() { - if (result.success) { - String reply; - if (result.isPercent) - reply = String.format(MainApp.gs(R.string.smscommunicator_tempbasalset_percent), result.percent, result.duration); - else - reply = String.format(MainApp.gs(R.string.smscommunicator_tempbasalset), result.absolute, result.duration); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); - } else { - String reply = MainApp.gs(R.string.smscommunicator_tempbasalfailed); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - } - }); - } - }); - } - } else { - Double tempBasal = SafeParse.stringToDouble(splitted[1]); - int duration = 30; - if (splitted.length > 2) - duration = SafeParse.stringToInt(splitted[2]); - final Profile profile = ProfileFunctions.getInstance().getProfile(); - if (profile == null) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.noprofile)); - else if (tempBasal == 0 && !splitted[1].equals("0")) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - else if (duration == 0) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - else { - tempBasal = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(tempBasal), profile).value(); - String passCode = generatePasscode(); - String reply = String.format(MainApp.gs(R.string.smscommunicator_basalreplywithcode), tempBasal, duration, passCode); - receivedSms.processed = true; - messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(tempBasal, duration) { - @Override - public void run() { - ConfigBuilderPlugin.getPlugin().getCommandQueue().tempBasalAbsolute(aDouble, secondInteger, true, profile, new Callback() { - @Override - public void run() { - if (result.success) { - String reply; - if (result.isPercent) - reply = String.format(MainApp.gs(R.string.smscommunicator_tempbasalset_percent), result.percent, result.duration); - else - reply = String.format(MainApp.gs(R.string.smscommunicator_tempbasalset), result.absolute, result.duration); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); - } else { - String reply = MainApp.gs(R.string.smscommunicator_tempbasalfailed); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - } - }); - } - }); - } - } - } - - private void processEXTENDED(String[] splitted, Sms receivedSms) { - if (splitted[1].toUpperCase().equals("CANCEL") || splitted[1].toUpperCase().equals("STOP")) { - String passCode = generatePasscode(); - String reply = String.format(MainApp.gs(R.string.smscommunicator_extendedstopreplywithcode), passCode); - receivedSms.processed = true; - messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction() { - @Override - public void run() { - ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelExtended(new Callback() { - @Override - public void run() { - if (result.success) { - String reply = MainApp.gs(R.string.smscommunicator_extendedcanceled); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); - } else { - String reply = MainApp.gs(R.string.smscommunicator_extendedcancelfailed); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - } - }); - } - }); - } else if (splitted.length != 3) { - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - } else { - Double extended = SafeParse.stringToDouble(splitted[1]); - int duration = SafeParse.stringToInt(splitted[2]); - extended = MainApp.getConstraintChecker().applyExtendedBolusConstraints(new Constraint<>(extended)).value(); - if (extended == 0 || duration == 0) - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - else { - String passCode = generatePasscode(); - String reply = String.format(MainApp.gs(R.string.smscommunicator_extendedreplywithcode), extended, duration, passCode); - receivedSms.processed = true; - messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(extended, duration) { - @Override - public void run() { - ConfigBuilderPlugin.getPlugin().getCommandQueue().extendedBolus(aDouble, secondInteger, new Callback() { - @Override - public void run() { - if (result.success) { - String reply = String.format(MainApp.gs(R.string.smscommunicator_extendedset), aDouble, duration); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); - } else { - String reply = MainApp.gs(R.string.smscommunicator_extendedfailed); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - } - }); - } - }); - } - } - } - - - private void processBOLUS(String[] splitted, Sms receivedSms) { - Double bolus = SafeParse.stringToDouble(splitted[1]); - bolus = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(bolus)).value(); - if (bolus > 0d) { - String passCode = generatePasscode(); - String reply = String.format(MainApp.gs(R.string.smscommunicator_bolusreplywithcode), bolus, passCode); - receivedSms.processed = true; - messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(bolus) { - @Override - public void run() { - DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); - detailedBolusInfo.insulin = aDouble; - detailedBolusInfo.source = Source.USER; - ConfigBuilderPlugin.getPlugin().getCommandQueue().bolus(detailedBolusInfo, new Callback() { - @Override - public void run() { - final boolean resultSuccess = result.success; - final double resultBolusDelivered = result.bolusDelivered; - ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("SMS", new Callback() { - @Override - public void run() { - PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); - if (resultSuccess) { - String reply = String.format(MainApp.gs(R.string.smscommunicator_bolusdelivered), resultBolusDelivered); - if (pump != null) - reply += "\n" + pump.shortStatus(true); - lastRemoteBolusTime = DateUtil.now(); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); - } else { - String reply = MainApp.gs(R.string.smscommunicator_bolusfailed); - if (pump != null) - reply += "\n" + pump.shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply)); - } - } - }); - } - }); - } - }); - } else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - } - - private void processCAL(String[] splitted, Sms receivedSms) { - Double cal = SafeParse.stringToDouble(splitted[1]); - if (cal > 0d) { - String passCode = generatePasscode(); - String reply = String.format(MainApp.gs(R.string.smscommunicator_calibrationreplywithcode), cal, passCode); - receivedSms.processed = true; - messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(cal) { - @Override - public void run() { - boolean result = XdripCalibrations.sendIntent(aDouble); - if (result) - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_calibrationsent)); - else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.smscommunicator_calibrationfailed)); - } - }); - } else - sendSMS(new Sms(receivedSms.phoneNumber, R.string.wrongformat)); - } - - public boolean sendNotificationToAllNumbers(String text) { - boolean result = true; - for (int i = 0; i < allowedNumbers.size(); i++) { - Sms sms = new Sms(allowedNumbers.get(i), text); - result = result && sendSMS(sms); - } - return result; - } - - private void sendSMSToAllNumbers(Sms sms) { - for (String number : allowedNumbers) { - sms.phoneNumber = number; - sendSMS(sms); - } - } - - boolean sendSMS(Sms sms) { - SmsManager smsManager = SmsManager.getDefault(); - sms.text = stripAccents(sms.text); - - try { - if (L.isEnabled(L.SMS)) - log.debug("Sending SMS to " + sms.phoneNumber + ": " + sms.text); - if (sms.text.getBytes().length <= 140) - smsManager.sendTextMessage(sms.phoneNumber, null, sms.text, null, null); - else { - ArrayList parts = smsManager.divideMessage(sms.text); - smsManager.sendMultipartTextMessage(sms.phoneNumber, null, parts, - null, null); - } - - messages.add(sms); - } catch (IllegalArgumentException e) { - if (e.getMessage().equals("Invalid message body")) { - Notification notification = new Notification(Notification.INVALID_MESSAGE_BODY, MainApp.gs(R.string.smscommunicator_messagebody), Notification.NORMAL); - RxBus.INSTANCE.send(new EventNewNotification(notification)); - return false; - } else { - Notification notification = new Notification(Notification.INVALID_PHONE_NUMBER, MainApp.gs(R.string.smscommunicator_invalidphonennumber), Notification.NORMAL); - RxBus.INSTANCE.send(new EventNewNotification(notification)); - return false; - } - } catch (java.lang.SecurityException e) { - Notification notification = new Notification(Notification.MISSING_SMS_PERMISSION, MainApp.gs(R.string.smscommunicator_missingsmspermission), Notification.NORMAL); - RxBus.INSTANCE.send(new EventNewNotification(notification)); - return false; - } - MainApp.bus().post(new EventSmsCommunicatorUpdateGui()); - return true; - } - - private String generatePasscode() { - int startChar1 = 'A'; // on iphone 1st char is uppercase :) - String passCode = Character.toString((char) (startChar1 + Math.random() * ('z' - 'a' + 1))); - int startChar2 = Math.random() > 0.5 ? 'a' : 'A'; - passCode += Character.toString((char) (startChar2 + Math.random() * ('z' - 'a' + 1))); - int startChar3 = Math.random() > 0.5 ? 'a' : 'A'; - passCode += Character.toString((char) (startChar3 + Math.random() * ('z' - 'a' + 1))); - passCode = passCode.replace('l', 'k').replace('I', 'J'); - return passCode; - } - - private static String stripAccents(String s) { - s = Normalizer.normalize(s, Normalizer.Form.NFD); - s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", ""); - return s; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt new file mode 100644 index 0000000000..e40ed8b1df --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -0,0 +1,922 @@ +package info.nightscout.androidaps.plugins.general.smsCommunicator + +import android.content.Intent +import android.preference.EditTextPreference +import android.preference.Preference +import android.preference.Preference.OnPreferenceChangeListener +import android.preference.PreferenceFragment +import android.telephony.SmsManager +import android.telephony.SmsMessage +import android.text.TextUtils +import com.andreabaccega.widget.ValidatingEditTextPreference +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.data.DetailedBolusInfo +import info.nightscout.androidaps.data.Profile +import info.nightscout.androidaps.db.DatabaseHelper +import info.nightscout.androidaps.db.Source +import info.nightscout.androidaps.db.TempTarget +import info.nightscout.androidaps.events.EventPreferenceChange +import info.nightscout.androidaps.events.EventRefreshOverview +import info.nightscout.androidaps.interfaces.Constraint +import info.nightscout.androidaps.interfaces.PluginBase +import info.nightscout.androidaps.interfaces.PluginDescription +import info.nightscout.androidaps.interfaces.PluginType +import info.nightscout.androidaps.logging.L +import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin +import info.nightscout.androidaps.plugins.bus.RxBus.send +import info.nightscout.androidaps.plugins.bus.RxBus.toObservable +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.nsclient.NSUpload +import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart +import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification +import info.nightscout.androidaps.plugins.general.overview.notifications.Notification +import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSmsCommunicatorUpdateGui +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.queue.Callback +import info.nightscout.androidaps.utils.* +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.schedulers.Schedulers +import org.apache.commons.lang3.StringUtils +import org.slf4j.LoggerFactory +import java.text.Normalizer +import java.util.* + +object SmsCommunicatorPlugin : PluginBase(PluginDescription() + .mainType(PluginType.GENERAL) + .fragmentClass(SmsCommunicatorFragment::class.java.name) + .pluginName(R.string.smscommunicator) + .shortName(R.string.smscommunicator_shortname) + .preferencesId(R.xml.pref_smscommunicator) + .description(R.string.description_sms_communicator) +) { + private val log = LoggerFactory.getLogger(L.SMS) + private val disposable = CompositeDisposable() + var allowedNumbers: MutableList = ArrayList() + var messageToConfirm: AuthRequest? = null + var lastRemoteBolusTime: Long = 0 + var messages = ArrayList() + + val commands = mapOf( + "BG" to "BG", + "LOOP" to "LOOP STOP/DISABLE/START/ENABLE/RESUME/STATUS\nLOOP SUSPEND 20", + "TREATMENTS" to "TREATMENTS REFRESH", + "NSCLIENT" to "NSCLIENT RESTART", + "PUMP" to "PUMP", + "BASAL" to "BASAL STOP/CANCEL\nBASAL 0.3\nBASAL 0.3 20\nBASAL 30%\nBASAL 30% 20\n", + "BOLUS" to "BOLUS 1.2\nBOLUS 1.2 MEAL", + "EXTENDED" to "EXTENDED STOP/CANCEL\nEXTENDED 2 120", + "CAL" to "CAL 5.6", + "PROFILE" to "PROFILE STATUS/LIST\nPROFILE 1\nPROFILE 2 30", + "TARGET" to "TARGET MEAL/ACTIVITY/HYPO/STOP", + "SMS" to "SMS DISABLE/STOP", + "CARBS" to "CARBS 12\nCARBS 12 23:05\nCARBS 12 11:05PM", + "HELP" to "HELP\nHELP command" + ) + + init { + processSettings(null) + } + + override fun onStart() { + super.onStart() + disposable.add(toObservable(EventPreferenceChange::class.java) + .observeOn(Schedulers.io()) + .subscribe({ event: EventPreferenceChange? -> processSettings(event) }) { throwable: Throwable? -> FabricPrivacy.logException(throwable) } + ) + } + + override fun onStop() { + disposable.clear() + super.onStop() + } + + override fun preprocessPreferences(preferenceFragment: PreferenceFragment) { + super.preprocessPreferences(preferenceFragment) + val distance = preferenceFragment.findPreference(MainApp.gs(R.string.key_smscommunicator_remotebolusmindistance)) as ValidatingEditTextPreference? + ?: return + val allowedNumbers = preferenceFragment.findPreference(MainApp.gs(R.string.key_smscommunicator_allowednumbers)) as EditTextPreference? + ?: return + if (!areMoreNumbers(allowedNumbers.text)) { + distance.title = (MainApp.gs(R.string.smscommunicator_remotebolusmindistance) + + ".\n" + + MainApp.gs(R.string.smscommunicator_remotebolusmindistance_caveat)) + distance.isEnabled = false + } else { + distance.title = MainApp.gs(R.string.smscommunicator_remotebolusmindistance) + distance.isEnabled = true + } + allowedNumbers.onPreferenceChangeListener = OnPreferenceChangeListener { _: Preference?, newValue: Any -> + if (!areMoreNumbers(newValue as String)) { + distance.text = (Constants.remoteBolusMinDistance / (60 * 1000L)).toString() + distance.title = (MainApp.gs(R.string.smscommunicator_remotebolusmindistance) + + ".\n" + + MainApp.gs(R.string.smscommunicator_remotebolusmindistance_caveat)) + distance.isEnabled = false + } else { + distance.title = MainApp.gs(R.string.smscommunicator_remotebolusmindistance) + distance.isEnabled = true + } + true + } + } + + override fun updatePreferenceSummary(pref: Preference) { + super.updatePreferenceSummary(pref) + if (pref is EditTextPreference) { + val editTextPref = pref + if (pref.getKey().contains("smscommunicator_allowednumbers") && (editTextPref.text == null || TextUtils.isEmpty(editTextPref.text.trim { it <= ' ' }))) { + pref.setSummary(MainApp.gs(R.string.smscommunicator_allowednumbers_summary)) + } + } + } + + private fun processSettings(ev: EventPreferenceChange?) { + if (ev == null || ev.isChanged(R.string.key_smscommunicator_allowednumbers)) { + val settings = SP.getString(R.string.key_smscommunicator_allowednumbers, "") + allowedNumbers.clear() + val substrings = settings.split(";").toTypedArray() + for (number in substrings) { + val cleaned = number.replace("\\s+".toRegex(), "") + allowedNumbers.add(cleaned) + log.debug("Found allowed number: $cleaned") + } + } + } + + fun isCommand(command: String, number: String): Boolean { + var found = false + commands.forEach { (k, _) -> + if (k == command) found = true + } + return found || messageToConfirm?.requester?.phoneNumber == number + } + + fun isAllowedNumber(number: String): Boolean { + for (num in allowedNumbers) { + if (num == number) return true + } + return false + } + + fun handleNewData(intent: Intent) { + val bundle = intent.extras ?: return + val format = bundle.getString("format") ?: return + val pdus = bundle["pdus"] as Array<*> + for (pdu in pdus) { + val message = SmsMessage.createFromPdu(pdu as ByteArray, format) + processSms(Sms(message)) + } + } + + fun processSms(receivedSms: Sms) { + if (!isEnabled(PluginType.GENERAL)) { + log.debug("Ignoring SMS. Plugin disabled.") + return + } + if (!isAllowedNumber(receivedSms.phoneNumber)) { + log.debug("Ignoring SMS from: " + receivedSms.phoneNumber + ". Sender not allowed") + receivedSms.ignored = true + messages.add(receivedSms) + send(EventSmsCommunicatorUpdateGui()) + return + } + val pump = ConfigBuilderPlugin.getPlugin().activePump ?: return + messages.add(receivedSms) + log.debug(receivedSms.toString()) + val splitted = receivedSms.text.split(Regex("\\s+")).toTypedArray() + val remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false) + if (splitted.isNotEmpty() && isCommand(splitted[0].toUpperCase(Locale.getDefault()), receivedSms.phoneNumber)) { + when (splitted[0].toUpperCase(Locale.getDefault())) { + "BG" -> + if (splitted.size == 1) processBG(receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "LOOP" -> + if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)) + else if (splitted.size == 2 || splitted.size == 3) processLOOP(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "TREATMENTS" -> + if (splitted.size == 2) processTREATMENTS(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "NSCLIENT" -> + if (splitted.size == 2) processNSCLIENT(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "PUMP" -> + if (splitted.size == 1) processPUMP(receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "PROFILE" -> + if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)) + else if (splitted.size == 2 || splitted.size == 3) processPROFILE(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "BASAL" -> + if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)) + else if (splitted.size == 2 || splitted.size == 3) processBASAL(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "EXTENDED" -> + if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)) + else if (splitted.size == 2 || splitted.size == 3) processEXTENDED(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "BOLUS" -> + if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)) + else if (splitted.size == 2 && DateUtil.now() - lastRemoteBolusTime < Constants.remoteBolusMinDistance) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotebolusnotallowed)) + else if (splitted.size == 2 && pump.isSuspended) sendSMS(Sms(receivedSms.phoneNumber, R.string.pumpsuspended)) + else if (splitted.size == 2 || splitted.size == 3) processBOLUS(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "CARBS" -> + if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)) + else if (splitted.size == 2 || splitted.size == 3) processCARBS(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "CAL" -> + if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)) + else if (splitted.size == 2) processCAL(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "TARGET" -> + if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)) + else if (splitted.size == 2) processTARGET(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "SMS" -> + if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed)) + else if (splitted.size == 2) processSMS(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + "HELP" -> + if (splitted.size == 1 || splitted.size == 2) processHELP(splitted, receivedSms) + else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + else -> + if (messageToConfirm?.requester?.phoneNumber == receivedSms.phoneNumber) { + messageToConfirm?.action(splitted[0]) + messageToConfirm = null + } else sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_unknowncommand)) + } + } + send(EventSmsCommunicatorUpdateGui()) + } + + private fun processBG(receivedSms: Sms) { + val actualBG = DatabaseHelper.actualBg() + val lastBG = DatabaseHelper.lastBg() + var reply = "" + val units = ProfileFunctions.getInstance().profileUnits + if (actualBG != null) { + reply = MainApp.gs(R.string.sms_actualbg) + " " + actualBG.valueToUnitsToString(units) + ", " + } else if (lastBG != null) { + val agoMsec = System.currentTimeMillis() - lastBG.date + val agoMin = (agoMsec / 60.0 / 1000.0).toInt() + reply = MainApp.gs(R.string.sms_lastbg) + " " + lastBG.valueToUnitsToString(units) + " " + String.format(MainApp.gs(R.string.sms_minago), agoMin) + ", " + } + val glucoseStatus = GlucoseStatus.getGlucoseStatusData() + if (glucoseStatus != null) reply += MainApp.gs(R.string.sms_delta) + " " + Profile.toUnitsString(glucoseStatus.delta, glucoseStatus.delta * Constants.MGDL_TO_MMOLL, units) + " " + units + ", " + TreatmentsPlugin.getPlugin().updateTotalIOBTreatments() + val bolusIob = TreatmentsPlugin.getPlugin().lastCalculationTreatments.round() + TreatmentsPlugin.getPlugin().updateTotalIOBTempBasals() + val basalIob = TreatmentsPlugin.getPlugin().lastCalculationTempBasals.round() + val cobInfo = IobCobCalculatorPlugin.getPlugin().getCobInfo(false, "SMS COB") + reply += (MainApp.gs(R.string.sms_iob) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U (" + + MainApp.gs(R.string.sms_bolus) + " " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " + + MainApp.gs(R.string.sms_basal) + " " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U), " + + MainApp.gs(R.string.cob) + ": " + cobInfo.generateCOBString()) + sendSMS(Sms(receivedSms.phoneNumber, reply)) + receivedSms.processed = true + } + + private fun processLOOP(splitted: Array, receivedSms: Sms) { + when (splitted[1].toUpperCase(Locale.getDefault())) { + "DISABLE", "STOP" -> { + val loopPlugin = LoopPlugin.getPlugin() + if (loopPlugin.isEnabled(PluginType.LOOP)) { + loopPlugin.setPluginEnabled(PluginType.LOOP, false) + ConfigBuilderPlugin.getPlugin().commandQueue.cancelTempBasal(true, object : Callback() { + override fun run() { + send(EventRefreshOverview("SMS_LOOP_STOP")) + val replyText = MainApp.gs(R.string.smscommunicator_loophasbeendisabled) + " " + + MainApp.gs(if (result.success) R.string.smscommunicator_tempbasalcanceled else R.string.smscommunicator_tempbasalcancelfailed) + sendSMS(Sms(receivedSms.phoneNumber, replyText)) + } + }) + } else + sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopisdisabled)) + receivedSms.processed = true + } + "ENABLE", "START" -> { + val loopPlugin = LoopPlugin.getPlugin() + if (!loopPlugin.isEnabled(PluginType.LOOP)) { + loopPlugin.setPluginEnabled(PluginType.LOOP, true) + sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_loophasbeenenabled)) + send(EventRefreshOverview("SMS_LOOP_START")) + } else + sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopisenabled)) + receivedSms.processed = true + } + "STATUS" -> { + val loopPlugin = LoopPlugin.getPlugin() + val reply = if (loopPlugin.isEnabled(PluginType.LOOP)) { + if (loopPlugin.isSuspended()) String.format(MainApp.gs(R.string.loopsuspendedfor), loopPlugin.minutesToEndOfSuspend()) + else MainApp.gs(R.string.smscommunicator_loopisenabled) + } else + MainApp.gs(R.string.smscommunicator_loopisdisabled) + sendSMS(Sms(receivedSms.phoneNumber, reply)) + receivedSms.processed = true + } + "RESUME" -> { + LoopPlugin.getPlugin().suspendTo(0) + send(EventRefreshOverview("SMS_LOOP_RESUME")) + NSUpload.uploadOpenAPSOffline(0.0) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, R.string.smscommunicator_loopresumed)) + } + "SUSPEND" -> { + var duration = 0 + if (splitted.size == 3) duration = SafeParse.stringToInt(splitted[2]) + duration = Math.max(0, duration) + duration = Math.min(180, duration) + if (duration == 0) { + receivedSms.processed = true + sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_wrongduration)) + return + } else { + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_suspendreplywithcode), duration, passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction(duration) { + override fun run() { + ConfigBuilderPlugin.getPlugin().commandQueue.cancelTempBasal(true, object : Callback() { + override fun run() { + if (result.success) { + LoopPlugin.getPlugin().suspendTo(System.currentTimeMillis() + anInteger() * 60L * 1000) + NSUpload.uploadOpenAPSOffline(anInteger() * 60.toDouble()) + send(EventRefreshOverview("SMS_LOOP_SUSPENDED")) + val replyText = MainApp.gs(R.string.smscommunicator_loopsuspended) + " " + + MainApp.gs(if (result.success) R.string.smscommunicator_tempbasalcanceled else R.string.smscommunicator_tempbasalcancelfailed) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } else { + var replyText = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMS(Sms(receivedSms.phoneNumber, replyText)) + } + } + }) + } + }) + } + } + else -> sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + } + } + + private fun processTREATMENTS(splitted: Array, receivedSms: Sms) { + if (splitted[1].toUpperCase(Locale.getDefault()) == "REFRESH") { + TreatmentsPlugin.getPlugin().service.resetTreatments() + send(EventNSClientRestart()) + sendSMS(Sms(receivedSms.phoneNumber, "TREATMENTS REFRESH SENT")) + receivedSms.processed = true + } else + sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + } + + private fun processNSCLIENT(splitted: Array, receivedSms: Sms) { + if (splitted[1].toUpperCase(Locale.getDefault()) == "RESTART") { + send(EventNSClientRestart()) + sendSMS(Sms(receivedSms.phoneNumber, "NSCLIENT RESTART SENT")) + receivedSms.processed = true + } else + sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + } + + private fun processHELP(splitted: Array, receivedSms: Sms) { + if (splitted.size == 1) { + sendSMS(Sms(receivedSms.phoneNumber, commands.keys.toString().replace("[", "").replace("]", ""))) + receivedSms.processed = true + } else if (isCommand(splitted[1].toUpperCase(Locale.getDefault()), receivedSms.phoneNumber)) { + commands[splitted[1].toUpperCase(Locale.getDefault())]?.let { + sendSMS(Sms(receivedSms.phoneNumber, it)) + receivedSms.processed = true + } + } else + sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + } + + private fun processPUMP(receivedSms: Sms) { + ConfigBuilderPlugin.getPlugin().commandQueue.readStatus("SMS", object : Callback() { + override fun run() { + val pump = ConfigBuilderPlugin.getPlugin().activePump + if (result.success) { + if (pump != null) { + val reply = pump.shortStatus(true) + sendSMS(Sms(receivedSms.phoneNumber, reply)) + } + } else { + val reply = MainApp.gs(R.string.readstatusfailed) + sendSMS(Sms(receivedSms.phoneNumber, reply)) + } + } + }) + receivedSms.processed = true + } + + private fun processPROFILE(splitted: Array, receivedSms: Sms) { // load profiles + val anInterface = ConfigBuilderPlugin.getPlugin().activeProfileInterface + if (anInterface == null) { + sendSMS(Sms(receivedSms.phoneNumber, R.string.notconfigured)) + receivedSms.processed = true + return + } + val store = anInterface.profile + if (store == null) { + sendSMS(Sms(receivedSms.phoneNumber, R.string.notconfigured)) + receivedSms.processed = true + return + } + val list = store.profileList + if (splitted[1].toUpperCase(Locale.getDefault()) == "STATUS") { + sendSMS(Sms(receivedSms.phoneNumber, ProfileFunctions.getInstance().profileName)) + } else if (splitted[1].toUpperCase(Locale.getDefault()) == "LIST") { + if (list.isEmpty()) sendSMS(Sms(receivedSms.phoneNumber, R.string.invalidprofile)) + else { + var reply = "" + for (i in list.indices) { + if (i > 0) reply += "\n" + reply += (i + 1).toString() + ". " + reply += list[i] + } + sendSMS(Sms(receivedSms.phoneNumber, reply)) + } + } else { + val pindex = SafeParse.stringToInt(splitted[1]) + var percentage = 100 + if (splitted.size > 2) percentage = SafeParse.stringToInt(splitted[2]) + if (pindex > list.size) sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + else if (percentage == 0) sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + else if (pindex == 0) sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + else { + val profile = store.getSpecificProfile(list[pindex - 1] as String) + if (profile == null) sendSMS(Sms(receivedSms.phoneNumber, R.string.noprofile)) + else { + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_profilereplywithcode), list[pindex - 1], percentage, passCode) + receivedSms.processed = true + val finalPercentage = percentage + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction(list[pindex - 1] as String, finalPercentage) { + override fun run() { + ProfileFunctions.doProfileSwitch(store, list[pindex - 1] as String, 0, finalPercentage, 0) + sendSMS(Sms(receivedSms.phoneNumber, R.string.profileswitchcreated)) + } + }) + } + } + } + receivedSms.processed = true + } + + private fun processBASAL(splitted: Array, receivedSms: Sms) { + if (splitted[1].toUpperCase(Locale.getDefault()) == "CANCEL" || splitted[1].toUpperCase(Locale.getDefault()) == "STOP") { + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_basalstopreplywithcode), passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() { + override fun run() { + ConfigBuilderPlugin.getPlugin().commandQueue.cancelTempBasal(true, object : Callback() { + override fun run() { + if (result.success) { + var replyText = MainApp.gs(R.string.smscommunicator_tempbasalcanceled) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } else { + var replyText = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMS(Sms(receivedSms.phoneNumber, replyText)) + } + } + }) + } + }) + } else if (splitted[1].endsWith("%")) { + var tempBasalPct = SafeParse.stringToInt(StringUtils.removeEnd(splitted[1], "%")) + var duration = 30 + if (splitted.size > 2) duration = SafeParse.stringToInt(splitted[2]) + val profile = ProfileFunctions.getInstance().profile + if (profile == null) sendSMS(Sms(receivedSms.phoneNumber, R.string.noprofile)) + else if (tempBasalPct == 0 && splitted[1] != "0%") sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + else if (duration == 0) sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + else { + tempBasalPct = MainApp.getConstraintChecker().applyBasalPercentConstraints(Constraint(tempBasalPct), profile).value() + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_basalpctreplywithcode), tempBasalPct, duration, passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction(tempBasalPct, duration) { + override fun run() { + ConfigBuilderPlugin.getPlugin().commandQueue.tempBasalPercent(anInteger(), secondInteger(), true, profile, object : Callback() { + override fun run() { + if (result.success) { + var replyText: String + replyText = if (result.isPercent) String.format(MainApp.gs(R.string.smscommunicator_tempbasalset_percent), result.percent, result.duration) else String.format(MainApp.gs(R.string.smscommunicator_tempbasalset), result.absolute, result.duration) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } else { + var replyText = MainApp.gs(R.string.smscommunicator_tempbasalfailed) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMS(Sms(receivedSms.phoneNumber, replyText)) + } + } + }) + } + }) + } + } else { + var tempBasal = SafeParse.stringToDouble(splitted[1]) + var duration = 30 + if (splitted.size > 2) duration = SafeParse.stringToInt(splitted[2]) + val profile = ProfileFunctions.getInstance().profile + if (profile == null) sendSMS(Sms(receivedSms.phoneNumber, R.string.noprofile)) + else if (tempBasal == 0.0 && splitted[1] != "0") sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + else if (duration == 0) sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + else { + tempBasal = MainApp.getConstraintChecker().applyBasalConstraints(Constraint(tempBasal), profile).value() + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_basalreplywithcode), tempBasal, duration, passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction(tempBasal, duration) { + override fun run() { + ConfigBuilderPlugin.getPlugin().commandQueue.tempBasalAbsolute(aDouble(), secondInteger(), true, profile, object : Callback() { + override fun run() { + if (result.success) { + var replyText = if (result.isPercent) String.format(MainApp.gs(R.string.smscommunicator_tempbasalset_percent), result.percent, result.duration) + else String.format(MainApp.gs(R.string.smscommunicator_tempbasalset), result.absolute, result.duration) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } else { + var replyText = MainApp.gs(R.string.smscommunicator_tempbasalfailed) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMS(Sms(receivedSms.phoneNumber, replyText)) + } + } + }) + } + }) + } + } + } + + private fun processEXTENDED(splitted: Array, receivedSms: Sms) { + if (splitted[1].toUpperCase(Locale.getDefault()) == "CANCEL" || splitted[1].toUpperCase(Locale.getDefault()) == "STOP") { + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_extendedstopreplywithcode), passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() { + override fun run() { + ConfigBuilderPlugin.getPlugin().commandQueue.cancelExtended(object : Callback() { + override fun run() { + if (result.success) { + var replyText = MainApp.gs(R.string.smscommunicator_extendedcanceled) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } else { + var replyText = MainApp.gs(R.string.smscommunicator_extendedcancelfailed) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMS(Sms(receivedSms.phoneNumber, replyText)) + } + } + }) + } + }) + } else if (splitted.size != 3) { + sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + } else { + var extended = SafeParse.stringToDouble(splitted[1]) + val duration = SafeParse.stringToInt(splitted[2]) + extended = MainApp.getConstraintChecker().applyExtendedBolusConstraints(Constraint(extended)).value() + if (extended == 0.0 || duration == 0) sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + else { + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_extendedreplywithcode), extended, duration, passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction(extended, duration) { + override fun run() { + ConfigBuilderPlugin.getPlugin().commandQueue.extendedBolus(aDouble(), secondInteger(), object : Callback() { + override fun run() { + if (result.success) { + var replyText = String.format(MainApp.gs(R.string.smscommunicator_extendedset), aDouble, duration) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } else { + var replyText = MainApp.gs(R.string.smscommunicator_extendedfailed) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMS(Sms(receivedSms.phoneNumber, replyText)) + } + } + }) + } + }) + } + } + } + + private fun processBOLUS(splitted: Array, receivedSms: Sms) { + var bolus = SafeParse.stringToDouble(splitted[1]) + val isMeal = splitted.size > 2 && splitted[2].equals("MEAL", ignoreCase = true) + bolus = MainApp.getConstraintChecker().applyBolusConstraints(Constraint(bolus)).value() + if (splitted.size == 3 && !isMeal) { + sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + } else if (bolus > 0.0) { + val passCode = generatePasscode() + val reply = if (isMeal) + String.format(MainApp.gs(R.string.smscommunicator_mealbolusreplywithcode), bolus, passCode) + else + String.format(MainApp.gs(R.string.smscommunicator_bolusreplywithcode), bolus, passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction(bolus) { + override fun run() { + val detailedBolusInfo = DetailedBolusInfo() + detailedBolusInfo.insulin = aDouble() + detailedBolusInfo.source = Source.USER + ConfigBuilderPlugin.getPlugin().commandQueue.bolus(detailedBolusInfo, object : Callback() { + override fun run() { + val resultSuccess = result.success + val resultBolusDelivered = result.bolusDelivered + ConfigBuilderPlugin.getPlugin().commandQueue.readStatus("SMS", object : Callback() { + override fun run() { + if (resultSuccess) { + var replyText = if (isMeal) + String.format(MainApp.gs(R.string.smscommunicator_mealbolusdelivered), resultBolusDelivered) + else + String.format(MainApp.gs(R.string.smscommunicator_bolusdelivered), resultBolusDelivered) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + lastRemoteBolusTime = DateUtil.now() + if (isMeal) { + ProfileFunctions.getInstance().profile?.let { currentProfile -> + var eatingSoonTTDuration = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration) + eatingSoonTTDuration = + if (eatingSoonTTDuration > 0) eatingSoonTTDuration + else Constants.defaultEatingSoonTTDuration + var eatingSoonTT = SP.getDouble(R.string.key_eatingsoon_target, if (currentProfile.units == Constants.MMOL) Constants.defaultEatingSoonTTmmol else Constants.defaultEatingSoonTTmgdl) + eatingSoonTT = + if (eatingSoonTT > 0) eatingSoonTT + else if (currentProfile.units == Constants.MMOL) Constants.defaultEatingSoonTTmmol + else Constants.defaultEatingSoonTTmgdl + val tempTarget = TempTarget() + .date(System.currentTimeMillis()) + .duration(eatingSoonTTDuration) + .reason(MainApp.gs(R.string.eatingsoon)) + .source(Source.USER) + .low(Profile.toMgdl(eatingSoonTT, currentProfile.units)) + .high(Profile.toMgdl(eatingSoonTT, currentProfile.units)) + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) + val tt = if (currentProfile.units == Constants.MMOL) { + DecimalFormatter.to1Decimal(eatingSoonTT) + } else DecimalFormatter.to0Decimal(eatingSoonTT) + replyText += "\n" + String.format(MainApp.gs(R.string.smscommunicator_mealbolusdelivered_tt), tt, eatingSoonTTDuration) + } + } + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } else { + var replyText = MainApp.gs(R.string.smscommunicator_bolusfailed) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMS(Sms(receivedSms.phoneNumber, replyText)) + } + } + }) + } + }) + } + }) + } else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + } + + private fun processCARBS(splitted: Array, receivedSms: Sms) { + var grams = SafeParse.stringToInt(splitted[1]) + var time = DateUtil.now() + if (splitted.size > 2) { + val seconds = DateUtil.toSeconds(splitted[2].toUpperCase(Locale.getDefault())) + val midnight = MidnightTime.calc() + if (seconds == 0 && (!splitted[2].startsWith("00:00") || !splitted[2].startsWith("12:00"))) { + sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + return + } + time = midnight + T.secs(seconds.toLong()).msecs() + } + grams = MainApp.getConstraintChecker().applyCarbsConstraints(Constraint(grams)).value() + if (grams == 0) sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + else { + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_carbsreplywithcode), grams, DateUtil.timeString(time), passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction(grams, time) { + override fun run() { + val detailedBolusInfo = DetailedBolusInfo() + detailedBolusInfo.carbs = anInteger().toDouble() + detailedBolusInfo.date = secondLong() + ConfigBuilderPlugin.getPlugin().commandQueue.bolus(detailedBolusInfo, object : Callback() { + override fun run() { + if (result.success) { + var replyText = String.format(MainApp.gs(R.string.smscommunicator_carbsset), anInteger) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } else { + var replyText = MainApp.gs(R.string.smscommunicator_carbsfailed) + replyText += "\n" + ConfigBuilderPlugin.getPlugin().activePump?.shortStatus(true) + sendSMS(Sms(receivedSms.phoneNumber, replyText)) + } + } + }) + } + }) + } + } + + private fun processTARGET(splitted: Array, receivedSms: Sms) { + val isMeal = splitted[1].equals("MEAL", ignoreCase = true) + val isActivity = splitted[1].equals("ACTIVITY", ignoreCase = true) + val isHypo = splitted[1].equals("HYPO", ignoreCase = true) + val isStop = splitted[1].equals("STOP", ignoreCase = true) || splitted[1].equals("CANCEL", ignoreCase = true) + if (isMeal || isActivity || isHypo) { + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_temptargetwithcode), splitted[1].toUpperCase(Locale.getDefault()), passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() { + override fun run() { + val currentProfile = ProfileFunctions.getInstance().profile + if (currentProfile != null) { + var keyDuration = 0 + var defaultTargetDuration = 0 + var keyTarget = 0 + var defaultTargetMMOL = 0.0 + var defaultTargetMGDL = 0.0 + if (isMeal) { + keyDuration = R.string.key_eatingsoon_duration + defaultTargetDuration = Constants.defaultEatingSoonTTDuration + keyTarget = R.string.key_eatingsoon_target + defaultTargetMMOL = Constants.defaultEatingSoonTTmmol + defaultTargetMGDL = Constants.defaultEatingSoonTTmgdl + } else if (isActivity) { + keyDuration = R.string.key_activity_duration + defaultTargetDuration = Constants.defaultActivityTTDuration + keyTarget = R.string.key_activity_target + defaultTargetMMOL = Constants.defaultActivityTTmmol + defaultTargetMGDL = Constants.defaultActivityTTmgdl + } else if (isHypo) { + keyDuration = R.string.key_hypo_duration + defaultTargetDuration = Constants.defaultHypoTTDuration + keyTarget = R.string.key_hypo_target + defaultTargetMMOL = Constants.defaultHypoTTmmol + defaultTargetMGDL = Constants.defaultHypoTTmgdl + } + var ttDuration = SP.getInt(keyDuration, defaultTargetDuration) + ttDuration = if (ttDuration > 0) ttDuration else defaultTargetDuration + var tt = SP.getDouble(keyTarget, if (currentProfile.units == Constants.MMOL) defaultTargetMMOL else defaultTargetMGDL) + tt = if (tt > 0) tt else if (currentProfile.units == Constants.MMOL) defaultTargetMMOL else defaultTargetMGDL + val tempTarget = TempTarget() + .date(System.currentTimeMillis()) + .duration(ttDuration) + .reason(MainApp.gs(R.string.eatingsoon)) + .source(Source.USER) + .low(Profile.toMgdl(tt, currentProfile.units)) + .high(Profile.toMgdl(tt, currentProfile.units)) + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) + val ttString = if (currentProfile.units == Constants.MMOL) DecimalFormatter.to1Decimal(tt) else DecimalFormatter.to0Decimal(tt) + val replyText = String.format(MainApp.gs(R.string.smscommunicator_tt_set), ttString, ttDuration) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } else { + sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_unknowncommand)) + } + } + }) + } else if (isStop) { + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_temptargetcancel), passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() { + override fun run() { + val currentProfile = ProfileFunctions.getInstance().profile + if (currentProfile != null) { + val tempTarget = TempTarget() + .source(Source.USER) + .date(DateUtil.now()) + .duration(0) + .low(0.0) + .high(0.0) + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) + val replyText = String.format(MainApp.gs(R.string.smscommunicator_tt_canceled)) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } else { + sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_unknowncommand)) + } + } + }) + } else + sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + } + + private fun processSMS(splitted: Array, receivedSms: Sms) { + val isStop = (splitted[1].equals("STOP", ignoreCase = true) + || splitted[1].equals("DISABLE", ignoreCase = true)) + if (isStop) { + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_stopsmswithcode), passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() { + override fun run() { + SP.putBoolean(R.string.key_smscommunicator_remotecommandsallowed, false) + val replyText = String.format(MainApp.gs(R.string.smscommunicator_stoppedsms)) + sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) + } + }) + } else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + } + + private fun processCAL(splitted: Array, receivedSms: Sms) { + val cal = SafeParse.stringToDouble(splitted[1]) + if (cal > 0.0) { + val passCode = generatePasscode() + val reply = String.format(MainApp.gs(R.string.smscommunicator_calibrationreplywithcode), cal, passCode) + receivedSms.processed = true + messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction(cal) { + override fun run() { + val result = XdripCalibrations.sendIntent(aDouble) + if (result) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, R.string.smscommunicator_calibrationsent)) else sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_calibrationfailed)) + } + }) + } else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat)) + } + + fun sendNotificationToAllNumbers(text: String): Boolean { + var result = true + for (i in allowedNumbers.indices) { + val sms = Sms(allowedNumbers[i], text) + result = result && sendSMS(sms) + } + return result + } + + private fun sendSMSToAllNumbers(sms: Sms) { + for (number in allowedNumbers) { + sms.phoneNumber = number + sendSMS(sms) + } + } + + fun sendSMS(sms: Sms): Boolean { + val smsManager = SmsManager.getDefault() + sms.text = stripAccents(sms.text) + try { + if (L.isEnabled(L.SMS)) log.debug("Sending SMS to " + sms.phoneNumber + ": " + sms.text) + if (sms.text.toByteArray().size <= 140) smsManager.sendTextMessage(sms.phoneNumber, null, sms.text, null, null) + else { + val parts = smsManager.divideMessage(sms.text) + smsManager.sendMultipartTextMessage(sms.phoneNumber, null, parts, + null, null) + } + messages.add(sms) + } catch (e: IllegalArgumentException) { + return if (e.message == "Invalid message body") { + val notification = Notification(Notification.INVALID_MESSAGE_BODY, MainApp.gs(R.string.smscommunicator_messagebody), Notification.NORMAL) + send(EventNewNotification(notification)) + false + } else { + val notification = Notification(Notification.INVALID_PHONE_NUMBER, MainApp.gs(R.string.smscommunicator_invalidphonennumber), Notification.NORMAL) + send(EventNewNotification(notification)) + false + } + } catch (e: SecurityException) { + val notification = Notification(Notification.MISSING_SMS_PERMISSION, MainApp.gs(R.string.smscommunicator_missingsmspermission), Notification.NORMAL) + send(EventNewNotification(notification)) + return false + } + send(EventSmsCommunicatorUpdateGui()) + return true + } + + private fun generatePasscode(): String { + val startChar1 = 'A'.toInt() // on iphone 1st char is uppercase :) + var passCode = Character.toString((startChar1 + Math.random() * ('z' - 'a' + 1)).toChar()) + val startChar2: Int = if (Math.random() > 0.5) 'a'.toInt() else 'A'.toInt() + passCode += Character.toString((startChar2 + Math.random() * ('z' - 'a' + 1)).toChar()) + val startChar3: Int = if (Math.random() > 0.5) 'a'.toInt() else 'A'.toInt() + passCode += Character.toString((startChar3 + Math.random() * ('z' - 'a' + 1)).toChar()) + passCode = passCode.replace('l', 'k').replace('I', 'J') + return passCode + } + + private fun stripAccents(str: String): String { + var s = str + s = Normalizer.normalize(s, Normalizer.Form.NFD) + s = s.replace("[\\p{InCombiningDiacriticalMarks}]".toRegex(), "") + return s + } + + fun areMoreNumbers(allowednumbers: String?): Boolean { + return allowednumbers?.let { + var countNumbers = 0 + val substrings = it.split(";").toTypedArray() + for (number in substrings) { + var cleaned = number.replace(Regex("\\s+"), "") + if (cleaned.length < 4) continue + cleaned = cleaned.replace("+", "") + cleaned = cleaned.replace("-", "") + if (!cleaned.matches(Regex("[0-9]+"))) continue + countNumbers++ + } + countNumbers > 1 + } ?: false + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/events/EventSmsCommunicatorUpdateGui.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/events/EventSmsCommunicatorUpdateGui.java deleted file mode 100644 index 7e50671c45..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/events/EventSmsCommunicatorUpdateGui.java +++ /dev/null @@ -1,9 +0,0 @@ -package info.nightscout.androidaps.plugins.general.smsCommunicator.events; - -import info.nightscout.androidaps.events.EventUpdateGui; - -/** - * Created by mike on 05.08.2016. - */ -public class EventSmsCommunicatorUpdateGui extends EventUpdateGui { -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/events/EventSmsCommunicatorUpdateGui.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/events/EventSmsCommunicatorUpdateGui.kt new file mode 100644 index 0000000000..4a0f95b244 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/events/EventSmsCommunicatorUpdateGui.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.general.smsCommunicator.events + +import info.nightscout.androidaps.events.EventUpdateGui + +class EventSmsCommunicatorUpdateGui : EventUpdateGui() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt index 78ad0ab37d..e8971ab9d5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.tidepool +import android.preference.PreferenceFragment import android.text.Spanned import info.nightscout.androidaps.Constants import info.nightscout.androidaps.MainApp @@ -34,7 +35,6 @@ object TidepoolPlugin : PluginBase(PluginDescription() .preferencesId(R.xml.pref_tidepool) .description(R.string.description_tidepool) ) { - private val log = LoggerFactory.getLogger(L.TIDEPOOL) private var disposable: CompositeDisposable = CompositeDisposable() @@ -111,6 +111,16 @@ object TidepoolPlugin : PluginBase(PluginDescription() super.onStop() } + override fun preprocessPreferences(preferenceFragment: PreferenceFragment) { + super.preprocessPreferences(preferenceFragment) + + val tidepoolTestLogin = preferenceFragment.findPreference(MainApp.gs(R.string.key_tidepool_test_login)) + tidepoolTestLogin?.setOnPreferenceClickListener { + TidepoolUploader.testLogin(preferenceFragment.getActivity()) + false + } + } + private fun doUpload() = when (TidepoolUploader.connectionStatus) { TidepoolUploader.ConnectionStatus.FAILED -> {} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolStatus.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolStatus.kt index aa69c11e95..e7854f8ff2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolStatus.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolStatus.kt @@ -17,7 +17,7 @@ class EventTidepoolStatus(val status: String) : Event() { log.debug("New status: $status") } - private var timeFormat = SimpleDateFormat("HH:mm:ss", LocaleHelper.getLocale()) + private var timeFormat = SimpleDateFormat("HH:mm:ss", LocaleHelper.currentLocale()) fun toPreparedHtml(): StringBuilder { val stringBuilder = StringBuilder() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.java index 25a6540dbd..1cf4dd7539 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.java @@ -437,11 +437,11 @@ public class ActionStringHandler { public static boolean isOldData(List historyList) { Object activePump = ConfigBuilderPlugin.getPlugin().getActivePump(); - PumpInterface dana = MainApp.getSpecificPlugin(DanaRPlugin.class); - PumpInterface danaRS = MainApp.getSpecificPlugin(DanaRSPlugin.class); - PumpInterface danaV2 = MainApp.getSpecificPlugin(DanaRv2Plugin.class); - PumpInterface danaKorean = MainApp.getSpecificPlugin(DanaRKoreanPlugin.class); - PumpInterface insight = MainApp.getSpecificPlugin(LocalInsightPlugin.class); + PumpInterface dana = DanaRPlugin.getPlugin(); + PumpInterface danaRS = DanaRSPlugin.getPlugin(); + PumpInterface danaV2 = DanaRv2Plugin.getPlugin(); + PumpInterface danaKorean = DanaRKoreanPlugin.getPlugin(); + PumpInterface insight = LocalInsightPlugin.getPlugin(); boolean startsYesterday = activePump == dana || activePump == danaRS || activePump == danaV2 || activePump == danaKorean || activePump == insight; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/WearPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/WearPlugin.java index 53b421ace3..ec01bff68b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/WearPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/WearPlugin.java @@ -2,9 +2,6 @@ package info.nightscout.androidaps.plugins.general.wear; import android.content.Context; import android.content.Intent; -import android.util.Log; - -import com.squareup.otto.Subscribe; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -21,14 +18,13 @@ import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; import info.nightscout.androidaps.plugins.aps.openAPSMA.events.EventOpenAPSUpdateGui; import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusprogressIfRunning; +import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.general.wear.wearintegration.WatchUpdaterService; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import io.reactivex.disposables.CompositeDisposable; -import io.reactivex.functions.Consumer; import io.reactivex.schedulers.Schedulers; /** @@ -72,7 +68,6 @@ public class WearPlugin extends PluginBase { @Override protected void onStart() { - MainApp.bus().register(this); if (watchUS != null) { watchUS.setSettings(); } @@ -81,15 +76,102 @@ public class WearPlugin extends PluginBase { disposable.add(RxBus.INSTANCE .toObservable(EventOpenAPSUpdateGui.class) .observeOn(Schedulers.io()) - .subscribe(eventOpenAPSUpdateGui -> sendDataToWatch(true, true, false), - error -> FabricPrivacy.logException(error) + .subscribe(event -> sendDataToWatch(true, true, false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventExtendedBolusChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendDataToWatch(true, true, false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventTempBasalChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendDataToWatch(true, true, false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventTreatmentChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendDataToWatch(true, true, false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventNewBasalProfile.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendDataToWatch(false, true, false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendDataToWatch(true, true, true), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + // possibly new high or low mark + resendDataToWatch(); + // status may be formated differently + sendDataToWatch(true, false, false); + }, FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventRefreshOverview.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (WatchUpdaterService.shouldReportLoopStatus(LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))) + sendDataToWatch(true, false, false); + }, FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventBolusRequested.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + String status = String.format(MainApp.gs(R.string.bolusrequested), event.getAmount()); + Intent intent = new Intent(ctx, WatchUpdaterService.class).setAction(WatchUpdaterService.ACTION_SEND_BOLUSPROGRESS); + intent.putExtra("progresspercent", 0); + intent.putExtra("progressstatus", status); + ctx.startService(intent); + }, FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventDismissBolusProgressIfRunning.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (event.getResult() == null) return; + String status; + if (event.getResult().success) { + status = MainApp.gs(R.string.success); + } else { + status = MainApp.gs(R.string.nosuccess); + } + Intent intent = new Intent(ctx, WatchUpdaterService.class).setAction(WatchUpdaterService.ACTION_SEND_BOLUSPROGRESS); + intent.putExtra("progresspercent", 100); + intent.putExtra("progressstatus", status); + ctx.startService(intent); + }, FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventOverviewBolusProgress.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (!event.isSMB() || SP.getBoolean("wear_notifySMB", true)) { + Intent intent = new Intent(ctx, WatchUpdaterService.class).setAction(WatchUpdaterService.ACTION_SEND_BOLUSPROGRESS); + intent.putExtra("progresspercent", event.getPercent()); + intent.putExtra("progressstatus", event.getStatus()); + ctx.startService(intent); + } + }, FabricPrivacy::logException )); } @Override protected void onStop() { - MainApp.bus().unregister(this); disposable.clear(); super.onStop(); } @@ -133,84 +215,6 @@ public class WearPlugin extends PluginBase { ctx.startService(intent); } - - @Subscribe - public void onStatusEvent(final EventPreferenceChange ev) { - // possibly new high or low mark - resendDataToWatch(); - // status may be formated differently - sendDataToWatch(true, false, false); - } - - @Subscribe - public void onStatusEvent(final EventTreatmentChange ev) { - sendDataToWatch(true, true, false); - } - - @Subscribe - public void onStatusEvent(final EventTempBasalChange ev) { - sendDataToWatch(true, true, false); - } - - @Subscribe - public void onStatusEvent(final EventExtendedBolusChange ev) { - sendDataToWatch(true, true, false); - } - - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - sendDataToWatch(true, true, true); - } - - @Subscribe - public void onStatusEvent(final EventNewBasalProfile ev) { - sendDataToWatch(false, true, false); - } - - @Subscribe - public void onStatusEvent(final EventRefreshOverview ev) { - if (WatchUpdaterService.shouldReportLoopStatus(LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))) { - sendDataToWatch(true, false, false); - } - } - - - @Subscribe - public void onStatusEvent(final EventOverviewBolusProgress ev) { - if (!ev.isSMB() || SP.getBoolean("wear_notifySMB", true)) { - Intent intent = new Intent(ctx, WatchUpdaterService.class).setAction(WatchUpdaterService.ACTION_SEND_BOLUSPROGRESS); - intent.putExtra("progresspercent", ev.percent); - intent.putExtra("progressstatus", ev.status); - ctx.startService(intent); - } - } - - @Subscribe - public void onStatusEvent(final EventBolusRequested ev) { - String status = String.format(MainApp.gs(R.string.bolusrequested), ev.getAmount()); - Intent intent = new Intent(ctx, WatchUpdaterService.class).setAction(WatchUpdaterService.ACTION_SEND_BOLUSPROGRESS); - intent.putExtra("progresspercent", 0); - intent.putExtra("progressstatus", status); - ctx.startService(intent); - - } - - @Subscribe - public void onStatusEvent(final EventDismissBolusprogressIfRunning ev) { - if (ev.result == null) return; - - String status; - if (ev.result.success) { - status = MainApp.gs(R.string.success); - } else { - status = MainApp.gs(R.string.nosuccess); - } - Intent intent = new Intent(ctx, WatchUpdaterService.class).setAction(WatchUpdaterService.ACTION_SEND_BOLUSPROGRESS); - intent.putExtra("progresspercent", 100); - intent.putExtra("progressstatus", status); - ctx.startService(intent); - } - public void requestActionConfirmation(String title, String message, String actionstring) { Intent intent = new Intent(ctx, WatchUpdaterService.class).setAction(WatchUpdaterService.ACTION_SEND_ACTIONCONFIRMATIONREQUEST); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/xdripStatusline/StatuslinePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/xdripStatusline/StatuslinePlugin.java index 2421a1561e..da61b83b98 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/xdripStatusline/StatuslinePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/xdripStatusline/StatuslinePlugin.java @@ -5,9 +5,8 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; -import androidx.annotation.NonNull; -import com.squareup.otto.Subscribe; +import androidx.annotation.NonNull; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -26,12 +25,16 @@ import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Created by adrian on 17/11/16. @@ -39,6 +42,8 @@ import info.nightscout.androidaps.utils.DecimalFormatter; public class StatuslinePlugin extends PluginBase { + private CompositeDisposable disposable = new CompositeDisposable(); + private static StatuslinePlugin statuslinePlugin; public static StatuslinePlugin getPlugin() { @@ -80,14 +85,64 @@ public class StatuslinePlugin extends PluginBase { @Override protected void onStart() { - MainApp.bus().register(this); super.onStart(); + disposable.add(RxBus.INSTANCE + .toObservable(EventRefreshOverview.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if ((lastLoopStatus != LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))) + sendStatus(); + }, + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventExtendedBolusChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendStatus(), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventTempBasalChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendStatus(), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventTreatmentChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendStatus(), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventConfigBuilderChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendStatus(), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendStatus(), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendStatus(), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppInitialized.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendStatus(), + FabricPrivacy::logException + )); } @Override protected void onStop() { super.onStop(); - MainApp.bus().unregister(this); + disposable.clear(); sendStatus(); } @@ -158,50 +213,4 @@ public class StatuslinePlugin extends PluginBase { return status; } - - - @Subscribe - public void onStatusEvent(final EventPreferenceChange ev) { - // status may be formated differently - sendStatus(); - } - - @Subscribe - public void onStatusEvent(final EventTreatmentChange ev) { - sendStatus(); - } - - @Subscribe - public void onStatusEvent(final EventTempBasalChange ev) { - sendStatus(); - } - - @Subscribe - public void onStatusEvent(final EventExtendedBolusChange ev) { - sendStatus(); - } - - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - sendStatus(); - } - - @Subscribe - public void onStatusEvent(final EventAppInitialized ev) { - sendStatus(); - } - - @Subscribe - public void onStatusEvent(final EventConfigBuilderChange ev) { - sendStatus(); - } - - @Subscribe - public void onStatusEvent(final EventRefreshOverview ev) { - //Filter events where loop is (de)activated - if ((lastLoopStatus != LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))) { - sendStatus(); - } - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java index 444b2db67a..67d46df22d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java @@ -1,12 +1,11 @@ package info.nightscout.androidaps.plugins.iob.iobCobCalculator; import android.os.SystemClock; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.collection.LongSparseArray; -import com.squareup.otto.Subscribe; - import org.json.JSONArray; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,16 +30,20 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData; -import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin; import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; import static info.nightscout.androidaps.utils.DateUtil.now; @@ -50,6 +53,7 @@ import static info.nightscout.androidaps.utils.DateUtil.now; public class IobCobCalculatorPlugin extends PluginBase { private Logger log = LoggerFactory.getLogger(L.AUTOSENS); + private CompositeDisposable disposable = new CompositeDisposable(); private static IobCobCalculatorPlugin plugin = null; @@ -83,14 +87,123 @@ public class IobCobCalculatorPlugin extends PluginBase { @Override protected void onStart() { - MainApp.bus().register(this); super.onStart(); + // EventConfigBuilderChange + disposable.add(RxBus.INSTANCE + .toObservable(EventConfigBuilderChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (this != getPlugin()) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Ignoring event for non default instance"); + return; + } + stopCalculation("onEventConfigBuilderChange"); + synchronized (dataLock) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Invalidating cached data because of configuration change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); + iobTable = new LongSparseArray<>(); + autosensDataTable = new LongSparseArray<>(); + } + runCalculation("onEventConfigBuilderChange", System.currentTimeMillis(), false, true, event); + }, FabricPrivacy::logException) + ); + // EventNewBasalProfile + disposable.add(RxBus.INSTANCE + .toObservable(EventNewBasalProfile.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (this != getPlugin()) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Ignoring event for non default instance"); + return; + } + if (ConfigBuilderPlugin.getPlugin() == null) + return; // app still initializing + if (event == null) { // on init no need of reset + return; + } + stopCalculation("onNewProfile"); + synchronized (dataLock) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Invalidating cached data because of new profile. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); + iobTable = new LongSparseArray<>(); + autosensDataTable = new LongSparseArray<>(); + basalDataTable = new LongSparseArray<>(); + } + runCalculation("onNewProfile", System.currentTimeMillis(), false, true, event); + }, FabricPrivacy::logException) + ); + // EventNewBG + disposable.add(RxBus.INSTANCE + .toObservable(EventNewBG.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (this != getPlugin()) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Ignoring event for non default instance"); + return; + } + stopCalculation("onEventNewBG"); + runCalculation("onEventNewBG", System.currentTimeMillis(), true, true, event); + }, FabricPrivacy::logException) + ); + // EventPreferenceChange + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (this != getPlugin()) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Ignoring event for non default instance"); + return; + } + if (event.isChanged(R.string.key_openapsama_autosens_period) || + event.isChanged(R.string.key_age) || + event.isChanged(R.string.key_absorption_maxtime) || + event.isChanged(R.string.key_openapsama_min_5m_carbimpact) || + event.isChanged(R.string.key_absorption_cutoff) || + event.isChanged(R.string.key_openapsama_autosens_max) || + event.isChanged(R.string.key_openapsama_autosens_min) || + event.isChanged(R.string.key_insulin_oref_peak) + ) { + stopCalculation("onEventPreferenceChange"); + synchronized (dataLock) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Invalidating cached data because of preference change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records" + " BasalData: " + basalDataTable.size() + " records"); + iobTable = new LongSparseArray<>(); + autosensDataTable = new LongSparseArray<>(); + basalDataTable = new LongSparseArray<>(); + } + runCalculation("onEventPreferenceChange", System.currentTimeMillis(), false, true, event); + } + }, FabricPrivacy::logException) + ); + // EventAppInitialized + disposable.add(RxBus.INSTANCE + .toObservable(EventAppInitialized.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (this != getPlugin()) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Ignoring event for non default instance"); + return; + } + runCalculation("onEventAppInitialized", System.currentTimeMillis(), true, true, event); + }, FabricPrivacy::logException) + ); + // EventNewHistoryData + disposable.add(RxBus.INSTANCE + .toObservable(EventNewHistoryData.class) + .observeOn(Schedulers.io()) + .subscribe(event -> newHistoryData(event), FabricPrivacy::logException) + ); } @Override protected void onStop() { + disposable.clear(); super.onStop(); - MainApp.bus().unregister(this); } public LongSparseArray getAutosensDataTable() { @@ -529,7 +642,7 @@ public class IobCobCalculatorPlugin extends PluginBase { count++; } } - return sum /count; + return sum / count; } @Nullable @@ -627,29 +740,6 @@ public class IobCobCalculatorPlugin extends PluginBase { return array; } - @Subscribe - @SuppressWarnings("unused") - public void onEventAppInitialized(EventAppInitialized ev) { - if (this != getPlugin()) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Ignoring event for non default instance"); - return; - } - runCalculation("onEventAppInitialized", System.currentTimeMillis(), true, true, ev); - } - - @Subscribe - @SuppressWarnings("unused") - public void onEventNewBG(EventNewBG ev) { - if (this != getPlugin()) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Ignoring event for non default instance"); - return; - } - stopCalculation("onEventNewBG"); - runCalculation("onEventNewBG", System.currentTimeMillis(), true, true, ev); - } - public void stopCalculation(String from) { if (thread != null && thread.getState() != Thread.State.TERMINATED) { stopCalculationTrigger = true; @@ -675,76 +765,8 @@ public class IobCobCalculatorPlugin extends PluginBase { } } - @Subscribe - public void onNewProfile(EventNewBasalProfile ev) { - if (this != getPlugin()) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Ignoring event for non default instance"); - return; - } - if (ConfigBuilderPlugin.getPlugin() == null) - return; // app still initializing - if (ev == null) { // on init no need of reset - return; - } - stopCalculation("onNewProfile"); - synchronized (dataLock) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Invalidating cached data because of new profile. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); - iobTable = new LongSparseArray<>(); - autosensDataTable = new LongSparseArray<>(); - basalDataTable = new LongSparseArray<>(); - } - runCalculation("onNewProfile", System.currentTimeMillis(), false, true, ev); - } - - @Subscribe - public void onEventPreferenceChange(EventPreferenceChange ev) { - if (this != getPlugin()) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Ignoring event for non default instance"); - return; - } - if (ev.isChanged(R.string.key_openapsama_autosens_period) || - ev.isChanged(R.string.key_age) || - ev.isChanged(R.string.key_absorption_maxtime) || - ev.isChanged(R.string.key_openapsama_min_5m_carbimpact) || - ev.isChanged(R.string.key_absorption_cutoff) || - ev.isChanged(R.string.key_openapsama_autosens_max) || - ev.isChanged(R.string.key_openapsama_autosens_min) - ) { - stopCalculation("onEventPreferenceChange"); - synchronized (dataLock) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Invalidating cached data because of preference change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records" + " BasalData: " + basalDataTable.size() + " records"); - iobTable = new LongSparseArray<>(); - autosensDataTable = new LongSparseArray<>(); - basalDataTable = new LongSparseArray<>(); - } - runCalculation("onEventPreferenceChange", System.currentTimeMillis(), false, true, ev); - } - } - - @Subscribe - public void onEventConfigBuilderChange(EventConfigBuilderChange ev) { - if (this != getPlugin()) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Ignoring event for non default instance"); - return; - } - stopCalculation("onEventConfigBuilderChange"); - synchronized (dataLock) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Invalidating cached data because of configuration change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); - iobTable = new LongSparseArray<>(); - autosensDataTable = new LongSparseArray<>(); - } - runCalculation("onEventConfigBuilderChange", System.currentTimeMillis(), false, true, ev); - } - // When historical data is changed (comming from NS etc) finished calculations after this date must be invalidated - @Subscribe - public void onEventNewHistoryData(EventNewHistoryData ev) { + public void newHistoryData(EventNewHistoryData ev) { if (this != getPlugin()) { if (L.isEnabled(L.AUTOSENS)) log.debug("Ignoring event for non default instance"); @@ -754,7 +776,7 @@ public class IobCobCalculatorPlugin extends PluginBase { stopCalculation("onEventNewHistoryData"); synchronized (dataLock) { // clear up 5 min back for proper COB calculation - long time = ev.time - 5 * 60 * 1000L; + long time = ev.getTime() - 5 * 60 * 1000L; if (L.isEnabled(L.AUTOSENS)) log.debug("Invalidating cached data to: " + DateUtil.dateAndTimeFullString(time)); for (int index = iobTable.size() - 1; index >= 0; index--) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java index 2b427602f5..07d1b52947 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java @@ -119,7 +119,7 @@ public class IobCobOref1Thread extends Thread { // start from oldest to be able sub cob for (int i = bucketed_data.size() - 4; i >= 0; i--) { String progress = i + (MainApp.isDev() ? " (" + from + ")" : ""); - MainApp.bus().post(new EventIobCalculationProgress(progress)); + RxBus.INSTANCE.send(new EventIobCalculationProgress(progress)); if (iobCobCalculatorPlugin.stopCalculationTrigger) { iobCobCalculatorPlugin.stopCalculationTrigger = false; @@ -387,12 +387,12 @@ public class IobCobOref1Thread extends Thread { } new Thread(() -> { SystemClock.sleep(1000); - MainApp.bus().post(new EventAutosensCalculationFinished(cause)); + RxBus.INSTANCE.send(new EventAutosensCalculationFinished(cause)); }).start(); } finally { if (mWakeLock != null) mWakeLock.release(); - MainApp.bus().post(new EventIobCalculationProgress("")); + RxBus.INSTANCE.send(new EventIobCalculationProgress("")); if (L.isEnabled(L.AUTOSENS)) { log.debug("AUTOSENSDATA thread ended: " + from); log.debug("Midnights: " + MidnightTime.log()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java index 756cee9853..a8ecc2a06a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.iob.iobCobCalculator; import android.content.Context; import android.os.PowerManager; import android.os.SystemClock; + import androidx.collection.LongSparseArray; import org.slf4j.Logger; @@ -117,7 +118,7 @@ public class IobCobThread extends Thread { // start from oldest to be able sub cob for (int i = bucketed_data.size() - 4; i >= 0; i--) { String progress = i + (MainApp.isDev() ? " (" + from + ")" : ""); - MainApp.bus().post(new EventIobCalculationProgress(progress)); + RxBus.INSTANCE.send(new EventIobCalculationProgress(progress)); if (iobCobCalculatorPlugin.stopCalculationTrigger) { iobCobCalculatorPlugin.stopCalculationTrigger = false; @@ -312,13 +313,12 @@ public class IobCobThread extends Thread { } new Thread(() -> { SystemClock.sleep(1000); - MainApp.bus().post(new EventAutosensCalculationFinished(cause)); RxBus.INSTANCE.send(new EventAutosensCalculationFinished(cause)); }).start(); } finally { if (mWakeLock != null) mWakeLock.release(); - MainApp.bus().post(new EventIobCalculationProgress("")); + RxBus.INSTANCE.send(new EventIobCalculationProgress("")); if (L.isEnabled(L.AUTOSENS)) { log.debug("AUTOSENSDATA thread ended: " + from); log.debug("Midnights: " + MidnightTime.log()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventIobCalculationProgress.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventIobCalculationProgress.java deleted file mode 100644 index 5a4a9a8eae..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventIobCalculationProgress.java +++ /dev/null @@ -1,11 +0,0 @@ -package info.nightscout.androidaps.plugins.iob.iobCobCalculator.events; - -import info.nightscout.androidaps.events.Event; - -public class EventIobCalculationProgress extends Event { - public String progress; - - public EventIobCalculationProgress(String progress) { - this.progress = progress; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventIobCalculationProgress.kt b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventIobCalculationProgress.kt new file mode 100644 index 0000000000..f2e8059b10 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventIobCalculationProgress.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.iob.iobCobCalculator.events + +import info.nightscout.androidaps.events.Event + +class EventIobCalculationProgress(var progress: String) : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventNewHistoryData.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventNewHistoryData.java deleted file mode 100644 index f84fdd1adf..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventNewHistoryData.java +++ /dev/null @@ -1,15 +0,0 @@ -package info.nightscout.androidaps.plugins.iob.iobCobCalculator.events; - -import info.nightscout.androidaps.events.Event; - -/** - * Created by mike on 26.04.2017. - */ - -public class EventNewHistoryData extends Event { - public long time = 0; - - public EventNewHistoryData(long time) { - this.time = time; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventNewHistoryData.kt b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventNewHistoryData.kt new file mode 100644 index 0000000000..c2372aff83 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/events/EventNewHistoryData.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.iob.iobCobCalculator.events + +import info.nightscout.androidaps.events.Event + +class EventNewHistoryData(var time: Long) : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java index a8f77ea54c..f8969e9594 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java @@ -13,8 +13,7 @@ import android.widget.RadioButton; import android.widget.TextView; import androidx.annotation.NonNull; - -import com.squareup.otto.Subscribe; +import androidx.fragment.app.Fragment; import java.text.DecimalFormat; @@ -23,19 +22,24 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.interfaces.PumpDescription; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment; import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.NumberPicker; import info.nightscout.androidaps.utils.SafeParse; import info.nightscout.androidaps.utils.TimeListEdit; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; import static info.nightscout.androidaps.plugins.insulin.InsulinOrefBasePlugin.MIN_DIA; -public class LocalProfileFragment extends SubscriberFragment { +public class LocalProfileFragment extends Fragment { + private CompositeDisposable disposable = new CompositeDisposable(); + private NumberPicker diaView; private RadioButton mgdlView; private RadioButton mmolView; @@ -143,6 +147,23 @@ public class LocalProfileFragment extends SubscriberFragment { return layout; } + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventInitializationChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI(), FabricPrivacy::logException) + ); + updateGUI(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + public void doEdit() { LocalProfilePlugin.getPlugin().setEdited(true); updateGUI(); @@ -157,12 +178,6 @@ public class LocalProfileFragment extends SubscriberFragment { return MainApp.gs(R.string.localprofile); } - @Subscribe - public void onStatusEvent(final EventInitializationChanged e) { - updateGUI(); - } - - @Override protected void updateGUI() { Activity activity = getActivity(); if (activity != null) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.java index 089611b40d..e3e068c1cb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.java @@ -18,6 +18,7 @@ import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.SP; @@ -83,7 +84,7 @@ public class LocalProfilePlugin extends PluginBase implements ProfileInterface { edited = false; if (L.isEnabled(L.PROFILE)) log.debug("Storing settings: " + getRawProfile().getData().toString()); - MainApp.bus().post(new EventProfileStoreChanged()); + RxBus.INSTANCE.send(new EventProfileStoreChanged()); } public synchronized void loadSettings() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfilePlugin.java index a6088f5515..38afe62bdf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfilePlugin.java @@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.events.EventProfileStoreChanged; @@ -58,13 +57,11 @@ public class NSProfilePlugin extends PluginBase implements ProfileInterface { @Override protected void onStart() { - MainApp.bus().register(this); super.onStart(); } @Override protected void onStop() { - MainApp.bus().unregister(this); super.onStop(); } @@ -78,7 +75,7 @@ public class NSProfilePlugin extends PluginBase implements ProfileInterface { profile = new ProfileStore(new JSONObject(profileString)); storeNSProfile(); if (isEnabled(PluginType.PROFILE)) { - MainApp.bus().post(new EventProfileStoreChanged()); + RxBus.INSTANCE.send(new EventProfileStoreChanged()); RxBus.INSTANCE.send(new EventNSProfileUpdateGUI()); } if (L.isEnabled(L.PROFILE)) @@ -111,7 +108,7 @@ public class NSProfilePlugin extends PluginBase implements ProfileInterface { if (L.isEnabled(L.PROFILE)) log.debug("Stored profile not found"); // force restart of nsclient to fetch profile - MainApp.bus().post(new EventNSClientRestart()); + RxBus.INSTANCE.send(new EventNSClientRestart()); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java index b8a7d16be5..ecf4fc423b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java @@ -1,7 +1,6 @@ package info.nightscout.androidaps.plugins.profile.simple; -import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -13,19 +12,24 @@ import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.fragment.app.Fragment; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventInitializationChanged; +import info.nightscout.androidaps.plugins.bus.RxBus; +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment; import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SafeParse; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; + +public class SimpleProfileFragment extends Fragment { + private CompositeDisposable disposable = new CompositeDisposable(); -public class SimpleProfileFragment extends SubscriberFragment { EditText diaView; RadioButton mgdlView; RadioButton mmolView; @@ -122,27 +126,34 @@ public class SimpleProfileFragment extends SubscriberFragment { return layout; } - @Subscribe - public void onStatusEvent(final EventInitializationChanged e) { + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventInitializationChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI(), FabricPrivacy::logException) + ); updateGUI(); } @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - boolean isValid = SimpleProfilePlugin.getPlugin().getProfile() != null && SimpleProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid(MainApp.gs(R.string.simpleprofile)); - if (!ConfigBuilderPlugin.getPlugin().getActivePump().isInitialized() || ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended() || !isValid) { - profileswitchButton.setVisibility(View.GONE); - } else { - profileswitchButton.setVisibility(View.VISIBLE); - } - if (isValid) - invalidProfile.setVisibility(View.GONE); - else - invalidProfile.setVisibility(View.VISIBLE); - }); + boolean isValid = SimpleProfilePlugin.getPlugin().getProfile() != null && SimpleProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid(MainApp.gs(R.string.simpleprofile)); + if (!ConfigBuilderPlugin.getPlugin().getActivePump().isInitialized() || ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended() || !isValid) { + profileswitchButton.setVisibility(View.GONE); + } else { + profileswitchButton.setVisibility(View.VISIBLE); + } + if (isValid) + invalidProfile.setVisibility(View.GONE); + else + invalidProfile.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfilePlugin.java index d3d3952fae..61ca403f55 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfilePlugin.java @@ -19,6 +19,7 @@ import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.utils.SP; /** @@ -75,7 +76,7 @@ public class SimpleProfilePlugin extends PluginBase implements ProfileInterface createConvertedProfile(); if (L.isEnabled(L.PROFILE)) log.debug("Storing settings: " + getRawProfile().getData().toString()); - MainApp.bus().post(new EventProfileStoreChanged()); + RxBus.INSTANCE.send(new EventProfileStoreChanged()); } private void loadSettings() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java index 1a924fa9f5..bfd5c6104e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java @@ -5,28 +5,33 @@ import android.app.Activity; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; -import androidx.annotation.NonNull; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; -import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.PumpState; -import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.history.Bolus; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.pump.combo.events.EventComboPumpUpdateGUI; +import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.PumpState; +import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.history.Bolus; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.events.EventQueueChanged; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; + +public class ComboFragment extends Fragment implements View.OnClickListener { + private CompositeDisposable disposable = new CompositeDisposable(); -public class ComboFragment extends SubscriberFragment implements View.OnClickListener { private TextView stateView; private TextView activityView; private TextView batteryView; @@ -58,10 +63,31 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis refreshButton = view.findViewById(R.id.combo_refresh_button); refreshButton.setOnClickListener(this); - updateGUI(); return view; } + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventComboPumpUpdateGUI.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventQueueChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + updateGui(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + private void runOnUiThread(Runnable action) { Activity activity = getActivity(); if (activity != null) { @@ -84,148 +110,135 @@ public class ComboFragment extends SubscriberFragment implements View.OnClickLis } } - @Subscribe - public void onStatusEvent(final EventComboPumpUpdateGUI ignored) { - updateGUI(); - } + public void updateGui() { + ComboPlugin plugin = ComboPlugin.getPlugin(); - @Subscribe - public void onStatusEvent(final EventQueueChanged ignored) { - updateGUI(); - } + // state + stateView.setText(plugin.getStateSummary()); + PumpState ps = plugin.getPump().state; + if (ps.insulinState == PumpState.EMPTY || ps.batteryState == PumpState.EMPTY + || ps.activeAlert != null && ps.activeAlert.errorCode != null) { + stateView.setTextColor(Color.RED); + stateView.setTypeface(null, Typeface.BOLD); + } else if (plugin.getPump().state.suspended + || ps.activeAlert != null && ps.activeAlert.warningCode != null) { + stateView.setTextColor(Color.YELLOW); + stateView.setTypeface(null, Typeface.BOLD); + } else { + stateView.setTextColor(Color.WHITE); + stateView.setTypeface(null, Typeface.NORMAL); + } + // activity + String activity = plugin.getPump().activity; + if (activity != null) { + activityView.setTextColor(Color.WHITE); + activityView.setTextSize(14); + activityView.setText(activity); + } else if (ConfigBuilderPlugin.getPlugin().getCommandQueue().size() > 0) { + activityView.setTextColor(Color.WHITE); + activityView.setTextSize(14); + activityView.setText(""); + } else if (plugin.isInitialized()) { + activityView.setTextColor(Color.WHITE); + activityView.setTextSize(20); + activityView.setText("{fa-bed}"); + } else { + activityView.setTextColor(Color.RED); + activityView.setTextSize(14); + activityView.setText(MainApp.gs(R.string.pump_unreachable)); + } - public void updateGUI() { - runOnUiThread(() -> { - ComboPlugin plugin = ComboPlugin.getPlugin(); - - // state - stateView.setText(plugin.getStateSummary()); - PumpState ps = plugin.getPump().state; - if (ps.insulinState == PumpState.EMPTY || ps.batteryState == PumpState.EMPTY - || ps.activeAlert != null && ps.activeAlert.errorCode != null) { - stateView.setTextColor(Color.RED); - stateView.setTypeface(null, Typeface.BOLD); - } else if (plugin.getPump().state.suspended - || ps.activeAlert != null && ps.activeAlert.warningCode != null) { - stateView.setTextColor(Color.YELLOW); - stateView.setTypeface(null, Typeface.BOLD); + if (plugin.isInitialized()) { + // battery + batteryView.setTextSize(20); + if (ps.batteryState == PumpState.EMPTY) { + batteryView.setText("{fa-battery-empty}"); + batteryView.setTextColor(Color.RED); + } else if (ps.batteryState == PumpState.LOW) { + batteryView.setText("{fa-battery-quarter}"); + batteryView.setTextColor(Color.YELLOW); } else { - stateView.setTextColor(Color.WHITE); - stateView.setTypeface(null, Typeface.NORMAL); + batteryView.setText("{fa-battery-full}"); + batteryView.setTextColor(Color.WHITE); } - // activity - String activity = plugin.getPump().activity; - if (activity != null) { - activityView.setTextColor(Color.WHITE); - activityView.setTextSize(14); - activityView.setText(activity); - } else if (ConfigBuilderPlugin.getPlugin().getCommandQueue().size() > 0) { - activityView.setTextColor(Color.WHITE); - activityView.setTextSize(14); - activityView.setText(""); - } else if (plugin.isInitialized()){ - activityView.setTextColor(Color.WHITE); - activityView.setTextSize(20); - activityView.setText("{fa-bed}"); + // reservoir + int reservoirLevel = plugin.getPump().reservoirLevel; + if (reservoirLevel != -1) { + reservoirView.setText(reservoirLevel + " " + MainApp.gs(R.string.insulin_unit_shortname)); + } else if (ps.insulinState == PumpState.LOW) { + reservoirView.setText(MainApp.gs(R.string.combo_reservoir_low)); + } else if (ps.insulinState == PumpState.EMPTY) { + reservoirView.setText(MainApp.gs(R.string.combo_reservoir_empty)); } else { - activityView.setTextColor(Color.RED); - activityView.setTextSize(14); - activityView.setText(MainApp.gs(R.string.pump_unreachable)); + reservoirView.setText(MainApp.gs(R.string.combo_reservoir_normal)); } - if (plugin.isInitialized()) { - // battery - batteryView.setTextSize(20); - if (ps.batteryState == PumpState.EMPTY) { - batteryView.setText("{fa-battery-empty}"); - batteryView.setTextColor(Color.RED); - } else if (ps.batteryState == PumpState.LOW) { - batteryView.setText("{fa-battery-quarter}"); - batteryView.setTextColor(Color.YELLOW); - } else { - batteryView.setText("{fa-battery-full}"); - batteryView.setTextColor(Color.WHITE); - } - - // reservoir - int reservoirLevel = plugin.getPump().reservoirLevel; - if (reservoirLevel != -1) { - reservoirView.setText(reservoirLevel + " " + MainApp.gs(R.string.insulin_unit_shortname)); - } else if (ps.insulinState == PumpState.LOW) { - reservoirView.setText(MainApp.gs(R.string.combo_reservoir_low)); - } else if (ps.insulinState == PumpState.EMPTY) { - reservoirView.setText(MainApp.gs(R.string.combo_reservoir_empty)); - } else { - reservoirView.setText(MainApp.gs(R.string.combo_reservoir_normal)); - } - - if (ps.insulinState == PumpState.UNKNOWN) { - reservoirView.setTextColor(Color.WHITE); - reservoirView.setTypeface(null, Typeface.NORMAL); - } else if (ps.insulinState == PumpState.LOW) { - reservoirView.setTextColor(Color.YELLOW); - reservoirView.setTypeface(null, Typeface.BOLD); - } else if (ps.insulinState == PumpState.EMPTY) { - reservoirView.setTextColor(Color.RED); - reservoirView.setTypeface(null, Typeface.BOLD); - } else { - reservoirView.setTextColor(Color.WHITE); - reservoirView.setTypeface(null, Typeface.NORMAL); - } - - // last connection - String minAgo = DateUtil.minAgo(plugin.getPump().lastSuccessfulCmdTime); - long min = (System.currentTimeMillis() - plugin.getPump().lastSuccessfulCmdTime) / 1000 / 60; - if (plugin.getPump().lastSuccessfulCmdTime + 60 * 1000 > System.currentTimeMillis()) { - lastConnectionView.setText(R.string.combo_pump_connected_now); - lastConnectionView.setTextColor(Color.WHITE); - } else if (plugin.getPump().lastSuccessfulCmdTime + 30 * 60 * 1000 < System.currentTimeMillis()) { - lastConnectionView.setText(MainApp.gs(R.string.combo_no_pump_connection, min)); - lastConnectionView.setTextColor(Color.RED); - } else { - lastConnectionView.setText(minAgo); - lastConnectionView.setTextColor(Color.WHITE); - } - - // last bolus - Bolus bolus = plugin.getPump().lastBolus; - if (bolus != null) { - long agoMsc = System.currentTimeMillis() - bolus.timestamp; - double bolusMinAgo = agoMsc / 60d / 1000d; - String unit = MainApp.gs(R.string.insulin_unit_shortname); - String ago; - if ((agoMsc < 60 * 1000)) { - ago = MainApp.gs(R.string.combo_pump_connected_now); - } else if (bolusMinAgo < 60) { - ago = DateUtil.minAgo(bolus.timestamp); - } else { - ago = DateUtil.hourAgo(bolus.timestamp); - } - lastBolusView.setText(MainApp.gs(R.string.combo_last_bolus, bolus.amount, unit, ago)); - } else { - lastBolusView.setText(""); - } - - // base basal rate - baseBasalRate.setText(MainApp.gs(R.string.pump_basebasalrate, plugin.getBaseBasalRate())); - - // TBR - String tbrStr = ""; - if (ps.tbrPercent != -1 && ps.tbrPercent != 100) { - long minSinceRead = (System.currentTimeMillis() - plugin.getPump().state.timestamp) / 1000 / 60; - long remaining = ps.tbrRemainingDuration - minSinceRead; - if (remaining >= 0) { - tbrStr = MainApp.gs(R.string.combo_tbr_remaining, ps.tbrPercent, remaining); - } - } - tempBasalText.setText(tbrStr); - - // stats - bolusCount.setText(String.valueOf(SP.getLong(ComboPlugin.COMBO_BOLUSES_DELIVERED, 0L))); - tbrCount.setText(String.valueOf(SP.getLong(ComboPlugin.COMBO_TBRS_SET, 0L))); + if (ps.insulinState == PumpState.UNKNOWN) { + reservoirView.setTextColor(Color.WHITE); + reservoirView.setTypeface(null, Typeface.NORMAL); + } else if (ps.insulinState == PumpState.LOW) { + reservoirView.setTextColor(Color.YELLOW); + reservoirView.setTypeface(null, Typeface.BOLD); + } else if (ps.insulinState == PumpState.EMPTY) { + reservoirView.setTextColor(Color.RED); + reservoirView.setTypeface(null, Typeface.BOLD); + } else { + reservoirView.setTextColor(Color.WHITE); + reservoirView.setTypeface(null, Typeface.NORMAL); } - }); + + // last connection + String minAgo = DateUtil.minAgo(plugin.getPump().lastSuccessfulCmdTime); + long min = (System.currentTimeMillis() - plugin.getPump().lastSuccessfulCmdTime) / 1000 / 60; + if (plugin.getPump().lastSuccessfulCmdTime + 60 * 1000 > System.currentTimeMillis()) { + lastConnectionView.setText(R.string.combo_pump_connected_now); + lastConnectionView.setTextColor(Color.WHITE); + } else if (plugin.getPump().lastSuccessfulCmdTime + 30 * 60 * 1000 < System.currentTimeMillis()) { + lastConnectionView.setText(MainApp.gs(R.string.combo_no_pump_connection, min)); + lastConnectionView.setTextColor(Color.RED); + } else { + lastConnectionView.setText(minAgo); + lastConnectionView.setTextColor(Color.WHITE); + } + + // last bolus + Bolus bolus = plugin.getPump().lastBolus; + if (bolus != null) { + long agoMsc = System.currentTimeMillis() - bolus.timestamp; + double bolusMinAgo = agoMsc / 60d / 1000d; + String unit = MainApp.gs(R.string.insulin_unit_shortname); + String ago; + if ((agoMsc < 60 * 1000)) { + ago = MainApp.gs(R.string.combo_pump_connected_now); + } else if (bolusMinAgo < 60) { + ago = DateUtil.minAgo(bolus.timestamp); + } else { + ago = DateUtil.hourAgo(bolus.timestamp); + } + lastBolusView.setText(MainApp.gs(R.string.combo_last_bolus, bolus.amount, unit, ago)); + } else { + lastBolusView.setText(""); + } + + // base basal rate + baseBasalRate.setText(MainApp.gs(R.string.pump_basebasalrate, plugin.getBaseBasalRate())); + + // TBR + String tbrStr = ""; + if (ps.tbrPercent != -1 && ps.tbrPercent != 100) { + long minSinceRead = (System.currentTimeMillis() - plugin.getPump().state.timestamp) / 1000 / 60; + long remaining = ps.tbrRemainingDuration - minSinceRead; + if (remaining >= 0) { + tbrStr = MainApp.gs(R.string.combo_tbr_remaining, ps.tbrPercent, remaining); + } + } + tempBasalText.setText(tbrStr); + + // stats + bolusCount.setText(String.valueOf(SP.getLong(ComboPlugin.COMBO_BOLUSES_DELIVERED, 0L))); + tbrCount.setText(String.valueOf(SP.getLong(ComboPlugin.COMBO_TBRS_SET, 0L))); + } } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java index 7058252eb3..32a17f8a89 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java @@ -375,7 +375,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint setValidBasalRateProfileSelectedOnPump(true); pump.initialized = true; - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventInitializationChanged()); // show notification to check pump date if last bolus is older than 24 hours // or is in the future @@ -390,7 +390,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint // ComboFragment updates state fully only after the pump has initialized, // so force an update after initialization completed - MainApp.bus().post(new EventComboPumpUpdateGUI()); + RxBus.INSTANCE.send(new EventComboPumpUpdateGUI()); } /** @@ -406,7 +406,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint if (result.state.menu != null) { pump.state = result.state; } - MainApp.bus().post(new EventComboPumpUpdateGUI()); + RxBus.INSTANCE.send(new EventComboPumpUpdateGUI()); } @Override @@ -433,26 +433,26 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint } private static BolusProgressReporter bolusProgressReporter = (state, percent, delivered) -> { - EventOverviewBolusProgress event = EventOverviewBolusProgress.getInstance(); + EventOverviewBolusProgress event = EventOverviewBolusProgress.INSTANCE; switch (state) { case PROGRAMMING: - event.status = MainApp.gs(R.string.combo_programming_bolus); + event.setStatus(MainApp.gs(R.string.combo_programming_bolus)); break; case DELIVERING: - event.status = MainApp.gs(R.string.bolusdelivering, delivered); + event.setStatus(MainApp.gs(R.string.bolusdelivering, delivered)); break; case DELIVERED: - event.status = MainApp.gs(R.string.bolusdelivered, delivered); + event.setStatus(MainApp.gs(R.string.bolusdelivered, delivered)); break; case STOPPING: - event.status = MainApp.gs(R.string.bolusstopping); + event.setStatus(MainApp.gs(R.string.bolusstopping)); break; case STOPPED: - event.status = MainApp.gs(R.string.bolusstopped); + event.setStatus(MainApp.gs(R.string.bolusstopped)); break; } - event.percent = percent; - MainApp.bus().post(event); + event.setPercent(percent); + RxBus.INSTANCE.send(event); }; /** @@ -474,18 +474,18 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint // no bolus required, carb only treatment TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.t = new Treatment(); - bolusingEvent.t.isSMB = detailedBolusInfo.isSMB; - bolusingEvent.percent = 100; - MainApp.bus().post(bolusingEvent); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setT(new Treatment()); + bolusingEvent.getT().isSMB = detailedBolusInfo.isSMB; + bolusingEvent.setPercent(100); + RxBus.INSTANCE.send(bolusingEvent); return new PumpEnactResult().success(true).enacted(true) .bolusDelivered(0d).carbsDelivered(detailedBolusInfo.carbs) .comment(MainApp.gs(R.string.virtualpump_resultok)); } } finally { - MainApp.bus().post(new EventComboPumpUpdateGUI()); + RxBus.INSTANCE.send(new EventComboPumpUpdateGUI()); } } @@ -493,7 +493,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint private PumpEnactResult deliverBolus(final DetailedBolusInfo detailedBolusInfo) { try { pump.activity = MainApp.gs(R.string.combo_pump_action_bolusing, detailedBolusInfo.insulin); - MainApp.bus().post(new EventComboPumpUpdateGUI()); + RxBus.INSTANCE.send(new EventComboPumpUpdateGUI()); // check pump is ready and all pump bolus records are known CommandResult stateResult = runCommand(null, 2, () -> ruffyScripter.readQuickInfo(1)); @@ -558,7 +558,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint Treatment treatment = new Treatment(); treatment.isSMB = detailedBolusInfo.isSMB; - EventOverviewBolusProgress.getInstance().t = treatment; + EventOverviewBolusProgress.INSTANCE.setT(treatment); // start bolus delivery scripterIsBolusing = true; @@ -628,7 +628,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint .carbsDelivered(detailedBolusInfo.carbs); } finally { pump.activity = null; - MainApp.bus().post(new EventComboPumpUpdateGUI()); + RxBus.INSTANCE.send(new EventComboPumpUpdateGUI()); RxBus.INSTANCE.send(new EventRefreshOverview("Bolus")); cancelBolus = false; } @@ -760,7 +760,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint .source(Source.USER); TreatmentsPlugin.getPlugin().addToHistoryTempBasal(tempStart); - MainApp.bus().post(new EventComboPumpUpdateGUI()); + RxBus.INSTANCE.send(new EventComboPumpUpdateGUI()); } incrementTbrCount(); @@ -848,7 +848,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint if (!ruffyScripter.isConnected()) { String originalActivity = pump.activity; pump.activity = MainApp.gs(R.string.combo_activity_checking_pump_state); - MainApp.bus().post(new EventComboPumpUpdateGUI()); + RxBus.INSTANCE.send(new EventComboPumpUpdateGUI()); CommandResult preCheckError = runOnConnectChecks(); pump.activity = originalActivity; if (preCheckError != null) { @@ -859,7 +859,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint if (activity != null) { pump.activity = activity; - MainApp.bus().post(new EventComboPumpUpdateGUI()); + RxBus.INSTANCE.send(new EventComboPumpUpdateGUI()); } commandResult = commandExecution.execute(); @@ -892,7 +892,7 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint } finally { if (activity != null) { pump.activity = null; - MainApp.bus().post(new EventComboPumpUpdateGUI()); + RxBus.INSTANCE.send(new EventComboPumpUpdateGUI()); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/events/EventComboPumpUpdateGUI.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/events/EventComboPumpUpdateGUI.java deleted file mode 100644 index 72359b7db2..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/events/EventComboPumpUpdateGUI.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.combo.events; - -/** - * Created by mike on 24.05.2017. - */ - -public class EventComboPumpUpdateGUI { -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/events/EventComboPumpUpdateGUI.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/events/EventComboPumpUpdateGUI.kt new file mode 100644 index 0000000000..604b55cd25 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/events/EventComboPumpUpdateGUI.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.pump.combo.events + +import info.nightscout.androidaps.events.EventUpdateGui + +class EventComboPumpUpdateGUI : EventUpdateGui() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java index fa68e55616..b1d5db0e2a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java @@ -6,8 +6,6 @@ import android.content.ServiceConnection; import androidx.fragment.app.FragmentActivity; -import com.squareup.otto.Subscribe; - import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; @@ -31,14 +29,19 @@ import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.pump.common.data.PumpStatus; import info.nightscout.androidaps.plugins.pump.common.defs.PumpDriverState; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; +import info.nightscout.androidaps.plugins.pump.medtronic.data.MedtronicHistoryData; import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Created by andy on 23.04.18. @@ -47,13 +50,14 @@ import info.nightscout.androidaps.utils.DecimalFormatter; // When using this class, make sure that your first step is to create mConnection (see MedtronicPumpPlugin) public abstract class PumpPluginAbstract extends PluginBase implements PumpInterface, ConstraintsInterface { + private CompositeDisposable disposable = new CompositeDisposable(); private static final Logger LOG = LoggerFactory.getLogger(L.PUMP); protected static final PumpEnactResult OPERATION_NOT_SUPPORTED = new PumpEnactResult().success(false) - .enacted(false).comment(MainApp.gs(R.string.pump_operation_not_supported_by_pump_driver)); + .enacted(false).comment(MainApp.gs(R.string.pump_operation_not_supported_by_pump_driver)); protected static final PumpEnactResult OPERATION_NOT_YET_SUPPORTED = new PumpEnactResult().success(false) - .enacted(false).comment(MainApp.gs(R.string.pump_operation_not_yet_supported_by_pump)); + .enacted(false).comment(MainApp.gs(R.string.pump_operation_not_yet_supported_by_pump)); protected PumpDescription pumpDescription = new PumpDescription(); protected PumpStatus pumpStatus; @@ -80,15 +84,21 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter @Override protected void onStart() { + super.onStart(); Context context = MainApp.instance().getApplicationContext(); Intent intent = new Intent(context, getServiceClass()); context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); serviceRunning = true; - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + MainApp.instance().getApplicationContext().unbindService(serviceConnection); + }, FabricPrivacy::logException) + ); onStartCustomActions(); - super.onStart(); } @@ -99,7 +109,7 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter serviceRunning = false; - MainApp.bus().unregister(this); + disposable.clear(); super.onStop(); } @@ -121,14 +131,6 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter */ public abstract Class getServiceClass(); - - @SuppressWarnings("UnusedParameters") - @Subscribe - public void onStatusEvent(final EventAppExit e) { - MainApp.instance().getApplicationContext().unbindService(serviceConnection); - } - - public PumpStatus getPumpStatusData() { return pumpStatus; } @@ -239,7 +241,7 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter @Override public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, - boolean enforceNew) { + boolean enforceNew) { if (isLoggingEnabled()) LOG.warn("setTempBasalAbsolute [PumpPluginAbstract] - Not implemented."); return getOperationNotSupportedWithCustomText(R.string.pump_operation_not_supported_by_pump_driver); @@ -248,7 +250,7 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter @Override public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, - boolean enforceNew) { + boolean enforceNew) { if (isLoggingEnabled()) LOG.warn("setTempBasalPercent [PumpPluginAbstract] - Not implemented."); return getOperationNotSupportedWithCustomText(R.string.pump_operation_not_supported_by_pump_driver); @@ -340,7 +342,7 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter TemporaryBasal tb = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(System.currentTimeMillis()); if (tb != null) { extended.put("TempBasalAbsoluteRate", - tb.tempBasalConvertedToAbsolute(System.currentTimeMillis(), profile)); + tb.tempBasalConvertedToAbsolute(System.currentTimeMillis(), profile)); extended.put("TempBasalStart", DateUtil.dateAndTimeString(tb.date)); extended.put("TempBasalRemaining", tb.getPlannedRemainingMinutes()); } @@ -372,20 +374,20 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter String ret = ""; if (pumpStatus.lastConnection != 0) { Long agoMsec = System.currentTimeMillis() - pumpStatus.lastConnection; - int agoMin = (int)(agoMsec / 60d / 1000d); + int agoMin = (int) (agoMsec / 60d / 1000d); ret += "LastConn: " + agoMin + " min ago\n"; } if (pumpStatus.lastBolusTime != null && pumpStatus.lastBolusTime.getTime() != 0) { ret += "LastBolus: " + DecimalFormatter.to2Decimal(pumpStatus.lastBolusAmount) + "U @" + // - android.text.format.DateFormat.format("HH:mm", pumpStatus.lastBolusTime) + "\n"; + android.text.format.DateFormat.format("HH:mm", pumpStatus.lastBolusTime) + "\n"; } TemporaryBasal activeTemp = TreatmentsPlugin.getPlugin() - .getRealTempBasalFromHistory(System.currentTimeMillis()); + .getRealTempBasalFromHistory(System.currentTimeMillis()); if (activeTemp != null) { ret += "Temp: " + activeTemp.toStringFull() + "\n"; } ExtendedBolus activeExtendedBolus = TreatmentsPlugin.getPlugin().getExtendedBolusFromHistory( - System.currentTimeMillis()); + System.currentTimeMillis()); if (activeExtendedBolus != null) { ret += "Extended: " + activeExtendedBolus.toString() + "\n"; } @@ -409,25 +411,28 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter if (isLoggingEnabled()) LOG.error("deliverTreatment: Invalid input"); return new PumpEnactResult().success(false).enacted(false).bolusDelivered(0d).carbsDelivered(0d) - .comment(MainApp.gs(R.string.danar_invalidinput)); + .comment(MainApp.gs(R.string.danar_invalidinput)); } else if (detailedBolusInfo.insulin > 0) { // bolus needed, ask pump to deliver it return deliverBolus(detailedBolusInfo); } else { + if (MedtronicHistoryData.doubleBolusDebug) + LOG.debug("DoubleBolusDebug: deliverTreatment::(carb only entry)"); + // no bolus required, carb only treatment TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, true); - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.t = new Treatment(); - bolusingEvent.t.isSMB = detailedBolusInfo.isSMB; - bolusingEvent.percent = 100; - MainApp.bus().post(bolusingEvent); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setT(new Treatment()); + bolusingEvent.getT().isSMB = detailedBolusInfo.isSMB; + bolusingEvent.setPercent(100); + RxBus.INSTANCE.send(bolusingEvent); if (isLoggingEnabled()) LOG.debug("deliverTreatment: Carb only treatment."); return new PumpEnactResult().success(true).enacted(true).bolusDelivered(0d) - .carbsDelivered(detailedBolusInfo.carbs).comment(MainApp.gs(R.string.virtualpump_resultok)); + .carbsDelivered(detailedBolusInfo.carbs).comment(MainApp.gs(R.string.virtualpump_resultok)); } } finally { triggerUIChange(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/bolusInfo/DetailedBolusInfoStorage.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/bolusInfo/DetailedBolusInfoStorage.kt index 293babfc2b..d7833a4fe4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/bolusInfo/DetailedBolusInfoStorage.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/bolusInfo/DetailedBolusInfoStorage.kt @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.pump.common.bolusInfo import info.nightscout.androidaps.data.DetailedBolusInfo import info.nightscout.androidaps.logging.L +import info.nightscout.androidaps.plugins.pump.medtronic.data.MedtronicHistoryData import info.nightscout.androidaps.utils.T import org.slf4j.LoggerFactory import java.util.* @@ -19,6 +20,10 @@ object DetailedBolusInfoStorage { @Synchronized fun findDetailedBolusInfo(bolusTime: Long, bolus: Double): DetailedBolusInfo? { + + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: findDetailedBolusInfo::bolusTime={}, bolus={}", bolusTime, bolus) + // Look for info with bolus for (i in store.indices) { val d = store[i] @@ -28,6 +33,9 @@ object DetailedBolusInfoStorage { if (L.isEnabled(L.PUMP)) log.debug("Using & removing bolus info: " + store[i]) store.removeAt(i) + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: findDetailedBolusInfo::selectedBolus[DetailedBolusInfo={}]", d) + return d } } @@ -38,6 +46,8 @@ object DetailedBolusInfoStorage { if (L.isEnabled(L.PUMP)) log.debug("Using & removing bolus info: " + store[i]) store.removeAt(i) + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: findDetailedBolusInfo::selectedBolus[DetailedBolusInfo={}]", d) return d } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bLoop.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bLoop.java index 3e551b6ccf..a7b005b9b8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bLoop.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/data/encoding/Encoding4b6bLoop.java @@ -16,6 +16,7 @@ import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; */ public class Encoding4b6bLoop extends Encoding4b6bAbstract { + private static final Logger log = LoggerFactory.getLogger(Encoding4b6bLoop.class); public static final Logger LOG = LoggerFactory.getLogger(Encoding4b6bLoop.class); public Map codesRev = null; @@ -108,9 +109,8 @@ public class Encoding4b6bLoop extends Encoding4b6bAbstract { int index2 = ((bitAccumulator >> (availBits - 12)) & 0b111111); hiNibble = codesRev.get((bitAccumulator >> (availBits - 6))); loNibble = codesRev.get(((bitAccumulator >> (availBits - 12)) & 0b111111)); - } catch (Exception ex) { - System.out.println("Exception: " + ex.getMessage()); - ex.printStackTrace(); + } catch (Exception e) { + log.error("Unhandled exception", e); return null; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneral.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneral.java index b886861b00..021d1ee068 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneral.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/dialog/RileyLinkStatusGeneral.java @@ -122,10 +122,10 @@ public class RileyLinkStatusGeneral extends Fragment implements RefreshableInter this.medtronicPumpStatus = MedtronicUtil.getPumpStatus(); if (medtronicPumpStatus != null) { - this.deviceType.setText(MainApp.gs(RileyLinkUtil.getTargetDevice().getResourceId())); + this.deviceType.setText(MainApp.gs(RileyLinkTargetDevice.MedtronicPump.getResourceId())); this.deviceModel.setText(medtronicPumpStatus.pumpType.getDescription()); this.serialNumber.setText(medtronicPumpStatus.serialNumber); - this.pumpFrequency.setText(medtronicPumpStatus.pumpFrequency); + this.pumpFrequency.setText(MainApp.gs(medtronicPumpStatus.pumpFrequency.equals("medtronic_pump_frequency_us_ca") ? R.string.medtronic_pump_frequency_us_ca : R.string.medtronic_pump_frequency_worldwide)); // TODO extend when Omnipod used diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java index e4f097e7db..783553f431 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java @@ -250,4 +250,29 @@ public class DateTimeUtil { return minutes.getMinutes(); } + + + public static long getMillisFromATDWithAddedMinutes(long atd, int minutesDiff) { + GregorianCalendar oldestEntryTime = DateTimeUtil.toGregorianCalendar(atd); + oldestEntryTime.add(Calendar.MINUTE, minutesDiff); + + return oldestEntryTime.getTimeInMillis(); + } + + + public static long getATDWithAddedMinutes(long atd, int minutesDiff) { + GregorianCalendar oldestEntryTime = DateTimeUtil.toGregorianCalendar(atd); + oldestEntryTime.add(Calendar.MINUTE, minutesDiff); + + return oldestEntryTime.getTimeInMillis(); + } + + + public static long getATDWithAddedMinutes(GregorianCalendar oldestEntryTime, int minutesDiff) { + oldestEntryTime.add(Calendar.MINUTE, minutesDiff); + + return toATechDate(oldestEntryTime); + } + + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRFragment.kt index b01bb22104..3f58acf4d2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRFragment.kt @@ -8,7 +8,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment -import com.squareup.otto.Subscribe import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.activities.TDDStatsActivity @@ -81,12 +80,39 @@ class DanaRFragment : Fragment() { @Synchronized override fun onResume() { super.onResume() - MainApp.bus().register(this) loopHandler.postDelayed(refreshLoop, T.mins(1).msecs()) disposable += RxBus .toObservable(EventDanaRNewStatus::class.java) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) + disposable += RxBus + .toObservable(EventExtendedBolusChange::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) + disposable += RxBus + .toObservable(EventTempBasalChange::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) + disposable += RxBus + .toObservable(EventQueueChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) + disposable += RxBus + .toObservable(EventPumpStatusChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + when { + it.sStatus == EventPumpStatusChanged.Status.CONNECTING -> danar_btconnection?.text = "{fa-bluetooth-b spin} " + it.sSecondsElapsed + "s" + it.sStatus == EventPumpStatusChanged.Status.CONNECTED -> danar_btconnection?.text = "{fa-bluetooth}" + it.sStatus == EventPumpStatusChanged.Status.DISCONNECTED -> danar_btconnection?.text = "{fa-bluetooth-b}" + } + if (it.getStatus() != "") { + dana_pumpstatus?.text = it.getStatus() + dana_pumpstatuslayout?.visibility = View.VISIBLE + } else { + dana_pumpstatuslayout?.visibility = View.GONE + } + }, { FabricPrivacy.logException(it) }) updateGUI() } @@ -94,41 +120,9 @@ class DanaRFragment : Fragment() { override fun onPause() { super.onPause() disposable.clear() - MainApp.bus().unregister(this) loopHandler.removeCallbacks(refreshLoop) } - @Subscribe - fun onStatusEvent(c: EventPumpStatusChanged) { - activity?.runOnUiThread { - when { - c.sStatus == EventPumpStatusChanged.CONNECTING -> danar_btconnection?.text = "{fa-bluetooth-b spin} " + c.sSecondsElapsed + "s" - c.sStatus == EventPumpStatusChanged.CONNECTED -> danar_btconnection?.text = "{fa-bluetooth}" - c.sStatus == EventPumpStatusChanged.DISCONNECTED -> danar_btconnection?.text = "{fa-bluetooth-b}" - } - if (c.textStatus() != "") { - dana_pumpstatus?.text = c.textStatus() - dana_pumpstatuslayout?.visibility = View.VISIBLE - } else { - dana_pumpstatuslayout?.visibility = View.GONE - } - } - - } - - @Subscribe - fun onStatusEvent(s: EventTempBasalChange) = - activity?.runOnUiThread { updateGUI() } - - - @Subscribe - fun onStatusEvent(s: EventExtendedBolusChange) = - activity?.runOnUiThread { updateGUI() } - - @Subscribe - fun onStatusEvent(s: EventQueueChanged) = - activity?.runOnUiThread { updateGUI() } - // GUI functions @Synchronized internal fun updateGUI() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRPlugin.java index 6a01c5fed8..e9293b39a6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/DanaRPlugin.java @@ -5,10 +5,6 @@ import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; -import androidx.fragment.app.FragmentActivity; -import androidx.appcompat.app.AlertDialog; - -import com.squareup.otto.Subscribe; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -22,19 +18,23 @@ import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusStartWithSpeed; import info.nightscout.androidaps.plugins.pump.danaR.services.DanaRExecutionService; import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.Round; import info.nightscout.androidaps.utils.SP; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Created by mike on 05.08.2016. */ public class DanaRPlugin extends AbstractDanaRPlugin { + private CompositeDisposable disposable = new CompositeDisposable(); private static DanaRPlugin plugin = null; @@ -55,7 +55,27 @@ public class DanaRPlugin extends AbstractDanaRPlugin { Context context = MainApp.instance().getApplicationContext(); Intent intent = new Intent(context, DanaRExecutionService.class); context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (isEnabled(PluginType.PUMP)) { + boolean previousValue = useExtendedBoluses; + useExtendedBoluses = SP.getBoolean(R.string.key_danar_useextended, false); + + if (useExtendedBoluses != previousValue && TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) { + sExecutionService.extendedBolusStop(); + } + } + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + MainApp.instance().getApplicationContext().unbindService(mConnection); + }, FabricPrivacy::logException) + ); super.onStart(); } @@ -64,7 +84,7 @@ public class DanaRPlugin extends AbstractDanaRPlugin { Context context = MainApp.instance().getApplicationContext(); context.unbindService(mConnection); - MainApp.bus().unregister(this); + disposable.clear(); super.onStop(); } @@ -84,24 +104,6 @@ public class DanaRPlugin extends AbstractDanaRPlugin { } }; - @SuppressWarnings("UnusedParameters") - @Subscribe - public void onStatusEvent(final EventAppExit e) { - MainApp.instance().getApplicationContext().unbindService(mConnection); - } - - @Subscribe - public void onStatusEvent(final EventPreferenceChange s) { - if (isEnabled(PluginType.PUMP)) { - boolean previousValue = useExtendedBoluses; - useExtendedBoluses = SP.getBoolean(R.string.key_danar_useextended, false); - - if (useExtendedBoluses != previousValue && TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) { - sExecutionService.extendedBolusStop(); - } - } - } - // Plugin base interface @Override public String getName() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRHistoryActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRHistoryActivity.java index 2d31510e71..f20a74b024 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRHistoryActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRHistoryActivity.java @@ -15,8 +15,6 @@ import androidx.cardview.widget.CardView; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,6 +30,7 @@ import info.nightscout.androidaps.db.DanaRHistoryRecord; import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.pump.danaR.comm.RecordTypes; @@ -41,10 +40,14 @@ import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.ToastUtils; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; public class DanaRHistoryActivity extends NoSplashActivity { private static Logger log = LoggerFactory.getLogger(L.PUMP); + private CompositeDisposable disposable = new CompositeDisposable(); static Profile profile = null; @@ -81,13 +84,26 @@ public class DanaRHistoryActivity extends NoSplashActivity { @Override protected void onResume() { super.onResume(); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventPumpStatusChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> statusView.setText(event.getStatus()), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventDanaRSyncStatus.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + if (L.isEnabled(L.PUMP)) + log.debug("EventDanaRSyncStatus: " + event.getMessage()); + statusView.setText(event.getMessage()); + }, FabricPrivacy::logException) + ); } @Override protected void onPause() { super.onPause(); - MainApp.bus().unregister(this); + disposable.clear(); } @Override @@ -316,21 +332,4 @@ public class DanaRHistoryActivity extends NoSplashActivity { historyList = new ArrayList<>(); runOnUiThread(() -> recyclerView.swapAdapter(new RecyclerViewAdapter(historyList), false)); } - - @Subscribe - public void onStatusEvent(final EventDanaRSyncStatus s) { - if (L.isEnabled(L.PUMP)) - log.debug("EventDanaRSyncStatus: " + s.message); - runOnUiThread( - () -> statusView.setText(s.message)); - } - - @Subscribe - public void onStatusEvent(final EventPumpStatusChanged s) { - runOnUiThread( - () -> statusView.setText(s.textStatus()) - ); - } - - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRNSHistorySync.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRNSHistorySync.java index a15c8d2883..dcb182a0d6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRNSHistorySync.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRNSHistorySync.java @@ -14,6 +14,7 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.DanaRHistoryRecord; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.pump.danaR.comm.RecordTypes; import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRSyncStatus; @@ -28,13 +29,13 @@ public class DanaRNSHistorySync { private static Logger log = LoggerFactory.getLogger(L.PUMP); private List historyRecords; - public final static int SYNC_BOLUS = 0b00000001; - public final static int SYNC_ERROR = 0b00000010; - public final static int SYNC_REFILL = 0b00000100; - public final static int SYNC_GLUCOSE = 0b00001000; - public final static int SYNC_CARBO = 0b00010000; - public final static int SYNC_ALARM = 0b00100000; - public final static int SYNC_BASALHOURS = 0b01000000; + private final static int SYNC_BOLUS = 0b00000001; + private final static int SYNC_ERROR = 0b00000010; + private final static int SYNC_REFILL = 0b00000100; + private final static int SYNC_GLUCOSE = 0b00001000; + private final static int SYNC_CARBO = 0b00010000; + private final static int SYNC_ALARM = 0b00100000; + private final static int SYNC_BASALHOURS = 0b01000000; public final static int SYNC_ALL = 0b11111111; public final static String DANARSIGNATURE = "DANARMESSAGE"; @@ -52,13 +53,13 @@ public class DanaRNSHistorySync { long uploaded = 0; if (L.isEnabled(L.PUMP)) log.debug("Database contains " + records + " records"); - EventDanaRSyncStatus ev = new EventDanaRSyncStatus(); + EventDanaRSyncStatus ev = new EventDanaRSyncStatus(""); for (DanaRHistoryRecord record : historyRecords) { processing++; if (record._id != null) continue; //log.debug(record.bytes); JSONObject nsrec = new JSONObject(); - ev.message = MainApp.gs(R.string.uploading) + " " + processing + "/" + records + " "; // TODO: translations + ev.setMessage(MainApp.gs(R.string.uploading) + " " + processing + "/" + records + " "); // TODO: translations switch (record.recordCode) { case RecordTypes.RECORD_TYPE_BOLUS: if ((what & SYNC_BOLUS) == 0) break; @@ -73,7 +74,7 @@ public class DanaRNSHistorySync { nsrec.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); NSUpload.uploadCareportalEntryToNS(nsrec); uploaded++; - ev.message += MainApp.gs(R.string.danar_sbolus); + ev.setMessage(ev.getMessage() + MainApp.gs(R.string.danar_sbolus)); break; case "E": if (record.recordDuration > 0) { @@ -92,7 +93,7 @@ public class DanaRNSHistorySync { nsrec.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); NSUpload.uploadCareportalEntryToNS(nsrec); uploaded++; - ev.message += MainApp.gs(R.string.danar_ebolus); + ev.setMessage(ev.getMessage() + MainApp.gs(R.string.danar_ebolus)); } else { if (L.isEnabled(L.PUMP)) log.debug("NOT Syncing extended bolus record " + record.recordValue + "U " + DateUtil.toISOString(record.recordDate) + " zero duration"); @@ -110,7 +111,7 @@ public class DanaRNSHistorySync { nsrec.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); NSUpload.uploadCareportalEntryToNS(nsrec); uploaded++; - ev.message += MainApp.gs(R.string.danar_dsbolus); + ev.setMessage(ev.getMessage() + MainApp.gs(R.string.danar_dsbolus)); break; case "DE": if (L.isEnabled(L.PUMP)) @@ -127,7 +128,7 @@ public class DanaRNSHistorySync { nsrec.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); NSUpload.uploadCareportalEntryToNS(nsrec); uploaded++; - ev.message += MainApp.gs(R.string.danar_debolus); + ev.setMessage(ev.getMessage() + MainApp.gs(R.string.danar_debolus)); break; default: log.error("Unknown bolus record"); @@ -145,7 +146,7 @@ public class DanaRNSHistorySync { nsrec.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); NSUpload.uploadCareportalEntryToNS(nsrec); uploaded++; - ev.message += MainApp.gs(R.string.danar_error); + ev.setMessage(ev.getMessage() + MainApp.gs(R.string.danar_error)); break; case RecordTypes.RECORD_TYPE_REFILL: if ((what & SYNC_REFILL) == 0) break; @@ -158,7 +159,7 @@ public class DanaRNSHistorySync { nsrec.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); NSUpload.uploadCareportalEntryToNS(nsrec); uploaded++; - ev.message += MainApp.gs(R.string.danar_refill); + ev.setMessage(ev.getMessage() + MainApp.gs(R.string.danar_refill)); break; case RecordTypes.RECORD_TYPE_BASALHOUR: if ((what & SYNC_BASALHOURS) == 0) break; @@ -172,7 +173,7 @@ public class DanaRNSHistorySync { nsrec.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); NSUpload.uploadCareportalEntryToNS(nsrec); uploaded++; - ev.message += MainApp.gs(R.string.danar_basalhour); + ev.setMessage(ev.getMessage() + MainApp.gs(R.string.danar_basalhour)); break; case RecordTypes.RECORD_TYPE_TB: //log.debug("Ignoring TB record " + record.bytes + " " + DateUtil.toISOString(record.recordDate)); @@ -189,7 +190,7 @@ public class DanaRNSHistorySync { nsrec.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); NSUpload.uploadCareportalEntryToNS(nsrec); uploaded++; - ev.message += MainApp.gs(R.string.danar_glucose); + ev.setMessage(ev.getMessage() + MainApp.gs(R.string.danar_glucose)); break; case RecordTypes.RECORD_TYPE_CARBO: if ((what & SYNC_CARBO) == 0) break; @@ -202,7 +203,7 @@ public class DanaRNSHistorySync { nsrec.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); NSUpload.uploadCareportalEntryToNS(nsrec); uploaded++; - ev.message += MainApp.gs(R.string.danar_carbohydrate); + ev.setMessage(ev.getMessage() + MainApp.gs(R.string.danar_carbohydrate)); break; case RecordTypes.RECORD_TYPE_ALARM: if ((what & SYNC_ALARM) == 0) break; @@ -215,7 +216,7 @@ public class DanaRNSHistorySync { nsrec.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); NSUpload.uploadCareportalEntryToNS(nsrec); uploaded++; - ev.message += MainApp.gs(R.string.danar_alarm); + ev.setMessage(ev.getMessage() + MainApp.gs(R.string.danar_alarm)); break; case RecordTypes.RECORD_TYPE_SUSPEND: // TODO: this too case RecordTypes.RECORD_TYPE_DAILY: @@ -226,10 +227,10 @@ public class DanaRNSHistorySync { log.error("Unknown record type"); break; } - MainApp.bus().post(ev); + RxBus.INSTANCE.send(ev); } - ev.message = String.format(MainApp.gs(R.string.danar_totaluploaded), uploaded); - MainApp.bus().post(ev); + ev.setMessage(String.format(MainApp.gs(R.string.danar_totaluploaded), uploaded)); + RxBus.INSTANCE.send(ev); } catch (JSONException e) { log.error("Unhandled exception", e); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.java index d5b4fd3158..f1181bb7cb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.java @@ -1,14 +1,11 @@ package info.nightscout.androidaps.plugins.pump.danaR.activities; -import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Switch; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,12 +18,16 @@ import info.nightscout.androidaps.activities.NoSplashActivity; import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump; import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin; import info.nightscout.androidaps.plugins.pump.danaRv2.DanaRv2Plugin; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.NumberPicker; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; /** * Created by Rumen Georgiev on 5/31/2018. @@ -34,6 +35,7 @@ import info.nightscout.androidaps.utils.NumberPicker; public class DanaRUserOptionsActivity extends NoSplashActivity { private static Logger log = LoggerFactory.getLogger(L.PUMP); + private CompositeDisposable disposable = new CompositeDisposable(); Switch timeFormat; Switch buttonScroll; @@ -49,20 +51,24 @@ public class DanaRUserOptionsActivity extends NoSplashActivity { NumberPicker lowReservoir; Button saveToPumpButton; // This is for Dana pumps only - boolean isRS = MainApp.getSpecificPlugin(DanaRSPlugin.class) != null && MainApp.getSpecificPlugin(DanaRSPlugin.class).isEnabled(PluginType.PUMP); - boolean isDanaR = MainApp.getSpecificPlugin(DanaRPlugin.class) != null && MainApp.getSpecificPlugin(DanaRPlugin.class).isEnabled(PluginType.PUMP); - boolean isDanaRv2 = MainApp.getSpecificPlugin(DanaRv2Plugin.class) != null && MainApp.getSpecificPlugin(DanaRv2Plugin.class).isEnabled(PluginType.PUMP); + boolean isRS = DanaRSPlugin.getPlugin().isEnabled(PluginType.PUMP); + boolean isDanaR = DanaRPlugin.getPlugin().isEnabled(PluginType.PUMP); + boolean isDanaRv2 = DanaRv2Plugin.getPlugin().isEnabled(PluginType.PUMP); @Override - protected void onResume() { + protected synchronized void onResume() { super.onResume(); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventInitializationChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> setData(), FabricPrivacy::logException) + ); } @Override - protected void onPause() { + protected synchronized void onPause() { + disposable.clear(); super.onPause(); - MainApp.bus().unregister(this); } @Override @@ -144,11 +150,6 @@ public class DanaRUserOptionsActivity extends NoSplashActivity { lowReservoir.setValue((double) pump.lowReservoirRate); } - @Subscribe - public void onEventInitializationChanged(EventInitializationChanged ignored) { - runOnUiThread(this::setData); - } - public void onSaveClick() { if (!isRS && !isDanaR && !isDanaRv2) { //exit if pump is not DanaRS, Dana!, or DanaR with upgraded firmware diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusProgress.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusProgress.java index 6fb1993237..362b72c929 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusProgress.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusProgress.java @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.treatments.Treatment; @@ -37,15 +38,15 @@ public class MsgBolusProgress extends MessageBase { lastReceive = System.currentTimeMillis(); Double done = (amount * 100 - progress) / 100d; t.insulin = done; - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.status = String.format(MainApp.gs(R.string.bolusdelivering), done); - bolusingEvent.t = t; - bolusingEvent.percent = Math.min((int) (done / amount * 100), 100); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setStatus(String.format(MainApp.gs(R.string.bolusdelivering), done)); + bolusingEvent.setT(t); + bolusingEvent.setPercent(Math.min((int) (done / amount * 100), 100)); if (L.isEnabled(L.PUMPCOMM)) { log.debug("Bolus remaining: " + progress + " delivered: " + done); } - MainApp.bus().post(bolusingEvent); + RxBus.INSTANCE.send(bolusingEvent); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStop.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStop.java index 39531b8b10..2ea50297fb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStop.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStop.java @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.treatments.Treatment; @@ -35,15 +36,15 @@ public class MsgBolusStop extends MessageBase { public void handleMessage(byte[] bytes) { if (L.isEnabled(L.PUMPCOMM)) log.debug("Messsage received"); - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; stopped = true; if (!forced) { t.insulin = amount; - bolusingEvent.status = MainApp.gs(R.string.overview_bolusprogress_delivered); - bolusingEvent.percent = 100; + bolusingEvent.setStatus(MainApp.gs(R.string.overview_bolusprogress_delivered)); + bolusingEvent.setPercent(100); } else { - bolusingEvent.status = MainApp.gs(R.string.overview_bolusprogress_stoped); + bolusingEvent.setStatus(MainApp.gs(R.string.overview_bolusprogress_stoped)); } - MainApp.bus().post(bolusingEvent); + RxBus.INSTANCE.send(bolusingEvent); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgCheckValue.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgCheckValue.java index d40f8f0cd6..317fe9db81 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgCheckValue.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgCheckValue.java @@ -32,7 +32,7 @@ public class MsgCheckValue extends MessageBase { pump.protocol = intFromBuff(bytes, 1, 1); pump.productCode = intFromBuff(bytes, 2, 1); if (pump.model != DanaRPump.EXPORT_MODEL) { - MainApp.getSpecificPlugin(DanaRPlugin.class).disconnect("Wrong Model"); + DanaRPlugin.getPlugin().disconnect("Wrong Model"); log.debug("Wrong model selected"); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgError.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgError.java index f697894778..a6fdaa8d0d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgError.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgError.java @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; @@ -44,10 +45,10 @@ public class MsgError extends MessageBase { } if (errorCode < 8) { // bolus delivering stopped - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; MsgBolusStop.stopped = true; - bolusingEvent.status = errorString; - MainApp.bus().post(bolusingEvent); + bolusingEvent.setStatus(errorString); + RxBus.INSTANCE.send(bolusingEvent); failed=true; } if (L.isEnabled(L.PUMPCOMM)) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgHistoryAll.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgHistoryAll.java index 7de2489c91..955d7fc090 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgHistoryAll.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgHistoryAll.java @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.db.DanaRHistoryRecord; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRSyncStatus; import info.nightscout.androidaps.utils.DateUtil; @@ -33,8 +34,6 @@ public class MsgHistoryAll extends MessageBase { byte paramByte8 = (byte) intFromBuff(bytes, 7, 1); double value = (double) intFromBuff(bytes, 8, 2); - EventDanaRSyncStatus ev = new EventDanaRSyncStatus(); - DanaRHistoryRecord danaRHistoryRecord = new DanaRHistoryRecord(); danaRHistoryRecord.recordCode = recordCode; @@ -145,11 +144,6 @@ public class MsgHistoryAll extends MessageBase { } MainApp.getDbHelper().createOrUpdate(danaRHistoryRecord); - - ev.message = DateUtil.dateAndTimeString(danaRHistoryRecord.recordDate); - ev.message += " " + messageType; - MainApp.bus().post(ev); - - return; + RxBus.INSTANCE.send(new EventDanaRSyncStatus(DateUtil.dateAndTimeString(danaRHistoryRecord.recordDate) + " " + messageType)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgInitConnStatusTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgInitConnStatusTime.java index 1ba35a3614..974e63fe80 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgInitConnStatusTime.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgInitConnStatusTime.java @@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.androidaps.events.EventRebuildTabs; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.bus.RxBus; @@ -31,23 +31,23 @@ public class MsgInitConnStatusTime extends MessageBase { if (bytes.length - 10 > 7) { Notification notification = new Notification(Notification.WRONG_DRIVER, MainApp.gs(R.string.pumpdrivercorrected), Notification.NORMAL); RxBus.INSTANCE.send(new EventNewNotification(notification)); - MainApp.getSpecificPlugin(DanaRPlugin.class).disconnect("Wrong Model"); + DanaRPlugin.getPlugin().disconnect("Wrong Model"); log.error("Wrong model selected. Switching to Korean DanaR"); - MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setPluginEnabled(PluginType.PUMP, true); - MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setFragmentVisible(PluginType.PUMP, true); - MainApp.getSpecificPlugin(DanaRPlugin.class).setPluginEnabled(PluginType.PUMP, false); - MainApp.getSpecificPlugin(DanaRPlugin.class).setFragmentVisible(PluginType.PUMP, false); + DanaRKoreanPlugin.getPlugin().setPluginEnabled(PluginType.PUMP, true); + DanaRKoreanPlugin.getPlugin().setFragmentVisible(PluginType.PUMP, true); + DanaRPlugin.getPlugin().setPluginEnabled(PluginType.PUMP, false); + DanaRPlugin.getPlugin().setFragmentVisible(PluginType.PUMP, false); DanaRPump.reset(); // mark not initialized //If profile coming from pump, switch it as well - if (MainApp.getSpecificPlugin(DanaRPlugin.class).isEnabled(PluginType.PROFILE)) { - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setPluginEnabled(PluginType.PROFILE, false); - (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setPluginEnabled(PluginType.PROFILE, true); + if (DanaRPlugin.getPlugin().isEnabled(PluginType.PROFILE)) { + (DanaRPlugin.getPlugin()).setPluginEnabled(PluginType.PROFILE, false); + (DanaRKoreanPlugin.getPlugin()).setPluginEnabled(PluginType.PROFILE, true); } ConfigBuilderPlugin.getPlugin().storeSettings("ChangingDanaDriver"); - MainApp.bus().post(new EventRefreshGui()); + RxBus.INSTANCE.send(new EventRebuildTabs()); ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection failed = false; return; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/events/EventDanaRSyncStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/events/EventDanaRSyncStatus.java deleted file mode 100644 index 5fc0bd740f..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/events/EventDanaRSyncStatus.java +++ /dev/null @@ -1,14 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.danaR.events; - -import info.nightscout.androidaps.events.Event; - -/** - * Created by mike on 20.07.2016. - */ -public class EventDanaRSyncStatus extends Event { - public String message; - - public EventDanaRSyncStatus() { - } - - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/events/EventDanaRSyncStatus.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/events/EventDanaRSyncStatus.kt new file mode 100644 index 0000000000..59ad7e2d83 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/events/EventDanaRSyncStatus.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.pump.danaR.events + +import info.nightscout.androidaps.events.Event + +class EventDanaRSyncStatus(var message: String) : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/AbstractDanaRExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/AbstractDanaRExecutionService.java index 9e3c1ff3ca..839debe9be 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/AbstractDanaRExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/AbstractDanaRExecutionService.java @@ -23,6 +23,7 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase; import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusStop; import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgHistoryAlarm; @@ -105,7 +106,7 @@ public abstract class AbstractDanaRExecutionService extends Service { if (mSerialIOThread != null) { mSerialIOThread.disconnect("BT disconnection broadcast"); } - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTED)); } } } @@ -135,7 +136,7 @@ public abstract class AbstractDanaRExecutionService extends Service { public void finishHandshaking() { mHandshakeInProgress = false; - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTED, 0)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTED, 0)); } public void disconnect(String from) { @@ -243,7 +244,7 @@ public abstract class AbstractDanaRExecutionService extends Service { long timeToWholeMinute = (60000 - time % 60000); if (timeToWholeMinute > 59800 || timeToWholeMinute < 3000) break; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.waitingfortimesynchronization, (int) (timeToWholeMinute / 1000)))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.waitingfortimesynchronization, (int) (timeToWholeMinute / 1000)))); SystemClock.sleep(Math.min(timeToWholeMinute, 100)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/DanaRExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/DanaRExecutionService.java index f623f292aa..dba3b4ca77 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/DanaRExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/DanaRExecutionService.java @@ -5,8 +5,6 @@ import android.content.IntentFilter; import android.os.Binder; import android.os.SystemClock; -import com.squareup.otto.Subscribe; - import java.io.IOException; import java.util.Date; @@ -67,15 +65,50 @@ import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.commands.Command; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; public class DanaRExecutionService extends AbstractDanaRExecutionService { + private CompositeDisposable disposable = new CompositeDisposable(); public DanaRExecutionService() { mBinder = new LocalBinder(); - registerBus(); MainApp.instance().getApplicationContext().registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); + } + + @Override + public void onCreate() { + super.onCreate(); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (mSerialIOThread != null) + mSerialIOThread.disconnect("EventPreferenceChange"); + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (L.isEnabled(L.PUMP)) + log.debug("EventAppExit received"); + + if (mSerialIOThread != null) + mSerialIOThread.disconnect("Application exit"); + MainApp.instance().getApplicationContext().unregisterReceiver(receiver); + stopSelf(); + }, FabricPrivacy::logException) + ); + } + + @Override + public void onDestroy() { + disposable.clear(); + super.onDestroy(); } public class LocalBinder extends Binder { @@ -84,21 +117,6 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { } } - private void registerBus() { - try { - MainApp.bus().unregister(this); - } catch (RuntimeException x) { - // Ignore - } - MainApp.bus().register(this); - } - - @Subscribe - public void onStatusEvent(final EventPreferenceChange pch) { - if (mSerialIOThread != null) - mSerialIOThread.disconnect("EventPreferenceChange"); - } - public void connect() { if (mConnectionInProgress) return; @@ -127,7 +145,7 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { } mSerialIOThread = new SerialIOThread(mRfcommSocket, MessageHashTableR.INSTANCE); mHandshakeInProgress = true; - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, 0)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.HANDSHAKING, 0)); } mConnectionInProgress = false; @@ -137,7 +155,7 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { public void getPumpStatus() { DanaRPump danaRPump = DanaRPump.getInstance(); try { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpstatus))); MsgStatus statusMsg = new MsgStatus(); MsgStatusBasic statusBasicMsg = new MsgStatusBasic(); MsgStatusTempBasal tempStatusMsg = new MsgStatusTempBasal(); @@ -153,11 +171,11 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { mSerialIOThread.sendMessage(statusMsg); mSerialIOThread.sendMessage(statusBasicMsg); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingtempbasalstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingtempbasalstatus))); mSerialIOThread.sendMessage(tempStatusMsg); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingextendedbolusstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingextendedbolusstatus))); mSerialIOThread.sendMessage(exStatusMsg); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); long now = System.currentTimeMillis(); danaRPump.lastConnection = now; @@ -165,15 +183,15 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { Profile profile = ProfileFunctions.getInstance().getProfile(); PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); if (profile != null && Math.abs(danaRPump.currentBasal - profile.getBasal()) >= pump.getPumpDescription().basalStep) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); mSerialIOThread.sendMessage(new MsgSettingBasal()); if (!pump.isThisProfileSet(profile) && !ConfigBuilderPlugin.getPlugin().getCommandQueue().isRunning(Command.CommandType.BASALPROFILE)) { - MainApp.bus().post(new EventProfileNeedsUpdate()); + RxBus.INSTANCE.send(new EventProfileNeedsUpdate()); } } if (danaRPump.lastSettingsRead + 60 * 60 * 1000L < now || !pump.isInitialized()) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); mSerialIOThread.sendMessage(new MsgSettingShippingInfo()); mSerialIOThread.sendMessage(new MsgSettingActiveProfile()); mSerialIOThread.sendMessage(new MsgSettingMeal()); @@ -185,7 +203,7 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { mSerialIOThread.sendMessage(new MsgSettingProfileRatios()); mSerialIOThread.sendMessage(new MsgSettingProfileRatiosAll()); mSerialIOThread.sendMessage(new MsgSettingUserOptions()); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime))); mSerialIOThread.sendMessage(new MsgSettingPumpTime()); if (danaRPump.pumpTime == 0) { // initial handshake was not successfull @@ -193,7 +211,7 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { danaRPump.lastConnection = 0; danaRPump.lastSettingsRead = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventInitializationChanged()); return; } long timeDiff = (danaRPump.pumpTime - System.currentTimeMillis()) / 1000L; @@ -210,7 +228,7 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { } RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventInitializationChanged()); NSUpload.uploadDeviceStatus(); if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { if (L.isEnabled(L.PUMP)) @@ -231,41 +249,41 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { DanaRPump danaRPump = DanaRPump.getInstance(); if (!isConnected()) return false; if (danaRPump.isTempBasalInProgress) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); SystemClock.sleep(500); } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStart(percent, durationInHours)); mSerialIOThread.sendMessage(new MsgStatusTempBasal()); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean tempBasalStop() { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); mSerialIOThread.sendMessage(new MsgStatusTempBasal()); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean extendedBolus(double insulin, int durationInHalfHours) { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingextendedbolus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingextendedbolus))); mSerialIOThread.sendMessage(new MsgSetExtendedBolusStart(insulin, (byte) (durationInHalfHours & 0xFF))); mSerialIOThread.sendMessage(new MsgStatusBolusExtended()); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean extendedBolusStop() { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingextendedbolus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingextendedbolus))); mSerialIOThread.sendMessage(new MsgSetExtendedBolusStop()); mSerialIOThread.sendMessage(new MsgStatusBolusExtended()); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } @@ -313,9 +331,9 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { } SystemClock.sleep(300); - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.t = t; - bolusingEvent.percent = 99; + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setT(t); + bolusingEvent.setPercent(99); mBolusingTreatment = null; @@ -339,8 +357,8 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { while (System.currentTimeMillis() < expectedEnd) { long waitTime = expectedEnd - System.currentTimeMillis(); - bolusingEvent.status = String.format(MainApp.gs(R.string.waitingforestimatedbolusend), waitTime / 1000); - MainApp.bus().post(bolusingEvent); + bolusingEvent.setStatus(String.format(MainApp.gs(R.string.waitingforestimatedbolusend), waitTime / 1000)); + RxBus.INSTANCE.send(bolusingEvent); SystemClock.sleep(1000); } @@ -365,7 +383,7 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { try { o.wait(); } catch (InterruptedException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } } } else { @@ -395,7 +413,7 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { public boolean updateBasalsInPump(final Profile profile) { DanaRPump danaRPump = DanaRPump.getInstance(); if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.updatingbasalrates))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.updatingbasalrates))); double[] basal = DanaRPump.getInstance().buildDanaRProfileRecord(profile); MsgSetBasalProfile msgSet = new MsgSetBasalProfile((byte) 0, basal); mSerialIOThread.sendMessage(msgSet); @@ -403,23 +421,10 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { mSerialIOThread.sendMessage(msgActivate); danaRPump.lastSettingsRead = 0; // force read full settings getPumpStatus(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } - @Subscribe - public void onStatusEvent(EventAppExit event) { - if (L.isEnabled(L.PUMP)) - log.debug("EventAppExit received"); - - if (mSerialIOThread != null) - mSerialIOThread.disconnect("Application exit"); - - MainApp.instance().getApplicationContext().unregisterReceiver(receiver); - - stopSelf(); - } - public PumpEnactResult setUserOptions() { if (!isConnected()) return new PumpEnactResult().success(false); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/DanaRKoreanPlugin.java index 64eccf05f1..2645e25ade 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/DanaRKoreanPlugin.java @@ -5,10 +5,6 @@ import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; -import androidx.fragment.app.FragmentActivity; -import androidx.appcompat.app.AlertDialog; - -import com.squareup.otto.Subscribe; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -22,7 +18,7 @@ import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.danaR.AbstractDanaRPlugin; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump; @@ -30,13 +26,17 @@ import info.nightscout.androidaps.plugins.pump.danaR.comm.MsgBolusStart; import info.nightscout.androidaps.plugins.pump.danaRKorean.services.DanaRKoreanExecutionService; import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.Round; import info.nightscout.androidaps.utils.SP; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Created by mike on 05.08.2016. */ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { + private CompositeDisposable disposable = new CompositeDisposable(); private static DanaRKoreanPlugin plugin = null; @@ -58,7 +58,27 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { Context context = MainApp.instance().getApplicationContext(); Intent intent = new Intent(context, DanaRKoreanExecutionService.class); context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (isEnabled(PluginType.PUMP)) { + boolean previousValue = useExtendedBoluses; + useExtendedBoluses = SP.getBoolean(R.string.key_danar_useextended, false); + + if (useExtendedBoluses != previousValue && TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) { + sExecutionService.extendedBolusStop(); + } + } + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + MainApp.instance().getApplicationContext().unbindService(mConnection); + }, FabricPrivacy::logException) + ); super.onStart(); } @@ -67,7 +87,7 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { Context context = MainApp.instance().getApplicationContext(); context.unbindService(mConnection); - MainApp.bus().unregister(this); + disposable.clear(); super.onStop(); } @@ -87,24 +107,6 @@ public class DanaRKoreanPlugin extends AbstractDanaRPlugin { } }; - @SuppressWarnings("UnusedParameters") - @Subscribe - public void onStatusEvent(final EventAppExit e) { - MainApp.instance().getApplicationContext().unbindService(mConnection); - } - - @Subscribe - public void onStatusEvent(final EventPreferenceChange s) { - if (isEnabled(PluginType.PUMP)) { - boolean previousValue = useExtendedBoluses; - useExtendedBoluses = SP.getBoolean(R.string.key_danar_useextended, false); - - if (useExtendedBoluses != previousValue && TreatmentsPlugin.getPlugin().isInHistoryExtendedBoluslInProgress()) { - sExecutionService.extendedBolusStop(); - } - } - } - // Plugin base interface @Override public String getName() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/comm/MsgInitConnStatusTime_k.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/comm/MsgInitConnStatusTime_k.java index 020bc3f3b5..8a642a89f8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/comm/MsgInitConnStatusTime_k.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/comm/MsgInitConnStatusTime_k.java @@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.androidaps.events.EventRebuildTabs; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.bus.RxBus; @@ -35,21 +35,21 @@ public class MsgInitConnStatusTime_k extends MessageBase { RxBus.INSTANCE.send(new EventNewNotification(notification)); DanaRKoreanPlugin.getPlugin().disconnect("Wrong Model"); log.error("Wrong model selected. Switching to export DanaR"); - MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setPluginEnabled(PluginType.PUMP, false); - MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setFragmentVisible(PluginType.PUMP, false); - MainApp.getSpecificPlugin(DanaRPlugin.class).setPluginEnabled(PluginType.PUMP, true); - MainApp.getSpecificPlugin(DanaRPlugin.class).setFragmentVisible(PluginType.PUMP, true); + DanaRKoreanPlugin.getPlugin().setPluginEnabled(PluginType.PUMP, false); + DanaRKoreanPlugin.getPlugin().setFragmentVisible(PluginType.PUMP, false); + DanaRPlugin.getPlugin().setPluginEnabled(PluginType.PUMP, true); + DanaRPlugin.getPlugin().setFragmentVisible(PluginType.PUMP, true); DanaRPump.reset(); // mark not initialized //If profile coming from pump, switch it as well - if (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).isEnabled(PluginType.PROFILE)) { - (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setPluginEnabled(PluginType.PROFILE, false); - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setPluginEnabled(PluginType.PROFILE, true); + if (DanaRKoreanPlugin.getPlugin().isEnabled(PluginType.PROFILE)) { + (DanaRKoreanPlugin.getPlugin()).setPluginEnabled(PluginType.PROFILE, false); + (DanaRPlugin.getPlugin()).setPluginEnabled(PluginType.PROFILE, true); } ConfigBuilderPlugin.getPlugin().storeSettings("ChangingKoreanDanaDriver"); - MainApp.bus().post(new EventRefreshGui()); + RxBus.INSTANCE.send(new EventRebuildTabs()); ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection return; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/services/DanaRKoreanExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/services/DanaRKoreanExecutionService.java index 6a10ffd7f6..bb1f226730 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/services/DanaRKoreanExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/services/DanaRKoreanExecutionService.java @@ -5,8 +5,6 @@ import android.content.IntentFilter; import android.os.Binder; import android.os.SystemClock; -import com.squareup.otto.Subscribe; - import java.io.IOException; import java.util.Date; @@ -59,51 +57,58 @@ import info.nightscout.androidaps.plugins.pump.danaRKorean.comm.MsgStatusBasic_k import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.queue.commands.Command; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { + private CompositeDisposable disposable = new CompositeDisposable(); public DanaRKoreanExecutionService() { mBinder = new LocalBinder(); - registerBus(); MainApp.instance().getApplicationContext().registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); } + @Override + public void onCreate() { + super.onCreate(); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (mSerialIOThread != null) + mSerialIOThread.disconnect("EventPreferenceChange"); + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (L.isEnabled(L.PUMP)) + log.debug("EventAppExit received"); + + if (mSerialIOThread != null) + mSerialIOThread.disconnect("Application exit"); + MainApp.instance().getApplicationContext().unregisterReceiver(receiver); + stopSelf(); + }, FabricPrivacy::logException) + ); + } + + @Override + public void onDestroy() { + disposable.clear(); + super.onDestroy(); + } + public class LocalBinder extends Binder { public DanaRKoreanExecutionService getServiceInstance() { return DanaRKoreanExecutionService.this; } } - private void registerBus() { - try { - MainApp.bus().unregister(this); - } catch (RuntimeException x) { - // Ignore - } - MainApp.bus().register(this); - } - - @Subscribe - public void onStatusEvent(EventAppExit event) { - if (L.isEnabled(L.PUMP)) - log.debug("EventAppExit received"); - - if (mSerialIOThread != null) - mSerialIOThread.disconnect("Application exit"); - - MainApp.instance().getApplicationContext().unregisterReceiver(receiver); - - stopSelf(); - } - - @Subscribe - public void onStatusEvent(final EventPreferenceChange pch) { - if (mSerialIOThread != null) - mSerialIOThread.disconnect("EventPreferenceChange"); - } - public void connect() { if (mConnectionInProgress) return; @@ -132,7 +137,7 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { } mSerialIOThread = new SerialIOThread(mRfcommSocket, MessageHashTableRkorean.INSTANCE); mHandshakeInProgress = true; - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, 0)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.HANDSHAKING, 0)); } mConnectionInProgress = false; @@ -142,7 +147,7 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { public void getPumpStatus() { DanaRPump danaRPump = DanaRPump.getInstance(); try { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpstatus))); //MsgStatus_k statusMsg = new MsgStatus_k(); MsgStatusBasic_k statusBasicMsg = new MsgStatusBasic_k(); MsgStatusTempBasal tempStatusMsg = new MsgStatusTempBasal(); @@ -158,11 +163,11 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { //mSerialIOThread.sendMessage(statusMsg); mSerialIOThread.sendMessage(statusBasicMsg); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingtempbasalstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingtempbasalstatus))); mSerialIOThread.sendMessage(tempStatusMsg); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingextendedbolusstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingextendedbolusstatus))); mSerialIOThread.sendMessage(exStatusMsg); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); long now = System.currentTimeMillis(); danaRPump.lastConnection = now; @@ -170,15 +175,15 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { Profile profile = ProfileFunctions.getInstance().getProfile(); PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); if (profile != null && Math.abs(danaRPump.currentBasal - profile.getBasal()) >= pump.getPumpDescription().basalStep) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); mSerialIOThread.sendMessage(new MsgSettingBasal()); if (!pump.isThisProfileSet(profile) && !ConfigBuilderPlugin.getPlugin().getCommandQueue().isRunning(Command.CommandType.BASALPROFILE)) { - MainApp.bus().post(new EventProfileNeedsUpdate()); + RxBus.INSTANCE.send(new EventProfileNeedsUpdate()); } } if (danaRPump.lastSettingsRead + 60 * 60 * 1000L < now || !pump.isInitialized()) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); mSerialIOThread.sendMessage(new MsgSettingShippingInfo()); mSerialIOThread.sendMessage(new MsgSettingMeal()); mSerialIOThread.sendMessage(new MsgSettingBasal_k()); @@ -186,7 +191,7 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { mSerialIOThread.sendMessage(new MsgSettingMaxValues()); mSerialIOThread.sendMessage(new MsgSettingGlucose()); mSerialIOThread.sendMessage(new MsgSettingProfileRatios()); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime))); mSerialIOThread.sendMessage(new MsgSettingPumpTime()); if (danaRPump.pumpTime == 0) { // initial handshake was not successfull @@ -194,7 +199,7 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { danaRPump.lastConnection = 0; danaRPump.lastSettingsRead = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventInitializationChanged()); return; } long timeDiff = (danaRPump.pumpTime - System.currentTimeMillis()) / 1000L; @@ -213,7 +218,7 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { } RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventInitializationChanged()); NSUpload.uploadDeviceStatus(); if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { if (L.isEnabled(L.PUMP)) @@ -234,41 +239,41 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { DanaRPump danaRPump = DanaRPump.getInstance(); if (!isConnected()) return false; if (danaRPump.isTempBasalInProgress) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); SystemClock.sleep(500); } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStart(percent, durationInHours)); mSerialIOThread.sendMessage(new MsgStatusTempBasal()); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean tempBasalStop() { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); mSerialIOThread.sendMessage(new MsgStatusTempBasal()); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean extendedBolus(double insulin, int durationInHalfHours) { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingextendedbolus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingextendedbolus))); mSerialIOThread.sendMessage(new MsgSetExtendedBolusStart(insulin, (byte) (durationInHalfHours & 0xFF))); mSerialIOThread.sendMessage(new MsgStatusBolusExtended()); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean extendedBolusStop() { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingextendedbolus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingextendedbolus))); mSerialIOThread.sendMessage(new MsgSetExtendedBolusStop()); mSerialIOThread.sendMessage(new MsgStatusBolusExtended()); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } @@ -336,13 +341,13 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { public boolean updateBasalsInPump(final Profile profile) { DanaRPump danaRPump = DanaRPump.getInstance(); if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.updatingbasalrates))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.updatingbasalrates))); double[] basal = DanaRPump.getInstance().buildDanaRProfileRecord(profile); MsgSetSingleBasalProfile msgSet = new MsgSetSingleBasalProfile(basal); mSerialIOThread.sendMessage(msgSet); danaRPump.lastSettingsRead = 0; // force read full settings getPumpStatus(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.java index ef44cad527..55be88996b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/DanaRSPlugin.java @@ -5,12 +5,12 @@ import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; +import android.preference.Preference; import androidx.annotation.Nullable; import androidx.fragment.app.FragmentActivity; -import com.squareup.otto.Subscribe; - +import org.jetbrains.annotations.NotNull; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; @@ -40,7 +40,6 @@ import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.common.ManufacturerType; -import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.actions.defs.CustomAction; import info.nightscout.androidaps.plugins.general.actions.defs.CustomActionType; @@ -48,6 +47,7 @@ import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNo import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin; +import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.danaR.DanaRFragment; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump; @@ -59,9 +59,12 @@ import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.Round; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Created by mike on 03.09.2017. @@ -69,6 +72,8 @@ import info.nightscout.androidaps.utils.T; public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInterface, ConstraintsInterface, ProfileInterface { private Logger log = LoggerFactory.getLogger(L.PUMP); + private CompositeDisposable disposable = new CompositeDisposable(); + private static DanaRSPlugin plugin = null; public static DanaRSPlugin getPlugin() { @@ -107,14 +112,35 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte } } + @Override + public void updatePreferenceSummary(@NotNull Preference pref) { + super.updatePreferenceSummary(pref); + + if (pref.getKey().equals(MainApp.gs(R.string.key_danars_name))) + pref.setSummary(SP.getString(R.string.key_danars_name, "")); + } + @Override protected void onStart() { Context context = MainApp.instance().getApplicationContext(); Intent intent = new Intent(context, DanaRSService.class); context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); - MainApp.bus().register(this); - onStatusEvent(new EventDanaRSDeviceChange()); // load device name + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + MainApp.instance().getApplicationContext().unbindService(mConnection); + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventDanaRSDeviceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + loadAddress(); + }, FabricPrivacy::logException) + ); + loadAddress(); // load device name super.onStart(); } @@ -123,7 +149,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte Context context = MainApp.instance().getApplicationContext(); context.unbindService(mConnection); - MainApp.bus().unregister(this); + disposable.clear(); super.onStop(); } @@ -148,14 +174,7 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte } }; - @SuppressWarnings("UnusedParameters") - @Subscribe - public void onStatusEvent(final EventAppExit e) { - MainApp.instance().getApplicationContext().unbindService(mConnection); - } - - @Subscribe - public void onStatusEvent(final EventDanaRSDeviceChange e) { + private void loadAddress() { mDeviceAddress = SP.getString(R.string.key_danars_address, ""); mDeviceName = SP.getString(R.string.key_danars_name, ""); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/BLEScanActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/BLEScanActivity.java index 7262153d5c..42e910f291 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/BLEScanActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/BLEScanActivity.java @@ -17,9 +17,9 @@ import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.pump.danaRS.events.EventDanaRSDeviceChange; import info.nightscout.androidaps.utils.SP; @@ -156,7 +156,7 @@ public class BLEScanActivity extends NoSplashAppCompatActivity { SP.putString(R.string.key_danars_address, item.device.getAddress()); SP.putString(R.string.key_danars_name, mName.getText().toString()); item.device.createBond(); - MainApp.bus().post(new EventDanaRSDeviceChange()); + RxBus.INSTANCE.send(new EventDanaRSDeviceChange()); finish(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingProgressDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingProgressDialog.java index 544b0c862e..afb5e86387 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingProgressDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingProgressDialog.java @@ -5,7 +5,6 @@ import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; -import androidx.fragment.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -13,14 +12,19 @@ import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.fragment.app.DialogFragment; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.pump.danaRS.events.EventDanaRSPairingSuccess; +import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; public class PairingProgressDialog extends DialogFragment implements View.OnClickListener { + private CompositeDisposable disposable = new CompositeDisposable(); TextView statusView; ProgressBar progressBar; @@ -101,7 +105,11 @@ public class PairingProgressDialog extends DialogFragment implements View.OnClic @Override public void onResume() { super.onResume(); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventDanaRSPairingSuccess.class) + .observeOn(Schedulers.io()) + .subscribe(event -> pairingEnded = true, FabricPrivacy::logException) + ); running = true; if (pairingEnded) dismiss(); } @@ -117,15 +125,10 @@ public class PairingProgressDialog extends DialogFragment implements View.OnClic @Override public void onPause() { super.onPause(); - MainApp.bus().unregister(this); + disposable.clear(); running = false; } - @Subscribe - public void onStatusEvent(final EventDanaRSPairingSuccess ev) { - pairingEnded = true; - } - public void setHelperActivity(PairingHelperActivity activity) { this.helperActivity = activity; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet.java index 3d59b34e16..55a2e0c51e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet.java @@ -4,11 +4,15 @@ import android.annotation.TargetApi; import android.os.Build; import com.cozmo.danar.util.BleCommandUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.nio.charset.StandardCharsets; import java.util.Date; public class DanaRS_Packet { + private static final Logger log = LoggerFactory.getLogger(DanaRS_Packet.class); + protected static final int TYPE_START = 0; protected static final int OPCODE_START = 1; protected static final int DATA_START = 2; @@ -73,7 +77,7 @@ public class DanaRS_Packet { return ret; } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } return null; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_APS_History_Events.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_APS_History_Events.java index 6723e5a0a2..fde34c58e0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_APS_History_Events.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_APS_History_Events.java @@ -17,6 +17,7 @@ import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; @@ -223,7 +224,7 @@ public class DanaRS_Packet_APS_History_Events extends DanaRS_Packet { if (datetime > lastEventTimeLoaded) lastEventTimeLoaded = datetime; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.processinghistory) + ": " + status)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.processinghistory) + ": " + status)); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Stop.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Stop.java index 5e3fe3fbcd..7cb0a11f9b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Stop.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Set_Step_Bolus_Stop.java @@ -9,6 +9,7 @@ import info.nightscout.androidaps.R; import com.cozmo.danar.util.BleCommandUtil; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; @@ -47,16 +48,16 @@ public class DanaRS_Packet_Bolus_Set_Step_Bolus_Stop extends DanaRS_Packet { } } - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; stopped = true; if (!forced) { t.insulin = amount; - bolusingEvent.status = MainApp.gs(R.string.overview_bolusprogress_delivered); - bolusingEvent.percent = 100; + bolusingEvent.setStatus(MainApp.gs(R.string.overview_bolusprogress_delivered)); + bolusingEvent.setPercent(100); } else { - bolusingEvent.status = MainApp.gs(R.string.overview_bolusprogress_stoped); + bolusingEvent.setStatus(MainApp.gs(R.string.overview_bolusprogress_stoped)); } - MainApp.bus().post(bolusingEvent); + RxBus.INSTANCE.send(bolusingEvent); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_.java index ceaf49908b..07893346d7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_.java @@ -10,6 +10,7 @@ import java.util.GregorianCalendar; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.db.DanaRHistoryRecord; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.pump.danaR.comm.RecordTypes; import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRSyncStatus; import info.nightscout.androidaps.utils.DateUtil; @@ -109,7 +110,6 @@ public abstract class DanaRS_Packet_History_ extends DanaRS_Packet { log.debug("History packet: " + recordCode + " Date: " + datetimewihtsec.toLocaleString() + " Code: " + historyCode + " Value: " + value); - EventDanaRSyncStatus ev = new EventDanaRSyncStatus(); DanaRHistoryRecord danaRHistoryRecord = new DanaRHistoryRecord(); danaRHistoryRecord.setBytes(data); @@ -224,9 +224,7 @@ public abstract class DanaRS_Packet_History_ extends DanaRS_Packet { MainApp.getDbHelper().createOrUpdate(danaRHistoryRecord); - ev.message = DateUtil.dateAndTimeString(danaRHistoryRecord.recordDate); - ev.message += " " + messageType; - MainApp.bus().post(ev); + RxBus.INSTANCE.send(new EventDanaRSyncStatus(DateUtil.dateAndTimeString(danaRHistoryRecord.recordDate) + " " + messageType)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Complete.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Complete.java index 3c916dc399..00b6483fbb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Complete.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Complete.java @@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.treatments.Treatment; @@ -39,12 +40,12 @@ public class DanaRS_Packet_Notify_Delivery_Complete extends DanaRS_Packet { if (t != null) { t.insulin = deliveredInsulin; - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.status = String.format(MainApp.gs(R.string.bolusdelivering), deliveredInsulin); - bolusingEvent.t = t; - bolusingEvent.percent = Math.min((int) (deliveredInsulin / amount * 100), 100); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setStatus(String.format(MainApp.gs(R.string.bolusdelivering), deliveredInsulin)); + bolusingEvent.setT(t); + bolusingEvent.setPercent(Math.min((int) (deliveredInsulin / amount * 100), 100)); done = true; - MainApp.bus().post(bolusingEvent); + RxBus.INSTANCE.send(bolusingEvent); } if (L.isEnabled(L.PUMPCOMM)) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Rate_Display.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Rate_Display.java index 290e9743ef..1d650cb779 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Rate_Display.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Notify_Delivery_Rate_Display.java @@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.treatments.Treatment; @@ -40,12 +41,12 @@ public class DanaRS_Packet_Notify_Delivery_Rate_Display extends DanaRS_Packet { if (t != null) { lastReceive = System.currentTimeMillis(); t.insulin = deliveredInsulin; - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.status = String.format(MainApp.gs(R.string.bolusdelivering), deliveredInsulin); - bolusingEvent.t = t; - bolusingEvent.percent = Math.min((int) (deliveredInsulin / amount * 100), 100); - failed = bolusingEvent.percent < 100? true: false; - MainApp.bus().post(bolusingEvent); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setStatus(String.format(MainApp.gs(R.string.bolusdelivering), deliveredInsulin)); + bolusingEvent.setT(t); + bolusingEvent.setPercent(Math.min((int) (deliveredInsulin / amount * 100), 100)); + failed = bolusingEvent.getPercent() < 100? true: false; + RxBus.INSTANCE.send(bolusingEvent); } if (L.isEnabled(L.PUMPCOMM)) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSDeviceChange.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSDeviceChange.java deleted file mode 100644 index 224f939ad7..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSDeviceChange.java +++ /dev/null @@ -1,10 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.danaRS.events; - -import info.nightscout.androidaps.events.Event; - -/** - * Created by mike on 05.09.2017. - */ - -public class EventDanaRSDeviceChange extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSDeviceChange.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSDeviceChange.kt new file mode 100644 index 0000000000..8b87d59ec1 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSDeviceChange.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.pump.danaRS.events + +import info.nightscout.androidaps.events.Event + +class EventDanaRSDeviceChange : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSPacket.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSPacket.java deleted file mode 100644 index e0c401a27f..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSPacket.java +++ /dev/null @@ -1,16 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.danaRS.events; - -import info.nightscout.androidaps.events.Event; -import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet; - -/** - * Created by mike on 01.09.2017. - */ - -public class EventDanaRSPacket extends Event{ - public EventDanaRSPacket(DanaRS_Packet data) { - this.data = data; - } - - public DanaRS_Packet data; -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSPairingSuccess.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSPairingSuccess.java deleted file mode 100644 index e332e9e0b2..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSPairingSuccess.java +++ /dev/null @@ -1,10 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.danaRS.events; - -import info.nightscout.androidaps.events.Event; - -/** - * Created by mike on 01.09.2017. - */ - -public class EventDanaRSPairingSuccess extends Event{ -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSPairingSuccess.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSPairingSuccess.kt new file mode 100644 index 0000000000..6e0379a4d3 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/events/EventDanaRSPairingSuccess.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.pump.danaRS.events + +import info.nightscout.androidaps.events.Event + +class EventDanaRSPairingSuccess : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.java index 3ce5c49a1e..253b861811 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.java @@ -35,7 +35,6 @@ import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin; import info.nightscout.androidaps.plugins.pump.danaRS.activities.PairingHelperActivity; import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRSMessageHashTable; import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet; -import info.nightscout.androidaps.plugins.pump.danaRS.events.EventDanaRSPacket; import info.nightscout.androidaps.plugins.pump.danaRS.events.EventDanaRSPairingSuccess; import info.nightscout.androidaps.utils.SP; @@ -339,7 +338,7 @@ public class BLEComm { close(); isConnected = false; isConnecting = false; - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTED)); if (L.isEnabled(L.PUMPBTCOMM)) log.debug("Device was disconnected " + gatt.getDevice().getName());//Device was disconnected } @@ -452,7 +451,7 @@ public class BLEComm { if (L.isEnabled(L.PUMPBTCOMM)) log.debug("<<<<< " + "ENCRYPTION__PUMP_CHECK (PUMP)" + " " + DanaRS_Packet.toHexString(inputBuffer)); mSendQueue.clear(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED, MainApp.gs(R.string.pumperror))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTED, MainApp.gs(R.string.pumperror))); NSUpload.uploadError(MainApp.gs(R.string.pumperror)); Notification n = new Notification(Notification.PUMPERROR, MainApp.gs(R.string.pumperror), Notification.URGENT); RxBus.INSTANCE.send(new EventNewNotification(n)); @@ -460,13 +459,13 @@ public class BLEComm { if (L.isEnabled(L.PUMPBTCOMM)) log.debug("<<<<< " + "ENCRYPTION__PUMP_CHECK (BUSY)" + " " + DanaRS_Packet.toHexString(inputBuffer)); mSendQueue.clear(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED, MainApp.gs(R.string.pumpbusy))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTED, MainApp.gs(R.string.pumpbusy))); } else { // ERROR in response, wrong serial number if (L.isEnabled(L.PUMPBTCOMM)) log.debug("<<<<< " + "ENCRYPTION__PUMP_CHECK (ERROR)" + " " + DanaRS_Packet.toHexString(inputBuffer)); mSendQueue.clear(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED, MainApp.gs(R.string.connectionerror))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTED, MainApp.gs(R.string.connectionerror))); SP.remove(MainApp.gs(R.string.key_danars_pairingkey) + DanaRSPlugin.mDeviceName); Notification n = new Notification(Notification.WRONGSERIALNUMBER, MainApp.gs(R.string.wrongpassword), Notification.URGENT); RxBus.INSTANCE.send(new EventNewNotification(n)); @@ -496,7 +495,7 @@ public class BLEComm { if (L.isEnabled(L.PUMPBTCOMM)) log.debug("<<<<< " + "ENCRYPTION__PASSKEY_RETURN " + DanaRS_Packet.toHexString(inputBuffer)); // Paring is successfull, sending time info - MainApp.bus().post(new EventDanaRSPairingSuccess()); + RxBus.INSTANCE.send(new EventDanaRSPairingSuccess()); SendTimeInfo(); byte[] pairingKey = {inputBuffer[2], inputBuffer[3]}; // store pairing key to preferences @@ -515,7 +514,7 @@ public class BLEComm { if (L.isEnabled(L.PUMPBTCOMM)) log.debug("Pump user password: " + Integer.toHexString(pass)); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTED)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTED)); isConnected = true; isConnecting = false; if (L.isEnabled(L.PUMPBTCOMM)) @@ -546,14 +545,13 @@ public class BLEComm { // notify to sendMessage message.notify(); } - MainApp.bus().post(new EventDanaRSPacket(message)); } else { log.error("Unknown message received " + DanaRS_Packet.toHexString(inputBuffer)); } break; } } catch (Exception e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } startSignatureFound = false; packetIsValid = false; @@ -637,7 +635,7 @@ public class BLEComm { message.wait(5000); } catch (InterruptedException e) { log.error("sendMessage InterruptedException", e); - e.printStackTrace(); + log.error("Unhandled exception", e); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/DanaRSService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/DanaRSService.java index 4c46f80de7..2850f08c8d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/DanaRSService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/DanaRSService.java @@ -6,8 +6,6 @@ import android.os.Binder; import android.os.IBinder; import android.os.SystemClock; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,6 +25,7 @@ import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; +import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressDialog; import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperActivity; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; @@ -80,12 +79,15 @@ import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.commands.Command; import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; public class DanaRSService extends Service { private Logger log = LoggerFactory.getLogger(L.PUMPCOMM); + private CompositeDisposable disposable = new CompositeDisposable(); private BLEComm bleComm = BLEComm.getInstance(this); @@ -97,12 +99,25 @@ public class DanaRSService extends Service { private long lastApproachingDailyLimit = 0; public DanaRSService() { - try { - MainApp.bus().unregister(this); - } catch (RuntimeException x) { - // Ignore - } - MainApp.bus().register(this); + } + + @Override + public void onCreate() { + super.onCreate(); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (L.isEnabled(L.PUMP)) log.debug("EventAppExit received"); + stopSelf(); + }, FabricPrivacy::logException) + ); + } + + @Override + public void onDestroy() { + disposable.clear(); + super.onDestroy(); } public boolean isConnected() { @@ -132,14 +147,14 @@ public class DanaRSService extends Service { public void getPumpStatus() { DanaRPump danaRPump = DanaRPump.getInstance(); try { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpstatus))); bleComm.sendMessage(new DanaRS_Packet_General_Initial_Screen_Information()); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingextendedbolusstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingextendedbolusstatus))); bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_Extended_Bolus_State()); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_Step_Bolus_Information()); // last bolus, bolusStep, maxBolus - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingtempbasalstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingtempbasalstatus))); bleComm.sendMessage(new DanaRS_Packet_Basal_Get_Temporary_Basal_State()); danaRPump.lastConnection = System.currentTimeMillis(); @@ -147,14 +162,14 @@ public class DanaRSService extends Service { Profile profile = ProfileFunctions.getInstance().getProfile(); PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); if (profile != null && Math.abs(danaRPump.currentBasal - profile.getBasal()) >= pump.getPumpDescription().basalStep) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); bleComm.sendMessage(new DanaRS_Packet_Basal_Get_Basal_Rate()); // basal profile, basalStep, maxBasal if (!pump.isThisProfileSet(profile) && !ConfigBuilderPlugin.getPlugin().getCommandQueue().isRunning(Command.CommandType.BASALPROFILE)) { - MainApp.bus().post(new EventProfileNeedsUpdate()); + RxBus.INSTANCE.send(new EventProfileNeedsUpdate()); } } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime))); bleComm.sendMessage(new DanaRS_Packet_Option_Get_Pump_Time()); long timeDiff = (danaRPump.pumpTime - System.currentTimeMillis()) / 1000L; @@ -163,7 +178,7 @@ public class DanaRSService extends Service { // deinitialize pump danaRPump.lastConnection = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventInitializationChanged()); return; } if (L.isEnabled(L.PUMPCOMM)) @@ -183,7 +198,7 @@ public class DanaRSService extends Service { //deinitialize pump danaRPump.lastConnection = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventInitializationChanged()); return; } else { waitForWholeMinute(); // Dana can set only whole minute @@ -198,7 +213,7 @@ public class DanaRSService extends Service { long now = System.currentTimeMillis(); if (danaRPump.lastSettingsRead + 60 * 60 * 1000L < now || !pump.isInitialized()) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); bleComm.sendMessage(new DanaRS_Packet_General_Get_Shipping_Information()); // serial no bleComm.sendMessage(new DanaRS_Packet_General_Get_Pump_Check()); // firmware bleComm.sendMessage(new DanaRS_Packet_Basal_Get_Profile_Number()); @@ -212,8 +227,8 @@ public class DanaRSService extends Service { loadEvents(); - MainApp.bus().post(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventDanaRNewStatus()); + RxBus.INSTANCE.send(new EventInitializationChanged()); NSUpload.uploadDeviceStatus(); if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { if (L.isEnabled(L.PUMPCOMM)) @@ -234,7 +249,7 @@ public class DanaRSService extends Service { public PumpEnactResult loadEvents() { - if (!MainApp.getSpecificPlugin(DanaRSPlugin.class).isInitialized()) { + if (!DanaRSPlugin.getPlugin().isInitialized()) { PumpEnactResult result = new PumpEnactResult().success(false); result.comment = "pump not initialized"; return result; @@ -250,7 +265,7 @@ public class DanaRSService extends Service { } else { msg = new DanaRS_Packet_APS_History_Events(lastHistoryFetched); if (L.isEnabled(L.PUMPCOMM)) - log.debug("Loading event history from: " +DateUtil.dateAndTimeFullString(lastHistoryFetched)); + log.debug("Loading event history from: " + DateUtil.dateAndTimeFullString(lastHistoryFetched)); } bleComm.sendMessage(msg); while (!msg.done && bleComm.isConnected()) { @@ -278,7 +293,7 @@ public class DanaRSService extends Service { if (!isConnected()) return false; if (BolusProgressDialog.stopPressed) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.startingbolus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.startingbolus))); bolusingTreatment = t; final int preferencesSpeed = SP.getInt(R.string.key_danars_bolusspeed, 0); DanaRS_Packet_Bolus_Set_Step_Bolus_Start start = new DanaRS_Packet_Bolus_Set_Step_Bolus_Start(insulin, preferencesSpeed); @@ -314,9 +329,9 @@ public class DanaRSService extends Service { } } - final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.t = t; - bolusingEvent.percent = 99; + final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setT(t); + bolusingEvent.setPercent(99); bolusingTreatment = null; int speed = 12; @@ -335,8 +350,8 @@ public class DanaRSService extends Service { long expectedEnd = bolusStart + bolusDurationInMSec + 2000; while (System.currentTimeMillis() < expectedEnd) { long waitTime = expectedEnd - System.currentTimeMillis(); - bolusingEvent.status = String.format(MainApp.gs(R.string.waitingforestimatedbolusend), waitTime / 1000); - MainApp.bus().post(bolusingEvent); + bolusingEvent.setStatus(String.format(MainApp.gs(R.string.waitingforestimatedbolusend), waitTime / 1000)); + RxBus.INSTANCE.send(bolusingEvent); SystemClock.sleep(1000); } // do not call loadEvents() directly, reconnection may be needed @@ -344,10 +359,10 @@ public class DanaRSService extends Service { @Override public void run() { // reread bolus status - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_Step_Bolus_Information()); // last bolus - bolusingEvent.percent = 100; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.disconnecting))); + bolusingEvent.setPercent(100); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.disconnecting))); } }); return !start.failed; @@ -372,30 +387,30 @@ public class DanaRSService extends Service { public boolean tempBasal(Integer percent, int durationInHours) { if (!isConnected()) return false; if (DanaRPump.getInstance().isTempBasalInProgress) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal()); SystemClock.sleep(500); } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Temporary_Basal(percent, durationInHours)); SystemClock.sleep(200); bleComm.sendMessage(new DanaRS_Packet_Basal_Get_Temporary_Basal_State()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean highTempBasal(Integer percent) { if (DanaRPump.getInstance().isTempBasalInProgress) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal()); SystemClock.sleep(500); } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); bleComm.sendMessage(new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(percent)); bleComm.sendMessage(new DanaRS_Packet_Basal_Get_Temporary_Basal_State()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } @@ -406,52 +421,52 @@ public class DanaRSService extends Service { } if (DanaRPump.getInstance().isTempBasalInProgress) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal()); SystemClock.sleep(500); } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); bleComm.sendMessage(new DanaRS_Packet_APS_Basal_Set_Temporary_Basal(percent, durationInMinutes == 15, durationInMinutes == 30)); bleComm.sendMessage(new DanaRS_Packet_Basal_Get_Temporary_Basal_State()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean tempBasalStop() { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); bleComm.sendMessage(new DanaRS_Packet_Basal_Set_Cancel_Temporary_Basal()); bleComm.sendMessage(new DanaRS_Packet_Basal_Get_Temporary_Basal_State()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean extendedBolus(Double insulin, int durationInHalfHours) { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingextendedbolus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingextendedbolus))); bleComm.sendMessage(new DanaRS_Packet_Bolus_Set_Extended_Bolus(insulin, durationInHalfHours)); SystemClock.sleep(200); bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_Extended_Bolus_State()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean extendedBolusStop() { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingextendedbolus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingextendedbolus))); bleComm.sendMessage(new DanaRS_Packet_Bolus_Set_Extended_Bolus_Cancel()); bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_Extended_Bolus_State()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean updateBasalsInPump(Profile profile) { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.updatingbasalrates))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.updatingbasalrates))); double[] basal = DanaRPump.getInstance().buildDanaRProfileRecord(profile); DanaRS_Packet_Basal_Set_Profile_Basal_Rate msgSet = new DanaRS_Packet_Basal_Set_Profile_Basal_Rate(0, basal); bleComm.sendMessage(msgSet); @@ -459,7 +474,7 @@ public class DanaRSService extends Service { bleComm.sendMessage(msgActivate); DanaRPump.getInstance().lastSettingsRead = 0; // force read full settings getPumpStatus(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } @@ -529,21 +544,13 @@ public class DanaRSService extends Service { return START_STICKY; } - @Subscribe - public void onStatusEvent(EventAppExit event) { - if (L.isEnabled(L.PUMP)) - log.debug("EventAppExit received"); - - stopSelf(); - } - void waitForWholeMinute() { while (true) { long time = DateUtil.now(); long timeToWholeMinute = (60000 - time % 60000); if (timeToWholeMinute > 59800 || timeToWholeMinute < 300) break; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.waitingfortimesynchronization, (int) (timeToWholeMinute / 1000)))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.waitingfortimesynchronization, (int) (timeToWholeMinute / 1000)))); SystemClock.sleep(Math.min(timeToWholeMinute, 100)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/DanaRv2Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/DanaRv2Plugin.java index 94e6929d21..3437d37656 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/DanaRv2Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/DanaRv2Plugin.java @@ -6,8 +6,6 @@ import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; -import com.squareup.otto.Subscribe; - import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; @@ -17,6 +15,7 @@ import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.danaR.AbstractDanaRPlugin; @@ -26,14 +25,18 @@ import info.nightscout.androidaps.plugins.pump.danaRv2.services.DanaRv2Execution import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.Round; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Created by mike on 05.08.2016. */ public class DanaRv2Plugin extends AbstractDanaRPlugin { + private CompositeDisposable disposable = new CompositeDisposable(); private static DanaRv2Plugin plugin = null; @@ -56,7 +59,13 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { Intent intent = new Intent(context, DanaRv2ExecutionService.class); context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + MainApp.instance().getApplicationContext().unbindService(mConnection); + }, FabricPrivacy::logException) + ); super.onStart(); } @@ -65,7 +74,7 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { Context context = MainApp.instance().getApplicationContext(); context.unbindService(mConnection); - MainApp.bus().unregister(this); + disposable.clear(); super.onStop(); } @@ -85,12 +94,6 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { } }; - @SuppressWarnings("UnusedParameters") - @Subscribe - public void onStatusEvent(final EventAppExit e) { - MainApp.instance().getApplicationContext().unbindService(mConnection); - } - // Plugin base interface @Override public String getName() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgCheckValue_v2.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgCheckValue_v2.java index f5341f033c..3070c52b64 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgCheckValue_v2.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgCheckValue_v2.java @@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.androidaps.events.EventRebuildTabs; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.bus.RxBus; @@ -45,23 +45,23 @@ public class MsgCheckValue_v2 extends MessageBase { pump.lastConnection = 0; Notification notification = new Notification(Notification.WRONG_DRIVER, MainApp.gs(R.string.pumpdrivercorrected), Notification.NORMAL); RxBus.INSTANCE.send(new EventNewNotification(notification)); - MainApp.getSpecificPlugin(DanaRPlugin.class).disconnect("Wrong Model"); + DanaRPlugin.getPlugin().disconnect("Wrong Model"); log.error("Wrong model selected. Switching to Korean DanaR"); - MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setPluginEnabled(PluginType.PUMP, true); - MainApp.getSpecificPlugin(DanaRKoreanPlugin.class).setFragmentVisible(PluginType.PUMP, true); - MainApp.getSpecificPlugin(DanaRPlugin.class).setPluginEnabled(PluginType.PUMP, false); - MainApp.getSpecificPlugin(DanaRPlugin.class).setFragmentVisible(PluginType.PUMP, false); + DanaRKoreanPlugin.getPlugin().setPluginEnabled(PluginType.PUMP, true); + DanaRKoreanPlugin.getPlugin().setFragmentVisible(PluginType.PUMP, true); + DanaRPlugin.getPlugin().setPluginEnabled(PluginType.PUMP, false); + DanaRPlugin.getPlugin().setFragmentVisible(PluginType.PUMP, false); DanaRPump.reset(); // mark not initialized //If profile coming from pump, switch it as well - if (MainApp.getSpecificPlugin(DanaRPlugin.class).isEnabled(PluginType.PROFILE)) { - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setPluginEnabled(PluginType.PROFILE, false); - (MainApp.getSpecificPlugin(DanaRKoreanPlugin.class)).setPluginEnabled(PluginType.PROFILE, true); + if (DanaRPlugin.getPlugin().isEnabled(PluginType.PROFILE)) { + (DanaRPlugin.getPlugin()).setPluginEnabled(PluginType.PROFILE, false); + (DanaRKoreanPlugin.getPlugin()).setPluginEnabled(PluginType.PROFILE, true); } ConfigBuilderPlugin.getPlugin().storeSettings("ChangingDanaRv2Driver"); - MainApp.bus().post(new EventRefreshGui()); + RxBus.INSTANCE.send(new EventRebuildTabs()); ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection return; } @@ -72,19 +72,19 @@ public class MsgCheckValue_v2 extends MessageBase { RxBus.INSTANCE.send(new EventNewNotification(notification)); DanaRKoreanPlugin.getPlugin().disconnect("Wrong Model"); log.error("Wrong model selected. Switching to non APS DanaR"); - (MainApp.getSpecificPlugin(DanaRv2Plugin.class)).setPluginEnabled(PluginType.PUMP, false); - (MainApp.getSpecificPlugin(DanaRv2Plugin.class)).setFragmentVisible(PluginType.PUMP, false); - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setPluginEnabled(PluginType.PUMP, true); - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setFragmentVisible(PluginType.PUMP, true); + (DanaRv2Plugin.getPlugin()).setPluginEnabled(PluginType.PUMP, false); + (DanaRv2Plugin.getPlugin()).setFragmentVisible(PluginType.PUMP, false); + (DanaRPlugin.getPlugin()).setPluginEnabled(PluginType.PUMP, true); + (DanaRPlugin.getPlugin()).setFragmentVisible(PluginType.PUMP, true); //If profile coming from pump, switch it as well - if (MainApp.getSpecificPlugin(DanaRv2Plugin.class).isEnabled(PluginType.PROFILE)) { - (MainApp.getSpecificPlugin(DanaRv2Plugin.class)).setPluginEnabled(PluginType.PROFILE, false); - (MainApp.getSpecificPlugin(DanaRPlugin.class)).setPluginEnabled(PluginType.PROFILE, true); + if (DanaRv2Plugin.getPlugin().isEnabled(PluginType.PROFILE)) { + (DanaRv2Plugin.getPlugin()).setPluginEnabled(PluginType.PROFILE, false); + (DanaRPlugin.getPlugin()).setPluginEnabled(PluginType.PROFILE, true); } ConfigBuilderPlugin.getPlugin().storeSettings("ChangingDanaRv2Driver"); - MainApp.bus().post(new EventRefreshGui()); + RxBus.INSTANCE.send(new EventRebuildTabs()); ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("PumpDriverChange", null); // force new connection return; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgHistoryEvents_v2.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgHistoryEvents_v2.java index 43ec35702f..3ef43cad86 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgHistoryEvents_v2.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgHistoryEvents_v2.java @@ -13,6 +13,7 @@ import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump; import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase; @@ -199,6 +200,6 @@ public class MsgHistoryEvents_v2 extends MessageBase { if (datetime > lastEventTimeLoaded) lastEventTimeLoaded = datetime; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.processinghistory) + ": " + status)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.processinghistory) + ": " + status)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/services/DanaRv2ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/services/DanaRv2ExecutionService.java index 2721ec7ff8..e528a67fae 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/services/DanaRv2ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/services/DanaRv2ExecutionService.java @@ -6,8 +6,6 @@ import android.content.IntentFilter; import android.os.Binder; import android.os.SystemClock; -import com.squareup.otto.Subscribe; - import java.io.IOException; import java.util.Date; @@ -74,17 +72,20 @@ import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.commands.Command; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { + private CompositeDisposable disposable = new CompositeDisposable(); private long lastHistoryFetched = 0; public DanaRv2ExecutionService() { mBinder = new LocalBinder(); - registerBus(); MainApp.instance().getApplicationContext().registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); } @@ -94,32 +95,36 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { } } - private void registerBus() { - try { - MainApp.bus().unregister(this); - } catch (RuntimeException x) { - // Ignore - } - MainApp.bus().register(this); + @Override + public void onCreate() { + super.onCreate(); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (mSerialIOThread != null) + mSerialIOThread.disconnect("EventPreferenceChange"); + }, FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (L.isEnabled(L.PUMP)) + log.debug("EventAppExit received"); + + if (mSerialIOThread != null) + mSerialIOThread.disconnect("Application exit"); + MainApp.instance().getApplicationContext().unregisterReceiver(receiver); + stopSelf(); + }, FabricPrivacy::logException) + ); } - @Subscribe - public void onStatusEvent(EventAppExit event) { - if (L.isEnabled(L.PUMP)) - log.debug("EventAppExit received"); - - if (mSerialIOThread != null) - mSerialIOThread.disconnect("Application exit"); - - MainApp.instance().getApplicationContext().unregisterReceiver(receiver); - - stopSelf(); - } - - @Subscribe - public void onStatusEvent(final EventPreferenceChange pch) { - if (mSerialIOThread != null) - mSerialIOThread.disconnect("EventPreferenceChange"); + @Override + public void onDestroy() { + disposable.clear(); + super.onDestroy(); } public void connect() { @@ -150,7 +155,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { } mSerialIOThread = new SerialIOThread(mRfcommSocket, MessageHashTableRv2.INSTANCE); mHandshakeInProgress = true; - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, 0)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.HANDSHAKING, 0)); } mConnectionInProgress = false; @@ -160,7 +165,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { public void getPumpStatus() { DanaRPump danaRPump = DanaRPump.getInstance(); try { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpstatus))); MsgStatus statusMsg = new MsgStatus(); MsgStatusBasic statusBasicMsg = new MsgStatusBasic(); MsgStatusTempBasal_v2 tempStatusMsg = new MsgStatusTempBasal_v2(); @@ -174,12 +179,12 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { } } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); mSerialIOThread.sendMessage(statusMsg); mSerialIOThread.sendMessage(statusBasicMsg); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingtempbasalstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingtempbasalstatus))); mSerialIOThread.sendMessage(tempStatusMsg); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingextendedbolusstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingextendedbolusstatus))); mSerialIOThread.sendMessage(exStatusMsg); danaRPump.lastConnection = System.currentTimeMillis(); @@ -187,14 +192,14 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { Profile profile = ProfileFunctions.getInstance().getProfile(); PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); if (profile != null && Math.abs(danaRPump.currentBasal - profile.getBasal()) >= pump.getPumpDescription().basalStep) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); mSerialIOThread.sendMessage(new MsgSettingBasal()); if (!pump.isThisProfileSet(profile) && !ConfigBuilderPlugin.getPlugin().getCommandQueue().isRunning(Command.CommandType.BASALPROFILE)) { - MainApp.bus().post(new EventProfileNeedsUpdate()); + RxBus.INSTANCE.send(new EventProfileNeedsUpdate()); } } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumptime))); mSerialIOThread.sendMessage(new MsgSettingPumpTime()); if (danaRPump.pumpTime == 0) { // initial handshake was not successfull @@ -202,7 +207,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { danaRPump.lastConnection = 0; danaRPump.lastSettingsRead = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventInitializationChanged()); return; } long timeDiff = (danaRPump.pumpTime - System.currentTimeMillis()) / 1000L; @@ -223,7 +228,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { //deinitialize pump danaRPump.lastConnection = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventInitializationChanged()); return; } else { waitForWholeMinute(); // Dana can set only whole minute @@ -238,7 +243,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { long now = System.currentTimeMillis(); if (danaRPump.lastSettingsRead + 60 * 60 * 1000L < now || !pump.isInitialized()) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingpumpsettings))); mSerialIOThread.sendMessage(new MsgSettingShippingInfo()); mSerialIOThread.sendMessage(new MsgSettingActiveProfile()); mSerialIOThread.sendMessage(new MsgSettingMeal()); @@ -256,7 +261,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { loadEvents(); RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventInitializationChanged()); NSUpload.uploadDeviceStatus(); if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { if (L.isEnabled(L.PUMP)) @@ -277,15 +282,15 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { DanaRPump danaRPump = DanaRPump.getInstance(); if (!isConnected()) return false; if (danaRPump.isTempBasalInProgress) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); SystemClock.sleep(500); } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStart(percent, durationInHours)); mSerialIOThread.sendMessage(new MsgStatusTempBasal_v2()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } @@ -293,15 +298,15 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { DanaRPump danaRPump = DanaRPump.getInstance(); if (!isConnected()) return false; if (danaRPump.isTempBasalInProgress) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); SystemClock.sleep(500); } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); mSerialIOThread.sendMessage(new MsgSetAPSTempBasalStart_v2(percent)); mSerialIOThread.sendMessage(new MsgStatusTempBasal_v2()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } @@ -314,45 +319,45 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { if (!isConnected()) return false; if (danaRPump.isTempBasalInProgress) { - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); SystemClock.sleep(500); } - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingtempbasal))); mSerialIOThread.sendMessage(new MsgSetAPSTempBasalStart_v2(percent, durationInMinutes == 15, durationInMinutes == 30)); mSerialIOThread.sendMessage(new MsgStatusTempBasal_v2()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean tempBasalStop() { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingtempbasal))); mSerialIOThread.sendMessage(new MsgSetTempBasalStop()); mSerialIOThread.sendMessage(new MsgStatusTempBasal_v2()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean extendedBolus(double insulin, int durationInHalfHours) { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.settingextendedbolus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.settingextendedbolus))); mSerialIOThread.sendMessage(new MsgSetExtendedBolusStart(insulin, (byte) (durationInHalfHours & 0xFF))); mSerialIOThread.sendMessage(new MsgStatusBolusExtended_v2()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } public boolean extendedBolusStop() { if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingextendedbolus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.stoppingextendedbolus))); mSerialIOThread.sendMessage(new MsgSetExtendedBolusStop()); mSerialIOThread.sendMessage(new MsgStatusBolusExtended_v2()); loadEvents(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } @@ -360,7 +365,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { if (!isConnected()) return false; if (BolusProgressDialog.stopPressed) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.startingbolus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.startingbolus))); mBolusingTreatment = t; final int preferencesSpeed = SP.getInt(R.string.key_danars_bolusspeed, 0); MessageBase start; @@ -398,9 +403,9 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { } } - final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.t = t; - bolusingEvent.percent = 99; + final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setT(t); + bolusingEvent.setPercent(99); mBolusingTreatment = null; int speed = 12; @@ -419,8 +424,8 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { long expectedEnd = bolusStart + bolusDurationInMSec + 2000; while (System.currentTimeMillis() < expectedEnd) { long waitTime = expectedEnd - System.currentTimeMillis(); - bolusingEvent.status = String.format(MainApp.gs(R.string.waitingforestimatedbolusend), waitTime / 1000); - MainApp.bus().post(bolusingEvent); + bolusingEvent.setStatus(String.format(MainApp.gs(R.string.waitingforestimatedbolusend), waitTime / 1000)); + RxBus.INSTANCE.send(bolusingEvent); SystemClock.sleep(1000); } // do not call loadEvents() directly, reconnection may be needed @@ -428,10 +433,10 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { @Override public void run() { // load last bolus status - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.gettingbolusstatus))); mSerialIOThread.sendMessage(new MsgStatus()); - bolusingEvent.percent = 100; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.disconnecting))); + bolusingEvent.setPercent(100); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.disconnecting))); } }); return !start.failed; @@ -466,7 +471,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { public PumpEnactResult loadEvents() { DanaRPump danaRPump = DanaRPump.getInstance(); - if (!MainApp.getSpecificPlugin(DanaRv2Plugin.class).isInitialized()) { + if (!DanaRv2Plugin.getPlugin().isInitialized()) { PumpEnactResult result = new PumpEnactResult().success(false); result.comment = "pump not initialized"; return result; @@ -496,7 +501,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { public boolean updateBasalsInPump(final Profile profile) { DanaRPump danaRPump = DanaRPump.getInstance(); if (!isConnected()) return false; - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.updatingbasalrates))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.updatingbasalrates))); double[] basal = DanaRPump.getInstance().buildDanaRProfileRecord(profile); MsgSetBasalProfile msgSet = new MsgSetBasalProfile((byte) 0, basal); mSerialIOThread.sendMessage(msgSet); @@ -504,7 +509,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { mSerialIOThread.sendMessage(msgActivate); danaRPump.lastSettingsRead = 0; // force read full settings getPumpStatus(); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); return true; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/EventLocalInsightUpdateGUI.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/EventLocalInsightUpdateGUI.java deleted file mode 100644 index 89b001891c..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/EventLocalInsightUpdateGUI.java +++ /dev/null @@ -1,6 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.insight; - -import info.nightscout.androidaps.events.EventUpdateGui; - -public class EventLocalInsightUpdateGUI extends EventUpdateGui { -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightFragment.java index b973424d71..3464a1803a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightFragment.java @@ -3,8 +3,6 @@ package info.nightscout.androidaps.plugins.pump.insight; import android.os.Bundle; import android.os.Handler; import android.os.Looper; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -12,14 +10,16 @@ import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; import java.util.ArrayList; import java.util.List; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.pump.insight.app_layer.parameter_blocks.TBROverNotificationBlock; import info.nightscout.androidaps.plugins.pump.insight.descriptors.ActiveBasalRate; @@ -28,11 +28,16 @@ import info.nightscout.androidaps.plugins.pump.insight.descriptors.ActiveTBR; import info.nightscout.androidaps.plugins.pump.insight.descriptors.CartridgeStatus; import info.nightscout.androidaps.plugins.pump.insight.descriptors.InsightState; import info.nightscout.androidaps.plugins.pump.insight.descriptors.TotalDailyDose; +import info.nightscout.androidaps.plugins.pump.insight.events.EventLocalInsightUpdateGUI; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; -public class LocalInsightFragment extends SubscriberFragment implements View.OnClickListener { +public class LocalInsightFragment extends Fragment implements View.OnClickListener { + private CompositeDisposable disposable = new CompositeDisposable(); private static final boolean ENABLE_OPERATING_MODE_BUTTON = false; @@ -61,6 +66,23 @@ public class LocalInsightFragment extends SubscriberFragment implements View.OnC return view; } + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventLocalInsightUpdateGUI.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI(), FabricPrivacy::logException) + ); + updateGUI(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + @Override public synchronized void onDestroyView() { super.onDestroyView(); @@ -121,12 +143,6 @@ public class LocalInsightFragment extends SubscriberFragment implements View.OnC } } - @Subscribe - public void onUpdateGUIEvent(EventLocalInsightUpdateGUI event) { - updateGUI(); - } - - @Override protected void updateGUI() { if (!viewsCreated) return; statusItemContainer.removeAllViews(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java index 087e9e8d7a..5104e70fe8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java @@ -78,6 +78,7 @@ import info.nightscout.androidaps.plugins.pump.insight.app_layer.history.history import info.nightscout.androidaps.plugins.pump.insight.app_layer.parameter_blocks.ActiveBRProfileBlock; import info.nightscout.androidaps.plugins.pump.insight.app_layer.parameter_blocks.BRProfile1Block; import info.nightscout.androidaps.plugins.pump.insight.app_layer.parameter_blocks.BRProfileBlock; +import info.nightscout.androidaps.plugins.pump.insight.app_layer.parameter_blocks.FactoryMinBasalAmountBlock; import info.nightscout.androidaps.plugins.pump.insight.app_layer.parameter_blocks.FactoryMinBolusAmountBlock; import info.nightscout.androidaps.plugins.pump.insight.app_layer.parameter_blocks.MaxBasalAmountBlock; import info.nightscout.androidaps.plugins.pump.insight.app_layer.parameter_blocks.MaxBolusAmountBlock; @@ -118,6 +119,7 @@ import info.nightscout.androidaps.plugins.pump.insight.descriptors.InsightState; import info.nightscout.androidaps.plugins.pump.insight.descriptors.OperatingMode; import info.nightscout.androidaps.plugins.pump.insight.descriptors.PumpTime; import info.nightscout.androidaps.plugins.pump.insight.descriptors.TotalDailyDose; +import info.nightscout.androidaps.plugins.pump.insight.events.EventLocalInsightUpdateGUI; import info.nightscout.androidaps.plugins.pump.insight.exceptions.InsightException; import info.nightscout.androidaps.plugins.pump.insight.exceptions.app_layer_errors.AppLayerErrorException; import info.nightscout.androidaps.plugins.pump.insight.exceptions.app_layer_errors.NoActiveTBRToCanceLException; @@ -147,8 +149,9 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con } else if (binder instanceof InsightAlertService.LocalBinder) { alertService = ((InsightAlertService.LocalBinder) binder).getService(); } - if (connectionService != null && alertService != null) - MainApp.bus().post(new EventInitializationChanged()); + if (connectionService != null && alertService != null) { + RxBus.INSTANCE.send(new EventInitializationChanged()); + } } @Override @@ -423,8 +426,8 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con } lastUpdated = System.currentTimeMillis(); new Handler(Looper.getMainLooper()).post(() -> { - MainApp.bus().post(new EventLocalInsightUpdateGUI()); - MainApp.bus().post(new EventRefreshOverview("LocalInsightPlugin::fetchStatus")); + RxBus.INSTANCE.send(new EventLocalInsightUpdateGUI()); + RxBus.INSTANCE.send(new EventRefreshOverview("LocalInsightPlugin::fetchStatus")); }); } @@ -432,7 +435,7 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con maximumBolusAmount = ParameterBlockUtil.readParameterBlock(connectionService, Service.CONFIGURATION, MaxBolusAmountBlock.class).getAmountLimitation(); maximumBasalAmount = ParameterBlockUtil.readParameterBlock(connectionService, Service.CONFIGURATION, MaxBasalAmountBlock.class).getAmountLimitation(); minimumBolusAmount = ParameterBlockUtil.readParameterBlock(connectionService, Service.CONFIGURATION, FactoryMinBolusAmountBlock.class).getAmountLimitation(); - minimumBasalAmount = ParameterBlockUtil.readParameterBlock(connectionService, Service.CONFIGURATION, FactoryMinBolusAmountBlock.class).getAmountLimitation(); + minimumBasalAmount = ParameterBlockUtil.readParameterBlock(connectionService, Service.CONFIGURATION, FactoryMinBasalAmountBlock.class).getAmountLimitation(); this.pumpDescription.basalMaximumRate = maximumBasalAmount; this.pumpDescription.basalMinimumRate = minimumBasalAmount; limitsFetched = true; @@ -554,11 +557,11 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con result.enacted = true; Treatment t = new Treatment(); t.isSMB = detailedBolusInfo.isSMB; - final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.t = t; - bolusingEvent.status = MainApp.gs(R.string.insight_delivered, 0d, insulin); - bolusingEvent.percent = 0; - MainApp.bus().post(bolusingEvent); + final EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setT(t); + bolusingEvent.setStatus(MainApp.gs(R.string.insight_delivered, 0d, insulin)); + bolusingEvent.setPercent(0); + RxBus.INSTANCE.send(bolusingEvent); int trials = 0; InsightBolusID insightBolusID = new InsightBolusID(); insightBolusID.bolusID = bolusID; @@ -585,18 +588,18 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con } if (activeBolus != null) { trials = -1; - int percentBefore = bolusingEvent.percent; - bolusingEvent.percent = (int) (100D / activeBolus.getInitialAmount() * (activeBolus.getInitialAmount() - activeBolus.getRemainingAmount())); - bolusingEvent.status = MainApp.gs(R.string.insight_delivered, activeBolus.getInitialAmount() - activeBolus.getRemainingAmount(), activeBolus.getInitialAmount()); - if (percentBefore != bolusingEvent.percent) - MainApp.bus().post(bolusingEvent); + int percentBefore = bolusingEvent.getPercent(); + bolusingEvent.setPercent((int) (100D / activeBolus.getInitialAmount() * (activeBolus.getInitialAmount() - activeBolus.getRemainingAmount()))); + bolusingEvent.setStatus(MainApp.gs(R.string.insight_delivered, activeBolus.getInitialAmount() - activeBolus.getRemainingAmount(), activeBolus.getInitialAmount())); + if (percentBefore != bolusingEvent.getPercent()) + RxBus.INSTANCE.send(bolusingEvent); } else { synchronized ($bolusLock) { if (bolusCancelled || trials == -1 || trials++ >= 5) { if (!bolusCancelled) { - bolusingEvent.status = MainApp.gs(R.string.insight_delivered, insulin, insulin); - bolusingEvent.percent = 100; - MainApp.bus().post(bolusingEvent); + bolusingEvent.setStatus(MainApp.gs(R.string.insight_delivered, insulin, insulin)); + bolusingEvent.setPercent(100); + RxBus.INSTANCE.send(bolusingEvent); } break; } @@ -1164,7 +1167,7 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con } catch (Exception e) { log.error("Exception while reading history", e); } - new Handler(Looper.getMainLooper()).post(() -> MainApp.bus().post(new EventRefreshOverview("LocalInsightPlugin::readHistory"))); + new Handler(Looper.getMainLooper()).post(() -> RxBus.INSTANCE.send(new EventRefreshOverview("LocalInsightPlugin::readHistory"))); } private void processHistoryEvents(String serial, List historyEvents) { @@ -1523,7 +1526,7 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con data.put("notes", note); NSUpload.uploadCareportalEntryToNS(data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } } @@ -1551,7 +1554,7 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con data.put("eventType", event); NSUpload.uploadCareportalEntryToNS(data); } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } } @@ -1599,9 +1602,9 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con activeTBR = null; activeBoluses = null; tbrOverNotificationBlock = null; - new Handler(Looper.getMainLooper()).post(() -> MainApp.bus().post(new EventRefreshOverview("LocalInsightPlugin::onStateChanged"))); + new Handler(Looper.getMainLooper()).post(() -> RxBus.INSTANCE.send(new EventRefreshOverview("LocalInsightPlugin::onStateChanged"))); } - new Handler(Looper.getMainLooper()).post(() -> MainApp.bus().post(new EventLocalInsightUpdateGUI())); + new Handler(Looper.getMainLooper()).post(() -> RxBus.INSTANCE.send(new EventLocalInsightUpdateGUI())); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/app_layer/history/history_events/HistoryEvent.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/app_layer/history/history_events/HistoryEvent.java index 874c908ca5..f6b3dc588c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/app_layer/history/history_events/HistoryEvent.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/app_layer/history/history_events/HistoryEvent.java @@ -3,8 +3,11 @@ package info.nightscout.androidaps.plugins.pump.insight.app_layer.history.histor import info.nightscout.androidaps.plugins.pump.insight.ids.HistoryEventIDs; import info.nightscout.androidaps.plugins.pump.insight.utils.BOCUtil; import info.nightscout.androidaps.plugins.pump.insight.utils.ByteBuf; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class HistoryEvent implements Comparable { + private static final Logger log = LoggerFactory.getLogger(HistoryEvent.class); private int eventYear; private int eventMonth; @@ -22,10 +25,8 @@ public class HistoryEvent implements Comparable { else { try { event = eventClass.newInstance(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); + } catch (IllegalAccessException | InstantiationException e) { + log.error("Unhandled exception", e); } } event.parseHeader(byteBuf); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/events/EventLocalInsightUpdateGUI.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/events/EventLocalInsightUpdateGUI.kt new file mode 100644 index 0000000000..d89515fe56 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/events/EventLocalInsightUpdateGUI.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.pump.insight.events + +import info.nightscout.androidaps.events.EventUpdateGui + +class EventLocalInsightUpdateGUI : EventUpdateGui() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.kt index d60a639106..69e0e0bbe8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicFragment.kt @@ -8,7 +8,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment -import com.squareup.otto.Subscribe import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.events.EventExtendedBolusChange @@ -104,7 +103,6 @@ class MedtronicFragment : Fragment() { @Synchronized override fun onResume() { super.onResume() - MainApp.bus().register(this) loopHandler.postDelayed(refreshLoop, T.mins(1).msecs()) disposable += RxBus .toObservable(EventRefreshButtonState::class.java) @@ -122,6 +120,14 @@ class MedtronicFragment : Fragment() { .toObservable(EventMedtronicPumpValuesChanged::class.java) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) + disposable += RxBus + .toObservable(EventExtendedBolusChange::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) + disposable += RxBus + .toObservable(EventTempBasalChange::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) disposable += RxBus .toObservable(EventMedtronicPumpConfigurationChanged::class.java) .observeOn(AndroidSchedulers.mainThread()) @@ -131,6 +137,14 @@ class MedtronicFragment : Fragment() { MedtronicUtil.getPumpStatus().verifyConfiguration() updateGUI() }, { FabricPrivacy.logException(it) }) + disposable += RxBus + .toObservable(EventPumpStatusChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) + disposable += RxBus + .toObservable(EventQueueChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) updateGUI() } @@ -139,30 +153,9 @@ class MedtronicFragment : Fragment() { override fun onPause() { super.onPause() disposable.clear() - MainApp.bus().unregister(this) loopHandler.removeCallbacks(refreshLoop) } - @Subscribe - fun onStatusEvent(c: EventPumpStatusChanged) { - activity?.runOnUiThread { updateGUI() } - } - - @Subscribe - fun onStatusEvent(s: EventTempBasalChange) { - activity?.runOnUiThread { updateGUI() } - } - - @Subscribe - fun onStatusEvent(s: EventExtendedBolusChange) { - activity?.runOnUiThread { updateGUI() } - } - - @Subscribe - fun onStatusEvent(s: EventQueueChanged) { - activity?.runOnUiThread { updateGUI() } - } - @Synchronized private fun setDeviceStatus() { val pumpStatus: MedtronicPumpStatus = MedtronicUtil.getPumpStatus() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java index cd5066d645..f0d10e0370 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java @@ -48,6 +48,7 @@ import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperAc import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.plugins.pump.common.PumpPluginAbstract; +import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.pump.common.defs.PumpDriverState; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst; @@ -372,7 +373,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter refreshAnyStatusThatNeedsToBeRefreshed(); } - RxBus.INSTANCE.send(new EventMedtronicPumpValuesChanged()); + RxBus.INSTANCE.send(new EventMedtronicPumpValuesChanged()); } @@ -386,7 +387,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter RileyLinkServiceState rileyLinkServiceState = MedtronicUtil.getServiceState(); - if (rileyLinkServiceState==null) { + if (rileyLinkServiceState == null) { LOG.error("RileyLink unreachable. RileyLinkServiceState is null."); return false; } @@ -744,13 +745,13 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter ClockDTO clock = MedtronicUtil.getPumpTime(); - if (clock==null) { // retry + if (clock == null) { // retry medtronicUIComm.executeCommand(MedtronicCommandType.GetRealTimeClock); clock = MedtronicUtil.getPumpTime(); } - if (clock==null) + if (clock == null) return; int timeDiff = Math.abs(clock.timeDifference); @@ -866,6 +867,11 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter }).start(); } + long now = System.currentTimeMillis(); + + detailedBolusInfo.date = now; + detailedBolusInfo.deliverAt = now; // not sure about that one + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, true); // we subtract insulin, exact amount will be visible with next remainingInsulin update. @@ -877,7 +883,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter // calculate time for bolus and set driver to busy for that time int bolusTime = (int) (detailedBolusInfo.insulin * 42.0d); - long time = System.currentTimeMillis() + (bolusTime * 1000); + long time = now + (bolusTime * 1000); this.busyTimestamps.add(time); setEnableCustomAction(MedtronicCustomActionType.ClearBolusBlock, true); @@ -1065,10 +1071,10 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter @Override public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, Profile profile, boolean enforceNew) { - if (percent==0) { + if (percent == 0) { return setTempBasalAbsolute(0.0d, durationInMinutes, profile, enforceNew); } else { - double absoluteValue = profile.getBasal() * (percent /100.0d); + double absoluteValue = profile.getBasal() * (percent / 100.0d); getMDTPumpStatus(); absoluteValue = pumpStatusLocal.pumpType.determineCorrectBasalSize(absoluteValue); LOG.warn("setTempBasalPercent [MedtronicPumpPlugin] - You are trying to use setTempBasalPercent with percent other then 0% (%d). This will start setTempBasalAbsolute, with calculated value (%.3f). Result might not be 100% correct.", percent, absoluteValue); @@ -1080,7 +1086,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter private void finishAction(String overviewKey) { if (overviewKey != null) - MainApp.bus().post(new EventRefreshOverview(overviewKey)); + RxBus.INSTANCE.send(new EventRefreshOverview(overviewKey)); triggerUIChange(); @@ -1603,7 +1609,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter } private void refreshCustomActionsList() { - MainApp.bus().post(new EventCustomActionsChanged()); + RxBus.INSTANCE.send(new EventCustomActionsChanged()); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java index 2562ac9376..e054539e8f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.java @@ -788,7 +788,7 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager return false; } - if (responseRaw.length == 1) { + if (responseRaw.length < 2) { return false; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.java index 32ca320de2..71f387d176 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/pump/MedtronicPumpHistoryDecoder.java @@ -412,25 +412,18 @@ public class MedtronicPumpHistoryDecoder extends MedtronicHistoryDecoder validEntries; - // private Object validValues; - public PumpHistoryResult(PumpHistoryEntry searchEntry, Long targetDate) { if (searchEntry != null) { /* @@ -109,9 +105,8 @@ public class PumpHistoryResult { if (unprocessedEntry.isAfter(this.searchDate)) { this.validEntries.add(unprocessedEntry); } else { - LOG.debug("PE. PumpHistoryResult. Not after.. Unprocessed Entry [year={},entry={}]", - DateTimeUtil.getYear(unprocessedEntry.atechDateTime), unprocessedEntry); - +// LOG.debug("PE. PumpHistoryResult. Not after.. Unprocessed Entry [year={},entry={}]", +// DateTimeUtil.getYear(unprocessedEntry.atechDateTime), unprocessedEntry); if (DateTimeUtil.getYear(unprocessedEntry.atechDateTime) > 2015) olderEntries++; } @@ -131,14 +126,6 @@ public class PumpHistoryResult { } - private void clearOrPrepareList() { - if (this.validEntries == null) - this.validEntries = new ArrayList<>(); - else - this.validEntries.clear(); - } - - public String toString() { return "PumpHistoryResult [unprocessed=" + (unprocessedEntries != null ? "" + unprocessedEntries.size() : "0") + // ", valid=" + (validEntries != null ? "" + validEntries.size() : "0") + // diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java index 00cda7feca..2e23d2a955 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.java @@ -6,6 +6,8 @@ import com.google.gson.GsonBuilder; import org.apache.commons.lang3.StringUtils; import org.joda.time.LocalDateTime; import org.joda.time.Minutes; +import org.json.JSONException; +import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,6 +22,7 @@ import java.util.Map; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.DetailedBolusInfo; +import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.DbObjectBase; import info.nightscout.androidaps.db.ExtendedBolus; @@ -27,6 +30,7 @@ import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TDD; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil; import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil; @@ -45,7 +49,9 @@ import info.nightscout.androidaps.plugins.pump.medtronic.driver.MedtronicPumpSta import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicConst; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; import info.nightscout.androidaps.plugins.treatments.Treatment; +import info.nightscout.androidaps.plugins.treatments.TreatmentService; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; +import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.SP; @@ -59,9 +65,9 @@ import info.nightscout.androidaps.utils.SP; // all times that time changed (TZ, DST, etc.). Data needs to be returned in batches (time_changed batches, so that we can // handle it. It would help to assign sort_ids to items (from oldest (1) to newest (x) +// All things marked with "TODO: Fix db code" needs to be updated in new 2.5 database code public class MedtronicHistoryData { - private static final Logger LOG = LoggerFactory.getLogger(L.PUMP); private List allHistory = null; @@ -71,20 +77,33 @@ public class MedtronicHistoryData { private boolean isInit = false; private Gson gson; + private Gson gsonCore; private DatabaseHelper databaseHelper = MainApp.getDbHelper(); private ClockDTO pumpTime; private long lastIdUsed = 0; + /** + * Double bolus debug. We seem to have small problem with double Boluses (or sometimes also missing boluses + * from history. This flag turns on debugging for that (default is off=false)... Debuging is pretty detailed, + * so log files will get bigger. + */ + public static boolean doubleBolusDebug = false; + public MedtronicHistoryData() { this.allHistory = new ArrayList<>(); this.gson = MedtronicUtil.gsonInstance; + this.gsonCore = MedtronicUtil.getGsonInstanceCore(); if (this.gson == null) { this.gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); } + + if (this.gsonCore == null) { + this.gsonCore = new GsonBuilder().create(); + } } @@ -311,6 +330,7 @@ public class MedtronicHistoryData { pumpHistoryEntryType == PumpHistoryEntryType.BasalProfileStart || // pumpHistoryEntryType == PumpHistoryEntryType.Bolus || // pumpHistoryEntryType == PumpHistoryEntryType.Resume || // + pumpHistoryEntryType == PumpHistoryEntryType.BatteryChange || // pumpHistoryEntryType == PumpHistoryEntryType.Prime); if (isLogEnabled()) @@ -353,6 +373,7 @@ public class MedtronicHistoryData { PumpHistoryEntryType.Resume, // PumpHistoryEntryType.Rewind, // PumpHistoryEntryType.NoDeliveryAlarm, // + PumpHistoryEntryType.BatteryChange, // PumpHistoryEntryType.BasalProfileStart); newAndAll2 = filterPumpSuspend(newAndAll2, 10); @@ -382,6 +403,22 @@ public class MedtronicHistoryData { */ public void processNewHistoryData() { + // TODO: Fix db code + // Prime (for reseting autosense) + List primeRecords = getFilteredItems(PumpHistoryEntryType.Prime); + + if (isLogEnabled()) + LOG.debug("ProcessHistoryData: Prime [count={}, items={}]", primeRecords.size(), gson.toJson(primeRecords)); + + if (isCollectionNotEmpty(primeRecords)) { + try { + processPrime(primeRecords); + } catch (Exception ex) { + LOG.error("ProcessHistoryData: Error processing Prime entries: " + ex.getMessage(), ex); + throw ex; + } + } + // TDD List tdds = getFilteredItems(PumpHistoryEntryType.EndResultTotals, getTDDType()); @@ -454,6 +491,49 @@ public class MedtronicHistoryData { } + private void processPrime(List primeRecords) { + + long maxAllowedTimeInPast = DateTimeUtil.getATDWithAddedMinutes(new GregorianCalendar(), -30); + + long lastPrimeRecord = 0L; + + for (PumpHistoryEntry primeRecord : primeRecords) { + + if (primeRecord.atechDateTime > maxAllowedTimeInPast) { + if (lastPrimeRecord < primeRecord.atechDateTime) { + lastPrimeRecord = primeRecord.atechDateTime; + } + } + } + + if (lastPrimeRecord != 0L) { + long lastPrimeFromAAPS = SP.getLong(MedtronicConst.Statistics.LastPrime, 0L); + + if (lastPrimeRecord != lastPrimeFromAAPS) { + uploadCareportalEvent(DateTimeUtil.toMillisFromATD(lastPrimeRecord), CareportalEvent.SITECHANGE); + + SP.putLong(MedtronicConst.Statistics.LastPrime, lastPrimeRecord); + } + } + } + + + private void uploadCareportalEvent(long date, String event) { + if (MainApp.getDbHelper().getCareportalEventFromTimestamp(date) != null) + return; + try { + JSONObject data = new JSONObject(); + String enteredBy = SP.getString("careportal_enteredby", ""); + if (!enteredBy.equals("")) data.put("enteredBy", enteredBy); + data.put("created_at", DateUtil.toISOString(date)); + data.put("eventType", event); + NSUpload.uploadCareportalEntryToNS(data); + } catch (JSONException e) { + LOG.error("Unhandled exception", e); + } + } + + private void processTDDs(List tddsIn) { List tdds = filterTDDs(tddsIn); @@ -517,23 +597,32 @@ public class MedtronicHistoryData { long oldestTimestamp = getOldestTimestamp(entryList); + Gson gson = MedtronicUtil.getGsonInstance(); + List entriesFromHistory = getDatabaseEntriesByLastTimestamp(oldestTimestamp, ProcessHistoryRecord.Bolus); -// LOG.debug(processHistoryRecord.getDescription() + " List (before filter): {}, FromDb={}", gsonPretty.toJson(entryList), -// gsonPretty.toJson(entriesFromHistory)); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: List (before filter): {}, FromDb={}", gson.toJson(entryList), + gsonCore.toJson(entriesFromHistory)); filterOutAlreadyAddedEntries(entryList, entriesFromHistory); - if (entryList.isEmpty()) + if (entryList.isEmpty()) { + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: EntryList was filtered out."); return; + } -// LOG.debug(processHistoryRecord.getDescription() + " List (after filter): {}, FromDb={}", gsonPretty.toJson(entryList), -// gsonPretty.toJson(entriesFromHistory)); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: List (after filter): {}, FromDb={}", gson.toJson(entryList), + gsonCore.toJson(entriesFromHistory)); if (isCollectionEmpty(entriesFromHistory)) { for (PumpHistoryEntry treatment : entryList) { if (isLogEnabled()) LOG.debug("Add Bolus (no db entry): " + treatment); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: Add Bolus: FromDb=null, Treatment={}", treatment); addBolus(treatment, null); } @@ -542,6 +631,8 @@ public class MedtronicHistoryData { DbObjectBase treatmentDb = findDbEntry(treatment, entriesFromHistory); if (isLogEnabled()) LOG.debug("Add Bolus {} - (entryFromDb={}) ", treatment, treatmentDb); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: Add Bolus: FromDb={}, Treatment={}", treatmentDb, treatment); addBolus(treatment, (Treatment) treatmentDb); } @@ -681,12 +772,11 @@ public class MedtronicHistoryData { /** * findDbEntry - finds Db entries in database, while theoretically this should have same dateTime they - * don't. Entry on pump is few seconds before treatment in AAPS, and on manual boluses on pump there - * is no treatment at all. For now we look fro tratment that was from 0s - 1m59s within pump entry. + * don't. Entry on pump is few seconds before treatment in AAPS, and on manual boluses on pump there + * is no treatment at all. For now we look fro tratment that was from 0s - 1m59s within pump entry. * - * @param treatment Pump Entry + * @param treatment Pump Entry * @param entriesFromHistory entries from history - * * @return DbObject from AAPS (if found) */ private DbObjectBase findDbEntry(PumpHistoryEntry treatment, List entriesFromHistory) { @@ -695,9 +785,30 @@ public class MedtronicHistoryData { //proposedTime += (this.pumpTime.timeDifference * 1000); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: findDbEntry Treatment={}, FromDb={}", treatment, gson.toJson(entriesFromHistory)); + if (entriesFromHistory.size() == 0) { + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: findDbEntry Treatment={}, FromDb=null", treatment); return null; } else if (entriesFromHistory.size() == 1) { + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: findDbEntry Treatment={}, FromDb={}. Type=SingleEntry", treatment, entriesFromHistory.get(0)); + + // TODO: Fix db code + // if difference is bigger than 2 minutes we discard entry + long maxMillisAllowed = DateTimeUtil.getMillisFromATDWithAddedMinutes(treatment.atechDateTime, 2); + + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: findDbEntry maxMillisAllowed={}, AtechDateTime={} (add 2 minutes). ", maxMillisAllowed, treatment.atechDateTime); + + if (entriesFromHistory.get(0).getDate() > maxMillisAllowed) { + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: findDbEntry entry filtered out, returning null. "); + return null; + } + return entriesFromHistory.get(0); } @@ -720,10 +831,10 @@ public class MedtronicHistoryData { } } -// LOG.debug("Entries: (timeDiff=[min={},sec={}],count={},list={})", min, sec, outList.size(), -// gsonPretty.toJson(outList)); - if (outList.size() == 1) { + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: findDbEntry Treatment={}, FromDb={}. Type=EntrySelected, AtTimeMin={}, AtTimeSec={}", treatment, entriesFromHistory.get(0), min, sec); + return outList.get(0); } @@ -731,6 +842,9 @@ public class MedtronicHistoryData { if (isLogEnabled()) LOG.error("Too many entries (with too small diff): (timeDiff=[min={},sec={}],count={},list={})", min, sec, outList.size(), gson.toJson(outList)); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: findDbEntry Error - Too many entries (with too small diff): (timeDiff=[min={},sec={}],count={},list={})", + min, sec, outList.size(), gson.toJson(outList)); } } } @@ -754,6 +868,7 @@ public class MedtronicHistoryData { return; List removeTreatmentsFromHistory = new ArrayList<>(); + List removeTreatmentsFromPH = new ArrayList<>(); for (DbObjectBase treatment : treatmentsFromHistory) { @@ -771,11 +886,17 @@ public class MedtronicHistoryData { if (selectedBolus != null) { entryList.remove(selectedBolus); + removeTreatmentsFromPH.add(selectedBolus); removeTreatmentsFromHistory.add(treatment); } } } + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: filterOutAlreadyAddedEntries: PumpHistory={}, Treatments={}", + gson.toJson(removeTreatmentsFromPH), + gsonCore.toJson(removeTreatmentsFromHistory)); + treatmentsFromHistory.removeAll(removeTreatmentsFromHistory); } @@ -785,6 +906,8 @@ public class MedtronicHistoryData { BolusDTO bolusDTO = (BolusDTO) bolus.getDecodedData().get("Object"); if (treatment == null) { + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: addBolus(tretament==null): Bolus={}", bolusDTO); switch (bolusDTO.getBolusType()) { case Normal: { @@ -797,6 +920,9 @@ public class MedtronicHistoryData { addCarbsFromEstimate(detailedBolusInfo, bolus); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: addBolus(tretament==null): DetailedBolusInfo={}", detailedBolusInfo); + boolean newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); bolus.setLinkedObject(detailedBolusInfo); @@ -819,6 +945,9 @@ public class MedtronicHistoryData { bolus.setLinkedObject(extendedBolus); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: addBolus(tretament==null): ExtendedBolus={}", extendedBolus); + TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus); if (isLogEnabled()) @@ -831,26 +960,23 @@ public class MedtronicHistoryData { } else { - DetailedBolusInfo detailedBolusInfo = DetailedBolusInfoStorage.INSTANCE.findDetailedBolusInfo(treatment.date, bolusDTO.getDeliveredAmount()); - if (detailedBolusInfo == null) { - detailedBolusInfo = new DetailedBolusInfo(); - } + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: addBolus(OldTreatment={}): Bolus={}", treatment, bolusDTO); - detailedBolusInfo.date = treatment.date; - detailedBolusInfo.source = Source.PUMP; - detailedBolusInfo.pumpId = bolus.getPumpId(); - detailedBolusInfo.insulin = bolusDTO.getDeliveredAmount(); - detailedBolusInfo.carbs = treatment.carbs; + treatment.source = Source.PUMP; + treatment.pumpId = bolus.getPumpId(); + treatment.insulin = bolusDTO.getDeliveredAmount(); - addCarbsFromEstimate(detailedBolusInfo, bolus); + TreatmentService.UpdateReturn updateReturn = TreatmentsPlugin.getPlugin().getService().createOrUpdateMedtronic(treatment, false); - boolean newRecord = TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); - - bolus.setLinkedObject(detailedBolusInfo); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: addBolus(tretament!=null): NewTreatment={}, UpdateReturn={}", treatment, updateReturn); if (isLogEnabled()) - LOG.debug("editBolus - [date={},pumpId={}, insulin={}, newRecord={}]", detailedBolusInfo.date, - detailedBolusInfo.pumpId, detailedBolusInfo.insulin, newRecord); + LOG.debug("editBolus - [date={},pumpId={}, insulin={}, newRecord={}]", treatment.date, + treatment.pumpId, treatment.insulin, updateReturn.toString()); + + bolus.setLinkedObject(treatment); } } @@ -862,6 +988,9 @@ public class MedtronicHistoryData { BolusWizardDTO bolusWizard = (BolusWizardDTO) bolus.getDecodedData().get("Estimate"); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: addCarbsFromEstimate: Bolus={}, BolusWizardDTO={}", bolus, bolusWizard); + detailedBolusInfo.carbs = bolusWizard.carbs; } } @@ -1210,24 +1339,26 @@ public class MedtronicHistoryData { } } - //LocalDateTime oldestEntryTime = null; + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: getOldestTimestamp. Oldest entry found: time={}, object={}", dt, currentTreatment); try { GregorianCalendar oldestEntryTime = DateTimeUtil.toGregorianCalendar(dt); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: getOldestTimestamp. oldestEntryTime: {}", DateTimeUtil.toString(oldestEntryTime)); oldestEntryTime.add(Calendar.MINUTE, -2); + if (doubleBolusDebug) + LOG.debug("DoubleBolusDebug: getOldestTimestamp. oldestEntryTime (-2m): {}, timeInMillis={}", DateTimeUtil.toString(oldestEntryTime), oldestEntryTime.getTimeInMillis()); + return oldestEntryTime.getTimeInMillis(); -// if (this.pumpTime.timeDifference < 0) { -// oldestEntryTime = oldestEntryTime.plusSeconds(this.pumpTime.timeDifference); -// } } catch (Exception ex) { - LOG.error("Problem decoding date from last record: {}" + currentTreatment); + LOG.error("Problem decoding date from last record: {}", currentTreatment); return 8; // default return of 6 minutes } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/BatteryStatusDTO.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/BatteryStatusDTO.java index c8bd2347d0..93b5eb10cb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/BatteryStatusDTO.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/BatteryStatusDTO.java @@ -2,6 +2,8 @@ package info.nightscout.androidaps.plugins.pump.medtronic.data.dto; import com.google.gson.annotations.Expose; +import java.util.Locale; + import info.nightscout.androidaps.plugins.pump.medtronic.defs.BatteryType; /** @@ -38,10 +40,11 @@ public class BatteryStatusDTO { public String toString() { - return String.format("BatteryStatusDTO [voltage=%.2f, alkaline=%d, lithium=%d]", + return String.format(Locale.ENGLISH, "BatteryStatusDTO [voltage=%.2f, alkaline=%d, lithium=%d, niZn={}]", voltage == null ? 0.0f : voltage, getCalculatedPercent(BatteryType.Alkaline), - getCalculatedPercent(BatteryType.Lithium)); + getCalculatedPercent(BatteryType.Lithium), + getCalculatedPercent(BatteryType.NiZn)); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/defs/BatteryType.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/defs/BatteryType.java index 0d228d43ec..5c7bf245a2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/defs/BatteryType.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/defs/BatteryType.java @@ -14,7 +14,9 @@ public enum BatteryType { None(R.string.key_medtronic_pump_battery_no, 0, 0), Alkaline(R.string.key_medtronic_pump_battery_alkaline, 1.20d, 1.47d), // - Lithium(R.string.key_medtronic_pump_battery_lithium, 1.22d, 1.64d); + Lithium(R.string.key_medtronic_pump_battery_lithium, 1.22d, 1.64d), // + NiZn(R.string.key_medtronic_pump_battery_nizn, 1.40d, 1.70d) // + ; private final String description; public double lowVoltage; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/MedtronicHistoryActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/MedtronicHistoryActivity.java index 9f148ac02e..2d1a3a871e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/MedtronicHistoryActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/dialog/MedtronicHistoryActivity.java @@ -19,7 +19,6 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.activities.NoSplashActivity; import info.nightscout.androidaps.logging.L; @@ -82,7 +81,6 @@ public class MedtronicHistoryActivity extends NoSplashActivity { @Override protected void onResume() { super.onResume(); - MainApp.bus().register(this); filterHistory(selectedGroup); setHistoryTypeSpinner(); } @@ -106,7 +104,6 @@ public class MedtronicHistoryActivity extends NoSplashActivity { @Override protected void onPause() { super.onPause(); - MainApp.bus().unregister(this); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java index 29387b0724..fc1208a340 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/driver/MedtronicPumpStatus.java @@ -22,6 +22,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.Rile import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkTargetFrequency; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkError; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState; +import info.nightscout.androidaps.plugins.pump.medtronic.data.MedtronicHistoryData; import info.nightscout.androidaps.plugins.pump.medtronic.defs.BasalProfileStatus; import info.nightscout.androidaps.plugins.pump.medtronic.defs.BatteryType; import info.nightscout.androidaps.plugins.pump.medtronic.defs.MedtronicDeviceType; @@ -281,6 +282,12 @@ public class MedtronicPumpStatus extends PumpStatus { MedtronicUtil.setBatteryType(this.batteryType); } + String bolusDebugEnabled = SP.getString(MedtronicConst.Prefs.BolusDebugEnabled, null); + + boolean bolusDebug = bolusDebugEnabled != null && bolusDebugEnabled.equals(MainApp.gs(R.string.common_on)); + + MedtronicHistoryData.doubleBolusDebug = bolusDebug; + reconfigureService(); return true; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicConst.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicConst.java index b5bd2f1f64..fef95087de 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicConst.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicConst.java @@ -11,17 +11,6 @@ public class MedtronicConst { static final String Prefix = "AAPS.Medtronic."; public class Prefs { - -// public static final String PrefPrefix = "pref_medtronic_"; -// public static final String PumpSerial = PrefPrefix + "serial"; -// public static final String PumpType = PrefPrefix + "pump_type"; -// public static final String PumpFrequency = PrefPrefix + "frequency"; -// public static final String MaxBolus = PrefPrefix + "max_bolus"; -// public static final String MaxBasal = PrefPrefix + "max_basal"; -// public static final String BolusDelay = PrefPrefix + "bolus_delay"; -// public static final String Encoding = PrefPrefix + "encoding"; -// public static final String BatteryType = PrefPrefix + "battery_type"; - public static final int PumpSerial = R.string.key_medtronic_serial; public static final int PumpType = R.string.key_medtronic_pump_type; public static final int PumpFrequency = R.string.key_medtronic_frequency; @@ -30,6 +19,7 @@ public class MedtronicConst { public static final int BolusDelay = R.string.key_medtronic_bolus_delay; public static final int Encoding = R.string.key_medtronic_encoding; public static final int BatteryType = R.string.key_medtronic_battery_type; + public static final int BolusDebugEnabled = R.string.key_medtronic_bolus_debug; } public class Statistics { @@ -42,6 +32,7 @@ public class MedtronicConst { public static final String StandardBoluses = StatsPrefix + "std_boluses_delivered"; public static final String SMBBoluses = StatsPrefix + "smb_boluses_delivered"; public static final String LastPumpHistoryEntry = StatsPrefix + "pump_history_entry"; + public static final String LastPrime = StatsPrefix + "last_sent_prime"; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicUtil.java index 96bf0a5d44..2c7ee69c2f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/util/MedtronicUtil.java @@ -61,8 +61,7 @@ public class MedtronicUtil extends RileyLinkUtil { private static int doneBit = 1 << 7; private static ClockDTO pumpTime; public static Gson gsonInstance = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); - public static Gson gsonInstancePretty = new GsonBuilder().excludeFieldsWithoutExposeAnnotation() - .setPrettyPrinting().create(); + public static Gson gsonInstanceCore = new GsonBuilder().create(); private static BatteryType batteryType = BatteryType.None; @@ -70,8 +69,9 @@ public class MedtronicUtil extends RileyLinkUtil { return gsonInstance; } - public static Gson getGsonInstancePretty() { - return gsonInstancePretty; + + public static Gson getGsonInstanceCore() { + return gsonInstanceCore; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpFragment.java deleted file mode 100644 index be9bf17c85..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpFragment.java +++ /dev/null @@ -1,125 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.virtual; - - -import android.app.Activity; -import android.os.Bundle; -import android.os.Handler; -import androidx.annotation.Nullable; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import com.squareup.otto.Subscribe; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.db.ExtendedBolus; -import info.nightscout.androidaps.db.TemporaryBasal; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; -import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; -import info.nightscout.androidaps.plugins.pump.virtual.events.EventVirtualPumpUpdateGui; -import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.utils.FabricPrivacy; - - -public class VirtualPumpFragment extends SubscriberFragment { - private static Logger log = LoggerFactory.getLogger(VirtualPumpFragment.class); - - TextView basaBasalRateView; - TextView tempBasalView; - TextView extendedBolusView; - TextView batteryView; - TextView reservoirView; - TextView pumpTypeView; - TextView pumpSettingsView; - - - private static Handler sLoopHandler = new Handler(); - private static Runnable sRefreshLoop = null; - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - if (sRefreshLoop == null) { - sRefreshLoop = new Runnable() { - @Override - public void run() { - updateGUI(); - sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L); - } - }; - sLoopHandler.postDelayed(sRefreshLoop, 60 * 1000L); - } - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - try { - View view = inflater.inflate(R.layout.virtualpump_fragment, container, false); - basaBasalRateView = (TextView) view.findViewById(R.id.virtualpump_basabasalrate); - tempBasalView = (TextView) view.findViewById(R.id.virtualpump_tempbasal); - extendedBolusView = (TextView) view.findViewById(R.id.virtualpump_extendedbolus); - batteryView = (TextView) view.findViewById(R.id.virtualpump_battery); - reservoirView = (TextView) view.findViewById(R.id.virtualpump_reservoir); - pumpTypeView = (TextView) view.findViewById(R.id.virtualpump_type); - pumpSettingsView = (TextView) view.findViewById(R.id.virtualpump_type_def); - - return view; - } catch (Exception e) { - FabricPrivacy.logException(e); - } - - return null; - } - - @Subscribe - public void onStatusEvent(final EventVirtualPumpUpdateGui ev) { - updateGUI(); - } - - @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null && basaBasalRateView != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - VirtualPumpPlugin virtualPump = VirtualPumpPlugin.getPlugin(); - basaBasalRateView.setText(virtualPump.getBaseBasalRate() + "U"); - TemporaryBasal activeTemp = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(System.currentTimeMillis()); - if (activeTemp != null) { - tempBasalView.setText(activeTemp.toStringFull()); - } else { - tempBasalView.setText(""); - } - ExtendedBolus activeExtendedBolus = TreatmentsPlugin.getPlugin().getExtendedBolusFromHistory(System.currentTimeMillis()); - if (activeExtendedBolus != null) { - extendedBolusView.setText(activeExtendedBolus.toString()); - } else { - extendedBolusView.setText(""); - } - batteryView.setText(virtualPump.batteryPercent + "%"); - reservoirView.setText(virtualPump.reservoirInUnits + "U"); - - virtualPump.refreshConfiguration(); - - PumpType pumpType = virtualPump.getPumpType(); - - pumpTypeView.setText(pumpType.getDescription()); - - String template = MainApp.gs(R.string.virtualpump_pump_def); - - - pumpSettingsView.setText(pumpType.getFullDescription(template, pumpType.hasExtendedBasals())); - - } - }); - } - - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpFragment.kt new file mode 100644 index 0000000000..08402520c2 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpFragment.kt @@ -0,0 +1,85 @@ +package info.nightscout.androidaps.plugins.pump.virtual + +import android.os.Bundle +import android.os.Handler +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.events.EventExtendedBolusChange +import info.nightscout.androidaps.events.EventTempBasalChange +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.pump.virtual.events.EventVirtualPumpUpdateGui +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.utils.FabricPrivacy +import info.nightscout.androidaps.utils.T +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.CompositeDisposable +import kotlinx.android.synthetic.main.virtualpump_fragment.* +import org.slf4j.LoggerFactory + +class VirtualPumpFragment : Fragment() { + private val disposable = CompositeDisposable() + + private val loopHandler = Handler() + private lateinit var refreshLoop: Runnable + + init { + refreshLoop = Runnable { + activity?.runOnUiThread { updateGui() } + loopHandler.postDelayed(refreshLoop, T.mins(1).msecs()) + } + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + return inflater.inflate(R.layout.virtualpump_fragment, container, false) + } + + @Synchronized + override fun onResume() { + super.onResume() + disposable.add(RxBus + .toObservable(EventVirtualPumpUpdateGui::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) + ) + disposable.add(RxBus + .toObservable(EventTempBasalChange::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) + ) + disposable.add(RxBus + .toObservable(EventExtendedBolusChange::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) + ) + loopHandler.postDelayed(refreshLoop, T.mins(1).msecs()) + updateGui() + } + + @Synchronized + override fun onPause() { + super.onPause() + disposable.clear() + loopHandler.removeCallbacks(refreshLoop) + } + + @Synchronized + private fun updateGui() { + val virtualPump = VirtualPumpPlugin.getPlugin() + virtualpump_basabasalrate?.text = MainApp.gs(R.string.pump_basebasalrate, virtualPump.baseBasalRate) + virtualpump_tempbasal?.text = TreatmentsPlugin.getPlugin().getTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() + ?: "" + virtualpump_extendedbolus?.text = TreatmentsPlugin.getPlugin().getExtendedBolusFromHistory(System.currentTimeMillis())?.toString() ?: "" + virtualpump_battery?.text = MainApp.gs(R.string.format_percent, virtualPump.batteryPercent) + virtualpump_reservoir?.text = MainApp.gs(R.string.formatinsulinunits, virtualPump.reservoirInUnits.toDouble()) + + virtualPump.refreshConfiguration() + val pumpType = virtualPump.pumpType + + virtualpump_type?.text = pumpType.description + virtualpump_type_def?.text = pumpType.getFullDescription(MainApp.gs(R.string.virtualpump_pump_def), pumpType.hasExtendedBasals()) + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpPlugin.java index 514ee912ad..447367c510 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/VirtualPumpPlugin.java @@ -2,8 +2,6 @@ package info.nightscout.androidaps.plugins.pump.virtual; import android.os.SystemClock; -import com.squareup.otto.Subscribe; - import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; @@ -41,8 +39,11 @@ import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; import info.nightscout.androidaps.plugins.pump.virtual.events.EventVirtualPumpUpdateGui; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.InstanceId; import info.nightscout.androidaps.utils.SP; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** @@ -50,6 +51,7 @@ import info.nightscout.androidaps.utils.SP; */ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { private Logger log = LoggerFactory.getLogger(L.PUMP); + private CompositeDisposable disposable = new CompositeDisposable(); Integer batteryPercent = 50; Integer reservoirInUnits = 50; @@ -122,22 +124,23 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { @Override protected void onStart() { super.onStart(); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventPreferenceChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (event.isChanged(R.string.key_virtualpump_type)) + refreshConfiguration(); + }, FabricPrivacy::logException) + ); refreshConfiguration(); } @Override protected void onStop() { - MainApp.bus().unregister(this); + disposable.clear(); super.onStop(); } - @Subscribe - public void onStatusEvent(final EventPreferenceChange s) { - if (s.isChanged(R.string.key_virtualpump_type)) - refreshConfiguration(); - } - @Override public boolean isFakingTempsByExtendedBoluses() { return (Config.NSCLIENT) && fromNSAreCommingFakedExtendedBoluses; @@ -268,21 +271,21 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { while (delivering < detailedBolusInfo.insulin) { SystemClock.sleep(200); - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.status = String.format(MainApp.gs(R.string.bolusdelivering), delivering); - bolusingEvent.percent = Math.min((int) (delivering / detailedBolusInfo.insulin * 100), 100); - MainApp.bus().post(bolusingEvent); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setStatus(String.format(MainApp.gs(R.string.bolusdelivering), delivering)); + bolusingEvent.setPercent(Math.min((int) (delivering / detailedBolusInfo.insulin * 100), 100)); + RxBus.INSTANCE.send(bolusingEvent); delivering += 0.1d; } SystemClock.sleep(200); - EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.getInstance(); - bolusingEvent.status = String.format(MainApp.gs(R.string.bolusdelivered), detailedBolusInfo.insulin); - bolusingEvent.percent = 100; - MainApp.bus().post(bolusingEvent); + EventOverviewBolusProgress bolusingEvent = EventOverviewBolusProgress.INSTANCE; + bolusingEvent.setStatus(String.format(MainApp.gs(R.string.bolusdelivered), detailedBolusInfo.insulin)); + bolusingEvent.setPercent(100); + RxBus.INSTANCE.send(bolusingEvent); SystemClock.sleep(1000); if (L.isEnabled(L.PUMPCOMM)) log.debug("Delivering treatment insulin: " + detailedBolusInfo.insulin + "U carbs: " + detailedBolusInfo.carbs + "g " + result); - MainApp.bus().post(new EventVirtualPumpUpdateGui()); + RxBus.INSTANCE.send(new EventVirtualPumpUpdateGui()); lastDataTime = System.currentTimeMillis(); TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); return result; @@ -310,7 +313,7 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { TreatmentsPlugin.getPlugin().addToHistoryTempBasal(tempBasal); if (L.isEnabled(L.PUMPCOMM)) log.debug("Setting temp basal absolute: " + result); - MainApp.bus().post(new EventVirtualPumpUpdateGui()); + RxBus.INSTANCE.send(new EventVirtualPumpUpdateGui()); lastDataTime = System.currentTimeMillis(); return result; } @@ -333,7 +336,7 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { TreatmentsPlugin.getPlugin().addToHistoryTempBasal(tempBasal); if (L.isEnabled(L.PUMPCOMM)) log.debug("Settings temp basal percent: " + result); - MainApp.bus().post(new EventVirtualPumpUpdateGui()); + RxBus.INSTANCE.send(new EventVirtualPumpUpdateGui()); lastDataTime = System.currentTimeMillis(); return result; } @@ -358,7 +361,7 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { TreatmentsPlugin.getPlugin().addToHistoryExtendedBolus(extendedBolus); if (L.isEnabled(L.PUMPCOMM)) log.debug("Setting extended bolus: " + result); - MainApp.bus().post(new EventVirtualPumpUpdateGui()); + RxBus.INSTANCE.send(new EventVirtualPumpUpdateGui()); lastDataTime = System.currentTimeMillis(); return result; } @@ -376,7 +379,7 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { //tempBasal = null; if (L.isEnabled(L.PUMPCOMM)) log.debug("Canceling temp basal: " + result); - MainApp.bus().post(new EventVirtualPumpUpdateGui()); + RxBus.INSTANCE.send(new EventVirtualPumpUpdateGui()); } lastDataTime = System.currentTimeMillis(); return result; @@ -396,7 +399,7 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { result.comment = MainApp.gs(R.string.virtualpump_resultok); if (L.isEnabled(L.PUMPCOMM)) log.debug("Canceling extended bolus: " + result); - MainApp.bus().post(new EventVirtualPumpUpdateGui()); + RxBus.INSTANCE.send(new EventVirtualPumpUpdateGui()); lastDataTime = System.currentTimeMillis(); return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/events/EventVirtualPumpUpdateGui.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/events/EventVirtualPumpUpdateGui.java deleted file mode 100644 index 795134997a..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/events/EventVirtualPumpUpdateGui.java +++ /dev/null @@ -1,9 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.virtual.events; - -import info.nightscout.androidaps.events.EventUpdateGui; - -/** - * Created by mike on 05.08.2016. - */ -public class EventVirtualPumpUpdateGui extends EventUpdateGui { -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/events/EventVirtualPumpUpdateGui.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/events/EventVirtualPumpUpdateGui.kt new file mode 100644 index 0000000000..a0498aa14d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/virtual/events/EventVirtualPumpUpdateGui.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.pump.virtual.events + +import info.nightscout.androidaps.events.EventUpdateGui + +class EventVirtualPumpUpdateGui : EventUpdateGui() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.java index 269ee2b3f5..6023d70d12 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.java @@ -1,19 +1,18 @@ package info.nightscout.androidaps.plugins.source; -import android.app.Activity; import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import java.util.List; @@ -21,19 +20,22 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.BgReading; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.T; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; /** * Created by mike on 16.10.2017. */ -public class BGSourceFragment extends SubscriberFragment { +public class BGSourceFragment extends Fragment { + private CompositeDisposable disposable = new CompositeDisposable(); RecyclerView recyclerView; String units = Constants.MGDL; @@ -66,19 +68,25 @@ public class BGSourceFragment extends SubscriberFragment { return null; } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished unused) { - updateGUI(); + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI(), FabricPrivacy::logException) + ); } @Override + public synchronized void onPause() { + disposable.clear(); + super.onPause(); + } + protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - long now = System.currentTimeMillis(); - recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getAllBgreadingsDataFromTime(now - MILLS_TO_THE_PAST, false)), true); - }); + long now = System.currentTimeMillis(); + recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getAllBgreadingsDataFromTime(now - MILLS_TO_THE_PAST, false)), true); } public class RecyclerViewAdapter extends RecyclerView.Adapter { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/SourceDexcomPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/source/SourceDexcomPlugin.kt index 1cf438b14d..633f16cf10 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/SourceDexcomPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/SourceDexcomPlugin.kt @@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.source import android.content.Intent import android.content.pm.PackageManager -import android.os.Build import androidx.core.content.ContextCompat import info.nightscout.androidaps.Constants import info.nightscout.androidaps.MainApp @@ -18,6 +17,7 @@ import info.nightscout.androidaps.logging.L import info.nightscout.androidaps.plugins.general.nsclient.NSUpload import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.SP +import info.nightscout.androidaps.utils.T import org.json.JSONObject import org.slf4j.LoggerFactory @@ -52,7 +52,7 @@ object SourceDexcomPlugin : PluginBase(PluginDescription() } fun findDexcomPackageName(): String? { - val packageManager = MainApp.instance().packageManager; + val packageManager = MainApp.instance().packageManager for (packageInfo in packageManager.getInstalledPackages(0)) { if (PACKAGE_NAMES.contains(packageInfo.packageName)) return packageInfo.packageName } @@ -62,45 +62,56 @@ object SourceDexcomPlugin : PluginBase(PluginDescription() override fun handleNewData(intent: Intent) { if (!isEnabled(PluginType.BGSOURCE)) return try { + val sensorType = intent.getStringExtra("sensorType") ?: "" val glucoseValues = intent.getBundleExtra("glucoseValues") for (i in 0 until glucoseValues.size()) { - val glucoseValue = glucoseValues.getBundle(i.toString()) - val bgReading = BgReading() - bgReading.value = glucoseValue!!.getInt("glucoseValue").toDouble() - bgReading.direction = glucoseValue.getString("trendArrow") - bgReading.date = glucoseValue.getLong("timestamp") * 1000 - bgReading.raw = 0.0 - if (MainApp.getDbHelper().createIfNotExists(bgReading, "Dexcom")) { - if (SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { - NSUpload.uploadBg(bgReading, "AndroidAPS-DexcomG6") - } - if (SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { - NSUpload.sendToXdrip(bgReading) + glucoseValues.getBundle(i.toString())?.let { glucoseValue -> + val bgReading = BgReading() + bgReading.value = glucoseValue.getInt("glucoseValue").toDouble() + bgReading.direction = glucoseValue.getString("trendArrow") + bgReading.date = glucoseValue.getLong("timestamp") * 1000 + bgReading.raw = 0.0 + if (MainApp.getDbHelper().createIfNotExists(bgReading, "Dexcom$sensorType")) { + if (SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { + NSUpload.uploadBg(bgReading, "AndroidAPS-Dexcom$sensorType") + } + if (SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { + NSUpload.sendToXdrip(bgReading) + } } } } val meters = intent.getBundleExtra("meters") for (i in 0 until meters.size()) { val meter = meters.getBundle(i.toString()) - val timestamp = meter!!.getLong("timestamp") * 1000 - if (MainApp.getDbHelper().getCareportalEventFromTimestamp(timestamp) != null) continue - val jsonObject = JSONObject() - jsonObject.put("enteredBy", "AndroidAPS-Dexcom") - jsonObject.put("created_at", DateUtil.toISOString(timestamp)) - jsonObject.put("eventType", CareportalEvent.BGCHECK) - jsonObject.put("glucoseType", "Finger") - jsonObject.put("glucose", meter.getInt("meterValue")) - jsonObject.put("units", Constants.MGDL) - NSUpload.uploadCareportalEntryToNS(jsonObject) + meter?.let { + val timestamp = it.getLong("timestamp") * 1000 + val now = DateUtil.now() + if (timestamp > now - T.months(1).msecs() && timestamp < now) + if (MainApp.getDbHelper().getCareportalEventFromTimestamp(timestamp) == null) { + val jsonObject = JSONObject() + jsonObject.put("enteredBy", "AndroidAPS-Dexcom$sensorType") + jsonObject.put("created_at", DateUtil.toISOString(timestamp)) + jsonObject.put("eventType", CareportalEvent.BGCHECK) + jsonObject.put("glucoseType", "Finger") + jsonObject.put("glucose", meter.getInt("meterValue")) + jsonObject.put("units", Constants.MGDL) + NSUpload.uploadCareportalEntryToNS(jsonObject) + } + } } if (SP.getBoolean(R.string.key_dexcom_lognssensorchange, false) && intent.hasExtra("sensorInsertionTime")) { - val sensorInsertionTime = intent.extras!!.getLong("sensorInsertionTime") * 1000 - if (MainApp.getDbHelper().getCareportalEventFromTimestamp(sensorInsertionTime) == null) { - val jsonObject = JSONObject() - jsonObject.put("enteredBy", "AndroidAPS-Dexcom") - jsonObject.put("created_at", DateUtil.toISOString(sensorInsertionTime)) - jsonObject.put("eventType", CareportalEvent.SENSORCHANGE) - NSUpload.uploadCareportalEntryToNS(jsonObject) + intent.extras?.let { + val sensorInsertionTime = it.getLong("sensorInsertionTime") * 1000 + val now = DateUtil.now() + if (sensorInsertionTime > now - T.months(1).msecs() && sensorInsertionTime < now) + if (MainApp.getDbHelper().getCareportalEventFromTimestamp(sensorInsertionTime) == null) { + val jsonObject = JSONObject() + jsonObject.put("enteredBy", "AndroidAPS-Dexcom$sensorType") + jsonObject.put("created_at", DateUtil.toISOString(sensorInsertionTime)) + jsonObject.put("eventType", CareportalEvent.SENSORCHANGE) + NSUpload.uploadCareportalEntryToNS(jsonObject) + } } } } catch (e: Exception) { 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 309191fdc0..1331c8eedf 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 @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.treatments; import android.content.Intent; import android.os.IBinder; + import androidx.annotation.Nullable; import com.j256.ormlite.android.apptools.OpenHelperManager; @@ -13,7 +14,6 @@ import com.j256.ormlite.stmt.QueryBuilder; import com.j256.ormlite.stmt.Where; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; -import com.squareup.otto.Subscribe; import org.apache.commons.lang3.StringUtils; import org.json.JSONException; @@ -29,7 +29,6 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.ICallback; import info.nightscout.androidaps.db.Source; @@ -38,9 +37,14 @@ import info.nightscout.androidaps.events.EventNsTreatment; import info.nightscout.androidaps.events.EventReloadTreatmentData; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData; +import info.nightscout.androidaps.plugins.pump.medtronic.data.MedtronicHistoryData; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.JsonHelper; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** @@ -49,6 +53,7 @@ import info.nightscout.androidaps.utils.JsonHelper; public class TreatmentService extends OrmLiteBaseService { private static Logger log = LoggerFactory.getLogger(L.DATATREATMENTS); + private CompositeDisposable disposable = new CompositeDisposable(); private static final ScheduledExecutorService treatmentEventWorker = Executors.newSingleThreadScheduledExecutor(); private static ScheduledFuture scheduledTreatmentEventPost = null; @@ -56,7 +61,20 @@ public class TreatmentService extends OrmLiteBaseService { public TreatmentService() { onCreate(); dbInitialize(); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventNsTreatment.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + int mode = event.getMode(); + JSONObject payload = event.getPayload(); + + if (mode == EventNsTreatment.Companion.getADD() || mode == EventNsTreatment.Companion.getUPDATE()) { + this.createTreatmentFromJsonIfNotExists(payload); + } else { // EventNsTreatment.REMOVE + this.deleteNS(payload); + } + }, FabricPrivacy::logException) + ); } /** @@ -87,19 +105,6 @@ public class TreatmentService extends OrmLiteBaseService { return null; } - @Subscribe - @SuppressWarnings("unused") - public void handleNsEvent(EventNsTreatment event) { - int mode = event.getMode(); - JSONObject payload = event.getPayload(); - - if (mode == EventNsTreatment.ADD || mode == EventNsTreatment.UPDATE) { - this.createTreatmentFromJsonIfNotExists(payload); - } else { // EventNsTreatment.REMOVE - this.deleteNS(payload); - } - } - @Override public void onCreate() { super.onCreate(); @@ -128,7 +133,7 @@ public class TreatmentService extends OrmLiteBaseService { try { getDao().executeRaw("ALTER TABLE `" + Treatment.TABLE_TREATMENTS + "` ADD COLUMN boluscalc STRING;"); } catch (SQLException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } } else { if (L.isEnabled(L.DATATREATMENTS)) @@ -142,7 +147,7 @@ public class TreatmentService extends OrmLiteBaseService { try { getDao().executeRaw("ALTER TABLE `" + Treatment.TABLE_TREATMENTS + "` DROP COLUMN boluscalc STRING;"); } catch (SQLException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } } } @@ -180,11 +185,11 @@ public class TreatmentService extends OrmLiteBaseService { public void run() { if (L.isEnabled(L.DATATREATMENTS)) log.debug("Firing EventReloadTreatmentData"); - MainApp.bus().post(event); + RxBus.INSTANCE.send(event); if (DatabaseHelper.earliestDataChange != null) { if (L.isEnabled(L.DATATREATMENTS)) log.debug("Firing EventNewHistoryData"); - MainApp.bus().post(new EventNewHistoryData(DatabaseHelper.earliestDataChange)); + RxBus.INSTANCE.send(new EventNewHistoryData(DatabaseHelper.earliestDataChange)); } DatabaseHelper.earliestDataChange = null; callback.setPost(null); @@ -192,7 +197,7 @@ public class TreatmentService extends OrmLiteBaseService { } // prepare task for execution in 1 sec // cancel waiting task to prevent sending multiple posts - ScheduledFuture scheduledFuture = callback.getPost(); + ScheduledFuture scheduledFuture = callback.getPost(); if (scheduledFuture != null) scheduledFuture.cancel(false); Runnable task = new PostRunnable(); @@ -245,12 +250,15 @@ public class TreatmentService extends OrmLiteBaseService { try { Treatment treatment = Treatment.createFromJson(json); if (treatment != null) { + + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: createTreatmentFromJsonIfNotExists:: medtronicPump={}", MedtronicUtil.isMedtronicPump()); + if (!MedtronicUtil.isMedtronicPump()) createOrUpdate(treatment); else - createOrUpdateMedtronic(treatment, false); - } - else + createOrUpdateMedtronic(treatment, true); + } else log.error("Date is null: " + treatment.toString()); } catch (JSONException e) { log.error("Unhandled exception", e); @@ -389,12 +397,19 @@ public class TreatmentService extends OrmLiteBaseService { public UpdateReturn createOrUpdateMedtronic(Treatment treatment, boolean fromNightScout) { + + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: createOrUpdateMedtronic:: originalTreatment={}, fromNightScout={}", treatment, fromNightScout); + try { treatment.date = DatabaseHelper.roundDateToSec(treatment.date); Treatment existingTreatment = getRecord(treatment.pumpId, treatment.date); - if (existingTreatment==null) { + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: createOrUpdateMedtronic:: existingTreatment={}", treatment); + + if (existingTreatment == null) { getDao().create(treatment); if (L.isEnabled(L.DATATREATMENTS)) log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); @@ -403,7 +418,10 @@ public class TreatmentService extends OrmLiteBaseService { return new UpdateReturn(true, true); } else { - if (existingTreatment.date==treatment.date) { + if (existingTreatment.date == treatment.date) { + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: createOrUpdateMedtronic::(existingTreatment.date==treatment.date)"); + // we will do update only, if entry changed if (!optionalTreatmentCopy(existingTreatment, treatment, fromNightScout)) { return new UpdateReturn(true, false); @@ -413,6 +431,9 @@ public class TreatmentService extends OrmLiteBaseService { scheduleTreatmentChange(treatment); return new UpdateReturn(true, false); } else { + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: createOrUpdateMedtronic::(existingTreatment.date != treatment.date)"); + // date is different, we need to remove entry getDao().delete(existingTreatment); optionalTreatmentCopy(existingTreatment, treatment, fromNightScout); @@ -476,15 +497,15 @@ public class TreatmentService extends OrmLiteBaseService { int source = Source.NONE; - if (oldTreatment.pumpId==0) { + if (oldTreatment.pumpId == 0) { if (newTreatment.pumpId > 0) { - oldTreatment.pumpId=newTreatment.pumpId; + oldTreatment.pumpId = newTreatment.pumpId; source = Source.PUMP; changed = true; } } - if (source==Source.NONE) { + if (source == Source.NONE) { if (oldTreatment.source == newTreatment.source) { source = oldTreatment.source; @@ -515,7 +536,6 @@ public class TreatmentService extends OrmLiteBaseService { log.debug("treatmentCopy [old={}, new={}]", oldTreatment.toString(), newTreatment.toString()); - if (fromNightScout) { long pumpId_old = oldTreatment.pumpId; boolean isSMB = (oldTreatment.isSMB || newTreatment.isSMB); @@ -545,7 +565,7 @@ public class TreatmentService extends OrmLiteBaseService { Treatment record = null; - if (pumpId>0) { + if (pumpId > 0) { record = getPumpRecordById(pumpId); @@ -565,7 +585,6 @@ public class TreatmentService extends OrmLiteBaseService { } - /** * Returns the record for the given id, null if none, throws RuntimeException * if multiple records with the same pump id exist. @@ -686,7 +705,7 @@ public class TreatmentService extends OrmLiteBaseService { return new ArrayList<>(); } - public List getTreatmentDataFromTime(long from, long to, boolean ascending) { + public List getTreatmentDataFromTime(long from, long to, boolean ascending) { try { Dao daoTreatments = getDao(); List treatments; @@ -717,6 +736,14 @@ public class TreatmentService extends OrmLiteBaseService { boolean newRecord; boolean success; + + @Override + public String toString() { + return "UpdateReturn [" + + "newRecord=" + newRecord + + ", success=" + success + + ']'; + } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsFragment.java index 192952057d..731042c237 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsFragment.java @@ -1,19 +1,18 @@ package info.nightscout.androidaps.plugins.treatments; import android.os.Bundle; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentTransaction; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventExtendedBolusChange; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsBolusFragment; import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsCareportalFragment; @@ -22,8 +21,12 @@ import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsProfile import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsTempTargetFragment; import info.nightscout.androidaps.plugins.treatments.fragments.TreatmentsTemporaryBasalsFragment; import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; + +public class TreatmentsFragment extends Fragment implements View.OnClickListener { + private CompositeDisposable disposable = new CompositeDisposable(); -public class TreatmentsFragment extends SubscriberFragment implements View.OnClickListener { TextView treatmentsTab; TextView extendedBolusesTab; TextView tempBasalsTab; @@ -34,32 +37,42 @@ public class TreatmentsFragment extends SubscriberFragment implements View.OnCli @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - try { - View view = inflater.inflate(R.layout.treatments_fragment, container, false); + View view = inflater.inflate(R.layout.treatments_fragment, container, false); - treatmentsTab = (TextView) view.findViewById(R.id.treatments_treatments); - extendedBolusesTab = (TextView) view.findViewById(R.id.treatments_extendedboluses); - tempBasalsTab = (TextView) view.findViewById(R.id.treatments_tempbasals); - tempTargetTab = (TextView) view.findViewById(R.id.treatments_temptargets); - profileSwitchTab = (TextView) view.findViewById(R.id.treatments_profileswitches); - careportalTab = (TextView) view.findViewById(R.id.treatments_careportal); - treatmentsTab.setOnClickListener(this); - extendedBolusesTab.setOnClickListener(this); - tempBasalsTab.setOnClickListener(this); - tempTargetTab.setOnClickListener(this); - profileSwitchTab.setOnClickListener(this); - careportalTab.setOnClickListener(this); + treatmentsTab = (TextView) view.findViewById(R.id.treatments_treatments); + extendedBolusesTab = (TextView) view.findViewById(R.id.treatments_extendedboluses); + tempBasalsTab = (TextView) view.findViewById(R.id.treatments_tempbasals); + tempTargetTab = (TextView) view.findViewById(R.id.treatments_temptargets); + profileSwitchTab = (TextView) view.findViewById(R.id.treatments_profileswitches); + careportalTab = (TextView) view.findViewById(R.id.treatments_careportal); + treatmentsTab.setOnClickListener(this); + extendedBolusesTab.setOnClickListener(this); + tempBasalsTab.setOnClickListener(this); + tempTargetTab.setOnClickListener(this); + profileSwitchTab.setOnClickListener(this); + careportalTab.setOnClickListener(this); - setFragment(new TreatmentsBolusFragment()); - setBackgroundColorOnSelected(treatmentsTab); + setFragment(new TreatmentsBolusFragment()); + setBackgroundColorOnSelected(treatmentsTab); - return view; - } catch (Exception e) { - FabricPrivacy.logException(e); - } + return view; + } - return null; + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventExtendedBolusChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + updateGui(); + } + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); } @Override @@ -111,13 +124,7 @@ public class TreatmentsFragment extends SubscriberFragment implements View.OnCli selected.setBackgroundColor(MainApp.gc(R.color.tabBgColorSelected)); } - @Subscribe - public void onStatusEvent(final EventExtendedBolusChange ev) { - updateGUI(); - } - - @Override - protected void updateGUI() { + private void updateGui() { if (ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription().isExtendedBolusCapable || TreatmentsPlugin.getPlugin().getExtendedBolusesFromHistory().size() > 0) { extendedBolusesTab.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java index 195005e87a..004f44858d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentsPlugin.java @@ -6,7 +6,6 @@ import android.os.Bundle; import androidx.annotation.Nullable; import com.google.firebase.analytics.FirebaseAnalytics; -import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,6 +51,7 @@ import info.nightscout.androidaps.plugins.general.overview.notifications.Notific import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; +import info.nightscout.androidaps.plugins.pump.medtronic.data.MedtronicHistoryData; import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil; import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin; @@ -59,6 +59,8 @@ import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; /** * Created by mike on 05.08.2016. @@ -66,6 +68,8 @@ import info.nightscout.androidaps.utils.T; public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface { private Logger log = LoggerFactory.getLogger(L.DATATREATMENTS); + private CompositeDisposable disposable = new CompositeDisposable(); + private static TreatmentsPlugin treatmentsPlugin; public static TreatmentsPlugin getPlugin() { @@ -99,18 +103,53 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface @Override protected void onStart() { - MainApp.bus().register(this); initializeTempBasalData(); initializeTreatmentData(); initializeExtendedBolusData(); initializeTempTargetData(); initializeProfileSwitchData(); super.onStart(); + disposable.add(RxBus.INSTANCE + .toObservable(EventReloadTreatmentData.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (L.isEnabled(L.DATATREATMENTS)) + log.debug("EventReloadTreatmentData"); + initializeTreatmentData(); + initializeExtendedBolusData(); + updateTotalIOBTreatments(); + RxBus.INSTANCE.send(event.getNext()); + }, + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventReloadProfileSwitchData.class) + .observeOn(Schedulers.io()) + .subscribe(event -> initializeProfileSwitchData(), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventTempTargetChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> initializeTempTargetData(), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventReloadTempBasalData.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (L.isEnabled(L.DATATREATMENTS)) + log.debug("EventReloadTempBasalData"); + initializeTempBasalData(); + updateTotalIOBTempBasals(); + }, + FabricPrivacy::logException + )); } @Override protected void onStop() { - MainApp.bus().register(this); + disposable.clear(); super.onStop(); } @@ -306,12 +345,20 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface @Override public List getTreatmentsFromHistoryAfterTimestamp(long fromTimestamp) { List in5minback = new ArrayList<>(); + long time = System.currentTimeMillis(); synchronized (treatments) { + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: AllTreatmentsInDb: {}", MedtronicUtil.getGsonInstanceCore().toJson(treatments)); + for (Treatment t : treatments) { if (t.date <= time && t.date >= fromTimestamp) in5minback.add(t); } + + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: FilteredTreatments: AfterTime={}, Items={}", fromTimestamp, MedtronicUtil.getGsonInstanceCore().toJson(in5minback)); + return in5minback; } } @@ -387,25 +434,6 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface return getExtendedBolusFromHistory(System.currentTimeMillis()) != null; //TODO: crosscheck here } - @Subscribe - public void onStatusEvent(final EventReloadTreatmentData ev) { - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("EventReloadTreatmentData"); - initializeTreatmentData(); - initializeExtendedBolusData(); - updateTotalIOBTreatments(); - MainApp.bus().post(ev.next); - } - - @Subscribe - @SuppressWarnings("unused") - public void onStatusEvent(final EventReloadTempBasalData ev) { - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("EventReloadTempBasalData"); - initializeTempBasalData(); - updateTotalIOBTempBasals(); - } - @Override public IobTotal getLastCalculationTempBasals() { return lastTempBasalsCalculation; @@ -604,6 +632,9 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo, boolean allowUpdate) { boolean medtronicPump = MedtronicUtil.isMedtronicPump(); + if (MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: addToHistoryTreatment::isMedtronicPump={}", medtronicPump); + Treatment treatment = new Treatment(); treatment.date = detailedBolusInfo.date; treatment.source = detailedBolusInfo.source; @@ -618,6 +649,9 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface treatment.boluscalc = detailedBolusInfo.boluscalc != null ? detailedBolusInfo.boluscalc.toString() : null; TreatmentService.UpdateReturn creatOrUpdateResult; + if (medtronicPump && MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: addToHistoryTreatment::treatment={}", treatment); + if (!medtronicPump) creatOrUpdateResult = getService().createOrUpdate(treatment); else @@ -626,12 +660,17 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface boolean newRecordCreated = creatOrUpdateResult.newRecord; //log.debug("Adding new Treatment record" + treatment.toString()); if (detailedBolusInfo.carbTime != 0) { + Treatment carbsTreatment = new Treatment(); carbsTreatment.source = detailedBolusInfo.source; carbsTreatment.pumpId = detailedBolusInfo.pumpId; // but this should never happen carbsTreatment.date = detailedBolusInfo.date + detailedBolusInfo.carbTime * 60 * 1000L + 1000L; // add 1 sec to make them different records carbsTreatment.carbs = detailedBolusInfo.carbs; carbsTreatment.source = detailedBolusInfo.source; + + if (medtronicPump && MedtronicHistoryData.doubleBolusDebug) + log.debug("DoubleBolusDebug: carbTime!=0, creating second treatment. CarbsTreatment={}", carbsTreatment); + if (!medtronicPump) getService().createOrUpdate(carbsTreatment); else @@ -681,13 +720,6 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface return oldestTime; } - // TempTargets - @Subscribe - @SuppressWarnings("unused") - public void onStatusEvent(final EventTempTargetChange ev) { - initializeTempTargetData(); - } - @Nullable @Override public TempTarget getTempTargetFromHistory() { @@ -718,13 +750,6 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface NSUpload.uploadTempTarget(tempTarget); } - // Profile Switch - @Subscribe - @SuppressWarnings("unused") - public void onStatusEvent(final EventReloadProfileSwitchData ev) { - initializeProfileSwitchData(); - } - @Override @Nullable public ProfileSwitch getProfileSwitchFromHistory(long time) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.java index 556d80f321..ee78c6748b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.java @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.treatments.fragments; -import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.graphics.Paint; @@ -14,12 +13,11 @@ import android.widget.TextView; import androidx.appcompat.app.AlertDialog; import androidx.cardview.widget.CardView; import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import com.squareup.otto.Subscribe; - import java.util.List; import info.nightscout.androidaps.MainApp; @@ -28,7 +26,7 @@ import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.events.EventTreatmentChange; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue; @@ -39,11 +37,16 @@ import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.treatments.dialogs.WizardInfoDialog; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; import static info.nightscout.androidaps.utils.DateUtil.now; -public class TreatmentsBolusFragment extends SubscriberFragment implements View.OnClickListener { +public class TreatmentsBolusFragment extends Fragment implements View.OnClickListener { + private CompositeDisposable disposable = new CompositeDisposable(); + RecyclerView recyclerView; LinearLayoutManager llm; @@ -162,7 +165,7 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. } TreatmentsPlugin.getPlugin().getService().delete(treatment); } - updateGUI(); + updateGui(); } }); builder.setNegativeButton(MainApp.gs(R.string.cancel), null); @@ -213,7 +216,6 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. context = getContext(); - updateGUI(); return view; } @@ -227,7 +229,7 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. builder.setMessage(MainApp.gs(R.string.refresheventsfromnightscout) + "?"); builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { TreatmentsPlugin.getPlugin().getService().resetTreatments(); - MainApp.bus().post(new EventNSClientRestart()); + RxBus.INSTANCE.send(new EventNSClientRestart()); }); builder.setNegativeButton(MainApp.gs(R.string.cancel), null); builder.show(); @@ -248,7 +250,7 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. } TreatmentsPlugin.getPlugin().getService().delete(treatment); } - updateGUI(); + updateGui(); }); builder.setNegativeButton(MainApp.gs(R.string.cancel), null); builder.show(); @@ -256,32 +258,39 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. } } - @Subscribe - public void onStatusEvent(final EventTreatmentChange ev) { - updateGUI(); - } - - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - updateGUI(); + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventTreatmentChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + updateGui(); } @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTreatmentsFromHistory()), false); - if (TreatmentsPlugin.getPlugin().getLastCalculationTreatments() != null) { - iobTotal.setText(DecimalFormatter.to2Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().iob) + " " + MainApp.gs(R.string.insulin_unit_shortname)); - activityTotal.setText(DecimalFormatter.to3Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().activity) + " " + MainApp.gs(R.string.insulin_unit_shortname)); - } - if (!TreatmentsPlugin.getPlugin().getService().getTreatmentDataFromTime(now() + 1000, true).isEmpty()) { - deleteFutureTreatments.setVisibility(View.VISIBLE); - } else { - deleteFutureTreatments.setVisibility(View.GONE); - } - }); + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + + private void updateGui() { + recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTreatmentsFromHistory()), false); + if (TreatmentsPlugin.getPlugin().getLastCalculationTreatments() != null) { + iobTotal.setText(DecimalFormatter.to2Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().iob) + " " + MainApp.gs(R.string.insulin_unit_shortname)); + activityTotal.setText(DecimalFormatter.to3Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().activity) + " " + MainApp.gs(R.string.insulin_unit_shortname)); + } + if (!TreatmentsPlugin.getPlugin().getService().getTreatmentDataFromTime(now() + 1000, true).isEmpty()) { + deleteFutureTreatments.setVisibility(View.VISIBLE); + } else { + deleteFutureTreatments.setVisibility(View.GONE); + } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsCareportalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsCareportalFragment.java index 4a0e767cbb..4810a1ed8c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsCareportalFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsCareportalFragment.java @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.treatments.fragments; -import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.graphics.Paint; @@ -13,30 +12,33 @@ import android.widget.TextView; import androidx.appcompat.app.AlertDialog; import androidx.cardview.widget.CardView; +import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import com.squareup.otto.Subscribe; - import java.util.List; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.events.EventCareportalEventChange; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.Translator; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; /** * Created by mike on 13/01/17. */ -public class TreatmentsCareportalFragment extends SubscriberFragment implements View.OnClickListener { +public class TreatmentsCareportalFragment extends Fragment implements View.OnClickListener { + private CompositeDisposable disposable = new CompositeDisposable(); RecyclerView recyclerView; LinearLayoutManager llm; @@ -148,10 +150,26 @@ public class TreatmentsCareportalFragment extends SubscriberFragment implements if (nsUploadOnly) refreshFromNS.setVisibility(View.GONE); - updateGUI(); return view; } + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventCareportalEventChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + updateGui(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + @Override public void onClick(View view) { switch (view.getId()) { @@ -162,7 +180,7 @@ public class TreatmentsCareportalFragment extends SubscriberFragment implements builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MainApp.getDbHelper().resetCareportalEvents(); - MainApp.bus().post(new EventNSClientRestart()); + RxBus.INSTANCE.send(new EventNSClientRestart()); } }); builder.setNegativeButton(MainApp.gs(R.string.cancel), null); @@ -182,21 +200,8 @@ public class TreatmentsCareportalFragment extends SubscriberFragment implements } - @Subscribe - public void onStatusEvent(final EventCareportalEventChange ev) { - updateGUI(); - } - - @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)), false); - } - }); + private void updateGui() { + recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)), false); } private void removeAndroidAPSStatedEvents() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.java index 24a5185ef0..5ce7b57564 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.java @@ -1,21 +1,20 @@ package info.nightscout.androidaps.plugins.treatments.fragments; -import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; -import androidx.core.content.ContextCompat; -import androidx.appcompat.app.AlertDialog; -import androidx.cardview.widget.CardView; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.appcompat.app.AlertDialog; +import androidx.cardview.widget.CardView; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -24,16 +23,21 @@ import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.events.EventExtendedBolusChange; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; -public class TreatmentsExtendedBolusesFragment extends SubscriberFragment { +public class TreatmentsExtendedBolusesFragment extends Fragment { + private CompositeDisposable disposable = new CompositeDisposable(); + RecyclerView recyclerView; LinearLayoutManager llm; @@ -173,30 +177,33 @@ public class TreatmentsExtendedBolusesFragment extends SubscriberFragment { context = getContext(); - updateGUI(); return view; } - @Subscribe - public void onStatusEvent(final EventExtendedBolusChange ev) { - updateGUI(); - } - - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - updateGUI(); + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventExtendedBolusChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + updateGui(); } @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null && recyclerView != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getExtendedBolusesFromHistory()), false); - } - }); + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + + private void updateGui() { + recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getExtendedBolusesFromHistory()), false); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsProfileSwitchFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsProfileSwitchFragment.java index 71d8eb2b18..2d06b2824f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsProfileSwitchFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsProfileSwitchFragment.java @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.treatments.fragments; -import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.graphics.Paint; @@ -14,12 +13,11 @@ import android.widget.TextView; import androidx.appcompat.app.AlertDialog; import androidx.cardview.widget.CardView; import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,21 +30,25 @@ import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.events.EventProfileNeedsUpdate; import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; /** * Created by mike on 13/01/17. */ -public class TreatmentsProfileSwitchFragment extends SubscriberFragment implements View.OnClickListener { +public class TreatmentsProfileSwitchFragment extends Fragment implements View.OnClickListener { private Logger log = LoggerFactory.getLogger(L.UI); + private CompositeDisposable disposable = new CompositeDisposable(); RecyclerView recyclerView; LinearLayoutManager llm; @@ -195,10 +197,26 @@ public class TreatmentsProfileSwitchFragment extends SubscriberFragment implemen if (nsUploadOnly) refreshFromNS.setVisibility(View.GONE); - updateGUI(); return view; } + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventProfileNeedsUpdate.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI(), FabricPrivacy::logException) + ); + updateGUI(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + @Override public void onClick(View view) { switch (view.getId()) { @@ -209,7 +227,7 @@ public class TreatmentsProfileSwitchFragment extends SubscriberFragment implemen builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MainApp.getDbHelper().resetProfileSwitch(); - MainApp.bus().post(new EventNSClientRestart()); + RxBus.INSTANCE.send(new EventNSClientRestart()); } }); builder.setNegativeButton(MainApp.gs(R.string.cancel), null); @@ -218,20 +236,7 @@ public class TreatmentsProfileSwitchFragment extends SubscriberFragment implemen } } - @Subscribe - public void onStatusEvent(final EventProfileNeedsUpdate ev) { - updateGUI(); - } - - @Override protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getProfileSwitchData(false)), false); - } - }); + recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getProfileSwitchData(false)), false); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTempTargetFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTempTargetFragment.java index 3c17e7b762..ccd1b327c9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTempTargetFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTempTargetFragment.java @@ -14,18 +14,17 @@ import android.widget.TextView; import androidx.appcompat.app.AlertDialog; import androidx.cardview.widget.CardView; import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import com.squareup.otto.Subscribe; - import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Intervals; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.events.EventTempTargetChange; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue; @@ -33,13 +32,17 @@ import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientR import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; /** * Created by mike on 13/01/17. */ -public class TreatmentsTempTargetFragment extends SubscriberFragment implements View.OnClickListener { +public class TreatmentsTempTargetFragment extends Fragment implements View.OnClickListener { + private CompositeDisposable disposable = new CompositeDisposable(); RecyclerView recyclerView; LinearLayoutManager llm; @@ -184,10 +187,26 @@ public class TreatmentsTempTargetFragment extends SubscriberFragment implements if (nsUploadOnly) refreshFromNS.setVisibility(View.GONE); - updateGUI(); return view; } + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventTempTargetChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + updateGui(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + @Override public void onClick(View view) { switch (view.getId()) { @@ -198,7 +217,7 @@ public class TreatmentsTempTargetFragment extends SubscriberFragment implements builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { MainApp.getDbHelper().resetTempTargets(); - MainApp.bus().post(new EventNSClientRestart()); + RxBus.INSTANCE.send(new EventNSClientRestart()); } }); builder.setNegativeButton(MainApp.gs(R.string.cancel), null); @@ -208,20 +227,7 @@ public class TreatmentsTempTargetFragment extends SubscriberFragment implements } - @Subscribe - public void onStatusEvent(final EventTempTargetChange ev) { - updateGUI(); - } - - @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTempTargetsFromHistory()), false); - } - }); + private void updateGui() { + recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTempTargetsFromHistory()), false); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.java index 6aed7aea43..45f930410e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.java @@ -1,21 +1,20 @@ package info.nightscout.androidaps.plugins.treatments.fragments; -import android.app.Activity; import android.content.Context; import android.graphics.Paint; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.core.content.ContextCompat; -import androidx.appcompat.app.AlertDialog; -import androidx.cardview.widget.CardView; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.cardview.widget.CardView; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -25,7 +24,7 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.events.EventTempBasalChange; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue; @@ -33,9 +32,14 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutos import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; -public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { +public class TreatmentsTemporaryBasalsFragment extends Fragment { + private CompositeDisposable disposable = new CompositeDisposable(); + RecyclerView recyclerView; LinearLayoutManager llm; @@ -199,30 +203,36 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { context = getContext(); - updateGUI(); return view; } - @Subscribe - public void onStatusEvent(final EventTempBasalChange ignored) { - updateGUI(); - } - - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ignored) { - updateGUI(); + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventTempBasalChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + updateGui(); } @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false); - IobTotal tempBasalsCalculation = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals(); - if (tempBasalsCalculation != null) - tempBasalTotalView.setText(DecimalFormatter.to2Decimal(tempBasalsCalculation.basaliob, " U")); - }); + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + + private void updateGui() { + recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false); + IobTotal tempBasalsCalculation = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals(); + if (tempBasalsCalculation != null) + tempBasalTotalView.setText(DecimalFormatter.to2Decimal(tempBasalsCalculation.basaliob, " U")); } } 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 3130cb0df3..f24b02171f 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -27,7 +27,7 @@ import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressDialog; import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressHelperActivity; -import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusprogressIfRunning; +import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning; import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; @@ -101,16 +101,20 @@ public class CommandQueue { } private synchronized void removeAll(Command.CommandType type) { - for (int i = queue.size() - 1; i >= 0; i--) { - if (queue.get(i).commandType == type) { - queue.remove(i); + synchronized (queue) { + for (int i = queue.size() - 1; i >= 0; i--) { + if (queue.get(i).commandType == type) { + queue.remove(i); + } } } } private synchronized boolean isLastScheduled(Command.CommandType type) { - if (queue.size() > 0 && queue.get(queue.size() - 1).commandType == type) { - return true; + synchronized (queue) { + if (queue.size() > 0 && queue.get(queue.size() - 1).commandType == type) { + return true; + } } return false; } @@ -119,26 +123,33 @@ public class CommandQueue { // inject as a first command if (L.isEnabled(L.PUMPQUEUE)) log.debug("Adding as first: " + command.getClass().getSimpleName() + " - " + command.status()); - queue.addFirst(command); + synchronized (queue) { + queue.addFirst(command); + } } private synchronized void add(Command command) { if (L.isEnabled(L.PUMPQUEUE)) log.debug("Adding: " + command.getClass().getSimpleName() + " - " + command.status()); - queue.add(command); + synchronized (queue) { + queue.add(command); + } } synchronized void pickup() { - performing = queue.poll(); + synchronized (queue) { + performing = queue.poll(); + } } synchronized void clear() { performing = null; - for (int i = 0; i < queue.size(); i++) { - queue.get(i).cancel(); + synchronized (queue) { + for (int i = 0; i < queue.size(); i++) { + queue.get(i).cancel(); + } + queue.clear(); } - - queue.clear(); } public int size() { @@ -181,9 +192,11 @@ public class CommandQueue { public synchronized boolean bolusInQueue() { if (isRunning(Command.CommandType.BOLUS)) return true; - for (int i = 0; i < queue.size(); i++) { - if (queue.get(i).commandType == Command.CommandType.BOLUS) { - return true; + synchronized (queue) { + for (int i = 0; i < queue.size(); i++) { + if (queue.get(i).commandType == Command.CommandType.BOLUS) { + return true; + } } } return false; @@ -236,7 +249,7 @@ public class CommandQueue { // not when the Bolus command is starting. The command closes the dialog upon completion). showBolusProgressDialog(detailedBolusInfo.insulin, detailedBolusInfo.context); // Notify Wear about upcoming bolus - MainApp.bus().post(new EventBolusRequested(detailedBolusInfo.insulin)); + RxBus.INSTANCE.send(new EventBolusRequested(detailedBolusInfo.insulin)); } } @@ -262,7 +275,7 @@ public class CommandQueue { public synchronized void cancelAllBoluses() { if (!isRunning(Command.CommandType.BOLUS)) { - MainApp.bus().post(new EventDismissBolusprogressIfRunning(new PumpEnactResult().success(true).enacted(false))); + RxBus.INSTANCE.send(new EventDismissBolusProgressIfRunning(new PumpEnactResult().success(true).enacted(false))); } removeAll(Command.CommandType.BOLUS); removeAll(Command.CommandType.SMB_BOLUS); @@ -440,9 +453,11 @@ public class CommandQueue { public synchronized boolean statusInQueue() { if (isRunning(Command.CommandType.READSTATUS)) return true; - for (int i = 0; i < queue.size(); i++) { - if (queue.get(i).commandType == Command.CommandType.READSTATUS) { - return true; + synchronized (queue) { + for (int i = 0; i < queue.size(); i++) { + if (queue.get(i).commandType == Command.CommandType.READSTATUS) { + return true; + } } } return false; @@ -528,15 +543,18 @@ public class CommandQueue { public Spanned spannedStatus() { String s = ""; int line = 0; - if (performing != null) { - s += "" + performing.status() + ""; + Command perf = performing; + if (perf != null) { + s += "" + perf.status() + ""; line++; } - for (int i = 0; i < queue.size(); i++) { - if (line != 0) - s += "
"; - s += queue.get(i).status(); - line++; + synchronized (queue) { + for (int i = 0; i < queue.size(); i++) { + if (line != 0) + s += "
"; + s += queue.get(i).status(); + line++; + } } return Html.fromHtml(s); } 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 59eaf6471e..85823c2c30 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java @@ -14,8 +14,9 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusprogressIfRunning; +import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning; import info.nightscout.androidaps.queue.events.EventQueueChanged; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; @@ -50,7 +51,7 @@ public class QueueThread extends Thread { public final void run() { if (mWakeLock != null) mWakeLock.acquire(T.mins(10).msecs()); - MainApp.bus().post(new EventQueueChanged()); + RxBus.INSTANCE.send(new EventQueueChanged()); long lastCommandTime; long connectionStartTime = lastCommandTime = System.currentTimeMillis(); @@ -60,15 +61,15 @@ public class QueueThread extends Thread { if (pump == null) { if (L.isEnabled(L.PUMPQUEUE)) log.debug("pump == null"); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.pumpNotInitialized))); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.pumpNotInitialized))); SystemClock.sleep(1000); continue; } long secondsElapsed = (System.currentTimeMillis() - connectionStartTime) / 1000; if (!pump.isConnected() && secondsElapsed > Constants.PUMP_MAX_CONNECTION_TIME_IN_SECONDS) { - MainApp.bus().post(new EventDismissBolusprogressIfRunning(null)); - MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.connectiontimedout))); + RxBus.INSTANCE.send(new EventDismissBolusProgressIfRunning(null)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(MainApp.gs(R.string.connectiontimedout))); if (L.isEnabled(L.PUMPQUEUE)) log.debug("timed out"); pump.stopConnecting(); @@ -100,9 +101,9 @@ public class QueueThread extends Thread { queue.clear(); if (L.isEnabled(L.PUMPQUEUE)) log.debug("no connection possible"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); pump.disconnect("Queue empty"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTED)); return; } } @@ -110,7 +111,7 @@ public class QueueThread extends Thread { if (pump.isHandshakeInProgress()) { if (L.isEnabled(L.PUMPQUEUE)) log.debug("handshaking " + secondsElapsed); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, (int) secondsElapsed)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.HANDSHAKING, (int) secondsElapsed)); SystemClock.sleep(100); continue; } @@ -118,7 +119,7 @@ public class QueueThread extends Thread { if (pump.isConnecting()) { if (L.isEnabled(L.PUMPQUEUE)) log.debug("connecting " + secondsElapsed); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING, (int) secondsElapsed)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTING, (int) secondsElapsed)); SystemClock.sleep(1000); continue; } @@ -126,7 +127,7 @@ public class QueueThread extends Thread { if (!pump.isConnected()) { if (L.isEnabled(L.PUMPQUEUE)) log.debug("connect"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING, (int) secondsElapsed)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTING, (int) secondsElapsed)); pump.connect("Connection needed"); SystemClock.sleep(1000); continue; @@ -144,10 +145,10 @@ public class QueueThread extends Thread { if (queue.performing() != null) { if (L.isEnabled(L.PUMPQUEUE)) log.debug("performing " + queue.performing().status()); - MainApp.bus().post(new EventQueueChanged()); + RxBus.INSTANCE.send(new EventQueueChanged()); queue.performing().execute(); queue.resetPerforming(); - MainApp.bus().post(new EventQueueChanged()); + RxBus.INSTANCE.send(new EventQueueChanged()); lastCommandTime = System.currentTimeMillis(); SystemClock.sleep(100); continue; @@ -161,9 +162,9 @@ public class QueueThread extends Thread { waitingForDisconnect = true; if (L.isEnabled(L.PUMPQUEUE)) log.debug("queue empty. disconnect"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)); pump.disconnect("Queue empty"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED)); + RxBus.INSTANCE.send(new EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTED)); if (L.isEnabled(L.PUMPQUEUE)) log.debug("disconnected"); return; diff --git a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.java b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.java index 4dc132e128..6348ea31b9 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandBolus.java @@ -3,13 +3,13 @@ package info.nightscout.androidaps.queue.commands; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressDialog; -import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusprogressIfRunning; +import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.DecimalFormatter; @@ -33,7 +33,7 @@ public class CommandBolus extends Command { PumpEnactResult r = ConfigBuilderPlugin.getPlugin().getActivePump().deliverTreatment(detailedBolusInfo); BolusProgressDialog.bolusEnded = true; - MainApp.bus().post(new EventDismissBolusprogressIfRunning(r)); + RxBus.INSTANCE.send(new EventDismissBolusProgressIfRunning(r)); if (L.isEnabled(L.PUMPQUEUE)) log.debug("Result success: " + r.success + " enacted: " + r.enacted); diff --git a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandSetProfile.java b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandSetProfile.java index 6b8a610fa6..b69f3f4dc7 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandSetProfile.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandSetProfile.java @@ -50,8 +50,8 @@ public class CommandSetProfile extends Command { // Send SMS notification if ProfileSwitch is comming from NS ProfileSwitch profileSwitch = TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(System.currentTimeMillis()); if (profileSwitch != null && r.enacted && profileSwitch.source == Source.NIGHTSCOUT) { - SmsCommunicatorPlugin smsCommunicatorPlugin = MainApp.getSpecificPlugin(SmsCommunicatorPlugin.class); - if (smsCommunicatorPlugin != null && smsCommunicatorPlugin.isEnabled(PluginType.GENERAL)) { + SmsCommunicatorPlugin smsCommunicatorPlugin = SmsCommunicatorPlugin.INSTANCE; + if (smsCommunicatorPlugin.isEnabled(PluginType.GENERAL)) { smsCommunicatorPlugin.sendNotificationToAllNumbers(MainApp.gs(R.string.profile_set_ok)); } } diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java index d325ee9506..cb49d8cf4e 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/ChargingStateReceiver.java @@ -5,8 +5,8 @@ import android.content.Context; import android.content.Intent; import android.os.BatteryManager; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventChargingState; +import info.nightscout.androidaps.plugins.bus.RxBus; public class ChargingStateReceiver extends BroadcastReceiver { @@ -17,11 +17,11 @@ public class ChargingStateReceiver extends BroadcastReceiver { EventChargingState event = grabChargingState(context); if (event != null) - MainApp.bus().post(event); + RxBus.INSTANCE.send(event); lastEvent = event; } - public EventChargingState grabChargingState(Context context) { + public static EventChargingState grabChargingState(Context context) { BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE); if (bm == null) diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java index 7a9aa31c46..4c8a3f660e 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java @@ -16,6 +16,7 @@ import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.events.EventProfileNeedsUpdate; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.queue.commands.Command; @@ -73,7 +74,7 @@ public class KeepAliveReceiver extends BroadcastReceiver { } if (!pump.isThisProfileSet(profile) && !ConfigBuilderPlugin.getPlugin().getCommandQueue().isRunning(Command.CommandType.BASALPROFILE)) { - MainApp.bus().post(new EventProfileNeedsUpdate()); + RxBus.INSTANCE.send(new EventProfileNeedsUpdate()); } else if (isStatusOutdated && !pump.isBusy()) { lastReadStatus = System.currentTimeMillis(); ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("KeepAlive. Status outdated.", null); diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java index 2695ad64f2..19cf12bc8a 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventNetworkChange; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; public class NetworkChangeReceiver extends BroadcastReceiver { @@ -23,15 +24,17 @@ public class NetworkChangeReceiver extends BroadcastReceiver { private static EventNetworkChange lastEvent = null; + public static final NetworkChangeReceiver instance = new NetworkChangeReceiver(); + @Override public void onReceive(final Context context, final Intent intent) { EventNetworkChange event = grabNetworkStatus(context); if (event != null) - MainApp.bus().post(event); + RxBus.INSTANCE.send(event); } @Nullable - public EventNetworkChange grabNetworkStatus(final Context context) { + public static EventNetworkChange grabNetworkStatus(final Context context) { EventNetworkChange event = new EventNetworkChange(); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); @@ -40,23 +43,23 @@ public class NetworkChangeReceiver extends BroadcastReceiver { if (activeNetwork != null) { if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI && activeNetwork.isConnected()) { - event.wifiConnected = true; + event.setWifiConnected(true); WifiManager wifiManager = (WifiManager) MainApp.instance().getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifiManager != null) { WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) { - event.ssid = wifiInfo.getSSID(); + event.setSsid(wifiInfo.getSSID()); } if (L.isEnabled(L.CORE)) - log.debug("NETCHANGE: Wifi connected. SSID: " + event.ssid); + log.debug("NETCHANGE: Wifi connected. SSID: " + event.connectedSsid()); } } if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { - event.mobileConnected = true; - event.roaming = activeNetwork.isRoaming(); + event.setMobileConnected(true); + event.setRoaming(activeNetwork.isRoaming()); if (L.isEnabled(L.CORE)) - log.debug("NETCHANGE: Mobile connected. Roaming: " + event.roaming); + log.debug("NETCHANGE: Mobile connected. Roaming: " + event.getRoaming()); } } else { if (L.isEnabled(L.CORE)) @@ -68,14 +71,14 @@ public class NetworkChangeReceiver extends BroadcastReceiver { } public static boolean isWifiConnected() { - return lastEvent != null && lastEvent.wifiConnected; + return lastEvent != null && lastEvent.getWifiConnected(); } - public static boolean isConnected() { - return lastEvent != null && (lastEvent.wifiConnected || lastEvent.mobileConnected); + public static boolean isConnected() { + return lastEvent != null && (lastEvent.getWifiConnected() || lastEvent.getMobileConnected()); } public static EventNetworkChange getLastEvent() { return lastEvent; } -} \ No newline at end of file +} diff --git a/app/src/main/java/info/nightscout/androidaps/services/DataService.java b/app/src/main/java/info/nightscout/androidaps/services/DataService.java index 29b9c5fd01..44050ece35 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/services/DataService.java @@ -45,7 +45,6 @@ public class DataService extends IntentService { public DataService() { super("DataService"); - registerBus(); } @Override @@ -87,14 +86,14 @@ public class DataService extends IntentService { } else if (Intents.ACTION_NEW_STATUS.equals(action)) { NSSettingsStatus.getInstance().handleNewData(intent); } else if (Intents.ACTION_NEW_FOOD.equals(action)) { - EventNsFood evt = new EventNsFood(EventNsFood.ADD, bundles); - MainApp.bus().post(evt); + EventNsFood evt = new EventNsFood(EventNsFood.Companion.getADD(), bundles); + RxBus.INSTANCE.send(evt); } else if (Intents.ACTION_CHANGED_FOOD.equals(action)) { - EventNsFood evt = new EventNsFood(EventNsFood.UPDATE, bundles); - MainApp.bus().post(evt); + EventNsFood evt = new EventNsFood(EventNsFood.Companion.getUPDATE(), bundles); + RxBus.INSTANCE.send(evt); } else if (Intents.ACTION_REMOVED_FOOD.equals(action)) { - EventNsFood evt = new EventNsFood(EventNsFood.REMOVE, bundles); - MainApp.bus().post(evt); + EventNsFood evt = new EventNsFood(EventNsFood.Companion.getREMOVE(), bundles); + RxBus.INSTANCE.send(evt); } else if (acceptNSData && (Intents.ACTION_NEW_TREATMENT.equals(action) || Intents.ACTION_CHANGED_TREATMENT.equals(action) || @@ -104,7 +103,7 @@ public class DataService extends IntentService { ) { handleNewDataFromNSClient(intent); } else if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(action)) { - SmsCommunicatorPlugin.getPlugin().handleNewData(intent); + SmsCommunicatorPlugin.INSTANCE.handleNewData(intent); } if (L.isEnabled(L.DATASERVICE)) @@ -115,16 +114,6 @@ public class DataService extends IntentService { @Override public void onDestroy() { super.onDestroy(); - MainApp.bus().unregister(this); - } - - private void registerBus() { - try { - MainApp.bus().unregister(this); - } catch (RuntimeException x) { - // Ignore - } - MainApp.bus().register(this); } private void handleNewDataFromNSClient(Intent intent) { @@ -198,8 +187,8 @@ public class DataService extends IntentService { private void handleRemovedTreatmentFromNS(JSONObject json) { // new DB model - EventNsTreatment evtTreatment = new EventNsTreatment(EventNsTreatment.REMOVE, json); - MainApp.bus().post(evtTreatment); + EventNsTreatment evtTreatment = new EventNsTreatment(EventNsTreatment.Companion.getREMOVE(), json); + RxBus.INSTANCE.send(evtTreatment); // old DB model String _id = JsonHelper.safeGetString(json, "_id"); MainApp.getDbHelper().deleteTempTargetById(_id); @@ -211,7 +200,7 @@ public class DataService extends IntentService { private void handleTreatmentFromNS(JSONObject json, Intent intent) { // new DB model - int mode = Intents.ACTION_NEW_TREATMENT.equals(intent.getAction()) ? EventNsTreatment.ADD : EventNsTreatment.UPDATE; + int mode = Intents.ACTION_NEW_TREATMENT.equals(intent.getAction()) ? EventNsTreatment.Companion.getADD() : EventNsTreatment.Companion.getUPDATE(); double insulin = JsonHelper.safeGetDouble(json, "insulin"); double carbs = JsonHelper.safeGetDouble(json, "carbs"); String eventType = JsonHelper.safeGetString(json, "eventType"); @@ -221,7 +210,7 @@ public class DataService extends IntentService { } if (insulin > 0 || carbs > 0) { EventNsTreatment evtTreatment = new EventNsTreatment(mode, json); - MainApp.bus().post(evtTreatment); + RxBus.INSTANCE.send(evtTreatment); } else if (json.has(DanaRNSHistorySync.DANARSIGNATURE)) { // old DB model MainApp.getDbHelper().updateDanaRHistoryRecordId(json); diff --git a/app/src/main/java/info/nightscout/androidaps/services/LocationService.java b/app/src/main/java/info/nightscout/androidaps/services/LocationService.java index 33f92d770a..9600515d6f 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/LocationService.java +++ b/app/src/main/java/info/nightscout/androidaps/services/LocationService.java @@ -11,23 +11,24 @@ import android.os.IBinder; import androidx.core.app.ActivityCompat; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventLocationChange; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.persistentNotification.PersistentNotificationPlugin; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; public class LocationService extends Service { private static Logger log = LoggerFactory.getLogger(L.LOCATION); + private CompositeDisposable disposable = new CompositeDisposable(); private LocationManager mLocationManager = null; private static final float LOCATION_DISTANCE = 10f; @@ -125,7 +126,14 @@ public class LocationService extends Service { } catch (IllegalArgumentException ex) { log.error("network provider does not exist, " + ex.getMessage()); } - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppExit.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (L.isEnabled(L.CORE)) log.debug("EventAppExit received"); + stopSelf(); + }, FabricPrivacy::logException) + ); } @Override @@ -143,15 +151,7 @@ public class LocationService extends Service { log.error("fail to remove location listener, ignore", ex); } } - MainApp.bus().unregister(this); - } - - @Subscribe - public void onStatusEvent(EventAppExit event) { - if (L.isEnabled(L.CORE)) - log.debug("EventAppExit received"); - - stopSelf(); + disposable.clear(); } private void initializeLocationManager() { diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java index 2c6902cb01..2c9e1ea51b 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java @@ -5,8 +5,6 @@ import android.content.Intent; import androidx.appcompat.app.AppCompatActivity; -import com.squareup.otto.Subscribe; - import java.util.ArrayList; import java.util.List; @@ -19,6 +17,7 @@ import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesFragment; @@ -45,7 +44,6 @@ import info.nightscout.androidaps.setupwizard.elements.SWHtmlLink; import info.nightscout.androidaps.setupwizard.elements.SWInfotext; import info.nightscout.androidaps.setupwizard.elements.SWPlugin; import info.nightscout.androidaps.setupwizard.elements.SWRadioButton; -import info.nightscout.androidaps.setupwizard.events.EventSWLabel; import info.nightscout.androidaps.setupwizard.events.EventSWUpdate; import info.nightscout.androidaps.utils.AndroidPermission; import info.nightscout.androidaps.utils.LocaleHelper; @@ -93,8 +91,7 @@ public class SWDefinition { .preferenceId(R.string.key_language).label(R.string.language) .comment(R.string.setupwizard_language_prompt)) .validator(() -> { - String lang = SP.getString("language", "en"); - LocaleHelper.setLocale(MainApp.instance().getApplicationContext(), lang); + LocaleHelper.INSTANCE.update(MainApp.instance().getApplicationContext()); return SP.contains(R.string.key_language); }); @@ -108,7 +105,7 @@ public class SWDefinition { .visibility(() -> !SP.getBoolean(R.string.key_i_understand, false)) .action(() -> { SP.putBoolean(R.string.key_i_understand, true); - MainApp.bus().post(new EventSWUpdate(false)); + RxBus.INSTANCE.send(new EventSWUpdate(false)); })) .visibility(() -> !SP.getBoolean(R.string.key_i_understand, false)) .validator(() -> SP.getBoolean(R.string.key_i_understand, false)); @@ -170,8 +167,8 @@ public class SWDefinition { NSClientPlugin.getPlugin().setFragmentVisible(PluginType.GENERAL, true); ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(NSClientPlugin.getPlugin(), PluginType.GENERAL); ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard"); - MainApp.bus().post(new EventConfigBuilderChange()); - MainApp.bus().post(new EventSWUpdate(true)); + RxBus.INSTANCE.send(new EventConfigBuilderChange()); + RxBus.INSTANCE.send(new EventSWUpdate(true)); }) .visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL))) .add(new SWEditUrl() @@ -186,15 +183,9 @@ public class SWDefinition { .label(R.string.nsclientinternal_secret_dialogtitle) .comment(R.string.nsclientinternal_secret_dialogmessage)) .add(new SWBreak()) - .add(new SWEventListener(this) + .add(new SWEventListener(this, EventNSClientStatus.class) .label(R.string.status) .initialStatus(NSClientPlugin.getPlugin().status) - .listener(new Object() { - @Subscribe - public void onEventNSClientStatus(EventNSClientStatus event) { - MainApp.bus().post(new EventSWLabel(event.status)); - } - }) ) .add(new SWBreak()) .validator(() -> NSClientPlugin.getPlugin().nsClientService != null && NSClientService.isConnected && NSClientService.hasWriteAuth) @@ -328,14 +319,7 @@ public class SWDefinition { .text(R.string.readstatus) .action(() -> ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("Clicked connect to pump", null)) .visibility(() -> ConfigBuilderPlugin.getPlugin().getActivePump() != null)) - .add(new SWEventListener(this) - .listener(new Object() { - @Subscribe - public void onEventPumpStatusChanged(EventPumpStatusChanged event) { - MainApp.bus().post(new EventSWLabel(event.textStatus())); - } - }) - ) + .add(new SWEventListener(this, EventPumpStatusChanged.class)) .validator(() -> ConfigBuilderPlugin.getPlugin().getActivePump() != null && ConfigBuilderPlugin.getPlugin().getActivePump().isInitialized()); private SWScreen screenAps = new SWScreen(R.string.configbuilder_aps) @@ -385,8 +369,8 @@ public class SWDefinition { LoopPlugin.getPlugin().setFragmentVisible(PluginType.LOOP, true); ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(LoopPlugin.getPlugin(), PluginType.LOOP); ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard"); - MainApp.bus().post(new EventConfigBuilderChange()); - MainApp.bus().post(new EventSWUpdate(true)); + RxBus.INSTANCE.send(new EventConfigBuilderChange()); + RxBus.INSTANCE.send(new EventSWUpdate(true)); }) .visibility(() -> !LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))) .validator(() -> LoopPlugin.getPlugin().isEnabled(PluginType.LOOP)) diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWEventListener.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWEventListener.java index 487a80d91c..26944effbf 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWEventListener.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWEventListener.java @@ -1,33 +1,42 @@ package info.nightscout.androidaps.setupwizard; import android.content.Context; -import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.events.EventStatus; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.setupwizard.elements.SWItem; -import info.nightscout.androidaps.setupwizard.events.EventSWLabel; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; public class SWEventListener extends SWItem { private static Logger log = LoggerFactory.getLogger(SWEventListener.class); + private CompositeDisposable disposable = new CompositeDisposable(); private int textLabel = 0; private String status = ""; TextView textView; - Object listener; SWDefinition definition; - SWEventListener(SWDefinition definition) { + SWEventListener(SWDefinition definition, Class clazz) { super(Type.LISTENER); this.definition = definition; - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(clazz) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + status = ((EventStatus) event).getStatus(); + if (textView != null) + textView.setText((textLabel != 0 ? MainApp.gs(textLabel) : "") + " " + status); + }) + ); + } public SWEventListener label(int newLabel) { @@ -35,16 +44,11 @@ public class SWEventListener extends SWItem { return this; } - public SWEventListener initialStatus(String status) { + SWEventListener initialStatus(String status) { this.status = status; return this; } - public SWEventListener listener(Object listener) { - this.listener = listener; - return this; - } - @Override public void generateDialog(LinearLayout layout) { Context context = layout.getContext(); @@ -53,20 +57,5 @@ public class SWEventListener extends SWItem { textView.setId(layout.generateViewId()); textView.setText((textLabel != 0 ? MainApp.gs(textLabel) : "") + " " + status); layout.addView(textView); - if (listener != null) - try { - MainApp.bus().register(listener); - } catch (Exception ignored) {} } - - @Subscribe - public void onEventSWLabel(final EventSWLabel l) { - status = l.label; - if (definition != null && definition.getActivity() != null) - definition.getActivity().runOnUiThread(() -> { - if (textView != null) - textView.setText((textLabel != 0 ? MainApp.gs(textLabel) : "") + " " + status); - }); - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java index 1d3b867a6d..e6a4831693 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java @@ -12,8 +12,6 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.core.app.ActivityCompat; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,17 +24,22 @@ import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; import info.nightscout.androidaps.events.EventProfileNeedsUpdate; import info.nightscout.androidaps.events.EventProfileStoreChanged; import info.nightscout.androidaps.events.EventPumpStatusChanged; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus; import info.nightscout.androidaps.setupwizard.elements.SWItem; import info.nightscout.androidaps.setupwizard.events.EventSWUpdate; import info.nightscout.androidaps.utils.AndroidPermission; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.LocaleHelper; import info.nightscout.androidaps.utils.OKDialog; import info.nightscout.androidaps.utils.SP; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; public class SetupWizardActivity extends NoSplashAppCompatActivity { //logging private static Logger log = LoggerFactory.getLogger(SetupWizardActivity.class); + private CompositeDisposable disposable = new CompositeDisposable(); ScrollView scrollView; @@ -48,7 +51,7 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - LocaleHelper.onCreate(this, "en"); + LocaleHelper.INSTANCE.update(getApplicationContext()); setContentView(R.layout.activity_setupwizard); scrollView = (ScrollView) findViewById(R.id.sw_scrollview); @@ -71,7 +74,8 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity { @Override public void onBackPressed() { - if (currentWizardPage == 0) OKDialog.showConfirmation(this, MainApp.gs(R.string.exitwizard), this::finish); + if (currentWizardPage == 0) + OKDialog.showConfirmation(this, MainApp.gs(R.string.exitwizard), this::finish); else showPreviousPage(null); } @@ -83,41 +87,41 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity { @Override public void onPause() { super.onPause(); - MainApp.bus().unregister(this); + disposable.clear(); } @Override protected void onResume() { super.onResume(); - MainApp.bus().register(this); swDefinition.setActivity(this); - } - - @Subscribe - public void onContentUpdate(EventSWUpdate ev) { - if (ev.redraw) - generateLayout(); - updateButtons(); - } - - @Subscribe - public void onEventNSClientStatus(EventNSClientStatus ignored) { - updateButtons(); - } - - @Subscribe - public void onEventPumpStatusChanged(EventPumpStatusChanged ignored) { - updateButtons(); - } - - @Subscribe - public void onEventProfileStoreChanged(EventProfileStoreChanged ignored) { - updateButtons(); - } - - @Subscribe - public void onEventProfileSwitchChange(EventProfileNeedsUpdate ignored) { - updateButtons(); + disposable.add(RxBus.INSTANCE + .toObservable(EventPumpStatusChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateButtons(), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventNSClientStatus.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateButtons(), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventProfileNeedsUpdate.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateButtons(), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventProfileStoreChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateButtons(), FabricPrivacy::logException) + ); + disposable.add(RxBus.INSTANCE + .toObservable(EventSWUpdate.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + if (event.getRedraw()) generateLayout(); + updateButtons(); + }, FabricPrivacy::logException) + ); } private void generateLayout() { @@ -127,7 +131,7 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity { SWItem currentItem = currentScreen.items.get(i); currentItem.generateDialog(layout); } - scrollView.smoothScrollTo(0,0); + scrollView.smoothScrollTo(0, 0); } private void updateButtons() { diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditUrl.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditUrl.java index 7e6ead7d72..f35decbf53 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditUrl.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditUrl.java @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.setupwizard.events.EventSWLabel; import info.nightscout.androidaps.utils.SP; @@ -62,7 +63,7 @@ public class SWEditUrl extends SWItem { if (Patterns.WEB_URL.matcher(s).matches()) save(s.toString(), updateDelay); else - MainApp.bus().post(new EventSWLabel(MainApp.gs(R.string.error_url_not_valid))); + RxBus.INSTANCE.send(new EventSWLabel(MainApp.gs(R.string.error_url_not_valid))); } @Override diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWItem.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWItem.java index 6206cb0d9a..6931344acf 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWItem.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWItem.java @@ -12,9 +12,9 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.setupwizard.events.EventSWUpdate; import info.nightscout.androidaps.utils.SP; @@ -98,9 +98,8 @@ public class SWItem { public void run() { if (L.isEnabled(L.CORE)) log.debug("Firing EventPreferenceChange"); - MainApp.bus().post(new EventPreferenceChange(preferenceId)); RxBus.INSTANCE.send(new EventPreferenceChange(preferenceId)); - MainApp.bus().post(new EventSWUpdate()); + RxBus.INSTANCE.send(new EventSWUpdate(false)); scheduledEventPost = null; } } diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java index 8c0be61598..e5dde501bc 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java @@ -17,7 +17,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventConfigBuilderChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.setupwizard.events.EventSWUpdate; @@ -88,8 +88,8 @@ public class SWPlugin extends SWItem { plugin.setFragmentVisible(pType, rb.isChecked() && makeVisible); ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(plugin, pType); ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard"); - MainApp.bus().post(new EventConfigBuilderChange()); - MainApp.bus().post(new EventSWUpdate()); + RxBus.INSTANCE.send(new EventConfigBuilderChange()); + RxBus.INSTANCE.send(new EventSWUpdate(false)); }); layout.addView(radioGroup); super.generateDialog(layout); diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/events/EventSWUpdate.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/events/EventSWUpdate.java deleted file mode 100644 index 181960ac40..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/events/EventSWUpdate.java +++ /dev/null @@ -1,14 +0,0 @@ -package info.nightscout.androidaps.setupwizard.events; - -import info.nightscout.androidaps.events.Event; - -public class EventSWUpdate extends Event { - public boolean redraw = false; - - public EventSWUpdate() { - } - - public EventSWUpdate(boolean redraw) { - this.redraw = redraw; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/events/EventSWUpdate.kt b/app/src/main/java/info/nightscout/androidaps/setupwizard/events/EventSWUpdate.kt new file mode 100644 index 0000000000..3c700cda41 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/events/EventSWUpdate.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.setupwizard.events + +import info.nightscout.androidaps.events.Event + +class EventSWUpdate(var redraw: Boolean) : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/BolusWizard.kt b/app/src/main/java/info/nightscout/androidaps/utils/BolusWizard.kt index 11e8f7a5a9..ef4cae3f83 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/BolusWizard.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/BolusWizard.kt @@ -17,6 +17,7 @@ import info.nightscout.androidaps.interfaces.PumpDescription import info.nightscout.androidaps.interfaces.PumpInterface import info.nightscout.androidaps.logging.L import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin +import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperActivity @@ -28,6 +29,7 @@ import org.json.JSONException import org.json.JSONObject import org.slf4j.LoggerFactory import java.util.* +import kotlin.math.abs class BolusWizard @JvmOverloads constructor(val profile: Profile, val profileName: String, @@ -60,14 +62,11 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, var glucoseStatus: GlucoseStatus? = null private set - var targetBGLow = 0.0 - private set + private var targetBGLow = 0.0 - var targetBGHigh = 0.0 - private set + private var targetBGHigh = 0.0 - var bgDiff = 0.0 - private set + private var bgDiff = 0.0 var insulinFromBG = 0.0 private set @@ -96,8 +95,7 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, var trend = 0.0 private set - var accepted = false - private set + private var accepted = false // Result var calculatedTotalInsulin: Double = 0.0 @@ -127,12 +125,10 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, targetBGHigh = Profile.fromMgdlToUnits(tempTarget.high, profile.units) } if (useBg && bg > 0) { - if (bg >= targetBGLow && bg <= targetBGHigh) { - bgDiff = 0.0 - } else if (bg <= targetBGLow) { - bgDiff = bg - targetBGLow - } else { - bgDiff = bg - targetBGHigh + bgDiff = when { + bg in targetBGLow..targetBGHigh -> 0.0 + bg <= targetBGLow -> bg - targetBGLow + else -> bg - targetBGHigh } insulinFromBG = bgDiff / sens } @@ -147,7 +143,7 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, } - // Insuling from carbs + // Insulin from carbs ic = profile.ic insulinFromCarbs = carbs / ic insulinFromCOB = if (useCob) (cob / ic) else 0.0 @@ -197,7 +193,7 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, log.debug(this.toString()) } - fun nsJSON(): JSONObject { + private fun nsJSON(): JSONObject { val boluscalcJSON = JSONObject() try { boluscalcJSON.put("profile", profileName) @@ -260,7 +256,7 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, if (absorptionRate > .25) confirmMessage += "
" + MainApp.gs(R.string.slowabsorptiondetected, MainApp.gc(R.color.cobAlert), (absorptionRate * 100).toInt()) } - if (Math.abs(insulinAfterConstraints - calculatedTotalInsulin) > pump.getPumpDescription().pumpType.determineCorrectBolusStepSize(insulinAfterConstraints)) { + if (abs(insulinAfterConstraints - calculatedTotalInsulin) > pump.pumpDescription.pumpType.determineCorrectBolusStepSize(insulinAfterConstraints)) { confirmMessage += "
" + MainApp.gs(R.string.bolusconstraintappliedwarning, MainApp.gc(R.color.warning), calculatedTotalInsulin, insulinAfterConstraints) } @@ -268,10 +264,10 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, } fun confirmAndExecute(context: Context) { - val profile = ProfileFunctions.getInstance().profile - val pump = ConfigBuilderPlugin.getPlugin().activePump + val profile = ProfileFunctions.getInstance().profile ?: return + val pump = ConfigBuilderPlugin.getPlugin().activePump ?: return - if (pump != null && profile != null && (calculatedTotalInsulin > 0.0 || carbs > 0.0)) { + if (calculatedTotalInsulin > 0.0 || carbs > 0.0) { val confirmMessage = confirmMessageAfterConstraints(pump) val builder = AlertDialog.Builder(context) @@ -289,7 +285,7 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, val loopPlugin = LoopPlugin.getPlugin() if (loopPlugin.isEnabled(PluginType.LOOP)) { loopPlugin.superBolusTo(System.currentTimeMillis() + 2 * 60L * 60 * 1000) - MainApp.bus().post(EventRefreshOverview("WizardDialog")) + RxBus.send(EventRefreshOverview("WizardDialog")) } val pump1 = ConfigBuilderPlugin.getPlugin().activePump diff --git a/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java b/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java index d592347d1b..98c4d3a071 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java @@ -93,15 +93,15 @@ public class DateUtil { } public static int toSeconds(String hh_colon_mm) { - Pattern p = Pattern.compile("(\\d+):(\\d+)( a.m.| p.m.| AM | PM|)"); + Pattern p = Pattern.compile("(\\d+):(\\d+)( a.m.| p.m.| AM| PM|AM|PM|)"); Matcher m = p.matcher(hh_colon_mm); int retval = 0; if (m.find()) { retval = SafeParse.stringToInt(m.group(1)) * 60 * 60 + SafeParse.stringToInt(m.group(2)) * 60; - if ((m.group(3).equals(" a.m.") || m.group(3).equals(" AM")) && m.group(1).equals("12")) + if ((m.group(3).equals(" a.m.") || m.group(3).equals(" AM") || m.group(3).equals("AM")) && m.group(1).equals("12")) retval -= 12 * 60 * 60; - if ((m.group(3).equals(" p.m.") || m.group(3).equals(" PM")) && !(m.group(1).equals("12"))) + if ((m.group(3).equals(" p.m.") || m.group(3).equals(" PM") || m.group(3).equals("PM")) && !(m.group(1).equals("12"))) retval += 12 * 60 * 60; } return retval; @@ -185,7 +185,7 @@ public class DateUtil { long remainingTimeMinutes = timeInMillis / (1000 * 60); long remainingTimeHours = remainingTimeMinutes / 60; remainingTimeMinutes = remainingTimeMinutes % 60; - return "(" + ((remainingTimeHours > 0) ? (remainingTimeHours + "h ") : "") + remainingTimeMinutes + "')"; + return "(" + ((remainingTimeHours > 0) ? (remainingTimeHours + MainApp.gs(R.string.shorthour) + " ") : "") + remainingTimeMinutes + "')"; } public static String sinceString(long timestamp) { diff --git a/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.java b/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.java index 3dd7fe4fd9..355623f38d 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.java @@ -8,10 +8,13 @@ import com.google.firebase.analytics.FirebaseAnalytics; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; + import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.constraints.signatureVerifier.SignatureVerifierPlugin; /** * Created by jamorham on 21/02/2018. @@ -112,19 +115,23 @@ public class FabricPrivacy { String closedLoopEnabled = MainApp.getConstraintChecker().isClosedLoopAllowed().value() ? "CLOSED_LOOP_ENABLED" : "CLOSED_LOOP_DISABLED"; // Size is limited to 36 chars - String remote = BuildConfig.REMOTE - .replace("https://","") - .replace("http://","") + String remote = BuildConfig.REMOTE.toLowerCase() + .replace("https://", "") + .replace("http://", "") .replace(".git", "") .replace(".com/", ":") .replace(".org/", ":") .replace(".net/", ":"); MainApp.getFirebaseAnalytics().setUserProperty("Mode", BuildConfig.APPLICATION_ID + "-" + closedLoopEnabled); - MainApp.getFirebaseAnalytics().setUserProperty("Language", LocaleHelper.getLanguage()); + MainApp.getFirebaseAnalytics().setUserProperty("Language", LocaleHelper.INSTANCE.currentLanguage()); MainApp.getFirebaseAnalytics().setUserProperty("Version", BuildConfig.VERSION); MainApp.getFirebaseAnalytics().setUserProperty("HEAD", BuildConfig.HEAD); MainApp.getFirebaseAnalytics().setUserProperty("Remote", remote); + List hashes = SignatureVerifierPlugin.getPlugin().shortHashes(); + if (hashes.size() >= 1) + MainApp.getFirebaseAnalytics().setUserProperty("Hash", hashes.get(0)); + if (ConfigBuilderPlugin.getPlugin().getActivePump() != null) MainApp.getFirebaseAnalytics().setUserProperty("Pump", ConfigBuilderPlugin.getPlugin().getActivePump().getClass().getSimpleName()); if (ConfigBuilderPlugin.getPlugin().getActiveAPS() != null) @@ -137,7 +144,6 @@ public class FabricPrivacy { MainApp.getFirebaseAnalytics().setUserProperty("Sensitivity", ConfigBuilderPlugin.getPlugin().getActiveSensitivity().getClass().getSimpleName()); if (ConfigBuilderPlugin.getPlugin().getActiveInsulin() != null) MainApp.getFirebaseAnalytics().setUserProperty("Insulin", ConfigBuilderPlugin.getPlugin().getActiveInsulin().getClass().getSimpleName()); - } } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/LocaleHelper.java b/app/src/main/java/info/nightscout/androidaps/utils/LocaleHelper.java deleted file mode 100644 index d2e4c0622b..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/utils/LocaleHelper.java +++ /dev/null @@ -1,69 +0,0 @@ -package info.nightscout.androidaps.utils; - -import android.content.Context; -import android.content.SharedPreferences; -import android.content.res.Configuration; -import android.content.res.Resources; -import android.preference.PreferenceManager; - -import java.util.Locale; - -/** - * This class is used to change your application locale and persist this change for the next time - * that your app is going to be used. - *

- * You can also change the locale of your application on the fly by using the setLocale method. - *

- * Created by gunhansancar on 07/10/15. - */ -public class LocaleHelper { - - private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language"; - - public static void onCreate(Context context) { - String lang = getPersistedData(Locale.getDefault().getLanguage()); - setLocale(context, lang); - } - - public static void onCreate(Context context, String defaultLanguage) { - String lang = getPersistedData(defaultLanguage); - setLocale(context, lang); - } - - public static String getLanguage() { - return getPersistedData(Locale.getDefault().getLanguage()); - } - - public static void setLocale(Context context, String language) { - persist(context, language); - updateResources(context, language); - } - - private static String getPersistedData(String defaultLanguage) { - return SP.getString(SELECTED_LANGUAGE, defaultLanguage); - } - - private static void persist(Context context, String language) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - SharedPreferences.Editor editor = preferences.edit(); - - editor.putString(SELECTED_LANGUAGE, language); - editor.apply(); - } - - private static void updateResources(Context context, String language) { - Locale locale = new Locale(language); - Locale.setDefault(locale); - - Resources resources = context.getResources(); - - Configuration configuration = resources.getConfiguration(); - configuration.locale = locale; - - resources.updateConfiguration(configuration, resources.getDisplayMetrics()); - } - - public static Locale getLocale() { - return new Locale(getPersistedData("en")); - } -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/LocaleHelper.kt b/app/src/main/java/info/nightscout/androidaps/utils/LocaleHelper.kt new file mode 100644 index 0000000000..6052c5699a --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/LocaleHelper.kt @@ -0,0 +1,32 @@ +package info.nightscout.androidaps.utils + +import android.content.Context +import info.nightscout.androidaps.R +import java.util.* + +object LocaleHelper { + fun update(context: Context) = + updateResources(context, currentLanguage()) + + fun currentLanguage(): String = + SP.getString(R.string.key_language, Locale.getDefault().language) + + fun currentLocale(): Locale = + Locale(SP.getString(R.string.key_language, Locale.getDefault().language)) + + @Suppress("DEPRECATION") + private fun updateResources(context: Context, language: String) { + var locale = Locale(language) + if (language.contains("_")) { + // language with country like pt_BR defined in arrays.xml + val lang = language.substring(0, 2) + val country = language.substring(3, 5) + locale = Locale(lang, country) + } + + Locale.setDefault(locale) + val resources = context.resources + resources.configuration.setLocale(locale) + resources.updateConfiguration(resources.configuration, resources.displayMetrics) + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/Round.java b/app/src/main/java/info/nightscout/androidaps/utils/Round.java index ba7f7e3f86..3e312a20a9 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/Round.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/Round.java @@ -1,14 +1,23 @@ package info.nightscout.androidaps.utils; +import java.math.BigDecimal; + /** * Created by mike on 20.06.2016. */ public class Round { public static Double roundTo(double x, Double step) { - if (x != 0d) { - return Math.round(x / step) * step; + if (x == 0d) { + return 0d; } - return 0d; + + //Double oldCalc = Math.round(x / step) * step; + Double newCalc = BigDecimal.valueOf(Math.round(x / step)).multiply(BigDecimal.valueOf(step)).doubleValue(); + + // just for the tests, forcing failures + //newCalc = oldCalc; + + return newCalc; } public static Double floorTo(Double x, Double step) { if (x != 0d) { diff --git a/app/src/main/java/info/nightscout/androidaps/utils/Translator.java b/app/src/main/java/info/nightscout/androidaps/utils/Translator.java index ee83f856e4..e25c083a7b 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/Translator.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/Translator.java @@ -33,6 +33,8 @@ public class Translator { return MainApp.gs(R.string.careportal_exercise); case "Site Change": return MainApp.gs(R.string.careportal_pumpsitechange); + case "Pump Battery Change": + return MainApp.gs(R.string.careportal_pumpbatterychange); case "Sensor Start": return MainApp.gs(R.string.careportal_cgmsensorstart); case "Sensor Change": diff --git a/app/src/main/res/layout/automation_fragment.xml b/app/src/main/res/layout/automation_fragment.xml index 96cbe8862b..2f24e1b70c 100644 --- a/app/src/main/res/layout/automation_fragment.xml +++ b/app/src/main/res/layout/automation_fragment.xml @@ -12,7 +12,7 @@ android:layout_above="@+id/automation_logView" android:layout_alignParentStart="true" android:layout_alignParentTop="true" - android:layout_marginBottom="-100dp" /> + android:layout_marginBottom="15dp" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + android:layout_gravity="center" + android:layout_marginLeft="10dp" + android:layout_marginRight="10dp" + android:text="@string/boluswizard" + android:textAppearance="?android:attr/textAppearanceLarge" + android:textColor="@color/colorCalculatorButton" /> + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -173,9 +192,9 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" - android:layout_marginLeft="10dp" + android:layout_marginStart="10dp" android:checked="false" - android:text="Super\nbolus" /> + android:text="@string/superbolus" /> @@ -211,7 +230,7 @@ @@ -219,7 +238,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" - android:width="120dp" + android:labelFor="@+id/treatment_wizard_notes" android:padding="10dp" android:text="@string/careportal_newnstreatment_notes_label" android:textAppearance="@android:style/TextAppearance.Material.Small" @@ -227,317 +246,373 @@ - + + + + + + +