Skip to content

Commit

Permalink
Merge pull request #97 from pachi81/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pachi81 committed Oct 10, 2023
2 parents bde3066 + e834ede commit ff81422
Show file tree
Hide file tree
Showing 24 changed files with 241 additions and 126 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

project.ext.set("versionCode", 1)
project.ext.set("versionName", "0.9.5")
project.ext.set("versionName", "0.9.6")
project.ext.set("compileSdk", 33)
project.ext.set("targetSdk", 33)
project.ext.set("minSdk", 26)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ object Constants {
const val SHARED_PREF_FLOATING_WIDGET = "floating_widget"
const val SHARED_PREF_FLOATING_WIDGET_STYLE = "floating_widget_style"
const val SHARED_PREF_FLOATING_WIDGET_SIZE = "floating_widget_size"
const val SHARED_PREF_FLOATING_WIDGET_TRANSPARENCY = "floating_widget_transparency"
const val SHARED_PREF_WIDGET_TRANSPARENCY = "widget_transparency"
const val SHARED_PREF_RELATIVE_TIME = "relative_time"

// internal app preferences (not changed by settings) -> use separate tag for not trigger onChanged events
Expand All @@ -63,4 +65,8 @@ object Constants {
const val SHARED_PREF_NOTIFICATION = "notification"
const val SHARED_PREF_FOREGROUND_SERVICE = "foreground_service"
const val SHARED_PREF_WEAR_COLORED_AOD = "colored_aod"



const val SHARED_PREF_DUMMY_VALUES = "dummy_values"
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,14 @@ abstract class GlucoDataService(source: AppSource) : WearableListenerService(),
xDripReceiver = XDripBroadcastReceiver()
registerReceiver(xDripReceiver,IntentFilter("com.eveningoutpost.dexdrip.BgEstimate"))

if (BuildConfig.DEBUG && sharedPref!!.getBoolean(Constants.SHARED_PREF_NOTIFICATION, false)) {
if (BuildConfig.DEBUG && sharedPref!!.getBoolean(Constants.SHARED_PREF_DUMMY_VALUES, false)) {
Thread {
try {
ReceiveData.time = 0L
while (true) {
// create Thread which send dummy intents
this.sendBroadcast(Utils.getDummyGlucodataIntent(true))
Thread.sleep(40000)
this.sendBroadcast(Utils.getDummyGlucodataIntent(false))
Thread.sleep(1000)
}
} catch (exc: Exception) {
Log.e(LOG_ID, "Send dummy glucodata exception: " + exc.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ object Utils {
)
}

fun getBackgroundColor(transparancyFactor: Int) : Int {
if( transparancyFactor <= 0 )
return 0
val transparency = 255 * minOf(10, transparancyFactor) / 10
return transparency shl (4*6)
}


private fun isShortText(text: String): Boolean = text.length <= (if (text.contains(".")) 3 else 2)

private fun calcMaxTextSizeForBitmap(bitmap: Bitmap, text: String, roundTarget: Boolean, maxTextSize: Float, top: Boolean, bold: Boolean): Float {
Expand Down Expand Up @@ -230,13 +238,13 @@ object Utils {
textSize -= textSize*0.05F
degrees = 0
shortArrowRate = 0.7F
} else if (rate >= 0F) {
} else {
text = ""
degrees = round(abs(minOf(2F, rate)-2F) * 90F/2F, 0).toInt()
} else { // < 0
degrees = round(abs(maxOf(-2F, minOf(2F, rate))-2F) * 90F/2F, 0).toInt()
} /*else { // < 0
text = "↓"
degrees = round((maxOf(-2F, rate) + 2F) * -90F/2F, 0).toInt()
}
}*/
textSize *= resizeFactor
if (GlucoDataService.sharedPref != null && !GlucoDataService.sharedPref!!.getBoolean(Constants.SHARED_PREF_LARGE_ARROW_ICON, true)) {
textSize *= shortArrowRate
Expand Down Expand Up @@ -283,34 +291,46 @@ object Utils {
}
}

private var rateDelta = 0.5F
private var rateDelta = 0.1F
private var rawDelta = 5
fun getDummyGlucodataIntent(random: Boolean = true) : Intent {
val useMmol = ReceiveData.isMmol
val time = if (!random) System.currentTimeMillis() else if (ReceiveData.time < System.currentTimeMillis()) System.currentTimeMillis() + 1000 else ReceiveData.time + 1000
val first = ReceiveData.time == 0L
ReceiveData.time = System.currentTimeMillis()-60000
val time = System.currentTimeMillis()
val intent = Intent(Constants.GLUCODATA_BROADCAST_ACTION)
var raw: Int
var glucose: Float
val rate: Float
var rate: Float
if (random) {
raw = Random.nextInt(40, 400)
glucose = if(useMmol) mgToMmol(raw.toFloat()) else raw.toFloat()
rate = round(Random.nextFloat() + Random.nextInt(-4, 4).toFloat(), 2)
} else {
if ((ReceiveData.rawValue == 200 && rawDelta > 0) || (ReceiveData.rawValue == 40 && rawDelta < 0)) {
if ((ReceiveData.rawValue >= 200 && rawDelta > 0) || (ReceiveData.rawValue <= 40 && rawDelta < 0)) {
rawDelta *= -1
}
raw =
if (ReceiveData.time == 0L || ReceiveData.rawValue == 400) Constants.GLUCOSE_MIN_VALUE else ReceiveData.rawValue + rawDelta
if (first || ReceiveData.rawValue == 400) Constants.GLUCOSE_MIN_VALUE else ReceiveData.rawValue + rawDelta
glucose = if (useMmol) mgToMmol(raw.toFloat()) else raw.toFloat()
if (useMmol && glucose == ReceiveData.glucose) {
raw += 1
glucose = mgToMmol(raw.toFloat())
}
if ((ReceiveData.rate >= 3.5F && rateDelta > 0F) || (ReceiveData.rate <= -3.5F && rateDelta < 0F)) {
rateDelta *= -1F
if (ReceiveData.rate >= 3.5F) {
rateDelta = -0.1F
rate = 2F
} else if (ReceiveData.rate <= -3.5F) {
rateDelta = 0.1F
rate = -2F
} else {
rate = if (first) -3.5F else ReceiveData.rate + rateDelta
}
rate = if (ReceiveData.time == 0L) -3.5F else ReceiveData.rate + rateDelta
if (rate > 2F && rateDelta > 0)
rate = 3.5F
else if (rate < -2F && rateDelta < 0)
rate = -3.5F

}
intent.putExtra(ReceiveData.SERIAL, "WUSEL_DUSEL")
intent.putExtra(ReceiveData.MGDL, raw)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP

if (BuildConfig.DEBUG) {
val notifySwitch =
findPreference<SwitchPreferenceCompat>(Constants.SHARED_PREF_NOTIFICATION)
findPreference<SwitchPreferenceCompat>(Constants.SHARED_PREF_DUMMY_VALUES)
notifySwitch!!.isVisible = true
}

Expand Down Expand Up @@ -140,6 +140,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP
setEnableState<ListPreference>(sharedPreferences, Constants.SHARED_PREF_SECOND_PERMANENT_NOTIFICATION_ICON, Constants.SHARED_PREF_PERMANENT_NOTIFICATION, Constants.SHARED_PREF_SECOND_PERMANENT_NOTIFICATION)
setEnableState<SeekBarPreference>(sharedPreferences, Constants.SHARED_PREF_FLOATING_WIDGET_SIZE, Constants.SHARED_PREF_FLOATING_WIDGET)
setEnableState<ListPreference>(sharedPreferences, Constants.SHARED_PREF_FLOATING_WIDGET_STYLE, Constants.SHARED_PREF_FLOATING_WIDGET)
setEnableState<ListPreference>(sharedPreferences, Constants.SHARED_PREF_FLOATING_WIDGET_TRANSPARENCY, Constants.SHARED_PREF_FLOATING_WIDGET)
} catch (exc: Exception) {
Log.e(LOG_ID, "updateEnableStates exception: " + exc.toString())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,66 @@
package de.michelinside.glucodatahandler.widget

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import de.michelinside.glucodatahandler.common.Constants
import de.michelinside.glucodatahandler.common.GlucoDataService
import de.michelinside.glucodatahandler.common.notifier.InternalNotifier
import de.michelinside.glucodatahandler.common.notifier.NotifierInterface
import de.michelinside.glucodatahandler.common.notifier.NotifyDataSource

object ActiveWidgetHandler: NotifierInterface {
object ActiveWidgetHandler: NotifierInterface, SharedPreferences.OnSharedPreferenceChangeListener {
private const val LOG_ID = "GlucoDataHandler.widget.ActiveWidgetHandler"
private var activeWidgets = mutableSetOf<WidgetType>()

init {
Log.d(LOG_ID, "init called")
}

fun addWidget(type: WidgetType) {
if(!activeWidgets.contains(type)) {
activeWidgets.add(type)
Log.i(LOG_ID, "add widget " + type.toString() + " new size " + activeWidgets.size)
if (activeWidgets.size == 1 || type == WidgetType.GLUCOSE_TREND_DELTA_TIME) {
fun addWidget(type: WidgetType, context: Context) {
try {
if(!activeWidgets.contains(type)) {
activeWidgets.add(type)
Log.i(LOG_ID, "add widget " + type.toString() + " new size " + activeWidgets.size)
if (activeWidgets.size == 1 || type == WidgetType.GLUCOSE_TREND_DELTA_TIME) {
val filter = mutableSetOf(
NotifyDataSource.BROADCAST,
NotifyDataSource.MESSAGECLIENT,
NotifyDataSource.SETTINGS,
NotifyDataSource.OBSOLETE_VALUE
) // to trigger re-start for the case of stopped by the system
if(activeWidgets.contains(WidgetType.GLUCOSE_TREND_DELTA_TIME))
filter.add(NotifyDataSource.TIME_VALUE)
InternalNotifier.addNotifier(this, filter)
}
if (activeWidgets.size == 1)
context.getSharedPreferences(Constants.SHARED_PREF_TAG, Context.MODE_PRIVATE).registerOnSharedPreferenceChangeListener(this)
}
} catch (exc: Exception) {
Log.e(LOG_ID, "Exception in addWidget: " + exc.toString())
}
}

fun remWidget(type: WidgetType, context: Context) {
try {
activeWidgets.remove(type)
Log.i(LOG_ID, "remove widget " + type.toString() + " new size " + activeWidgets.size)
if (activeWidgets.isEmpty()) {
InternalNotifier.remNotifier(this)
context.getSharedPreferences(Constants.SHARED_PREF_TAG, Context.MODE_PRIVATE).unregisterOnSharedPreferenceChangeListener(this)
} else if (type == WidgetType.GLUCOSE_TREND_DELTA_TIME) {
// re-add filter to remove TIME_VALUE
val filter = mutableSetOf(
NotifyDataSource.BROADCAST,
NotifyDataSource.MESSAGECLIENT,
NotifyDataSource.SETTINGS,
NotifyDataSource.OBSOLETE_VALUE
) // to trigger re-start for the case of stopped by the system
if(activeWidgets.contains(WidgetType.GLUCOSE_TREND_DELTA_TIME))
filter.add(NotifyDataSource.TIME_VALUE)
)
InternalNotifier.addNotifier(this, filter)
}
}
}

fun remWidget(type: WidgetType) {
activeWidgets.remove(type)
Log.i(LOG_ID, "remove widget " + type.toString() + " new size " + activeWidgets.size)
if (activeWidgets.isEmpty()) {
InternalNotifier.remNotifier(this)
} else if (type == WidgetType.GLUCOSE_TREND_DELTA_TIME) {
// re-add filter to remove TIME_VALUE
val filter = mutableSetOf(
NotifyDataSource.BROADCAST,
NotifyDataSource.MESSAGECLIENT,
NotifyDataSource.SETTINGS,
NotifyDataSource.OBSOLETE_VALUE
)
InternalNotifier.addNotifier(this, filter)
} catch (exc: Exception) {
Log.e(LOG_ID, "Exception in remWidget: " + exc.toString())
}
}

Expand All @@ -57,4 +71,19 @@ object ActiveWidgetHandler: NotifierInterface {
GlucoseBaseWidget.updateWidgets(context, it)
}
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
try {
Log.d(LOG_ID, "onSharedPreferenceChanged called for key " + key)
if (GlucoDataService.context != null) {
when (key) {
Constants.SHARED_PREF_WIDGET_TRANSPARENCY -> {
OnNotifyData(GlucoDataService.context!!, NotifyDataSource.SETTINGS, null)
}
}
}
} catch (exc: Exception) {
Log.e(LOG_ID, "onSharedPreferenceChanged exception: " + exc.toString() )
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class FloatingWidget(val context: Context) : NotifierInterface, SharedPreference
windowManager!!.addView(floatingView, params)

val widget = floatingView.findViewById<View>(R.id.widget)
widget.setBackgroundColor(Utils.getBackgroundColor(sharedPref.getInt(Constants.SHARED_PREF_FLOATING_WIDGET_TRANSPARENCY, 3)))
widget.setOnClickListener {
Log.d(LOG_ID, "onClick called")
var launchIntent: Intent? =
Expand Down Expand Up @@ -242,7 +243,8 @@ class FloatingWidget(val context: Context) : NotifierInterface, SharedPreference
when(key) {
Constants.SHARED_PREF_FLOATING_WIDGET,
Constants.SHARED_PREF_FLOATING_WIDGET_SIZE,
Constants.SHARED_PREF_FLOATING_WIDGET_STYLE -> {
Constants.SHARED_PREF_FLOATING_WIDGET_STYLE,
Constants.SHARED_PREF_FLOATING_WIDGET_TRANSPARENCY -> {
remove()
update()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ abstract class GlucoseBaseWidget(private val type: WidgetType,
// Enter relevant functionality for when the first widget is created
Log.d(LOG_ID, "onEnabled called for " + this.toString())
GlucoDataServiceMobile.start(context)
ActiveWidgetHandler.addWidget(type)
ActiveWidgetHandler.addWidget(type, context)
} catch (exc: Exception) {
Log.e(LOG_ID, "Exception in onEnabled: " + exc.message.toString())
}
Expand All @@ -124,7 +124,7 @@ abstract class GlucoseBaseWidget(private val type: WidgetType,
try {
// Enter relevant functionality for when the last widget is disabled
Log.d(LOG_ID, "onDisabled calledd for " + this.toString())
ActiveWidgetHandler.remWidget(type)
ActiveWidgetHandler.remWidget(type, context)
} catch (exc: Exception) {
Log.e(LOG_ID, "Exception in onDisabled: " + exc.message.toString())
}
Expand Down Expand Up @@ -178,6 +178,9 @@ abstract class GlucoseBaseWidget(private val type: WidgetType,
val layout = if (shortWidget) getShortLayout() else if (longWidget) getLongLayout() else getLayout()
val remoteViews = RemoteViews(context.packageName, layout)

val sharedPref = context.getSharedPreferences(Constants.SHARED_PREF_TAG, Context.MODE_PRIVATE)
remoteViews.setInt(R.id.widget, "setBackgroundColor", Utils.getBackgroundColor(sharedPref.getInt(Constants.SHARED_PREF_WIDGET_TRANSPARENCY, 3)))

if (!hasTrend || !shortWidget) {
// short widget with trend, using the glucose+trend image
remoteViews.setTextViewText(R.id.glucose, ReceiveData.getClucoseAsString())
Expand Down
14 changes: 10 additions & 4 deletions mobile/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,20 @@
android:layout_marginEnd="20dp"
android:text="@string/no_data"
android:textAlignment="center"
android:textSize="20sp"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/layout_glucose_trend" />
app:layout_constraintTop_toBottomOf="@+id/layout_glucose_trend"
tools:ignore="SpUsage" />

<TextView
android:id="@+id/txtWearInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/activity_main_disconnected_label"
android:textSize="14dp"
tools:ignore="SpUsage"
app:layout_constraintBottom_toTopOf="@+id/txtCarInfo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Expand All @@ -79,7 +82,9 @@
android:text="@string/activity_main_car_disconnected_label"
app:layout_constraintBottom_toTopOf="@+id/txtVersion"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintStart_toStartOf="parent"
android:textSize="14dp"
tools:ignore="SpUsage" />

<TextView
android:id="@+id/txtVersion"
Expand All @@ -90,6 +95,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="HardcodedText" />
android:textSize="14dp"
tools:ignore="HardcodedText,SpUsage" />

</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit ff81422

Please sign in to comment.