diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 798185f68a..66acee4016 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -10,22 +10,16 @@ import android.net.wifi.WifiManager; import androidx.annotation.StringRes; import androidx.localbroadcastmanager.content.LocalBroadcastManager; -import com.crashlytics.android.Crashlytics; -import com.google.firebase.analytics.FirebaseAnalytics; import com.j256.ormlite.android.apptools.OpenHelperManager; import net.danlew.android.joda.JodaTimeAndroid; -import org.json.JSONException; - import java.util.List; import javax.inject.Inject; import dagger.android.AndroidInjector; import dagger.android.DaggerApplication; -import dagger.android.HasAndroidInjector; -import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.dependencyInjection.DaggerAppComponent; import info.nightscout.androidaps.interfaces.PluginBase; @@ -33,7 +27,6 @@ import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.PluginStore; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction; import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtils; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.receivers.BTReceiver; @@ -41,35 +34,24 @@ import info.nightscout.androidaps.receivers.ChargingStateReceiver; import info.nightscout.androidaps.receivers.DataReceiver; import info.nightscout.androidaps.receivers.KeepAliveReceiver; import info.nightscout.androidaps.receivers.NetworkChangeReceiver; -import info.nightscout.androidaps.receivers.ReceiverStatusStore; import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver; import info.nightscout.androidaps.services.Intents; import info.nightscout.androidaps.utils.ActivityMonitor; -import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.LocaleHelper; -import info.nightscout.androidaps.utils.resources.ResourceHelper; import info.nightscout.androidaps.utils.sharedPreferences.SP; -import io.fabric.sdk.android.Fabric; public class MainApp extends DaggerApplication { static MainApp sInstance; private static Resources sResources; - static FirebaseAnalytics firebaseAnalytics; - static DatabaseHelper sDatabaseHelper = null; @Inject PluginStore pluginStore; - @Inject public HasAndroidInjector injector; @Inject AAPSLogger aapsLogger; - @Inject ReceiverStatusStore receiverStatusStore; @Inject ActivityMonitor activityMonitor; - @Inject FabricPrivacy fabricPrivacy; - @Inject ResourceHelper resourceHelper; @Inject VersionCheckerUtils versionCheckersUtils; @Inject SP sp; - @Inject ProfileFunction profileFunction; @Inject ConfigBuilderPlugin configBuilderPlugin; @Inject KeepAliveReceiver.KeepAliveManager keepAliveManager; @@ -93,19 +75,8 @@ public class MainApp extends DaggerApplication { aapsLogger.error("Uncaught exception crashing app", ex); }); - try { - if (fabricPrivacy.fabricEnabled()) { - Fabric.with(this, new Crashlytics()); - } - } catch (Exception e) { - aapsLogger.error("Error with Fabric init! " + e); - } - registerActivityLifecycleCallbacks(activityMonitor); - firebaseAnalytics = FirebaseAnalytics.getInstance(this); - firebaseAnalytics.setAnalyticsCollectionEnabled(!Boolean.getBoolean("disableFirebase") && fabricPrivacy.fabricEnabled()); - JodaTimeAndroid.init(this); aapsLogger.debug("Version: " + BuildConfig.VERSION_NAME); @@ -130,26 +101,6 @@ public class MainApp extends DaggerApplication { private void doMigrations() { - // guarantee that the unreachable threshold is at least 30 and of type String - // Added in 1.57 at 21.01.2018 - 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, Integer.toString(unreachable_threshold)); - - // 2.5 -> 2.6 - if (!sp.contains(R.string.key_units)) { - String newUnits = Constants.MGDL; - Profile p = profileFunction.getProfile(); - if (p != null && p.getData() != null && p.getData().has("units")) { - try { - newUnits = p.getData().getString("units"); - } catch (JSONException e) { - aapsLogger.error("Unhandled exception", e); - } - } - sp.putString(R.string.key_units, newUnits); - } } @Override @@ -199,11 +150,6 @@ public class MainApp extends DaggerApplication { return sResources.getString(id); } - @Deprecated - public static String gs(@StringRes int id, Object... args) { - return sResources.getString(id, args); - } - @Deprecated public static MainApp instance() { return sInstance; @@ -213,10 +159,6 @@ public class MainApp extends DaggerApplication { return sDatabaseHelper; } - public FirebaseAnalytics getFirebaseAnalytics() { - return firebaseAnalytics; - } - @Override public void onTerminate() { aapsLogger.debug(LTag.CORE, "onTerminate"); diff --git a/app/src/main/java/info/nightscout/androidaps/data/Profile.java b/app/src/main/java/info/nightscout/androidaps/data/Profile.java index f1204f8b72..0fcc0321c2 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Profile.java +++ b/app/src/main/java/info/nightscout/androidaps/data/Profile.java @@ -64,7 +64,7 @@ public class Profile { // Default constructor for DB public Profile() { - MainApp.instance().injector.androidInjector().inject(this); + MainApp.instance().androidInjector().inject(this); } protected Profile(HasAndroidInjector injector) { diff --git a/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java b/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java index c16b34d96a..00c31586f4 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java +++ b/app/src/main/java/info/nightscout/androidaps/db/ProfileSwitch.java @@ -76,7 +76,7 @@ public class ProfileSwitch implements Interval, DataPointWithLabelInterface { @Inject public ResourceHelper resourceHelper; public ProfileSwitch() { - this.injector = MainApp.instance().injector; + this.injector = MainApp.instance(); injector.androidInjector().inject(this); } diff --git a/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java b/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java index 9966425260..925d56e81d 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TempTarget.java @@ -17,6 +17,7 @@ import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.resources.ResourceHelper; @DatabaseTable(tableName = DatabaseHelper.DATABASE_TEMPTARGETS) public class TempTarget implements Interval { @@ -195,10 +196,10 @@ public class TempTarget implements Interval { '}'; } - public String friendlyDescription(String units) { + public String friendlyDescription(String units, ResourceHelper resourceHelper) { return Profile.toTargetRangeString(low, high, Constants.MGDL, units) + units + - "@" + MainApp.gs(R.string.mins, durationInMinutes) + + "@" + resourceHelper.gs(R.string.mins, durationInMinutes) + (reason != null && !reason.equals("") ? "(" + reason + ")" : ""); } diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/DataClassesModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/DataClassesModule.kt index 2eb498b0e7..99ffb12f25 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/DataClassesModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/DataClassesModule.kt @@ -11,6 +11,7 @@ import info.nightscout.androidaps.db.ProfileSwitch import info.nightscout.androidaps.db.TemporaryBasal import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.plugins.treatments.Treatment +import info.nightscout.androidaps.plugins.treatments.TreatmentService import info.nightscout.androidaps.utils.wizard.BolusWizard import info.nightscout.androidaps.utils.wizard.QuickWizardEntry @@ -29,6 +30,8 @@ abstract class DataClassesModule { @ContributesAndroidInjector abstract fun careportalEventInjector(): CareportalEvent @ContributesAndroidInjector abstract fun extendedBolusInjector(): ExtendedBolus + @ContributesAndroidInjector abstract fun treatmentServiceInjector(): TreatmentService + @ContributesAndroidInjector abstract fun bolusWizardInjector(): BolusWizard @ContributesAndroidInjector abstract fun quickWizardEntryInjector(): QuickWizardEntry } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.kt index d7f1b6a4e2..d0cb6b09e7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionStartTempTarget.kt @@ -36,7 +36,7 @@ class ActionStartTempTarget(injector: HasAndroidInjector) : Action(injector) { } override fun friendlyName(): Int = R.string.starttemptarget - override fun shortDescription(): String = resourceHelper.gs(R.string.starttemptarget) + ": " + tt().friendlyDescription(value.units) + override fun shortDescription(): String = resourceHelper.gs(R.string.starttemptarget) + ": " + tt().friendlyDescription(value.units, resourceHelper) @DrawableRes override fun icon(): Int = R.drawable.icon_cp_cgm_target override fun doAction(callback: Callback) { 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 e44060d9ed..ad15a1e64f 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 @@ -37,6 +37,7 @@ public class ComboFragment extends DaggerFragment implements View.OnClickListene @Inject ResourceHelper resourceHelper; @Inject RxBusWrapper rxBus; @Inject SP sp; + @Inject FabricPrivacy fabricPrivacy; private CompositeDisposable disposable = new CompositeDisposable(); @@ -80,12 +81,12 @@ public class ComboFragment extends DaggerFragment implements View.OnClickListene disposable.add(rxBus .toObservable(EventComboPumpUpdateGUI.class) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(event -> updateGui(), exception -> FabricPrivacy.getInstance().logException(exception)) + .subscribe(event -> updateGui(), fabricPrivacy::logException) ); disposable.add(rxBus .toObservable(EventQueueChanged.class) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(event -> updateGui(), exception -> FabricPrivacy.getInstance().logException(exception)) + .subscribe(event -> updateGui(), fabricPrivacy::logException) ); updateGui(); } 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 45ed524fdf..4bb5609f33 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 @@ -105,7 +105,6 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter private final RileyLinkServiceData rileyLinkServiceData; private final ServiceTaskExecutor serviceTaskExecutor; - protected static MedtronicPumpPlugin plugin = null; private RileyLinkMedtronicService rileyLinkMedtronicService; // variables for handling statuses and history @@ -149,7 +148,6 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter PumpType.Medtronic_522_722, // we default to most basic model, correct model from config is loaded later injector, resourceHelper, aapsLogger, commandQueue, rxBus, activePlugin, sp, context, fabricPrivacy ); - this.plugin = this; this.rileyLinkUtil = rileyLinkUtil; this.medtronicUtil = medtronicUtil; @@ -194,14 +192,6 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter super.onStart(); } - @Deprecated - public static MedtronicPumpPlugin getPlugin() { - if (plugin == null) - throw new IllegalStateException("Plugin not injected jet"); - return plugin; - } - - @Override public void updatePreferenceSummary(@NotNull Preference pref) { super.updatePreferenceSummary(pref); 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 6e250bd851..cd81621252 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 @@ -18,8 +18,6 @@ import com.j256.ormlite.table.TableUtils; import org.apache.commons.lang3.StringUtils; import org.json.JSONException; import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.sql.SQLException; import java.util.ArrayList; @@ -29,6 +27,9 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import dagger.android.HasAndroidInjector; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.ICallback; import info.nightscout.androidaps.db.Source; @@ -36,13 +37,12 @@ import info.nightscout.androidaps.events.Event; 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.logging.StacktraceLoggerWrapper; -import info.nightscout.androidaps.plugins.bus.RxBus; +import info.nightscout.androidaps.logging.AAPSLogger; +import info.nightscout.androidaps.logging.LTag; +import info.nightscout.androidaps.plugins.bus.RxBusWrapper; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData; import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicPumpPlugin; 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; @@ -54,16 +54,22 @@ import io.reactivex.schedulers.Schedulers; */ public class TreatmentService extends OrmLiteBaseService { - private static Logger log = StacktraceLoggerWrapper.getLogger(L.DATATREATMENTS); + + @Inject AAPSLogger aapsLogger; + @Inject FabricPrivacy fabricPrivacy; + @Inject RxBusWrapper rxBus; + @Inject MedtronicPumpPlugin medtronicPumpPlugin; + private CompositeDisposable disposable = new CompositeDisposable(); private static final ScheduledExecutorService treatmentEventWorker = Executors.newSingleThreadScheduledExecutor(); private static ScheduledFuture scheduledTreatmentEventPost = null; - public TreatmentService() { + public TreatmentService(HasAndroidInjector injector) { + injector.androidInjector().inject(this); onCreate(); dbInitialize(); - disposable.add(RxBus.Companion.getINSTANCE() + disposable.add(rxBus .toObservable(EventNsTreatment.class) .observeOn(Schedulers.io()) .subscribe(event -> { @@ -75,7 +81,7 @@ public class TreatmentService extends OrmLiteBaseService { } else { // EventNsTreatment.REMOVE this.deleteNS(payload); } - }, exception -> FabricPrivacy.getInstance().logException(exception)) + }, fabricPrivacy::logException) ); } @@ -101,7 +107,7 @@ public class TreatmentService extends OrmLiteBaseService { try { return DaoManager.createDao(this.getConnectionSource(), Treatment.class); } catch (SQLException e) { - log.error("Cannot create Dao for Treatment.class"); + aapsLogger.error("Cannot create Dao for Treatment.class"); } return null; @@ -111,35 +117,33 @@ public class TreatmentService extends OrmLiteBaseService { public void onCreate() { super.onCreate(); try { - if (L.isEnabled(L.DATATREATMENTS)) - log.info("onCreate"); + aapsLogger.info(LTag.DATATREATMENTS, "onCreate"); TableUtils.createTableIfNotExists(this.getConnectionSource(), Treatment.class); } catch (SQLException e) { - log.error("Can't create database", e); + aapsLogger.error("Can't create database", e); throw new RuntimeException(e); } } public void onUpgrade(ConnectionSource connectionSource, int oldVersion, int newVersion) { if (oldVersion == 7 && newVersion == 8) { - log.debug("Upgrading database from v7 to v8"); + aapsLogger.debug("Upgrading database from v7 to v8"); try { TableUtils.dropTable(connectionSource, Treatment.class, true); TableUtils.createTableIfNotExists(connectionSource, Treatment.class); } catch (SQLException e) { - log.error("Can't create database", e); + aapsLogger.error("Can't create database", e); throw new RuntimeException(e); } } else if (oldVersion == 8 && newVersion == 9) { - log.debug("Upgrading database from v8 to v9"); + aapsLogger.debug("Upgrading database from v8 to v9"); try { getDao().executeRaw("ALTER TABLE `" + Treatment.TABLE_TREATMENTS + "` ADD COLUMN boluscalc STRING;"); } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } } else { - if (L.isEnabled(L.DATATREATMENTS)) - log.info("onUpgrade"); + aapsLogger.info(LTag.DATATREATMENTS, "onUpgrade"); // this.resetFood(); } } @@ -149,7 +153,7 @@ public class TreatmentService extends OrmLiteBaseService { try { getDao().executeRaw("ALTER TABLE `" + Treatment.TABLE_TREATMENTS + "` DROP COLUMN boluscalc STRING;"); } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } } } @@ -160,7 +164,7 @@ public class TreatmentService extends OrmLiteBaseService { TableUtils.createTableIfNotExists(this.getConnectionSource(), Treatment.class); DatabaseHelper.updateEarliestDataChange(0); } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } scheduleTreatmentChange(null, true); } @@ -185,13 +189,11 @@ public class TreatmentService extends OrmLiteBaseService { class PostRunnable implements Runnable { public void run() { - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Firing EventReloadTreatmentData"); - RxBus.Companion.getINSTANCE().send(event); + aapsLogger.debug(LTag.DATATREATMENTS, "Firing EventReloadTreatmentData"); + rxBus.send(event); if (DatabaseHelper.earliestDataChange != null) { - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Firing EventNewHistoryData"); - RxBus.Companion.getINSTANCE().send(new EventNewHistoryData(DatabaseHelper.earliestDataChange)); + aapsLogger.debug(LTag.DATATREATMENTS, "Firing EventNewHistoryData"); + rxBus.send(new EventNewHistoryData(DatabaseHelper.earliestDataChange)); } DatabaseHelper.earliestDataChange = null; callback.setPost(null); @@ -212,13 +214,11 @@ public class TreatmentService extends OrmLiteBaseService { */ public void scheduleTreatmentChange(@Nullable final Treatment treatment, boolean runImmediately) { if (runImmediately) { - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Firing EventReloadTreatmentData"); - RxBus.Companion.getINSTANCE().send(new EventReloadTreatmentData(new EventTreatmentChange(treatment))); + aapsLogger.debug(LTag.DATATREATMENTS, "Firing EventReloadTreatmentData"); + rxBus.send(new EventReloadTreatmentData(new EventTreatmentChange(treatment))); if (DatabaseHelper.earliestDataChange != null) { - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Firing EventNewHistoryData"); - RxBus.Companion.getINSTANCE().send(new EventNewHistoryData(DatabaseHelper.earliestDataChange)); + aapsLogger.debug(LTag.DATATREATMENTS, "Firing EventNewHistoryData"); + rxBus.send(new EventNewHistoryData(DatabaseHelper.earliestDataChange)); } DatabaseHelper.earliestDataChange = null; } else { @@ -240,7 +240,7 @@ public class TreatmentService extends OrmLiteBaseService { try { return this.getDao().queryForAll(); } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } return new ArrayList<>(); @@ -266,16 +266,16 @@ public class TreatmentService extends OrmLiteBaseService { if (treatment != null) { if (MedtronicHistoryData.doubleBolusDebug) - log.debug("DoubleBolusDebug: createTreatmentFromJsonIfNotExists:: medtronicPump={}", MedtronicPumpPlugin.getPlugin().isEnabled()); + aapsLogger.debug(LTag.DATATREATMENTS, "DoubleBolusDebug: createTreatmentFromJsonIfNotExists:: medtronicPump={}", medtronicPumpPlugin.isEnabled()); - if (!MedtronicPumpPlugin.getPlugin().isEnabled()) + if (!medtronicPumpPlugin.isEnabled()) createOrUpdate(treatment); else createOrUpdateMedtronic(treatment, true); } else - log.error("Date is null: " + treatment.toString()); + aapsLogger.error("Date is null: " + treatment.toString()); } catch (JSONException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } } @@ -283,7 +283,7 @@ public class TreatmentService extends OrmLiteBaseService { // return true if new record is created public UpdateReturn createOrUpdate(Treatment treatment) { if (treatment != null && treatment.source == Source.NONE) { - log.error("Coder error: source is not set for treatment: " + treatment, new Exception()); + aapsLogger.error("Coder error: source is not set for treatment: " + treatment, new Exception()); //FabricPrivacy.logException(new Exception("Coder error: source is not set for treatment: " + treatment)); } try { @@ -298,8 +298,7 @@ public class TreatmentService extends OrmLiteBaseService { boolean sameSource = existingTreatment.source == treatment.source; if (!equalRePumpHistory) { // another treatment exists. Update it with the treatment coming from the pump - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); long oldDate = existingTreatment.date; //preserve carbs @@ -323,8 +322,7 @@ public class TreatmentService extends OrmLiteBaseService { boolean equalRePumpHistory = existingTreatment.equalsRePumpHistory(treatment); boolean sameSource = existingTreatment.source == treatment.source; long oldDate = existingTreatment.date; - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); //preserve carbs if (existingTreatment.isValid && existingTreatment.carbs > 0 && treatment.carbs == 0) { @@ -340,8 +338,7 @@ public class TreatmentService extends OrmLiteBaseService { return new UpdateReturn(equalRePumpHistory || sameSource, false); } getDao().create(treatment); - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); scheduleTreatmentChange(treatment, true); return new UpdateReturn(true, true); @@ -355,8 +352,7 @@ public class TreatmentService extends OrmLiteBaseService { getDao().delete(old); // need to delete/create because date may change too old.copyFrom(treatment); getDao().create(old); - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Updating record by date from: " + Source.getString(treatment.source) + " " + old.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "Updating record by date from: " + Source.getString(treatment.source) + " " + old.toString()); if (historyChange) { DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(old.date); @@ -364,8 +360,7 @@ public class TreatmentService extends OrmLiteBaseService { scheduleTreatmentChange(treatment, false); return new UpdateReturn(true, true); } - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Equal record by date from: " + Source.getString(treatment.source) + " " + old.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "Equal record by date from: " + Source.getString(treatment.source) + " " + old.toString()); return new UpdateReturn(true, false); } // find by NS _id @@ -378,8 +373,7 @@ public class TreatmentService extends OrmLiteBaseService { getDao().delete(old); // need to delete/create because date may change too old.copyFrom(treatment); getDao().create(old); - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Updating record by _id from: " + Source.getString(treatment.source) + " " + old.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "Updating record by _id from: " + Source.getString(treatment.source) + " " + old.toString()); if (historyChange) { DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(old.date); @@ -387,28 +381,25 @@ public class TreatmentService extends OrmLiteBaseService { scheduleTreatmentChange(treatment, false); return new UpdateReturn(true, true); } - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Equal record by _id from: " + Source.getString(treatment.source) + " " + old.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "Equal record by _id from: " + Source.getString(treatment.source) + " " + old.toString()); return new UpdateReturn(true, false); } } getDao().create(treatment); - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); scheduleTreatmentChange(treatment, false); return new UpdateReturn(true, true); } if (treatment.source == Source.USER) { getDao().create(treatment); - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); scheduleTreatmentChange(treatment, true); return new UpdateReturn(true, true); } } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } return new UpdateReturn(false, false); } @@ -417,7 +408,7 @@ public class TreatmentService extends OrmLiteBaseService { public UpdateReturn createOrUpdateMedtronic(Treatment treatment, boolean fromNightScout) { if (MedtronicHistoryData.doubleBolusDebug) - log.debug("DoubleBolusDebug: createOrUpdateMedtronic:: originalTreatment={}, fromNightScout={}", treatment, fromNightScout); + aapsLogger.debug(LTag.DATATREATMENTS, "DoubleBolusDebug: createOrUpdateMedtronic:: originalTreatment={}, fromNightScout={}", treatment, fromNightScout); try { treatment.date = DatabaseHelper.roundDateToSec(treatment.date); @@ -425,12 +416,11 @@ public class TreatmentService extends OrmLiteBaseService { Treatment existingTreatment = getRecord(treatment.pumpId, treatment.date); if (MedtronicHistoryData.doubleBolusDebug) - log.debug("DoubleBolusDebug: createOrUpdateMedtronic:: existingTreatment={}", treatment); + aapsLogger.debug(LTag.DATATREATMENTS, "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()); + aapsLogger.debug(LTag.DATATREATMENTS, "New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); scheduleTreatmentChange(treatment, true); return new UpdateReturn(true, true); @@ -438,7 +428,7 @@ public class TreatmentService extends OrmLiteBaseService { if (existingTreatment.date == treatment.date) { if (MedtronicHistoryData.doubleBolusDebug) - log.debug("DoubleBolusDebug: createOrUpdateMedtronic::(existingTreatment.date==treatment.date)"); + aapsLogger.debug(LTag.DATATREATMENTS, "DoubleBolusDebug: createOrUpdateMedtronic::(existingTreatment.date==treatment.date)"); // we will do update only, if entry changed if (!optionalTreatmentCopy(existingTreatment, treatment, fromNightScout)) { @@ -450,7 +440,7 @@ public class TreatmentService extends OrmLiteBaseService { return new UpdateReturn(true, false); } else { if (MedtronicHistoryData.doubleBolusDebug) - log.debug("DoubleBolusDebug: createOrUpdateMedtronic::(existingTreatment.date != treatment.date)"); + aapsLogger.debug(LTag.DATATREATMENTS, "DoubleBolusDebug: createOrUpdateMedtronic::(existingTreatment.date != treatment.date)"); // date is different, we need to remove entry getDao().delete(existingTreatment); @@ -463,7 +453,7 @@ public class TreatmentService extends OrmLiteBaseService { } } catch (SQLException e) { - log.error("Unhandled SQL exception: {}", e.getMessage(), e); + aapsLogger.error("Unhandled SQL exception: {}", e.getMessage(), e); } return new UpdateReturn(false, false); } @@ -471,7 +461,7 @@ public class TreatmentService extends OrmLiteBaseService { private boolean optionalTreatmentCopy(Treatment oldTreatment, Treatment newTreatment, boolean fromNightScout) { - log.debug("optionalTreatmentCopy [old={}, new={}]", oldTreatment.toString(), newTreatment.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "optionalTreatmentCopy [old={}, new={}]", oldTreatment.toString(), newTreatment.toString()); boolean changed = false; @@ -537,7 +527,7 @@ public class TreatmentService extends OrmLiteBaseService { changed = true; } - log.debug("optionalTreatmentCopy [changed={}, newAfterChange={}]", changed, oldTreatment.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "optionalTreatmentCopy [changed={}, newAfterChange={}]", changed, oldTreatment.toString()); return changed; } @@ -551,7 +541,7 @@ public class TreatmentService extends OrmLiteBaseService { @Deprecated private void treatmentCopy(Treatment oldTreatment, Treatment newTreatment, boolean fromNightScout) { - log.debug("treatmentCopy [old={}, new={}]", oldTreatment.toString(), newTreatment.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "treatmentCopy [old={}, new={}]", oldTreatment.toString(), newTreatment.toString()); if (fromNightScout) { @@ -574,7 +564,7 @@ public class TreatmentService extends OrmLiteBaseService { oldTreatment.copyFrom(newTreatment); } - log.debug("treatmentCopy [newAfterChange={}]", oldTreatment.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "treatmentCopy [newAfterChange={}]", oldTreatment.toString()); } @@ -595,7 +585,7 @@ public class TreatmentService extends OrmLiteBaseService { try { record = getDao().queryForId(date); } catch (SQLException ex) { - log.error("Error getting entry by id ({}", date); + aapsLogger.error("Error getting entry by id ({}", date); } return record; @@ -619,7 +609,7 @@ public class TreatmentService extends OrmLiteBaseService { if (result.isEmpty()) return null; if (result.size() > 1) - log.warn("Multiple records with the same pump id found (returning first one): " + result.toString()); + aapsLogger.warn(LTag.DATATREATMENTS, "Multiple records with the same pump id found (returning first one): " + result.toString()); return result.get(0); } catch (SQLException e) { throw new RuntimeException(e); @@ -642,12 +632,11 @@ public class TreatmentService extends OrmLiteBaseService { private void deleteByNSId(String _id) { Treatment stored = findByNSId(_id); if (stored != null) { - if (L.isEnabled(L.DATATREATMENTS)) - log.debug("Removing Treatment record from database: " + stored.toString()); + aapsLogger.debug(LTag.DATATREATMENTS, "Removing Treatment record from database: " + stored.toString()); try { getDao().delete(stored); } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } DatabaseHelper.updateEarliestDataChange(stored.date); this.scheduleTreatmentChange(stored, false); @@ -667,7 +656,7 @@ public class TreatmentService extends OrmLiteBaseService { DatabaseHelper.updateEarliestDataChange(treatment.date); this.scheduleTreatmentChange(treatment, true); } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } } @@ -676,7 +665,7 @@ public class TreatmentService extends OrmLiteBaseService { getDao().update(treatment); DatabaseHelper.updateEarliestDataChange(treatment.date); } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } scheduleTreatmentChange(treatment, true); } @@ -705,7 +694,7 @@ public class TreatmentService extends OrmLiteBaseService { return trList.get(0); } } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } return null; } @@ -722,7 +711,7 @@ public class TreatmentService extends OrmLiteBaseService { treatments = daoTreatments.query(preparedQuery); return treatments; } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } return new ArrayList<>(); } @@ -739,7 +728,7 @@ public class TreatmentService extends OrmLiteBaseService { treatments = daoTreatments.query(preparedQuery); return treatments; } catch (SQLException e) { - log.error("Unhandled exception", e); + aapsLogger.error("Unhandled exception", e); } return new ArrayList<>(); } 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 460fa3eb23..51c602bfd2 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 @@ -122,7 +122,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface @Override protected void onStart() { - this.service = new TreatmentService(); + this.service = new TreatmentService(getInjector()); initializeData(range()); super.onStart(); disposable.add(rxBus 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 32803f9cae..1d95d4306b 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 @@ -47,6 +47,7 @@ public class TreatmentsTempTargetFragment extends DaggerFragment { @Inject RxBusWrapper rxBus; @Inject ProfileFunction profileFunction; @Inject ResourceHelper resourceHelper; + @Inject FabricPrivacy fabricPrivacy; private CompositeDisposable disposable = new CompositeDisposable(); @@ -139,7 +140,7 @@ public class TreatmentsTempTargetFragment extends DaggerFragment { remove.setOnClickListener(v -> { final TempTarget tempTarget = (TempTarget) v.getTag(); OKDialog.showConfirmation(getContext(), resourceHelper.gs(R.string.removerecord), - resourceHelper.gs(R.string.careportal_temporarytarget) + ": " + tempTarget.friendlyDescription(profileFunction.getUnits()) + + resourceHelper.gs(R.string.careportal_temporarytarget) + ": " + tempTarget.friendlyDescription(profileFunction.getUnits(), resourceHelper) + "\n" + DateUtil.dateAndTimeString(tempTarget.date), (dialog, id) -> { final String _id = tempTarget._id; @@ -189,7 +190,7 @@ public class TreatmentsTempTargetFragment extends DaggerFragment { disposable.add(rxBus .toObservable(EventTempTargetChange.class) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(event -> updateGui(), exception -> FabricPrivacy.getInstance().logException(exception)) + .subscribe(event -> updateGui(), fabricPrivacy::logException) ); updateGui(); } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.kt b/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.kt index 41eb2464f5..a87fd3fef9 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/FabricPrivacy.kt @@ -1,11 +1,11 @@ package info.nightscout.androidaps.utils +import android.content.Context import android.os.Bundle import com.crashlytics.android.Crashlytics import com.google.firebase.analytics.FirebaseAnalytics import info.nightscout.androidaps.BuildConfig import info.nightscout.androidaps.Config -import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.logging.AAPSLogger @@ -13,6 +13,7 @@ import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker import info.nightscout.androidaps.plugins.constraints.signatureVerifier.SignatureVerifierPlugin import info.nightscout.androidaps.utils.sharedPreferences.SP +import io.fabric.sdk.android.Fabric import java.util.* import javax.inject.Inject import javax.inject.Singleton @@ -24,16 +25,24 @@ import javax.inject.Singleton */ @Singleton class FabricPrivacy @Inject constructor( + context: Context, private val aapsLogger: AAPSLogger, private val sp: SP, private val constraintChecker: ConstraintChecker, - private val mainApp: MainApp, private val signatureVerifierPlugin: SignatureVerifierPlugin, private val activePlugin: ActivePluginProvider ) { + private var firebaseAnalytics: FirebaseAnalytics + init { instance = this + firebaseAnalytics = FirebaseAnalytics.getInstance(context) + firebaseAnalytics.setAnalyticsCollectionEnabled(!java.lang.Boolean.getBoolean("disableFirebase") && fabricEnabled()) + + if (fabricEnabled()) { + Fabric.with(context, Crashlytics()) + } } companion object { @@ -48,7 +57,7 @@ class FabricPrivacy @Inject constructor( fun logCustom(event: Bundle) { try { if (fabricEnabled()) { - mainApp.firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, event) + firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, event) } else { aapsLogger.debug(LTag.CORE, "Ignoring recently opted-out event: $event") } @@ -63,7 +72,7 @@ class FabricPrivacy @Inject constructor( fun logCustom(event: String) { try { if (fabricEnabled()) { - mainApp.firebaseAnalytics.logEvent(event, Bundle()) + firebaseAnalytics.logEvent(event, Bundle()) } else { aapsLogger.debug(LTag.CORE, "Ignoring recently opted-out event: $event") } @@ -125,19 +134,19 @@ class FabricPrivacy @Inject constructor( .replace(".com/", ":") .replace(".org/", ":") .replace(".net/", ":") - mainApp.firebaseAnalytics.setUserProperty("Mode", BuildConfig.APPLICATION_ID + "-" + closedLoopEnabled) - mainApp.firebaseAnalytics.setUserProperty("Language", sp.getString(R.string.key_language, Locale.getDefault().language)) - mainApp.firebaseAnalytics.setUserProperty("Version", BuildConfig.VERSION) - mainApp.firebaseAnalytics.setUserProperty("HEAD", BuildConfig.HEAD) - mainApp.firebaseAnalytics.setUserProperty("Remote", remote) + firebaseAnalytics.setUserProperty("Mode", BuildConfig.APPLICATION_ID + "-" + closedLoopEnabled) + firebaseAnalytics.setUserProperty("Language", sp.getString(R.string.key_language, Locale.getDefault().language)) + firebaseAnalytics.setUserProperty("Version", BuildConfig.VERSION) + firebaseAnalytics.setUserProperty("HEAD", BuildConfig.HEAD) + firebaseAnalytics.setUserProperty("Remote", remote) val hashes: List = signatureVerifierPlugin.shortHashes() - if (hashes.isNotEmpty()) mainApp.firebaseAnalytics.setUserProperty("Hash", hashes[0]) - activePlugin.activePump.let { mainApp.firebaseAnalytics.setUserProperty("Pump", it::class.java.simpleName) } + if (hashes.isNotEmpty()) firebaseAnalytics.setUserProperty("Hash", hashes[0]) + activePlugin.activePump.let { firebaseAnalytics.setUserProperty("Pump", it::class.java.simpleName) } if (!Config.NSCLIENT && !Config.PUMPCONTROL) - activePlugin.activeAPS.let { mainApp.firebaseAnalytics.setUserProperty("Aps", it::class.java.simpleName) } - activePlugin.activeBgSource.let { mainApp.firebaseAnalytics.setUserProperty("BgSource", it::class.java.simpleName) } - mainApp.firebaseAnalytics.setUserProperty("Profile", activePlugin.activeProfileInterface.javaClass.simpleName) - activePlugin.activeSensitivity.let { mainApp.firebaseAnalytics.setUserProperty("Sensitivity", it::class.java.simpleName) } - activePlugin.activeInsulin.let { mainApp.firebaseAnalytics.setUserProperty("Insulin", it::class.java.simpleName) } + activePlugin.activeAPS.let { firebaseAnalytics.setUserProperty("Aps", it::class.java.simpleName) } + activePlugin.activeBgSource.let { firebaseAnalytics.setUserProperty("BgSource", it::class.java.simpleName) } + firebaseAnalytics.setUserProperty("Profile", activePlugin.activeProfileInterface.javaClass.simpleName) + activePlugin.activeSensitivity.let { firebaseAnalytics.setUserProperty("Sensitivity", it::class.java.simpleName) } + activePlugin.activeInsulin.let { firebaseAnalytics.setUserProperty("Insulin", it::class.java.simpleName) } } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/SP.java b/app/src/main/java/info/nightscout/androidaps/utils/SP.java index b3c581fee1..08ad3a8693 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/SP.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/SP.java @@ -145,7 +145,7 @@ public class SP { @Deprecated static public void incInt(int resourceID) { - int value = SP.getInt(resourceID, 0) + 1; + int value = getInt(resourceID, 0) + 1; sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply(); } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt index 0dc0cab7f3..906516d5ea 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt @@ -35,7 +35,7 @@ class TddCalculator @Inject constructor( ) : TreatmentsPlugin(injector, aapsLogger, rxBus, resourceHelper, mainApp, sp, profileFunction, activePlugin, fabricPrivacy) { init { - service = TreatmentService() // plugin is not started + service = TreatmentService(injector) // plugin is not started } fun calculate(days: Long): LongSparseArray { diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicHistoryDataUTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicHistoryDataUTest.java index 4db64d90b1..824d9a3f1e 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicHistoryDataUTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicHistoryDataUTest.java @@ -11,7 +11,6 @@ import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil; import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.RawHistoryPage; import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.MedtronicPumpHistoryDecoder; import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.PumpHistoryEntry; -import info.nightscout.androidaps.utils.SP; import static org.mockito.Mockito.when; //import uk.org.lidalia.slf4jtest.TestLogger;