diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000000..48bbf82e58 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,137 @@ + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000000..79ee123c2b --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a35b2fabe9..b571e247c2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -51,12 +51,11 @@ - @@ -124,16 +123,6 @@ - - - - - - - diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index c0ebb847b3..ff33ec930f 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -42,7 +42,6 @@ import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; import info.nightscout.androidaps.activities.PreferencesActivity; import info.nightscout.androidaps.activities.SingleFragmentActivity; import info.nightscout.androidaps.activities.StatsActivity; -import info.nightscout.androidaps.activities.SurveyActivity; import info.nightscout.androidaps.events.EventAppExit; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRebuildTabs; @@ -58,6 +57,7 @@ import info.nightscout.androidaps.tabs.TabPageAdapter; 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.PasswordProtection; import info.nightscout.androidaps.utils.SP; import io.reactivex.android.schedulers.AndroidSchedulers; @@ -239,10 +239,7 @@ public class MainActivity extends NoSplashAppCompatActivity { switch (requestCode) { case AndroidPermission.CASE_STORAGE: //show dialog after permission is granted - AlertDialog.Builder alert = new AlertDialog.Builder(this); - alert.setMessage(R.string.alert_dialog_storage_permission_text); - alert.setPositiveButton(R.string.ok, null); - alert.show(); + OKDialog.show(this, "", MainApp.gs(R.string.alert_dialog_storage_permission_text)); break; case AndroidPermission.CASE_LOCATION: case AndroidPermission.CASE_SMS: diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 5173377b33..9933f75ac9 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -49,13 +49,9 @@ import info.nightscout.androidaps.plugins.general.maintenance.LoggerUtils; import info.nightscout.androidaps.plugins.general.maintenance.MaintenancePlugin; import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; -import info.nightscout.androidaps.plugins.general.nsclient.receivers.AckAlarmReceiver; -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.smsCommunicator.SmsCommunicatorPlugin; -import info.nightscout.androidaps.plugins.source.RandomBgPlugin; -import info.nightscout.androidaps.utils.ActivityMonitor; import info.nightscout.androidaps.plugins.general.wear.WearPlugin; import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin; import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin; @@ -77,6 +73,7 @@ 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.source.RandomBgPlugin; import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin; import info.nightscout.androidaps.plugins.source.SourceEversensePlugin; import info.nightscout.androidaps.plugins.source.SourceGlimpPlugin; @@ -91,6 +88,7 @@ import info.nightscout.androidaps.receivers.KeepAliveReceiver; import info.nightscout.androidaps.receivers.NSAlarmReceiver; 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.SP; @@ -115,8 +113,6 @@ public class MainApp extends Application { private static DataReceiver dataReceiver = new DataReceiver(); private static NSAlarmReceiver alarmReciever = new NSAlarmReceiver(); - private static AckAlarmReceiver ackAlarmReciever = new AckAlarmReceiver(); - private static DBAccessReceiver dbAccessReciever = new DBAccessReceiver(); private LocalBroadcastManager lbm; BroadcastReceiver btReceiver; TimeDateOrTZChangeReceiver timeDateOrTZChangeReceiver; @@ -297,12 +293,6 @@ public class MainApp extends Application { lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_CLEAR_ALARM)); lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_URGENT_ALARM)); - //register ack alarm - lbm.registerReceiver(ackAlarmReciever, new IntentFilter(Intents.ACTION_ACK_ALARM)); - - //register dbaccess - lbm.registerReceiver(dbAccessReciever, new IntentFilter(Intents.ACTION_DATABASE)); - this.timeDateOrTZChangeReceiver = new TimeDateOrTZChangeReceiver(); this.timeDateOrTZChangeReceiver.registerBroadcasts(this); diff --git a/app/src/main/java/info/nightscout/androidaps/activities/BolusProgressHelperActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/BolusProgressHelperActivity.kt new file mode 100644 index 0000000000..93a0725aaf --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/activities/BolusProgressHelperActivity.kt @@ -0,0 +1,14 @@ +package info.nightscout.androidaps.activities + +import android.os.Bundle +import info.nightscout.androidaps.dialogs.BolusProgressDialog + +class BolusProgressHelperActivity : NoSplashAppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + BolusProgressDialog() + .setHelperActivity(this) + .setInsulin(intent.getDoubleExtra("insulin", 0.0)) + .show(supportFragmentManager, "BolusProgress") + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/ErrorHelperActivity.kt similarity index 85% rename from app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.kt rename to app/src/main/java/info/nightscout/androidaps/activities/ErrorHelperActivity.kt index 15840d3499..8ab5c05425 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorHelperActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/ErrorHelperActivity.kt @@ -1,8 +1,8 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs +package info.nightscout.androidaps.activities import android.os.Bundle import info.nightscout.androidaps.R -import info.nightscout.androidaps.activities.NoSplashAppCompatActivity +import info.nightscout.androidaps.dialogs.ErrorDialog import info.nightscout.androidaps.plugins.general.nsclient.NSUpload import info.nightscout.androidaps.utils.SP 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 c0dbf43527..328f143836 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java @@ -89,7 +89,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre return; } 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); + OKDialog.show(this, MainApp.gs(R.string.configbuilder_sensitivity), MainApp.gs(R.string.sensitivity_warning)); } updatePrefSummary(myPreferenceFragment.findPreference(key)); } diff --git a/app/src/main/java/info/nightscout/androidaps/activities/SingleFragmentActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/SingleFragmentActivity.java deleted file mode 100644 index a9a72adf09..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/activities/SingleFragmentActivity.java +++ /dev/null @@ -1,59 +0,0 @@ -package info.nightscout.androidaps.activities; - -import android.content.Intent; -import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; - -import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; -import androidx.fragment.app.Fragment; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.utils.PasswordProtection; - -public class SingleFragmentActivity extends AppCompatActivity { - - private PluginBase plugin; - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_single_fragment); - - this.plugin = MainApp.getPluginsList().get(getIntent().getIntExtra("plugin", -1)); - setTitle(plugin.getName()); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - getSupportActionBar().setDisplayShowHomeEnabled(true); - - if (savedInstanceState == null) { - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, - Fragment.instantiate(this, plugin.pluginDescription.getFragmentClass())).commit(); - } - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - finish(); - return true; - } else if (item.getItemId() == R.id.nav_plugin_preferences) { - PasswordProtection.QueryPassword(this, R.string.settings_password, "settings_password", () -> { - Intent i = new Intent(this, PreferencesActivity.class); - i.putExtra("id", plugin.getPreferencesId()); - startActivity(i); - }, null); - return true; - } - return false; - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - if (plugin.getPreferencesId() != -1) - getMenuInflater().inflate(R.menu.menu_single_fragment, menu); - return super.onCreateOptionsMenu(menu); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/activities/SingleFragmentActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/SingleFragmentActivity.kt new file mode 100644 index 0000000000..3bf329debe --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/activities/SingleFragmentActivity.kt @@ -0,0 +1,53 @@ +package info.nightscout.androidaps.activities + +import android.content.Context +import android.content.Intent +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import androidx.appcompat.app.AppCompatActivity +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.interfaces.PluginBase +import info.nightscout.androidaps.utils.LocaleHelper +import info.nightscout.androidaps.utils.PasswordProtection + +class SingleFragmentActivity : AppCompatActivity() { + private var plugin: PluginBase? = null + public override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_single_fragment) + plugin = MainApp.getPluginsList()[intent.getIntExtra("plugin", -1)] + title = plugin?.name + supportActionBar?.setDisplayHomeAsUpEnabled(true) + supportActionBar?.setDisplayShowHomeEnabled(true) + if (savedInstanceState == null) { + supportFragmentManager.beginTransaction().replace(R.id.frame_layout, + supportFragmentManager.fragmentFactory.instantiate(ClassLoader.getSystemClassLoader(), plugin?.pluginDescription?.fragmentClass!!)).commit() + } + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + if (item.itemId == android.R.id.home) { + finish() + return true + } else if (item.itemId == R.id.nav_plugin_preferences) { + PasswordProtection.QueryPassword(this, R.string.settings_password, "settings_password", Runnable { + val i = Intent(this, PreferencesActivity::class.java) + i.putExtra("id", plugin?.preferencesId) + startActivity(i) + }, null) + return true + } + return false + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + if (plugin?.preferencesId != -1) menuInflater.inflate(R.menu.menu_single_fragment, menu) + return super.onCreateOptionsMenu(menu) + } + + public override fun attachBaseContext(newBase: Context) { + super.attachBaseContext(LocaleHelper.wrap(newBase)) + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/activities/StatsActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/StatsActivity.kt index 4877eea492..d0ac8e3f6f 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/StatsActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/StatsActivity.kt @@ -21,10 +21,10 @@ class StatsActivity : NoSplashAppCompatActivity() { ok.setOnClickListener { finish() } stats_reset.setOnClickListener { - OKDialog.showConfirmation(this, MainApp.gs(R.string.doyouwantresetstats)) { + OKDialog.showConfirmation(this, MainApp.gs(R.string.doyouwantresetstats), Runnable { ActivityMonitor.reset() recreate() - } + }) } } } diff --git a/app/src/main/java/info/nightscout/androidaps/activities/SurveyActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/SurveyActivity.kt index 74fe35f90f..bf07eda9af 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/SurveyActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/SurveyActivity.kt @@ -8,7 +8,7 @@ import info.nightscout.androidaps.R import info.nightscout.androidaps.data.defaultProfile.DefaultProfile import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions -import info.nightscout.androidaps.plugins.treatments.fragments.ProfileViewerDialog +import info.nightscout.androidaps.dialogs.ProfileViewerDialog import info.nightscout.androidaps.utils.* import kotlinx.android.synthetic.main.survey_activity.* import org.slf4j.LoggerFactory diff --git a/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java b/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java index f287e169ce..bec190a353 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java @@ -11,6 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.utils.DateUtil; /** * Created by mike on 27.02.2016. @@ -40,36 +41,49 @@ public class DbRequest { } // dbAdd - public DbRequest(String action, String collection, String nsClientID, JSONObject data) { + public DbRequest(String action, String collection, JSONObject data) { this.action = action; this.collection = collection; + this.nsClientID = "" + DateUtil.now(); + try { + data.put("NSCLIENT_ID", nsClientID); + } catch (JSONException e) { + e.printStackTrace(); + } this.data = data.toString(); - this.nsClientID = nsClientID; this._id = ""; } // dbUpdate, dbUpdateUnset - public DbRequest(String action, String collection, String nsClientID, String _id, JSONObject data) { + public DbRequest(String action, String collection, String _id, JSONObject data) { this.action = action; this.collection = collection; + this.nsClientID = "" + DateUtil.now(); + try { + data.put("NSCLIENT_ID", nsClientID); + } catch (JSONException e) { + e.printStackTrace(); + } this.data = data.toString(); - this.nsClientID = nsClientID; this._id = _id; } // dbRemove - public DbRequest(String action, String collection, String nsClientID, String _id) { + public DbRequest(String action, String collection, + String _id) { + JSONObject data = new JSONObject(); this.action = action; this.collection = collection; - this.data = new JSONObject().toString(); - this.nsClientID = nsClientID; + this.nsClientID = "" + DateUtil.now(); + try { + data.put("NSCLIENT_ID", nsClientID); + } catch (JSONException e) { + e.printStackTrace(); + } + this.data = data.toString(); this._id = _id; } - public String hash() { - return Hashing.sha1().hashString(action + collection + _id + data.toString(), Charsets.UTF_8).toString(); - } - public JSONObject toJSON() { JSONObject object = new JSONObject(); try { diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/BolusProgressDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/BolusProgressDialog.kt new file mode 100644 index 0000000000..6f3c048aa8 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/BolusProgressDialog.kt @@ -0,0 +1,159 @@ +package info.nightscout.androidaps.dialogs + +import android.app.Activity +import android.os.Bundle +import android.os.SystemClock +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.view.Window +import android.view.WindowManager +import androidx.fragment.app.DialogFragment +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.BolusProgressHelperActivity +import info.nightscout.androidaps.events.EventPumpStatusChanged +import info.nightscout.androidaps.logging.L +import info.nightscout.androidaps.plugins.bus.RxBus.toObservable +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +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 +import kotlinx.android.synthetic.main.dialog_bolusprogress.* +import org.slf4j.LoggerFactory + +class BolusProgressDialog : DialogFragment() { + private val log = LoggerFactory.getLogger(L.UI) + private val disposable = CompositeDisposable() + + companion object { + private val DEFAULT_STATE = MainApp.gs(R.string.waitingforpump) + @JvmField + var bolusEnded = false + @JvmField + var stopPressed = false + } + + private var running = true + private var amount = 0.0 + private var state: String? = null + private var helpActivity: BolusProgressHelperActivity? = null + + fun setInsulin(amount: Double): BolusProgressDialog { + this.amount = amount + bolusEnded = false + return this + } + + fun setHelperActivity(activity: BolusProgressHelperActivity): BolusProgressDialog { + helpActivity = activity + return this + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE) + dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) + isCancelable = true + dialog?.setCanceledOnTouchOutside(false) + return inflater.inflate(R.layout.dialog_bolusprogress, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + overview_bolusprogress_title.text = String.format(MainApp.gs(R.string.overview_bolusprogress_goingtodeliver), amount) + overview_bolusprogress_stop.setOnClickListener { + if (L.isEnabled(L.UI)) log.debug("Stop bolus delivery button pressed") + stopPressed = true + overview_bolusprogress_stoppressed.visibility = View.VISIBLE + overview_bolusprogress_stop.visibility = View.INVISIBLE + ConfigBuilderPlugin.getPlugin().commandQueue.cancelAllBoluses() + } + overview_bolusprogress_progressbar.max = 100 + state = savedInstanceState?.getString("state", DEFAULT_STATE) ?: DEFAULT_STATE + overview_bolusprogress_status.text = state + stopPressed = false + } + + override fun onStart() { + super.onStart() + dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + } + + override fun onResume() { + super.onResume() + if (L.isEnabled(L.UI)) log.debug("onResume") + if (!ConfigBuilderPlugin.getPlugin().commandQueue.bolusInQueue()) + bolusEnded = true + + if (bolusEnded) dismiss() + else running = true + + disposable.add(toObservable(EventPumpStatusChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ overview_bolusprogress_status.text = it.getStatus() }) { FabricPrivacy.logException(it) } + ) + disposable.add(toObservable(EventDismissBolusProgressIfRunning::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ if (running) dismiss() }) { FabricPrivacy.logException(it) } + ) + disposable.add(toObservable(EventOverviewBolusProgress::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + if (L.isEnabled(L.UI)) log.debug("Status: " + it.status + " Percent: " + it.percent) + overview_bolusprogress_status.text = it.status + overview_bolusprogress_progressbar.progress = it.percent + if (it.percent == 100) { + overview_bolusprogress_stop.visibility = View.INVISIBLE + scheduleDismiss() + } + state = it.status + }) { FabricPrivacy.logException(it) } + ) + } + + override fun dismiss() { + if (L.isEnabled(L.UI)) log.debug("dismiss") + try { + super.dismiss() + } catch (e: IllegalStateException) { + // dialog not running yet. onResume will try again. Set bolusEnded to make extra + // sure onResume will catch this + bolusEnded = true + log.error("Unhandled exception", e) + } + helpActivity?.finish() + } + + override fun onPause() { + super.onPause() + if (L.isEnabled(L.UI)) log.debug("onPause") + running = false + disposable.clear() + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putString("state", state) + } + + private fun scheduleDismiss() { + if (L.isEnabled(L.UI)) log.debug("scheduleDismiss") + Thread(Runnable { + SystemClock.sleep(5000) + bolusEnded = true + val activity: Activity? = activity + activity?.runOnUiThread { + if (running) { + if (L.isEnabled(L.UI)) log.debug("executing") + try { + dismiss() + } catch (e: Exception) { + log.error("Unhandled exception", e) + } + } + } + }).start() + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt new file mode 100644 index 0000000000..949e4e5fef --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt @@ -0,0 +1,68 @@ +package info.nightscout.androidaps.dialogs + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.google.common.base.Joiner +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.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus +import info.nightscout.androidaps.utils.HtmlHelper +import info.nightscout.androidaps.utils.OKDialog +import info.nightscout.androidaps.utils.XdripCalibrations +import kotlinx.android.synthetic.main.dialog_calibration.* +import kotlinx.android.synthetic.main.okcancel.* +import java.text.DecimalFormat +import java.util.* + +class CalibrationDialog : DialogFragmentWithDate() { + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("overview_calibration_bg", overview_calibration_bg.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateViewGeneral() + return inflater.inflate(R.layout.dialog_calibration, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + val units = ProfileFunctions.getSystemUnits() + val bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData()?.glucose + ?: 0.0, units) + if (units == Constants.MMOL) + overview_calibration_bg.setParams(savedInstanceState?.getDouble("overview_calibration_bg") + ?: bg, 2.0, 30.0, 0.1, DecimalFormat("0.0"), false, ok) + else + overview_calibration_bg.setParams(savedInstanceState?.getDouble("overview_calibration_bg") + ?: bg, 36.0, 500.0, 1.0, DecimalFormat("0"), false, ok) + overview_calibration_units.text = if (units == Constants.MMOL) MainApp.gs(R.string.mmol) else MainApp.gs(R.string.mgdl) + } + + override fun submit() :Boolean { + val units = ProfileFunctions.getSystemUnits() + val unitLabel = if (units == Constants.MMOL) MainApp.gs(R.string.mmol) else MainApp.gs(R.string.mgdl) + val actions: LinkedList = LinkedList() + val bg = overview_calibration_bg.value + actions.add(MainApp.gs(R.string.treatments_wizard_bg_label) + ": " + Profile.toCurrentUnitsString(bg) + " " + unitLabel) + if (bg > 0) { + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + XdripCalibrations.confirmAndSendCalibration(bg, context) + }) + } + } else + activity?.let { activity -> + OKDialog.show(activity, MainApp.gs(R.string.overview_calibration), MainApp.gs(R.string.no_action_selected)) + } + return true + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt new file mode 100644 index 0000000000..4e0e2980e8 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/CarbsDialog.kt @@ -0,0 +1,220 @@ +package info.nightscout.androidaps.dialogs + +import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.google.common.base.Joiner +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.CareportalEvent +import info.nightscout.androidaps.db.DatabaseHelper +import info.nightscout.androidaps.db.Source +import info.nightscout.androidaps.db.TempTarget +import info.nightscout.androidaps.interfaces.Constraint +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.nsclient.NSUpload +import info.nightscout.androidaps.plugins.treatments.CarbsGenerator +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.utils.* +import kotlinx.android.synthetic.main.dialog_carbs.* +import kotlinx.android.synthetic.main.notes.* +import kotlinx.android.synthetic.main.okcancel.* +import java.text.DecimalFormat +import java.util.* +import kotlin.math.max + +class CarbsDialog : DialogFragmentWithDate() { + + companion object { + private const val FAV1_DEFAULT = 5 + private const val FAV2_DEFAULT = 10 + private const val FAV3_DEFAULT = 20 + } + + private val maxCarbs = MainApp.getConstraintChecker().maxCarbsAllowed.value().toDouble() + + private val textWatcher: TextWatcher = object : TextWatcher { + override fun afterTextChanged(s: Editable) { + validateInputs() + } + + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} + } + + private fun validateInputs() { + val time = overview_carbs_time.value.toInt() + if (time > 12 * 60 || time < -12 * 60) { + overview_carbs_time.value = 0.0 + ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.constraintapllied)) + } + if (overview_carbs_duration.value > 10) { + overview_carbs_duration.value = 0.0 + ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.constraintapllied)) + } + if (overview_carbs_carbs.value.toInt() > maxCarbs) { + overview_carbs_carbs.value = 0.0 + ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.carbsconstraintapplied)) + } + } + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("overview_carbs_time", overview_carbs_time.value) + savedInstanceState.putDouble("overview_carbs_duration", overview_carbs_duration.value) + savedInstanceState.putDouble("overview_carbs_carbs", overview_carbs_carbs.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateViewGeneral() + return inflater.inflate(R.layout.dialog_carbs, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + overview_carbs_time.setParams(savedInstanceState?.getDouble("overview_carbs_time") + ?: 0.0, -12 * 60.0, 12 * 60.0, 5.0, DecimalFormat("0"), false, ok, textWatcher) + + overview_carbs_duration.setParams(savedInstanceState?.getDouble("overview_carbs_duration") + ?: 0.0, 0.0, 10.0, 1.0, DecimalFormat("0"), false, ok, textWatcher) + + overview_carbs_carbs.setParams(savedInstanceState?.getDouble("overview_carbs_carbs") + ?: 0.0, 0.0, maxCarbs, 1.0, DecimalFormat("0"), false, ok, textWatcher) + + overview_carbs_plus1.text = toSignedString(SP.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT)) + overview_carbs_plus1.setOnClickListener { + overview_carbs_carbs.value = max(0.0, overview_carbs_carbs.value + + SP.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT)) + validateInputs() + } + + overview_carbs_plus2.text = toSignedString(SP.getInt(R.string.key_carbs_button_increment_2, FAV2_DEFAULT)) + overview_carbs_plus2.setOnClickListener { + overview_carbs_carbs.value = max(0.0, overview_carbs_carbs.value + + SP.getInt(R.string.key_carbs_button_increment_2, FAV2_DEFAULT)) + validateInputs() + } + + overview_carbs_plus3.text = toSignedString(SP.getInt(R.string.key_carbs_button_increment_3, FAV3_DEFAULT)) + overview_carbs_plus3.setOnClickListener { + overview_carbs_carbs.value = max(0.0, overview_carbs_carbs.value + + SP.getInt(R.string.key_carbs_button_increment_3, FAV3_DEFAULT)) + validateInputs() + } + + DatabaseHelper.actualBg()?.let { bgReading -> + if (bgReading.value < 72) + overview_carbs_hypo_tt.setChecked(true) + } + overview_carbs_hypo_tt.setOnClickListener { + overview_carbs_activity_tt.isChecked = false + overview_carbs_eating_soon_tt.isChecked = false + } + overview_carbs_activity_tt.setOnClickListener { + overview_carbs_hypo_tt.isChecked = false + overview_carbs_eating_soon_tt.isChecked = false + } + overview_carbs_eating_soon_tt.setOnClickListener { + overview_carbs_hypo_tt.isChecked = false + overview_carbs_activity_tt.isChecked = false + } + } + + private fun toSignedString(value: Int): String { + return if (value > 0) "+$value" else value.toString() + } + + override fun submit(): Boolean { + val carbs = overview_carbs_carbs.value.toInt() + val carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(Constraint(carbs)).value() + val units = ProfileFunctions.getSystemUnits() + val activityTTDuration = DefaultValueHelper.determineActivityTTDuration() + val activityTT = DefaultValueHelper.determineActivityTT() + val eatingSoonTTDuration = DefaultValueHelper.determineEatingSoonTTDuration() + val eatingSoonTT = DefaultValueHelper.determineEatingSoonTT() + val hypoTTDuration = DefaultValueHelper.determineHypoTTDuration() + val hypoTT = DefaultValueHelper.determineHypoTT() + val actions: LinkedList = LinkedList() + val unitLabel = if (units == Constants.MMOL) MainApp.gs(R.string.mmol) else MainApp.gs(R.string.mgdl) + + val activitySelected = overview_carbs_activity_tt.isChecked + if (activitySelected) + actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(activityTT) + " " + unitLabel + " (" + activityTTDuration + " " + MainApp.gs(R.string.unit_minute_short) + ")") + val eatingSoonSelected = overview_carbs_eating_soon_tt.isChecked + if (eatingSoonSelected) + actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(eatingSoonTT) + " " + unitLabel + " (" + eatingSoonTTDuration + " " + MainApp.gs(R.string.unit_minute_short) + ")") + val hypoSelected = overview_carbs_hypo_tt.isChecked + if (hypoSelected) + actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(hypoTT) + " " + unitLabel + " (" + hypoTTDuration + " " + MainApp.gs(R.string.unit_minute_short) + ")") + + val timeOffset = overview_carbs_time.value.toInt() + val time = DateUtil.now() + timeOffset * 1000 * 60 + if (timeOffset != 0) + actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(time)) + val duration = overview_carbs_duration.value.toInt() + if (duration > 0) + actions.add(MainApp.gs(R.string.duration) + ": " + duration + MainApp.gs(R.string.shorthour)) + if (carbsAfterConstraints > 0) { + actions.add(MainApp.gs(R.string.carbs) + ": " + "" + MainApp.gs(R.string.format_carbs, carbsAfterConstraints) + "") + if (carbsAfterConstraints != carbs) + actions.add("" + MainApp.gs(R.string.carbsconstraintapplied) + "") + } + val notes = notes.text.toString() + if (notes.isNotEmpty()) + actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes) + + if (carbsAfterConstraints > 0 || activitySelected || eatingSoonSelected || hypoSelected) { + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.carbs), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + if (activitySelected) { + val tempTarget = TempTarget() + .date(System.currentTimeMillis()) + .duration(activityTTDuration) + .reason(MainApp.gs(R.string.activity)) + .source(Source.USER) + .low(Profile.toMgdl(activityTT, ProfileFunctions.getSystemUnits())) + .high(Profile.toMgdl(activityTT, ProfileFunctions.getSystemUnits())) + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) + } else if (eatingSoonSelected) { + val tempTarget = TempTarget() + .date(System.currentTimeMillis()) + .duration(eatingSoonTTDuration) + .reason(MainApp.gs(R.string.eatingsoon)) + .source(Source.USER) + .low(Profile.toMgdl(eatingSoonTT, ProfileFunctions.getSystemUnits())) + .high(Profile.toMgdl(eatingSoonTT, ProfileFunctions.getSystemUnits())) + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) + } else if (hypoSelected) { + val tempTarget = TempTarget() + .date(System.currentTimeMillis()) + .duration(hypoTTDuration) + .reason(MainApp.gs(R.string.hypo)) + .source(Source.USER) + .low(Profile.toMgdl(hypoTT, ProfileFunctions.getSystemUnits())) + .high(Profile.toMgdl(hypoTT, ProfileFunctions.getSystemUnits())) + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) + } + if (carbsAfterConstraints > 0) { + if (duration == 0) { + CarbsGenerator.createCarb(carbsAfterConstraints, time, CareportalEvent.CARBCORRECTION, notes) + } else { + CarbsGenerator.generateCarbs(carbsAfterConstraints, time, duration, notes) + NSUpload.uploadEvent(CareportalEvent.NOTE, DateUtil.now() - 2000, MainApp.gs(R.string.generated_ecarbs_note, carbsAfterConstraints, duration, timeOffset)) + } + } + }, null) + } + } else + activity?.let { activity -> + OKDialog.show(activity, MainApp.gs(R.string.carbs), MainApp.gs(R.string.no_action_selected)) + } + return true + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/CareDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/CareDialog.kt new file mode 100644 index 0000000000..946a3fec5b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/CareDialog.kt @@ -0,0 +1,150 @@ +package info.nightscout.androidaps.dialogs + +import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.annotation.StringRes +import com.google.common.base.Joiner +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.CareportalEvent +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.general.nsclient.NSUpload +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.HtmlHelper +import info.nightscout.androidaps.utils.OKDialog +import info.nightscout.androidaps.utils.SP +import info.nightscout.androidaps.utils.Translator +import kotlinx.android.synthetic.main.dialog_care.* +import kotlinx.android.synthetic.main.notes.* +import kotlinx.android.synthetic.main.okcancel.* +import org.json.JSONObject +import java.text.DecimalFormat +import java.util.* + +class CareDialog : DialogFragmentWithDate() { + + enum class EventType { + BGCHECK, + SENSOR_INSERT, + BATTERY_CHANGE + } + + private var options: EventType = EventType.BGCHECK + @StringRes + private var event: Int = R.string.none + + fun setOptions(options: EventType, @StringRes event: Int): CareDialog { + this.options = options + this.event = event + return this + } + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("actions_care_bg", actions_care_bg.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateViewGeneral() + return inflater.inflate(R.layout.dialog_care, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + actions_care_icon.setImageResource(when (options) { + EventType.BGCHECK -> R.drawable.icon_cp_bgcheck + EventType.SENSOR_INSERT -> R.drawable.icon_cp_cgm_insert + EventType.BATTERY_CHANGE -> R.drawable.icon_cp_pump_battery + }) + actions_care_title.text = MainApp.gs(when (options) { + EventType.BGCHECK -> R.string.careportal_bgcheck + EventType.SENSOR_INSERT -> R.string.careportal_cgmsensorinsert + EventType.BATTERY_CHANGE -> R.string.careportal_pumpbatterychange + }) + + when (options) { + EventType.SENSOR_INSERT, + EventType.BATTERY_CHANGE -> { + action_care_bg_layout.visibility = View.GONE + actions_care_bgsource.visibility = View.GONE + } + + else -> { + } + } + + val bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData()?.glucose + ?: 0.0, ProfileFunctions.getSystemUnits()) + val bgTextWatcher: TextWatcher = object : TextWatcher { + override fun afterTextChanged(s: Editable) {} + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { + if (actions_care_sensor.isChecked) actions_care_meter.isChecked = true + } + } + + if (ProfileFunctions.getSystemUnits() == Constants.MMOL) { + actions_care_bgunits.text = MainApp.gs(R.string.mmol) + actions_care_bg.setParams(savedInstanceState?.getDouble("actions_care_bg") + ?: bg, 2.0, 30.0, 0.1, DecimalFormat("0.0"), false, ok, bgTextWatcher) + } else { + actions_care_bgunits.text = MainApp.gs(R.string.mgdl) + actions_care_bg.setParams(savedInstanceState?.getDouble("actions_care_bg") + ?: bg, 36.0, 500.0, 1.0, DecimalFormat("0"), false, ok, bgTextWatcher) + } + } + + override fun submit(): Boolean { + val enteredBy = SP.getString("careportal_enteredby", "") + val unitResId = if (ProfileFunctions.getSystemUnits() == Constants.MGDL) R.string.mgdl else R.string.mmol + + val json = JSONObject() + val actions: LinkedList = LinkedList() + if (options == EventType.BGCHECK) { + val type = + when { + actions_care_meter.isChecked -> "Finger" + actions_care_sensor.isChecked -> "Sensor" + else -> "Manual" + } + actions.add(MainApp.gs(R.string.careportal_newnstreatment_glucosetype) + ": " + Translator.translate(type)) + actions.add(MainApp.gs(R.string.treatments_wizard_bg_label) + ": " + Profile.toCurrentUnitsString(actions_care_bg.value) + " " + MainApp.gs(unitResId)) + json.put("glucose", actions_care_bg.value) + json.put("glucoseType", type) + } + val notes = notes.text.toString() + if (notes.isNotEmpty()) { + actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes) + json.put("notes", notes) + } + if (eventTimeChanged) + actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(eventTime)) + + json.put("created_at", DateUtil.toISOString(eventTime)) + json.put("eventType", when (options) { + EventType.BGCHECK -> CareportalEvent.BGCHECK + EventType.SENSOR_INSERT -> CareportalEvent.SENSORCHANGE + EventType.BATTERY_CHANGE -> CareportalEvent.PUMPBATTERYCHANGE + }) + json.put("units", ProfileFunctions.getSystemUnits()) + if (enteredBy.isNotEmpty()) + json.put("enteredBy", enteredBy) + + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(event), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + MainApp.getDbHelper().createCareportalEventFromJsonIfNotExists(json) + NSUpload.uploadCareportalEntryToNS(json) + }, null) + } + return true + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/DialogFragmentWithDate.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/DialogFragmentWithDate.kt new file mode 100644 index 0000000000..da45e77990 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/DialogFragmentWithDate.kt @@ -0,0 +1,124 @@ +package info.nightscout.androidaps.dialogs + +import android.app.DatePickerDialog +import android.app.TimePickerDialog +import android.os.Bundle +import android.text.format.DateFormat +import android.view.View +import android.view.ViewGroup +import android.view.Window +import android.view.WindowManager +import androidx.fragment.app.DialogFragment +import info.nightscout.androidaps.R +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.SP +import info.nightscout.androidaps.utils.toVisibility +import kotlinx.android.synthetic.main.datetime.* +import kotlinx.android.synthetic.main.notes.* +import kotlinx.android.synthetic.main.okcancel.* +import org.slf4j.LoggerFactory +import java.util.* + +abstract class DialogFragmentWithDate : DialogFragment() { + private val log = LoggerFactory.getLogger(DialogFragmentWithDate::class.java) + + var eventTime = DateUtil.now() + var eventTimeChanged = false + + //one shot guards + private var okClicked: Boolean = false + + companion object { + private var seconds: Int = (Math.random() * 59.0).toInt() + } + + override fun onStart() { + super.onStart() + dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + } + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putLong("eventTime", eventTime) + savedInstanceState.putBoolean("eventTimeChanged", eventTimeChanged) + } + + fun onCreateViewGeneral() { + dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE) + dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) + isCancelable = true + dialog?.setCanceledOnTouchOutside(false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + eventTime = savedInstanceState?.getLong("eventTime") ?: DateUtil.now() + eventTimeChanged = savedInstanceState?.getBoolean("eventTimeChanged") ?: false + overview_eventdate?.text = DateUtil.dateString(eventTime) + overview_eventtime?.text = DateUtil.timeString(eventTime) + + // create an OnDateSetListener + val dateSetListener = DatePickerDialog.OnDateSetListener { _, year, monthOfYear, dayOfMonth -> + val cal = Calendar.getInstance() + cal.timeInMillis = eventTime + cal.set(Calendar.YEAR, year) + cal.set(Calendar.MONTH, monthOfYear) + cal.set(Calendar.DAY_OF_MONTH, dayOfMonth) + eventTime = cal.timeInMillis + eventTimeChanged = true + overview_eventdate?.text = DateUtil.dateString(eventTime) + } + + overview_eventdate?.setOnClickListener { + context?.let { + val cal = Calendar.getInstance() + cal.timeInMillis = eventTime + DatePickerDialog(it, dateSetListener, + cal.get(Calendar.YEAR), + cal.get(Calendar.MONTH), + cal.get(Calendar.DAY_OF_MONTH) + ).show() + } + } + + // create an OnTimeSetListener + val timeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute -> + val cal = Calendar.getInstance() + cal.timeInMillis = eventTime + cal.set(Calendar.HOUR_OF_DAY, hour) + cal.set(Calendar.MINUTE, minute) + cal.set(Calendar.SECOND, seconds++) // randomize seconds to prevent creating record of the same time, if user choose time manually + eventTime = cal.timeInMillis + eventTimeChanged = true + overview_eventtime?.text = DateUtil.timeString(eventTime) + } + + overview_eventtime?.setOnClickListener { + context?.let { + val cal = Calendar.getInstance() + cal.timeInMillis = eventTime + TimePickerDialog(it, timeSetListener, + cal.get(Calendar.HOUR_OF_DAY), + cal.get(Calendar.MINUTE), + DateFormat.is24HourFormat(context) + ).show() + } + } + + notes_layout?.visibility = SP.getBoolean(R.string.key_show_notes_entry_dialogs, false).toVisibility() + + ok.setOnClickListener { + synchronized(okClicked) { + if (okClicked) { + log.debug("guarding: ok already clicked") + } else { + okClicked = true + if (submit()) dismiss() + else okClicked = false + } + } + } + cancel.setOnClickListener { dismiss() } + } + + abstract fun submit(): Boolean +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/ErrorDialog.kt similarity index 77% rename from app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.kt rename to app/src/main/java/info/nightscout/androidaps/dialogs/ErrorDialog.kt index 8ea499d4ef..538a2ca483 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/ErrorDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/ErrorDialog.kt @@ -1,5 +1,4 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs - +package info.nightscout.androidaps.dialogs import android.content.Intent import android.os.Build @@ -7,11 +6,14 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.view.Window +import android.view.WindowManager import androidx.fragment.app.DialogFragment import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.ErrorHelperActivity import info.nightscout.androidaps.services.AlarmSoundService -import kotlinx.android.synthetic.main.overview_error_dialog.* +import kotlinx.android.synthetic.main.dialog_error.* import org.slf4j.LoggerFactory class ErrorDialog : DialogFragment() { @@ -24,20 +26,23 @@ class ErrorDialog : DialogFragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - dialog?.setTitle(title) - isCancelable = false + dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE) + dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) + isCancelable = true + dialog?.setCanceledOnTouchOutside(false) savedInstanceState?.let { bundle -> bundle.getString("status")?.let { status = it } bundle.getString("title")?.let { title = it } sound = bundle.getInt("sound", R.raw.error) } - return inflater.inflate(R.layout.overview_error_dialog, container, false) + return inflater.inflate(R.layout.dialog_error, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + error_title.text = title overview_error_ok.setOnClickListener { log.debug("Error dialog ok button pressed") dismiss() @@ -56,9 +61,13 @@ class ErrorDialog : DialogFragment() { bundle.putInt("sound", sound) } + override fun onStart() { + super.onStart() + dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + } + override fun onResume() { super.onResume() - dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) overview_error_status.text = status } @@ -81,5 +90,5 @@ class ErrorDialog : DialogFragment() { } private fun stopAlarm() = - MainApp.instance().stopService(Intent(MainApp.instance().applicationContext, AlarmSoundService::class.java)) + MainApp.instance().stopService(Intent(MainApp.instance().applicationContext, AlarmSoundService::class.java)) } diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/ExtendedBolusDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/ExtendedBolusDialog.kt new file mode 100644 index 0000000000..a717e79b49 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/ExtendedBolusDialog.kt @@ -0,0 +1,82 @@ +package info.nightscout.androidaps.dialogs + +import android.content.Intent +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.google.common.base.Joiner +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.ErrorHelperActivity +import info.nightscout.androidaps.interfaces.Constraint +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.queue.Callback +import info.nightscout.androidaps.utils.HtmlHelper +import info.nightscout.androidaps.utils.OKDialog +import info.nightscout.androidaps.utils.SafeParse +import kotlinx.android.synthetic.main.dialog_extendedbolus.* +import kotlinx.android.synthetic.main.okcancel.* +import java.text.DecimalFormat +import java.util.* +import kotlin.math.abs + +class ExtendedBolusDialog : DialogFragmentWithDate() { + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("actions_extendedbolus_insulin", actions_extendedbolus_insulin.value) + savedInstanceState.putDouble("actions_extendedbolus_duration", actions_extendedbolus_duration.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateViewGeneral() + return inflater.inflate(R.layout.dialog_extendedbolus, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription ?: return + + val maxInsulin = MainApp.getConstraintChecker().maxExtendedBolusAllowed.value() + val extendedStep = pumpDescription.extendedBolusStep + actions_extendedbolus_insulin.setParams(savedInstanceState?.getDouble("actions_extendedbolus_insulin") + ?: extendedStep, extendedStep, maxInsulin, extendedStep, DecimalFormat("0.00"), false, ok) + + val extendedDurationStep = pumpDescription.extendedBolusDurationStep + val extendedMaxDuration = pumpDescription.extendedBolusMaxDuration + actions_extendedbolus_duration.setParams(savedInstanceState?.getDouble("actions_extendedbolus_duration") + ?: extendedDurationStep, extendedDurationStep, extendedMaxDuration, extendedDurationStep, DecimalFormat("0"), false, ok) + } + + override fun submit(): Boolean { + val insulin = SafeParse.stringToDouble(actions_extendedbolus_insulin.text) + val durationInMinutes = SafeParse.stringToInt(actions_extendedbolus_duration.text) + val actions: LinkedList = LinkedList() + val insulinAfterConstraint = MainApp.getConstraintChecker().applyExtendedBolusConstraints(Constraint(insulin)).value() + actions.add(MainApp.gs(R.string.formatinsulinunits, insulinAfterConstraint)) + actions.add(MainApp.gs(R.string.duration) + ": " + MainApp.gs(R.string.format_mins, durationInMinutes)) + if (abs(insulinAfterConstraint - insulin) > 0.01) + actions.add("" + MainApp.gs(R.string.constraintapllied) + "") + + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.extended_bolus), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + ConfigBuilderPlugin.getPlugin().commandQueue.extendedBolus(insulinAfterConstraint, durationInMinutes, object : Callback() { + override fun run() { + if (!result.success) { + val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) + i.putExtra("soundid", R.raw.boluserror) + i.putExtra("status", result.comment) + i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror)) + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + MainApp.instance().startActivity(i) + } + } + }) + }, null) + } + return true + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/FillDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/FillDialog.kt new file mode 100644 index 0000000000..8d55df9bc1 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/FillDialog.kt @@ -0,0 +1,132 @@ +package info.nightscout.androidaps.dialogs + +import android.content.Intent +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.google.common.base.Joiner +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.ErrorHelperActivity +import info.nightscout.androidaps.data.DetailedBolusInfo +import info.nightscout.androidaps.db.CareportalEvent +import info.nightscout.androidaps.db.Source +import info.nightscout.androidaps.interfaces.Constraint +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.general.nsclient.NSUpload +import info.nightscout.androidaps.queue.Callback +import info.nightscout.androidaps.utils.* +import kotlinx.android.synthetic.main.dialog_fill.* +import kotlinx.android.synthetic.main.notes.* +import kotlinx.android.synthetic.main.okcancel.* +import java.util.* +import kotlin.math.abs + +class FillDialog : DialogFragmentWithDate() { + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("fill_insulinamount", fill_insulinamount.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateViewGeneral() + return inflater.inflate(R.layout.dialog_fill, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + val maxInsulin = MainApp.getConstraintChecker().maxBolusAllowed.value() + val bolusStep = ConfigBuilderPlugin.getPlugin().activePump!!.pumpDescription.bolusStep + fill_insulinamount.setParams(savedInstanceState?.getDouble("fill_insulinamount") + ?: 0.0, 0.0, maxInsulin, bolusStep, DecimalFormatter.pumpSupportedBolusFormat(), true, ok) + val amount1 = SP.getDouble("fill_button1", 0.3) + if (amount1 > 0) { + fill_preset_button1.visibility = View.VISIBLE + fill_preset_button1.text = DecimalFormatter.toPumpSupportedBolus(amount1) // + "U"); + fill_preset_button1.setOnClickListener { fill_insulinamount.value = amount1 } + } else { + fill_preset_button1.visibility = View.GONE + } + val amount2 = SP.getDouble("fill_button2", 0.0) + if (amount2 > 0) { + fill_preset_button2.visibility = View.VISIBLE + fill_preset_button2.text = DecimalFormatter.toPumpSupportedBolus(amount2) // + "U"); + fill_preset_button2.setOnClickListener { fill_insulinamount.value = amount2 } + } else { + fill_preset_button2.visibility = View.GONE + } + val amount3 = SP.getDouble("fill_button3", 0.0) + if (amount3 > 0) { + fill_preset_button3.visibility = View.VISIBLE + fill_preset_button3.text = DecimalFormatter.toPumpSupportedBolus(amount3) // + "U"); + fill_preset_button3.setOnClickListener { fill_insulinamount.value = amount3 } + } else { + fill_preset_button3.visibility = View.GONE + } + + } + + override fun submit(): Boolean { + val insulin = SafeParse.stringToDouble(fill_insulinamount.text) + val actions: LinkedList = LinkedList() + + val insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(Constraint(insulin)).value() + if (insulinAfterConstraints > 0) { + actions.add(MainApp.gs(R.string.fillwarning)) + actions.add("") + actions.add(MainApp.gs(R.string.bolus) + ": " + "" + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints) + MainApp.gs(R.string.insulin_unit_shortname) + "") + if (abs(insulinAfterConstraints - insulin) > 0.01) + actions.add(MainApp.gs(R.string.bolusconstraintappliedwarning, MainApp.gc(R.color.warning), insulin, insulinAfterConstraints)) + } + val siteChange = fill_catheter_change.isChecked + if (siteChange) + actions.add("" + "" + MainApp.gs(R.string.record_pump_site_change) + "") + val insulinChange = fill_cartridge_change.isChecked + if (insulinChange) + actions.add("" + "" + MainApp.gs(R.string.record_insulin_cartridge_change) + "") + val notes = notes.text.toString() + if (notes.isNotEmpty()) + actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes) + if (eventTimeChanged) + actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(eventTime)) + + if (insulinAfterConstraints > 0 || fill_catheter_change.isChecked || fill_cartridge_change.isChecked) { + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.primefill), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + if (insulinAfterConstraints > 0) { + val detailedBolusInfo = DetailedBolusInfo() + detailedBolusInfo.insulin = insulinAfterConstraints + detailedBolusInfo.context = context + detailedBolusInfo.source = Source.USER + detailedBolusInfo.isValid = false // do not count it in IOB (for pump history) + detailedBolusInfo.notes = notes + ConfigBuilderPlugin.getPlugin().commandQueue.bolus(detailedBolusInfo, object : Callback() { + override fun run() { + if (!result.success) { + val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) + i.putExtra("soundid", R.raw.boluserror) + i.putExtra("status", result.comment) + i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror)) + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + MainApp.instance().startActivity(i) + } + } + }) + } + if (siteChange) NSUpload.uploadEvent(CareportalEvent.SITECHANGE, eventTime, notes) + if (insulinChange) NSUpload.uploadEvent(CareportalEvent.INSULINCHANGE, eventTime + 1000, notes) + }, null) + } + } else { + activity?.let { activity -> + OKDialog.show(activity, MainApp.gs(R.string.primefill), MainApp.gs(R.string.no_action_selected)) + } + } + dismiss() + return true + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/InsulinDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/InsulinDialog.kt new file mode 100644 index 0000000000..d6f64d87f1 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/InsulinDialog.kt @@ -0,0 +1,193 @@ +package info.nightscout.androidaps.dialogs + +import android.content.Intent +import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.google.common.base.Joiner +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.ErrorHelperActivity +import info.nightscout.androidaps.data.DetailedBolusInfo +import info.nightscout.androidaps.data.Profile +import info.nightscout.androidaps.db.CareportalEvent +import info.nightscout.androidaps.db.Source +import info.nightscout.androidaps.db.TempTarget +import info.nightscout.androidaps.interfaces.Constraint +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.queue.Callback +import info.nightscout.androidaps.utils.* +import kotlinx.android.synthetic.main.dialog_insulin.* +import kotlinx.android.synthetic.main.notes.* +import kotlinx.android.synthetic.main.okcancel.* +import java.text.DecimalFormat +import java.util.* +import kotlin.math.abs +import kotlin.math.max + +class InsulinDialog : DialogFragmentWithDate() { + + companion object { + private const val PLUS1_DEFAULT = 0.5 + private const val PLUS2_DEFAULT = 1.0 + private const val PLUS3_DEFAULT = 2.0 + } + + private val maxInsulin = MainApp.getConstraintChecker().maxBolusAllowed.value() + + private val textWatcher: TextWatcher = object : TextWatcher { + override fun afterTextChanged(s: Editable) { + validateInputs() + } + + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} + } + + private fun validateInputs() { + if (abs(overview_insulin_time.value.toInt()) > 12 * 60) { + overview_insulin_time.value = 0.0 + ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.constraintapllied)) + } + if (overview_insulin_amount.value > maxInsulin) { + overview_insulin_amount.value = 0.0 + ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.bolusconstraintapplied)) + } + } + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("overview_insulin_time", overview_insulin_time.value) + savedInstanceState.putDouble("overview_insulin_amount", overview_insulin_amount.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateViewGeneral() + return inflater.inflate(R.layout.dialog_insulin, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + overview_insulin_time.setParams(savedInstanceState?.getDouble("overview_insulin_time") + ?: 0.0, -12 * 60.0, 12 * 60.0, 5.0, DecimalFormat("0"), false, ok, textWatcher) + overview_insulin_amount.setParams(savedInstanceState?.getDouble("overview_insulin_amount") + ?: 0.0, 0.0, maxInsulin, ConfigBuilderPlugin.getPlugin().activePump!!.pumpDescription.bolusStep, DecimalFormatter.pumpSupportedBolusFormat(), false, ok, textWatcher) + + overview_insulin_plus05.text = toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT)) + overview_insulin_plus05.setOnClickListener { + overview_insulin_amount.value = max(0.0, overview_insulin_amount.value + + SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT)) + validateInputs() + } + overview_insulin_plus10.text = toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT)) + overview_insulin_plus10.setOnClickListener { + overview_insulin_amount.value = max(0.0, overview_insulin_amount.value + + SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT)) + validateInputs() + } + overview_insulin_plus20.text = toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_3), PLUS3_DEFAULT)) + overview_insulin_plus20.setOnClickListener { + overview_insulin_amount.value = Math.max(0.0, overview_insulin_amount.value + + SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_3), PLUS3_DEFAULT)) + validateInputs() + } + + overview_insulin_time_layout.visibility = View.GONE + overview_insulin_record_only.setOnCheckedChangeListener { _, isChecked: Boolean -> + overview_insulin_time_layout.visibility = isChecked.toVisibility() + } + } + + private fun toSignedString(value: Double): String { + val formatted = DecimalFormatter.toPumpSupportedBolus(value) + return if (value > 0) "+$formatted" else formatted + } + + override fun submit(): Boolean { + val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription + ?: return false + val insulin = SafeParse.stringToDouble(overview_insulin_amount.text) + val insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(Constraint(insulin)).value() + val actions: LinkedList = LinkedList() + val units = ProfileFunctions.getSystemUnits() + val unitLabel = if (units == Constants.MMOL) MainApp.gs(R.string.mmol) else MainApp.gs(R.string.mgdl) + val recordOnlyChecked = overview_insulin_record_only.isChecked + val eatingSoonChecked = overview_insulin_start_eating_soon_tt.isChecked + + if (insulinAfterConstraints > 0) { + actions.add(MainApp.gs(R.string.bolus) + ": " + "" + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints) + MainApp.gs(R.string.insulin_unit_shortname) + "") + if (recordOnlyChecked) + actions.add("" + MainApp.gs(R.string.bolusrecordedonly) + "") + if (abs(insulinAfterConstraints - insulin) > pumpDescription.pumpType.determineCorrectBolusStepSize(insulinAfterConstraints)) + actions.add(MainApp.gs(R.string.bolusconstraintappliedwarning, MainApp.gc(R.color.warning), insulin, insulinAfterConstraints)) + } + val eatingSoonTTDuration = DefaultValueHelper.determineEatingSoonTTDuration() + val eatingSoonTT = DefaultValueHelper.determineEatingSoonTT() + if (eatingSoonChecked) + actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(eatingSoonTT) + " " + unitLabel + " (" + eatingSoonTTDuration + " " + MainApp.gs(R.string.unit_minute_short) + ")") + + val timeOffset = overview_insulin_time.value.toInt() + val time = DateUtil.now() + T.mins(timeOffset.toLong()).msecs() + if (timeOffset != 0) + actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(time)) + + val notes = notes.text.toString() + if (notes.isNotEmpty()) + actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes) + + if (insulinAfterConstraints > 0 || eatingSoonChecked) { + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.bolus), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + if (eatingSoonChecked) { + val tempTarget = TempTarget() + .date(System.currentTimeMillis()) + .duration(eatingSoonTTDuration) + .reason(MainApp.gs(R.string.eatingsoon)) + .source(Source.USER) + .low(Profile.toMgdl(eatingSoonTT, ProfileFunctions.getSystemUnits())) + .high(Profile.toMgdl(eatingSoonTT, ProfileFunctions.getSystemUnits())) + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) + } + if (insulinAfterConstraints > 0) { + val detailedBolusInfo = DetailedBolusInfo() + detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS + detailedBolusInfo.insulin = insulinAfterConstraints + detailedBolusInfo.context = context + detailedBolusInfo.source = Source.USER + detailedBolusInfo.notes = notes + if (recordOnlyChecked) { + detailedBolusInfo.date = time + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false) + } else { + detailedBolusInfo.date = DateUtil.now() + ConfigBuilderPlugin.getPlugin().commandQueue.bolus(detailedBolusInfo, object : Callback() { + override fun run() { + if (!result.success) { + val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) + i.putExtra("soundid", R.raw.boluserror) + i.putExtra("status", result.comment) + i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror)) + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + MainApp.instance().startActivity(i) + } + } + }) + } + } + }) + } + } else + activity?.let { activity -> + OKDialog.show(activity, MainApp.gs(R.string.bolus), MainApp.gs(R.string.no_action_selected)) + } + return true + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/ProfileSwitchDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/ProfileSwitchDialog.kt new file mode 100644 index 0000000000..5df248593c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/ProfileSwitchDialog.kt @@ -0,0 +1,105 @@ +package info.nightscout.androidaps.dialogs + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter +import com.google.common.base.Joiner +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.HtmlHelper +import info.nightscout.androidaps.utils.OKDialog +import kotlinx.android.synthetic.main.dialog_profileswitch.* +import kotlinx.android.synthetic.main.notes.* +import kotlinx.android.synthetic.main.okcancel.* +import java.text.DecimalFormat +import java.util.* + +class ProfileSwitchDialog : DialogFragmentWithDate() { + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("overview_profileswitch_duration", overview_profileswitch_duration.value) + savedInstanceState.putDouble("overview_profileswitch_percentage", overview_profileswitch_percentage.value) + savedInstanceState.putDouble("overview_profileswitch_timeshift", overview_profileswitch_timeshift.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateViewGeneral() + return inflater.inflate(R.layout.dialog_profileswitch, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + overview_profileswitch_duration.setParams(savedInstanceState?.getDouble("overview_profileswitch_duration") + ?: 0.0, 0.0, Constants.MAX_PROFILE_SWITCH_DURATION, 10.0, DecimalFormat("0"), false, ok) + overview_profileswitch_percentage.setParams(savedInstanceState?.getDouble("overview_profileswitch_percentage") + ?: 100.0, Constants.CPP_MIN_PERCENTAGE.toDouble(), Constants.CPP_MAX_PERCENTAGE.toDouble(), 1.0, DecimalFormat("0"), false, ok) + overview_profileswitch_timeshift.setParams(savedInstanceState?.getDouble("overview_profileswitch_timeshift") + ?: 0.0, Constants.CPP_MIN_TIMESHIFT.toDouble(), Constants.CPP_MAX_TIMESHIFT.toDouble(), 1.0, DecimalFormat("0"), false, ok) + + // profile + context?.let { context -> + val profileStore = ConfigBuilderPlugin.getPlugin().activeProfileInterface?.profile + ?: return + val profileList = profileStore.getProfileList() + val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList) + overview_profileswitch_profile.adapter = adapter + // set selected to actual profile + for (p in profileList.indices) + if (profileList[p] == ProfileFunctions.getInstance().getProfileName(false)) + overview_profileswitch_profile.setSelection(p) + } ?: return + + TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(DateUtil.now())?.let { ps -> + if (ps.isCPP) { + overview_profileswitch_reuselayout.visibility = View.VISIBLE + overview_profileswitch_reusebutton.text = MainApp.gs(R.string.reuse) + " " + ps.percentage + "% " + ps.timeshift + "h" + overview_profileswitch_reusebutton.setOnClickListener { + overview_profileswitch_percentage.value = ps.percentage.toDouble() + overview_profileswitch_timeshift.value = ps.timeshift.toDouble() + } + } else { + overview_profileswitch_reuselayout.visibility = View.GONE + } + } + } + + override fun submit(): Boolean { + val profileStore = ConfigBuilderPlugin.getPlugin().activeProfileInterface?.profile + ?: return false + + val actions: LinkedList = LinkedList() + val duration = overview_profileswitch_duration.value + if (duration > 0) + actions.add(MainApp.gs(R.string.duration) + ": " + MainApp.gs(R.string.format_hours, duration)) + val profile = overview_profileswitch_profile.selectedItem.toString() + actions.add(MainApp.gs(R.string.profile) + ": " + profile) + val percent = overview_profileswitch_percentage.value.toInt() + if (percent != 100) + actions.add(MainApp.gs(R.string.percent) + ": " + percent + "%") + val timeShift = overview_profileswitch_timeshift.value.toInt() + if (timeShift != 0) + actions.add(MainApp.gs(R.string.careportal_newnstreatment_timeshift_label) + ": " + MainApp.gs(R.string.format_hours, timeShift.toDouble())) + val notes = notes.text.toString() + if (notes.isNotEmpty()) + actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes) + if (eventTimeChanged) + actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(eventTime)) + + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.careportal_profileswitch), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + ProfileFunctions.doProfileSwitch(profileStore, profile, duration.toInt(), percent, timeShift, eventTime) + }) + } + return true + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/ProfileViewerDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/ProfileViewerDialog.kt similarity index 74% rename from app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/ProfileViewerDialog.kt rename to app/src/main/java/info/nightscout/androidaps/dialogs/ProfileViewerDialog.kt index 69d89d0063..ef3b19d4d8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/ProfileViewerDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/ProfileViewerDialog.kt @@ -1,20 +1,20 @@ -package info.nightscout.androidaps.plugins.treatments.fragments +package info.nightscout.androidaps.dialogs import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.view.Window +import android.view.WindowManager import androidx.fragment.app.DialogFragment 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.interfaces.ProfileInterface -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.utils.DateUtil import kotlinx.android.synthetic.main.close.* -import kotlinx.android.synthetic.main.profileviewer_fragment.* +import kotlinx.android.synthetic.main.dialog_profileviewer.* import org.json.JSONObject class ProfileViewerDialog : DialogFragment() { @@ -22,8 +22,7 @@ class ProfileViewerDialog : DialogFragment() { enum class Mode(val i: Int) { RUNNING_PROFILE(1), - PUMP_PROFILE(2), - CUSTOM_PROFILE(3) + CUSTOM_PROFILE(2) } private var mode: Mode = Mode.RUNNING_PROFILE @@ -42,17 +41,18 @@ class ProfileViewerDialog : DialogFragment() { customProfileName = bundle.getString("customProfileName", "") } - return inflater.inflate(R.layout.profileviewer_fragment, container, false) + dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE) + dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) + isCancelable = true + dialog?.setCanceledOnTouchOutside(false) + + return inflater.inflate(R.layout.dialog_profileviewer, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) close.setOnClickListener { dismiss() } - profileview_reload.setOnClickListener { - ConfigBuilderPlugin.getPlugin().commandQueue.readStatus("ProfileViewDialog", null) - dismiss() - } val profile: Profile? val profileName: String? @@ -62,22 +62,14 @@ class ProfileViewerDialog : DialogFragment() { profile = TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(time)?.profileObject profileName = TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(time)?.customizedName date = DateUtil.dateAndTimeString(TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(time)?.date - ?: 0) - profileview_reload.visibility = View.GONE + ?: 0) profileview_datelayout.visibility = View.VISIBLE } - Mode.PUMP_PROFILE -> { - profile = (ConfigBuilderPlugin.getPlugin().activePump as ProfileInterface?)?.profile?.getDefaultProfile() - profileName = (ConfigBuilderPlugin.getPlugin().activePump as ProfileInterface?)?.profileName - date = "" - profileview_reload.visibility = View.VISIBLE - profileview_datelayout.visibility = View.GONE - } - Mode.CUSTOM_PROFILE -> { + + Mode.CUSTOM_PROFILE -> { profile = Profile(JSONObject(customProfileJson), customProfileUnits) profileName = customProfileName date = "" - profileview_reload.visibility = View.GONE profileview_datelayout.visibility = View.GONE } } @@ -99,9 +91,9 @@ class ProfileViewerDialog : DialogFragment() { } } - override fun onResume() { + override fun onStart() { + super.onStart() dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) - super.onResume() } override fun onSaveInstanceState(bundle: Bundle) { diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/TempBasalDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/TempBasalDialog.kt new file mode 100644 index 0000000000..dc31349650 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/TempBasalDialog.kt @@ -0,0 +1,115 @@ +package info.nightscout.androidaps.dialogs + +import android.content.Intent +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.google.common.base.Joiner +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.ErrorHelperActivity +import info.nightscout.androidaps.interfaces.Constraint +import info.nightscout.androidaps.interfaces.PumpDescription +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.queue.Callback +import info.nightscout.androidaps.utils.HtmlHelper +import info.nightscout.androidaps.utils.OKDialog +import info.nightscout.androidaps.utils.SafeParse +import kotlinx.android.synthetic.main.dialog_tempbasal.* +import kotlinx.android.synthetic.main.okcancel.* +import java.text.DecimalFormat +import java.util.* +import kotlin.math.abs + +class TempBasalDialog : DialogFragmentWithDate() { + private var isPercentPump = true + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("actions_tempbasal_duration", actions_tempbasal_duration.value) + savedInstanceState.putDouble("actions_tempbasal_basalpercentinput", actions_tempbasal_basalpercentinput.value) + savedInstanceState.putDouble("actions_tempbasal_basalabsoluteinput", actions_tempbasal_basalabsoluteinput.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateViewGeneral() + return inflater.inflate(R.layout.dialog_tempbasal, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription ?: return + val profile = ProfileFunctions.getInstance().profile ?: return + + val maxTempPercent = pumpDescription.maxTempPercent.toDouble() + val tempPercentStep = pumpDescription.tempPercentStep.toDouble() + + actions_tempbasal_basalpercentinput.setParams(savedInstanceState?.getDouble("actions_tempbasal_basalpercentinput") + ?: 100.0, 0.0, maxTempPercent, tempPercentStep, DecimalFormat("0"), true, ok) + + actions_tempbasal_basalabsoluteinput.setParams(savedInstanceState?.getDouble("actions_tempbasal_basalabsoluteinput") + ?: profile.basal, 0.0, pumpDescription.maxTempAbsolute, pumpDescription.tempAbsoluteStep, DecimalFormat("0.00"), true, ok) + + val tempDurationStep = pumpDescription.tempDurationStep.toDouble() + val tempMaxDuration = pumpDescription.tempMaxDuration.toDouble() + actions_tempbasal_duration.setParams(savedInstanceState?.getDouble("actions_tempbasal_duration") + ?: tempDurationStep, tempDurationStep, tempMaxDuration, tempDurationStep, DecimalFormat("0"), false, ok) + + isPercentPump = pumpDescription.tempBasalStyle and PumpDescription.PERCENT == PumpDescription.PERCENT + if (isPercentPump) { + actions_tempbasal_percent_layout.visibility = View.VISIBLE + actions_tempbasal_absolute_layout.visibility = View.GONE + } else { + actions_tempbasal_percent_layout.visibility = View.GONE + actions_tempbasal_absolute_layout.visibility = View.VISIBLE + } + } + + override fun submit(): Boolean { + var percent = 0 + var absolute = 0.0 + val durationInMinutes = SafeParse.stringToInt(actions_tempbasal_duration.text) + val profile = ProfileFunctions.getInstance().profile ?: return false + val actions: LinkedList = LinkedList() + if (isPercentPump) { + val basalPercentInput = SafeParse.stringToInt(actions_tempbasal_basalpercentinput.text) + percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(Constraint(basalPercentInput), profile).value() + actions.add(MainApp.gs(R.string.pump_tempbasal_label)+ ": $percent%") + actions.add(MainApp.gs(R.string.duration) + ": " + MainApp.gs(R.string.format_mins, durationInMinutes)) + if (percent != basalPercentInput) actions.add(MainApp.gs(R.string.constraintapllied)) + } else { + val basalAbsoluteInput = SafeParse.stringToDouble(actions_tempbasal_basalabsoluteinput.text) + absolute = MainApp.getConstraintChecker().applyBasalConstraints(Constraint(basalAbsoluteInput), profile).value() + actions.add(MainApp.gs(R.string.pump_tempbasal_label)+ ": " + MainApp.gs(R.string.pump_basebasalrate, absolute)) + actions.add(MainApp.gs(R.string.duration) + ": " + MainApp.gs(R.string.format_mins, durationInMinutes)) + if (abs(absolute - basalAbsoluteInput) > 0.01) + actions.add("" + MainApp.gs(R.string.constraintapllied) + "") + } + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.pump_tempbasal_label), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + val callback: Callback = object : Callback() { + override fun run() { + if (!result.success) { + val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) + 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) + } + } + } + if (isPercentPump) { + ConfigBuilderPlugin.getPlugin().commandQueue.tempBasalPercent(percent, durationInMinutes, true, profile, callback) + } else { + ConfigBuilderPlugin.getPlugin().commandQueue.tempBasalAbsolute(absolute, durationInMinutes, true, profile, callback) + } + }) + } + return true + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/TempTargetDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/TempTargetDialog.kt new file mode 100644 index 0000000000..5408105a71 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/TempTargetDialog.kt @@ -0,0 +1,152 @@ +package info.nightscout.androidaps.dialogs + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.AdapterView +import android.widget.ArrayAdapter +import com.google.common.base.Joiner +import com.google.common.collect.Lists +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.Source +import info.nightscout.androidaps.db.TempTarget +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.DefaultValueHelper +import info.nightscout.androidaps.utils.HtmlHelper +import info.nightscout.androidaps.utils.OKDialog +import info.nightscout.androidaps.utils.SP +import kotlinx.android.synthetic.main.dialog_temptarget.* +import kotlinx.android.synthetic.main.okcancel.* +import java.text.DecimalFormat +import java.util.* + +class TempTargetDialog : DialogFragmentWithDate() { + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("overview_temptarget_duration", overview_temptarget_duration.value) + savedInstanceState.putDouble("overview_temptarget_temptarget", overview_temptarget_temptarget.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateViewGeneral() + return inflater.inflate(R.layout.dialog_temptarget, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + overview_temptarget_duration.setParams(savedInstanceState?.getDouble("overview_temptarget_duration") + ?: 0.0, 0.0, Constants.MAX_PROFILE_SWITCH_DURATION, 10.0, DecimalFormat("0"), false, ok) + + if (ProfileFunctions.getSystemUnits() == Constants.MMOL) + overview_temptarget_temptarget.setParams( + savedInstanceState?.getDouble("overview_temptarget_temptarget") + ?: Constants.MIN_TT_MMOL, + Constants.MIN_TT_MMOL, Constants.MAX_TT_MMOL, 0.1, DecimalFormat("0.0"), false, ok) + else + overview_temptarget_temptarget.setParams( + savedInstanceState?.getDouble("overview_temptarget_temptarget") + ?: Constants.MIN_TT_MGDL, + Constants.MIN_TT_MGDL, Constants.MAX_TT_MGDL, 1.0, DecimalFormat("0"), false, ok) + + val units = ProfileFunctions.getSystemUnits() + overview_temptarget_units.text = if (units == Constants.MMOL) MainApp.gs(R.string.mmol) else MainApp.gs(R.string.mgdl) + // temp target + context?.let { context -> + val reasonList: List = Lists.newArrayList( + MainApp.gs(R.string.manual), + MainApp.gs(R.string.cancel), + MainApp.gs(R.string.eatingsoon), + MainApp.gs(R.string.activity), + MainApp.gs(R.string.hypo) + ) + val adapterReason = ArrayAdapter(context, R.layout.spinner_centered, reasonList) + overview_temptarget_reason.adapter = adapterReason + overview_temptarget_reason.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onItemSelected(parent: AdapterView<*>?, view: View, position: Int, id: Long) { + val defaultDuration: Double + val defaultTarget: Double + when (reasonList[position]) { + MainApp.gs(R.string.eatingsoon) -> { + defaultDuration = DefaultValueHelper.determineEatingSoonTTDuration().toDouble() + defaultTarget = DefaultValueHelper.determineEatingSoonTT() + } + + MainApp.gs(R.string.activity) -> { + defaultDuration = DefaultValueHelper.determineActivityTTDuration().toDouble() + defaultTarget = DefaultValueHelper.determineActivityTT() + } + + MainApp.gs(R.string.hypo) -> { + defaultDuration = DefaultValueHelper.determineHypoTTDuration().toDouble() + defaultTarget = DefaultValueHelper.determineHypoTT() + } + + MainApp.gs(R.string.cancel) -> { + defaultDuration = 0.0 + defaultTarget = 0.0 + } + + else -> { + defaultDuration = overview_temptarget_duration.value + defaultTarget = overview_temptarget_temptarget.value + } + } + overview_temptarget_temptarget.value = defaultTarget + overview_temptarget_duration.value = defaultDuration + } + + override fun onNothingSelected(parent: AdapterView<*>?) {} + } + } + } + + override fun submit(): Boolean { + val actions: LinkedList = LinkedList() + val reason = overview_temptarget_reason.selectedItem.toString() + val unitResId = if (ProfileFunctions.getSystemUnits() == Constants.MGDL) R.string.mgdl else R.string.mmol + val target = overview_temptarget_temptarget.value + val duration = overview_temptarget_duration.value + if (target != 0.0 && duration != 0.0) { + actions.add(MainApp.gs(R.string.reason) + ": " + reason) + actions.add(MainApp.gs(R.string.nsprofileview_target_label) + ": " + Profile.toCurrentUnitsString(target) + " " + MainApp.gs(unitResId)) + actions.add(MainApp.gs(R.string.duration) + ": " + MainApp.gs(R.string.format_hours, duration)) + } else { + actions.add(MainApp.gs(R.string.stoptemptarget)) + } + if (eventTimeChanged) + actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(eventTime)) + + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.careportal_temporarytarget), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + if (target == 0.0 || duration == 0.0) { + val tempTarget = TempTarget() + .date(eventTime) + .duration(0) + .low(0.0).high(0.0) + .source(Source.USER) + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) + } else { + val tempTarget = TempTarget() + .date(eventTime) + .duration(duration.toInt()) + .reason(reason) + .source(Source.USER) + .low(Profile.toMgdl(target, ProfileFunctions.getSystemUnits())) + .high(Profile.toMgdl(target, ProfileFunctions.getSystemUnits())) + TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) + } + if (duration == 10.0) SP.putBoolean(R.string.key_objectiveusetemptarget, true) + }) + } + return true + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/TreatmentDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/TreatmentDialog.kt new file mode 100644 index 0000000000..8667befaf2 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/TreatmentDialog.kt @@ -0,0 +1,132 @@ +package info.nightscout.androidaps.dialogs + +import android.content.Intent +import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.google.common.base.Joiner +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.ErrorHelperActivity +import info.nightscout.androidaps.data.DetailedBolusInfo +import info.nightscout.androidaps.db.CareportalEvent +import info.nightscout.androidaps.db.Source +import info.nightscout.androidaps.interfaces.Constraint +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin +import info.nightscout.androidaps.queue.Callback +import info.nightscout.androidaps.utils.DecimalFormatter +import info.nightscout.androidaps.utils.HtmlHelper +import info.nightscout.androidaps.utils.OKDialog +import info.nightscout.androidaps.utils.SafeParse +import info.nightscout.androidaps.utils.ToastUtils +import kotlinx.android.synthetic.main.dialog_treatment.* +import kotlinx.android.synthetic.main.okcancel.* +import java.text.DecimalFormat +import java.util.* +import kotlin.math.abs + +class TreatmentDialog : DialogFragmentWithDate() { + private var maxCarbs = MainApp.getConstraintChecker().maxCarbsAllowed.value().toDouble() + private var maxInsulin = MainApp.getConstraintChecker().maxBolusAllowed.value() + + private val textWatcher: TextWatcher = object : TextWatcher { + override fun afterTextChanged(s: Editable) {} + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { + validateInputs() + } + } + + private fun validateInputs() { + if (SafeParse.stringToInt(overview_treatment_carbs.text) > maxCarbs) { + overview_treatment_carbs.value = 0.0 + ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.carbsconstraintapplied)) + } + if (SafeParse.stringToDouble(overview_treatment_insulin.text) > maxInsulin) { + overview_treatment_insulin.value = 0.0 + ToastUtils.showToastInUiThread(MainApp.instance().applicationContext, MainApp.gs(R.string.bolusconstraintapplied)) + } + } + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putDouble("overview_treatment_carbs", overview_treatment_carbs.value) + savedInstanceState.putDouble("overview_treatment_insulin", overview_treatment_insulin.value) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + onCreateViewGeneral() + return inflater.inflate(R.layout.dialog_treatment, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription ?: return + overview_treatment_carbs.setParams(savedInstanceState?.getDouble("overview_treatment_carbs") + ?: 0.0, 0.0, maxCarbs, 1.0, DecimalFormat("0"), false, ok, textWatcher) + overview_treatment_insulin.setParams(savedInstanceState?.getDouble("overview_treatment_insulin") + ?: 0.0, 0.0, maxInsulin, pumpDescription.bolusStep, DecimalFormatter.pumpSupportedBolusFormat(), false, ok, textWatcher) + } + + override fun submit(): Boolean { + val pumpDescription = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription + ?: return false + val insulin = SafeParse.stringToDouble(overview_treatment_insulin.text) + val carbs = SafeParse.stringToInt(overview_treatment_carbs.text) + val recordOnlyChecked = overview_treatment_record_only.isChecked + val actions: LinkedList = LinkedList() + val insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(Constraint(insulin)).value() + val carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(Constraint(carbs)).value() + + if (insulinAfterConstraints > 0) { + actions.add(MainApp.gs(R.string.bolus) + ": " + "" + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints) + MainApp.gs(R.string.insulin_unit_shortname) + "") + if (recordOnlyChecked) + actions.add("" + MainApp.gs(R.string.bolusrecordedonly) + "") + if (abs(insulinAfterConstraints - insulin) > pumpDescription.pumpType.determineCorrectBolusStepSize(insulinAfterConstraints)) + actions.add(MainApp.gs(R.string.bolusconstraintappliedwarning, MainApp.gc(R.color.warning), insulin, insulinAfterConstraints)) + } + if (carbsAfterConstraints > 0) { + actions.add(MainApp.gs(R.string.carbs) + ": " + "" + MainApp.gs(R.string.format_carbs, carbsAfterConstraints) + "") + if (carbsAfterConstraints != carbs) + actions.add("" + MainApp.gs(R.string.carbsconstraintapplied) + "") + } + if (insulinAfterConstraints > 0 || carbsAfterConstraints > 0) { + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.overview_treatment_label), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { + val detailedBolusInfo = DetailedBolusInfo() + if (insulinAfterConstraints == 0.0) detailedBolusInfo.eventType = CareportalEvent.CARBCORRECTION + if (carbsAfterConstraints == 0) detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS + detailedBolusInfo.insulin = insulinAfterConstraints + detailedBolusInfo.carbs = carbsAfterConstraints.toDouble() + detailedBolusInfo.context = context + detailedBolusInfo.source = Source.USER + if (!(recordOnlyChecked && (detailedBolusInfo.insulin > 0 || pumpDescription.storesCarbInfo))) { + ConfigBuilderPlugin.getPlugin().commandQueue.bolus(detailedBolusInfo, object : Callback() { + override fun run() { + if (!result.success) { + val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) + i.putExtra("soundid", R.raw.boluserror) + i.putExtra("status", result.comment) + i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror)) + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + MainApp.instance().startActivity(i) + } + } + }) + } else + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false) + }) + } + } else + activity?.let { activity -> + OKDialog.show(activity, MainApp.gs(R.string.overview_treatment_label), MainApp.gs(R.string.no_action_selected)) + } + return true + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/WizardDialog.kt similarity index 79% rename from app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt rename to app/src/main/java/info/nightscout/androidaps/dialogs/WizardDialog.kt index 46462b0dfc..c84bd4a0b6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/WizardDialog.kt @@ -1,10 +1,13 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs +package info.nightscout.androidaps.dialogs -import android.content.Context import android.os.Bundle import android.text.Editable import android.text.TextWatcher -import android.view.* +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.view.Window +import android.view.WindowManager import android.widget.AdapterView import android.widget.AdapterView.OnItemSelectedListener import android.widget.ArrayAdapter @@ -26,7 +29,7 @@ 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.overview_wizard_dialog.* +import kotlinx.android.synthetic.main.dialog_wizard.* import org.slf4j.LoggerFactory import java.text.DecimalFormat import java.util.* @@ -36,16 +39,13 @@ class WizardDialog : DialogFragment() { private val log = LoggerFactory.getLogger(WizardDialog::class.java) private var wizard: BolusWizard? = null - private var parentContext: Context? = null //one shot guards private var okClicked: Boolean = false private val textWatcher = object : TextWatcher { override fun afterTextChanged(s: Editable) {} - override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} - override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { calculateInsulin() } @@ -53,21 +53,11 @@ class WizardDialog : DialogFragment() { private var disposable: CompositeDisposable = CompositeDisposable() - override fun onAttach(context: Context) { - super.onAttach(context) - 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 onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) savedInstanceState.putDouble("treatments_wizard_bg_input", treatments_wizard_bg_input.value) @@ -83,28 +73,28 @@ class WizardDialog : DialogFragment() { isCancelable = true dialog?.setCanceledOnTouchOutside(false) - return inflater.inflate(R.layout.overview_wizard_dialog, container, false) + return inflater.inflate(R.layout.dialog_wizard, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { loadCheckedStates() processCobCheckBox() - treatments_wizard_sbcheckbox.visibility = if (SP.getBoolean(R.string.key_usesuperbolus, false)) View.VISIBLE else View.GONE - treatments_wizard_notes_layout.visibility = if (SP.getBoolean(R.string.key_show_notes_entry_dialogs, false)) View.VISIBLE else View.GONE + treatments_wizard_sbcheckbox.visibility = SP.getBoolean(R.string.key_usesuperbolus, false).toVisibility() + treatments_wizard_notes_layout.visibility = SP.getBoolean(R.string.key_show_notes_entry_dialogs, false).toVisibility() val maxCarbs = MainApp.getConstraintChecker().maxCarbsAllowed.value() val maxCorrection = MainApp.getConstraintChecker().maxBolusAllowed.value() 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) + ?: 0.0, 0.0, 500.0, 0.1, DecimalFormat("0.0"), false, ok, textWatcher) treatments_wizard_carbs_input.setParams(savedInstanceState?.getDouble("treatments_wizard_carbs_input") - ?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, ok, textWatcher) + ?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, ok, textWatcher) val bolusStep = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription?.bolusStep - ?: 0.1 + ?: 0.1 treatments_wizard_correction_input.setParams(savedInstanceState?.getDouble("treatments_wizard_correction_input") - ?: 0.0, -maxCorrection, maxCorrection, bolusStep, DecimalFormatter.pumpSupportedBolusFormat(), false, ok, textWatcher) + ?: 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) + ?: 0.0, -60.0, 60.0, 5.0, DecimalFormat("0"), false, ok, textWatcher) initDialog() treatments_wizard_percent_used.text = MainApp.gs(R.string.format_percent, SP.getInt(R.string.key_boluswizard_percentage, 100)) @@ -115,7 +105,7 @@ class WizardDialog : DialogFragment() { } else { okClicked = true calculateInsulin() - parentContext?.let { context -> + context?.let { context -> wizard?.confirmAndExecute(context) } } @@ -124,23 +114,23 @@ class WizardDialog : DialogFragment() { // cancel button cancel.setOnClickListener { dismiss() } // checkboxes - treatments_wizard_bgcheckbox.setOnCheckedChangeListener { buttonView, _ -> onCheckedChanged(buttonView) } - treatments_wizard_ttcheckbox.setOnCheckedChangeListener { buttonView, _ -> onCheckedChanged(buttonView) } - treatments_wizard_cobcheckbox.setOnCheckedChangeListener { buttonView, _ -> onCheckedChanged(buttonView) } - treatments_wizard_basaliobcheckbox.setOnCheckedChangeListener { buttonView, _ -> onCheckedChanged(buttonView) } - treatments_wizard_bolusiobcheckbox.setOnCheckedChangeListener { buttonView, _ -> onCheckedChanged(buttonView) } - treatments_wizard_bgtrendcheckbox.setOnCheckedChangeListener { buttonView, _ -> onCheckedChanged(buttonView) } - treatments_wizard_sbcheckbox.setOnCheckedChangeListener { buttonView, _ -> onCheckedChanged(buttonView) } + treatments_wizard_bgcheckbox.setOnCheckedChangeListener(::onCheckedChanged) + treatments_wizard_ttcheckbox.setOnCheckedChangeListener(::onCheckedChanged) + treatments_wizard_cobcheckbox.setOnCheckedChangeListener(::onCheckedChanged) + treatments_wizard_basaliobcheckbox.setOnCheckedChangeListener(::onCheckedChanged) + treatments_wizard_bolusiobcheckbox.setOnCheckedChangeListener(::onCheckedChanged) + treatments_wizard_bgtrendcheckbox.setOnCheckedChangeListener(::onCheckedChanged) + treatments_wizard_sbcheckbox.setOnCheckedChangeListener(::onCheckedChanged) 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_delimiter.visibility = showCalc.toVisibility() + treatments_wizard_resulttable.visibility = showCalc.toVisibility() 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 + treatments_wizard_delimiter.visibility = isChecked.toVisibility() + treatments_wizard_resulttable.visibility = isChecked.toVisibility() } } // profile spinner @@ -157,13 +147,13 @@ class WizardDialog : DialogFragment() { } // bus disposable.add(RxBus - .toObservable(EventAutosensCalculationFinished::class.java) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - activity?.runOnUiThread { calculateInsulin() } - }, { - FabricPrivacy.logException(it) - }) + .toObservable(EventAutosensCalculationFinished::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + activity?.runOnUiThread { calculateInsulin() } + }, { + FabricPrivacy.logException(it) + }) ) } @@ -173,7 +163,7 @@ class WizardDialog : DialogFragment() { disposable.clear() } - private fun onCheckedChanged(buttonView: CompoundButton) { + private fun onCheckedChanged(buttonView: CompoundButton, @Suppress("UNUSED_PARAMETER") state: Boolean) { saveCheckedStates() treatments_wizard_ttcheckbox.isEnabled = treatments_wizard_bgcheckbox.isChecked && TreatmentsPlugin.getPlugin().tempTargetFromHistory != null if (buttonView.id == treatments_wizard_cobcheckbox.id) @@ -221,7 +211,6 @@ class WizardDialog : DialogFragment() { treatments_wizard_profile.adapter = adapter } ?: return - val units = ProfileFunctions.getSystemUnits() treatments_wizard_bgunits.text = units if (units == Constants.MGDL) @@ -250,7 +239,7 @@ class WizardDialog : DialogFragment() { calculateInsulin() - treatments_wizard_percent_used.visibility = if (SP.getInt(R.string.key_boluswizard_percentage, 100) != 100) View.VISIBLE else View.GONE + treatments_wizard_percent_used.visibility = (SP.getInt(R.string.key_boluswizard_percentage, 100) != 100).toVisibility() } private fun calculateInsulin() { @@ -291,15 +280,15 @@ class WizardDialog : DialogFragment() { val carbTime = SafeParse.stringToInt(treatments_wizard_carb_time_input.text) 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, - treatments_wizard_bolusiobcheckbox.isChecked, - treatments_wizard_basaliobcheckbox.isChecked, - treatments_wizard_sbcheckbox.isChecked, - treatments_wizard_ttcheckbox.isChecked, - treatments_wizard_bgtrendcheckbox.isChecked, - treatment_wizard_notes.text.toString(), carbTime) + SP.getInt(R.string.key_boluswizard_percentage, 100).toDouble(), + treatments_wizard_bgcheckbox.isChecked, + treatments_wizard_cobcheckbox.isChecked, + treatments_wizard_bolusiobcheckbox.isChecked, + treatments_wizard_basaliobcheckbox.isChecked, + treatments_wizard_sbcheckbox.isChecked, + treatments_wizard_ttcheckbox.isChecked, + treatments_wizard_bgtrendcheckbox.isChecked, + treatment_wizard_notes.text.toString(), carbTime) wizard?.let { wizard -> treatments_wizard_bg.text = String.format(MainApp.gs(R.string.format_bg_isf), BgReading().value(Profile.toMgdl(bg, ProfileFunctions.getSystemUnits())).valueToUnitsToString(ProfileFunctions.getSystemUnits()), wizard.sens) @@ -320,8 +309,8 @@ class WizardDialog : DialogFragment() { // Trend if (treatments_wizard_bgtrendcheckbox.isChecked && wizard.glucoseStatus != null) { treatments_wizard_bgtrend.text = ((if (wizard.trend > 0) "+" else "") - + Profile.toUnitsString(wizard.trend * 3, wizard.trend * 3 / Constants.MMOLL_TO_MGDL, ProfileFunctions.getSystemUnits()) - + " " + ProfileFunctions.getSystemUnits()) + + Profile.toUnitsString(wizard.trend * 3, wizard.trend * 3 / Constants.MMOLL_TO_MGDL, ProfileFunctions.getSystemUnits()) + + " " + ProfileFunctions.getSystemUnits()) } else { treatments_wizard_bgtrend.text = "" } 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 8de88e1e0e..e236125255 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java @@ -4,7 +4,6 @@ import android.os.SystemClock; import android.preference.Preference; import android.preference.PreferenceFragment; -import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.FragmentActivity; import org.jetbrains.annotations.NotNull; @@ -20,6 +19,7 @@ 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.OKDialog; import info.nightscout.androidaps.utils.SP; /** @@ -58,20 +58,16 @@ public abstract class PluginBase { if (allowHardwarePump || activity == null) { performPluginSwitch(newState, type); } else { - AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setMessage(R.string.allow_hardware_pump_text) - .setPositiveButton(R.string.yes, (dialog, id) -> { - performPluginSwitch(newState, type); - SP.putBoolean("allow_hardware_pump", true); - if (L.isEnabled(L.PUMP)) - log.debug("First time HW pump allowed!"); - }) - .setNegativeButton(R.string.cancel, (dialog, id) -> { - RxBus.INSTANCE.send(new EventConfigBuilderUpdateGui()); - if (L.isEnabled(L.PUMP)) - log.debug("User does not allow switching to HW pump!"); - }); - builder.create().show(); + OKDialog.showConfirmation(activity, MainApp.gs(R.string.allow_hardware_pump_text), () -> { + performPluginSwitch(newState, type); + SP.putBoolean("allow_hardware_pump", true); + if (L.isEnabled(L.PUMP)) + log.debug("First time HW pump allowed!"); + }, () -> { + RxBus.INSTANCE.send(new EventConfigBuilderUpdateGui()); + if (L.isEnabled(L.PUMP)) + log.debug("User does not allow switching to HW pump!"); + }); } } else { performPluginSwitch(newState, type); 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 f339bd3771..7492dc64dd 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 @@ -50,7 +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.activities.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; @@ -60,7 +60,6 @@ import info.nightscout.androidaps.queue.commands.Command; 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; 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 86e0f39437..2670e78633 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 @@ -2,7 +2,11 @@ package info.nightscout.androidaps.plugins.configBuilder import android.content.Intent import android.view.View -import android.widget.* +import android.widget.CheckBox +import android.widget.ImageButton +import android.widget.LinearLayout +import android.widget.RadioButton +import android.widget.TextView import info.nightscout.androidaps.R import info.nightscout.androidaps.activities.PreferencesActivity import info.nightscout.androidaps.events.EventRebuildTabs @@ -10,6 +14,7 @@ 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 +import info.nightscout.androidaps.utils.toVisibility class PluginViewHolder internal constructor(private val fragment: ConfigBuilderFragment, private val pluginType: PluginType, @@ -56,8 +61,8 @@ class PluginViewHolder internal constructor(private val fragment: ConfigBuilderF } fun update() { - enabledExclusive.visibility = if (areMultipleSelectionsAllowed(pluginType)) View.GONE else View.VISIBLE - enabledInclusive.visibility = if (areMultipleSelectionsAllowed(pluginType)) View.VISIBLE else View.GONE + enabledExclusive.visibility = areMultipleSelectionsAllowed(pluginType).not().toVisibility() + enabledInclusive.visibility = areMultipleSelectionsAllowed(pluginType).toVisibility() enabledExclusive.isChecked = plugin.isEnabled(pluginType) enabledInclusive.isChecked = plugin.isEnabled(pluginType) enabledInclusive.isEnabled = !plugin.pluginDescription.alwaysEnabled @@ -70,7 +75,7 @@ class PluginViewHolder internal constructor(private val fragment: ConfigBuilderF pluginDescription.text = plugin.description } pluginPreferences.visibility = if (plugin.preferencesId == -1 || !plugin.isEnabled(pluginType)) View.INVISIBLE else View.VISIBLE - pluginVisibility.visibility = if (plugin.hasFragment()) View.VISIBLE else View.INVISIBLE + pluginVisibility.visibility = plugin.hasFragment().toVisibility() pluginVisibility.isEnabled = !(plugin.pluginDescription.neverVisible || plugin.pluginDescription.alwaysVisible) && plugin.isEnabled(pluginType) pluginVisibility.isChecked = plugin.isFragmentVisible } 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 63da02e2bd..bdde6606bb 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 @@ -24,7 +24,7 @@ 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.activities.ErrorHelperActivity; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.DateUtil; @@ -170,8 +170,8 @@ public class ProfileFunctions { return profileSwitch; } - public static void doProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift) { - ProfileSwitch profileSwitch = prepareProfileSwitch(profileStore, profileName, duration, percentage, timeshift, System.currentTimeMillis()); + public static void doProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift, final long date) { + ProfileSwitch profileSwitch = prepareProfileSwitch(profileStore, profileName, duration, percentage, timeshift, date); TreatmentsPlugin.getPlugin().addToHistoryProfileSwitch(profileSwitch); if (percentage == 90 && duration == 10) SP.putBoolean(R.string.key_objectiveuseprofileswitch, true); 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 9864f875e4..7f004b7f02 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 @@ -72,13 +72,13 @@ class ObjectivesFragment : Fragment() { override fun onResume() { super.onResume() disposable.add(RxBus - .toObservable(EventObjectivesUpdateGui::class.java) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - objectives_recyclerview.adapter?.notifyDataSetChanged() - }, { - FabricPrivacy.logException(it) - }) + .toObservable(EventObjectivesUpdateGui::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + objectives_recyclerview.adapter?.notifyDataSetChanged() + }, { + FabricPrivacy.logException(it) + }) ) } @@ -285,11 +285,13 @@ class ObjectivesFragment : Fragment() { }.start() } holder.unStart.setOnClickListener { - OKDialog.showConfirmation(activity, MainApp.gs(R.string.doyouwantresetstart)) { - objective.startedOn = 0 - scrollToCurrentObjective() - RxBus.send(EventObjectivesUpdateGui()) - RxBus.send(EventSWUpdate(false)) + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.objectives), MainApp.gs(R.string.doyouwantresetstart), Runnable { + objective.startedOn = 0 + scrollToCurrentObjective() + RxBus.send(EventObjectivesUpdateGui()) + RxBus.send(EventSWUpdate(false)) + }) } } holder.unFinish.setOnClickListener { @@ -320,7 +322,6 @@ class ObjectivesFragment : Fragment() { } } - override fun getItemCount(): Int { return ObjectivesPlugin.objectives.size } 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 f8b0ca26ec..ebae80150c 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 @@ -122,9 +122,9 @@ object ObjectivesPlugin : PluginBase(PluginDescription() SP.putLong("Objectives_" + "smb" + "_started", DateUtil.now()) SP.putLong("Objectives_" + "smb" + "_accomplished", DateUtil.now()) setupObjectives() - OKDialog.show(activity, "", MainApp.gs(R.string.codeaccepted), null) + OKDialog.show(activity, MainApp.gs(R.string.objectives), MainApp.gs(R.string.codeaccepted)) } else { - OKDialog.show(activity, "", MainApp.gs(R.string.codeinvalid), null) + OKDialog.show(activity, MainApp.gs(R.string.objectives), MainApp.gs(R.string.codeinvalid)) } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/dialogs/NtpProgressDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/dialogs/NtpProgressDialog.kt index cef9bc232a..77e6f22b43 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/dialogs/NtpProgressDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/dialogs/NtpProgressDialog.kt @@ -14,7 +14,7 @@ import info.nightscout.androidaps.plugins.constraints.objectives.events.EventNtp import info.nightscout.androidaps.utils.FabricPrivacy import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable -import kotlinx.android.synthetic.main.overview_bolusprogress_dialog.* +import kotlinx.android.synthetic.main.dialog_bolusprogress.* import org.slf4j.LoggerFactory class NtpProgressDialog : DialogFragment() { @@ -33,16 +33,17 @@ class NtpProgressDialog : DialogFragment() { state = savedInstanceState?.getString("state", DEFAULT_STATE) ?: DEFAULT_STATE percent = savedInstanceState?.getInt("percent", 0) ?: 0 - return inflater.inflate(R.layout.overview_bolusprogress_dialog, container, false) + return inflater.inflate(R.layout.dialog_bolusprogress, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) overview_bolusprogress_stop.setOnClickListener { dismiss() } - overview_bolusprogress_status.setText(state) - overview_bolusprogress_progressbar.setMax(100) - overview_bolusprogress_progressbar.setProgress(percent) + overview_bolusprogress_status.text = state + overview_bolusprogress_progressbar.max = 100 + overview_bolusprogress_progressbar.progress = percent overview_bolusprogress_stop.text = MainApp.gs(R.string.close) + overview_bolusprogress_title.text = MainApp.gs(R.string.please_wait) } override fun onResume() { 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 index dfc06e6156..5143cdebc5 100644 --- 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 @@ -7,6 +7,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.LinearLayout +import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import info.nightscout.androidaps.Config import info.nightscout.androidaps.MainApp @@ -18,17 +19,25 @@ 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.dialogs.CareDialog +import info.nightscout.androidaps.dialogs.ExtendedBolusDialog +import info.nightscout.androidaps.dialogs.FillDialog +import info.nightscout.androidaps.dialogs.TempBasalDialog import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment -import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog +import info.nightscout.androidaps.activities.ErrorHelperActivity +import info.nightscout.androidaps.dialogs.ProfileSwitchDialog +import info.nightscout.androidaps.dialogs.TempTargetDialog import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.queue.Callback -import info.nightscout.androidaps.utils.* +import info.nightscout.androidaps.utils.FabricPrivacy +import info.nightscout.androidaps.utils.SP +import info.nightscout.androidaps.utils.SingleClickButton +import info.nightscout.androidaps.utils.plusAssign +import info.nightscout.androidaps.utils.toVisibility import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import kotlinx.android.synthetic.main.actions_fragment.* +import kotlinx.android.synthetic.main.careportal_stats_fragment.* import java.util.* class ActionsFragment : Fragment() { @@ -47,39 +56,45 @@ class ActionsFragment : Fragment() { 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") } + fragmentManager?.let { ProfileSwitchDialog().show(it, "Actions") } } 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") } + fragmentManager?.let { TempTargetDialog().show(it, "Actions") } } actions_extendedbolus.setOnClickListener { - fragmentManager?.let { NewExtendedBolusDialog().show(it, "NewExtendedDialog") } + fragmentManager?.let { ExtendedBolusDialog().show(it, "Actions") } } 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)) + if (!result.success) { + val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) + 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) + } } }) } } - actions_settempbasal.setOnClickListener { fragmentManager?.let { NewTempBasalDialog().show(it, "NewTempDialog") } } + actions_settempbasal.setOnClickListener { + fragmentManager?.let { TempBasalDialog().show(it, "Actions") } + } 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)) + if (!result.success) { + val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) + 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) + } } }) } @@ -87,6 +102,15 @@ class ActionsFragment : Fragment() { 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)) } + actions_bgcheck.setOnClickListener { + fragmentManager?.let { CareDialog().setOptions(CareDialog.EventType.BGCHECK, R.string.careportal_bgcheck).show(it, "Actions") } + } + actions_cgmsensorinsert.setOnClickListener { + fragmentManager?.let { CareDialog().setOptions(CareDialog.EventType.SENSOR_INSERT, R.string.careportal_cgmsensorinsert).show(it, "Actions") } + } + actions_pumpbatterychange.setOnClickListener { + fragmentManager?.let { CareDialog().setOptions(CareDialog.EventType.BATTERY_CHANGE, R.string.careportal_pumpbatterychange).show(it, "Actions") } + } SP.putBoolean(R.string.key_objectiveuseactions, true) } @@ -97,43 +121,27 @@ class ActionsFragment : Fragment() { disposable += RxBus .toObservable(EventInitializationChanged::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGui() - }, { - FabricPrivacy.logException(it) - }) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) disposable += RxBus .toObservable(EventRefreshOverview::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGui() - }, { - FabricPrivacy.logException(it) - }) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) disposable += RxBus .toObservable(EventExtendedBolusChange::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGui() - }, { - FabricPrivacy.logException(it) - }) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) disposable += RxBus .toObservable(EventTempBasalChange::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGui() - }, { - FabricPrivacy.logException(it) - }) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) disposable += RxBus .toObservable(EventCustomActionsChanged::class.java) .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGui() - }, { - FabricPrivacy.logException(it) - }) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) + disposable += RxBus + .toObservable(EventCareportalEventChange::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGui() }, { FabricPrivacy.logException(it) }) updateGui() } @@ -198,8 +206,11 @@ class ActionsFragment : Fragment() { 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 + actions_temptarget?.visibility = Config.APS.toVisibility() + actions_tddstats?.visibility = pump.pumpDescription.supportsTDDs.toVisibility() + activity?.let { activity -> + CareportalFragment.updateAge(activity, careportal_sensorage, careportal_insulinage, careportal_canulaage, careportal_pbage) + } checkPumpCustomActions() } @@ -224,8 +235,7 @@ class ActionsFragment : Fragment() { val action = this.pumpCustomActions[b.text.toString()] ConfigBuilderPlugin.getPlugin().activePump!!.executeCustomAction(action!!.customActionType) } - - val top = resources.getDrawable(customAction.iconResourceId) + val top = activity?.let { ContextCompat.getDrawable(it, customAction.iconResourceId) } btn.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null) action_buttons_layout?.addView(btn) 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 04afb826a2..e534fefa26 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 @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.actions +import info.nightscout.androidaps.Config import info.nightscout.androidaps.R import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.PluginDescription @@ -8,6 +9,8 @@ import info.nightscout.androidaps.interfaces.PluginType object ActionsPlugin : PluginBase(PluginDescription() .mainType(PluginType.GENERAL) .fragmentClass(ActionsFragment::class.qualifiedName) + .enableByDefault(Config.APS || Config.PUMPCONTROL) + .visibleByDefault(Config.APS || Config.PUMPCONTROL) .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/actions/defs/CustomAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomAction.java deleted file mode 100644 index 5e5f707928..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomAction.java +++ /dev/null @@ -1,63 +0,0 @@ -package info.nightscout.androidaps.plugins.general.actions.defs; - -import info.nightscout.androidaps.R; - -/** - * Created by andy on 9/20/18. - */ - -public class CustomAction { - - private int name; - private String iconName; - private CustomActionType customActionType; - private int iconResourceId; - private boolean enabled = true; - - - public CustomAction(int nameResourceId, CustomActionType actionType) { - this(nameResourceId, actionType, R.drawable.icon_actions_profileswitch, true); - } - - - public CustomAction(int nameResourceId, CustomActionType actionType, int iconResourceId) { - this(nameResourceId, actionType, iconResourceId, true); - } - - public CustomAction(int nameResourceId, CustomActionType actionType, boolean enabled) { - this(nameResourceId, actionType, R.drawable.icon_actions_profileswitch, enabled); - } - - public CustomAction(int nameResourceId, CustomActionType actionType, int iconResourceId, boolean enabled) { - this.name = nameResourceId; - this.customActionType = actionType; - this.iconResourceId = iconResourceId; - this.enabled = enabled; - } - - - public int getName() { - return name; - } - - - public CustomActionType getCustomActionType() { - return customActionType; - } - - - public int getIconResourceId() { - return iconResourceId; - } - - - public boolean isEnabled() { - return enabled; - } - - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomAction.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomAction.kt new file mode 100644 index 0000000000..fe9af4a03e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomAction.kt @@ -0,0 +1,10 @@ +package info.nightscout.androidaps.plugins.general.actions.defs + +import info.nightscout.androidaps.R + +class CustomAction @JvmOverloads constructor(val name: Int, val customActionType: CustomActionType?, val iconResourceId: Int = R.drawable.icon_actions_profileswitch, var isEnabled: Boolean = true) { + + constructor(nameResourceId: Int, actionType: CustomActionType?, enabled: Boolean) : + this(nameResourceId, actionType, R.drawable.icon_actions_profileswitch, enabled) + +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomActionType.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomActionType.java deleted file mode 100644 index 74ecf3c0e3..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomActionType.java +++ /dev/null @@ -1,11 +0,0 @@ -package info.nightscout.androidaps.plugins.general.actions.defs; - -/** - * Created by andy on 9/20/18. - */ - -public interface CustomActionType { - - String getKey(); - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomActionType.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomActionType.kt new file mode 100644 index 0000000000..fb472d5592 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/defs/CustomActionType.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.plugins.general.actions.defs + +interface CustomActionType { + val key: String? +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/FillDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/FillDialog.java deleted file mode 100644 index 77e4f85ea0..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/FillDialog.java +++ /dev/null @@ -1,255 +0,0 @@ -package info.nightscout.androidaps.plugins.general.actions.dialogs; - -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import androidx.fragment.app.DialogFragment; -import androidx.appcompat.app.AlertDialog; -import android.text.Editable; -import android.text.Html; -import android.text.TextWatcher; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.view.Window; -import android.view.WindowManager; -import android.widget.Button; -import android.widget.CheckBox; -import android.widget.EditText; -import android.widget.LinearLayout; - -import com.google.common.base.Joiner; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.LinkedList; -import java.util.List; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.DetailedBolusInfo; -import info.nightscout.androidaps.db.CareportalEvent; -import info.nightscout.androidaps.db.Source; -import info.nightscout.androidaps.interfaces.Constraint; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; -import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperActivity; -import info.nightscout.androidaps.queue.Callback; -import info.nightscout.androidaps.utils.DecimalFormatter; -import info.nightscout.androidaps.utils.NumberPicker; -import info.nightscout.androidaps.utils.SP; -import info.nightscout.androidaps.utils.SafeParse; -import info.nightscout.androidaps.utils.ToastUtils; - -import static info.nightscout.androidaps.utils.DateUtil.now; - -public class FillDialog extends DialogFragment implements OnClickListener { - private static Logger log = LoggerFactory.getLogger(FillDialog.class); - - private CheckBox pumpSiteChangeCheckbox; - private CheckBox insulinCartridgeChangeCheckbox; - - private NumberPicker editInsulin; - - double amount1 = 0d; - double amount2 = 0d; - double amount3 = 0d; - - private EditText notesEdit; - - //one shot guards - private boolean accepted; - private boolean okClicked; - - final private TextWatcher textWatcher = new TextWatcher() { - @Override - public void afterTextChanged(Editable s) { - validateInputs(); - } - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - }; - - private void validateInputs() { - int time = editInsulin.getValue().intValue(); - if (Math.abs(time) > 12 * 60) { - editInsulin.setValue(0d); - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.constraintapllied)); - } - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.actions_fill_dialog, container, false); - - view.findViewById(R.id.ok).setOnClickListener(this); - view.findViewById(R.id.cancel).setOnClickListener(this); - - getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); - getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - - pumpSiteChangeCheckbox = view.findViewById(R.id.fill_catheter_change); - insulinCartridgeChangeCheckbox = view.findViewById(R.id.fill_cartridge_change); - - Double maxInsulin = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); - double bolusstep = ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription().bolusStep; - editInsulin = view.findViewById(R.id.fill_insulinamount); - editInsulin.setParams(0d, 0d, maxInsulin, bolusstep, DecimalFormatter.pumpSupportedBolusFormat(), false, view.findViewById(R.id.ok), textWatcher); - - - Button preset1Button = view.findViewById(R.id.fill_preset_button1); - amount1 = SP.getDouble("fill_button1", 0.3); - if (amount1 > 0) { - preset1Button.setVisibility(View.VISIBLE); - preset1Button.setText(DecimalFormatter.toPumpSupportedBolus(amount1)); // + "U"); - preset1Button.setOnClickListener(this); - } else { - preset1Button.setVisibility(View.GONE); - } - Button preset2Button = view.findViewById(R.id.fill_preset_button2); - amount2 = SP.getDouble("fill_button2", 0d); - if (amount2 > 0) { - preset2Button.setVisibility(View.VISIBLE); - preset2Button.setText(DecimalFormatter.toPumpSupportedBolus(amount2)); // + "U"); - preset2Button.setOnClickListener(this); - } else { - preset2Button.setVisibility(View.GONE); - } - Button preset3Button = view.findViewById(R.id.fill_preset_button3); - amount3 = SP.getDouble("fill_button3", 0d); - if (amount3 > 0) { - preset3Button.setVisibility(View.VISIBLE); - preset3Button.setText(DecimalFormatter.toPumpSupportedBolus(amount3)); // + "U"); - preset3Button.setOnClickListener(this); - } else { - preset3Button.setVisibility(View.GONE); - } - - LinearLayout notesLayout = view.findViewById(R.id.fill_notes_layout); - notesLayout.setVisibility(SP.getBoolean(R.string.key_show_notes_entry_dialogs, false) ? View.VISIBLE : View.GONE); - notesEdit = view.findViewById(R.id.fill_notes); - - setCancelable(true); - getDialog().setCanceledOnTouchOutside(false); - return view; - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.ok: - confirmAndDeliver(); - break; - case R.id.cancel: - dismiss(); - break; - case R.id.fill_preset_button1: - editInsulin.setValue(amount1); - break; - case R.id.fill_preset_button2: - editInsulin.setValue(amount2); - break; - case R.id.fill_preset_button3: - editInsulin.setValue(amount3); - break; - } - - } - - private synchronized void confirmAndDeliver() { - if (okClicked) { - log.debug("guarding: ok already clicked"); - dismiss(); - return; - } - okClicked = true; - - try { - Double insulin = SafeParse.stringToDouble(editInsulin.getText()); - - List confirmMessage = new LinkedList<>(); - - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); - if (insulinAfterConstraints > 0) { - confirmMessage.add(MainApp.gs(R.string.fillwarning)); - confirmMessage.add(""); - confirmMessage.add(MainApp.gs(R.string.bolus) + ": " + "" + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints) + "U" + ""); - if (Math.abs(insulinAfterConstraints - insulin) > 0.01d) - confirmMessage.add(MainApp.gs(R.string.bolusconstraintappliedwarning, MainApp.gc(R.color.warning), insulin, insulinAfterConstraints)); - } - - if (pumpSiteChangeCheckbox.isChecked()) - confirmMessage.add("" + "" + MainApp.gs(R.string.record_pump_site_change) + ""); - - if (insulinCartridgeChangeCheckbox.isChecked()) - confirmMessage.add("" + "" + MainApp.gs(R.string.record_insulin_cartridge_change) + ""); - - final String notes = notesEdit.getText().toString(); - if (!notes.isEmpty()) { - confirmMessage.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes); - } - - final Double finalInsulinAfterConstraints = insulinAfterConstraints; - - final Context context = getContext(); - AlertDialog.Builder builder = new AlertDialog.Builder(context); - - builder.setTitle(MainApp.gs(R.string.confirmation)); - if (insulinAfterConstraints > 0 || pumpSiteChangeCheckbox.isChecked() || insulinCartridgeChangeCheckbox.isChecked()) { - builder.setMessage(Html.fromHtml(Joiner.on("
").join(confirmMessage))); - builder.setPositiveButton(MainApp.gs(R.string.primefill), (dialog, id) -> { - synchronized (builder) { - if (accepted) { - log.debug("guarding: already accepted"); - return; - } - accepted = true; - - if (finalInsulinAfterConstraints > 0) { - DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); - detailedBolusInfo.insulin = finalInsulinAfterConstraints; - detailedBolusInfo.context = context; - detailedBolusInfo.source = Source.USER; - detailedBolusInfo.isValid = false; // do not count it in IOB (for pump history) - detailedBolusInfo.notes = notes; - ConfigBuilderPlugin.getPlugin().getCommandQueue().bolus(detailedBolusInfo, 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.treatmentdeliveryerror)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - } - }); - } - if (pumpSiteChangeCheckbox.isChecked()) - NSUpload.uploadEvent(CareportalEvent.SITECHANGE, now(), notes); - if (insulinCartridgeChangeCheckbox.isChecked()) - NSUpload.uploadEvent(CareportalEvent.INSULINCHANGE, now() + 1000, notes); - } - }); - } else { - builder.setMessage(MainApp.gs(R.string.no_action_selected)); - } - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - dismiss(); - } catch (RuntimeException e) { - log.error("Unhandled exception", e); - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/NewExtendedBolusDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/NewExtendedBolusDialog.java deleted file mode 100644 index 99e877efcc..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/NewExtendedBolusDialog.java +++ /dev/null @@ -1,115 +0,0 @@ -package info.nightscout.androidaps.plugins.general.actions.dialogs; - -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.os.Bundle; -import androidx.fragment.app.DialogFragment; -import androidx.appcompat.app.AlertDialog; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.text.DecimalFormat; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.interfaces.Constraint; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperActivity; -import info.nightscout.androidaps.queue.Callback; -import info.nightscout.androidaps.utils.NumberPicker; -import info.nightscout.androidaps.utils.SafeParse; - -public class NewExtendedBolusDialog extends DialogFragment implements View.OnClickListener { - private static Logger log = LoggerFactory.getLogger(NewExtendedBolusDialog.class); - - NumberPicker editInsulin; - NumberPicker editDuration; - - public NewExtendedBolusDialog() { - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - getDialog().setTitle(MainApp.gs(R.string.overview_extendedbolus_button)); - - View view = inflater.inflate(R.layout.overview_newextendedbolus_dialog, container, false); - - Double maxInsulin = MainApp.getConstraintChecker().getMaxExtendedBolusAllowed().value(); - editInsulin = (NumberPicker) view.findViewById(R.id.overview_newextendedbolus_insulin); - editInsulin.setParams(0d, 0d, maxInsulin, 0.1d, new DecimalFormat("0.00"), false, view.findViewById(R.id.ok)); - - double extendedDurationStep = ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription().extendedBolusDurationStep; - double extendedMaxDuration = ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription().extendedBolusMaxDuration; - editDuration = (NumberPicker) view.findViewById(R.id.overview_newextendedbolus_duration); - editDuration.setParams(extendedDurationStep, extendedDurationStep, extendedMaxDuration, extendedDurationStep, new DecimalFormat("0"), false, view.findViewById(R.id.ok)); - - view.findViewById(R.id.ok).setOnClickListener(this); - view.findViewById(R.id.cancel).setOnClickListener(this); - - setCancelable(true); - getDialog().setCanceledOnTouchOutside(false); - return view; - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.ok: - try { - Double insulin = SafeParse.stringToDouble(editInsulin.getText()); - int durationInMinutes = SafeParse.stringToInt(editDuration.getText()); - - String confirmMessage = MainApp.gs(R.string.setextendedbolusquestion); - - Double insulinAfterConstraint = MainApp.getConstraintChecker().applyExtendedBolusConstraints(new Constraint<>(insulin)).value(); - confirmMessage += " " + insulinAfterConstraint + " U "; - confirmMessage += MainApp.gs(R.string.duration) + " " + durationInMinutes + "min ?"; - if (Math.abs(insulinAfterConstraint - insulin) > 0.01d) - confirmMessage += "\n" + MainApp.gs(R.string.constraintapllied); - insulin = insulinAfterConstraint; - - final Double finalInsulin = insulin; - final int finalDurationInMinutes = durationInMinutes; - - final Context context = getContext(); - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(confirmMessage); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - ConfigBuilderPlugin.getPlugin().getCommandQueue().extendedBolus(finalInsulin, finalDurationInMinutes, 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.treatmentdeliveryerror)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - } - }); - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - dismiss(); - - } catch (Exception e) { - log.error("Unhandled exception", e); - } - break; - case R.id.cancel: - dismiss(); - break; - } - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/NewTempBasalDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/NewTempBasalDialog.java deleted file mode 100644 index cad2e22f85..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/actions/dialogs/NewTempBasalDialog.java +++ /dev/null @@ -1,196 +0,0 @@ -package info.nightscout.androidaps.plugins.general.actions.dialogs; - -import android.content.DialogInterface; -import android.content.Intent; -import android.os.Bundle; -import androidx.fragment.app.DialogFragment; -import androidx.appcompat.app.AlertDialog; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.LinearLayout; -import android.widget.RadioButton; -import android.widget.RadioGroup; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.text.DecimalFormat; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.Profile; -import info.nightscout.androidaps.interfaces.Constraint; -import info.nightscout.androidaps.interfaces.PumpDescription; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperActivity; -import info.nightscout.androidaps.queue.Callback; -import info.nightscout.androidaps.utils.NumberPicker; -import info.nightscout.androidaps.utils.SafeParse; - -public class NewTempBasalDialog extends DialogFragment implements View.OnClickListener, RadioGroup.OnCheckedChangeListener { - private static Logger log = LoggerFactory.getLogger(NewTempBasalDialog.class); - - RadioButton percentRadio; - RadioButton absoluteRadio; - RadioGroup basalTypeRadioGroup; - LinearLayout typeSelectorLayout; - - LinearLayout percentLayout; - LinearLayout absoluteLayout; - - NumberPicker basalPercent; - NumberPicker basalAbsolute; - NumberPicker duration; - - public NewTempBasalDialog() { - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - getDialog().setTitle(MainApp.gs(R.string.overview_tempbasal_button)); - - View view = inflater.inflate(R.layout.overview_newtempbasal_dialog, container, false); - - percentLayout = (LinearLayout) view.findViewById(R.id.overview_newtempbasal_percent_layout); - absoluteLayout = (LinearLayout) view.findViewById(R.id.overview_newtempbasal_absolute_layout); - percentRadio = (RadioButton) view.findViewById(R.id.overview_newtempbasal_percent_radio); - basalTypeRadioGroup = (RadioGroup) view.findViewById(R.id.overview_newtempbasal_radiogroup); - absoluteRadio = (RadioButton) view.findViewById(R.id.overview_newtempbasal_absolute_radio); - typeSelectorLayout = (LinearLayout) view.findViewById(R.id.overview_newtempbasal_typeselector_layout); - - PumpDescription pumpDescription = ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription(); - - basalPercent = (NumberPicker) view.findViewById(R.id.overview_newtempbasal_basalpercentinput); - double maxTempPercent = pumpDescription.maxTempPercent; - double tempPercentStep = pumpDescription.tempPercentStep; - basalPercent.setParams(100d, 0d, maxTempPercent, tempPercentStep, new DecimalFormat("0"), true, view.findViewById(R.id.ok)); - - Profile profile = ProfileFunctions.getInstance().getProfile(); - Double currentBasal = profile != null ? profile.getBasal() : 0d; - basalAbsolute = (NumberPicker) view.findViewById(R.id.overview_newtempbasal_basalabsoluteinput); - basalAbsolute.setParams(currentBasal, 0d, pumpDescription.maxTempAbsolute, pumpDescription.tempAbsoluteStep, new DecimalFormat("0.00"), true, view.findViewById(R.id.ok)); - - double tempDurationStep = pumpDescription.tempDurationStep; - double tempMaxDuration = pumpDescription.tempMaxDuration; - duration = (NumberPicker) view.findViewById(R.id.overview_newtempbasal_duration); - duration.setParams(tempDurationStep, tempDurationStep, tempMaxDuration, tempDurationStep, new DecimalFormat("0"), false, view.findViewById(R.id.ok)); - - if ((pumpDescription.tempBasalStyle & PumpDescription.PERCENT) == PumpDescription.PERCENT && (pumpDescription.tempBasalStyle & PumpDescription.ABSOLUTE) == PumpDescription.ABSOLUTE) { - // Both allowed - typeSelectorLayout.setVisibility(View.VISIBLE); - } else { - typeSelectorLayout.setVisibility(View.GONE); - } - - if ((pumpDescription.tempBasalStyle & PumpDescription.PERCENT) == PumpDescription.PERCENT) { - percentRadio.setChecked(true); - absoluteRadio.setChecked(false); - percentLayout.setVisibility(View.VISIBLE); - absoluteLayout.setVisibility(View.GONE); - } else if ((pumpDescription.tempBasalStyle & PumpDescription.ABSOLUTE) == PumpDescription.ABSOLUTE) { - percentRadio.setChecked(false); - absoluteRadio.setChecked(true); - percentLayout.setVisibility(View.GONE); - absoluteLayout.setVisibility(View.VISIBLE); - } - - view.findViewById(R.id.ok).setOnClickListener(this); - view.findViewById(R.id.cancel).setOnClickListener(this); - basalTypeRadioGroup.setOnCheckedChangeListener(this); - - setCancelable(true); - getDialog().setCanceledOnTouchOutside(false); - return view; - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.ok: - try { - int percent = 0; - Double absolute = 0d; - final boolean setAsPercent = percentRadio.isChecked(); - int durationInMinutes = SafeParse.stringToInt(duration.getText()); - - Profile profile = ProfileFunctions.getInstance().getProfile(); - if (profile == null) - return; - - String confirmMessage = MainApp.gs(R.string.setbasalquestion); - if (setAsPercent) { - int basalPercentInput = SafeParse.stringToInt(basalPercent.getText()); - percent = MainApp.getConstraintChecker().applyBasalPercentConstraints(new Constraint<>(basalPercentInput), profile).value(); - confirmMessage += "\n" + percent + "% "; - confirmMessage += "\n" + MainApp.gs(R.string.duration) + " " + durationInMinutes + "min ?"; - if (percent != basalPercentInput) - confirmMessage += "\n" + MainApp.gs(R.string.constraintapllied); - } else { - Double basalAbsoluteInput = SafeParse.stringToDouble(basalAbsolute.getText()); - absolute = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(basalAbsoluteInput), profile).value(); - confirmMessage += "\n" + absolute + " U/h "; - confirmMessage += "\n" + MainApp.gs(R.string.duration) + " " + durationInMinutes + "min ?"; - if (Math.abs(absolute - basalAbsoluteInput) > 0.01d) - confirmMessage += "\n" + MainApp.gs(R.string.constraintapllied); - } - - final int finalBasalPercent = percent; - final Double finalBasal = absolute; - final int finalDurationInMinutes = durationInMinutes; - - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(confirmMessage); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - Callback callback = 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.tempbasaldeliveryerror)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - } - }; - if (setAsPercent) { - ConfigBuilderPlugin.getPlugin().getCommandQueue().tempBasalPercent(finalBasalPercent, finalDurationInMinutes, true, profile, callback); - } else { - ConfigBuilderPlugin.getPlugin().getCommandQueue().tempBasalAbsolute(finalBasal, finalDurationInMinutes, true, profile, callback); - } - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - dismiss(); - - } catch (Exception e) { - log.error("Unhandled exception", e); - } - break; - case R.id.cancel: - dismiss(); - break; - } - } - - @Override - public void onCheckedChanged(RadioGroup group, int checkedId) { - switch (checkedId) { - case R.id.overview_newtempbasal_percent_radio: - percentLayout.setVisibility(View.VISIBLE); - absoluteLayout.setVisibility(View.GONE); - break; - case R.id.overview_newtempbasal_absolute_radio: - percentLayout.setVisibility(View.GONE); - absoluteLayout.setVisibility(View.VISIBLE); - break; - } - } -} 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 5266c0781c..1db87f6472 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 @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.general.automation import android.os.Bundle +import android.text.method.ScrollingMovementMethod import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -16,12 +17,12 @@ import info.nightscout.androidaps.plugins.general.automation.dragHelpers.SimpleI import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationDataChanged import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui import info.nightscout.androidaps.utils.FabricPrivacy +import info.nightscout.androidaps.utils.HtmlHelper import info.nightscout.androidaps.utils.plusAssign import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import kotlinx.android.synthetic.main.automation_fragment.* - class AutomationFragment : Fragment(), OnStartDragListener { private var disposable: CompositeDisposable = CompositeDisposable() @@ -40,6 +41,8 @@ class AutomationFragment : Fragment(), OnStartDragListener { automation_eventListView.layoutManager = LinearLayoutManager(context) automation_eventListView.adapter = eventListAdapter + automation_logView.setMovementMethod(ScrollingMovementMethod()) + automation_fabAddEvent.setOnClickListener { val dialog = EditEventDialog() val args = Bundle() @@ -88,8 +91,8 @@ class AutomationFragment : Fragment(), OnStartDragListener { eventListAdapter?.notifyDataSetChanged() val sb = StringBuilder() for (l in AutomationPlugin.executionLog.reversed()) - sb.append(l).append("\n") - automation_logView?.text = sb.toString() + sb.append(l).append("
") + automation_logView?.text = HtmlHelper.fromHtml(sb.toString()) } override fun onStartDrag(viewHolder: RecyclerView.ViewHolder) { 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 b559311f9d..0d220c2dd4 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 @@ -178,10 +178,10 @@ object AutomationPlugin : PluginBase(PluginDescription() val sb = StringBuilder() sb.append(DateUtil.timeString(DateUtil.now())) sb.append(" ") - sb.append(if (result.success) "☺" else "X") - sb.append(" ") + sb.append(if (result.success) "☺" else "▼") + sb.append(" ") sb.append(event.title) - sb.append(": ") + sb.append(": ") sb.append(action.shortDescription()) sb.append(": ") sb.append(result.comment) 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 8a4762bd08..63c923fdcf 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 @@ -21,17 +21,29 @@ import info.nightscout.androidaps.plugins.general.automation.elements.InputProfi import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; import info.nightscout.androidaps.queue.Callback; +import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.JsonHelper; public class ActionProfileSwitch extends Action { private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); - public InputProfileName inputProfileName = new InputProfileName(ProfileFunctions.getInstance().getProfileName()); + InputProfileName inputProfileName; String profileName = ""; public ActionProfileSwitch() { // Prevent action if active profile is already active // but we don't have a trigger IS_NOT_EQUAL // so check is in the doRun() + ProfileInterface profileInterface = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface(); + if (profileInterface != null) { + ProfileStore profileStore = profileInterface.getProfile(); + if (profileStore != null) { + String name = profileStore.getDefaultProfileName(); + if (name != null) { + profileName = name; + } + } + } + inputProfileName = new InputProfileName(profileName); } @Override @@ -50,23 +62,43 @@ public class ActionProfileSwitch extends Action { String activeProfileName = ProfileFunctions.getInstance().getProfileName(); //Check for uninitialized profileName - if ( profileName.equals("")){ profileName = activeProfileName; } - + if ( profileName.equals("")){ + log.error("Selected profile not initialized"); + if (callback != null) + callback.result(new PumpEnactResult().success(false).comment(R.string.error_field_must_not_be_empty)).run(); + return; + } + if ( ProfileFunctions.getInstance().getProfile() == null){ + log.error("ProfileFunctions not initialized"); + if (callback != null) + callback.result(new PumpEnactResult().success(false).comment(R.string.noprofile)).run(); + return; + } if (profileName.equals(activeProfileName)) { - // Profile is already switched + if (L.isEnabled(L.AUTOMATION)) + log.debug("Profile is already switched"); + if (callback != null) + callback.result(new PumpEnactResult().success(true).comment(R.string.alreadyset)).run(); return; } ProfileInterface activeProfile = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface(); - if (activeProfile == null) return; + if (activeProfile == null) { + log.error("ProfileInterface not initialized"); + if (callback != null) + callback.result(new PumpEnactResult().success(false).comment(R.string.noprofile)).run(); + return; + } ProfileStore profileStore = activeProfile.getProfile(); if (profileStore == null) return; if(profileStore.getSpecificProfile(profileName) == null) { if (L.isEnabled(L.AUTOMATION)) log.error("Selected profile does not exist! - "+ profileName); + if (callback != null) + callback.result(new PumpEnactResult().success(false).comment(R.string.notexists)).run(); return; } - ProfileFunctions.doProfileSwitch(profileStore, profileName, 0, 100, 0); + ProfileFunctions.doProfileSwitch(profileStore, profileName, 0, 100, 0, DateUtil.now()); if (callback != null) callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run(); } 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 b0ef645069..1d7ab8db48 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 @@ -75,7 +75,7 @@ public class ActionStartTempTarget extends Action { int unitResId = value.getUnits().equals(Constants.MGDL) ? R.string.mgdl : R.string.mmol; new LayoutBuilder() - .add(new LabelWithElement(MainApp.gs(R.string.careportal_temporarytarget) + " [" + MainApp.gs(unitResId) + "]", "", value)) + .add(new LabelWithElement(MainApp.gs(R.string.careportal_temporarytarget) + "\n[" + MainApp.gs(unitResId) + "]", "", value)) .add(new LabelWithElement(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "", duration)) .build(root); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.kt index 8c9efedd20..d3a8eb20c0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.kt @@ -32,7 +32,7 @@ class ActionListAdapter(private val fragmentManager: FragmentManager, private va class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) { - fun bind(action: Action, fragmentManager: FragmentManager, recyclerView: RecyclerView.Adapter, position : Int, actionList: MutableList) { + fun bind(action: Action, fragmentManager: FragmentManager, recyclerView: RecyclerView.Adapter, position: Int, actionList: MutableList) { view.findViewById(R.id.automation_layoutText).setOnClickListener { if (action.hasDialog()) { val args = Bundle() @@ -48,6 +48,7 @@ class ActionListAdapter(private val fragmentManager: FragmentManager, private va recyclerView.notifyDataSetChanged() RxBus.send(EventAutomationUpdateGui()) } + if (action.icon().isPresent) view.findViewById(R.id.automation_action_image).setImageResource(action.icon().get()) view.findViewById(R.id.automation_viewActionTitle).text = action.shortDescription() } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.kt index 2096d5a541..c6097a1e88 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.kt @@ -5,19 +5,18 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.RadioButton -import androidx.fragment.app.DialogFragment import info.nightscout.androidaps.R +import info.nightscout.androidaps.dialogs.DialogFragmentWithDate import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin import info.nightscout.androidaps.plugins.general.automation.actions.Action import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationAddAction import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui import kotlinx.android.synthetic.main.automation_dialog_choose_action.* -import kotlinx.android.synthetic.main.okcancel.* -class ChooseActionDialog : DialogFragment() { +class ChooseActionDialog : DialogFragmentWithDate() { - var checkedIndex = -1 + private var checkedIndex = -1 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { @@ -26,7 +25,7 @@ class ChooseActionDialog : DialogFragment() { checkedIndex = bundle.getInt("checkedIndex") } - dialog?.setCanceledOnTouchOutside(false) + onCreateViewGeneral() return inflater.inflate(R.layout.automation_dialog_choose_action, container, false) } @@ -42,27 +41,19 @@ class ChooseActionDialog : DialogFragment() { if (checkedIndex != -1) (automation_radioGroup.getChildAt(checkedIndex) as RadioButton).isChecked = true + } - // OK button - ok.setOnClickListener { - dismiss() - instantiateAction()?.let { - RxBus.send(EventAutomationAddAction(it)) - RxBus.send(EventAutomationUpdateGui()) - } + override fun submit(): Boolean { + instantiateAction()?.let { + RxBus.send(EventAutomationAddAction(it)) + RxBus.send(EventAutomationUpdateGui()) } - - // Cancel button - cancel.setOnClickListener { dismiss() } + return true } - override fun onStart() { - super.onStart() - dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) - } - - override fun onSaveInstanceState(bundle: Bundle) { - bundle.putInt("checkedIndex", determineCheckedIndex()) + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putInt("checkedIndex", determineCheckedIndex()) } private fun instantiateAction(): Action? { @@ -86,5 +77,4 @@ class ChooseActionDialog : DialogFragment() { } return -1 } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.kt index b2317fc6a2..eef3a7057e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.kt @@ -5,17 +5,15 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.RadioButton -import androidx.fragment.app.DialogFragment import info.nightscout.androidaps.R +import info.nightscout.androidaps.dialogs.DialogFragmentWithDate import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger import kotlinx.android.synthetic.main.automation_dialog_choose_trigger.* -import kotlinx.android.synthetic.main.okcancel.* -class ChooseTriggerDialog : DialogFragment() { +class ChooseTriggerDialog : DialogFragmentWithDate() { private var checkedIndex = -1 - private var clickListener: OnClickListener? = null interface OnClickListener { @@ -29,7 +27,7 @@ class ChooseTriggerDialog : DialogFragment() { checkedIndex = bundle.getInt("checkedIndex") } - dialog?.setCanceledOnTouchOutside(false) + onCreateViewGeneral() return inflater.inflate(R.layout.automation_dialog_choose_trigger, container, false) } @@ -45,30 +43,22 @@ class ChooseTriggerDialog : DialogFragment() { if (checkedIndex != -1) (automation_chooseTriggerRadioGroup.getChildAt(checkedIndex) as RadioButton).isChecked = true - - // OK button - ok.setOnClickListener { - dismiss() - instantiateTrigger()?.let { - clickListener?.onClick(it) - } - } - - // Cancel button - cancel.setOnClickListener { dismiss() } } - override fun onStart() { - super.onStart() - dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + override fun submit(): Boolean { + instantiateTrigger()?.let { + clickListener?.onClick(it) + } + return true } fun setOnClickListener(clickListener: OnClickListener) { this.clickListener = clickListener } - override fun onSaveInstanceState(bundle: Bundle) { - bundle.putInt("checkedIndex", determineCheckedIndex()) + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putInt("checkedIndex", determineCheckedIndex()) } private fun instantiateTrigger(): Trigger? { @@ -92,5 +82,4 @@ class ChooseTriggerDialog : DialogFragment() { } return -1 } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt index 2c49f81bba..e738b7f94c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt @@ -4,16 +4,15 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.fragment.app.DialogFragment import info.nightscout.androidaps.R +import info.nightscout.androidaps.dialogs.DialogFragmentWithDate import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.automation.actions.Action import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateAction import kotlinx.android.synthetic.main.automation_dialog_action.* -import kotlinx.android.synthetic.main.okcancel.* import org.json.JSONObject -class EditActionDialog : DialogFragment() { +class EditActionDialog : DialogFragmentWithDate() { private var action: Action? = null private var actionPosition: Int = -1 @@ -24,8 +23,7 @@ class EditActionDialog : DialogFragment() { actionPosition = bundle.getInt("actionPosition", -1) bundle.getString("action")?.let { action = Action.instantiate(JSONObject(it)) } } - - dialog?.setCanceledOnTouchOutside(false) + onCreateViewGeneral() return inflater.inflate(R.layout.automation_dialog_action, container, false) } @@ -37,22 +35,13 @@ class EditActionDialog : DialogFragment() { automation_editActionLayout.removeAllViews() it.generateDialog(automation_editActionLayout) } - - // OK button - ok.setOnClickListener { - dismiss() - action?.let { - RxBus.send(EventAutomationUpdateAction(it, actionPosition)) - } - } - - // Cancel button - cancel.setOnClickListener { dismiss() } } - override fun onStart() { - super.onStart() - dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + override fun submit(): Boolean { + action?.let { + RxBus.send(EventAutomationUpdateAction(it, actionPosition)) + } + return true } override fun onSaveInstanceState(bundle: Bundle) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt index 85aab0f538..0bcb183e11 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt @@ -4,9 +4,9 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.fragment.app.DialogFragment import androidx.recyclerview.widget.LinearLayoutManager import info.nightscout.androidaps.R +import info.nightscout.androidaps.dialogs.DialogFragmentWithDate import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.automation.AutomationEvent import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin @@ -17,9 +17,8 @@ import info.nightscout.androidaps.utils.ToastUtils import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import kotlinx.android.synthetic.main.automation_dialog_event.* -import kotlinx.android.synthetic.main.okcancel.* -class EditEventDialog : DialogFragment() { +class EditEventDialog : DialogFragmentWithDate() { private var actionListAdapter: ActionListAdapter? = null private var event: AutomationEvent = AutomationEvent() @@ -35,7 +34,7 @@ class EditEventDialog : DialogFragment() { bundle.getString("event")?.let { event = AutomationEvent().fromJSON(it) } } - dialog?.setCanceledOnTouchOutside(false) + onCreateViewGeneral() return inflater.inflate(R.layout.automation_dialog_event, container, false) } @@ -60,39 +59,6 @@ class EditEventDialog : DialogFragment() { automation_addAction.setOnClickListener { fragmentManager?.let { ChooseActionDialog().show(it, "ChooseActionDialog") } } - // OK button - ok.setOnClickListener { - // check for title - val title = automation_inputEventTitle.text.toString() - if (title.isEmpty()) { - ToastUtils.showToastInUiThread(context, R.string.automation_missing_task_name) - return@setOnClickListener - } - event.title = title - // check for at least one trigger - val con = event.trigger as TriggerConnector - if (con.size() == 0) { - ToastUtils.showToastInUiThread(context, R.string.automation_missing_trigger) - return@setOnClickListener - } - // check for at least one action - if (event.actions.isEmpty()) { - ToastUtils.showToastInUiThread(context, R.string.automation_missing_action) - return@setOnClickListener - } - // store - if (position == -1) - AutomationPlugin.automationEvents.add(event) - else - AutomationPlugin.automationEvents[position] = event - - dismiss() - RxBus.send(EventAutomationDataChanged()) - } - - // Cancel button - cancel.setOnClickListener { dismiss() } - showPreconditions() disposable.add(RxBus @@ -137,9 +103,33 @@ class EditEventDialog : DialogFragment() { ) } - override fun onStart() { - super.onStart() - dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + override fun submit() : Boolean{ + // check for title + val title = automation_inputEventTitle.text.toString() + if (title.isEmpty()) { + ToastUtils.showToastInUiThread(context, R.string.automation_missing_task_name) + return false + } + event.title = title + // check for at least one trigger + val con = event.trigger as TriggerConnector + if (con.size() == 0) { + ToastUtils.showToastInUiThread(context, R.string.automation_missing_trigger) + return false + } + // check for at least one action + if (event.actions.isEmpty()) { + ToastUtils.showToastInUiThread(context, R.string.automation_missing_action) + return false + } + // store + if (position == -1) + AutomationPlugin.automationEvents.add(event) + else + AutomationPlugin.automationEvents[position] = event + + RxBus.send(EventAutomationDataChanged()) + return true } override fun onDestroyView() { @@ -147,10 +137,10 @@ class EditEventDialog : DialogFragment() { disposable.clear() } - override fun onSaveInstanceState(bundle: Bundle) { - super.onSaveInstanceState(bundle) - bundle.putString("event", event.toJSON()) - bundle.putInt("position", position) + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putString("event", event.toJSON()) + savedInstanceState.putInt("position", position) } private fun showPreconditions() { @@ -164,5 +154,4 @@ class EditEventDialog : DialogFragment() { automation_forcedTriggerDescriptionLabel.visibility = View.GONE } } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditTriggerDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditTriggerDialog.kt index eda9c6177f..b30a724ec5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditTriggerDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditTriggerDialog.kt @@ -4,15 +4,14 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.fragment.app.DialogFragment import info.nightscout.androidaps.R +import info.nightscout.androidaps.dialogs.DialogFragmentWithDate import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateTrigger import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger import kotlinx.android.synthetic.main.automation_dialog_edit_trigger.* -import kotlinx.android.synthetic.main.okcancel.* -class EditTriggerDialog : DialogFragment() { +class EditTriggerDialog : DialogFragmentWithDate() { private var trigger: Trigger? = null @@ -23,7 +22,7 @@ class EditTriggerDialog : DialogFragment() { bundle.getString("trigger")?.let { trigger = Trigger.instantiate(it) } } - dialog?.setCanceledOnTouchOutside(false) + onCreateViewGeneral() return inflater.inflate(R.layout.automation_dialog_edit_trigger, container, false) } @@ -32,24 +31,15 @@ class EditTriggerDialog : DialogFragment() { // display root trigger trigger?.generateDialog(automation_layoutTrigger, fragmentManager) - - // OK button - ok.setOnClickListener { - dismiss() - trigger?.let { trigger -> RxBus.send(EventAutomationUpdateTrigger(trigger)) } - } - - // Cancel button - cancel.setOnClickListener { dismiss() } } - override fun onStart() { - super.onStart() - dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + override fun submit():Boolean { + trigger?.let { trigger -> RxBus.send(EventAutomationUpdateTrigger(trigger)) } + return true } - override fun onSaveInstanceState(bundle: Bundle) { - super.onSaveInstanceState(bundle) - trigger?.let { bundle.putString("trigger", it.toJSON()) } + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + trigger?.let { savedInstanceState.putString("trigger", it.toJSON()) } } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/TriggerListAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/TriggerListAdapter.java index 98d225d84e..3daa0fbd17 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/TriggerListAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/TriggerListAdapter.java @@ -75,7 +75,7 @@ public class TriggerListAdapter { private Spinner createSpinner() { Spinner spinner = new Spinner(mContext); - ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, TriggerConnector.Type.labels()); + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(mContext, R.layout.spinner_centered, TriggerConnector.Type.labels()); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerArrayAdapter); return spinner; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/Comparator.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/Comparator.java index 334adfcc01..c0e23d979c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/Comparator.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/Comparator.java @@ -87,7 +87,7 @@ public class Comparator extends Element { @Override public void addToLayout(LinearLayout root) { Spinner spinner = new Spinner(root.getContext()); - ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(root.getContext(), android.R.layout.simple_spinner_item, Compare.labels()); + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(root.getContext(), R.layout.spinner_centered, Compare.labels()); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerArrayAdapter); LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams( diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/ComparatorExists.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/ComparatorExists.java index 2bde0cb842..4bc0082afa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/ComparatorExists.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/ComparatorExists.java @@ -54,7 +54,7 @@ public class ComparatorExists extends Element { @Override public void addToLayout(LinearLayout root) { Spinner spinner = new Spinner(root.getContext()); - ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(root.getContext(), android.R.layout.simple_spinner_item, Compare.labels()); + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(root.getContext(), R.layout.spinner_centered, Compare.labels()); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerArrayAdapter); LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams( diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java index ad0e84bec4..4dc9358f6c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputDelta.java @@ -87,7 +87,7 @@ public class InputDelta extends Element { @Override public void addToLayout(LinearLayout root) { Spinner spinner = new Spinner(root.getContext()); - ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(root.getContext(), android.R.layout.simple_spinner_item, DeltaType.labels()); + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(root.getContext(), R.layout.spinner_centered, DeltaType.labels()); spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(spinnerArrayAdapter); LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams( 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 f45efdf4a4..a5fc6ba092 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 @@ -93,7 +93,7 @@ public class TriggerTempTarget extends Trigger { @Override public int friendlyName() { - return R.string.temptarget; + return R.string.careportal_temporarytarget; } @Override @@ -124,7 +124,7 @@ public class TriggerTempTarget extends Trigger { @Override public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { new LayoutBuilder() - .add(new StaticLabel(R.string.temptarget)) + .add(new StaticLabel(R.string.careportal_temporarytarget)) .add(comparator) .build(root); } 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 42ae7fd6fc..5dd9292343 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 @@ -63,7 +63,6 @@ public class CareportalFragment extends Fragment implements View.OnClickListener public static final OptionsToShow TEMPBASALSTART = new OptionsToShow(R.id.careportal_tempbasalstart, R.string.careportal_tempbasalstart).date().bg().duration().percent().absolute(); public static final OptionsToShow TEMPBASALEND = new OptionsToShow(R.id.careportal_tempbasalend, R.string.careportal_tempbasalend).date().bg(); public static final OptionsToShow PROFILESWITCH = new OptionsToShow(R.id.careportal_profileswitch, R.string.careportal_profileswitch).date().duration().profile(); - public static final OptionsToShow PROFILESWITCHDIRECT = new OptionsToShow(R.id.careportal_profileswitch, R.string.careportal_profileswitch).duration().profile(); public static final OptionsToShow OPENAPSOFFLINE = new OptionsToShow(R.id.careportal_openapsoffline, R.string.careportal_openapsoffline).date().duration(); public static final OptionsToShow TEMPTARGET = new OptionsToShow(R.id.careportal_temporarytarget, R.string.careportal_temporarytarget).date().duration().tempTarget(); @@ -180,7 +179,6 @@ public class CareportalFragment extends Fragment implements View.OnClickListener newDialog.setOptions(NOTE, R.string.careportal_note); break; case R.id.careportal_profileswitch: - PROFILESWITCH.executeProfileSwitch = false; newDialog.setOptions(PROFILESWITCH, R.string.careportal_profileswitch); break; case R.id.careportal_pumpsitechange: @@ -202,7 +200,6 @@ public class CareportalFragment extends Fragment implements View.OnClickListener newDialog.setOptions(OPENAPSOFFLINE, R.string.careportal_openapsoffline); break; case R.id.careportal_temporarytarget: - TEMPTARGET.executeTempTarget = false; newDialog.setOptions(TEMPTARGET, R.string.careportal_temporarytarget); break; default: @@ -222,7 +219,7 @@ public class CareportalFragment extends Fragment implements View.OnClickListener activity.runOnUiThread( () -> { CareportalEvent careportalEvent; - NSSettingsStatus nsSettings = new NSSettingsStatus().getInstance(); + NSSettingsStatus nsSettings = NSSettingsStatus.getInstance(); double iageUrgent = nsSettings.getExtendedWarnValue("iage", "urgent", 96); double iageWarn = nsSettings.getExtendedWarnValue("iage", "warn", 72); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalPlugin.java index fa8f87ef46..fc28d23188 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalPlugin.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.careportal; +import info.nightscout.androidaps.Config; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; @@ -22,10 +23,14 @@ public class CareportalPlugin extends PluginBase { .fragmentClass(CareportalFragment.class.getName()) .pluginName(R.string.careportal) .shortName(R.string.careportal_shortname) - .visibleByDefault(true) - .enableByDefault(true) + .visibleByDefault(Config.NSCLIENT) + .enableByDefault(Config.NSCLIENT) .description(R.string.description_careportal) ); } + @Override + public boolean specialEnableCondition() { + return Config.NSCLIENT; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java index 64b7b05987..38ad5bb879 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/Dialogs/NewNSTreatmentDialog.java @@ -1,7 +1,6 @@ package info.nightscout.androidaps.plugins.general.careportal.Dialogs; -import android.content.Context; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -9,6 +8,8 @@ import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; @@ -18,7 +19,6 @@ import android.widget.RadioButton; import android.widget.Spinner; import android.widget.TextView; -import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatDialogFragment; import androidx.fragment.app.DialogFragment; @@ -45,8 +45,6 @@ import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.ProfileSwitch; -import info.nightscout.androidaps.db.Source; -import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow; @@ -58,6 +56,7 @@ import info.nightscout.androidaps.utils.DefaultValueHelper; import info.nightscout.androidaps.utils.HardLimits; import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.NumberPicker; +import info.nightscout.androidaps.utils.OKDialog; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SafeParse; import info.nightscout.androidaps.utils.Translator; @@ -68,7 +67,7 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie private static OptionsToShow options; private static String event; - Profile profile; + private Profile profile; public ProfileStore profileStore; TextView eventTypeText; @@ -105,9 +104,10 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie private static Integer seconds = null; - public void setOptions(OptionsToShow options, int event) { + public NewNSTreatmentDialog setOptions(OptionsToShow options, int event) { this.options = options; this.event = MainApp.gs(event); + return this; } public NewNSTreatmentDialog() { @@ -122,7 +122,10 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (options == null) return null; - getDialog().setTitle(MainApp.gs(options.eventName)); + getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); + getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + setCancelable(true); + getDialog().setCanceledOnTouchOutside(false); setStyle(DialogFragment.STYLE_NORMAL, getTheme()); View view = inflater.inflate(R.layout.careportal_newnstreatment_dialog, container, false); @@ -200,16 +203,15 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie boolean erase = false; String units = ProfileFunctions.getSystemUnits(); - DefaultValueHelper helper = new DefaultValueHelper(); if (MainApp.gs(R.string.eatingsoon).equals(reasonList.get(position))) { - defaultDuration = helper.determineEatingSoonTTDuration(); - defaultTarget = helper.determineEatingSoonTT(); + defaultDuration = DefaultValueHelper.determineEatingSoonTTDuration(); + defaultTarget = DefaultValueHelper.determineEatingSoonTT(); } else if (MainApp.gs(R.string.activity).equals(reasonList.get(position))) { - defaultDuration = helper.determineActivityTTDuration(); - defaultTarget = helper.determineActivityTT(); + defaultDuration = DefaultValueHelper.determineActivityTTDuration(); + defaultTarget = DefaultValueHelper.determineActivityTT(); } else if (MainApp.gs(R.string.hypo).equals(reasonList.get(position))) { - defaultDuration = helper.determineHypoTTDuration(); - defaultTarget = helper.determineHypoTT(); + defaultDuration = DefaultValueHelper.determineHypoTTDuration(); + defaultTarget = DefaultValueHelper.determineHypoTT(); } else if (editDuration.getValue() != 0) { defaultDuration = editDuration.getValue(); } else { @@ -394,6 +396,13 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie return view; } + @Override + public void onResume() { + super.onResume(); + getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + + } + @Override public void onClick(View view) { Calendar calendar = Calendar.getInstance(); @@ -585,12 +594,12 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie String buildConfirmText(JSONObject data) { String ret = ""; - if (data.has("eventType")) { - ret += MainApp.gs(R.string.careportal_newnstreatment_eventtype); - ret += ": "; - ret += Translator.translate(JsonHelper.safeGetString(data, "eventType", "")); - ret += "\n"; - } +// if (data.has("eventType")) { +// ret += MainApp.gs(R.string.careportal_newnstreatment_eventtype); +// ret += ": "; +// ret += Translator.translate(JsonHelper.safeGetString(data, "eventType", "")); +// ret += "\n"; +// } if (data.has("glucose")) { ret += MainApp.gs(R.string.treatments_wizard_bg_label); ret += ": "; @@ -672,7 +681,7 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie ret += "\n"; } if (data.has("created_at")) { - ret += MainApp.gs(R.string.careportal_newnstreatment_eventtime_label); + ret += MainApp.gs(R.string.event_time_label); ret += ": "; ret += eventTime.toLocaleString(); ret += "\n"; @@ -687,61 +696,25 @@ public class NewNSTreatmentDialog extends AppCompatDialogFragment implements Vie return ret; } - void confirmNSTreatmentCreation() { - Context context = getContext(); - if (context != null) { - final JSONObject data = gatherData(); - final String confirmText = buildConfirmText(data); - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(confirmText); - builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> createNSTreatment(data)); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - } + private void confirmNSTreatmentCreation() { + final JSONObject data = gatherData(); + OKDialog.showConfirmation(getContext(), Translator.translate(JsonHelper.safeGetString(data, "eventType", MainApp.gs(R.string.overview_treatment_label))), buildConfirmText(data), () -> createNSTreatment(data)); } void createNSTreatment(JSONObject data) { - if (options.executeProfileSwitch) { - if (data.has("profile")) { - ProfileFunctions.doProfileSwitch(profileStore, JsonHelper.safeGetString(data, "profile"), JsonHelper.safeGetInt(data, "duration"), JsonHelper.safeGetInt(data, "percentage"), JsonHelper.safeGetInt(data, "timeshift")); - } - } else if (options.executeTempTarget) { - final int duration = JsonHelper.safeGetInt(data, "duration"); - final double targetBottom = JsonHelper.safeGetDouble(data, "targetBottom"); - final double targetTop = JsonHelper.safeGetDouble(data, "targetTop"); - final String reason = JsonHelper.safeGetString(data, "reason", ""); - if ((targetBottom != 0d && targetTop != 0d) || duration == 0) { - TempTarget tempTarget = new TempTarget() - .date(eventTime.getTime()) - .duration(duration) - .reason(reason) - .source(Source.USER); - if (tempTarget.durationInMinutes != 0) { - tempTarget.low(Profile.toMgdl(targetBottom, ProfileFunctions.getSystemUnits())) - .high(Profile.toMgdl(targetTop, ProfileFunctions.getSystemUnits())); - } else { - tempTarget.low(0).high(0); - } - TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); - } - if (duration == 10) - SP.putBoolean(R.string.key_objectiveusetemptarget, true); + if (JsonHelper.safeGetString(data, "eventType", "").equals(CareportalEvent.PROFILESWITCH)) { + ProfileSwitch profileSwitch = ProfileFunctions.prepareProfileSwitch( + profileStore, + JsonHelper.safeGetString(data, "profile"), + JsonHelper.safeGetInt(data, "duration"), + JsonHelper.safeGetInt(data, "percentage"), + JsonHelper.safeGetInt(data, "timeshift"), + eventTime.getTime() + ); + NSUpload.uploadProfileSwitch(profileSwitch); } else { - if (JsonHelper.safeGetString(data, "eventType").equals(CareportalEvent.PROFILESWITCH)) { - ProfileSwitch profileSwitch = ProfileFunctions.prepareProfileSwitch( - profileStore, - JsonHelper.safeGetString(data, "profile"), - JsonHelper.safeGetInt(data, "duration"), - JsonHelper.safeGetInt(data, "percentage"), - JsonHelper.safeGetInt(data, "timeshift"), - eventTime.getTime() - ); - NSUpload.uploadProfileSwitch(profileSwitch); - } else { - NSUpload.uploadCareportalEntryToNS(data); - } + NSUpload.uploadCareportalEntryToNS(data); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/OptionsToShow.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/OptionsToShow.java index 08f65a967f..e1effcd561 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/OptionsToShow.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/OptionsToShow.java @@ -19,10 +19,6 @@ public class OptionsToShow { public boolean split; public boolean tempTarget; - // perform direct actions - public boolean executeProfileSwitch = false; - public boolean executeTempTarget = false; - public OptionsToShow(int eventType, int eventName) { this.eventType = eventType; this.eventName = eventName; 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 997c6b0ba9..074310a8dd 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,6 +1,5 @@ package info.nightscout.androidaps.plugins.general.food; -import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; import android.text.Editable; @@ -14,14 +13,11 @@ import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; -import androidx.appcompat.app.AlertDialog; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -33,6 +29,7 @@ import info.nightscout.androidaps.events.EventFoodDatabaseChanged; 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.OKDialog; import info.nightscout.androidaps.utils.SpinnerHelper; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; @@ -42,43 +39,36 @@ import io.reactivex.disposables.CompositeDisposable; */ public class FoodFragment extends Fragment { - private static Logger log = LoggerFactory.getLogger(FoodFragment.class); private CompositeDisposable disposable = new CompositeDisposable(); - EditText filter; - ImageView clearFilter; - SpinnerHelper category; - SpinnerHelper subcategory; - RecyclerView recyclerView; + private EditText filter; + private SpinnerHelper category; + private SpinnerHelper subcategory; + private RecyclerView recyclerView; - List unfiltered; - List filtered; - ArrayList categories; - ArrayList subcategories; + private List unfiltered; + private List filtered; - final String EMPTY = MainApp.gs(R.string.none); + private final String EMPTY = MainApp.gs(R.string.none); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 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); + filter = view.findViewById(R.id.food_filter); + ImageView clearFilter = 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 = 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(v -> { + filter.setText(""); + category.setSelection(0); + subcategory.setSelection(0); + filterData(); }); category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @@ -149,11 +139,11 @@ public class FoodFragment extends Fragment { disposable.clear(); } - void loadData() { + private void loadData() { unfiltered = FoodPlugin.getPlugin().getService().getFoodData(); } - void fillCategories() { + private void fillCategories() { Set catSet = new HashSet<>(); for (Food f : unfiltered) { @@ -162,7 +152,7 @@ public class FoodFragment extends Fragment { } // make it unique - categories = new ArrayList<>(catSet); + ArrayList categories = new ArrayList<>(catSet); categories.add(0, MainApp.gs(R.string.none)); ArrayAdapter adapterCategories = new ArrayAdapter<>(getContext(), @@ -170,7 +160,7 @@ public class FoodFragment extends Fragment { category.setAdapter(adapterCategories); } - void fillSubcategories() { + private void fillSubcategories() { String categoryFilter = category.getSelectedItem().toString(); Set subCatSet = new HashSet<>(); @@ -184,7 +174,7 @@ public class FoodFragment extends Fragment { } // make it unique - subcategories = new ArrayList<>(subCatSet); + ArrayList subcategories = new ArrayList<>(subCatSet); subcategories.add(0, MainApp.gs(R.string.none)); ArrayAdapter adapterSubcategories = new ArrayAdapter<>(getContext(), @@ -192,7 +182,7 @@ public class FoodFragment extends Fragment { subcategory.setAdapter(adapterSubcategories); } - void filterData() { + private void filterData() { String textFilter = filter.getText().toString(); String categoryFilter = category.getSelectedItem().toString(); String subcategoryFilter = subcategory.getSelectedItem().toString(); @@ -227,6 +217,7 @@ public class FoodFragment extends Fragment { this.foodList = foodList; } + @NonNull @Override public FoodsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.food_item, viewGroup, false); @@ -257,7 +248,7 @@ public class FoodFragment extends Fragment { return foodList.size(); } - class FoodsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + class FoodsViewHolder extends RecyclerView.ViewHolder { TextView name; TextView portion; TextView carbs; @@ -269,43 +260,26 @@ public class FoodFragment extends Fragment { FoodsViewHolder(View itemView) { super(itemView); - name = (TextView) itemView.findViewById(R.id.food_name); - portion = (TextView) itemView.findViewById(R.id.food_portion); - carbs = (TextView) itemView.findViewById(R.id.food_carbs); - fat = (TextView) itemView.findViewById(R.id.food_fat); - protein = (TextView) itemView.findViewById(R.id.food_protein); - energy = (TextView) itemView.findViewById(R.id.food_energy); - ns = (TextView) itemView.findViewById(R.id.ns_sign); - remove = (TextView) itemView.findViewById(R.id.food_remove); - remove.setOnClickListener(this); + name = itemView.findViewById(R.id.food_name); + portion = itemView.findViewById(R.id.food_portion); + carbs = itemView.findViewById(R.id.food_carbs); + fat = itemView.findViewById(R.id.food_fat); + protein = itemView.findViewById(R.id.food_protein); + energy = itemView.findViewById(R.id.food_energy); + ns = itemView.findViewById(R.id.ns_sign); + remove = itemView.findViewById(R.id.food_remove); + remove.setOnClickListener(v -> { + final Food food = (Food) v.getTag(); + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.confirmation), MainApp.gs(R.string.removerecord) + "\n" + food.name, (dialog, id) -> { + final String _id = food._id; + if (_id != null && !_id.equals("")) { + NSUpload.removeFoodFromNS(_id); + } + FoodPlugin.getPlugin().getService().delete(food); + }, null); + }); remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); } - - @Override - public void onClick(View v) { - final Food food = (Food) v.getTag(); - switch (v.getId()) { - - case R.id.food_remove: - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.removerecord) + "\n" + food.name); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - final String _id = food._id; - if (_id != null && !_id.equals("")) { - NSUpload.removeFoodFromNS(_id); - } - FoodPlugin.getPlugin().getService().delete(food); - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - - } - } } } - } 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 ae9867b0a2..faabbd8192 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 @@ -2,15 +2,14 @@ package info.nightscout.androidaps.plugins.general.maintenance; import android.Manifest; import android.app.Activity; -import android.app.AlertDialog; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.Environment; import android.preference.PreferenceManager; -import androidx.core.app.ActivityCompat; -import androidx.fragment.app.Fragment; + import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,7 +38,7 @@ import info.nightscout.androidaps.utils.ToastUtils; public class ImportExportPrefs { private static Logger log = LoggerFactory.getLogger(L.CORE); - static File path = new File(Environment.getExternalStorageDirectory().toString()); + private static File path = new File(Environment.getExternalStorageDirectory().toString()); static public final File file = new File(path, MainApp.gs(R.string.app_name) + "Preferences"); private static final int REQUEST_EXTERNAL_STORAGE = 1; @@ -48,21 +47,7 @@ public class ImportExportPrefs { Manifest.permission.WRITE_EXTERNAL_STORAGE }; - public static void verifyStoragePermissions(Activity activity) { - // Check if we have write permission - int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); - - if (permission != PackageManager.PERMISSION_GRANTED) { - // We don't have permission so prompt the user - ActivityCompat.requestPermissions( - activity, - PERMISSIONS_STORAGE, - REQUEST_EXTERNAL_STORAGE - ); - } - } - - public static void verifyStoragePermissions(Fragment fragment) { + static void verifyStoragePermissions(Fragment fragment) { int permission = ContextCompat.checkSelfPermission(fragment.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE); @@ -73,85 +58,73 @@ public class ImportExportPrefs { } - public static void exportSharedPreferences(final Fragment f) { + static void exportSharedPreferences(final Fragment f) { exportSharedPreferences(f.getContext()); } - public static void exportSharedPreferences(final Context c) { - - new AlertDialog.Builder(c) - .setMessage(MainApp.gs(R.string.export_to) + " " + file + " ?") - .setPositiveButton(android.R.string.yes, (dialog, which) -> { - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); - try { - FileWriter fw = new FileWriter(file); - PrintWriter pw = new PrintWriter(fw); - Map prefsMap = prefs.getAll(); - for (Map.Entry entry : prefsMap.entrySet()) { - pw.println(entry.getKey() + "::" + entry.getValue().toString()); - } - pw.close(); - fw.close(); - ToastUtils.showToastInUiThread(c, MainApp.gs(R.string.exported)); - } catch (FileNotFoundException e) { - ToastUtils.showToastInUiThread(c, MainApp.gs(R.string.filenotfound) + " " + file); - log.error("Unhandled exception", e); - } catch (IOException e) { - log.error("Unhandled exception", e); - } - }) - .setNegativeButton(android.R.string.cancel, null) - .show(); + private static void exportSharedPreferences(final Context context) { + OKDialog.showConfirmation(context, MainApp.gs(R.string.maintenance), MainApp.gs(R.string.export_to) + " " + file + " ?", () -> { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + try { + FileWriter fw = new FileWriter(file); + PrintWriter pw = new PrintWriter(fw); + Map prefsMap = prefs.getAll(); + for (Map.Entry entry : prefsMap.entrySet()) { + pw.println(entry.getKey() + "::" + entry.getValue().toString()); + } + pw.close(); + fw.close(); + ToastUtils.showToastInUiThread(context, MainApp.gs(R.string.exported)); + } catch (FileNotFoundException e) { + ToastUtils.showToastInUiThread(context, MainApp.gs(R.string.filenotfound) + " " + file); + log.error("Unhandled exception", e); + } catch (IOException e) { + log.error("Unhandled exception", e); + } + }); } - public static void importSharedPreferences(final Fragment fragment) { + static void importSharedPreferences(final Fragment fragment) { importSharedPreferences(fragment.getContext()); } public static void importSharedPreferences(final Context context) { - new AlertDialog.Builder(context) - .setMessage(MainApp.gs(R.string.import_from) + " " + file + " ?") - .setPositiveButton(android.R.string.yes, (dialog, which) -> { + OKDialog.showConfirmation(context, MainApp.gs(R.string.maintenance), MainApp.gs(R.string.import_from) + " " + file + " ?", () -> { + String line; + String[] lineParts; + try { + SP.clear(); - String line; - String[] lineParts; - try { - SP.clear(); - - BufferedReader reader = new BufferedReader(new FileReader(file)); - while ((line = reader.readLine()) != null) { - lineParts = line.split("::"); - if (lineParts.length == 2) { - if (lineParts[1].equals("true") || lineParts[1].equals("false")) { - SP.putBoolean(lineParts[0], Boolean.parseBoolean(lineParts[1])); - } else { - SP.putString(lineParts[0], lineParts[1]); - } - } + BufferedReader reader = new BufferedReader(new FileReader(file)); + while ((line = reader.readLine()) != null) { + lineParts = line.split("::"); + if (lineParts.length == 2) { + if (lineParts[1].equals("true") || lineParts[1].equals("false")) { + SP.putBoolean(lineParts[0], Boolean.parseBoolean(lineParts[1])); + } else { + SP.putString(lineParts[0], lineParts[1]); } - reader.close(); - SP.putBoolean(R.string.key_setupwizard_processed, true); - OKDialog.show(context, MainApp.gs(R.string.setting_imported), MainApp.gs(R.string.restartingapp), () -> { - log.debug("Exiting"); - MainApp.instance().stopKeepAliveService(); - RxBus.INSTANCE.send(new EventAppExit()); - MainApp.closeDbHelper(); - if (context instanceof Activity) { - ((Activity)context).finish(); - } - System.runFinalization(); - System.exit(0); - }); - } catch (FileNotFoundException e) { - ToastUtils.showToastInUiThread(context, MainApp.gs(R.string.filenotfound) + " " + file); - log.error("Unhandled exception", e); - } catch (IOException e) { - log.error("Unhandled exception", e); } - }) - .setNegativeButton(android.R.string.cancel, null) - .show(); + } + reader.close(); + SP.putBoolean(R.string.key_setupwizard_processed, true); + OKDialog.show(context, MainApp.gs(R.string.setting_imported), MainApp.gs(R.string.restartingapp), () -> { + log.debug("Exiting"); + MainApp.instance().stopKeepAliveService(); + RxBus.INSTANCE.send(new EventAppExit()); + MainApp.closeDbHelper(); + if (context instanceof Activity) { + ((Activity) context).finish(); + } + System.runFinalization(); + System.exit(0); + }); + } catch (FileNotFoundException e) { + ToastUtils.showToastInUiThread(context, MainApp.gs(R.string.filenotfound) + " " + file); + log.error("Unhandled exception", e); + } catch (IOException e) { + log.error("Unhandled exception", e); + } + }); } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.java index 6b8863242a..ae1262b651 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.java @@ -2,81 +2,58 @@ package info.nightscout.androidaps.plugins.general.maintenance; import android.content.Intent; import android.os.Bundle; -import androidx.fragment.app.Fragment; -import androidx.appcompat.app.AlertDialog; 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.plugins.general.food.FoodPlugin; import info.nightscout.androidaps.plugins.general.maintenance.activities.LogSettingActivity; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; +import info.nightscout.androidaps.utils.OKDialog; /** * */ public class MaintenanceFragment extends Fragment { - private Fragment f; - - @Override - public void onResume() { - super.onResume(); - - this.f = this; - } - - @Override - public void onPause() { - super.onPause(); - - this.f = null; - } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.maintenance_fragment, container, false); - final Fragment f = this; - view.findViewById(R.id.log_send).setOnClickListener(view1 -> MaintenancePlugin.getPlugin().sendLogs()); view.findViewById(R.id.log_delete).setOnClickListener(view1 -> MaintenancePlugin.getPlugin().deleteLogs()); - view.findViewById(R.id.nav_resetdb).setOnClickListener(view1 -> new AlertDialog.Builder(f.getContext()) - .setTitle(R.string.nav_resetdb) - .setMessage(R.string.reset_db_confirm) - .setNegativeButton(android.R.string.cancel, null) - .setPositiveButton(android.R.string.ok, (dialog, which) -> { + view.findViewById(R.id.nav_resetdb).setOnClickListener(view1 -> + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.maintenance), MainApp.gs(R.string.reset_db_confirm), () -> { MainApp.getDbHelper().resetDatabases(); // should be handled by Plugin-Interface and // additional service interface and plugin registry FoodPlugin.getPlugin().getService().resetFood(); TreatmentsPlugin.getPlugin().getService().resetTreatments(); }) - .create() - .show()); - + ); view.findViewById(R.id.nav_export).setOnClickListener(view1 -> { // start activity for checking permissions... - ImportExportPrefs.verifyStoragePermissions(f); - ImportExportPrefs.exportSharedPreferences(f); + ImportExportPrefs.verifyStoragePermissions(this); + ImportExportPrefs.exportSharedPreferences(this); }); view.findViewById(R.id.nav_import).setOnClickListener(view1 -> { // start activity for checking permissions... - ImportExportPrefs.verifyStoragePermissions(f); - ImportExportPrefs.importSharedPreferences(f); + ImportExportPrefs.verifyStoragePermissions(this); + ImportExportPrefs.importSharedPreferences(this); }); view.findViewById(R.id.nav_logsettings).setOnClickListener(view1 -> { startActivity(new Intent(getActivity(), LogSettingActivity.class)); }); - return view; } 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 112eb9c951..ea3ee3f9a7 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,9 +1,6 @@ package info.nightscout.androidaps.plugins.general.nsclient; -import android.app.AlertDialog; -import android.content.Context; -import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; import android.text.Html; @@ -25,6 +22,7 @@ import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientN 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.OKDialog; import info.nightscout.androidaps.utils.SP; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; @@ -113,20 +111,11 @@ public class NSClientFragment extends Fragment implements View.OnClickListener, NSClientPlugin.getPlugin().clearLog(); break; case R.id.nsclientinternal_clearqueue: - final Context context = getContext(); - AlertDialog.Builder builder = new AlertDialog.Builder(context); - - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage("Clear queue? All data in queue will be lost!"); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - UploadQueue.clearQueue(); - updateGui(); - FabricPrivacy.getInstance().logCustom("NSClientClearQueue"); - } + OKDialog.showConfirmation(getContext(),MainApp.gs(R.string.nsclientinternal), MainApp.gs(R.string.clearqueueconfirm), () -> { + UploadQueue.clearQueue(); + updateGui(); + FabricPrivacy.getInstance().logCustom("NSClientClearQueue"); }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); break; case R.id.nsclientinternal_showqueue: RxBus.INSTANCE.send(new EventNSClientNewLog("QUEUE", NSClientPlugin.getPlugin().queue().textList())); 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 037f91b3f0..957d69a935 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 @@ -32,6 +32,8 @@ 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.data.AlarmAck; +import info.nightscout.androidaps.plugins.general.nsclient.data.NSAlarm; 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; @@ -250,4 +252,25 @@ public class NSClientPlugin extends PluginBase { public boolean hasWritePermission() { return nsClientService.hasWriteAuth; } + + public void handleClearAlarm(NSAlarm originalAlarm, long silenceTimeInMsec) { + + if (!isEnabled(PluginType.GENERAL)) { + return; + } + if (SP.getBoolean(R.string.key_ns_noupload, false)) { + if (L.isEnabled(L.NSCLIENT)) + log.debug("Upload disabled. Message dropped"); + return; + } + + AlarmAck ack = new AlarmAck(); + ack.level = originalAlarm.getLevel(); + ack.group = originalAlarm.getGroup(); + ack.silenceTime = silenceTimeInMsec; + + if (nsClientService != null) + nsClientService.sendAlarmAck(ack); + } + } 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 92fd099e56..b735a153fb 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 @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.general.nsclient; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ResolveInfo; @@ -9,7 +8,6 @@ import android.os.Bundle; import android.preference.PreferenceManager; import androidx.annotation.Nullable; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; @@ -30,6 +28,7 @@ import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.db.DbRequest; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.TempTarget; @@ -40,9 +39,7 @@ import info.nightscout.androidaps.plugins.aps.loop.DeviceStatus; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.nsclient.data.DbLogger; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; -import info.nightscout.androidaps.services.Intents; import info.nightscout.androidaps.utils.BatteryLevel; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.SP; @@ -56,7 +53,6 @@ public class NSUpload { public static void uploadTempBasalStartAbsolute(TemporaryBasal temporaryBasal, Double originalExtendedAmount) { try { - Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); data.put("eventType", CareportalEvent.TEMPBASAL); data.put("duration", temporaryBasal.durationInMinutes); @@ -68,15 +64,7 @@ public class NSUpload { data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); if (originalExtendedAmount != null) data.put("originalExtendedAmount", originalExtendedAmount); // for back synchronization - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -99,7 +87,6 @@ public class NSUpload { uploadTempBasalStartAbsolute(t, null); } } else { - Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); data.put("eventType", CareportalEvent.TEMPBASAL); data.put("duration", temporaryBasal.durationInMinutes); @@ -110,15 +97,7 @@ public class NSUpload { data.put("pumpId", temporaryBasal.pumpId); data.put("created_at", DateUtil.toISOString(temporaryBasal.date)); data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } } catch (JSONException e) { log.error("Unhandled exception", e); @@ -127,7 +106,6 @@ public class NSUpload { public static void uploadTempBasalEnd(long time, boolean isFakedTempBasal, long pumpId) { try { - Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); data.put("eventType", CareportalEvent.TEMPBASAL); data.put("created_at", DateUtil.toISOString(time)); @@ -136,15 +114,7 @@ public class NSUpload { data.put("isFakedTempBasal", isFakedTempBasal); if (pumpId != 0) data.put("pumpId", pumpId); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -152,7 +122,6 @@ public class NSUpload { public static void uploadExtendedBolus(ExtendedBolus extendedBolus) { try { - Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); data.put("eventType", CareportalEvent.COMBOBOLUS); data.put("duration", extendedBolus.durationInMinutes); @@ -164,15 +133,7 @@ public class NSUpload { data.put("pumpId", extendedBolus.pumpId); data.put("created_at", DateUtil.toISOString(extendedBolus.date)); data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -180,7 +141,6 @@ public class NSUpload { public static void uploadExtendedBolusEnd(long time, long pumpId) { try { - Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); data.put("eventType", CareportalEvent.COMBOBOLUS); data.put("duration", 0); @@ -192,15 +152,7 @@ public class NSUpload { data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); if (pumpId != 0) data.put("pumpId", pumpId); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -266,16 +218,7 @@ public class NSUpload { deviceStatus.uploaderBattery = batteryLevel; deviceStatus.created_at = DateUtil.toISOString(new Date()); - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "devicestatus"); - bundle.putString("data", deviceStatus.mongoRecord().toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, deviceStatus.mongoRecord().toString()); + UploadQueue.add(new DbRequest("dbAdd", "devicestatus", deviceStatus.mongoRecord())); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -341,17 +284,7 @@ public class NSUpload { try { JSONObject data = getJson(profileSwitch); if (profileSwitch._id != null) { - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbUpdate"); - bundle.putString("collection", "treatments"); - bundle.putString("data", data.toString()); - bundle.putString("_id", profileSwitch._id); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbUpdate", "treatments", profileSwitch._id, data)); } } catch (JSONException e) { log.error("Unhandled exception", e); @@ -390,16 +323,7 @@ public class NSUpload { prebolus.put("created_at", DateUtil.toISOString(preBolusDate)); uploadCareportalEntryToNS(prebolus); } - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } catch (Exception e) { log.error("Unhandled exception", e); } @@ -407,40 +331,17 @@ public class NSUpload { } public static void removeCareportalEntryFromNS(String _id) { - try { - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbRemove"); - bundle.putString("collection", "treatments"); - bundle.putString("_id", _id); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbRemove(intent, _id); - } catch (Exception e) { - log.error("Unhandled exception", e); - } - + UploadQueue.add(new DbRequest("dbRemove", "treatments", _id)); } public static void uploadOpenAPSOffline(double durationInMinutes) { try { - Context context = MainApp.instance().getApplicationContext(); JSONObject data = new JSONObject(); data.put("eventType", "OpenAPS Offline"); data.put("duration", durationInMinutes); data.put("created_at", DateUtil.toISOString(new Date())); data.put("enteredBy", "openaps://" + MainApp.gs(R.string.app_name)); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -451,10 +352,6 @@ public class NSUpload { } public static void uploadError(String error, Date date) { - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); JSONObject data = new JSONObject(); try { data.put("eventType", "Announcement"); @@ -465,19 +362,10 @@ public class NSUpload { } catch (JSONException e) { log.error("Unhandled exception", e); } - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } public static void uploadBg(BgReading reading, String source) { - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "entries"); JSONObject data = new JSONObject(); try { data.put("device", source); @@ -489,20 +377,11 @@ public class NSUpload { } catch (JSONException e) { log.error("Unhandled exception", e); } - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "entries", data)); } public static void uploadAppStart() { if (SP.getBoolean(R.string.key_ns_logappstartedevent, true)) { - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); JSONObject data = new JSONObject(); try { data.put("eventType", "Note"); @@ -511,35 +390,17 @@ public class NSUpload { } catch (JSONException e) { log.error("Unhandled exception", e); } - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } } public static void uploadProfileStore(JSONObject profileStore) { if (SP.getBoolean(R.string.key_ns_uploadlocalprofile, false)) { - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "profile"); - bundle.putString("data", String.valueOf(profileStore)); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, String.valueOf(profileStore)); + UploadQueue.add(new DbRequest("dbAdd", "profile", String.valueOf(profileStore))); } } public static void uploadEvent(String careportalEvent, long time, @Nullable String notes) { - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbAdd"); - bundle.putString("collection", "treatments"); JSONObject data = new JSONObject(); try { data.put("eventType", careportalEvent); @@ -551,26 +412,12 @@ public class NSUpload { } catch (JSONException e) { log.error("Unhandled exception", e); } - bundle.putString("data", data.toString()); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbAdd(intent, data.toString()); + UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); } public static void removeFoodFromNS(String _id) { try { - Context context = MainApp.instance().getApplicationContext(); - Bundle bundle = new Bundle(); - bundle.putString("action", "dbRemove"); - bundle.putString("collection", "food"); - bundle.putString("_id", _id); - Intent intent = new Intent(Intents.ACTION_DATABASE); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(context).sendBroadcast(intent); - DbLogger.dbRemove(intent, _id); + UploadQueue.add(new DbRequest("dbRemove", "food", _id)); } catch (Exception e) { log.error("Unhandled exception", e); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/broadcasts/BroadcastAckAlarm.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/broadcasts/BroadcastAckAlarm.java deleted file mode 100644 index 7179231068..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/broadcasts/BroadcastAckAlarm.java +++ /dev/null @@ -1,43 +0,0 @@ -package info.nightscout.androidaps.plugins.general.nsclient.broadcasts; - -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.services.Intents; -import info.nightscout.androidaps.plugins.general.nsclient.data.NSAlarm; -import info.nightscout.androidaps.utils.SP; - -/** - * Created by mike on 11.06.2017. - */ - -public class BroadcastAckAlarm { - - public static void handleClearAlarm(NSAlarm originalAlarm, Context context, long silenceTimeInMsec) { - - Bundle bundle = new Bundle(); - bundle.putInt("level", originalAlarm.getLevel()); - bundle.putString("group", originalAlarm.getGroup()); - bundle.putLong("silenceTime", silenceTimeInMsec); - Intent intent = new Intent(Intents.ACTION_ACK_ALARM); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent); - - if(SP.getBoolean(R.string.key_nsclient_localbroadcasts, false)) { - bundle = new Bundle(); - bundle.putInt("level", originalAlarm.getLevel()); - bundle.putString("group", originalAlarm.getGroup()); - bundle.putLong("silenceTime", silenceTimeInMsec); - intent = new Intent(Intents.ACTION_ACK_ALARM); - intent.putExtras(bundle); - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); - } - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/broadcasts/BroadcastTreatment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/broadcasts/BroadcastTreatment.java index a364d0d4ae..7b722f562d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/broadcasts/BroadcastTreatment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/broadcasts/BroadcastTreatment.java @@ -25,12 +25,11 @@ import info.nightscout.androidaps.utils.SP; public class BroadcastTreatment { private static Logger log = LoggerFactory.getLogger(L.NSCLIENT); - public static void handleNewTreatment(JSONObject treatment, boolean isDelta, boolean isLocalBypass) { + public static void handleNewTreatment(JSONObject treatment, boolean isDelta) { Bundle bundle = new Bundle(); bundle.putString("treatment", treatment.toString()); bundle.putBoolean("delta", isDelta); - bundle.putBoolean("islocal", isLocalBypass); Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/data/DbLogger.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/data/DbLogger.java deleted file mode 100644 index 3aaf4ccac4..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/data/DbLogger.java +++ /dev/null @@ -1,43 +0,0 @@ -package info.nightscout.androidaps.plugins.general.nsclient.data; - -import android.content.Intent; -import android.content.pm.ResolveInfo; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.utils.ToastUtils; - -/** - * Created by mike on 02.07.2016. - */ -public class DbLogger { - private static Logger log = LoggerFactory.getLogger(L.NSCLIENT); - - public static void dbAdd(Intent intent, String data) { - List q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(intent, 0); - if (q.size() < 1) { - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.nsclientnotinstalled)); - log.error("DBADD No receivers"); - } else if (L.isEnabled(L.NSCLIENT)) { - if (L.isEnabled(L.NSCLIENT)) - log.debug("DBADD dbAdd " + q.size() + " receivers " + data); - } - } - - public static void dbRemove(Intent intent, String data) { - List q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(intent, 0); - if (q.size() < 1) { - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.nsclientnotinstalled)); - log.error("DBREMOVE No receivers"); - } else if (L.isEnabled(L.NSCLIENT)) { - if (L.isEnabled(L.NSCLIENT)) - log.debug("DBREMOVE dbRemove " + q.size() + " receivers " + data); - } - } -} 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 deleted file mode 100644 index 1c46716d69..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/receivers/AckAlarmReceiver.java +++ /dev/null @@ -1,60 +0,0 @@ -package info.nightscout.androidaps.plugins.general.nsclient.receivers; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.os.PowerManager; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; -import info.nightscout.androidaps.plugins.general.nsclient.data.AlarmAck; -import info.nightscout.androidaps.plugins.general.nsclient.services.NSClientService; -import info.nightscout.androidaps.utils.SP; - -public class AckAlarmReceiver extends BroadcastReceiver { - private static Logger log = LoggerFactory.getLogger(L.NSCLIENT); - - - @Override - public void onReceive(Context context, Intent intent) { - PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, - AckAlarmReceiver.class.getSimpleName()); - NSClientPlugin nsClientPlugin = NSClientPlugin.getPlugin(); - if (!nsClientPlugin.isEnabled(PluginType.GENERAL)) { - return; - } - if (SP.getBoolean(R.string.key_ns_noupload, false)) { - if (L.isEnabled(L.NSCLIENT)) - log.debug("Upload disabled. Message dropped"); - return; - } - wakeLock.acquire(); - try { - Bundle bundles = intent.getExtras(); - if (bundles == null) return; - if (!bundles.containsKey("level")) return; - if (!bundles.containsKey("group")) return; - if (!bundles.containsKey("silenceTime")) return; - - AlarmAck ack = new AlarmAck(); - ack.level = bundles.getInt("level"); - ack.group = bundles.getString("group"); - ack.silenceTime = bundles.getLong("silenceTime"); - - NSClientService nsClientService = nsClientPlugin.nsClientService; - if (nsClientService != null) - nsClientService.sendAlarmAck(ack); - - } finally { - wakeLock.release(); - } - } -} 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 deleted file mode 100644 index 4838c178ef..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/receivers/DBAccessReceiver.java +++ /dev/null @@ -1,147 +0,0 @@ -package info.nightscout.androidaps.plugins.general.nsclient.receivers; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.os.PowerManager; - -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.db.DbRequest; -import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.logging.BundleLogger; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; -import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue; -import info.nightscout.androidaps.plugins.general.nsclient.broadcasts.BroadcastTreatment; -import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.SP; - -public class DBAccessReceiver extends BroadcastReceiver { - private static Logger log = LoggerFactory.getLogger(L.NSCLIENT); - - - @Override - public void onReceive(Context context, Intent intent) { - PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); - PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, - DBAccessReceiver.class.getSimpleName()); - wakeLock.acquire(); - try { - Bundle bundles = intent.getExtras(); - if (bundles == null) return; - if (!bundles.containsKey("action")) return; - - if (L.isEnabled(L.NSCLIENT)) - log.debug(BundleLogger.log(bundles)); - - String collection = null; - String _id = null; - JSONObject data = null; - String action = bundles.getString("action"); - try { - collection = bundles.getString("collection"); - } catch (Exception e) { - log.error("Unhandled exception", e); - return; - } - try { - if (!action.equals("dbAdd")) - _id = bundles.getString("_id"); - } catch (Exception e) { - log.error("Unhandled exception", e); - return; - } - try { - if (!action.equals("dbRemove")) - data = new JSONObject(bundles.getString("data")); - } catch (Exception e) { - log.error("Unhandled exception", e); - return; - } - - if (data == null && !action.equals("dbRemove") || _id == null && action.equals("dbRemove")) { - log.error("DBACCESS no data inside record"); - return; - } - - if (action.equals("dbRemove")) { - data = new JSONObject(); - } - // mark by id - Long nsclientid = System.currentTimeMillis(); - try { - data.put("NSCLIENT_ID", nsclientid); - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - - if (!isAllowedCollection(collection)) { - log.error("DBACCESS wrong collection specified"); - return; - } - - if (action.equals("dbRemove")) { - if (shouldUpload()) { - DbRequest dbr = new DbRequest(action, collection, nsclientid.toString(), _id); - UploadQueue.add(dbr); - } - } else if (action.equals("dbUpdate")) { - if (shouldUpload()) { - DbRequest dbr = new DbRequest(action, collection, nsclientid.toString(), _id, data); - UploadQueue.add(dbr); - } - } else { - DbRequest dbr = new DbRequest(action, collection, nsclientid.toString(), data); - // this is not used as mongo _id but only for searching in UploadQueue database - // if record has to be removed from queue before upload - dbr._id = nsclientid.toString(); - - if (shouldUpload()) { - UploadQueue.add(dbr); - } - if (collection.equals("treatments")) { - generateTreatmentOfflineBroadcast(dbr); - } - } - - } finally { - wakeLock.release(); - } - - } - - public boolean shouldUpload() { - NSClientPlugin nsClientPlugin = NSClientPlugin.getPlugin(); - return nsClientPlugin.isEnabled(PluginType.GENERAL) && !SP.getBoolean(R.string.key_ns_noupload, false); - } - - public void generateTreatmentOfflineBroadcast(DbRequest request) { - if (request.action.equals("dbAdd")) { - try { - JSONObject data = new JSONObject(request.data); - data.put("mills", DateUtil.fromISODateString(data.getString("created_at")).getTime()); - data.put("_id", data.get("NSCLIENT_ID")); // this is only fake id - BroadcastTreatment.handleNewTreatment(data, false, true); - } catch (Exception e) { - log.error("Unhadled exception", e); - } - } - } - - private boolean isAllowedCollection(String collection) { - // "treatments" || "entries" || "devicestatus" || "profile" || "food" - if (collection.equals("treatments")) return true; - if (collection.equals("entries")) return true; - if (collection.equals("devicestatus")) return true; - if (collection.equals("profile")) return true; - if (collection.equals("food")) return true; - return false; - } -} 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 f3b718743e..21a3e1b271 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 @@ -61,6 +61,14 @@ import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TemporaryBasal; +import info.nightscout.androidaps.dialogs.CalibrationDialog; +import info.nightscout.androidaps.dialogs.CarbsDialog; +import info.nightscout.androidaps.dialogs.InsulinDialog; +import info.nightscout.androidaps.dialogs.ProfileSwitchDialog; +import info.nightscout.androidaps.dialogs.ProfileViewerDialog; +import info.nightscout.androidaps.dialogs.TempTargetDialog; +import info.nightscout.androidaps.dialogs.TreatmentDialog; +import info.nightscout.androidaps.dialogs.WizardDialog; import info.nightscout.androidaps.events.EventAcceptOpenLoopChange; import info.nightscout.androidaps.events.EventCareportalEventChange; import info.nightscout.androidaps.events.EventExtendedBolusChange; @@ -84,16 +92,9 @@ 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; -import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; -import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.nsclient.data.NSDeviceStatus; import info.nightscout.androidaps.plugins.general.overview.activities.QuickWizardListActivity; -import info.nightscout.androidaps.plugins.general.overview.dialogs.CalibrationDialog; -import info.nightscout.androidaps.plugins.general.overview.dialogs.NewCarbsDialog; -import info.nightscout.androidaps.plugins.general.overview.dialogs.NewInsulinDialog; -import info.nightscout.androidaps.plugins.general.overview.dialogs.NewTreatmentDialog; -import info.nightscout.androidaps.plugins.general.overview.dialogs.WizardDialog; import info.nightscout.androidaps.plugins.general.overview.graphData.GraphData; import info.nightscout.androidaps.plugins.general.wear.ActionStringHandler; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; @@ -105,7 +106,6 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCa import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin; import info.nightscout.androidaps.plugins.source.SourceXdripPlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.plugins.treatments.fragments.ProfileViewerDialog; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.BolusWizard; import info.nightscout.androidaps.utils.DateUtil; @@ -744,11 +744,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, updateGUI("suspendmenu"); return true; } else if (item.getTitle().equals(MainApp.gs(R.string.careportal_profileswitch))) { - NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); - final OptionsToShow profileswitch = CareportalFragment.PROFILESWITCHDIRECT; - profileswitch.executeProfileSwitch = true; - newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); - newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); + FragmentManager manager = getFragmentManager(); + if (manager != null) + new ProfileSwitchDialog().show(manager, "Overview"); } else if (item.getTitle().equals(MainApp.gs(R.string.danar_viewprofile))) { Bundle args = new Bundle(); args.putLong("time", DateUtil.now()); @@ -759,44 +757,39 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, if (manager != null) pvd.show(manager, "ProfileViewDialog"); } else if (item.getTitle().equals(MainApp.gs(R.string.eatingsoon))) { - DefaultValueHelper defHelper = new DefaultValueHelper(); - double target = Profile.toMgdl(defHelper.determineEatingSoonTT(), ProfileFunctions.getSystemUnits()); + double target = Profile.toMgdl(DefaultValueHelper.determineEatingSoonTT(), ProfileFunctions.getSystemUnits()); TempTarget tempTarget = new TempTarget() .date(System.currentTimeMillis()) - .duration(defHelper.determineEatingSoonTTDuration()) + .duration(DefaultValueHelper.determineEatingSoonTTDuration()) .reason(MainApp.gs(R.string.eatingsoon)) .source(Source.USER) .low(target) .high(target); TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); } else if (item.getTitle().equals(MainApp.gs(R.string.activity))) { - DefaultValueHelper defHelper = new DefaultValueHelper(); - double target = Profile.toMgdl(defHelper.determineActivityTT(), ProfileFunctions.getSystemUnits()); + double target = Profile.toMgdl(DefaultValueHelper.determineActivityTT(), ProfileFunctions.getSystemUnits()); TempTarget tempTarget = new TempTarget() .date(now()) - .duration(defHelper.determineActivityTTDuration()) + .duration(DefaultValueHelper.determineActivityTTDuration()) .reason(MainApp.gs(R.string.activity)) .source(Source.USER) .low(target) .high(target); TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); } else if (item.getTitle().equals(MainApp.gs(R.string.hypo))) { - DefaultValueHelper defHelper = new DefaultValueHelper(); - double target = Profile.toMgdl(defHelper.determineHypoTT(), ProfileFunctions.getSystemUnits()); + double target = Profile.toMgdl(DefaultValueHelper.determineHypoTT(), ProfileFunctions.getSystemUnits()); TempTarget tempTarget = new TempTarget() .date(now()) - .duration(defHelper.determineHypoTTDuration()) + .duration(DefaultValueHelper.determineHypoTTDuration()) .reason(MainApp.gs(R.string.hypo)) .source(Source.USER) .low(target) .high(target); TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); } else if (item.getTitle().equals(MainApp.gs(R.string.custom))) { - NewNSTreatmentDialog newTTDialog = new NewNSTreatmentDialog(); - final OptionsToShow temptarget = CareportalFragment.TEMPTARGET; - temptarget.executeTempTarget = true; - newTTDialog.setOptions(temptarget, R.string.careportal_temporarytarget); - newTTDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); + FragmentManager manager = getFragmentManager(); + if (manager != null) + new TempTargetDialog().show(manager, "Overview"); } else if (item.getTitle().equals(MainApp.gs(R.string.cancel))) { TempTarget tempTarget = new TempTarget() .source(Source.USER) @@ -818,7 +811,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, FragmentManager manager = getFragmentManager(); // try to fix https://fabric.io/nightscout3/android/apps/info.nightscout.androidaps/issues/5aca7a1536c7b23527eb4be7?time=last-seven-days // https://stackoverflow.com/questions/14860239/checking-if-state-is-saved-before-committing-a-fragmenttransaction - if (manager.isStateSaved()) + if (manager == null || manager.isStateSaved()) return; switch (v.getId()) { case R.id.overview_accepttempbutton: @@ -863,14 +856,13 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } break; case R.id.overview_treatmentbutton: - NewTreatmentDialog treatmentDialogFragment = new NewTreatmentDialog(); - treatmentDialogFragment.show(manager, "TreatmentDialog"); + new TreatmentDialog().show(manager, "Overview"); break; case R.id.overview_insulinbutton: - new NewInsulinDialog().show(manager, "InsulinDialog"); + new InsulinDialog().show(manager, "Overview"); break; case R.id.overview_carbsbutton: - new NewCarbsDialog().show(manager, "CarbsDialog"); + new CarbsDialog().show(manager, "Overview"); break; case R.id.overview_pumpstatus: if (ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended() || !ConfigBuilderPlugin.getPlugin().getActivePump().isInitialized()) @@ -891,10 +883,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, getContext().startActivity(intent); return true; } catch (ActivityNotFoundException e) { - new AlertDialog.Builder(getContext()) - .setMessage(R.string.error_starting_cgm) - .setPositiveButton("OK", null) - .show(); + OKDialog.show(getContext(), "", MainApp.gs(R.string.error_starting_cgm)); return false; } } @@ -920,16 +909,11 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, LoopPlugin.getPlugin().invoke("Accept temp button", false); final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun; if (finalLastRun != null && finalLastRun.lastAPSRun != null && finalLastRun.constraintsProcessed.isChangeRequested()) { - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.setbasalquestion) + "\n" + finalLastRun.constraintsProcessed); - builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { + OKDialog.showConfirmation(context, MainApp.gs(R.string.pump_tempbasal_label), finalLastRun.constraintsProcessed.toSpanned(), () -> { hideTempRecommendation(); clearNotification(); LoopPlugin.getPlugin().acceptChangeRequest(); }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); } } } @@ -949,7 +933,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(quickWizardEntry.carbs())).value(); if (Math.abs(wizard.getInsulinAfterConstraints() - wizard.getCalculatedTotalInsulin()) >= pump.getPumpDescription().pumpType.determineCorrectBolusStepSize(wizard.getInsulinAfterConstraints()) || !carbsAfterConstraints.equals(quickWizardEntry.carbs())) { - OKDialog.show(getContext(), MainApp.gs(R.string.treatmentdeliveryerror), MainApp.gs(R.string.constraints_violation) + "\n" + MainApp.gs(R.string.changeyourinput), null); + OKDialog.show(getContext(), MainApp.gs(R.string.treatmentdeliveryerror), MainApp.gs(R.string.constraints_violation) + "\n" + MainApp.gs(R.string.changeyourinput)); return; } @@ -1178,7 +1162,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, if (activeTemp != null) { fullText += MainApp.gs(R.string.pump_tempbasal_label) + ": " + activeTemp.toStringFull(); } - OKDialog.show(getActivity(), MainApp.gs(R.string.basal), fullText, null); + OKDialog.show(getActivity(), MainApp.gs(R.string.basal), fullText); }); } else { @@ -1214,7 +1198,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } extendedBolusView.setText(extendedBolusText); if (Config.NSCLIENT) { - extendedBolusView.setOnClickListener(v -> OKDialog.show(getActivity(), MainApp.gs(R.string.extendedbolus), extendedBolus.toString(), null)); + extendedBolusView.setOnClickListener(v -> OKDialog.show(getActivity(), MainApp.gs(R.string.extended_bolus), extendedBolus.toString())); } if (extendedBolusText.equals("")) extendedBolusView.setVisibility(Config.NSCLIENT ? View.INVISIBLE : View.GONE); @@ -1308,7 +1292,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, String iobtext1 = DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U\n" + MainApp.gs(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U\n" + MainApp.gs(R.string.basal) + ": " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U\n"; - OKDialog.show(getActivity(), MainApp.gs(R.string.iob), iobtext1, null); + OKDialog.show(getActivity(), MainApp.gs(R.string.iob), iobtext1); }); } else if (MainApp.sResources.getBoolean(R.bool.isTablet)) { String iobtext = DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U (" @@ -1360,19 +1344,19 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, // pump status from ns if (pumpDeviceStatusView != null) { pumpDeviceStatusView.setText(NSDeviceStatus.getInstance().getPumpStatus()); - pumpDeviceStatusView.setOnClickListener(v -> OKDialog.show(getActivity(), MainApp.gs(R.string.pump), NSDeviceStatus.getInstance().getExtendedPumpStatus(), null)); + pumpDeviceStatusView.setOnClickListener(v -> OKDialog.show(getActivity(), MainApp.gs(R.string.pump), NSDeviceStatus.getInstance().getExtendedPumpStatus())); } // OpenAPS status from ns if (openapsDeviceStatusView != null) { openapsDeviceStatusView.setText(NSDeviceStatus.getInstance().getOpenApsStatus()); - openapsDeviceStatusView.setOnClickListener(v -> OKDialog.show(getActivity(), MainApp.gs(R.string.openaps), NSDeviceStatus.getInstance().getExtendedOpenApsStatus(), null)); + openapsDeviceStatusView.setOnClickListener(v -> OKDialog.show(getActivity(), MainApp.gs(R.string.openaps), NSDeviceStatus.getInstance().getExtendedOpenApsStatus())); } // Uploader status from ns if (uploaderDeviceStatusView != null) { uploaderDeviceStatusView.setText(NSDeviceStatus.getInstance().getUploaderStatusSpanned()); - uploaderDeviceStatusView.setOnClickListener(v -> OKDialog.show(getActivity(), MainApp.gs(R.string.uploader), NSDeviceStatus.getInstance().getExtendedUploaderStatus(), null)); + uploaderDeviceStatusView.setOnClickListener(v -> OKDialog.show(getActivity(), MainApp.gs(R.string.uploader), NSDeviceStatus.getInstance().getExtendedUploaderStatus())); } // Sensitivity 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 deleted file mode 100644 index 645f6d2b19..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/BolusProgressDialog.java +++ /dev/null @@ -1,198 +0,0 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs; - - -import android.app.Activity; -import android.os.Bundle; -import android.os.SystemClock; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.ProgressBar; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.fragment.app.DialogFragment; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -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.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; - ProgressBar progressBar; - BolusProgressHelperActivity helperActivity; - - static double amount; - public static boolean bolusEnded = false; - public static boolean running = true; - public static boolean stopPressed = false; - - private String state; - private final static String DEFAULT_STATE = MainApp.gs(R.string.waitingforpump); - - public BolusProgressDialog() { - super(); - } - - public void setInsulin(double amount) { - BolusProgressDialog.amount = amount; - bolusEnded = false; - } - - public void setHelperActivity(BolusProgressHelperActivity activity) { - this.helperActivity = activity; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - getDialog().setTitle(String.format(MainApp.gs(R.string.overview_bolusprogress_goingtodeliver), amount)); - View view = inflater.inflate(R.layout.overview_bolusprogress_dialog, container, false); - stopButton = view.findViewById(R.id.overview_bolusprogress_stop); - statusView = view.findViewById(R.id.overview_bolusprogress_status); - stopPressedView = view.findViewById(R.id.overview_bolusprogress_stoppressed); - progressBar = view.findViewById(R.id.overview_bolusprogress_progressbar); - stopButton.setOnClickListener(this); - progressBar.setMax(100); - state = savedInstanceState != null ? savedInstanceState.getString("state", DEFAULT_STATE) : DEFAULT_STATE; - statusView.setText(state); - setCancelable(false); - stopPressed = false; - return view; - } - - @Override - public void onResume() { - super.onResume(); - if (L.isEnabled(L.UI)) - log.debug("onResume"); - if (!ConfigBuilderPlugin.getPlugin().getCommandQueue().bolusInQueue()) { - bolusEnded = true; - } - if (bolusEnded) { - dismiss(); - } else { - if (getDialog() != null) - getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); - running = true; - if (L.isEnabled(L.UI)) - log.debug("onResume running"); - } - 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 - public void dismiss() { - if (L.isEnabled(L.UI)) - log.debug("dismiss"); - try { - super.dismiss(); - } catch (IllegalStateException e) { - // dialog not running yet. onResume will try again. Set bolusEnded to make extra - // sure onResume will catch this - bolusEnded = true; - log.error("Unhandled exception", e); - } - if (helperActivity != null) { - helperActivity.finish(); - } - } - - @Override - public void onPause() { - if (L.isEnabled(L.UI)) - log.debug("onPause"); - running = false; - super.onPause(); - disposable.clear(); - } - - @Override - public void onSaveInstanceState(@NonNull Bundle outState) { - outState.putString("state", state); - log.debug("storing state: " + state); - super.onSaveInstanceState(outState); - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.overview_bolusprogress_stop: - if (L.isEnabled(L.UI)) - log.debug("Stop bolus delivery button pressed"); - stopPressed = true; - stopPressedView.setVisibility(View.VISIBLE); - stopButton.setVisibility(View.INVISIBLE); - ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelAllBoluses(); - break; - } - } - - private void scheduleDismiss() { - if (L.isEnabled(L.UI)) - log.debug("scheduleDismiss"); - Thread t = new Thread(() -> { - SystemClock.sleep(5000); - BolusProgressDialog.bolusEnded = true; - Activity activity = getActivity(); - if (activity != null) { - activity.runOnUiThread(() -> { - try { - if (running) { - if (L.isEnabled(L.UI)) - log.debug("executing"); - dismiss(); - } - } catch (Exception e) { - log.error("Unhandled exception", e); - } - }); - } - }); - t.start(); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/BolusProgressHelperActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/BolusProgressHelperActivity.java deleted file mode 100644 index 54f6aa7688..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/BolusProgressHelperActivity.java +++ /dev/null @@ -1,20 +0,0 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs; - -import android.os.Bundle; - -import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; - -public class BolusProgressHelperActivity extends NoSplashAppCompatActivity { - public BolusProgressHelperActivity() { - super(); - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - BolusProgressDialog bolusProgressDialog = new BolusProgressDialog(); - bolusProgressDialog.setHelperActivity(this); - bolusProgressDialog.setInsulin(getIntent().getDoubleExtra("insulin", 0d)); - bolusProgressDialog.show(getSupportFragmentManager(), "BolusProgress"); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/CalibrationDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/CalibrationDialog.java deleted file mode 100644 index 4c5d5b5d80..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/CalibrationDialog.java +++ /dev/null @@ -1,94 +0,0 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs; - - -import android.content.Context; -import android.os.Bundle; -import androidx.fragment.app.DialogFragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.Window; -import android.view.WindowManager; -import android.widget.TextView; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.text.DecimalFormat; - -import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus; -import info.nightscout.androidaps.data.Profile; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.utils.NumberPicker; -import info.nightscout.androidaps.utils.SafeParse; -import info.nightscout.androidaps.utils.XdripCalibrations; - -public class CalibrationDialog extends DialogFragment implements View.OnClickListener { - private static Logger log = LoggerFactory.getLogger(CalibrationDialog.class); - - NumberPicker bgNumber; - TextView unitsView; - - Context context; - - public CalibrationDialog() { - // Required empty public constructor - } - - @Override - public void onAttach(Context context) { - super.onAttach(context); - this.context = context; - } - - @Override - public void onDetach() { - super.onDetach(); - this.context = null; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.overview_calibration_dialog, container, false); - - getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); - getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - - view.findViewById(R.id.ok).setOnClickListener(this); - view.findViewById(R.id.cancel).setOnClickListener(this); - - String units = ProfileFunctions.getSystemUnits(); - Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, units); - - bgNumber = (NumberPicker) view.findViewById(R.id.overview_calibration_bg); - - if (units.equals(Constants.MMOL)) - bgNumber.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false, view.findViewById(R.id.ok)); - else - bgNumber.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false, view.findViewById(R.id.ok)); - - unitsView = (TextView) view.findViewById(R.id.overview_calibration_units); - unitsView.setText(units); - - setCancelable(true); - getDialog().setCanceledOnTouchOutside(false); - return view; - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.ok: - final Double bg = SafeParse.stringToDouble(bgNumber.getText()); - XdripCalibrations.confirmAndSendCalibration(bg, context); - dismiss(); - break; - case R.id.cancel: - dismiss(); - break; - } - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/NewCarbsDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/NewCarbsDialog.java deleted file mode 100644 index 36cfd07be9..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/NewCarbsDialog.java +++ /dev/null @@ -1,443 +0,0 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs; - -import android.os.Bundle; -import androidx.fragment.app.DialogFragment; -import androidx.appcompat.app.AlertDialog; -import android.text.Editable; -import android.text.Html; -import android.text.TextWatcher; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.view.Window; -import android.view.WindowManager; -import android.widget.Button; -import android.widget.CompoundButton; -import android.widget.EditText; -import android.widget.LinearLayout; -import android.widget.RadioButton; - -import com.google.common.base.Joiner; - -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.text.DecimalFormat; -import java.util.LinkedList; -import java.util.List; - -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.CareportalEvent; -import info.nightscout.androidaps.db.DatabaseHelper; -import info.nightscout.androidaps.db.Source; -import info.nightscout.androidaps.db.TempTarget; -import info.nightscout.androidaps.interfaces.Constraint; -import info.nightscout.androidaps.plugins.treatments.CarbsGenerator; -import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.DecimalFormatter; -import info.nightscout.androidaps.utils.DefaultValueHelper; -import info.nightscout.androidaps.utils.NumberPicker; -import info.nightscout.androidaps.utils.SP; -import info.nightscout.androidaps.utils.ToastUtils; - -import static info.nightscout.androidaps.utils.DateUtil.now; - -public class NewCarbsDialog extends DialogFragment implements OnClickListener, CompoundButton.OnCheckedChangeListener { - private static Logger log = LoggerFactory.getLogger(NewCarbsDialog.class); - - private static final int FAV1_DEFAULT = 5; - private static final int FAV2_DEFAULT = 10; - private static final int FAV3_DEFAULT = 20; - - private RadioButton startActivityTTCheckbox; - private RadioButton startEatingSoonTTCheckbox; - private RadioButton startHypoTTCheckbox; - private boolean togglingTT; - - private NumberPicker editTime; - private NumberPicker editDuration; - private NumberPicker editCarbs; - private Integer maxCarbs; - - private EditText notesEdit; - - //one shot guards - private boolean accepted; - private boolean okClicked; - - public NewCarbsDialog() { - } - - final private TextWatcher textWatcher = new TextWatcher() { - @Override - public void afterTextChanged(Editable s) { - validateInputs(); - } - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - }; - - private void validateInputs() { - int time = editTime.getValue().intValue(); - if (time > 12 * 60 || time < -12 * 60) { - editTime.setValue(0d); - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.constraintapllied)); - } - Double duration = editDuration.getValue(); - if (duration > 10) { - editDuration.setValue(0d); - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.constraintapllied)); - } - int carbs = editCarbs.getValue().intValue(); - if (carbs > maxCarbs) { - editCarbs.setValue(0d); - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.carbsconstraintapplied)); - } - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.overview_newcarbs_dialog, container, false); - - view.findViewById(R.id.ok).setOnClickListener(this); - view.findViewById(R.id.cancel).setOnClickListener(this); - - getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); - getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - - startActivityTTCheckbox = view.findViewById(R.id.newcarbs_activity_tt); - startActivityTTCheckbox.setOnCheckedChangeListener(this); - startEatingSoonTTCheckbox = view.findViewById(R.id.newcarbs_eating_soon_tt); - startEatingSoonTTCheckbox.setOnCheckedChangeListener(this); - startHypoTTCheckbox = view.findViewById(R.id.newcarbs_hypo_tt); - - editTime = view.findViewById(R.id.newcarbs_time); - editTime.setParams(0d, -12 * 60d, 12 * 60d, 5d, new DecimalFormat("0"), false, view.findViewById(R.id.ok), textWatcher); - - editDuration = view.findViewById(R.id.new_carbs_duration); - editDuration.setParams(0d, 0d, 10d, 1d, new DecimalFormat("0"), false, view.findViewById(R.id.ok), textWatcher); - - maxCarbs = MainApp.getConstraintChecker().getMaxCarbsAllowed().value(); - - editCarbs = view.findViewById(R.id.newcarb_carbsamount); - editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false, view.findViewById(R.id.ok), textWatcher); - - Button fav1Button = view.findViewById(R.id.newcarbs_plus1); - fav1Button.setOnClickListener(this); - fav1Button.setText(toSignedString(SP.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT))); - - Button fav2Button = view.findViewById(R.id.newcarbs_plus2); - fav2Button.setOnClickListener(this); - fav2Button.setText(toSignedString(SP.getInt(R.string.key_carbs_button_increment_2, FAV2_DEFAULT))); - - Button fav3Button = view.findViewById(R.id.newcarbs_plus3); - fav3Button.setOnClickListener(this); - fav3Button.setText(toSignedString(SP.getInt(R.string.key_carbs_button_increment_3, FAV3_DEFAULT))); - - LinearLayout notesLayout = view.findViewById(R.id.newcarbs_notes_layout); - notesLayout.setVisibility(SP.getBoolean(R.string.key_show_notes_entry_dialogs, false) ? View.VISIBLE : View.GONE); - notesEdit = view.findViewById(R.id.newcarbs_notes); - - BgReading bgReading = DatabaseHelper.actualBg(); - if (bgReading != null && bgReading.value < 72) { - startHypoTTCheckbox.setChecked(true); - // see #onCheckedChanged why listeners are registered like this - startHypoTTCheckbox.setOnClickListener(this); - } else { - startHypoTTCheckbox.setOnCheckedChangeListener(this); - } - - setCancelable(true); - getDialog().setCanceledOnTouchOutside(false); - - //recovering state if there is something - if (savedInstanceState != null) { - editCarbs.setValue(savedInstanceState.getDouble("editCarbs")); - editTime.setValue(savedInstanceState.getDouble("editTime")); - editDuration.setValue(savedInstanceState.getDouble("editDuration")); - } - return view; - } - - private String toSignedString(int value) { - return value > 0 ? "+" + value : String.valueOf(value); - } - - - @Override - public void onSaveInstanceState(Bundle carbsDialogState) { - carbsDialogState.putBoolean("startActivityTTCheckbox",startActivityTTCheckbox.isChecked()); - carbsDialogState.putBoolean("startEatingSoonTTCheckbox", startEatingSoonTTCheckbox.isChecked()); - carbsDialogState.putBoolean("startHypoTTCheckbox", startHypoTTCheckbox.isChecked()); - carbsDialogState.putDouble("editTime", editTime.getValue()); - carbsDialogState.putDouble("editDuration", editDuration.getValue()); - carbsDialogState.putDouble("editCarbs", editCarbs.getValue()); - super.onSaveInstanceState(carbsDialogState); - } - - - @Override - public synchronized void onClick(View view) { - switch (view.getId()) { - case R.id.ok: - submit(); - break; - case R.id.cancel: - dismiss(); - break; - case R.id.newcarbs_plus1: - editCarbs.setValue(Math.max(0, editCarbs.getValue() - + SP.getInt(R.string.key_carbs_button_increment_1, FAV1_DEFAULT))); - validateInputs(); - break; - case R.id.newcarbs_plus2: - editCarbs.setValue(Math.max(0, editCarbs.getValue() - + SP.getInt(R.string.key_carbs_button_increment_2, FAV2_DEFAULT))); - validateInputs(); - break; - case R.id.newcarbs_plus3: - editCarbs.setValue(Math.max(0, editCarbs.getValue() - + SP.getInt(R.string.key_carbs_button_increment_3, FAV3_DEFAULT))); - validateInputs(); - break; - case R.id.newcarbs_activity_tt: - if (togglingTT) { - togglingTT = false; - break; - } - startActivityTTCheckbox.setOnClickListener(null); - startActivityTTCheckbox.setOnCheckedChangeListener(null); - startActivityTTCheckbox.setChecked(false); - startActivityTTCheckbox.setOnCheckedChangeListener(this); - break; - case R.id.newcarbs_eating_soon_tt: - if (togglingTT) { - togglingTT = false; - break; - } - startEatingSoonTTCheckbox.setOnClickListener(null); - startEatingSoonTTCheckbox.setOnCheckedChangeListener(null); - startEatingSoonTTCheckbox.setChecked(false); - startEatingSoonTTCheckbox.setOnCheckedChangeListener(this); - break; - case R.id.newcarbs_hypo_tt: - if (togglingTT) { - togglingTT = false; - break; - } - startHypoTTCheckbox.setOnClickListener(null); - startHypoTTCheckbox.setOnCheckedChangeListener(null); - startHypoTTCheckbox.setChecked(false); - startHypoTTCheckbox.setOnCheckedChangeListener(this); - break; - } - } - - - - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - // Logic to disable a selected radio when pressed: when a checked radio - // is pressed, no CheckChanged event is triggered, so register a Click event - // when checking a radio. Since Click events come after CheckChanged events, - // the Click event is triggered immediately after this. Thus, set togglingTT - // var to true, so that the first Click event fired after this is ignored. - // Radios remove themselves from Click events once unchecked. - // Since radios are not in a group, their state is manually updated here. - switch (buttonView.getId()) { - case R.id.newcarbs_activity_tt: - togglingTT = true; - startActivityTTCheckbox.setOnClickListener(this); - - startEatingSoonTTCheckbox.setOnCheckedChangeListener(null); - startEatingSoonTTCheckbox.setChecked(false); - startEatingSoonTTCheckbox.setOnCheckedChangeListener(this); - - startHypoTTCheckbox.setOnCheckedChangeListener(null); - startHypoTTCheckbox.setChecked(false); - startHypoTTCheckbox.setOnCheckedChangeListener(this); - break; - case R.id.newcarbs_eating_soon_tt: - togglingTT = true; - startEatingSoonTTCheckbox.setOnClickListener(this); - - startActivityTTCheckbox.setOnCheckedChangeListener(null); - startActivityTTCheckbox.setChecked(false); - startActivityTTCheckbox.setOnCheckedChangeListener(this); - - startHypoTTCheckbox.setOnCheckedChangeListener(null); - startHypoTTCheckbox.setChecked(false); - startHypoTTCheckbox.setOnCheckedChangeListener(this); - break; - case R.id.newcarbs_hypo_tt: - togglingTT = true; - startHypoTTCheckbox.setOnClickListener(this); - - startActivityTTCheckbox.setOnCheckedChangeListener(null); - startActivityTTCheckbox.setChecked(false); - startActivityTTCheckbox.setOnCheckedChangeListener(this); - - startEatingSoonTTCheckbox.setOnCheckedChangeListener(null); - startEatingSoonTTCheckbox.setChecked(false); - startEatingSoonTTCheckbox.setOnCheckedChangeListener(this); - break; - } - } - - private void submit() { - if (okClicked) { - log.debug("guarding: ok already clicked"); - dismiss(); - return; - } - okClicked = true; - try { - final Profile currentProfile = ProfileFunctions.getInstance().getProfile(); - if (currentProfile == null) { - return; - } - - int carbs = editCarbs.getValue().intValue(); - Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(carbs)).value(); - - final String units = ProfileFunctions.getSystemUnits(); - DefaultValueHelper helper = new DefaultValueHelper(); - - int activityTTDuration = helper.determineActivityTTDuration(); - double activityTT = helper.determineActivityTT(); - - int eatingSoonTTDuration = helper.determineEatingSoonTTDuration(); - double eatingSoonTT = helper.determineEatingSoonTT(); - - int hypoTTDuration = helper.determineHypoTTDuration(); - double hypoTT = helper.determineHypoTT(); - - List actions = new LinkedList<>(); - - if (startActivityTTCheckbox.isChecked()) { - String unitLabel = "mg/dl"; - if (units.equals(Constants.MMOL)) { - unitLabel = "mmol/l"; - } - actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(activityTT) + " " + unitLabel + " (" + activityTTDuration + " min)"); - } - if (startEatingSoonTTCheckbox.isChecked()) { - if (units.equals(Constants.MMOL)) { - actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(eatingSoonTT) + " mmol/l (" + eatingSoonTTDuration + " min)"); - } else { - actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to0Decimal(eatingSoonTT) + " mg/dl (" + eatingSoonTTDuration + " min)"); - } - } - if (startHypoTTCheckbox.isChecked()) { - if (units.equals(Constants.MMOL)) { - actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(hypoTT) + " mmol/l (" + hypoTTDuration + " min)"); - } else { - actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to0Decimal(hypoTT) + " mg/dl (" + hypoTTDuration + " min)"); - } - } - - int timeOffset = editTime.getValue().intValue(); - final long time = now() + timeOffset * 1000 * 60; - if (timeOffset != 0) { - actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(time)); - } - int duration = editDuration.getValue().intValue(); - if (duration > 0) { - actions.add(MainApp.gs(R.string.duration) + ": " + duration + MainApp.gs(R.string.shorthour)); - } - if (carbs > 0) { - actions.add(MainApp.gs(R.string.carbs) + ": " + "" + carbsAfterConstraints + "g" + ""); - } - if (!carbsAfterConstraints.equals(carbs)) { - actions.add("" + MainApp.gs(R.string.carbsconstraintapplied) + ""); - } - final String notes = notesEdit.getText().toString(); - if (!notes.isEmpty()) { - actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes); - } - - final double finalActivityTT = activityTT; - final int finalActivityTTDuration = activityTTDuration; - final double finalEatigSoonTT = eatingSoonTT; - final int finalEatingSoonTTDuration = eatingSoonTTDuration; - final double finalHypoTT = hypoTT; - final int finalHypoTTDuration = hypoTTDuration; - - final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(MainApp.gs(R.string.confirmation)); - if (carbsAfterConstraints > 0 || startActivityTTCheckbox.isChecked() - || startEatingSoonTTCheckbox.isChecked() || startHypoTTCheckbox.isChecked()) { - builder.setMessage(Html.fromHtml(Joiner.on("
").join(actions))); - builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { - synchronized (builder) { - if (accepted) { - log.debug("guarding: already accepted"); - return; - } - accepted = true; - - if (startActivityTTCheckbox.isChecked()) { - TempTarget tempTarget = new TempTarget() - .date(System.currentTimeMillis()) - .duration(finalActivityTTDuration) - .reason(MainApp.gs(R.string.activity)) - .source(Source.USER) - .low(Profile.toMgdl(finalActivityTT, ProfileFunctions.getSystemUnits())) - .high(Profile.toMgdl(finalActivityTT, ProfileFunctions.getSystemUnits())); - TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); - } else if (startEatingSoonTTCheckbox.isChecked()) { - TempTarget tempTarget = new TempTarget() - .date(System.currentTimeMillis()) - .duration(finalEatingSoonTTDuration) - .reason(MainApp.gs(R.string.eatingsoon)) - .source(Source.USER) - .low(Profile.toMgdl(finalEatigSoonTT, ProfileFunctions.getSystemUnits())) - .high(Profile.toMgdl(finalEatigSoonTT, ProfileFunctions.getSystemUnits())); - TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); - } else if (startHypoTTCheckbox.isChecked()) { - TempTarget tempTarget = new TempTarget() - .date(System.currentTimeMillis()) - .duration(finalHypoTTDuration) - .reason(MainApp.gs(R.string.hypo)) - .source(Source.USER) - .low(Profile.toMgdl(finalHypoTT, ProfileFunctions.getSystemUnits())) - .high(Profile.toMgdl(finalHypoTT, ProfileFunctions.getSystemUnits())); - TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); - } - - if (carbsAfterConstraints > 0) { - if (duration == 0) { - CarbsGenerator.createCarb(carbsAfterConstraints, time, CareportalEvent.CARBCORRECTION, notes); - } else { - CarbsGenerator.generateCarbs(carbsAfterConstraints, time, duration, notes); - NSUpload.uploadEvent(CareportalEvent.NOTE, now() - 2000, MainApp.gs(R.string.generated_ecarbs_note, carbsAfterConstraints, duration, timeOffset)); - } - } - } - }); - } else { - builder.setMessage(MainApp.gs(R.string.no_action_selected)); - } - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - dismiss(); - } catch (Exception e) { - log.error("Unhandled exception", e); - } - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/NewInsulinDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/NewInsulinDialog.java deleted file mode 100644 index b708296776..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/NewInsulinDialog.java +++ /dev/null @@ -1,317 +0,0 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs; - -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import androidx.fragment.app.DialogFragment; -import androidx.appcompat.app.AlertDialog; -import android.text.Editable; -import android.text.Html; -import android.text.TextWatcher; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.view.Window; -import android.view.WindowManager; -import android.widget.Button; -import android.widget.CheckBox; -import android.widget.EditText; -import android.widget.LinearLayout; - -import com.google.common.base.Joiner; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.text.DecimalFormat; -import java.util.LinkedList; -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.Profile; -import info.nightscout.androidaps.db.CareportalEvent; -import info.nightscout.androidaps.db.Source; -import info.nightscout.androidaps.db.TempTarget; -import info.nightscout.androidaps.interfaces.Constraint; -import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -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.NumberPicker; -import info.nightscout.androidaps.utils.SP; -import info.nightscout.androidaps.utils.SafeParse; -import info.nightscout.androidaps.utils.T; -import info.nightscout.androidaps.utils.ToastUtils; - -import static info.nightscout.androidaps.utils.DateUtil.now; - -public class NewInsulinDialog extends DialogFragment implements OnClickListener { - private static Logger log = LoggerFactory.getLogger(NewInsulinDialog.class); - - private static final double PLUS1_DEFAULT = 0.5d; - private static final double PLUS2_DEFAULT = 1d; - private static final double PLUS3_DEFAULT = 2d; - - private CheckBox startEatingSoonTTCheckbox; - private CheckBox recordOnlyCheckbox; - - private LinearLayout editLayout; - private NumberPicker editTime; - private NumberPicker editInsulin; - private Double maxInsulin; - - private EditText notesEdit; - - //one shot guards - private boolean accepted; - private boolean okClicked; - - public NewInsulinDialog() { - } - - final private TextWatcher textWatcher = new TextWatcher() { - @Override - public void afterTextChanged(Editable s) { - validateInputs(); - } - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - }; - - private void validateInputs() { - int time = editTime.getValue().intValue(); - if (Math.abs(time) > 12 * 60) { - editTime.setValue(0d); - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.constraintapllied)); - } - Double insulin = editInsulin.getValue(); - if (insulin > maxInsulin) { - editInsulin.setValue(0d); - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.bolusconstraintapplied)); - } - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.overview_newinsulin_dialog, container, false); - - view.findViewById(R.id.ok).setOnClickListener(this); - view.findViewById(R.id.cancel).setOnClickListener(this); - - getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); - getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - - startEatingSoonTTCheckbox = view.findViewById(R.id.newinsulin_start_eating_soon_tt); - - recordOnlyCheckbox = view.findViewById(R.id.newinsulin_record_only); - recordOnlyCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> editLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE)); - - editLayout = view.findViewById(R.id.newinsulin_time_layout); - editLayout.setVisibility(View.GONE); - editTime = view.findViewById(R.id.newinsulin_time); - editTime.setParams(0d, -12 * 60d, 12 * 60d, 5d, new DecimalFormat("0"), false, view.findViewById(R.id.ok), textWatcher); - - maxInsulin = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); - - editInsulin = view.findViewById(R.id.newinsulin_amount); - editInsulin.setParams(0d, 0d, maxInsulin, ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription().bolusStep, DecimalFormatter.pumpSupportedBolusFormat(), false, view.findViewById(R.id.ok), textWatcher); - - Button plus1Button = view.findViewById(R.id.newinsulin_plus05); - plus1Button.setOnClickListener(this); - plus1Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT))); - Button plus2Button = view.findViewById(R.id.newinsulin_plus10); - plus2Button.setOnClickListener(this); - plus2Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT))); - Button plus3Button = view.findViewById(R.id.newinsulin_plus20); - plus3Button.setOnClickListener(this); - plus3Button.setText(toSignedString(SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_3), PLUS3_DEFAULT))); - - LinearLayout notesLayout = view.findViewById(R.id.newinsulin_notes_layout); - notesLayout.setVisibility(SP.getBoolean(R.string.key_show_notes_entry_dialogs, false) ? View.VISIBLE : View.GONE); - notesEdit = view.findViewById(R.id.newinsulin_notes); - - setCancelable(true); - getDialog().setCanceledOnTouchOutside(false); - if (savedInstanceState != null) { -// log.debug("savedInstanceState in onCreate is:" + savedInstanceState.toString()); - editInsulin.setValue(savedInstanceState.getDouble("editInsulin")); - editTime.setValue(savedInstanceState.getDouble("editTime")); - } - return view; - } - - private String toSignedString(double value) { - String formatted = DecimalFormatter.toPumpSupportedBolus(value); - return value > 0 ? "+" + formatted : formatted; - } - - @Override - public void onSaveInstanceState(Bundle insulinDialogState) { - insulinDialogState.putBoolean("startEatingSoonTTCheckbox", startEatingSoonTTCheckbox.isChecked()); - insulinDialogState.putBoolean("recordOnlyCheckbox", recordOnlyCheckbox.isChecked()); - insulinDialogState.putDouble("editTime", editTime.getValue()); - insulinDialogState.putDouble("editInsulin", editInsulin.getValue()); - insulinDialogState.putString("notesEdit", notesEdit.getText().toString()); - log.debug("Instance state saved:" + insulinDialogState.toString()); - super.onSaveInstanceState(insulinDialogState); - } - - @Override - public synchronized void onClick(View view) { - switch (view.getId()) { - case R.id.ok: - submit(); - break; - case R.id.cancel: - dismiss(); - break; - case R.id.newinsulin_plus05: - editInsulin.setValue(Math.max(0, editInsulin.getValue() - + SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_1), PLUS1_DEFAULT))); - validateInputs(); - break; - case R.id.newinsulin_plus10: - editInsulin.setValue(Math.max(0, editInsulin.getValue() - + SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_2), PLUS2_DEFAULT))); - validateInputs(); - break; - case R.id.newinsulin_plus20: - editInsulin.setValue(Math.max(0, editInsulin.getValue() - + SP.getDouble(MainApp.gs(R.string.key_insulin_button_increment_3), PLUS3_DEFAULT))); - validateInputs(); - break; - } - } - - private void submit() { - if (okClicked) { - log.debug("guarding: ok already clicked"); - dismiss(); - return; - } - okClicked = true; - - try { - final PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); - if (pump == null) - return; - - Double insulin = SafeParse.stringToDouble(editInsulin.getText()); - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); - - List actions = new LinkedList<>(); - if (insulin > 0) { - actions.add(MainApp.gs(R.string.bolus) + ": " + "" + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints) + "U" + ""); - if (recordOnlyCheckbox.isChecked()) { - actions.add("" + MainApp.gs(R.string.bolusrecordedonly) + ""); - } - } - - if (Math.abs(insulinAfterConstraints - insulin) > pump.getPumpDescription().pumpType.determineCorrectBolusStepSize(insulinAfterConstraints)) - actions.add(MainApp.gs(R.string.bolusconstraintappliedwarning, MainApp.gc(R.color.warning), insulin, insulinAfterConstraints)); - - int eatingSoonTTDuration = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration); - eatingSoonTTDuration = eatingSoonTTDuration > 0 ? eatingSoonTTDuration : Constants.defaultEatingSoonTTDuration; - double eatingSoonTT = SP.getDouble(R.string.key_eatingsoon_target, ProfileFunctions.getSystemUnits().equals(Constants.MMOL) ? Constants.defaultEatingSoonTTmmol : Constants.defaultEatingSoonTTmgdl); - eatingSoonTT = eatingSoonTT > 0 ? eatingSoonTT : ProfileFunctions.getSystemUnits().equals(Constants.MMOL) ? Constants.defaultEatingSoonTTmmol : Constants.defaultEatingSoonTTmgdl; - - if (startEatingSoonTTCheckbox.isChecked()) { - if (ProfileFunctions.getSystemUnits().equals(Constants.MMOL)) { - actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(eatingSoonTT) + " mmol/l (" + eatingSoonTTDuration + " min)"); - } else - actions.add(MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to0Decimal(eatingSoonTT) + " mg/dl (" + eatingSoonTTDuration + " min)"); - } - - int timeOffset = editTime.getValue().intValue(); - final long time = now() + T.mins(timeOffset).msecs(); - if (timeOffset != 0) { - actions.add(MainApp.gs(R.string.time) + ": " + DateUtil.dateAndTimeString(time)); - } - final String notes = notesEdit.getText().toString(); - if (!notes.isEmpty()) { - actions.add(MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + notes); - } - - final double finalInsulinAfterConstraints = insulinAfterConstraints; - final double finalEatigSoonTT = eatingSoonTT; - final int finalEatingSoonTTDuration = eatingSoonTTDuration; - - final Context context = getContext(); - final AlertDialog.Builder builder = new AlertDialog.Builder(context); - - builder.setTitle(MainApp.gs(R.string.confirmation)); - if (finalInsulinAfterConstraints > 0 || startEatingSoonTTCheckbox.isChecked()) { - builder.setMessage(Html.fromHtml(Joiner.on("
").join(actions))); - builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { - synchronized (builder) { - if (accepted) { - log.debug("guarding: already accepted"); - return; - } - accepted = true; - - if (startEatingSoonTTCheckbox.isChecked()) { - TempTarget tempTarget = new TempTarget() - .date(System.currentTimeMillis()) - .duration(finalEatingSoonTTDuration) - .reason(MainApp.gs(R.string.eatingsoon)) - .source(Source.USER) - .low(Profile.toMgdl(finalEatigSoonTT, ProfileFunctions.getSystemUnits())) - .high(Profile.toMgdl(finalEatigSoonTT, ProfileFunctions.getSystemUnits())); - TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); - } - - if (finalInsulinAfterConstraints > 0) { - DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); - detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS; - detailedBolusInfo.insulin = finalInsulinAfterConstraints; - detailedBolusInfo.context = context; - detailedBolusInfo.source = Source.USER; - detailedBolusInfo.notes = notes; - if (recordOnlyCheckbox.isChecked()) { - detailedBolusInfo.date = time; - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); - } else { - detailedBolusInfo.date = now(); - ConfigBuilderPlugin.getPlugin().getCommandQueue().bolus(detailedBolusInfo, 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.treatmentdeliveryerror)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - } - }); - } - } - } - }); - } else { - builder.setMessage(MainApp.gs(R.string.no_action_selected)); - } - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - dismiss(); - } catch (Exception e) { - log.error("Unhandled exception", e); - } - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/NewTreatmentDialog.java deleted file mode 100644 index 9f2f42b2d9..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/NewTreatmentDialog.java +++ /dev/null @@ -1,213 +0,0 @@ -package info.nightscout.androidaps.plugins.general.overview.dialogs; - -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.os.Bundle; -import androidx.fragment.app.DialogFragment; -import androidx.appcompat.app.AlertDialog; -import android.text.Editable; -import android.text.Html; -import android.text.TextWatcher; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.view.Window; -import android.view.WindowManager; -import android.widget.CheckBox; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.text.DecimalFormat; -import java.util.Objects; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.DetailedBolusInfo; -import info.nightscout.androidaps.db.CareportalEvent; -import info.nightscout.androidaps.db.Source; -import info.nightscout.androidaps.interfaces.Constraint; -import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; -import info.nightscout.androidaps.queue.Callback; -import info.nightscout.androidaps.utils.DecimalFormatter; -import info.nightscout.androidaps.utils.NumberPicker; -import info.nightscout.androidaps.utils.SafeParse; -import info.nightscout.androidaps.utils.ToastUtils; - -public class NewTreatmentDialog extends DialogFragment implements OnClickListener { - private static Logger log = LoggerFactory.getLogger(NewTreatmentDialog.class); - - private NumberPicker editCarbs; - private NumberPicker editInsulin; - - private Integer maxCarbs; - private Double maxInsulin; - - //one shot guards - private boolean accepted; - private boolean okClicked; - - private CheckBox recordOnlyCheckbox; - - public NewTreatmentDialog() { - } - - final private TextWatcher textWatcher = new TextWatcher() { - @Override - public void afterTextChanged(Editable s) { - } - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - validateInputs(); - } - }; - - private void validateInputs() { - Integer carbs = SafeParse.stringToInt(editCarbs.getText()); - if (carbs > maxCarbs) { - editCarbs.setValue(0d); - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.carbsconstraintapplied)); - } - Double insulin = SafeParse.stringToDouble(editInsulin.getText()); - if (insulin > maxInsulin) { - editInsulin.setValue(0d); - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.bolusconstraintapplied)); - } - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.overview_newtreatment_dialog, container, false); - - view.findViewById(R.id.ok).setOnClickListener(this); - view.findViewById(R.id.cancel).setOnClickListener(this); - - getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); - getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - - maxCarbs = MainApp.getConstraintChecker().getMaxCarbsAllowed().value(); - maxInsulin = MainApp.getConstraintChecker().getMaxBolusAllowed().value(); - - editCarbs = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_carbsamount); - editInsulin = (NumberPicker) view.findViewById(R.id.treatments_newtreatment_insulinamount); - - editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false, view.findViewById(R.id.ok), textWatcher); - editInsulin.setParams(0d, 0d, maxInsulin, ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription().bolusStep, DecimalFormatter.pumpSupportedBolusFormat(), false, view.findViewById(R.id.ok), textWatcher); - - recordOnlyCheckbox = (CheckBox) view.findViewById(R.id.newtreatment_record_only); - - setCancelable(true); - getDialog().setCanceledOnTouchOutside(false); - return view; - } - - @Override - public synchronized void onClick(View view) { - switch (view.getId()) { - case R.id.ok: - if (okClicked) { - log.debug("guarding: ok already clicked"); - dismiss(); - return; - } - okClicked = true; - - try { - final PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); - - if (pump == null) - return; - - Double insulin = SafeParse.stringToDouble(editInsulin.getText()); - final Integer carbs = SafeParse.stringToInt(editCarbs.getText()); - - String confirmMessage = MainApp.gs(R.string.entertreatmentquestion) + "
"; - - Double insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(insulin)).value(); - Integer carbsAfterConstraints = MainApp.getConstraintChecker().applyCarbsConstraints(new Constraint<>(carbs)).value(); - - if (insulin > 0) { - confirmMessage += MainApp.gs(R.string.bolus) + ": " + "" + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints) + "U" + ""; - if (recordOnlyCheckbox.isChecked()) { - confirmMessage += "
" + MainApp.gs(R.string.bolusrecordedonly) + ""; - } - if (Math.abs(insulinAfterConstraints - insulin) > pump.getPumpDescription().pumpType.determineCorrectBolusStepSize(insulinAfterConstraints) || !Objects.equals(carbsAfterConstraints, carbs)) - confirmMessage += "
" + MainApp.gs(R.string.bolusconstraintappliedwarning, MainApp.gc(R.color.warning), insulin, insulinAfterConstraints); - } - if (carbsAfterConstraints > 0) - confirmMessage += "
" + MainApp.gs(R.string.carbs) + ": " + "" + carbsAfterConstraints + "g" + ""; - - - final double finalInsulinAfterConstraints = insulinAfterConstraints; - final int finalCarbsAfterConstraints = carbsAfterConstraints; - - final Context context = getContext(); - final AlertDialog.Builder builder = new AlertDialog.Builder(context); - - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(Html.fromHtml(confirmMessage)); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - synchronized (builder) { - if (accepted) { - log.debug("guarding: already accepted"); - return; - } - accepted = true; - if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) { - DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); - if (finalInsulinAfterConstraints == 0) - detailedBolusInfo.eventType = CareportalEvent.CARBCORRECTION; - if (finalCarbsAfterConstraints == 0) - detailedBolusInfo.eventType = CareportalEvent.CORRECTIONBOLUS; - detailedBolusInfo.insulin = finalInsulinAfterConstraints; - detailedBolusInfo.carbs = finalCarbsAfterConstraints; - detailedBolusInfo.context = context; - detailedBolusInfo.source = Source.USER; - if (!(recordOnlyCheckbox.isChecked() && (detailedBolusInfo.insulin > 0 || ConfigBuilderPlugin.getPlugin().getActivePump().getPumpDescription().storesCarbInfo))) { - ConfigBuilderPlugin.getPlugin().getCommandQueue().bolus(detailedBolusInfo, 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.treatmentdeliveryerror)); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - MainApp.instance().startActivity(i); - } - } - }); - } else { - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false); - } - } - } - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - - dismiss(); - } catch (Exception e) { - log.error("Unhandled exception", e); - } - break; - case R.id.cancel: - dismiss(); - break; - } - - } - -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationRecyclerViewAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationRecyclerViewAdapter.java index 1e96b02be1..8d1664c7e4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationRecyclerViewAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationRecyclerViewAdapter.java @@ -21,7 +21,7 @@ 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.nsclient.broadcasts.BroadcastAckAlarm; +import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin; import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification; import info.nightscout.androidaps.utils.DateUtil; @@ -93,7 +93,7 @@ public class NotificationRecyclerViewAdapter extends RecyclerView.Adapter(session, "Login", { - OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Successfully logged into Tidepool.", null) + OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Successfully logged into Tidepool.") }, { - OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Failed to log into Tidepool.\nCheck that your user name and password are correct.", null) + OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Failed to log into Tidepool.\nCheck that your user name and password are correct.") })) } - ?: OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Cannot do login as user credentials have not been set correctly", null) + ?: OKDialog.show(rootContext, MainApp.gs(R.string.tidepool), "Cannot do login as user credentials have not been set correctly") } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt index 777afc7331..4478c025d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.profile.local - import android.os.Bundle import android.text.Editable import android.text.TextWatcher @@ -14,10 +13,9 @@ 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.dialogs.ProfileSwitchDialog 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.insulin.InsulinOrefBasePlugin.MIN_DIA import info.nightscout.androidaps.plugins.profile.local.events.EventLocalProfileChanged import info.nightscout.androidaps.utils.* @@ -108,7 +106,7 @@ class LocalProfileFragment : Fragment() { // Spinner spinner = SpinnerHelper(view?.findViewById(R.id.localprofile_spinner)) val profileList: ArrayList = LocalProfilePlugin.profile?.getProfileList() - ?: ArrayList() + ?: ArrayList() context?.let { context -> val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList) spinner?.adapter = adapter @@ -121,10 +119,10 @@ class LocalProfileFragment : Fragment() { override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { if (LocalProfilePlugin.isEdited) { activity?.let { activity -> - OKDialog.showConfirmation(activity, MainApp.gs(R.string.doyouwantswitchprofile), { + OKDialog.showConfirmation(activity, MainApp.gs(R.string.doyouwantswitchprofile), Runnable { LocalProfilePlugin.currentProfileIndex = position build() - }, { + }, Runnable { spinner?.setSelection(LocalProfilePlugin.currentProfileIndex) }) } @@ -137,7 +135,7 @@ class LocalProfileFragment : Fragment() { localprofile_profile_add.setOnClickListener { if (LocalProfilePlugin.isEdited) { - activity?.let { OKDialog.show(it, "", MainApp.gs(R.string.saveorresetchangesfirst), null) } + activity?.let { OKDialog.show(it, "", MainApp.gs(R.string.saveorresetchangesfirst)) } } else { LocalProfilePlugin.addNewProfile() build() @@ -146,7 +144,7 @@ class LocalProfileFragment : Fragment() { localprofile_profile_clone.setOnClickListener { if (LocalProfilePlugin.isEdited) { - activity?.let { OKDialog.show(it, "", MainApp.gs(R.string.saveorresetchangesfirst), null) } + activity?.let { OKDialog.show(it, "", MainApp.gs(R.string.saveorresetchangesfirst)) } } else { LocalProfilePlugin.cloneProfile() build() @@ -155,7 +153,7 @@ class LocalProfileFragment : Fragment() { localprofile_profile_remove.setOnClickListener { activity?.let { activity -> - OKDialog.showConfirmation(activity, MainApp.gs(R.string.deletecurrentprofile), { + OKDialog.showConfirmation(activity, MainApp.gs(R.string.deletecurrentprofile), Runnable { LocalProfilePlugin.removeCurrentProfile() build() }, null) @@ -170,11 +168,7 @@ class LocalProfileFragment : Fragment() { localprofile_profileswitch.setOnClickListener { // TODO: select in dialog LocalProfilePlugin.currentProfileIndex - val newDialog = NewNSTreatmentDialog() - val profileSwitch = CareportalFragment.PROFILESWITCHDIRECT - profileSwitch.executeProfileSwitch = true - newDialog.setOptions(profileSwitch, R.string.careportal_profileswitch) - fragmentManager?.let { newDialog.show(it, "NewNSTreatmentDialog") } + fragmentManager?.let { ProfileSwitchDialog().show(it, "NewNSTreatmentDialog") } } localprofile_reset.setOnClickListener { @@ -204,9 +198,9 @@ class LocalProfileFragment : Fragment() { override fun onResume() { super.onResume() disposable.add(RxBus - .toObservable(EventLocalProfileChanged::class.java) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ build() }, { FabricPrivacy.logException(it) }) + .toObservable(EventLocalProfileChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ build() }, { FabricPrivacy.logException(it) }) ) build() } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt index e2c8d051ac..a3e3819b51 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt @@ -27,11 +27,11 @@ import java.util.* import kotlin.collections.ArrayList object LocalProfilePlugin : PluginBase(PluginDescription() - .mainType(PluginType.PROFILE) - .fragmentClass(LocalProfileFragment::class.java.name) - .pluginName(R.string.localprofile) - .shortName(R.string.localprofile_shortname) - .description(R.string.description_profile_local)), ProfileInterface { + .mainType(PluginType.PROFILE) + .fragmentClass(LocalProfileFragment::class.java.name) + .pluginName(R.string.localprofile) + .shortName(R.string.localprofile_shortname) + .description(R.string.description_profile_local)), ProfileInterface { override fun onStart() { super.onStart() @@ -98,7 +98,7 @@ object LocalProfilePlugin : PluginBase(PluginDescription() @Synchronized fun isValidEditState(): Boolean { return createProfileStore().getDefaultProfile()?.isValid(MainApp.gs(R.string.localprofile), false) - ?: false + ?: false } @Synchronized @@ -132,7 +132,7 @@ object LocalProfilePlugin : PluginBase(PluginDescription() rawProfile?.let { NSUpload.uploadProfileStore(it.data) } else activity?.let { - OKDialog.show(it,"", MainApp.gs(R.string.profilenamecontainsdot), null) + OKDialog.show(it, "", MainApp.gs(R.string.profilenamecontainsdot)) } } @@ -423,7 +423,7 @@ object LocalProfilePlugin : PluginBase(PluginDescription() override fun getProfileName(): String { return DecimalFormatter.to2Decimal(rawProfile?.getDefaultProfile()?.percentageBasalSum() - ?: 0.0) + "U " + ?: 0.0) + "U " } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.kt index 7b377c9078..6c111faf3f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/ns/NSProfileFragment.kt @@ -12,6 +12,7 @@ import info.nightscout.androidaps.R import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions import info.nightscout.androidaps.plugins.profile.ns.events.EventNSProfileUpdateGUI +import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DecimalFormatter import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.OKDialog @@ -21,7 +22,6 @@ import kotlinx.android.synthetic.main.close.* import kotlinx.android.synthetic.main.nsprofile_fragment.* import kotlinx.android.synthetic.main.profileviewer_fragment.* - class NSProfileFragment : Fragment() { private var disposable: CompositeDisposable = CompositeDisposable() @@ -39,10 +39,11 @@ class NSProfileFragment : Fragment() { val name = nsprofile_spinner.selectedItem?.toString() ?: "" NSProfilePlugin.getPlugin().profile?.let { store -> store.getSpecificProfile(name)?.let { - OKDialog.showConfirmation(activity, - MainApp.gs(R.string.activate_profile) + ": " + name + " ?" - ) { - ProfileFunctions.doProfileSwitch(store, name, 0, 100, 0) + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.nsprofile), + MainApp.gs(R.string.activate_profile) + ": " + name + " ?", Runnable { + ProfileFunctions.doProfileSwitch(store, name, 0, 100, 0, DateUtil.now()) + }) } } } @@ -96,13 +97,9 @@ class NSProfileFragment : Fragment() { override fun onResume() { super.onResume() disposable.add(RxBus - .toObservable(EventNSProfileUpdateGUI::class.java) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ - updateGUI() - }, { - FabricPrivacy.logException(it) - }) + .toObservable(EventNSProfileUpdateGUI::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) ) updateGUI() } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/LocationHelper.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/LocationHelper.java index 98682679a7..065f0ec4d6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/LocationHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/LocationHelper.java @@ -1,14 +1,13 @@ package info.nightscout.androidaps.plugins.pump.common.utils; import android.app.Activity; -import android.app.AlertDialog; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.location.LocationManager; -import android.os.Build; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.utils.OKDialog; /** * Helper for checking if location services are enabled on the device. @@ -45,18 +44,9 @@ public class LocationHelper { } // Shamelessly borrowed from http://stackoverflow.com/a/10311877/868533 - - AlertDialog.Builder builder = new AlertDialog.Builder(parent); - builder.setTitle(R.string.location_not_found_title); - builder.setMessage(R.string.location_not_found_message); - builder.setPositiveButton(R.string.location_yes, new DialogInterface.OnClickListener() { - - public void onClick(DialogInterface dialogInterface, int i) { - parent.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); - } + OKDialog.showConfirmation(parent, MainApp.gs(R.string.location_not_found_title), MainApp.gs(R.string.location_not_found_message), () -> { + parent.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); }); - builder.setNegativeButton(R.string.location_no, null); - builder.create().show(); } 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 3f58acf4d2..af1e80602e 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 @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.pump.danaR - import android.content.Intent import android.os.Bundle import android.os.Handler @@ -11,6 +10,7 @@ import androidx.fragment.app.Fragment import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.activities.TDDStatsActivity +import info.nightscout.androidaps.dialogs.ProfileViewerDialog import info.nightscout.androidaps.events.EventExtendedBolusChange import info.nightscout.androidaps.events.EventPumpStatusChanged import info.nightscout.androidaps.events.EventTempBasalChange @@ -24,9 +24,12 @@ import info.nightscout.androidaps.plugins.pump.danaR.activities.DanaRUserOptions import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRNewStatus import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin -import info.nightscout.androidaps.plugins.treatments.fragments.ProfileViewerDialog import info.nightscout.androidaps.queue.events.EventQueueChanged -import info.nightscout.androidaps.utils.* +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.FabricPrivacy +import info.nightscout.androidaps.utils.SetWarnColor +import info.nightscout.androidaps.utils.T +import info.nightscout.androidaps.utils.plusAssign import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import kotlinx.android.synthetic.main.danar_fragment.* @@ -59,9 +62,16 @@ class DanaRFragment : Fragment() { danar_history.setOnClickListener { startActivity(Intent(context, DanaRHistoryActivity::class.java)) } danar_viewprofile.setOnClickListener { fragmentManager?.let { fragmentManager -> + val profile = DanaRPump.getInstance().createConvertedProfile()?.getDefaultProfile() + ?: return@let + val profileName = DanaRPump.getInstance().createConvertedProfile()?.getDefaultProfileName() + ?: return@let val args = Bundle() args.putLong("time", DateUtil.now()) - args.putInt("mode", ProfileViewerDialog.Mode.PUMP_PROFILE.ordinal) + args.putInt("mode", ProfileViewerDialog.Mode.CUSTOM_PROFILE.ordinal) + args.putString("customProfile", profile.data.toString()) + args.putString("customProfileUnits", profile.units) + args.putString("customProfileName", profileName) val pvd = ProfileViewerDialog() pvd.arguments = args pvd.show(fragmentManager, "ProfileViewDialog") @@ -82,37 +92,37 @@ class DanaRFragment : Fragment() { super.onResume() loopHandler.postDelayed(refreshLoop, T.mins(1).msecs()) disposable += RxBus - .toObservable(EventDanaRNewStatus::class.java) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ updateGUI() }, { FabricPrivacy.logException(it) }) + .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) }) + .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) }) + .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) }) + .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) }) + .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() } @@ -151,14 +161,14 @@ class DanaRFragment : Fragment() { // DanaRPlugin, DanaRKoreanPlugin if (ConfigBuilderPlugin.getPlugin().activePump!!.isFakingTempsByExtendedBoluses) { danar_tempbasal.text = TreatmentsPlugin.getPlugin() - .getRealTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: "" + .getRealTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: "" } else { // v2 plugin danar_tempbasal.text = TreatmentsPlugin.getPlugin() - .getTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: "" + .getTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: "" } danar_extendedbolus.text = TreatmentsPlugin.getPlugin() - .getExtendedBolusFromHistory(System.currentTimeMillis())?.toString() ?: "" + .getExtendedBolusFromHistory(System.currentTimeMillis())?.toString() ?: "" danar_reservoir.text = MainApp.gs(R.string.reservoirvalue, pump.reservoirRemainingUnits, 300) SetWarnColor.setColorInverse(danar_reservoir, pump.reservoirRemainingUnits, 50.0, 20.0) danar_battery.text = "{fa-battery-" + pump.batteryRemaining / 25 + "}" 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 deleted file mode 100644 index 97f029bd8c..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRHistoryActivity.java +++ /dev/null @@ -1,327 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.danaR.activities; - -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.Button; -import android.widget.Spinner; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.cardview.widget.CardView; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -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.activities.NoSplashAppCompatActivity; -import info.nightscout.androidaps.data.Profile; -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; -import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRSyncStatus; -import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin; -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 io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.disposables.CompositeDisposable; - -public class DanaRHistoryActivity extends NoSplashAppCompatActivity { - private static Logger log = LoggerFactory.getLogger(L.PUMP); - private CompositeDisposable disposable = new CompositeDisposable(); - - Spinner historyTypeSpinner; - TextView statusView; - Button reloadButton; - RecyclerView recyclerView; - LinearLayoutManager llm; - - static byte showingType = RecordTypes.RECORD_TYPE_ALARM; - List historyList = new ArrayList<>(); - - public static class TypeList { - public byte type; - String name; - - TypeList(byte type, String name) { - this.type = type; - this.name = name; - } - - @NonNull - @Override - public String toString() { - return name; - } - } - - public DanaRHistoryActivity() { - super(); - } - - - @Override - protected void onResume() { - super.onResume(); - 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(); - disposable.clear(); - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.danar_historyactivity); - - historyTypeSpinner = findViewById(R.id.danar_historytype); - statusView = findViewById(R.id.danar_historystatus); - reloadButton = findViewById(R.id.danar_historyreload); - recyclerView = findViewById(R.id.danar_history_recyclerview); - - recyclerView.setHasFixedSize(true); - llm = new LinearLayoutManager(this); - recyclerView.setLayoutManager(llm); - - RecyclerViewAdapter adapter = new RecyclerViewAdapter(historyList); - recyclerView.setAdapter(adapter); - - statusView.setVisibility(View.GONE); - - boolean isKorean = DanaRKoreanPlugin.getPlugin().isEnabled(PluginType.PUMP); - boolean isRS = DanaRSPlugin.getPlugin().isEnabled(PluginType.PUMP); - - // Types - - ArrayList typeList = new ArrayList<>(); - typeList.add(new TypeList(RecordTypes.RECORD_TYPE_ALARM, MainApp.gs(R.string.danar_history_alarm))); - typeList.add(new TypeList(RecordTypes.RECORD_TYPE_BASALHOUR, MainApp.gs(R.string.danar_history_basalhours))); - typeList.add(new TypeList(RecordTypes.RECORD_TYPE_BOLUS, MainApp.gs(R.string.danar_history_bolus))); - typeList.add(new TypeList(RecordTypes.RECORD_TYPE_CARBO, MainApp.gs(R.string.danar_history_carbohydrates))); - typeList.add(new TypeList(RecordTypes.RECORD_TYPE_DAILY, MainApp.gs(R.string.danar_history_dailyinsulin))); - typeList.add(new TypeList(RecordTypes.RECORD_TYPE_GLUCOSE, MainApp.gs(R.string.danar_history_glucose))); - if (!isKorean && !isRS) { - typeList.add(new TypeList(RecordTypes.RECORD_TYPE_ERROR, MainApp.gs(R.string.danar_history_errors))); - } - if (isRS) - typeList.add(new TypeList(RecordTypes.RECORD_TYPE_PRIME, MainApp.gs(R.string.danar_history_prime))); - if (!isKorean) { - typeList.add(new TypeList(RecordTypes.RECORD_TYPE_REFILL, MainApp.gs(R.string.danar_history_refill))); - typeList.add(new TypeList(RecordTypes.RECORD_TYPE_SUSPEND, MainApp.gs(R.string.danar_history_syspend))); - } - ArrayAdapter spinnerAdapter = new ArrayAdapter<>(this, - R.layout.spinner_centered, typeList); - historyTypeSpinner.setAdapter(spinnerAdapter); - - reloadButton.setOnClickListener(v -> { - final TypeList selected = (TypeList) historyTypeSpinner.getSelectedItem(); - runOnUiThread(() -> { - reloadButton.setVisibility(View.GONE); - statusView.setVisibility(View.VISIBLE); - }); - clearCardView(); - ConfigBuilderPlugin.getPlugin().getCommandQueue().loadHistory(selected.type, new Callback() { - @Override - public void run() { - loadDataFromDB(selected.type); - runOnUiThread(() -> { - reloadButton.setVisibility(View.VISIBLE); - statusView.setVisibility(View.GONE); - }); - } - }); - }); - - historyTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - TypeList selected = (TypeList) historyTypeSpinner.getSelectedItem(); - loadDataFromDB(selected.type); - showingType = selected.type; - } - - @Override - public void onNothingSelected(AdapterView parent) { - clearCardView(); - } - }); - } - - public static class RecyclerViewAdapter extends RecyclerView.Adapter { - - List historyList; - - RecyclerViewAdapter(List historyList) { - this.historyList = historyList; - } - - @NonNull - @Override - public HistoryViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) { - View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.danar_history_item, viewGroup, false); - return new HistoryViewHolder(v); - } - - @Override - public void onBindViewHolder(@NonNull HistoryViewHolder holder, int position) { - DanaRHistoryRecord record = historyList.get(position); - holder.time.setText(DateUtil.dateAndTimeString(record.recordDate)); - holder.value.setText(DecimalFormatter.to2Decimal(record.recordValue)); - holder.stringvalue.setText(record.stringRecordValue); - holder.bolustype.setText(record.bolusType); - holder.duration.setText(DecimalFormatter.to0Decimal(record.recordDuration)); - holder.alarm.setText(record.recordAlarm); - switch (showingType) { - case RecordTypes.RECORD_TYPE_ALARM: - holder.time.setVisibility(View.VISIBLE); - holder.value.setVisibility(View.VISIBLE); - holder.stringvalue.setVisibility(View.GONE); - holder.bolustype.setVisibility(View.GONE); - holder.duration.setVisibility(View.GONE); - holder.dailybasal.setVisibility(View.GONE); - holder.dailybolus.setVisibility(View.GONE); - holder.dailytotal.setVisibility(View.GONE); - holder.alarm.setVisibility(View.VISIBLE); - break; - case RecordTypes.RECORD_TYPE_BOLUS: - holder.time.setVisibility(View.VISIBLE); - holder.value.setVisibility(View.VISIBLE); - holder.stringvalue.setVisibility(View.GONE); - holder.bolustype.setVisibility(View.VISIBLE); - holder.duration.setVisibility(View.VISIBLE); - holder.dailybasal.setVisibility(View.GONE); - holder.dailybolus.setVisibility(View.GONE); - holder.dailytotal.setVisibility(View.GONE); - holder.alarm.setVisibility(View.GONE); - break; - case RecordTypes.RECORD_TYPE_DAILY: - holder.dailybasal.setText(DecimalFormatter.to2Decimal(record.recordDailyBasal) + "U"); - holder.dailybolus.setText(DecimalFormatter.to2Decimal(record.recordDailyBolus) + "U"); - holder.dailytotal.setText(DecimalFormatter.to2Decimal(record.recordDailyBolus + record.recordDailyBasal) + "U"); - holder.time.setText(DateUtil.dateString(record.recordDate)); - holder.time.setVisibility(View.VISIBLE); - holder.value.setVisibility(View.GONE); - holder.stringvalue.setVisibility(View.GONE); - holder.bolustype.setVisibility(View.GONE); - holder.duration.setVisibility(View.GONE); - holder.dailybasal.setVisibility(View.VISIBLE); - holder.dailybolus.setVisibility(View.VISIBLE); - holder.dailytotal.setVisibility(View.VISIBLE); - holder.alarm.setVisibility(View.GONE); - break; - case RecordTypes.RECORD_TYPE_GLUCOSE: - holder.value.setText(Profile.toUnitsString(record.recordValue, record.recordValue * Constants.MGDL_TO_MMOLL, ProfileFunctions.getSystemUnits())); - // rest is the same - case RecordTypes.RECORD_TYPE_CARBO: - case RecordTypes.RECORD_TYPE_BASALHOUR: - case RecordTypes.RECORD_TYPE_ERROR: - case RecordTypes.RECORD_TYPE_PRIME: - case RecordTypes.RECORD_TYPE_REFILL: - case RecordTypes.RECORD_TYPE_TB: - holder.time.setVisibility(View.VISIBLE); - holder.value.setVisibility(View.VISIBLE); - holder.stringvalue.setVisibility(View.GONE); - holder.bolustype.setVisibility(View.GONE); - holder.duration.setVisibility(View.GONE); - holder.dailybasal.setVisibility(View.GONE); - holder.dailybolus.setVisibility(View.GONE); - holder.dailytotal.setVisibility(View.GONE); - holder.alarm.setVisibility(View.GONE); - break; - case RecordTypes.RECORD_TYPE_SUSPEND: - holder.time.setVisibility(View.VISIBLE); - holder.value.setVisibility(View.GONE); - holder.stringvalue.setVisibility(View.VISIBLE); - holder.bolustype.setVisibility(View.GONE); - holder.duration.setVisibility(View.GONE); - holder.dailybasal.setVisibility(View.GONE); - holder.dailybolus.setVisibility(View.GONE); - holder.dailytotal.setVisibility(View.GONE); - holder.alarm.setVisibility(View.GONE); - break; - } - } - - @Override - public int getItemCount() { - return historyList.size(); - } - - @Override - public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { - super.onAttachedToRecyclerView(recyclerView); - } - - static class HistoryViewHolder extends RecyclerView.ViewHolder { - CardView cv; - TextView time; - TextView value; - TextView bolustype; - TextView stringvalue; - TextView duration; - TextView dailybasal; - TextView dailybolus; - TextView dailytotal; - TextView alarm; - - HistoryViewHolder(View itemView) { - super(itemView); - cv = itemView.findViewById(R.id.danar_history_cardview); - time = itemView.findViewById(R.id.danar_history_time); - value = itemView.findViewById(R.id.danar_history_value); - bolustype = itemView.findViewById(R.id.danar_history_bolustype); - stringvalue = itemView.findViewById(R.id.danar_history_stringvalue); - duration = itemView.findViewById(R.id.danar_history_duration); - dailybasal = itemView.findViewById(R.id.danar_history_dailybasal); - dailybolus = itemView.findViewById(R.id.danar_history_dailybolus); - dailytotal = itemView.findViewById(R.id.danar_history_dailytotal); - alarm = itemView.findViewById(R.id.danar_history_alarm); - } - } - } - - private void loadDataFromDB(byte type) { - historyList = MainApp.getDbHelper().getDanaRHistoryRecordsByType(type); - - runOnUiThread(() -> recyclerView.swapAdapter(new RecyclerViewAdapter(historyList), false)); - } - - private void clearCardView() { - historyList = new ArrayList<>(); - runOnUiThread(() -> recyclerView.swapAdapter(new RecyclerViewAdapter(historyList), false)); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRHistoryActivity.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRHistoryActivity.kt new file mode 100644 index 0000000000..361f8e716e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRHistoryActivity.kt @@ -0,0 +1,248 @@ +package info.nightscout.androidaps.plugins.pump.danaR.activities + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.AdapterView +import android.widget.ArrayAdapter +import android.widget.TextView +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.NoSplashAppCompatActivity +import info.nightscout.androidaps.data.Profile +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.toObservable +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin +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 +import info.nightscout.androidaps.plugins.pump.danaRKorean.DanaRKoreanPlugin +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 io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.CompositeDisposable +import kotlinx.android.synthetic.main.danar_historyactivity.* +import org.slf4j.LoggerFactory +import java.util.* + +class DanaRHistoryActivity : NoSplashAppCompatActivity() { + private val log = LoggerFactory.getLogger(L.PUMP) + private val disposable = CompositeDisposable() + + private var showingType = RecordTypes.RECORD_TYPE_ALARM + private var historyList: List = ArrayList() + + class TypeList internal constructor(var type: Byte, var name: String) { + override fun toString(): String = name + } + + override fun onResume() { + super.onResume() + disposable.add(toObservable(EventPumpStatusChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ danar_history_status.text = it.getStatus() }) { FabricPrivacy.logException(it) } + ) + disposable.add(toObservable(EventDanaRSyncStatus::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + if (L.isEnabled(L.PUMP)) + log.debug("EventDanaRSyncStatus: " + it.message) + danar_history_status.text = it.message + }) { FabricPrivacy.logException(it) } + ) + } + + override fun onPause() { + super.onPause() + disposable.clear() + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.danar_historyactivity) + + danar_history_recyclerview.setHasFixedSize(true) + danar_history_recyclerview.layoutManager = LinearLayoutManager(this) + danar_history_recyclerview.adapter = RecyclerViewAdapter(historyList) + danar_history_status.visibility = View.GONE + + val isKorean = DanaRKoreanPlugin.getPlugin().isEnabled(PluginType.PUMP) + val isRS = DanaRSPlugin.getPlugin().isEnabled(PluginType.PUMP) + + // Types + val typeList = ArrayList() + typeList.add(TypeList(RecordTypes.RECORD_TYPE_ALARM, MainApp.gs(R.string.danar_history_alarm))) + typeList.add(TypeList(RecordTypes.RECORD_TYPE_BASALHOUR, MainApp.gs(R.string.danar_history_basalhours))) + typeList.add(TypeList(RecordTypes.RECORD_TYPE_BOLUS, MainApp.gs(R.string.danar_history_bolus))) + typeList.add(TypeList(RecordTypes.RECORD_TYPE_CARBO, MainApp.gs(R.string.danar_history_carbohydrates))) + typeList.add(TypeList(RecordTypes.RECORD_TYPE_DAILY, MainApp.gs(R.string.danar_history_dailyinsulin))) + typeList.add(TypeList(RecordTypes.RECORD_TYPE_GLUCOSE, MainApp.gs(R.string.danar_history_glucose))) + if (!isKorean && !isRS) { + typeList.add(TypeList(RecordTypes.RECORD_TYPE_ERROR, MainApp.gs(R.string.danar_history_errors))) + } + if (isRS) typeList.add(TypeList(RecordTypes.RECORD_TYPE_PRIME, MainApp.gs(R.string.danar_history_prime))) + if (!isKorean) { + typeList.add(TypeList(RecordTypes.RECORD_TYPE_REFILL, MainApp.gs(R.string.danar_history_refill))) + typeList.add(TypeList(RecordTypes.RECORD_TYPE_SUSPEND, MainApp.gs(R.string.danar_history_syspend))) + } + danar_history_spinner.adapter = ArrayAdapter(this, R.layout.spinner_centered, typeList) + + danar_history_reload.setOnClickListener { + val selected = danar_history_spinner.selectedItem as TypeList + runOnUiThread { + danar_history_reload.visibility = View.GONE + danar_history_status.visibility = View.VISIBLE + } + clearCardView() + ConfigBuilderPlugin.getPlugin().commandQueue.loadHistory(selected.type, object : Callback() { + override fun run() { + loadDataFromDB(selected.type) + runOnUiThread { + danar_history_reload.visibility = View.VISIBLE + danar_history_status.visibility = View.GONE + } + } + }) + } + danar_history_spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onItemSelected(parent: AdapterView<*>?, view: View, position: Int, id: Long) { + val selected = danar_history_spinner.selectedItem as TypeList + loadDataFromDB(selected.type) + showingType = selected.type + } + + override fun onNothingSelected(parent: AdapterView<*>?) { + clearCardView() + } + } + } + + inner class RecyclerViewAdapter internal constructor(private var historyList: List) : RecyclerView.Adapter() { + override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): HistoryViewHolder = + HistoryViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.danar_history_item, viewGroup, false)) + + override fun onBindViewHolder(holder: HistoryViewHolder, position: Int) { + val record = historyList[position] + holder.time.text = DateUtil.dateAndTimeString(record.recordDate) + holder.value.text = DecimalFormatter.to2Decimal(record.recordValue) + holder.stringValue.text = record.stringRecordValue + holder.bolusType.text = record.bolusType + holder.duration.text = DecimalFormatter.to0Decimal(record.recordDuration.toDouble()) + holder.alarm.text = record.recordAlarm + when (showingType) { + RecordTypes.RECORD_TYPE_ALARM -> { + holder.time.visibility = View.VISIBLE + holder.value.visibility = View.VISIBLE + holder.stringValue.visibility = View.GONE + holder.bolusType.visibility = View.GONE + holder.duration.visibility = View.GONE + holder.dailyBasal.visibility = View.GONE + holder.dailyBolus.visibility = View.GONE + holder.dailyTotal.visibility = View.GONE + holder.alarm.visibility = View.VISIBLE + } + + RecordTypes.RECORD_TYPE_BOLUS -> { + holder.time.visibility = View.VISIBLE + holder.value.visibility = View.VISIBLE + holder.stringValue.visibility = View.GONE + holder.bolusType.visibility = View.VISIBLE + holder.duration.visibility = View.VISIBLE + holder.dailyBasal.visibility = View.GONE + holder.dailyBolus.visibility = View.GONE + holder.dailyTotal.visibility = View.GONE + holder.alarm.visibility = View.GONE + } + + RecordTypes.RECORD_TYPE_DAILY -> { + holder.dailyBasal.text = MainApp.gs(R.string.formatinsulinunits, record.recordDailyBasal) + holder.dailyBolus.text = MainApp.gs(R.string.formatinsulinunits, record.recordDailyBolus) + holder.dailyTotal.text = MainApp.gs(R.string.formatinsulinunits, record.recordDailyBolus + record.recordDailyBasal) + holder.time.text = DateUtil.dateString(record.recordDate) + holder.time.visibility = View.VISIBLE + holder.value.visibility = View.GONE + holder.stringValue.visibility = View.GONE + holder.bolusType.visibility = View.GONE + holder.duration.visibility = View.GONE + holder.dailyBasal.visibility = View.VISIBLE + holder.dailyBolus.visibility = View.VISIBLE + holder.dailyTotal.visibility = View.VISIBLE + holder.alarm.visibility = View.GONE + } + + RecordTypes.RECORD_TYPE_GLUCOSE -> { + holder.value.text = Profile.toUnitsString(record.recordValue, record.recordValue * Constants.MGDL_TO_MMOLL, ProfileFunctions.getSystemUnits()) + holder.time.visibility = View.VISIBLE + holder.value.visibility = View.VISIBLE + holder.stringValue.visibility = View.GONE + holder.bolusType.visibility = View.GONE + holder.duration.visibility = View.GONE + holder.dailyBasal.visibility = View.GONE + holder.dailyBolus.visibility = View.GONE + holder.dailyTotal.visibility = View.GONE + holder.alarm.visibility = View.GONE + } + + RecordTypes.RECORD_TYPE_CARBO, RecordTypes.RECORD_TYPE_BASALHOUR, RecordTypes.RECORD_TYPE_ERROR, RecordTypes.RECORD_TYPE_PRIME, RecordTypes.RECORD_TYPE_REFILL, RecordTypes.RECORD_TYPE_TB -> { + holder.time.visibility = View.VISIBLE + holder.value.visibility = View.VISIBLE + holder.stringValue.visibility = View.GONE + holder.bolusType.visibility = View.GONE + holder.duration.visibility = View.GONE + holder.dailyBasal.visibility = View.GONE + holder.dailyBolus.visibility = View.GONE + holder.dailyTotal.visibility = View.GONE + holder.alarm.visibility = View.GONE + } + + RecordTypes.RECORD_TYPE_SUSPEND -> { + holder.time.visibility = View.VISIBLE + holder.value.visibility = View.GONE + holder.stringValue.visibility = View.VISIBLE + holder.bolusType.visibility = View.GONE + holder.duration.visibility = View.GONE + holder.dailyBasal.visibility = View.GONE + holder.dailyBolus.visibility = View.GONE + holder.dailyTotal.visibility = View.GONE + holder.alarm.visibility = View.GONE + } + } + } + + override fun getItemCount(): Int { + return historyList.size + } + + inner class HistoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + var time: TextView = itemView.findViewById(R.id.danar_history_time) + var value: TextView = itemView.findViewById(R.id.danar_history_value) + var bolusType: TextView = itemView.findViewById(R.id.danar_history_bolustype) + var stringValue: TextView = itemView.findViewById(R.id.danar_history_stringvalue) + var duration: TextView = itemView.findViewById(R.id.danar_history_duration) + var dailyBasal: TextView = itemView.findViewById(R.id.danar_history_dailybasal) + var dailyBolus: TextView = itemView.findViewById(R.id.danar_history_dailybolus) + var dailyTotal: TextView = itemView.findViewById(R.id.danar_history_dailytotal) + var alarm: TextView = itemView.findViewById(R.id.danar_history_alarm) + } + } + + private fun loadDataFromDB(type: Byte) { + historyList = MainApp.getDbHelper().getDanaRHistoryRecordsByType(type) + runOnUiThread { danar_history_recyclerview?.swapAdapter(RecyclerViewAdapter(historyList), false) } + } + + private fun clearCardView() { + historyList = ArrayList() + runOnUiThread { danar_history_recyclerview?.swapAdapter(RecyclerViewAdapter(historyList), false) } + } +} \ No newline at end of file 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 deleted file mode 100644 index 0346b5320e..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.java +++ /dev/null @@ -1,213 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.danaR.activities; - -import android.os.Bundle; -import android.widget.Button; -import android.widget.RadioButton; -import android.widget.RadioGroup; -import android.widget.Switch; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.text.DecimalFormat; - -import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; -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. - */ - -public class DanaRUserOptionsActivity extends NoSplashAppCompatActivity { - private static Logger log = LoggerFactory.getLogger(L.PUMP); - private CompositeDisposable disposable = new CompositeDisposable(); - - Switch timeFormat; - Switch buttonScroll; - Switch beep; - RadioGroup pumpAlarm; - RadioButton pumpAlarmSound; - RadioButton pumpAlarmVibrate; - RadioButton pumpAlarmBoth; - Switch pumpUnits; - NumberPicker screenTimeout; - NumberPicker backlightTimeout; - NumberPicker shutdown; - NumberPicker lowReservoir; - Button saveToPumpButton; - // This is for Dana pumps only - boolean isRS = DanaRSPlugin.getPlugin().isEnabled(PluginType.PUMP); - boolean isDanaR = DanaRPlugin.getPlugin().isEnabled(PluginType.PUMP); - boolean isDanaRv2 = DanaRv2Plugin.getPlugin().isEnabled(PluginType.PUMP); - - @Override - protected synchronized void onResume() { - super.onResume(); - disposable.add(RxBus.INSTANCE - .toObservable(EventInitializationChanged.class) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(event -> setData(), FabricPrivacy::logException) - ); - } - - @Override - protected synchronized void onPause() { - disposable.clear(); - super.onPause(); - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.danar_user_options); - - timeFormat = (Switch) findViewById(R.id.danar_timeformat); - buttonScroll = (Switch) findViewById(R.id.danar_buttonscroll); - beep = (Switch) findViewById(R.id.danar_beep); - pumpAlarm = (RadioGroup) findViewById(R.id.danar_pumpalarm); - pumpAlarmSound = (RadioButton) findViewById(R.id.danar_pumpalarm_sound); - pumpAlarmVibrate = (RadioButton) findViewById(R.id.danar_pumpalarm_vibrate); - pumpAlarmBoth = (RadioButton) findViewById(R.id.danar_pumpalarm_both); - screenTimeout = (NumberPicker) findViewById(R.id.danar_screentimeout); - backlightTimeout = (NumberPicker) findViewById(R.id.danar_backlight); - pumpUnits = (Switch) findViewById(R.id.danar_units); - shutdown = (NumberPicker) findViewById(R.id.danar_shutdown); - lowReservoir = (NumberPicker) findViewById(R.id.danar_lowreservoir); - saveToPumpButton = (Button) findViewById(R.id.save_user_options); - - saveToPumpButton.setOnClickListener(v -> onSaveClick()); - - DanaRPump pump = DanaRPump.getInstance(); - //used for debugging - if (L.isEnabled(L.PUMP)) - log.debug("UserOptionsLoaded:" + (System.currentTimeMillis() - pump.lastConnection) / 1000 + " s ago" - + "\ntimeDisplayType:" + pump.timeDisplayType - + "\nbuttonScroll:" + pump.buttonScrollOnOff - + "\ntimeDisplayType:" + pump.timeDisplayType - + "\nlcdOnTimeSec:" + pump.lcdOnTimeSec - + "\nbacklight:" + pump.backlightOnTimeSec - + "\npumpUnits:" + pump.units - + "\nlowReservoir:" + pump.lowReservoirRate); - - screenTimeout.setParams((double) pump.lcdOnTimeSec, 5d, 240d, 5d, new DecimalFormat("1"), false, findViewById(R.id.ok)); - backlightTimeout.setParams((double) pump.backlightOnTimeSec, 1d, 60d, 1d, new DecimalFormat("1"), false, findViewById(R.id.ok)); - shutdown.setParams((double) pump.shutdownHour, 0d, 24d, 1d, new DecimalFormat("1"), true, findViewById(R.id.ok)); - lowReservoir.setParams((double) pump.lowReservoirRate, 10d, 60d, 10d, new DecimalFormat("10"), false, findViewById(R.id.ok)); - switch (pump.beepAndAlarm) { - case 0x01: - pumpAlarmSound.setChecked(true); - break; - case 0x02: - pumpAlarmVibrate.setChecked(true); - break; - case 0x11: - pumpAlarmBoth.setChecked(true); - break; - case 0x101: - pumpAlarmSound.setChecked(true); - beep.setChecked(true); - break; - case 0x110: - pumpAlarmVibrate.setChecked(true); - beep.setChecked(true); - break; - case 0x111: - pumpAlarmBoth.setChecked(true); - beep.setChecked(true); - break; - } - if (pump.lastSettingsRead == 0) - log.error("No settings loaded from pump!"); - else - setData(); - } - - public void setData() { - DanaRPump pump = DanaRPump.getInstance(); - // in DanaRS timeDisplay values are reversed - timeFormat.setChecked((!isRS && pump.timeDisplayType != 0) || (isRS && pump.timeDisplayType == 0)); - buttonScroll.setChecked(pump.buttonScrollOnOff != 0); - beep.setChecked(pump.beepAndAlarm > 4); - screenTimeout.setValue((double) pump.lcdOnTimeSec); - backlightTimeout.setValue((double) pump.backlightOnTimeSec); - pumpUnits.setChecked(pump.getUnits() != null && pump.getUnits().equals(Constants.MMOL)); - shutdown.setValue((double) pump.shutdownHour); - lowReservoir.setValue((double) pump.lowReservoirRate); - } - - public void onSaveClick() { - if (!isRS && !isDanaR && !isDanaRv2) { - //exit if pump is not DanaRS, Dana!, or DanaR with upgraded firmware - return; - } - DanaRPump pump = DanaRPump.getInstance(); - if (timeFormat.isChecked()) - pump.timeDisplayType = 1; - else - pump.timeDisplayType = 0; - // displayTime on RS is reversed - if (isRS) { - if (timeFormat.isChecked()) - pump.timeDisplayType = 0; - else - pump.timeDisplayType = 1; - } - if (buttonScroll.isChecked()) - pump.buttonScrollOnOff = 1; - else - pump.buttonScrollOnOff = 0; - - pump.beepAndAlarm = 1; // default - if (pumpAlarmSound.isChecked()) pump.beepAndAlarm = 1; - else if (pumpAlarmVibrate.isChecked()) pump.beepAndAlarm = 2; - else if (pumpAlarmBoth.isChecked()) pump.beepAndAlarm = 3; - if (beep.isChecked()) pump.beepAndAlarm += 4; - - - // step is 5 seconds - int screenTimeoutValue = !screenTimeout.getText().isEmpty() ? (Integer.parseInt(screenTimeout.getText().toString()) / 5) * 5 : 5; - if (screenTimeoutValue > 4 && screenTimeoutValue < 241) { - pump.lcdOnTimeSec = screenTimeoutValue; - } else { - pump.lcdOnTimeSec = 5; - } - int backlightTimeoutValue = !backlightTimeout.getText().isEmpty() ? Integer.parseInt(backlightTimeout.getText().toString()) : 1; - if (backlightTimeoutValue > 0 && backlightTimeoutValue < 61) { - pump.backlightOnTimeSec = backlightTimeoutValue; - } - if (pumpUnits.isChecked()) { - pump.units = 1; - } else { - pump.units = 0; - } - int shutDownValue = !shutdown.getText().isEmpty() ? Integer.parseInt(shutdown.getText().toString()) : 0; - if (shutDownValue > -1 && shutDownValue < 25) { - pump.shutdownHour = shutDownValue; - } else { - pump.shutdownHour = 0; - } - int lowReservoirValue = !lowReservoir.getText().isEmpty() ? (Integer.parseInt(lowReservoir.getText().toString()) * 10) / 10 : 10; - if (lowReservoirValue > 9 && lowReservoirValue < 51) { - pump.lowReservoirRate = lowReservoirValue; - } else - pump.lowReservoirRate = 10; - - ConfigBuilderPlugin.getPlugin().getCommandQueue().setUserOptions(null); - finish(); - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.kt new file mode 100644 index 0000000000..a3a6c3c083 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.kt @@ -0,0 +1,159 @@ +package info.nightscout.androidaps.plugins.pump.danaR.activities + +import android.content.Intent +import android.os.Bundle +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.ErrorHelperActivity +import info.nightscout.androidaps.activities.NoSplashAppCompatActivity +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.toObservable +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.queue.Callback +import info.nightscout.androidaps.utils.FabricPrivacy +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.CompositeDisposable +import kotlinx.android.synthetic.main.danar_user_options_activity.* +import org.slf4j.LoggerFactory +import java.text.DecimalFormat +import kotlin.math.max +import kotlin.math.min + +class DanaRUserOptionsActivity : NoSplashAppCompatActivity() { + + private val log = LoggerFactory.getLogger(L.PUMP) + + private val disposable = CompositeDisposable() + + // This is for Dana pumps only + private var isRS = DanaRSPlugin.getPlugin().isEnabled(PluginType.PUMP) + private var isDanaR = DanaRPlugin.getPlugin().isEnabled(PluginType.PUMP) + private var isDanaRv2 = DanaRv2Plugin.getPlugin().isEnabled(PluginType.PUMP) + + @Synchronized + override fun onResume() { + super.onResume() + disposable.add(toObservable(EventInitializationChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ setData() }) { FabricPrivacy.logException(it) } + ) + } + + @Synchronized + override fun onPause() { + disposable.clear() + super.onPause() + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.danar_user_options_activity) + + save_user_options.setOnClickListener { onSaveClick() } + val pump = DanaRPump.getInstance() + + if (L.isEnabled(L.PUMP)) + log.debug("UserOptionsLoaded:" + (System.currentTimeMillis() - pump.lastConnection) / 1000 + " s ago" + + "\ntimeDisplayType:" + pump.timeDisplayType + + "\nbuttonScroll:" + pump.buttonScrollOnOff + + "\ntimeDisplayType:" + pump.timeDisplayType + + "\nlcdOnTimeSec:" + pump.lcdOnTimeSec + + "\nbackLight:" + pump.backlightOnTimeSec + + "\npumpUnits:" + pump.units + + "\nlowReservoir:" + pump.lowReservoirRate) + + danar_screentimeout.setParams(pump.lcdOnTimeSec.toDouble(), 5.0, 240.0, 5.0, DecimalFormat("1"), false, save_user_options) + danar_backlight.setParams(pump.backlightOnTimeSec.toDouble(), 1.0, 60.0, 1.0, DecimalFormat("1"), false, save_user_options) + danar_shutdown.setParams(pump.shutdownHour.toDouble(), 0.0, 24.0, 1.0, DecimalFormat("1"), true, save_user_options) + danar_lowreservoir.setParams(pump.lowReservoirRate.toDouble(), 10.0, 60.0, 10.0, DecimalFormat("10"), false, save_user_options) + when (pump.beepAndAlarm) { + 0x01 -> danar_pumpalarm_sound.isChecked = true + 0x02 -> danar_pumpalarm_vibrate.isChecked = true + 0x11 -> danar_pumpalarm_both.isChecked = true + + 0x101 -> { + danar_pumpalarm_sound.isChecked = true + danar_beep.isChecked = true + } + + 0x110 -> { + danar_pumpalarm_vibrate.isChecked = true + danar_beep.isChecked = true + } + + 0x111 -> { + danar_pumpalarm_both.isChecked = true + danar_beep.isChecked = true + } + } + if (pump.lastSettingsRead == 0L) + log.error("No settings loaded from pump!") else setData() + } + + fun setData() { + val pump = DanaRPump.getInstance() + // in DanaRS timeDisplay values are reversed + danar_timeformat.isChecked = !isRS && pump.timeDisplayType != 0 || isRS && pump.timeDisplayType == 0 + danar_buttonscroll.isChecked = pump.buttonScrollOnOff != 0 + danar_beep.isChecked = pump.beepAndAlarm > 4 + danar_screentimeout.value = pump.lcdOnTimeSec.toDouble() + danar_backlight.value = pump.backlightOnTimeSec.toDouble() + danar_units.isChecked = pump.getUnits() == Constants.MMOL + danar_shutdown.value = pump.shutdownHour.toDouble() + danar_lowreservoir.value = pump.lowReservoirRate.toDouble() + } + + private fun onSaveClick() { + //exit if pump is not DanaRS, DanaR, or DanaR with upgraded firmware + if (!isRS && !isDanaR && !isDanaRv2) return + + val pump = DanaRPump.getInstance() + + if (isRS) // displayTime on RS is reversed + pump.timeDisplayType = if (danar_timeformat.isChecked) 0 else 1 + else + pump.timeDisplayType = if (danar_timeformat.isChecked) 1 else 0 + + pump.buttonScrollOnOff = if (danar_buttonscroll.isChecked) 1 else 0 + pump.beepAndAlarm = when { + danar_pumpalarm_sound.isChecked -> 1 + danar_pumpalarm_vibrate.isChecked -> 2 + danar_pumpalarm_both.isChecked -> 3 + else -> 1 + } + if (danar_beep.isChecked) pump.beepAndAlarm += 4 + + // step is 5 seconds, 5 to 240 + pump.lcdOnTimeSec = min(max(danar_screentimeout.value.toInt() / 5 * 5, 5), 240) + // 1 to 60 + pump.backlightOnTimeSec = min(max(danar_backlight.value.toInt(), 1), 60) + + pump.units = if (danar_units.isChecked) 1 else 0 + + pump.shutdownHour = min(danar_shutdown.value.toInt(),24) + + // 10 to 50 + pump.lowReservoirRate = min(max(danar_lowreservoir.value.toInt() * 10 / 10, 10), 50) + + ConfigBuilderPlugin.getPlugin().commandQueue.setUserOptions(object : Callback() { + override fun run() { + if (!result.success) { + val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) + i.putExtra("soundid", R.raw.boluserror) + i.putExtra("status", result.comment) + i.putExtra("title", MainApp.gs(R.string.pumperror)) + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + MainApp.instance().startActivity(i) + } + } + }) + finish() + } +} \ No newline at end of file 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 dba3b4ca77..848abd9b52 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 @@ -24,7 +24,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.BolusProgressDialog; +import info.nightscout.androidaps.dialogs.BolusProgressDialog; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; 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 bb1f226730..0032a7e5e0 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 @@ -24,7 +24,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.BolusProgressDialog; +import info.nightscout.androidaps.dialogs.BolusProgressDialog; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump; 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 42e910f291..87c904212c 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 @@ -5,6 +5,7 @@ import android.bluetooth.BluetoothDevice; import android.bluetooth.le.BluetoothLeScanner; import android.bluetooth.le.ScanCallback; import android.bluetooth.le.ScanResult; +import android.content.pm.ActivityInfo; import android.os.Bundle; import android.os.Handler; import android.view.View; @@ -24,22 +25,20 @@ import info.nightscout.androidaps.plugins.pump.danaRS.events.EventDanaRSDeviceCh import info.nightscout.androidaps.utils.SP; public class BLEScanActivity extends NoSplashAppCompatActivity { - private ListView listView = null; private ListAdapter mListAdapter = null; private ArrayList mDevices = new ArrayList<>(); - private BluetoothAdapter mBluetoothAdapter = null; private BluetoothLeScanner mBluetoothLeScanner = null; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.danars_blescanner_activity); + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); mListAdapter = new ListAdapter(); - listView = (ListView) findViewById(R.id.danars_blescanner_listview); + ListView listView = findViewById(R.id.danars_blescanner_listview); listView.setEmptyView(findViewById(R.id.danars_blescanner_nodevice)); listView.setAdapter(mListAdapter); @@ -50,8 +49,9 @@ public class BLEScanActivity extends NoSplashAppCompatActivity { protected void onResume() { super.onResume(); - mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter != null) { + if (!mBluetoothAdapter.isEnabled()) mBluetoothAdapter.enable(); mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (mBluetoothLeScanner == null) { @@ -89,11 +89,7 @@ public class BLEScanActivity extends NoSplashAppCompatActivity { } mDevices.add(item); - new Handler().post(new Runnable() { - public void run() { - mListAdapter.notifyDataSetChanged(); - } - }); + new Handler().post(() -> mListAdapter.notifyDataSetChanged()); } private ScanCallback mBleScanCallback = new ScanCallback() { @@ -134,19 +130,19 @@ public class BLEScanActivity extends NoSplashAppCompatActivity { } BluetoothDeviceItem item = getItem(i); - holder.setData(i, item); + holder.setData(item); return v; } private class ViewHolder implements View.OnClickListener { private BluetoothDeviceItem item = null; - private TextView mName = null; - private TextView mAddress = null; + private TextView mName; + private TextView mAddress; - public ViewHolder(View v) { - mName = (TextView) v.findViewById(R.id.ble_name); - mAddress = (TextView) v.findViewById(R.id.ble_address); + ViewHolder(View v) { + mName = v.findViewById(R.id.ble_name); + mAddress = v.findViewById(R.id.ble_address); v.setOnClickListener(ViewHolder.this); } @@ -160,7 +156,7 @@ public class BLEScanActivity extends NoSplashAppCompatActivity { finish(); } - public void setData(int pos, BluetoothDeviceItem data) { + public void setData(BluetoothDeviceItem data) { if (data != null) { try { String tTitle = data.device.getName(); @@ -174,7 +170,7 @@ public class BLEScanActivity extends NoSplashAppCompatActivity { mAddress.setText(data.device.getAddress()); item = data; - } catch (Exception e) { + } catch (Exception ignored) { } } } @@ -185,14 +181,14 @@ public class BLEScanActivity extends NoSplashAppCompatActivity { private class BluetoothDeviceItem { private BluetoothDevice device; - public BluetoothDeviceItem(BluetoothDevice device) { + BluetoothDeviceItem(BluetoothDevice device) { super(); this.device = device; } @Override public boolean equals(Object o) { - if (device == null || o == null || !(o instanceof BluetoothDeviceItem)) { + if (device == null || !(o instanceof BluetoothDeviceItem)) { return false; } BluetoothDeviceItem checkItem = (BluetoothDeviceItem) o; @@ -202,7 +198,7 @@ public class BLEScanActivity extends NoSplashAppCompatActivity { return stringEquals(device.getAddress(), checkItem.device.getAddress()); } - public boolean stringEquals(String arg1, String arg2) { + boolean stringEquals(String arg1, String arg2) { try { return arg1.equals(arg2); } catch (Exception e) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingHelperActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingHelperActivity.java deleted file mode 100644 index 5be6d7fbd1..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingHelperActivity.java +++ /dev/null @@ -1,16 +0,0 @@ -package info.nightscout.androidaps.plugins.pump.danaRS.activities; - -import android.os.Bundle; - -import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; - -public class PairingHelperActivity extends NoSplashAppCompatActivity { - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - PairingProgressDialog bolusProgressDialog = new PairingProgressDialog(); - bolusProgressDialog.setHelperActivity(this); - bolusProgressDialog.show(this.getSupportFragmentManager(), "PairingProgress"); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingHelperActivity.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingHelperActivity.kt new file mode 100644 index 0000000000..ba73e1b368 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingHelperActivity.kt @@ -0,0 +1,16 @@ +package info.nightscout.androidaps.plugins.pump.danaRS.activities + +import android.content.pm.ActivityInfo +import android.os.Bundle +import info.nightscout.androidaps.activities.NoSplashAppCompatActivity +import info.nightscout.androidaps.plugins.pump.danaRS.dialogs.PairingProgressDialog + +class PairingHelperActivity : NoSplashAppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + PairingProgressDialog() + .setHelperActivity(this) + .show(supportFragmentManager, "PairingProgress") + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; + } +} \ No newline at end of file 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/dialogs/PairingProgressDialog.java similarity index 75% rename from app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingProgressDialog.java rename to app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/dialogs/PairingProgressDialog.java index afb5e86387..bad9e98112 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/dialogs/PairingProgressDialog.java @@ -1,4 +1,4 @@ -package info.nightscout.androidaps.plugins.pump.danaRS.activities; +package info.nightscout.androidaps.plugins.pump.danaRS.dialogs; import android.app.Activity; @@ -8,6 +8,8 @@ import android.os.HandlerThread; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; @@ -17,23 +19,22 @@ 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.activities.PairingHelperActivity; 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 { +public class PairingProgressDialog extends DialogFragment { private CompositeDisposable disposable = new CompositeDisposable(); - TextView statusView; - ProgressBar progressBar; - Button button; - PairingHelperActivity helperActivity; + private TextView statusView; + private ProgressBar progressBar; + private Button button; + private PairingHelperActivity helperActivity; - static int secondsPassed = 0; - public static boolean pairingEnded = false; - public static boolean running = true; + private static boolean pairingEnded = false; private static Handler sHandler; private static HandlerThread sHandlerThread; @@ -46,7 +47,6 @@ public class PairingProgressDialog extends DialogFragment implements View.OnClic sHandlerThread.start(); sHandler = new Handler(sHandlerThread.getLooper()); } - secondsPassed = 0; } @@ -54,17 +54,21 @@ public class PairingProgressDialog extends DialogFragment implements View.OnClic public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.danars_pairingprogressdialog, container, false); - getDialog().setTitle(MainApp.gs(R.string.pairing)); - statusView = (TextView) view.findViewById(R.id.danars_pairingprogress_status); - progressBar = (ProgressBar) view.findViewById(R.id.danars_pairingprogress_progressbar); - button = (Button) view.findViewById(R.id.ok); + + getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); + getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + setCancelable(false); + getDialog().setCanceledOnTouchOutside(false); + + statusView = view.findViewById(R.id.danars_pairingprogress_status); + progressBar = view.findViewById(R.id.danars_pairingprogress_progressbar); + button = view.findViewById(R.id.ok); progressBar.setMax(100); progressBar.setProgress(0); statusView.setText(MainApp.gs(R.string.waitingforpairing)); button.setVisibility(View.GONE); - button.setOnClickListener(this); - setCancelable(false); + button.setOnClickListener(v -> dismiss()); sHandler.post(() -> { for (int i = 0; i < 20; i++) { @@ -110,8 +114,8 @@ public class PairingProgressDialog extends DialogFragment implements View.OnClic .observeOn(Schedulers.io()) .subscribe(event -> pairingEnded = true, FabricPrivacy::logException) ); - running = true; if (pairingEnded) dismiss(); + getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } @Override @@ -126,16 +130,10 @@ public class PairingProgressDialog extends DialogFragment implements View.OnClic public void onPause() { super.onPause(); disposable.clear(); - running = false; } - public void setHelperActivity(PairingHelperActivity activity) { + public PairingProgressDialog setHelperActivity(PairingHelperActivity activity) { this.helperActivity = activity; - } - - @Override - public void onClick(View v) { - running = false; - dismiss(); + return this; } } 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 2850f08c8d..4e56133222 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 @@ -26,8 +26,8 @@ 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.dialogs.BolusProgressDialog; +import info.nightscout.androidaps.activities.ErrorHelperActivity; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; @@ -181,6 +181,20 @@ public class DanaRSService extends Service { RxBus.INSTANCE.send(new EventInitializationChanged()); return; } + long now = System.currentTimeMillis(); + if (danaRPump.lastSettingsRead + 60 * 60 * 1000L < now || !pump.isInitialized()) { + 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()); + bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_Bolus_Option()); // isExtendedEnabled + bleComm.sendMessage(new DanaRS_Packet_Basal_Get_Basal_Rate()); // basal profile, basalStep, maxBasal + bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_Calculation_Information()); // target + bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_CIR_CF_Array()); + bleComm.sendMessage(new DanaRS_Packet_Option_Get_User_Option()); // Getting user options + danaRPump.lastSettingsRead = now; + } + if (L.isEnabled(L.PUMPCOMM)) log.debug("Pump time difference: " + timeDiff + " seconds"); if (Math.abs(timeDiff) > 3) { @@ -201,9 +215,13 @@ public class DanaRSService extends Service { RxBus.INSTANCE.send(new EventInitializationChanged()); return; } else { - waitForWholeMinute(); // Dana can set only whole minute - // add 10sec to be sure we are over minute (will be cutted off anyway) - bleComm.sendMessage(new DanaRS_Packet_Option_Set_Pump_Time(new Date(DateUtil.now() + T.secs(10).msecs()))); + if (danaRPump.protocol >= 6) { + bleComm.sendMessage(new DanaRS_Packet_Option_Set_Pump_Time(new Date())); + } else { + waitForWholeMinute(); // Dana can set only whole minute + // add 10sec to be sure we are over minute (will be cutted off anyway) + bleComm.sendMessage(new DanaRS_Packet_Option_Set_Pump_Time(new Date(DateUtil.now() + T.secs(10).msecs()))); + } bleComm.sendMessage(new DanaRS_Packet_Option_Get_Pump_Time()); timeDiff = (danaRPump.pumpTime - System.currentTimeMillis()) / 1000L; if (L.isEnabled(L.PUMPCOMM)) @@ -211,20 +229,6 @@ public class DanaRSService extends Service { } } - long now = System.currentTimeMillis(); - if (danaRPump.lastSettingsRead + 60 * 60 * 1000L < now || !pump.isInitialized()) { - 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()); - bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_Bolus_Option()); // isExtendedEnabled - bleComm.sendMessage(new DanaRS_Packet_Basal_Get_Basal_Rate()); // basal profile, basalStep, maxBasal - bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_Calculation_Information()); // target - bleComm.sendMessage(new DanaRS_Packet_Bolus_Get_CIR_CF_Array()); - bleComm.sendMessage(new DanaRS_Packet_Option_Get_User_Option()); // Getting user options - danaRPump.lastSettingsRead = now; - } - loadEvents(); RxBus.INSTANCE.send(new EventDanaRNewStatus()); 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 e528a67fae..ef254580f9 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 @@ -25,8 +25,8 @@ 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.dialogs.BolusProgressDialog; +import info.nightscout.androidaps.activities.ErrorHelperActivity; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; 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 f0d10e0370..48780e2343 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 @@ -44,11 +44,10 @@ import info.nightscout.androidaps.plugins.common.ManufacturerType; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.general.actions.defs.CustomAction; import info.nightscout.androidaps.plugins.general.actions.defs.CustomActionType; -import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperActivity; +import info.nightscout.androidaps.activities.ErrorHelperActivity; 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; 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 1d72ed2e72..b67cadfed4 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,6 +1,5 @@ package info.nightscout.androidaps.plugins.source; -import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; import android.view.LayoutInflater; @@ -9,7 +8,6 @@ import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -25,6 +23,7 @@ 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.OKDialog; import info.nightscout.androidaps.utils.T; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; @@ -35,17 +34,17 @@ import io.reactivex.disposables.CompositeDisposable; public class BGSourceFragment extends Fragment { private CompositeDisposable disposable = new CompositeDisposable(); - RecyclerView recyclerView; + private RecyclerView recyclerView; - final long MILLS_TO_THE_PAST = T.hours(12).msecs(); + private final long MILLS_TO_THE_PAST = T.hours(12).msecs(); @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { try { View view = inflater.inflate(R.layout.bgsource_fragment, container, false); - recyclerView = (RecyclerView) view.findViewById(R.id.bgsource_recyclerview); + recyclerView = view.findViewById(R.id.bgsource_recyclerview); recyclerView.setHasFixedSize(true); LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); recyclerView.setLayoutManager(llm); @@ -91,6 +90,7 @@ public class BGSourceFragment extends Fragment { this.bgReadings = bgReadings; } + @NonNull @Override public BgReadingsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.bgsource_item, viewGroup, false); @@ -113,7 +113,7 @@ public class BGSourceFragment extends Fragment { return bgReadings.size(); } - class BgReadingsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + class BgReadingsViewHolder extends RecyclerView.ViewHolder { TextView date; TextView value; TextView direction; @@ -123,46 +123,22 @@ public class BGSourceFragment extends Fragment { BgReadingsViewHolder(View itemView) { super(itemView); - date = (TextView) itemView.findViewById(R.id.bgsource_date); - value = (TextView) itemView.findViewById(R.id.bgsource_value); - direction = (TextView) itemView.findViewById(R.id.bgsource_direction); - invalid = (TextView) itemView.findViewById(R.id.invalid_sign); - ns = (TextView) itemView.findViewById(R.id.ns_sign); - remove = (TextView) itemView.findViewById(R.id.bgsource_remove); - remove.setOnClickListener(this); + date = itemView.findViewById(R.id.bgsource_date); + value = itemView.findViewById(R.id.bgsource_value); + direction = itemView.findViewById(R.id.bgsource_direction); + invalid = itemView.findViewById(R.id.invalid_sign); + ns = itemView.findViewById(R.id.ns_sign); + remove = itemView.findViewById(R.id.bgsource_remove); + remove.setOnClickListener(v -> { + final BgReading bgReading = (BgReading) v.getTag(); + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(bgReading.date) + "\n" + bgReading.valueToUnitsToString(ProfileFunctions.getSystemUnits()), () -> { + bgReading.isValid = false; + MainApp.getDbHelper().update(bgReading); + updateGUI(); + }); + }); remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); } - - @Override - public void onClick(View v) { - final BgReading bgReading = (BgReading) v.getTag(); - switch (v.getId()) { - - case R.id.bgsource_remove: - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(bgReading.date) + "\n" + bgReading.valueToUnitsToString(ProfileFunctions.getSystemUnits())); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { -/* final String _id = bgReading._id; - if (NSUpload.isIdValid(_id)) { - NSUpload.removeFoodFromNS(_id); - } else { - UploadQueue.removeID("dbAdd", _id); - } -*/ - bgReading.isValid = false; - MainApp.getDbHelper().update(bgReading); - updateGUI(); - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - - } - } } } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/CarbsGenerator.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/CarbsGenerator.java index 0e7215b368..f22523160d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/CarbsGenerator.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/CarbsGenerator.java @@ -9,7 +9,7 @@ import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperActivity; +import info.nightscout.androidaps.activities.ErrorHelperActivity; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.T; 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 b2d18aa4b7..69589a126f 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 @@ -45,7 +45,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.activities.ErrorHelperActivity; import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensData; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/dialogs/WizardInfoDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/dialogs/WizardInfoDialog.kt index 50bf9aa790..63f3b57f65 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/dialogs/WizardInfoDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/dialogs/WizardInfoDialog.kt @@ -35,7 +35,7 @@ class WizardInfoDialog : DialogFragment() { close.setOnClickListener { dismiss() } val units = ProfileFunctions.getSystemUnits() - var bg_string= "" + val bg_string: String if (units.equals(Constants.MGDL)) { bg_string = DecimalFormatter.to0Decimal(JsonHelper.safeGetDouble(json, "bg"))} else { bg_string = DecimalFormatter.to1Decimal(JsonHelper.safeGetDouble(json, "bg"))} // BG treatments_wizard_bg.text = MainApp.gs(R.string.format_bg_isf, bg_string , JsonHelper.safeGetDouble(json, "isf")) 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 ee78c6748b..27e35ed456 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,7 +1,5 @@ package info.nightscout.androidaps.plugins.treatments.fragments; -import android.content.Context; -import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; import android.view.LayoutInflater; @@ -10,7 +8,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; -import androidx.appcompat.app.AlertDialog; +import androidx.annotation.NonNull; import androidx.cardview.widget.CardView; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; @@ -38,24 +36,21 @@ 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.OKDialog; 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 Fragment implements View.OnClickListener { +public class TreatmentsBolusFragment extends Fragment { private CompositeDisposable disposable = new CompositeDisposable(); - RecyclerView recyclerView; - LinearLayoutManager llm; + private RecyclerView recyclerView; - TextView iobTotal; - TextView activityTotal; - Button refreshFromNS; - Button deleteFutureTreatments; - - Context context; + private TextView iobTotal; + private TextView activityTotal; + private Button deleteFutureTreatments; public class RecyclerViewAdapter extends RecyclerView.Adapter { @@ -65,6 +60,7 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis this.treatments = treatments; } + @NonNull @Override public TreatmentsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_bolus_item, viewGroup, false); @@ -72,16 +68,16 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis } @Override - public void onBindViewHolder(TreatmentsViewHolder holder, int position) { + public void onBindViewHolder(@NonNull TreatmentsViewHolder holder, int position) { Profile profile = ProfileFunctions.getInstance().getProfile(); if (profile == null) return; Treatment t = treatments.get(position); holder.date.setText(DateUtil.dateAndTimeString(t.date)); - holder.insulin.setText(DecimalFormatter.toPumpSupportedBolus(t.insulin) + " U"); - holder.carbs.setText(DecimalFormatter.to0Decimal(t.carbs) + " g"); + holder.insulin.setText(MainApp.gs(R.string.formatinsulinunits, t.insulin)); + holder.carbs.setText(MainApp.gs(R.string.format_carbs, (int) t.carbs)); Iob iob = t.iobCalc(System.currentTimeMillis(), profile.getDia()); - holder.iob.setText(DecimalFormatter.to2Decimal(iob.iobContrib) + " U"); + holder.iob.setText(MainApp.gs(R.string.formatinsulinunits, iob.iobContrib)); holder.mealOrCorrection.setText(t.isSMB ? "SMB" : t.mealBolus ? MainApp.gs(R.string.mealbolus) : MainApp.gs(R.string.correctionbous)); holder.ph.setVisibility(t.source == Source.PUMP ? View.VISIBLE : View.GONE); holder.ns.setVisibility(NSUpload.isIdValid(t._id) ? View.VISIBLE : View.GONE); @@ -105,7 +101,7 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis } @Override - public void onAttachedToRecyclerView(RecyclerView recyclerView) { + public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } @@ -124,19 +120,19 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis TreatmentsViewHolder(View itemView) { super(itemView); - cv = (CardView) itemView.findViewById(R.id.treatments_cardview); - date = (TextView) itemView.findViewById(R.id.treatments_date); - insulin = (TextView) itemView.findViewById(R.id.treatments_insulin); - carbs = (TextView) itemView.findViewById(R.id.treatments_carbs); - iob = (TextView) itemView.findViewById(R.id.treatments_iob); - mealOrCorrection = (TextView) itemView.findViewById(R.id.treatments_mealorcorrection); - ph = (TextView) itemView.findViewById(R.id.pump_sign); - ns = (TextView) itemView.findViewById(R.id.ns_sign); - invalid = (TextView) itemView.findViewById(R.id.invalid_sign); - calculation = (TextView) itemView.findViewById(R.id.treatments_calculation); + cv = itemView.findViewById(R.id.treatments_cardview); + date = itemView.findViewById(R.id.treatments_date); + insulin = itemView.findViewById(R.id.treatments_insulin); + carbs = itemView.findViewById(R.id.treatments_carbs); + iob = itemView.findViewById(R.id.treatments_iob); + mealOrCorrection = itemView.findViewById(R.id.treatments_mealorcorrection); + ph = itemView.findViewById(R.id.pump_sign); + ns = itemView.findViewById(R.id.ns_sign); + invalid = itemView.findViewById(R.id.invalid_sign); + calculation = itemView.findViewById(R.id.treatments_calculation); calculation.setOnClickListener(this); calculation.setPaintFlags(calculation.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); - remove = (TextView) itemView.findViewById(R.id.treatments_remove); + remove = itemView.findViewById(R.id.treatments_remove); remove.setOnClickListener(this); remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); } @@ -148,28 +144,25 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis return; switch (v.getId()) { case R.id.treatments_remove: - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(treatment.date)); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - final String _id = treatment._id; - if (treatment.source == Source.PUMP) { - treatment.isValid = false; - TreatmentsPlugin.getPlugin().getService().update(treatment); - } else { - if (NSUpload.isIdValid(_id)) { - NSUpload.removeCareportalEntryFromNS(_id); + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.removerecord), + MainApp.gs(R.string.configbuilder_insulin) + ": " + MainApp.gs(R.string.formatinsulinunits, treatment.insulin) + + "\n" + MainApp.gs(R.string.carbs) + ": " + MainApp.gs(R.string.format_carbs, (int) treatment.carbs) + + "\n" + MainApp.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(treatment.date), + (dialog, id) -> { + final String _id = treatment._id; + if (treatment.source == Source.PUMP) { + treatment.isValid = false; + TreatmentsPlugin.getPlugin().getService().update(treatment); } else { - UploadQueue.removeID("dbAdd", _id); + if (NSUpload.isIdValid(_id)) { + NSUpload.removeCareportalEntryFromNS(_id); + } else { + UploadQueue.removeID("dbAdd", _id); + } + TreatmentsPlugin.getPlugin().getService().delete(treatment); } - TreatmentsPlugin.getPlugin().getService().delete(treatment); - } - updateGui(); - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); + updateGui(); + }, null); break; case R.id.treatments_calculation: FragmentManager manager = getFragmentManager(); @@ -193,71 +186,48 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis Bundle savedInstanceState) { View view = inflater.inflate(R.layout.treatments_bolus_fragment, container, false); - recyclerView = (RecyclerView) view.findViewById(R.id.treatments_recyclerview); + recyclerView = view.findViewById(R.id.treatments_recyclerview); recyclerView.setHasFixedSize(true); - llm = new LinearLayoutManager(view.getContext()); + LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); recyclerView.setLayoutManager(llm); RecyclerViewAdapter adapter = new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTreatmentsFromHistory()); recyclerView.setAdapter(adapter); - iobTotal = (TextView) view.findViewById(R.id.treatments_iobtotal); - activityTotal = (TextView) view.findViewById(R.id.treatments_iobactivitytotal); + iobTotal = view.findViewById(R.id.treatments_iobtotal); + activityTotal = view.findViewById(R.id.treatments_iobactivitytotal); - refreshFromNS = (Button) view.findViewById(R.id.treatments_reshreshfromnightscout); - refreshFromNS.setOnClickListener(this); + Button refreshFromNS = view.findViewById(R.id.treatments_reshreshfromnightscout); + refreshFromNS.setOnClickListener(v -> OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.refresheventsfromnightscout) + "?", () -> { + TreatmentsPlugin.getPlugin().getService().resetTreatments(); + RxBus.INSTANCE.send(new EventNSClientRestart()); + })); - deleteFutureTreatments = (Button) view.findViewById(R.id.treatments_delete_future_treatments); - deleteFutureTreatments.setOnClickListener(this); + deleteFutureTreatments = view.findViewById(R.id.treatments_delete_future_treatments); + deleteFutureTreatments.setOnClickListener(v -> { + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.overview_treatment_label), MainApp.gs(R.string.deletefuturetreatments) + "?", () -> { + final List futureTreatments = TreatmentsPlugin.getPlugin().getService() + .getTreatmentDataFromTime(now() + 1000, true); + for (Treatment treatment : futureTreatments) { + final String _id = treatment._id; + if (NSUpload.isIdValid(_id)) { + NSUpload.removeCareportalEntryFromNS(_id); + } else { + UploadQueue.removeID("dbAdd", _id); + } + TreatmentsPlugin.getPlugin().getService().delete(treatment); + } + updateGui(); + }); + }); boolean nsUploadOnly = SP.getBoolean(R.string.key_ns_upload_only, false); if (nsUploadOnly) refreshFromNS.setVisibility(View.GONE); - context = getContext(); - return view; } - @Override - public void onClick(View view) { - AlertDialog.Builder builder; - switch (view.getId()) { - case R.id.treatments_reshreshfromnightscout: - builder = new AlertDialog.Builder(this.getContext()); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.refresheventsfromnightscout) + "?"); - builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { - TreatmentsPlugin.getPlugin().getService().resetTreatments(); - RxBus.INSTANCE.send(new EventNSClientRestart()); - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - case R.id.treatments_delete_future_treatments: - builder = new AlertDialog.Builder(this.getContext()); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.deletefuturetreatments) + "?"); - builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { - final List futureTreatments = TreatmentsPlugin.getPlugin().getService() - .getTreatmentDataFromTime(now() + 1000, true); - for (Treatment treatment : futureTreatments) { - final String _id = treatment._id; - if (NSUpload.isIdValid(_id)) { - NSUpload.removeCareportalEntryFromNS(_id); - } else { - UploadQueue.removeID("dbAdd", _id); - } - TreatmentsPlugin.getPlugin().getService().delete(treatment); - } - updateGui(); - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - } - } - @Override public synchronized void onResume() { super.onResume(); @@ -292,5 +262,4 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis 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 4810a1ed8c..5aa4f091d8 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,7 +1,5 @@ package info.nightscout.androidaps.plugins.treatments.fragments; -import android.content.Context; -import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; import android.view.LayoutInflater; @@ -10,7 +8,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; -import androidx.appcompat.app.AlertDialog; +import androidx.annotation.NonNull; import androidx.cardview.widget.CardView; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; @@ -28,6 +26,7 @@ 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.OKDialog; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.Translator; import io.reactivex.android.schedulers.AndroidSchedulers; @@ -37,14 +36,10 @@ import io.reactivex.disposables.CompositeDisposable; * Created by mike on 13/01/17. */ -public class TreatmentsCareportalFragment extends Fragment implements View.OnClickListener { +public class TreatmentsCareportalFragment extends Fragment { private CompositeDisposable disposable = new CompositeDisposable(); - RecyclerView recyclerView; - LinearLayoutManager llm; - Button refreshFromNS; - - Context context; + private RecyclerView recyclerView; public class RecyclerViewAdapter extends RecyclerView.Adapter { @@ -54,11 +49,11 @@ public class TreatmentsCareportalFragment extends Fragment implements View.OnCli this.careportalEventList = careportalEventList; } + @NonNull @Override public CareportalEventsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_careportal_item, viewGroup, false); - CareportalEventsViewHolder CareportalEventsViewHolder = new CareportalEventsViewHolder(v); - return CareportalEventsViewHolder; + return new CareportalEventsViewHolder(v); } @Override @@ -77,11 +72,11 @@ public class TreatmentsCareportalFragment extends Fragment implements View.OnCli } @Override - public void onAttachedToRecyclerView(RecyclerView recyclerView) { + public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } - public class CareportalEventsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + class CareportalEventsViewHolder extends RecyclerView.ViewHolder { CardView cv; TextView date; TextView type; @@ -91,38 +86,30 @@ public class TreatmentsCareportalFragment extends Fragment implements View.OnCli CareportalEventsViewHolder(View itemView) { super(itemView); - cv = (CardView) itemView.findViewById(R.id.careportal_cardview); - date = (TextView) itemView.findViewById(R.id.careportal_date); - type = (TextView) itemView.findViewById(R.id.careportal_type); - note = (TextView) itemView.findViewById(R.id.careportal_note); - ns = (TextView) itemView.findViewById(R.id.ns_sign); - remove = (TextView) itemView.findViewById(R.id.careportal_remove); - remove.setOnClickListener(this); + cv = itemView.findViewById(R.id.careportal_cardview); + date = itemView.findViewById(R.id.careportal_date); + type = itemView.findViewById(R.id.careportal_type); + note = itemView.findViewById(R.id.careportal_note); + ns = itemView.findViewById(R.id.ns_sign); + remove = itemView.findViewById(R.id.careportal_remove); + remove.setOnClickListener(v -> { + final CareportalEvent careportalEvent = (CareportalEvent) v.getTag(); + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.removerecord), + "\n" + MainApp.gs(R.string.careportal_newnstreatment_eventtype) + ": " + Translator.translate(careportalEvent.eventType) + + "\n" + MainApp.gs(R.string.careportal_newnstreatment_notes_label) + ": " + careportalEvent.getNotes() + + "\n" + MainApp.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(careportalEvent.date), + (dialog, id) -> { + final String _id = careportalEvent._id; + if (NSUpload.isIdValid(_id)) { + NSUpload.removeCareportalEntryFromNS(_id); + } else { + UploadQueue.removeID("dbAdd", _id); + } + MainApp.getDbHelper().delete(careportalEvent); + }, null); + }); remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); } - - @Override - public void onClick(View v) { - final CareportalEvent careportalEvent = (CareportalEvent) v.getTag(); - switch (v.getId()) { - case R.id.careportal_remove: - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(careportalEvent.date)); - builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { - final String _id = careportalEvent._id; - if (NSUpload.isIdValid(_id)) { - NSUpload.removeCareportalEntryFromNS(_id); - } else { - UploadQueue.removeID("dbAdd", _id); - } - MainApp.getDbHelper().delete(careportalEvent); - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - } - } } } @@ -131,20 +118,23 @@ public class TreatmentsCareportalFragment extends Fragment implements View.OnCli Bundle savedInstanceState) { View view = inflater.inflate(R.layout.treatments_careportal_fragment, container, false); - recyclerView = (RecyclerView) view.findViewById(R.id.careportal_recyclerview); + recyclerView = view.findViewById(R.id.careportal_recyclerview); recyclerView.setHasFixedSize(true); - llm = new LinearLayoutManager(view.getContext()); + LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); recyclerView.setLayoutManager(llm); RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)); recyclerView.setAdapter(adapter); - refreshFromNS = (Button) view.findViewById(R.id.careportal_refreshfromnightscout); - refreshFromNS.setOnClickListener(this); + Button refreshFromNS = view.findViewById(R.id.careportal_refreshfromnightscout); + refreshFromNS.setOnClickListener(v -> + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.careportal), MainApp.gs(R.string.refresheventsfromnightscout) + " ?", () -> { + MainApp.getDbHelper().resetCareportalEvents(); + RxBus.INSTANCE.send(new EventNSClientRestart()); + })); - view.findViewById(R.id.careportal_removeandroidapsstartedevents).setOnClickListener(this); - - context = getContext(); + view.findViewById(R.id.careportal_removeandroidapsstartedevents).setOnClickListener(v -> + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.careportal), MainApp.gs(R.string.careportal_removestartedevents), this::removeAndroidAPSStatedEvents)); boolean nsUploadOnly = SP.getBoolean(R.string.key_ns_upload_only, false); if (nsUploadOnly) @@ -170,36 +160,6 @@ public class TreatmentsCareportalFragment extends Fragment implements View.OnCli disposable.clear(); } - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.careportal_refreshfromnightscout: - AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.refresheventsfromnightscout) + " ?"); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - MainApp.getDbHelper().resetCareportalEvents(); - RxBus.INSTANCE.send(new EventNSClientRestart()); - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - case R.id.careportal_removeandroidapsstartedevents: - builder = new AlertDialog.Builder(context); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.careportal_removestartedevents)); - builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { - removeAndroidAPSStatedEvents(); - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - } - - } - private void updateGui() { recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)), false); } 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 5ce7b57564..928e2d30aa 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,7 +1,5 @@ package info.nightscout.androidaps.plugins.treatments.fragments; -import android.content.Context; -import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; import android.view.LayoutInflater; @@ -9,7 +7,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; -import androidx.appcompat.app.AlertDialog; +import androidx.annotation.NonNull; import androidx.cardview.widget.CardView; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; @@ -31,6 +29,7 @@ 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.OKDialog; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; @@ -38,10 +37,7 @@ import io.reactivex.disposables.CompositeDisposable; public class TreatmentsExtendedBolusesFragment extends Fragment { private CompositeDisposable disposable = new CompositeDisposable(); - RecyclerView recyclerView; - LinearLayoutManager llm; - - Context context; + private RecyclerView recyclerView; public class RecyclerViewAdapter extends RecyclerView.Adapter { @@ -51,6 +47,7 @@ public class TreatmentsExtendedBolusesFragment extends Fragment { this.extendedBolusList = extendedBolusList; } + @NonNull @Override public ExtendedBolusesViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_extendedbolus_item, viewGroup, false); @@ -101,11 +98,11 @@ public class TreatmentsExtendedBolusesFragment extends Fragment { } @Override - public void onAttachedToRecyclerView(RecyclerView recyclerView) { + public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } - public class ExtendedBolusesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + class ExtendedBolusesViewHolder extends RecyclerView.ViewHolder { CardView cv; TextView date; TextView duration; @@ -120,31 +117,22 @@ public class TreatmentsExtendedBolusesFragment extends Fragment { ExtendedBolusesViewHolder(View itemView) { super(itemView); - cv = (CardView) itemView.findViewById(R.id.extendedboluses_cardview); - date = (TextView) itemView.findViewById(R.id.extendedboluses_date); - duration = (TextView) itemView.findViewById(R.id.extendedboluses_duration); - insulin = (TextView) itemView.findViewById(R.id.extendedboluses_insulin); - realDuration = (TextView) itemView.findViewById(R.id.extendedboluses_realduration); - ratio = (TextView) itemView.findViewById(R.id.extendedboluses_ratio); - insulinSoFar = (TextView) itemView.findViewById(R.id.extendedboluses_netinsulin); - iob = (TextView) itemView.findViewById(R.id.extendedboluses_iob); - ph = (TextView) itemView.findViewById(R.id.pump_sign); - ns = (TextView) itemView.findViewById(R.id.ns_sign); - remove = (TextView) itemView.findViewById(R.id.extendedboluses_remove); - remove.setOnClickListener(this); - remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); - } - - @Override - public void onClick(View v) { - final ExtendedBolus extendedBolus = (ExtendedBolus) v.getTag(); - switch (v.getId()) { - case R.id.extendedboluses_remove: - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(extendedBolus.date)); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { + cv = itemView.findViewById(R.id.extendedboluses_cardview); + date = itemView.findViewById(R.id.extendedboluses_date); + duration = itemView.findViewById(R.id.extendedboluses_duration); + insulin = itemView.findViewById(R.id.extendedboluses_insulin); + realDuration = itemView.findViewById(R.id.extendedboluses_realduration); + ratio = itemView.findViewById(R.id.extendedboluses_ratio); + insulinSoFar = itemView.findViewById(R.id.extendedboluses_netinsulin); + iob = itemView.findViewById(R.id.extendedboluses_iob); + ph = itemView.findViewById(R.id.pump_sign); + ns = itemView.findViewById(R.id.ns_sign); + remove = itemView.findViewById(R.id.extendedboluses_remove); + remove.setOnClickListener(v -> { + final ExtendedBolus extendedBolus = (ExtendedBolus) v.getTag(); + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.removerecord), + MainApp.gs(R.string.extended_bolus) + + "\n" + MainApp.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(extendedBolus.date), (dialog, id) -> { final String _id = extendedBolus._id; if (NSUpload.isIdValid(_id)) { NSUpload.removeCareportalEntryFromNS(_id); @@ -152,12 +140,9 @@ public class TreatmentsExtendedBolusesFragment extends Fragment { UploadQueue.removeID("dbAdd", _id); } MainApp.getDbHelper().delete(extendedBolus); - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - } + }, null); + }); + remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); } } } @@ -167,16 +152,14 @@ public class TreatmentsExtendedBolusesFragment extends Fragment { Bundle savedInstanceState) { View view = inflater.inflate(R.layout.treatments_extendedbolus_fragment, container, false); - recyclerView = (RecyclerView) view.findViewById(R.id.extendedboluses_recyclerview); + recyclerView = view.findViewById(R.id.extendedboluses_recyclerview); recyclerView.setHasFixedSize(true); - llm = new LinearLayoutManager(view.getContext()); + LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); recyclerView.setLayoutManager(llm); RecyclerViewAdapter adapter = new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getExtendedBolusesFromHistory()); recyclerView.setAdapter(adapter); - context = getContext(); - return view; } @@ -205,5 +188,4 @@ public class TreatmentsExtendedBolusesFragment extends Fragment { 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.kt b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsProfileSwitchFragment.kt index e65bd15200..45d25124de 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsProfileSwitchFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsProfileSwitchFragment.kt @@ -1,13 +1,11 @@ package info.nightscout.androidaps.plugins.treatments.fragments -import android.content.DialogInterface import android.graphics.Paint import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView -import androidx.appcompat.app.AlertDialog import androidx.cardview.widget.CardView import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment @@ -17,6 +15,7 @@ import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.db.ProfileSwitch import info.nightscout.androidaps.db.Source +import info.nightscout.androidaps.dialogs.ProfileViewerDialog import info.nightscout.androidaps.events.EventProfileNeedsUpdate import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.nsclient.NSUpload @@ -42,18 +41,15 @@ class TreatmentsProfileSwitchFragment : Fragment() { super.onViewCreated(view, savedInstanceState) profileswitch_recyclerview.setHasFixedSize(true) profileswitch_recyclerview.layoutManager = LinearLayoutManager(view.context) - profileswitch_recyclerview.adapter = RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(),false)) + profileswitch_recyclerview.adapter = RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(), false)) profileswitch_refreshfromnightscout.setOnClickListener { - val builder = AlertDialog.Builder(this.context!!) - builder.setTitle(MainApp.gs(R.string.confirmation)) - builder.setMessage(MainApp.gs(R.string.refresheventsfromnightscout) + "?") - builder.setPositiveButton(MainApp.gs(R.string.ok)) { _ , _-> - MainApp.getDbHelper().resetProfileSwitch() - RxBus.send(EventNSClientRestart()) + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.refresheventsfromnightscout) + "?", Runnable { + MainApp.getDbHelper().resetProfileSwitch() + RxBus.send(EventNSClientRestart()) + }) } - builder.setNegativeButton(MainApp.gs(R.string.cancel), null) - builder.show() } if (SP.getBoolean(R.string.key_ns_upload_only, false)) profileswitch_refreshfromnightscout.visibility = View.GONE @@ -63,9 +59,9 @@ class TreatmentsProfileSwitchFragment : Fragment() { override fun onResume() { super.onResume() disposable.add(RxBus - .toObservable(EventProfileNeedsUpdate::class.java) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ updateGUI() }) { FabricPrivacy.logException(it) } + .toObservable(EventProfileNeedsUpdate::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ updateGUI() }) { FabricPrivacy.logException(it) } ) updateGUI() } @@ -77,17 +73,17 @@ class TreatmentsProfileSwitchFragment : Fragment() { } fun updateGUI() = - profileswitch_recyclerview?.swapAdapter(RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(),false)), false) + profileswitch_recyclerview?.swapAdapter(RecyclerProfileViewAdapter(MainApp.getDbHelper().getProfileSwitchData(DateUtil.now() - T.days(30).msecs(), false)), false) - inner class RecyclerProfileViewAdapter(var profileSwitchList: List) : RecyclerView.Adapter() { + inner class RecyclerProfileViewAdapter(private var profileSwitchList: List) : RecyclerView.Adapter() { override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ProfileSwitchViewHolder { return ProfileSwitchViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.treatments_profileswitch_item, viewGroup, false)) } override fun onBindViewHolder(holder: ProfileSwitchViewHolder, position: Int) { val profileSwitch = profileSwitchList[position] - holder.ph.visibility = if (profileSwitch.source == Source.PUMP) View.VISIBLE else View.GONE - holder.ns.visibility = if (NSUpload.isIdValid(profileSwitch._id)) View.VISIBLE else View.GONE + holder.ph.visibility = (profileSwitch.source == Source.PUMP).toVisibility() + holder.ns.visibility = NSUpload.isIdValid(profileSwitch._id).toVisibility() holder.date.text = DateUtil.dateAndTimeString(profileSwitch.date) if (!profileSwitch.isEndingEvent) { holder.duration.text = DecimalFormatter.to0Decimal(profileSwitch.durationInMinutes.toDouble()) + " " + MainApp.gs(R.string.unit_minute_short) @@ -121,21 +117,28 @@ class TreatmentsProfileSwitchFragment : Fragment() { override fun onClick(v: View) { val profileSwitch = v.tag as ProfileSwitch when (v.id) { - R.id.profileswitch_remove -> - OKDialog.showConfirmation(activity, MainApp.gs(R.string.removerecord) + "\n" + profileSwitch.profileName + "\n" + DateUtil.dateAndTimeString(profileSwitch.date)) { - val id = profileSwitch._id - if (NSUpload.isIdValid(id)) NSUpload.removeCareportalEntryFromNS(id) - else UploadQueue.removeID("dbAdd", id) - MainApp.getDbHelper().delete(profileSwitch) + R.id.profileswitch_remove -> + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.removerecord), + MainApp.gs(R.string.careportal_profileswitch) + ": " + profileSwitch.profileName + + "\n" + MainApp.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(profileSwitch.date), Runnable { + val id = profileSwitch._id + if (NSUpload.isIdValid(id)) NSUpload.removeCareportalEntryFromNS(id) + else UploadQueue.removeID("dbAdd", id) + MainApp.getDbHelper().delete(profileSwitch) + }) } - R.id.profileswitch_clone -> - OKDialog.showConfirmation(activity, MainApp.gs(R.string.copytolocalprofile) + "\n" + profileSwitch.customizedName + "\n" + DateUtil.dateAndTimeString(profileSwitch.date)) { - profileSwitch.profileObject?.let { - val nonCustomized = it.convertToNonCustomizedProfile() - LocalProfilePlugin.addProfile(LocalProfilePlugin.SingleProfile().copyFrom(nonCustomized, profileSwitch.customizedName + " " + DateUtil.dateAndTimeString(profileSwitch.date).replace(".", "_"))) - RxBus.send(EventLocalProfileChanged()) - } + R.id.profileswitch_clone -> + activity?.let { activity -> + OKDialog.showConfirmation(activity, MainApp.gs(R.string.careportal_profileswitch), MainApp.gs(R.string.copytolocalprofile) + "\n" + profileSwitch.customizedName + "\n" + DateUtil.dateAndTimeString(profileSwitch.date), Runnable { + profileSwitch.profileObject?.let { + val nonCustomized = it.convertToNonCustomizedProfile() + LocalProfilePlugin.addProfile(LocalProfilePlugin.SingleProfile().copyFrom(nonCustomized, profileSwitch.customizedName + " " + DateUtil.dateAndTimeString(profileSwitch.date).replace(".", "_"))) + RxBus.send(EventLocalProfileChanged()) + } + }) } + R.id.profileswitch_date, R.id.profileswitch_name -> { val args = Bundle() args.putLong("time", (v.tag as ProfileSwitch).date) 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 3ee36b87dd..8b73ec8ed2 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 @@ -1,8 +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; import android.os.Bundle; import android.view.LayoutInflater; @@ -11,7 +8,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; -import androidx.appcompat.app.AlertDialog; +import androidx.annotation.NonNull; import androidx.cardview.widget.CardView; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; @@ -33,6 +30,7 @@ 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.OKDialog; import info.nightscout.androidaps.utils.SP; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; @@ -41,14 +39,10 @@ import io.reactivex.disposables.CompositeDisposable; * Created by mike on 13/01/17. */ -public class TreatmentsTempTargetFragment extends Fragment implements View.OnClickListener { +public class TreatmentsTempTargetFragment extends Fragment { private CompositeDisposable disposable = new CompositeDisposable(); - RecyclerView recyclerView; - LinearLayoutManager llm; - Button refreshFromNS; - - Context context; + private RecyclerView recyclerView; public class RecyclerViewAdapter extends RecyclerView.Adapter { @@ -60,11 +54,11 @@ public class TreatmentsTempTargetFragment extends Fragment implements View.OnCli currentlyActiveTarget = tempTargetList.getValueByInterval(System.currentTimeMillis()); } + @NonNull @Override public TempTargetsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_temptarget_item, viewGroup, false); - TempTargetsViewHolder TempTargetsViewHolder = new TempTargetsViewHolder(v); - return TempTargetsViewHolder; + return new TempTargetsViewHolder(v); } @Override @@ -104,11 +98,11 @@ public class TreatmentsTempTargetFragment extends Fragment implements View.OnCli } @Override - public void onAttachedToRecyclerView(RecyclerView recyclerView) { + public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } - public class TempTargetsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + class TempTargetsViewHolder extends RecyclerView.ViewHolder { CardView cv; TextView date; TextView duration; @@ -123,31 +117,23 @@ public class TreatmentsTempTargetFragment extends Fragment implements View.OnCli TempTargetsViewHolder(View itemView) { super(itemView); - cv = (CardView) itemView.findViewById(R.id.temptargetrange_cardview); - date = (TextView) itemView.findViewById(R.id.temptargetrange_date); - duration = (TextView) itemView.findViewById(R.id.temptargetrange_duration); - low = (TextView) itemView.findViewById(R.id.temptargetrange_low); - high = (TextView) itemView.findViewById(R.id.temptargetrange_high); - reason = (TextView) itemView.findViewById(R.id.temptargetrange_reason); - reasonLabel = (TextView) itemView.findViewById(R.id.temptargetrange_reason_label); - reasonColon = (TextView) itemView.findViewById(R.id.temptargetrange_reason_colon); - ph = (TextView) itemView.findViewById(R.id.pump_sign); - ns = (TextView) itemView.findViewById(R.id.ns_sign); - remove = (TextView) itemView.findViewById(R.id.temptargetrange_remove); - remove.setOnClickListener(this); - remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); - } - - @Override - public void onClick(View v) { - final TempTarget tempTarget = (TempTarget) v.getTag(); - switch (v.getId()) { - case R.id.temptargetrange_remove: - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(tempTarget.date)); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { + cv = itemView.findViewById(R.id.temptargetrange_cardview); + date = itemView.findViewById(R.id.temptargetrange_date); + duration = itemView.findViewById(R.id.temptargetrange_duration); + low = itemView.findViewById(R.id.temptargetrange_low); + high = itemView.findViewById(R.id.temptargetrange_high); + reason = itemView.findViewById(R.id.temptargetrange_reason); + reasonLabel = itemView.findViewById(R.id.temptargetrange_reason_label); + reasonColon = itemView.findViewById(R.id.temptargetrange_reason_colon); + ph = itemView.findViewById(R.id.pump_sign); + ns = itemView.findViewById(R.id.ns_sign); + remove = itemView.findViewById(R.id.temptargetrange_remove); + remove.setOnClickListener(v -> { + final TempTarget tempTarget = (TempTarget) v.getTag(); + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.removerecord), + MainApp.gs(R.string.careportal_temporarytarget) + ": " + tempTarget.friendlyDescription(ProfileFunctions.getSystemUnits()) + + "\n" + DateUtil.dateAndTimeString(tempTarget.date), + (dialog, id) -> { final String _id = tempTarget._id; if (NSUpload.isIdValid(_id)) { NSUpload.removeCareportalEntryFromNS(_id); @@ -155,12 +141,9 @@ public class TreatmentsTempTargetFragment extends Fragment implements View.OnCli UploadQueue.removeID("dbAdd", _id); } MainApp.getDbHelper().delete(tempTarget); - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - } + }, null); + }); + remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); } } } @@ -170,18 +153,20 @@ public class TreatmentsTempTargetFragment extends Fragment implements View.OnCli Bundle savedInstanceState) { View view = inflater.inflate(R.layout.treatments_temptarget_fragment, container, false); - recyclerView = (RecyclerView) view.findViewById(R.id.temptargetrange_recyclerview); + recyclerView = view.findViewById(R.id.temptargetrange_recyclerview); recyclerView.setHasFixedSize(true); - llm = new LinearLayoutManager(view.getContext()); + LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); recyclerView.setLayoutManager(llm); RecyclerViewAdapter adapter = new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTempTargetsFromHistory()); recyclerView.setAdapter(adapter); - refreshFromNS = (Button) view.findViewById(R.id.temptargetrange_refreshfromnightscout); - refreshFromNS.setOnClickListener(this); - - context = getContext(); + Button refreshFromNS = view.findViewById(R.id.temptargetrange_refreshfromnightscout); + refreshFromNS.setOnClickListener(v -> + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.refresheventsfromnightscout) + " ?", () -> { + MainApp.getDbHelper().resetTempTargets(); + RxBus.INSTANCE.send(new EventNSClientRestart()); + })); boolean nsUploadOnly = SP.getBoolean(R.string.key_ns_upload_only, false); if (nsUploadOnly) @@ -207,26 +192,6 @@ public class TreatmentsTempTargetFragment extends Fragment implements View.OnCli disposable.clear(); } - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.temptargetrange_refreshfromnightscout: - AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.refresheventsfromnightscout) + " ?"); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - MainApp.getDbHelper().resetTempTargets(); - RxBus.INSTANCE.send(new EventNSClientRestart()); - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - } - - } - 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 45f930410e..22dd86e381 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,6 +1,5 @@ package info.nightscout.androidaps.plugins.treatments.fragments; -import android.content.Context; import android.graphics.Paint; import android.os.Bundle; import android.view.LayoutInflater; @@ -9,7 +8,6 @@ import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; import androidx.cardview.widget.CardView; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; @@ -33,6 +31,7 @@ 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.OKDialog; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; @@ -40,12 +39,9 @@ import io.reactivex.disposables.CompositeDisposable; public class TreatmentsTemporaryBasalsFragment extends Fragment { private CompositeDisposable disposable = new CompositeDisposable(); - RecyclerView recyclerView; - LinearLayoutManager llm; + private RecyclerView recyclerView; - TextView tempBasalTotalView; - - Context context; + private TextView tempBasalTotalView; public class RecyclerViewAdapter extends RecyclerView.Adapter { @@ -55,6 +51,7 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment { this.tempBasalList = tempBasalList; } + @NonNull @Override public TempBasalsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.treatments_tempbasals_item, viewGroup, false); @@ -123,11 +120,11 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment { } @Override - public void onAttachedToRecyclerView(RecyclerView recyclerView) { + public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } - public class TempBasalsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + class TempBasalsViewHolder extends RecyclerView.ViewHolder { CardView cv; TextView date; TextView duration; @@ -144,45 +141,37 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment { TempBasalsViewHolder(View itemView) { super(itemView); - cv = (CardView) itemView.findViewById(R.id.tempbasals_cardview); - date = (TextView) itemView.findViewById(R.id.tempbasals_date); - duration = (TextView) itemView.findViewById(R.id.tempbasals_duration); - absolute = (TextView) itemView.findViewById(R.id.tempbasals_absolute); - percent = (TextView) itemView.findViewById(R.id.tempbasals_percent); - realDuration = (TextView) itemView.findViewById(R.id.tempbasals_realduration); - netRatio = (TextView) itemView.findViewById(R.id.tempbasals_netratio); - netInsulin = (TextView) itemView.findViewById(R.id.tempbasals_netinsulin); - iob = (TextView) itemView.findViewById(R.id.tempbasals_iob); - extendedFlag = (TextView) itemView.findViewById(R.id.tempbasals_extendedflag); - ph = (TextView) itemView.findViewById(R.id.pump_sign); - ns = (TextView) itemView.findViewById(R.id.ns_sign); - remove = (TextView) itemView.findViewById(R.id.tempbasals_remove); - remove.setOnClickListener(this); + cv = itemView.findViewById(R.id.tempbasals_cardview); + date = itemView.findViewById(R.id.tempbasals_date); + duration = itemView.findViewById(R.id.tempbasals_duration); + absolute = itemView.findViewById(R.id.tempbasals_absolute); + percent = itemView.findViewById(R.id.tempbasals_percent); + realDuration = itemView.findViewById(R.id.tempbasals_realduration); + netRatio = itemView.findViewById(R.id.tempbasals_netratio); + netInsulin = itemView.findViewById(R.id.tempbasals_netinsulin); + iob = itemView.findViewById(R.id.tempbasals_iob); + extendedFlag = itemView.findViewById(R.id.tempbasals_extendedflag); + ph = itemView.findViewById(R.id.pump_sign); + ns = itemView.findViewById(R.id.ns_sign); + remove = itemView.findViewById(R.id.tempbasals_remove); + remove.setOnClickListener(v -> { + final TemporaryBasal tempBasal = (TemporaryBasal) v.getTag(); + OKDialog.showConfirmation(getContext(), MainApp.gs(R.string.removerecord), + MainApp.gs(R.string.pump_tempbasal_label) + ": " + tempBasal.toStringFull() + + "\n" + MainApp.gs(R.string.date) + ": " + DateUtil.dateAndTimeString(tempBasal.date), + ((dialog, id) -> { + final String _id = tempBasal._id; + if (NSUpload.isIdValid(_id)) { + NSUpload.removeCareportalEntryFromNS(_id); + } else { + UploadQueue.removeID("dbAdd", _id); + } + MainApp.getDbHelper().delete(tempBasal); + }), null); + }); remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); } - @Override - public void onClick(View v) { - final TemporaryBasal tempBasal = (TemporaryBasal) v.getTag(); - switch (v.getId()) { - case R.id.tempbasals_remove: - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(MainApp.gs(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(tempBasal.date)); - builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { - final String _id = tempBasal._id; - if (NSUpload.isIdValid(_id)) { - NSUpload.removeCareportalEntryFromNS(_id); - } else { - UploadQueue.removeID("dbAdd", _id); - } - MainApp.getDbHelper().delete(tempBasal); - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); - break; - } - } } } @@ -191,17 +180,15 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment { Bundle savedInstanceState) { View view = inflater.inflate(R.layout.treatments_tempbasals_fragment, container, false); - recyclerView = (RecyclerView) view.findViewById(R.id.tempbasals_recyclerview); + recyclerView = view.findViewById(R.id.tempbasals_recyclerview); recyclerView.setHasFixedSize(true); - llm = new LinearLayoutManager(view.getContext()); + LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); recyclerView.setLayoutManager(llm); RecyclerViewAdapter adapter = new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()); recyclerView.setAdapter(adapter); - tempBasalTotalView = (TextView) view.findViewById(R.id.tempbasals_totaltempiob); - - context = getContext(); + tempBasalTotalView = view.findViewById(R.id.tempbasals_totaltempiob); return view; } 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 35d33ab884..05efc63aa3 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -15,6 +15,7 @@ import java.util.LinkedList; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.activities.BolusProgressHelperActivity; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; @@ -25,8 +26,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.overview.dialogs.BolusProgressDialog; -import info.nightscout.androidaps.plugins.general.overview.dialogs.BolusProgressHelperActivity; +import info.nightscout.androidaps.dialogs.BolusProgressDialog; 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; 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 6348ea31b9..5a67759594 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 @@ -8,7 +8,7 @@ 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.dialogs.BolusProgressDialog; import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.DecimalFormatter; 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 44050ece35..5907ac8392 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/services/DataService.java @@ -56,10 +56,6 @@ public class DataService extends IntentService { boolean acceptNSData = !SP.getBoolean(R.string.key_ns_upload_only, false); Bundle bundles = intent.getExtras(); - if (bundles != null && bundles.containsKey("islocal")) { - acceptNSData = acceptNSData || bundles.getBoolean("islocal"); - } - final String action = intent.getAction(); if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) { diff --git a/app/src/main/java/info/nightscout/androidaps/services/Intents.java b/app/src/main/java/info/nightscout/androidaps/services/Intents.java index 98c2355a4f..e4a2d0b33b 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/Intents.java +++ b/app/src/main/java/info/nightscout/androidaps/services/Intents.java @@ -20,11 +20,6 @@ public interface Intents { String ACTION_URGENT_ALARM = "info.nightscout.client.URGENT_ALARM"; String ACTION_CLEAR_ALARM = "info.nightscout.client.CLEAR_ALARM"; - - // App -> NSClient - String ACTION_DATABASE = "info.nightscout.client.DBACCESS"; - String ACTION_ACK_ALARM = "info.nightscout.client.ACK_ALARM"; - // xDrip -> App String RECEIVER_PERMISSION = "com.eveningoutpost.dexdrip.permissions.RECEIVE_BG_ESTIMATE"; 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 45ab985fce..a0dff38104 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java @@ -23,13 +23,11 @@ import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesFragment; import info.nightscout.androidaps.plugins.constraints.objectives.ObjectivesPlugin; -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.general.maintenance.ImportExportPrefs; import info.nightscout.androidaps.plugins.general.nsclient.NSClientPlugin; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus; import info.nightscout.androidaps.plugins.general.nsclient.services.NSClientService; +import info.nightscout.androidaps.dialogs.ProfileSwitchDialog; import info.nightscout.androidaps.plugins.profile.local.LocalProfileFragment; import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin; import info.nightscout.androidaps.plugins.profile.ns.NSProfileFragment; @@ -297,18 +295,14 @@ public class SWDefinition { .validator(() -> LocalProfilePlugin.INSTANCE.getProfile() != null && LocalProfilePlugin.INSTANCE.getProfile().getDefaultProfile() != null && LocalProfilePlugin.INSTANCE.getProfile().getDefaultProfile().isValid("StartupWizard")) .visibility(() -> LocalProfilePlugin.INSTANCE.isEnabled(PluginType.PROFILE)); - private SWScreen screenProfileSwitch = new SWScreen(R.string.profileswitch) + private SWScreen screenProfileSwitch = new SWScreen(R.string.careportal_profileswitch) .skippable(false) .add(new SWInfotext() .label(R.string.profileswitch_ismissing)) .add(new SWButton() .text(R.string.doprofileswitch) .action(() -> { - NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); - final OptionsToShow profileSwitch = CareportalFragment.PROFILESWITCHDIRECT; - profileSwitch.executeProfileSwitch = true; - newDialog.setOptions(profileSwitch, R.string.careportal_profileswitch); - newDialog.show(getActivity().getSupportFragmentManager(), "NewNSTreatmentDialog"); + new ProfileSwitchDialog().show(getActivity().getSupportFragmentManager(), "SetupWizard"); })) .validator(() -> ProfileFunctions.getInstance().getProfile() != null) .visibility(() -> ProfileFunctions.getInstance().getProfile() == null); 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 8d0d37e003..b74567d027 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java @@ -8,13 +8,10 @@ import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; import androidx.core.app.ActivityCompat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.List; import info.nightscout.androidaps.MainActivity; @@ -37,8 +34,6 @@ 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; @@ -54,7 +49,7 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity { LocaleHelper.INSTANCE.update(getApplicationContext()); setContentView(R.layout.activity_setupwizard); - scrollView = (ScrollView) findViewById(R.id.sw_scrollview); + scrollView = findViewById(R.id.sw_scrollview); Intent intent = getIntent(); currentWizardPage = intent.getIntExtra(SetupWizardActivity.INTENT_MESSAGE, 0); @@ -62,7 +57,7 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity { SWScreen currentScreen = screens.get(currentWizardPage); //Set screen name - TextView screenName = (TextView) findViewById(R.id.sw_content); + TextView screenName = findViewById(R.id.sw_content); screenName.setText(currentScreen.getHeader()); swDefinition.setActivity(this); @@ -187,7 +182,7 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity { return page; page++; } - return Math.min(currentWizardPage, screens.size() -1); + return Math.min(currentWizardPage, screens.size() - 1); } private int previousPage() { @@ -201,17 +196,14 @@ public class SetupWizardActivity extends NoSplashAppCompatActivity { } @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) { switch (requestCode) { case AndroidPermission.CASE_STORAGE: //show dialog after permission is granted - AlertDialog.Builder alert = new AlertDialog.Builder(this); - alert.setMessage(R.string.alert_dialog_storage_permission_text); - alert.setPositiveButton(R.string.ok, null); - alert.show(); + OKDialog.show(this, MainApp.gs(R.string.permission), MainApp.gs(R.string.alert_dialog_storage_permission_text)); break; case AndroidPermission.CASE_LOCATION: case AndroidPermission.CASE_SMS: 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 89e0751e60..6d71a80ba1 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/BolusWizard.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/BolusWizard.kt @@ -2,9 +2,9 @@ package info.nightscout.androidaps.utils import android.content.Context import android.content.Intent -import androidx.appcompat.app.AlertDialog import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.ErrorHelperActivity import info.nightscout.androidaps.data.DetailedBolusInfo import info.nightscout.androidaps.data.Profile import info.nightscout.androidaps.db.CareportalEvent @@ -20,7 +20,6 @@ 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 import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin @@ -127,8 +126,8 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, if (useBg && bg > 0) { bgDiff = when { bg in targetBGLow..targetBGHigh -> 0.0 - bg <= targetBGLow -> bg - targetBGLow - else -> bg - targetBGHigh + bg <= targetBGLow -> bg - targetBGLow + else -> bg - targetBGHigh } insulinFromBG = bgDiff / sens } @@ -142,7 +141,6 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, } } - // Insulin from carbs ic = profile.ic insulinFromCarbs = carbs / ic @@ -185,7 +183,7 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, } val bolusStep = ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription?.bolusStep - ?: 0.1 + ?: 0.1 calculatedTotalInsulin = Round.roundTo(calculatedTotalInsulin, bolusStep) insulinAfterConstraints = MainApp.getConstraintChecker().applyBolusConstraints(Constraint(calculatedTotalInsulin)).value() @@ -236,7 +234,7 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, private fun confirmMessageAfterConstraints(pump: PumpInterface): String { - var confirmMessage = MainApp.gs(R.string.entertreatmentquestion) + var confirmMessage = "" if (insulinAfterConstraints > 0) { val pct = if (percentageCorrection != 100.0) " (" + percentageCorrection.toInt() + "%)" else "" confirmMessage += "
" + MainApp.gs(R.string.bolus) + ": " + "" + DecimalFormatter.toPumpSupportedBolus(insulinAfterConstraints) + "U" + pct + "" @@ -268,89 +266,83 @@ class BolusWizard @JvmOverloads constructor(val profile: Profile, val pump = ConfigBuilderPlugin.getPlugin().activePump ?: return if (calculatedTotalInsulin > 0.0 || carbs > 0.0) { + if (accepted) { + log.debug("guarding: already accepted") + return + } + accepted = true + val confirmMessage = confirmMessageAfterConstraints(pump) - val builder = AlertDialog.Builder(context) - builder.setTitle(MainApp.gs(R.string.confirmation)) - builder.setMessage(HtmlHelper.fromHtml(confirmMessage)) - builder.setPositiveButton(MainApp.gs(R.string.ok)) { _, _ -> - synchronized(builder) { - if (accepted) { - log.debug("guarding: already accepted") - return@setPositiveButton - } - accepted = true - if (insulinAfterConstraints > 0 || carbs > 0) { - if (useSuperBolus) { - val loopPlugin = LoopPlugin.getPlugin() - if (loopPlugin.isEnabled(PluginType.LOOP)) { - loopPlugin.superBolusTo(System.currentTimeMillis() + 2 * 60L * 60 * 1000) - RxBus.send(EventRefreshOverview("WizardDialog")) - } - - val pump1 = ConfigBuilderPlugin.getPlugin().activePump - - if (pump1?.pumpDescription?.tempBasalStyle == PumpDescription.ABSOLUTE) { - ConfigBuilderPlugin.getPlugin().commandQueue.tempBasalAbsolute(0.0, 120, true, profile, object : Callback() { - override fun run() { - if (!result.success) { - val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) - 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) - } - } - }) - } else { - - ConfigBuilderPlugin.getPlugin().commandQueue.tempBasalPercent(0, 120, true, profile, object : Callback() { - override fun run() { - if (!result.success) { - val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) - 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) - } - } - }) - } + OKDialog.showConfirmation(context, MainApp.gs(R.string.boluswizard), HtmlHelper.fromHtml(confirmMessage), Runnable { + if (insulinAfterConstraints > 0 || carbs > 0) { + if (useSuperBolus) { + val loopPlugin = LoopPlugin.getPlugin() + if (loopPlugin.isEnabled(PluginType.LOOP)) { + loopPlugin.superBolusTo(System.currentTimeMillis() + 2 * 60L * 60 * 1000) + RxBus.send(EventRefreshOverview("WizardDialog")) } - val detailedBolusInfo = DetailedBolusInfo() - detailedBolusInfo.eventType = CareportalEvent.BOLUSWIZARD - detailedBolusInfo.insulin = insulinAfterConstraints - detailedBolusInfo.carbs = carbs.toDouble() - detailedBolusInfo.context = context - detailedBolusInfo.glucose = bg - detailedBolusInfo.glucoseType = "Manual" - detailedBolusInfo.carbTime = carbTime - detailedBolusInfo.boluscalc = nsJSON() - detailedBolusInfo.source = Source.USER - detailedBolusInfo.notes = notes - if (detailedBolusInfo.insulin > 0 || ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription?.storesCarbInfo == true) { - ConfigBuilderPlugin.getPlugin().commandQueue.bolus(detailedBolusInfo, object : Callback() { + + val pump1 = ConfigBuilderPlugin.getPlugin().activePump + + if (pump1?.pumpDescription?.tempBasalStyle == PumpDescription.ABSOLUTE) { + ConfigBuilderPlugin.getPlugin().commandQueue.tempBasalAbsolute(0.0, 120, true, profile, object : Callback() { override fun run() { if (!result.success) { val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) i.putExtra("soundid", R.raw.boluserror) i.putExtra("status", result.comment) - i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror)) + i.putExtra("title", MainApp.gs(R.string.tempbasaldeliveryerror)) i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) MainApp.instance().startActivity(i) } } }) } else { - TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false) + + ConfigBuilderPlugin.getPlugin().commandQueue.tempBasalPercent(0, 120, true, profile, object : Callback() { + override fun run() { + if (!result.success) { + val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) + 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) + } + } + }) } } + val detailedBolusInfo = DetailedBolusInfo() + detailedBolusInfo.eventType = CareportalEvent.BOLUSWIZARD + detailedBolusInfo.insulin = insulinAfterConstraints + detailedBolusInfo.carbs = carbs.toDouble() + detailedBolusInfo.context = context + detailedBolusInfo.glucose = bg + detailedBolusInfo.glucoseType = "Manual" + detailedBolusInfo.carbTime = carbTime + detailedBolusInfo.boluscalc = nsJSON() + detailedBolusInfo.source = Source.USER + detailedBolusInfo.notes = notes + if (detailedBolusInfo.insulin > 0 || ConfigBuilderPlugin.getPlugin().activePump?.pumpDescription?.storesCarbInfo == true) { + ConfigBuilderPlugin.getPlugin().commandQueue.bolus(detailedBolusInfo, object : Callback() { + override fun run() { + if (!result.success) { + val i = Intent(MainApp.instance(), ErrorHelperActivity::class.java) + i.putExtra("soundid", R.raw.boluserror) + i.putExtra("status", result.comment) + i.putExtra("title", MainApp.gs(R.string.treatmentdeliveryerror)) + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + MainApp.instance().startActivity(i) + } + } + }) + } else { + TreatmentsPlugin.getPlugin().addToHistoryTreatment(detailedBolusInfo, false) + } } - } - builder.setNegativeButton(MainApp.gs(R.string.cancel), null) - builder.show() + }) } } } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/DefaultValueHelper.java b/app/src/main/java/info/nightscout/androidaps/utils/DefaultValueHelper.java deleted file mode 100644 index 7ea507e280..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/utils/DefaultValueHelper.java +++ /dev/null @@ -1,95 +0,0 @@ -package info.nightscout.androidaps.utils; - -import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.Profile; -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; - -public class DefaultValueHelper { - - /** - * returns the corresponding EatingSoon TempTarget based on the given units (MMOL / MGDL) - * - * @param units - * @return - */ - public double getDefaultEatingSoonTT(String units) { - return Constants.MMOL.equals(units) ? Constants.defaultEatingSoonTTmmol - : Constants.defaultEatingSoonTTmgdl; - } - - /** - * returns the corresponding Activity TempTarget based on the given units (MMOL / MGDL) - * - * @param units - * @return - */ - public double getDefaultActivityTT(String units) { - return Constants.MMOL.equals(units) ? Constants.defaultActivityTTmmol - : Constants.defaultActivityTTmgdl; - } - - /** - * returns the corresponding Hypo TempTarget based on the given units (MMOL / MGDL) - * - * @param units - * @return - */ - public double getDefaultHypoTT(String units) { - return Constants.MMOL.equals(units) ? Constants.defaultHypoTTmmol - : Constants.defaultHypoTTmgdl; - } - - /** - * returns the configured EatingSoon TempTarget, if this is set to 0, the Default-Value is returned. - * - * @return - */ - public double determineEatingSoonTT() { - String units = ProfileFunctions.getSystemUnits(); - double value = SP.getDouble(R.string.key_eatingsoon_target, this.getDefaultEatingSoonTT(units)); - value = Profile.toCurrentUnits(value); - return value > 0 ? value : this.getDefaultEatingSoonTT(units); - } - - public int determineEatingSoonTTDuration() { - int value = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration); - return value > 0 ? value : Constants.defaultEatingSoonTTDuration; - } - - - /** - * returns the configured Activity TempTarget, if this is set to 0, the Default-Value is returned. - * - * @return - */ - public double determineActivityTT() { - String units = ProfileFunctions.getSystemUnits(); - double value = SP.getDouble(R.string.key_activity_target, this.getDefaultActivityTT(units)); - value = Profile.toCurrentUnits(value); - return value > 0 ? value : this.getDefaultActivityTT(units); - } - - public int determineActivityTTDuration() { - int value = SP.getInt(R.string.key_activity_duration, Constants.defaultActivityTTDuration); - return value > 0 ? value : Constants.defaultActivityTTDuration; - } - - /** - * returns the configured Hypo TempTarget, if this is set to 0, the Default-Value is returned. - * - * @return - */ - public double determineHypoTT() { - String units = ProfileFunctions.getSystemUnits(); - double value = SP.getDouble(R.string.key_hypo_target, this.getDefaultHypoTT(units)); - value = Profile.toCurrentUnits(value); - return value > 0 ? value : this.getDefaultHypoTT(units); - } - - public int determineHypoTTDuration() { - int value = SP.getInt(R.string.key_hypo_duration, Constants.defaultHypoTTDuration); - return value > 0 ? value : Constants.defaultHypoTTDuration; - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/utils/DefaultValueHelper.kt b/app/src/main/java/info/nightscout/androidaps/utils/DefaultValueHelper.kt new file mode 100644 index 0000000000..7b40d5d35b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/DefaultValueHelper.kt @@ -0,0 +1,95 @@ +package info.nightscout.androidaps.utils + +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.R +import info.nightscout.androidaps.data.Profile +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions + +object DefaultValueHelper { + /** + * returns the corresponding EatingSoon TempTarget based on the given units (MMOL / MGDL) + * + * @param units + * @return + */ + fun getDefaultEatingSoonTT(units: String): Double { + return if (Constants.MMOL == units) Constants.defaultEatingSoonTTmmol else Constants.defaultEatingSoonTTmgdl + } + + /** + * returns the corresponding Activity TempTarget based on the given units (MMOL / MGDL) + * + * @param units + * @return + */ + fun getDefaultActivityTT(units: String): Double { + return if (Constants.MMOL == units) Constants.defaultActivityTTmmol else Constants.defaultActivityTTmgdl + } + + /** + * returns the corresponding Hypo TempTarget based on the given units (MMOL / MGDL) + * + * @param units + * @return + */ + fun getDefaultHypoTT(units: String): Double { + return if (Constants.MMOL == units) Constants.defaultHypoTTmmol else Constants.defaultHypoTTmgdl + } + + /** + * returns the configured EatingSoon TempTarget, if this is set to 0, the Default-Value is returned. + * + * @return + */ + @JvmStatic + fun determineEatingSoonTT(): Double { + val units = ProfileFunctions.getSystemUnits() + var value = SP.getDouble(R.string.key_eatingsoon_target, getDefaultEatingSoonTT(units)) + value = Profile.toCurrentUnits(value) + return if (value > 0) value else getDefaultEatingSoonTT(units) + } + + @JvmStatic + fun determineEatingSoonTTDuration(): Int { + val value = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration) + return if (value > 0) value else Constants.defaultEatingSoonTTDuration + } + + /** + * returns the configured Activity TempTarget, if this is set to 0, the Default-Value is returned. + * + * @return + */ + @JvmStatic + fun determineActivityTT(): Double { + val units = ProfileFunctions.getSystemUnits() + var value = SP.getDouble(R.string.key_activity_target, getDefaultActivityTT(units)) + value = Profile.toCurrentUnits(value) + return if (value > 0) value else getDefaultActivityTT(units) + } + + @JvmStatic + fun determineActivityTTDuration(): Int { + val value = SP.getInt(R.string.key_activity_duration, Constants.defaultActivityTTDuration) + return if (value > 0) value else Constants.defaultActivityTTDuration + } + + /** + * returns the configured Hypo TempTarget, if this is set to 0, the Default-Value is returned. + * + * @return + */ + @JvmStatic + fun determineHypoTT(): Double { + val units = ProfileFunctions.getSystemUnits() + var value = SP.getDouble(R.string.key_hypo_target, getDefaultHypoTT(units)) + value = Profile.toCurrentUnits(value) + return if (value > 0) value else getDefaultHypoTT(units) + } + + @JvmStatic + fun determineHypoTTDuration(): Int { + val value = SP.getInt(R.string.key_hypo_duration, Constants.defaultHypoTTDuration) + return if (value > 0) value else Constants.defaultHypoTTDuration + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.java b/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.java deleted file mode 100644 index cee3b7ea08..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.java +++ /dev/null @@ -1,106 +0,0 @@ -package info.nightscout.androidaps.utils; - -import android.app.Activity; -import android.content.Context; -import android.content.DialogInterface; -import android.os.Handler; -import android.os.SystemClock; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.view.ContextThemeWrapper; -import android.text.Spanned; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; - -/** - * Created by mike on 31.03.2017. - */ - -public class OKDialog { - private static Logger log = LoggerFactory.getLogger(OKDialog.class); - - public static void show(final Context context, String title, String message, final Runnable runnable) { - try { - AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AppTheme)); - builder.setTitle(title); - builder.setMessage(message); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - if (runnable != null) { - SystemClock.sleep(100); - runOnUiThread(runnable); - } - } - }); - - builder.create().show(); - } catch (Exception e) { - log.debug("show_dialog exception: ", e); - } - } - - public static boolean runOnUiThread(Runnable theRunnable) { - final Handler mainHandler = new Handler(MainApp.instance().getApplicationContext().getMainLooper()); - return mainHandler.post(theRunnable); - } - - public static void show(final Activity activity, String title, Spanned message, final Runnable runnable) { - try { - AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme)); - builder.setTitle(title); - builder.setMessage(message); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - if (runnable != null) { - SystemClock.sleep(100); - activity.runOnUiThread(runnable); - } - } - }); - - builder.create().show(); - } catch (Exception e) { - log.debug("show_dialog exception: " + e); - } - } - - public static void showConfirmation(final Activity activity, String message, final Runnable runnable) { - AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme)) - .setMessage(message) - .setPositiveButton(android.R.string.ok, (dialog, which) -> { - dialog.dismiss(); - if (runnable != null) { - SystemClock.sleep(100); - activity.runOnUiThread(runnable); - } - }) - .setNegativeButton(android.R.string.cancel, null) - .show(); - } - - public static void showConfirmation(final Activity activity, String message, final Runnable ok, final Runnable cancel) { - AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme)) - .setMessage(message) - .setPositiveButton(android.R.string.ok, (dialog, which) -> { - dialog.dismiss(); - if (ok != null) { - SystemClock.sleep(100); - activity.runOnUiThread(ok); - } - }) - .setNegativeButton(android.R.string.cancel, (dialog, which) -> { - dialog.dismiss(); - if (cancel != null) { - SystemClock.sleep(100); - activity.runOnUiThread(cancel); - } - }) - .show(); - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.kt b/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.kt new file mode 100644 index 0000000000..1d22c380ca --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.kt @@ -0,0 +1,203 @@ +package info.nightscout.androidaps.utils + +import android.app.Activity +import android.content.Context +import android.content.DialogInterface +import android.os.Handler +import android.os.SystemClock +import android.text.Spanned +import android.view.LayoutInflater +import android.view.View +import android.widget.ImageView +import android.widget.TextView +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.view.ContextThemeWrapper +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R + +object OKDialog { + @JvmStatic + @JvmOverloads + fun show(context: Context, title: String, message: String, runnable: Runnable? = null) { + var notEmptytitle = title + if (notEmptytitle.isEmpty()) notEmptytitle = MainApp.gs(R.string.message) + val titleLayout = LayoutInflater.from(context).inflate(R.layout.dialog_alert_custom, null) + (titleLayout.findViewById(R.id.alertdialog_title) as TextView).text = notEmptytitle + (titleLayout.findViewById(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp) + AlertDialog.Builder(ContextThemeWrapper(context, R.style.AppTheme)) + .setCustomTitle(titleLayout) + .setMessage(message) + .setPositiveButton(MainApp.gs(R.string.ok)) { dialog: DialogInterface, _: Int -> + dialog.dismiss() + SystemClock.sleep(100) + runOnUiThread(runnable) + } + .show() + .setCanceledOnTouchOutside(false) + } + + fun runOnUiThread(theRunnable: Runnable?) { + val mainHandler = Handler(MainApp.instance().applicationContext.mainLooper) + theRunnable?.let { mainHandler.post(it) } + } + + @JvmStatic + @JvmOverloads + fun show(activity: Activity, title: String, message: Spanned, runnable: Runnable? = null) { + var notEmptytitle = title + if (notEmptytitle.isEmpty()) notEmptytitle = MainApp.gs(R.string.message) + val titleLayout = activity.layoutInflater.inflate(R.layout.dialog_alert_custom, null) + (titleLayout.findViewById(R.id.alertdialog_title) as TextView).text = notEmptytitle + (titleLayout.findViewById(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp) + AlertDialog.Builder(ContextThemeWrapper(activity, R.style.AppTheme)) + .setCustomTitle(titleLayout) + .setMessage(message) + .setPositiveButton(MainApp.gs(R.string.ok)) { dialog: DialogInterface, _: Int -> + dialog.dismiss() + SystemClock.sleep(100) + runnable?.let { activity.runOnUiThread(it) } + } + .show() + .setCanceledOnTouchOutside(false) + } + + @JvmStatic + @JvmOverloads + fun showConfirmation(activity: Activity, message: String, ok: Runnable?) { + showConfirmation(activity, MainApp.gs(R.string.confirmation), message, ok, null) + } + + @JvmStatic + @JvmOverloads + fun showConfirmation(activity: Activity, message: Spanned, ok: Runnable?) { + showConfirmation(activity, MainApp.gs(R.string.confirmation), message, ok, null) + } + + @JvmStatic + @JvmOverloads + fun showConfirmation(activity: Activity, title: String, message: Spanned, ok: Runnable?, cancel: Runnable? = null) { + val titleLayout = activity.layoutInflater.inflate(R.layout.dialog_alert_custom, null) + (titleLayout.findViewById(R.id.alertdialog_title) as TextView).text = title + (titleLayout.findViewById(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp) + AlertDialog.Builder(ContextThemeWrapper(activity, R.style.AppTheme)) + .setMessage(message) + .setCustomTitle(titleLayout) + .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> + dialog.dismiss() + SystemClock.sleep(100) + ok?.let { activity.runOnUiThread(it) } + } + .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> + dialog.dismiss() + SystemClock.sleep(100) + cancel?.let { activity.runOnUiThread(it) } + } + .setNegativeButton(android.R.string.cancel, null) + .show() + .setCanceledOnTouchOutside(false) + } + + @JvmStatic + fun showConfirmation(activity: Activity, title: String, message: String, ok: Runnable?, cancel: Runnable? = null) { + val titleLayout = activity.layoutInflater.inflate(R.layout.dialog_alert_custom, null) + (titleLayout.findViewById(R.id.alertdialog_title) as TextView).text = title + (titleLayout.findViewById(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp) + AlertDialog.Builder(ContextThemeWrapper(activity, R.style.AppTheme)) + .setMessage(message) + .setCustomTitle(titleLayout) + .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> + dialog.dismiss() + SystemClock.sleep(100) + ok?.let { activity.runOnUiThread(it) } + } + .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> + dialog.dismiss() + SystemClock.sleep(100) + cancel?.let { activity.runOnUiThread(it) } + } + .show() + .setCanceledOnTouchOutside(false) + } + + @JvmStatic + @JvmOverloads + fun showConfirmation(context: Context, message: Spanned, ok: Runnable?, cancel: Runnable? = null) { + showConfirmation(context, MainApp.gs(R.string.confirmation), message, ok, cancel) + } + + @JvmStatic + @JvmOverloads + fun showConfirmation(context: Context, title: String, message: Spanned, ok: Runnable?, cancel: Runnable? = null) { + val titleLayout = LayoutInflater.from(context).inflate(R.layout.dialog_alert_custom, null) + (titleLayout.findViewById(R.id.alertdialog_title) as TextView).text = title + (titleLayout.findViewById(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp) + AlertDialog.Builder(ContextThemeWrapper(context, R.style.AppTheme)) + .setMessage(message) + .setCustomTitle(titleLayout) + .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> + dialog.dismiss() + SystemClock.sleep(100) + runOnUiThread(ok) + } + .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> + dialog.dismiss() + SystemClock.sleep(100) + runOnUiThread(cancel) + } + .setNegativeButton(android.R.string.cancel, null) + .show() + .setCanceledOnTouchOutside(false) + } + + @JvmStatic + @JvmOverloads + fun showConfirmation(context: Context, message: String, ok: Runnable?, cancel: Runnable? = null) { + showConfirmation(context, MainApp.gs(R.string.confirmation), message, ok, cancel) + } + + @JvmStatic + @JvmOverloads + fun showConfirmation(context: Context, title: String, message: String, ok: Runnable?, cancel: Runnable? = null) { + val titleLayout = LayoutInflater.from(context).inflate(R.layout.dialog_alert_custom, null) + (titleLayout.findViewById(R.id.alertdialog_title) as TextView).text = title + (titleLayout.findViewById(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp) + AlertDialog.Builder(ContextThemeWrapper(context, R.style.AppTheme)) + .setMessage(message) + .setCustomTitle(titleLayout) + .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> + dialog.dismiss() + SystemClock.sleep(100) + runOnUiThread(ok) + } + .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> + dialog.dismiss() + SystemClock.sleep(100) + runOnUiThread(cancel) + } + .show() + .setCanceledOnTouchOutside(false) + } + + @JvmStatic + @JvmOverloads + fun showConfirmation(context: Context, title: String, message: String, ok: DialogInterface.OnClickListener?, cancel: DialogInterface.OnClickListener? = null) { + val titleLayout = LayoutInflater.from(context).inflate(R.layout.dialog_alert_custom, null) + (titleLayout.findViewById(R.id.alertdialog_title) as TextView).text = title + (titleLayout.findViewById(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp) + AlertDialog.Builder(ContextThemeWrapper(context, R.style.AppTheme)) + .setMessage(message) + .setCustomTitle(titleLayout) + .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, which: Int -> + dialog.dismiss() + SystemClock.sleep(100) + ok?.onClick(dialog, which) + } + .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, which: Int -> + dialog.dismiss() + SystemClock.sleep(100) + cancel?.onClick(dialog, which) + } + .show() + .setCanceledOnTouchOutside(false) + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/PasswordProtection.java b/app/src/main/java/info/nightscout/androidaps/utils/PasswordProtection.java index 230be97e22..6c4348bb03 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/PasswordProtection.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/PasswordProtection.java @@ -2,10 +2,10 @@ package info.nightscout.androidaps.utils; import android.app.AlertDialog; import android.content.Context; -import android.content.DialogInterface; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; +import android.widget.ImageView; import android.widget.TextView; import info.nightscout.androidaps.MainApp; @@ -18,10 +18,7 @@ import info.nightscout.androidaps.R; public class PasswordProtection { static public boolean isLocked(String preference) { final String password = SP.getString(preference, ""); - if (password.equals("")) { - return false; - } - return true; + return !password.equals(""); } static public void QueryPassword(final Context context, int stringID, String preference, final Runnable ok, final Runnable fail) { @@ -30,39 +27,38 @@ public class PasswordProtection { if (ok != null) ok.run(); return; } - LayoutInflater li = LayoutInflater.from(context); - View promptsView = li.inflate(R.layout.passwordprompt, null); + View promptsView = LayoutInflater.from(context).inflate(R.layout.passwordprompt, null); + + View titleLayout = LayoutInflater.from(context).inflate(R.layout.dialog_alert_custom, null); + ((TextView) titleLayout.findViewById(R.id.alertdialog_title)).setText(R.string.confirmation); + ((ImageView) titleLayout.findViewById(R.id.alertdialog_icon)).setImageResource(R.drawable.ic_check_while_48dp); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); alertDialogBuilder.setView(promptsView); + alertDialogBuilder.setCustomTitle(titleLayout); - final TextView label = (TextView) promptsView.findViewById(R.id.passwordprompt_text); + final TextView label = promptsView.findViewById(R.id.passwordprompt_text); label.setText(MainApp.gs(stringID)); - final EditText userInput = (EditText) promptsView.findViewById(R.id.passwordprompt_pass); + final EditText userInput = promptsView.findViewById(R.id.passwordprompt_pass); // set dialog message alertDialogBuilder .setCancelable(false) - .setPositiveButton("OK", - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog,int id) { - String enteredPassword = userInput.getText().toString(); - if (password.equals(enteredPassword)) { - if (ok != null) ok.run(); - } else { - ToastUtils.showToastInUiThread(context, MainApp.gs(R.string.wrongpassword)); - if (fail != null) fail.run(); - } + .setPositiveButton(android.R.string.ok, + (dialog, id) -> { + String enteredPassword = userInput.getText().toString(); + if (password.equals(enteredPassword)) { + if (ok != null) ok.run(); + } else { + ToastUtils.showToastInUiThread(context, MainApp.gs(R.string.wrongpassword)); + if (fail != null) fail.run(); } }) - .setNegativeButton("Cancel", - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog,int id) { - dialog.cancel(); - } - }); + .setNegativeButton(android.R.string.cancel, + (dialog, id) -> dialog.cancel()); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); + alertDialog.setCanceledOnTouchOutside(false); } } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/UIUtils.kt b/app/src/main/java/info/nightscout/androidaps/utils/UIUtils.kt new file mode 100644 index 0000000000..99c4a4af31 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/UIUtils.kt @@ -0,0 +1,10 @@ +package info.nightscout.androidaps.utils + +import android.view.View + +/** + * Created by adrian on 2019-12-20. + */ + +fun Boolean.toVisibility() = if (this) View.VISIBLE else View.GONE + diff --git a/app/src/main/java/info/nightscout/androidaps/utils/XdripCalibrations.java b/app/src/main/java/info/nightscout/androidaps/utils/XdripCalibrations.java index 404a082112..4354ccd528 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/XdripCalibrations.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/XdripCalibrations.java @@ -1,11 +1,9 @@ package info.nightscout.androidaps.utils; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ResolveInfo; import android.os.Bundle; -import androidx.appcompat.app.AlertDialog; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,20 +23,10 @@ import info.nightscout.androidaps.services.Intents; public class XdripCalibrations { private static Logger log = LoggerFactory.getLogger(XdripCalibrations.class); - public static void confirmAndSendCalibration(final Double bg, Context parentContext) { + public static void confirmAndSendCalibration(final Double bg, final Context parentContext) { if (parentContext != null) { String confirmMessage = String.format(MainApp.gs(R.string.send_calibration), bg); - - AlertDialog.Builder builder = new AlertDialog.Builder(parentContext); - builder.setTitle(MainApp.gs(R.string.confirmation)); - builder.setMessage(confirmMessage); - builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - sendIntent(bg); - } - }); - builder.setNegativeButton(MainApp.gs(R.string.cancel), null); - builder.show(); + OKDialog.showConfirmation(parentContext, confirmMessage, () -> sendIntent(bg)); } } diff --git a/app/src/main/res/drawable/ic_action_orange_48dp.xml b/app/src/main/res/drawable/ic_action_orange_48dp.xml new file mode 100644 index 0000000000..07e8c306a8 --- /dev/null +++ b/app/src/main/res/drawable/ic_action_orange_48dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_bluetooth_white_48dp.xml b/app/src/main/res/drawable/ic_bluetooth_white_48dp.xml new file mode 100644 index 0000000000..f5f2fa4797 --- /dev/null +++ b/app/src/main/res/drawable/ic_bluetooth_white_48dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_check_while_48dp.xml b/app/src/main/res/drawable/ic_check_while_48dp.xml new file mode 100644 index 0000000000..2c136685ec --- /dev/null +++ b/app/src/main/res/drawable/ic_check_while_48dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_error_red_48dp.xml b/app/src/main/res/drawable/ic_error_red_48dp.xml new file mode 100644 index 0000000000..b3666f2662 --- /dev/null +++ b/app/src/main/res/drawable/ic_error_red_48dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_trending_flat_white_48dp.xml b/app/src/main/res/drawable/ic_trending_flat_white_48dp.xml new file mode 100644 index 0000000000..a9bd780027 --- /dev/null +++ b/app/src/main/res/drawable/ic_trending_flat_white_48dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_trigger_green_48dp.xml b/app/src/main/res/drawable/ic_trigger_green_48dp.xml new file mode 100644 index 0000000000..1bfc8d3e0d --- /dev/null +++ b/app/src/main/res/drawable/ic_trigger_green_48dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/actions_fragment.xml b/app/src/main/res/layout/actions_fragment.xml index 3e809b3b02..bbf0bb59c6 100644 --- a/app/src/main/res/layout/actions_fragment.xml +++ b/app/src/main/res/layout/actions_fragment.xml @@ -1,138 +1,247 @@ - - + android:layout_height="wrap_content" + android:orientation="vertical"> - + android:layout_marginTop="15dp" + android:paddingStart="15dp" + android:paddingEnd="15dp" + android:text="@string/actions" /> + + + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/careportal_profileswitch" + + app:layout_column="0" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="0" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/careportal_temporarytarget" + + app:layout_column="1" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="0" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/overview_tempbasal_button" + + app:layout_column="0" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="1" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/canceltemp" + android:visibility="gone" + + app:layout_column="0" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="1" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/overview_extendedbolus_button" + + app:layout_column="1" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="1" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/overview_extendedbolus_cancel_button" + android:visibility="gone" + + app:layout_column="1" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="1" /> + + + + + + + + + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/primefill" + + app:layout_column="1" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="2" /> + + + + + + + + + + + + + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/nav_historybrowser" + app:layout_column="0" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="4" /> + android:paddingLeft="0dp" + android:paddingRight="0dp" + android:text="@string/danar_stats_tdd" - + app:layout_column="1" + app:layout_columnWeight="1" + app:layout_gravity="fill" + app:layout_row="4" /> - + - +
+ + diff --git a/app/src/main/res/layout/automation_action_item.xml b/app/src/main/res/layout/automation_action_item.xml index 2741f1d5ff..539ce1a400 100644 --- a/app/src/main/res/layout/automation_action_item.xml +++ b/app/src/main/res/layout/automation_action_item.xml @@ -25,11 +25,18 @@ android:id="@+id/automation_layoutText" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:orientation="vertical" + android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_toStartOf="@id/automation_iconTrash" - android:layout_alignParentStart="true"> + android:gravity="center_vertical" + android:orientation="horizontal"> + + + android:focusableInTouchMode="true" + android:minWidth="300dp" + android:orientation="vertical" + tools:context=".plugins.general.automation.dialogs.EditActionDialog"> - + android:layout_gravity="center" + android:background="@color/dialog_title_background" + android:orientation="horizontal" + android:padding="5dp"> + + + + + + + + diff --git a/app/src/main/res/layout/automation_dialog_choose_action.xml b/app/src/main/res/layout/automation_dialog_choose_action.xml index 8a2d714067..029ba36b07 100644 --- a/app/src/main/res/layout/automation_dialog_choose_action.xml +++ b/app/src/main/res/layout/automation_dialog_choose_action.xml @@ -3,26 +3,56 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog"> + android:focusableInTouchMode="true" + android:minWidth="300dp" + android:orientation="vertical" + tools:context=".plugins.general.automation.dialogs.EditEventDialog"> + android:orientation="vertical"> - + android:layout_gravity="center" + android:background="@color/dialog_title_background" + android:orientation="horizontal" + android:padding="5dp"> + + + + + + + + + android:padding="10dp"/> diff --git a/app/src/main/res/layout/automation_dialog_choose_trigger.xml b/app/src/main/res/layout/automation_dialog_choose_trigger.xml index 8589096af7..11b874b3eb 100644 --- a/app/src/main/res/layout/automation_dialog_choose_trigger.xml +++ b/app/src/main/res/layout/automation_dialog_choose_trigger.xml @@ -3,26 +3,49 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog"> + android:focusableInTouchMode="true" + android:minWidth="300dp" + android:orientation="vertical" + tools:context=".plugins.general.automation.dialogs.EditEventDialog"> + android:orientation="vertical"> - + android:layout_gravity="center" + android:background="@color/dialog_title_background" + android:orientation="horizontal" + android:padding="5dp"> + + + + + + + android:padding="10dp"/> diff --git a/app/src/main/res/layout/automation_dialog_edit_trigger.xml b/app/src/main/res/layout/automation_dialog_edit_trigger.xml index 594a78055e..a30a54ce4b 100644 --- a/app/src/main/res/layout/automation_dialog_edit_trigger.xml +++ b/app/src/main/res/layout/automation_dialog_edit_trigger.xml @@ -3,27 +3,49 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog"> + android:focusableInTouchMode="true" + android:minWidth="300dp" + android:orientation="vertical" + tools:context=".plugins.general.automation.dialogs.EditEventDialog"> + android:orientation="vertical"> - + android:layout_gravity="center" + android:background="@color/dialog_title_background" + android:orientation="horizontal" + android:padding="5dp"> + + + + + + diff --git a/app/src/main/res/layout/automation_dialog_event.xml b/app/src/main/res/layout/automation_dialog_event.xml index d440b8cf27..03ff617168 100644 --- a/app/src/main/res/layout/automation_dialog_event.xml +++ b/app/src/main/res/layout/automation_dialog_event.xml @@ -1,112 +1,171 @@ + android:focusableInTouchMode="true" + android:minWidth="300dp" + android:orientation="vertical" + tools:context=".plugins.general.automation.dialogs.EditEventDialog"> - - - - + android:orientation="vertical"> + android:layout_height="wrap_content" + android:layout_gravity="center" + android:background="@color/dialog_title_background" + android:orientation="horizontal" + android:padding="5dp"> - + android:contentDescription="@string/automation" + android:src="@drawable/ic_action_orange_48dp" /> + android:layout_centerInParent="true" + android:layout_gravity="center" + android:layout_marginLeft="10dp" + android:layout_marginRight="10dp" + android:text="@string/automation_event" + android:textAlignment="center" + android:textAppearance="?android:attr/textAppearanceLarge" /> - - - + android:orientation="horizontal" + android:padding="5dp" /> - + android:padding="10dp" + android:orientation="vertical"> - + - + + + + + + + + + + + + android:layout_marginTop="10dp" + android:layout_marginBottom="10dp" + android:orientation="vertical" /> - + - + - + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/automation_fragment.xml b/app/src/main/res/layout/automation_fragment.xml index 2f24e1b70c..57ba8c60e0 100644 --- a/app/src/main/res/layout/automation_fragment.xml +++ b/app/src/main/res/layout/automation_fragment.xml @@ -18,6 +18,7 @@ android:id="@+id/automation_logView" android:layout_width="match_parent" android:layout_height="100dp" + android:scrollbars = "vertical" android:layout_alignParentBottom="true" /> diff --git a/app/src/main/res/layout/careportal_newnstreatment_dialog.xml b/app/src/main/res/layout/careportal_newnstreatment_dialog.xml index fc0fa1ea49..4251efd3a2 100644 --- a/app/src/main/res/layout/careportal_newnstreatment_dialog.xml +++ b/app/src/main/res/layout/careportal_newnstreatment_dialog.xml @@ -544,7 +544,7 @@ android:layout_gravity="center_horizontal" android:width="120dp" android:padding="10dp" - android:text="@string/careportal_newnstreatment_eventtime_label" + android:text="@string/event_time_label" android:textAppearance="@android:style/TextAppearance.Material.Small" android:textStyle="bold" /> diff --git a/app/src/main/res/layout/close.xml b/app/src/main/res/layout/close.xml index a500295275..682cf2bc22 100644 --- a/app/src/main/res/layout/close.xml +++ b/app/src/main/res/layout/close.xml @@ -1,25 +1,11 @@ - @@ -27,8 +13,7 @@ android:id="@+id/close" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginRight="16dp" android:layout_marginEnd="16dp" style="@style/mdtp_ActionButton.Text" - android:text="@string/close_navigation" /> + android:text="@string/close" /> diff --git a/app/src/main/res/layout/danar_historyactivity.xml b/app/src/main/res/layout/danar_historyactivity.xml index 3324a98029..2693384c10 100644 --- a/app/src/main/res/layout/danar_historyactivity.xml +++ b/app/src/main/res/layout/danar_historyactivity.xml @@ -3,50 +3,74 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" - android:paddingBottom="@dimen/activity_vertical_margin" - android:paddingTop="@dimen/activity_vertical_margin" tools:context=".plugins.pump.danaR.activities.DanaRHistoryActivity"> - + + + + + + + + + + - - - + android:layout_marginEnd="20dp" /> @@ -54,23 +78,15 @@ android:id="@+id/danar_history_recyclerview" android:layout_width="match_parent" android:layout_height="fill_parent" - android:layout_above="@+id/danar_historybuttons" - android:layout_below="@+id/danar_historystatus" /> + android:layout_below="@+id/danar_history_status" /> - - -