diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ServicesModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ServicesModule.kt index 612a905f18..1a7eae5daa 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ServicesModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/ServicesModule.kt @@ -10,6 +10,8 @@ import info.nightscout.androidaps.plugins.pump.danaR.services.DanaRExecutionServ import info.nightscout.androidaps.plugins.pump.danaRKorean.services.DanaRKoreanExecutionService import info.nightscout.androidaps.plugins.pump.danaRS.services.DanaRSService import info.nightscout.androidaps.plugins.pump.danaRv2.services.DanaRv2ExecutionService +import info.nightscout.androidaps.plugins.pump.insight.InsightAlertService +import info.nightscout.androidaps.plugins.pump.insight.connection_service.InsightConnectionService import info.nightscout.androidaps.services.AlarmSoundService import info.nightscout.androidaps.services.DataService import info.nightscout.androidaps.services.LocationService @@ -29,4 +31,6 @@ abstract class ServicesModule { @ContributesAndroidInjector abstract fun contributesDanaRExecutionService(): DanaRExecutionService @ContributesAndroidInjector abstract fun contributesDanaRKoreanExecutionService(): DanaRKoreanExecutionService @ContributesAndroidInjector abstract fun contributesWatchUpdaterService(): WatchUpdaterService + @ContributesAndroidInjector abstract fun contributesInsightAlertService(): InsightAlertService + @ContributesAndroidInjector abstract fun contributesInsightConnectionService(): InsightConnectionService } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStop.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStop.kt index e4ee2486c5..bdabef46da 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStop.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStop.kt @@ -30,7 +30,7 @@ class MsgBolusStop( bolusingEvent.status = resourceHelper.gs(R.string.overview_bolusprogress_delivered) bolusingEvent.percent = 100 } else { - bolusingEvent.status = MainApp.gs(R.string.overview_bolusprogress_stoped) + bolusingEvent.status = resourceHelper.gs(R.string.overview_bolusprogress_stoped) } rxBus.send(bolusingEvent) } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/InsightAlertService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/InsightAlertService.java index e5afd2ba08..2f8b451e0a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/InsightAlertService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/InsightAlertService.java @@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.pump.insight; import android.app.Notification; import android.app.PendingIntent; -import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; @@ -16,12 +15,12 @@ import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.lifecycle.MutableLiveData; -import org.slf4j.Logger; +import javax.inject.Inject; -import info.nightscout.androidaps.MainApp; +import dagger.android.DaggerService; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; +import info.nightscout.androidaps.logging.AAPSLogger; +import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.pump.insight.activities.InsightAlertActivity; import info.nightscout.androidaps.plugins.pump.insight.app_layer.remote_control.ConfirmAlertMessage; import info.nightscout.androidaps.plugins.pump.insight.app_layer.remote_control.SnoozeAlertMessage; @@ -33,12 +32,16 @@ import info.nightscout.androidaps.plugins.pump.insight.descriptors.AlertType; import info.nightscout.androidaps.plugins.pump.insight.descriptors.InsightState; 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.utils.AlertUtilsKt; +import info.nightscout.androidaps.plugins.pump.insight.utils.AlertUtils; import info.nightscout.androidaps.plugins.pump.insight.utils.ExceptionTranslator; +import info.nightscout.androidaps.utils.resources.ResourceHelper; -public class InsightAlertService extends Service implements InsightConnectionService.StateCallback { +public class InsightAlertService extends DaggerService implements InsightConnectionService.StateCallback { + + @Inject AAPSLogger aapsLogger; + @Inject ResourceHelper resourceHelper; + @Inject AlertUtils alertUtils; - private static Logger log = StacktraceLoggerWrapper.getLogger(L.PUMPCOMM); private static final int NOTIFICATION_ID = 31345; private LocalBinder localBinder = new LocalBinder(); @@ -91,6 +94,7 @@ public class InsightAlertService extends Service implements InsightConnectionSer @Override public void onCreate() { + super.onCreate(); vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); bindService(new Intent(this, InsightConnectionService.class), serviceConnection, BIND_AUTO_CREATE); alertLiveData.setValue(null); @@ -189,11 +193,11 @@ public class InsightAlertService extends Service implements InsightConnectionSer connectionService.withdrawConnectionRequest(thread); break; } catch (AppLayerErrorException e) { - log.info("Exception while fetching alert: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); + aapsLogger.info(LTag.PUMP, "Exception while fetching alert: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); } catch (InsightException e) { - log.info("Exception while fetching alert: " + e.getClass().getSimpleName()); + aapsLogger.info(LTag.PUMP, "Exception while fetching alert: " + e.getClass().getSimpleName()); } catch (Exception e) { - log.error("Exception while fetching alert", e); + aapsLogger.error(LTag.PUMP, "Exception while fetching alert", e); } try { Thread.sleep(1000); @@ -240,13 +244,13 @@ public class InsightAlertService extends Service implements InsightConnectionSer connectionService.requestMessage(snoozeAlertMessage).await(); } } catch (AppLayerErrorException e) { - log.info("Exception while muting alert: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); + aapsLogger.info(LTag.PUMP, "Exception while muting alert: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); ExceptionTranslator.makeToast(InsightAlertService.this, e); } catch (InsightException e) { - log.info("Exception while muting alert: " + e.getClass().getSimpleName()); + aapsLogger.info(LTag.PUMP, "Exception while muting alert: " + e.getClass().getSimpleName()); ExceptionTranslator.makeToast(InsightAlertService.this, e); } catch (Exception e) { - log.error("Exception while muting alert", e); + aapsLogger.error(LTag.PUMP, "Exception while muting alert", e); ExceptionTranslator.makeToast(InsightAlertService.this, e); } }).start(); @@ -266,13 +270,13 @@ public class InsightAlertService extends Service implements InsightConnectionSer this.alert = null; } } catch (AppLayerErrorException e) { - log.info("Exception while confirming alert: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); + aapsLogger.info(LTag.PUMP, "Exception while confirming alert: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); ExceptionTranslator.makeToast(InsightAlertService.this, e); } catch (InsightException e) { - log.info("Exception while confirming alert: " + e.getClass().getSimpleName()); + aapsLogger.info(LTag.PUMP, "Exception while confirming alert: " + e.getClass().getSimpleName()); ExceptionTranslator.makeToast(InsightAlertService.this, e); } catch (Exception e) { - log.error("Exception while confirming alert", e); + aapsLogger.error(LTag.PUMP, "Exception while confirming alert", e); ExceptionTranslator.makeToast(InsightAlertService.this, e); } }).start(); @@ -288,10 +292,10 @@ public class InsightAlertService extends Service implements InsightConnectionSer notificationBuilder.setOngoing(true); notificationBuilder.setOnlyAlertOnce(true); notificationBuilder.setAutoCancel(false); - notificationBuilder.setSmallIcon(AlertUtilsKt.getAlertIcon(alert.getAlertCategory())); + notificationBuilder.setSmallIcon(alertUtils.getAlertIcon(alert.getAlertCategory())); - notificationBuilder.setContentTitle(AlertUtilsKt.getAlertCode(alert.getAlertType()) + " – " + AlertUtilsKt.getAlertTitle(alert.getAlertType())); - String description = AlertUtilsKt.getAlertDescription(alert); + notificationBuilder.setContentTitle(alertUtils.getAlertCode(alert.getAlertType()) + " – " + alertUtils.getAlertTitle(alert.getAlertType())); + String description = alertUtils.getAlertDescription(alert); if (description != null) notificationBuilder.setContentText(Html.fromHtml(description).toString()); @@ -303,11 +307,11 @@ public class InsightAlertService extends Service implements InsightConnectionSer case ACTIVE: Intent muteIntent = new Intent(this, InsightAlertService.class).putExtra("command", "mute"); PendingIntent mutePendingIntent = PendingIntent.getService(this, 1, muteIntent, PendingIntent.FLAG_UPDATE_CURRENT); - notificationBuilder.addAction(0, MainApp.gs(R.string.mute_alert), mutePendingIntent); + notificationBuilder.addAction(0, resourceHelper.gs(R.string.mute_alert), mutePendingIntent); case SNOOZED: Intent confirmIntent = new Intent(this, InsightAlertService.class).putExtra("command", "confirm"); PendingIntent confirmPendingIntent = PendingIntent.getService(this, 2, confirmIntent, PendingIntent.FLAG_UPDATE_CURRENT); - notificationBuilder.addAction(0, MainApp.gs(R.string.confirm), confirmPendingIntent); + notificationBuilder.addAction(0, resourceHelper.gs(R.string.confirm), confirmPendingIntent); } Notification notification = notificationBuilder.build(); 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 a9221cac0d..603e8aabb9 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 @@ -43,6 +43,7 @@ public class LocalInsightFragment extends DaggerFragment implements View.OnClick @Inject CommandQueue commandQueue; @Inject RxBusWrapper rxBus; @Inject ResourceHelper resourceHelper; + @Inject FabricPrivacy fabricPrivacy; private CompositeDisposable disposable = new CompositeDisposable(); @@ -79,7 +80,7 @@ public class LocalInsightFragment extends DaggerFragment implements View.OnClick disposable.add(rxBus .toObservable(EventLocalInsightUpdateGUI.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/insight/LocalInsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java index 567ea448d9..a75398554e 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 @@ -53,7 +53,6 @@ import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.bus.RxBusWrapper; import info.nightscout.androidaps.plugins.common.ManufacturerType; -import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction; import info.nightscout.androidaps.plugins.general.actions.defs.CustomAction; import info.nightscout.androidaps.plugins.general.actions.defs.CustomActionType; @@ -517,17 +516,17 @@ public class LocalInsightPlugin extends PumpPluginBase implements PumpInterface, aapsLogger.info(LTag.PUMP, "Exception while setting profile: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE, resourceHelper.gs(R.string.failedupdatebasalprofile), Notification.URGENT); rxBus.send(new EventNewNotification(notification)); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (InsightException e) { aapsLogger.info(LTag.PUMP, "Exception while setting profile: " + e.getClass().getCanonicalName()); Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE, resourceHelper.gs(R.string.failedupdatebasalprofile), Notification.URGENT); rxBus.send(new EventNewNotification(notification)); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (Exception e) { aapsLogger.error("Exception while setting profile", e); Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE, resourceHelper.gs(R.string.failedupdatebasalprofile), Notification.URGENT); rxBus.send(new EventNewNotification(notification)); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } return result; } @@ -658,13 +657,13 @@ public class LocalInsightPlugin extends PumpPluginBase implements PumpInterface, fetchStatus(); } catch (AppLayerErrorException e) { aapsLogger.info(LTag.PUMP, "Exception while delivering bolus: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (InsightException e) { aapsLogger.info(LTag.PUMP, "Exception while delivering bolus: " + e.getClass().getCanonicalName()); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (Exception e) { aapsLogger.error("Exception while delivering bolus", e); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } } else if (detailedBolusInfo.carbs > 0) { result.success = true; @@ -775,13 +774,13 @@ public class LocalInsightPlugin extends PumpPluginBase implements PumpInterface, fetchStatus(); } catch (AppLayerErrorException e) { aapsLogger.info(LTag.PUMP, "Exception while setting TBR: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (InsightException e) { aapsLogger.info(LTag.PUMP, "Exception while setting TBR: " + e.getClass().getCanonicalName()); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (Exception e) { aapsLogger.error("Exception while setting TBR", e); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } return result; } @@ -829,13 +828,13 @@ public class LocalInsightPlugin extends PumpPluginBase implements PumpInterface, result.comment = resourceHelper.gs(R.string.virtualpump_resultok); } catch (AppLayerErrorException e) { aapsLogger.info(LTag.PUMP, "Exception while delivering extended bolus: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (InsightException e) { aapsLogger.info(LTag.PUMP, "Exception while delivering extended bolus: " + e.getClass().getCanonicalName()); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (Exception e) { aapsLogger.error("Exception while delivering extended bolus", e); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } return result; } @@ -878,13 +877,13 @@ public class LocalInsightPlugin extends PumpPluginBase implements PumpInterface, result.comment = resourceHelper.gs(R.string.virtualpump_resultok); } catch (AppLayerErrorException e) { aapsLogger.info(LTag.PUMP, "Exception while canceling TBR: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (InsightException e) { aapsLogger.info(LTag.PUMP, "Exception while canceling TBR: " + e.getClass().getCanonicalName()); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (Exception e) { aapsLogger.error("Exception while canceling TBR", e); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } return result; } @@ -940,13 +939,13 @@ public class LocalInsightPlugin extends PumpPluginBase implements PumpInterface, result.comment = resourceHelper.gs(R.string.virtualpump_resultok); } catch (AppLayerErrorException e) { aapsLogger.info(LTag.PUMP, "Exception while canceling extended bolus: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (InsightException e) { aapsLogger.info(LTag.PUMP, "Exception while canceling extended bolus: " + e.getClass().getCanonicalName()); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (Exception e) { aapsLogger.error("Exception while canceling extended bolus", e); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } return result; } @@ -1050,13 +1049,13 @@ public class LocalInsightPlugin extends PumpPluginBase implements PumpInterface, readHistory(); } catch (AppLayerErrorException e) { aapsLogger.info(LTag.PUMP, "Exception while stopping pump: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (InsightException e) { aapsLogger.info(LTag.PUMP, "Exception while stopping pump: " + e.getClass().getCanonicalName()); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (Exception e) { aapsLogger.error("Exception while stopping pump", e); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } return result; } @@ -1073,13 +1072,13 @@ public class LocalInsightPlugin extends PumpPluginBase implements PumpInterface, readHistory(); } catch (AppLayerErrorException e) { aapsLogger.info(LTag.PUMP, "Exception while starting pump: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (InsightException e) { aapsLogger.info(LTag.PUMP, "Exception while starting pump: " + e.getClass().getCanonicalName()); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (Exception e) { aapsLogger.error("Exception while starting pump", e); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } return result; } @@ -1095,15 +1094,15 @@ public class LocalInsightPlugin extends PumpPluginBase implements PumpInterface, } catch (AppLayerErrorException e) { tbrOverNotificationBlock.setEnabled(valueBefore); aapsLogger.info(LTag.PUMP, "Exception while updating TBR notification block: " + e.getClass().getCanonicalName() + " (" + e.getErrorCode() + ")"); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (InsightException e) { tbrOverNotificationBlock.setEnabled(valueBefore); aapsLogger.info(LTag.PUMP, "Exception while updating TBR notification block: " + e.getClass().getSimpleName()); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } catch (Exception e) { tbrOverNotificationBlock.setEnabled(valueBefore); aapsLogger.error("Exception while updating TBR notification block", e); - result.comment = ExceptionTranslator.getString(e); + result.comment = ExceptionTranslator.getString(context, e); } return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightAlertActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightAlertActivity.java index 8ebd05801b..65034d321a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightAlertActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightAlertActivity.java @@ -12,17 +12,20 @@ import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; -import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; +import javax.inject.Inject; + +import dagger.android.support.DaggerAppCompatActivity; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; import info.nightscout.androidaps.plugins.pump.insight.InsightAlertService; import info.nightscout.androidaps.plugins.pump.insight.descriptors.Alert; import info.nightscout.androidaps.plugins.pump.insight.descriptors.AlertStatus; -import info.nightscout.androidaps.plugins.pump.insight.utils.AlertUtilsKt; +import info.nightscout.androidaps.plugins.pump.insight.utils.AlertUtils; -public class InsightAlertActivity extends AppCompatActivity { +public class InsightAlertActivity extends DaggerAppCompatActivity { + + @Inject AlertUtils alertUtils; private InsightAlertService alertService; @@ -81,10 +84,10 @@ public class InsightAlertActivity extends AppCompatActivity { mute.setEnabled(true); mute.setVisibility(alert.getAlertStatus() == AlertStatus.SNOOZED ? View.GONE : View.VISIBLE); confirm.setEnabled(true); - this.icon.setImageDrawable(ContextCompat.getDrawable(this, AlertUtilsKt.getAlertIcon(alert.getAlertCategory()))); - this.errorCode.setText(AlertUtilsKt.getAlertCode(alert.getAlertType())); - this.errorTitle.setText(AlertUtilsKt.getAlertTitle(alert.getAlertType())); - String description = AlertUtilsKt.getAlertDescription(alert); + this.icon.setImageDrawable(ContextCompat.getDrawable(this, alertUtils.getAlertIcon(alert.getAlertCategory()))); + this.errorCode.setText(alertUtils.getAlertCode(alert.getAlertType())); + this.errorTitle.setText(alertUtils.getAlertTitle(alert.getAlertType())); + String description = alertUtils.getAlertDescription(alert); if (description == null) this.errorDescription.setVisibility(View.GONE); else { this.errorDescription.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/connection_service/InsightConnectionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/connection_service/InsightConnectionService.java index ab988b879f..a646cc9f05 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/connection_service/InsightConnectionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/connection_service/InsightConnectionService.java @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.pump.insight.connection_service; -import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; @@ -8,10 +7,9 @@ import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.os.PowerManager; + import androidx.annotation.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.spongycastle.crypto.InvalidCipherTextException; import java.io.IOException; @@ -19,8 +17,11 @@ import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; +import javax.inject.Inject; + +import dagger.android.DaggerService; +import info.nightscout.androidaps.logging.AAPSLogger; +import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.pump.insight.app_layer.AppLayerMessage; import info.nightscout.androidaps.plugins.pump.insight.app_layer.ReadParameterBlockMessage; import info.nightscout.androidaps.plugins.pump.insight.app_layer.configuration.CloseConfigurationWriteSessionMessage; @@ -83,11 +84,12 @@ import info.nightscout.androidaps.plugins.pump.insight.utils.PairingDataStorage; import info.nightscout.androidaps.plugins.pump.insight.utils.crypto.Cryptograph; import info.nightscout.androidaps.plugins.pump.insight.utils.crypto.DerivedKeys; import info.nightscout.androidaps.plugins.pump.insight.utils.crypto.KeyPair; -import info.nightscout.androidaps.utils.SP; +import info.nightscout.androidaps.utils.sharedPreferences.SP; -public class InsightConnectionService extends Service implements ConnectionEstablisher.Callback, InputStreamReader.Callback, OutputStreamWriter.Callback { +public class InsightConnectionService extends DaggerService implements ConnectionEstablisher.Callback, InputStreamReader.Callback, OutputStreamWriter.Callback { - private static Logger log = StacktraceLoggerWrapper.getLogger(L.PUMPCOMM); + @Inject AAPSLogger aapsLogger; + @Inject SP sp; private static final int BUFFER_SIZE = 1024; private static final int TIMEOUT_DURING_HANDSHAKE_NOTIFICATION_THRESHOLD = 3; @@ -139,10 +141,10 @@ public class InsightConnectionService extends Service implements ConnectionEstab } private void increaseRecoveryDuration() { - long maxRecoveryDuration = SP.getInt("insight_max_recovery_duration", 20); + long maxRecoveryDuration = sp.getInt("insight_max_recovery_duration", 20); maxRecoveryDuration = Math.min(maxRecoveryDuration, 20); maxRecoveryDuration = Math.max(maxRecoveryDuration, 0); - long minRecoveryDuration = SP.getInt("insight_min_recovery_duration", 5); + long minRecoveryDuration = sp.getInt("insight_min_recovery_duration", 5); minRecoveryDuration = Math.min(minRecoveryDuration, 20); minRecoveryDuration = Math.max(minRecoveryDuration, 0); recoveryDuration += 1000; @@ -252,6 +254,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab @Override public synchronized void onCreate() { + super.onCreate(); pairingDataStorage = new PairingDataStorage(this); state = pairingDataStorage.isPaired() ? InsightState.DISCONNECTED : InsightState.NOT_PAIRED; wakeLock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AndroidAPS:InsightConnectionService"); @@ -265,7 +268,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab else if (!wakeLock.isHeld()) wakeLock.acquire(); this.state = state; for (StateCallback stateCallback : stateCallbacks) stateCallback.onStateChanged(state); - log.info("Insight state changed: " + state.name()); + aapsLogger.info(LTag.PUMP, "Insight state changed: " + state.name()); } public synchronized void requestConnection(Object lock) { @@ -292,10 +295,10 @@ public class InsightConnectionService extends Service implements ConnectionEstab setState(InsightState.DISCONNECTED); cleanup(true); } else if (state != InsightState.DISCONNECTED) { - long disconnectTimeout = SP.getInt("insight_disconnect_delay", 5); + long disconnectTimeout = sp.getInt("insight_disconnect_delay", 5); disconnectTimeout = Math.min(disconnectTimeout, 15); disconnectTimeout = Math.max(disconnectTimeout, 0); - log.info("Last connection lock released, will disconnect in " + disconnectTimeout + " seconds"); + aapsLogger.info(LTag.PUMP, "Last connection lock released, will disconnect in " + disconnectTimeout + " seconds"); disconnectTimer = DelayedActionThread.runDelayed("Disconnect Timer", disconnectTimeout * 1000, this::disconnect); } } @@ -354,7 +357,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab case RECOVERING: return; } - log.info("Exception occurred: " + e.getClass().getSimpleName()); + aapsLogger.info(LTag.PUMP, "Exception occurred: " + e.getClass().getSimpleName()); if (pairingDataStorage.isPaired()) { if (e instanceof TimeoutException && (state == InsightState.SATL_SYN_REQUEST || state == InsightState.APP_CONNECT_MESSAGE)) { if (++timeoutDuringHandshakeCounter == TIMEOUT_DURING_HANDSHAKE_NOTIFICATION_THRESHOLD) { @@ -412,7 +415,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab throw new IllegalStateException("Pump must be unbonded first."); if (connectionRequests.size() == 0) throw new IllegalStateException("A connection lock must be hold for pairing"); - log.info("Pairing initiated"); + aapsLogger.info(LTag.PUMP, "Pairing initiated"); cleanup(true); pairingDataStorage.setMacAddress(macAddress); connect(); @@ -743,7 +746,7 @@ public class InsightConnectionService extends Service implements ConnectionEstab SystemIdentification systemIdentification = ((SystemIdentificationBlock) message.getParameterBlock()).getSystemIdentification(); pairingDataStorage.setSystemIdentification(systemIdentification); pairingDataStorage.setPaired(true); - log.info("Pairing completed YEE-HAW ♪ ┏(・o・)┛ ♪ ┗( ・o・)┓ ♪"); + aapsLogger.info(LTag.PUMP, "Pairing completed YEE-HAW ♪ ┏(・o・)┛ ♪ ┗( ・o・)┓ ♪"); setState(InsightState.CONNECTED); for (StateCallback stateCallback : stateCallbacks) stateCallback.onPumpPaired(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/AlertUtils.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/AlertUtils.kt index 7cafb950cc..8e26c318c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/AlertUtils.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/AlertUtils.kt @@ -1,107 +1,113 @@ package info.nightscout.androidaps.plugins.pump.insight.utils -import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.plugins.pump.insight.descriptors.Alert import info.nightscout.androidaps.plugins.pump.insight.descriptors.AlertCategory import info.nightscout.androidaps.plugins.pump.insight.descriptors.AlertType +import info.nightscout.androidaps.utils.resources.ResourceHelper import java.text.DecimalFormat +import javax.inject.Inject +import javax.inject.Singleton -fun getAlertCode(alertType: AlertType) = MainApp.gs(when (alertType) { - AlertType.REMINDER_01 -> R.string.alert_r1_code - AlertType.REMINDER_02 -> R.string.alert_r2_code - AlertType.REMINDER_03 -> R.string.alert_r3_code - AlertType.REMINDER_04 -> R.string.alert_r4_code - AlertType.REMINDER_07 -> R.string.alert_r7_code - AlertType.WARNING_31 -> R.string.alert_w31_code - AlertType.WARNING_32 -> R.string.alert_w32_code - AlertType.WARNING_33 -> R.string.alert_w33_code - AlertType.WARNING_34 -> R.string.alert_w34_code - AlertType.WARNING_36 -> R.string.alert_w36_code - AlertType.WARNING_38 -> R.string.alert_w38_code - AlertType.WARNING_39 -> R.string.alert_w39_code - AlertType.MAINTENANCE_20 -> R.string.alert_m20_code - AlertType.MAINTENANCE_21 -> R.string.alert_m21_code - AlertType.MAINTENANCE_22 -> R.string.alert_m22_code - AlertType.MAINTENANCE_23 -> R.string.alert_m23_code - AlertType.MAINTENANCE_24 -> R.string.alert_m24_code - AlertType.MAINTENANCE_25 -> R.string.alert_m25_code - AlertType.MAINTENANCE_26 -> R.string.alert_m26_code - AlertType.MAINTENANCE_27 -> R.string.alert_m27_code - AlertType.MAINTENANCE_28 -> R.string.alert_m28_code - AlertType.MAINTENANCE_29 -> R.string.alert_m29_code - AlertType.MAINTENANCE_30 -> R.string.alert_m30_code - AlertType.ERROR_6 -> R.string.alert_e6_code - AlertType.ERROR_10 -> R.string.alert_e10_code - AlertType.ERROR_13 -> R.string.alert_e13_code -}) +@Singleton +class AlertUtils @Inject constructor(private val resourceHelper: ResourceHelper) { -fun getAlertTitle(alertType: AlertType) = MainApp.gs(when (alertType) { - AlertType.REMINDER_01 -> R.string.alert_r1_title - AlertType.REMINDER_02 -> R.string.alert_r2_title - AlertType.REMINDER_03 -> R.string.alert_r3_title - AlertType.REMINDER_04 -> R.string.alert_r4_title - AlertType.REMINDER_07 -> R.string.alert_r7_title - AlertType.WARNING_31 -> R.string.alert_w31_title - AlertType.WARNING_32 -> R.string.alert_w32_title - AlertType.WARNING_33 -> R.string.alert_w33_title - AlertType.WARNING_34 -> R.string.alert_w34_title - AlertType.WARNING_36 -> R.string.alert_w36_title - AlertType.WARNING_38 -> R.string.alert_w38_title - AlertType.WARNING_39 -> R.string.alert_w39_title - AlertType.MAINTENANCE_20 -> R.string.alert_m20_title - AlertType.MAINTENANCE_21 -> R.string.alert_m21_title - AlertType.MAINTENANCE_22 -> R.string.alert_m22_title - AlertType.MAINTENANCE_23 -> R.string.alert_m23_title - AlertType.MAINTENANCE_24 -> R.string.alert_m24_title - AlertType.MAINTENANCE_25 -> R.string.alert_m25_title - AlertType.MAINTENANCE_26 -> R.string.alert_m26_title - AlertType.MAINTENANCE_27 -> R.string.alert_m27_title - AlertType.MAINTENANCE_28 -> R.string.alert_m28_title - AlertType.MAINTENANCE_29 -> R.string.alert_m29_title - AlertType.MAINTENANCE_30 -> R.string.alert_m30_title - AlertType.ERROR_6 -> R.string.alert_e6_title - AlertType.ERROR_10 -> R.string.alert_e10_title - AlertType.ERROR_13 -> R.string.alert_e13_title -}) + fun getAlertCode(alertType: AlertType) = resourceHelper.gs(when (alertType) { + AlertType.REMINDER_01 -> R.string.alert_r1_code + AlertType.REMINDER_02 -> R.string.alert_r2_code + AlertType.REMINDER_03 -> R.string.alert_r3_code + AlertType.REMINDER_04 -> R.string.alert_r4_code + AlertType.REMINDER_07 -> R.string.alert_r7_code + AlertType.WARNING_31 -> R.string.alert_w31_code + AlertType.WARNING_32 -> R.string.alert_w32_code + AlertType.WARNING_33 -> R.string.alert_w33_code + AlertType.WARNING_34 -> R.string.alert_w34_code + AlertType.WARNING_36 -> R.string.alert_w36_code + AlertType.WARNING_38 -> R.string.alert_w38_code + AlertType.WARNING_39 -> R.string.alert_w39_code + AlertType.MAINTENANCE_20 -> R.string.alert_m20_code + AlertType.MAINTENANCE_21 -> R.string.alert_m21_code + AlertType.MAINTENANCE_22 -> R.string.alert_m22_code + AlertType.MAINTENANCE_23 -> R.string.alert_m23_code + AlertType.MAINTENANCE_24 -> R.string.alert_m24_code + AlertType.MAINTENANCE_25 -> R.string.alert_m25_code + AlertType.MAINTENANCE_26 -> R.string.alert_m26_code + AlertType.MAINTENANCE_27 -> R.string.alert_m27_code + AlertType.MAINTENANCE_28 -> R.string.alert_m28_code + AlertType.MAINTENANCE_29 -> R.string.alert_m29_code + AlertType.MAINTENANCE_30 -> R.string.alert_m30_code + AlertType.ERROR_6 -> R.string.alert_e6_code + AlertType.ERROR_10 -> R.string.alert_e10_code + AlertType.ERROR_13 -> R.string.alert_e13_code + }) -fun getAlertDescription(alert: Alert): String? { - val decimalFormat = DecimalFormat("##0.00") - val hours = alert.tbrDuration / 60 - val minutes = alert.tbrDuration - hours * 60 - return when (alert.alertType!!) { - AlertType.REMINDER_01 -> null - AlertType.REMINDER_02 -> null - AlertType.REMINDER_03 -> null - AlertType.REMINDER_04 -> null - AlertType.REMINDER_07 -> MainApp.gs(R.string.alert_r7_description, alert.tbrAmount, DecimalFormat("#0").format(hours.toLong()) + ":" + DecimalFormat("00").format(minutes.toLong())) - AlertType.WARNING_31 -> MainApp.gs(R.string.alert_w31_description, decimalFormat.format(alert.cartridgeAmount)) - AlertType.WARNING_32 -> MainApp.gs(R.string.alert_w32_description) - AlertType.WARNING_33 -> MainApp.gs(R.string.alert_w33_description) - AlertType.WARNING_34 -> MainApp.gs(R.string.alert_w34_description) - AlertType.WARNING_36 -> MainApp.gs(R.string.alert_w36_description, alert.tbrAmount, DecimalFormat("#0").format(hours.toLong()) + ":" + DecimalFormat("00").format(minutes.toLong())) - AlertType.WARNING_38 -> MainApp.gs(R.string.alert_w38_description, decimalFormat.format(alert.programmedBolusAmount), decimalFormat.format(alert.deliveredBolusAmount)) - AlertType.WARNING_39 -> null - AlertType.MAINTENANCE_20 -> MainApp.gs(R.string.alert_m20_description) - AlertType.MAINTENANCE_21 -> MainApp.gs(R.string.alert_m21_description) - AlertType.MAINTENANCE_22 -> MainApp.gs(R.string.alert_m22_description) - AlertType.MAINTENANCE_23 -> MainApp.gs(R.string.alert_m23_description) - AlertType.MAINTENANCE_24 -> MainApp.gs(R.string.alert_m24_description) - AlertType.MAINTENANCE_25 -> MainApp.gs(R.string.alert_m25_description) - AlertType.MAINTENANCE_26 -> MainApp.gs(R.string.alert_m26_description) - AlertType.MAINTENANCE_27 -> MainApp.gs(R.string.alert_m27_description) - AlertType.MAINTENANCE_28 -> MainApp.gs(R.string.alert_m28_description) - AlertType.MAINTENANCE_29 -> MainApp.gs(R.string.alert_m29_description) - AlertType.MAINTENANCE_30 -> MainApp.gs(R.string.alert_m30_description) - AlertType.ERROR_6 -> MainApp.gs(R.string.alert_e6_description) - AlertType.ERROR_10 -> MainApp.gs(R.string.alert_e10_description) - AlertType.ERROR_13 -> MainApp.gs(R.string.alert_e13_description) + fun getAlertTitle(alertType: AlertType) = resourceHelper.gs(when (alertType) { + AlertType.REMINDER_01 -> R.string.alert_r1_title + AlertType.REMINDER_02 -> R.string.alert_r2_title + AlertType.REMINDER_03 -> R.string.alert_r3_title + AlertType.REMINDER_04 -> R.string.alert_r4_title + AlertType.REMINDER_07 -> R.string.alert_r7_title + AlertType.WARNING_31 -> R.string.alert_w31_title + AlertType.WARNING_32 -> R.string.alert_w32_title + AlertType.WARNING_33 -> R.string.alert_w33_title + AlertType.WARNING_34 -> R.string.alert_w34_title + AlertType.WARNING_36 -> R.string.alert_w36_title + AlertType.WARNING_38 -> R.string.alert_w38_title + AlertType.WARNING_39 -> R.string.alert_w39_title + AlertType.MAINTENANCE_20 -> R.string.alert_m20_title + AlertType.MAINTENANCE_21 -> R.string.alert_m21_title + AlertType.MAINTENANCE_22 -> R.string.alert_m22_title + AlertType.MAINTENANCE_23 -> R.string.alert_m23_title + AlertType.MAINTENANCE_24 -> R.string.alert_m24_title + AlertType.MAINTENANCE_25 -> R.string.alert_m25_title + AlertType.MAINTENANCE_26 -> R.string.alert_m26_title + AlertType.MAINTENANCE_27 -> R.string.alert_m27_title + AlertType.MAINTENANCE_28 -> R.string.alert_m28_title + AlertType.MAINTENANCE_29 -> R.string.alert_m29_title + AlertType.MAINTENANCE_30 -> R.string.alert_m30_title + AlertType.ERROR_6 -> R.string.alert_e6_title + AlertType.ERROR_10 -> R.string.alert_e10_title + AlertType.ERROR_13 -> R.string.alert_e13_title + }) + + fun getAlertDescription(alert: Alert): String? { + val decimalFormat = DecimalFormat("##0.00") + val hours = alert.tbrDuration / 60 + val minutes = alert.tbrDuration - hours * 60 + return when (alert.alertType!!) { + AlertType.REMINDER_01 -> null + AlertType.REMINDER_02 -> null + AlertType.REMINDER_03 -> null + AlertType.REMINDER_04 -> null + AlertType.REMINDER_07 -> resourceHelper.gs(R.string.alert_r7_description, alert.tbrAmount, DecimalFormat("#0").format(hours.toLong()) + ":" + DecimalFormat("00").format(minutes.toLong())) + AlertType.WARNING_31 -> resourceHelper.gs(R.string.alert_w31_description, decimalFormat.format(alert.cartridgeAmount)) + AlertType.WARNING_32 -> resourceHelper.gs(R.string.alert_w32_description) + AlertType.WARNING_33 -> resourceHelper.gs(R.string.alert_w33_description) + AlertType.WARNING_34 -> resourceHelper.gs(R.string.alert_w34_description) + AlertType.WARNING_36 -> resourceHelper.gs(R.string.alert_w36_description, alert.tbrAmount, DecimalFormat("#0").format(hours.toLong()) + ":" + DecimalFormat("00").format(minutes.toLong())) + AlertType.WARNING_38 -> resourceHelper.gs(R.string.alert_w38_description, decimalFormat.format(alert.programmedBolusAmount), decimalFormat.format(alert.deliveredBolusAmount)) + AlertType.WARNING_39 -> null + AlertType.MAINTENANCE_20 -> resourceHelper.gs(R.string.alert_m20_description) + AlertType.MAINTENANCE_21 -> resourceHelper.gs(R.string.alert_m21_description) + AlertType.MAINTENANCE_22 -> resourceHelper.gs(R.string.alert_m22_description) + AlertType.MAINTENANCE_23 -> resourceHelper.gs(R.string.alert_m23_description) + AlertType.MAINTENANCE_24 -> resourceHelper.gs(R.string.alert_m24_description) + AlertType.MAINTENANCE_25 -> resourceHelper.gs(R.string.alert_m25_description) + AlertType.MAINTENANCE_26 -> resourceHelper.gs(R.string.alert_m26_description) + AlertType.MAINTENANCE_27 -> resourceHelper.gs(R.string.alert_m27_description) + AlertType.MAINTENANCE_28 -> resourceHelper.gs(R.string.alert_m28_description) + AlertType.MAINTENANCE_29 -> resourceHelper.gs(R.string.alert_m29_description) + AlertType.MAINTENANCE_30 -> resourceHelper.gs(R.string.alert_m30_description) + AlertType.ERROR_6 -> resourceHelper.gs(R.string.alert_e6_description) + AlertType.ERROR_10 -> resourceHelper.gs(R.string.alert_e10_description) + AlertType.ERROR_13 -> resourceHelper.gs(R.string.alert_e13_description) + } } -} -fun getAlertIcon(alertCategory: AlertCategory) = when (alertCategory) { - AlertCategory.ERROR -> R.drawable.ic_error - AlertCategory.MAINTENANCE -> R.drawable.ic_maintenance - AlertCategory.WARNING -> R.drawable.ic_warning - AlertCategory.REMINDER -> R.drawable.ic_reminder + fun getAlertIcon(alertCategory: AlertCategory) = when (alertCategory) { + AlertCategory.ERROR -> R.drawable.ic_error + AlertCategory.MAINTENANCE -> R.drawable.ic_maintenance + AlertCategory.WARNING -> R.drawable.ic_warning + AlertCategory.REMINDER -> R.drawable.ic_reminder + } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/ExceptionTranslator.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/ExceptionTranslator.java index 7eda84d01b..d756a803f0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/ExceptionTranslator.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/ExceptionTranslator.java @@ -8,7 +8,6 @@ import android.widget.Toast; import java.util.HashMap; import java.util.Map; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.pump.insight.exceptions.ConnectionFailedException; import info.nightscout.androidaps.plugins.pump.insight.exceptions.ConnectionLostException; @@ -44,12 +43,12 @@ public class ExceptionTranslator { TABLE.put(RunModeNotAllowedException.class, R.string.run_mode_not_allowed); } - public static String getString(Exception exception) { + public static String getString(Context context, Exception exception) { Integer res = TABLE.get(exception.getClass()); - return res == null ? exception.getClass().getSimpleName() : MainApp.gs(res); + return res == null ? exception.getClass().getSimpleName() : context.getString(res); } public static void makeToast(Context context, Exception exception) { - new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, getString(exception), Toast.LENGTH_LONG).show()); + new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, getString(context, exception), Toast.LENGTH_LONG).show()); } }