11package de.michelinside.glucodataauto
22
3+ import android.annotation.SuppressLint
34import android.app.Activity
4- import android.app.AlarmManager
55import android.content.Context
66import android.content.Intent
77import android.content.SharedPreferences
@@ -46,15 +46,15 @@ import de.michelinside.glucodatahandler.common.utils.GitHubVersionChecker
4646import de.michelinside.glucodatahandler.common.utils.Utils
4747import de.michelinside.glucodatahandler.common.ui.Dialogs
4848import de.michelinside.glucodatahandler.common.utils.TextToSpeechUtils
49- import java.text.DateFormat
50- import java.util.Date
5149import de.michelinside.glucodatahandler.common.R as CR
5250
5351class MainActivity : AppCompatActivity (), NotifierInterface {
5452 private lateinit var txtBgValue: TextView
5553 private lateinit var viewIcon: ImageView
5654 private lateinit var timeText: TextView
5755 private lateinit var deltaText: TextView
56+ private lateinit var iobText: TextView
57+ private lateinit var cobText: TextView
5858 private lateinit var txtLastValue: TextView
5959 private lateinit var txtVersion: TextView
6060 private lateinit var tableDetails: TableLayout
@@ -79,6 +79,8 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
7979 viewIcon = findViewById(R .id.viewIcon)
8080 timeText = findViewById(R .id.timeText)
8181 deltaText = findViewById(R .id.deltaText)
82+ iobText = findViewById(R .id.iobText)
83+ cobText = findViewById(R .id.cobText)
8284 txtLastValue = findViewById(R .id.txtLastValue)
8385 btnSources = findViewById(R .id.btnSources)
8486 tableConnections = findViewById(R .id.tableConnections)
@@ -165,6 +167,7 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
165167
166168 GlucoDataServiceAuto .startDataSync()
167169 versionChecker.checkVersion(1 )
170+ checkNewSettings()
168171 } catch (exc: Exception ) {
169172 Log .e(LOG_ID , " onResume exception: " + exc.message.toString() )
170173 }
@@ -178,27 +181,12 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
178181 return false
179182 }
180183 }
181- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
182- val alarmManager = this .getSystemService(Context .ALARM_SERVICE ) as AlarmManager
183- if (! alarmManager.canScheduleExactAlarms()) {
184- Log .i(LOG_ID , " Request exact alarm permission..." )
185- startActivity(Intent (Settings .ACTION_REQUEST_SCHEDULE_EXACT_ALARM ))
186- }
187- }
188184 requestExactAlarmPermission()
189185 return true
190186 }
191187
192- private fun canScheduleExactAlarms (): Boolean {
193- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
194- val alarmManager = this .getSystemService(Context .ALARM_SERVICE ) as AlarmManager
195- return alarmManager.canScheduleExactAlarms()
196- }
197- return true
198- }
199-
200188 private fun requestExactAlarmPermission () {
201- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S && ! canScheduleExactAlarms()) {
189+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S && ! Utils . canScheduleExactAlarms(this )) {
202190 Log .i(LOG_ID , " Request exact alarm permission..." )
203191 val builder: AlertDialog .Builder = AlertDialog .Builder (this )
204192 builder
@@ -215,6 +203,49 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
215203 }
216204 }
217205
206+ private fun checkNewSettings () {
207+ try {
208+ if (! sharedPref.contains(Constants .SHARED_PREF_DISCLAIMER_SHOWN )) {
209+ Dialogs .showOkDialog(this ,
210+ CR .string.gdh_disclaimer_title,
211+ CR .string.gdh_disclaimer_message,
212+ null
213+ )
214+ with (sharedPref.edit()) {
215+ putString(Constants .SHARED_PREF_DISCLAIMER_SHOWN , BuildConfig .VERSION_NAME )
216+ apply ()
217+ }
218+ }
219+ if (! sharedPref.contains(Constants .SHARED_PREF_LIBRE_AUTO_ACCEPT_TOU )) {
220+ if (sharedPref.getBoolean(Constants .SHARED_PREF_LIBRE_ENABLED , false )) {
221+ Dialogs .showOkCancelDialog(this ,
222+ resources.getString(CR .string.src_cat_libreview),
223+ resources.getString(CR .string.src_libre_tou_message),
224+ { _, _ ->
225+ with (sharedPref.edit()) {
226+ putBoolean(Constants .SHARED_PREF_LIBRE_AUTO_ACCEPT_TOU , true )
227+ apply ()
228+ }
229+ },
230+ { _, _ ->
231+ with (sharedPref.edit()) {
232+ putBoolean(Constants .SHARED_PREF_LIBRE_AUTO_ACCEPT_TOU , false )
233+ apply ()
234+ }
235+ })
236+ } else {
237+ with (sharedPref.edit()) {
238+ putBoolean(Constants .SHARED_PREF_LIBRE_AUTO_ACCEPT_TOU , true )
239+ apply ()
240+ }
241+ }
242+ }
243+
244+ } catch (exc: Exception ) {
245+ Log .e(LOG_ID , " checkNewSettings exception: " + exc.message.toString() )
246+ }
247+ }
248+
218249 override fun onCreateOptionsMenu (menu : Menu ? ): Boolean {
219250 try {
220251 Log .v(LOG_ID , " onCreateOptionsMenu called" )
@@ -311,6 +342,14 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
311342 startActivity(mailIntent)
312343 return true
313344 }
345+ R .id.action_google_groups -> {
346+ val browserIntent = Intent (
347+ Intent .ACTION_VIEW ,
348+ Uri .parse(resources.getText(CR .string.google_gdh_group_url).toString())
349+ )
350+ startActivity(browserIntent)
351+ return true
352+ }
314353 R .id.action_facebook -> {
315354 val browserIntent = Intent (
316355 Intent .ACTION_VIEW ,
@@ -355,6 +394,7 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
355394 return super .onOptionsItemSelected(item)
356395 }
357396
397+ @SuppressLint(" SetTextI18n" )
358398 private fun update () {
359399 try {
360400 Log .v(LOG_ID , " update values" )
@@ -370,6 +410,12 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
370410 timeText.text = " 🕒 ${ReceiveData .getElapsedRelativeTimeAsString(this )} "
371411 timeText.contentDescription = ReceiveData .getElapsedRelativeTimeAsString(this , true )
372412 deltaText.text = " Δ ${ReceiveData .getDeltaAsString()} "
413+ iobText.text = " 💉 " + ReceiveData .getIobAsString()
414+ iobText.contentDescription = getString(CR .string.info_label_iob) + " " + ReceiveData .getIobAsString()
415+ iobText.visibility = if (ReceiveData .isIobCobObsolete()) View .GONE else View .VISIBLE
416+ cobText.text = " 🍔 " + ReceiveData .getCobAsString()
417+ cobText.contentDescription = getString(CR .string.info_label_cob) + " " + ReceiveData .getCobAsString()
418+ cobText.visibility = iobText.visibility
373419
374420 if (ReceiveData .time == 0L ) {
375421 txtLastValue.visibility = View .VISIBLE
@@ -394,17 +440,41 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
394440
395441 private fun updateConnectionsTable () {
396442 tableConnections.removeViews(1 , maxOf(0 , tableConnections.childCount - 1 ))
397- if (SourceStateData .lastState != SourceState .NONE )
398- tableConnections.addView(createRow(
399- SourceStateData .lastSource.resId,
400- SourceStateData .getStateMessage(this )))
443+ if (SourceStateData .lastState != SourceState .NONE ) {
444+ val msg = SourceStateData .getStateMessage(this )
445+ tableConnections.addView(createRow(SourceStateData .lastSource.resId,msg))
446+ if (SourceStateData .lastState == SourceState .ERROR && SourceStateData .lastSource == DataSource .DEXCOM_SHARE ) {
447+ if (msg.contains(" 500:" )) { // invalid password
448+ val us_account = sharedPref.getBoolean(Constants .SHARED_PREF_DEXCOM_SHARE_USE_US_URL , false )
449+ val browserIntent = Intent (
450+ Intent .ACTION_VIEW ,
451+ Uri .parse(resources.getString(if (us_account)CR .string.dexcom_account_us_url else CR .string.dexcom_account_non_us_url))
452+ )
453+ val onClickListener = View .OnClickListener {
454+ startActivity(browserIntent)
455+ }
456+ tableConnections.addView(
457+ createRow(
458+ SourceStateData .lastSource.resId,
459+ resources.getString(if (us_account) CR .string.dexcom_share_check_us_account else CR .string.dexcom_share_check_non_us_account),
460+ onClickListener
461+ )
462+ )
463+ }
464+ }
465+ if (SourceStateData .lastErrorInfo.isNotEmpty()) {
466+ // add error specific information in an own row
467+ tableConnections.addView(createRow(SourceStateData .lastErrorInfo))
468+ }
469+ tableConnections.addView(createRow(CR .string.request_timestamp, Utils .getUiTimeStamp(SourceStateData .lastStateTime)))
470+ }
401471 tableConnections.addView(createRow(CR .string.pref_cat_android_auto, if (GlucoDataServiceAuto .connected) resources.getString(CR .string.connected_label) else resources.getString(CR .string.disconnected_label)))
402472 checkTableVisibility(tableConnections)
403473 }
404474
405475 private fun updateNotesTable () {
406476 tableNotes.removeViews(1 , maxOf(0 , tableNotes.childCount - 1 ))
407- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S && ! canScheduleExactAlarms()) {
477+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S && ! Utils . canScheduleExactAlarms(this )) {
408478 Log .w(LOG_ID , " Schedule exact alarm is not active!!!" )
409479 val onClickListener = View .OnClickListener {
410480 startActivity(Intent (Settings .ACTION_REQUEST_SCHEDULE_EXACT_ALARM ))
@@ -469,16 +539,20 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
469539 tableAlarms.addView(createRow(CR .string.info_label_alarm, resources.getString(deltaAlarmType.resId)))
470540 }
471541 if (AlarmHandler .isSnoozeActive)
472- tableAlarms.addView(createRow(CR .string.snooze , AlarmHandler .snoozeTimestamp))
542+ tableAlarms.addView(createRow(CR .string.snooze_until , AlarmHandler .snoozeTimestamp))
473543 checkTableVisibility(tableAlarms)
474544 }
475545
476546 private fun updateDetailsTable () {
477547 tableDetails.removeViews(1 , maxOf(0 , tableDetails.childCount - 1 ))
548+ if (! GlucoDataServiceAuto .patientName.isNullOrEmpty()) {
549+ tableDetails.addView(createRow(CR .string.patient_name, GlucoDataServiceAuto .patientName!! ))
550+ }
551+
478552 if (ReceiveData .time > 0 ) {
479553 if (ReceiveData .isMmol)
480554 tableDetails.addView(createRow(CR .string.info_label_raw, " ${ReceiveData .rawValue} mg/dl" ))
481- tableDetails.addView(createRow(CR .string.info_label_timestamp, DateFormat .getTimeInstance( DateFormat . DEFAULT ).format( Date ( ReceiveData .time) )))
555+ tableDetails.addView(createRow(CR .string.info_label_timestamp, Utils .getUiTimeStamp( ReceiveData .time)))
482556 if (ReceiveData .sensorID?.isNotEmpty() == true ) {
483557 tableDetails.addView(createRow(CR .string.info_label_sensor_id, if (BuildConfig .DEBUG ) " ABCDE12345" else ReceiveData .sensorID!! ))
484558 }
@@ -520,12 +594,12 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
520594 return row
521595 }
522596
523- private fun createRow (key : String , onClickListener : View .OnClickListener ? = null) : TableRow {
597+ private fun createRow (value : String , onClickListener : View .OnClickListener ? = null) : TableRow {
524598 val row = TableRow (this )
525599 row.weightSum = 1f
526600 // row.setBackgroundColor(resources.getColor(R.color.table_row))
527601 row.setPadding(Utils .dpToPx(5F , this ))
528- row.addView(createColumn(key , false , onClickListener))
602+ row.addView(createColumn(value , false , onClickListener))
529603 return row
530604 }
531605
0 commit comments