Skip to content

Commit

Permalink
Merge pull request #148 from pachi81/vNext
Browse files Browse the repository at this point in the history
vNext
  • Loading branch information
pachi81 authored Apr 18, 2024
2 parents dba2844 + ba10c7c commit d6cc2d9
Show file tree
Hide file tree
Showing 50 changed files with 1,242 additions and 109 deletions.
5 changes: 2 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions auto/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ android {
resValue "string", "app_name", "GlucoDataAuto"
}
dev_release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-dev-rules.pro'
versionNameSuffix '-dev'
resValue "string", "app_name", "GlucoDataAuto"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class GlucoDataServiceAuto: Service() {
Log.d(LOG_ID, "onStartCommand called")
super.onStartCommand(intent, flags, startId)
val isForeground = intent.getBooleanExtra(Constants.SHARED_PREF_FOREGROUND_SERVICE, false)
if (isForeground && !isForegroundService && Utils.checkPermission(this, android.Manifest.permission.POST_NOTIFICATIONS, Build.VERSION_CODES.TIRAMISU)) {
if (isForeground && !isForegroundService) {
Log.i(LOG_ID, "Starting service in foreground!")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
startForeground(NOTIFICATION_ID, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
InternalNotifier.addNotifier( this, this, mutableSetOf(
NotifySource.BROADCAST,
NotifySource.IOB_COB_CHANGE,
NotifySource.IOB_COB_TIME,
NotifySource.MESSAGECLIENT,
NotifySource.CAPILITY_INFO,
NotifySource.NODE_BATTERY_LEVEL,
Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.1.2' apply false
id 'com.android.library' version '8.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
id 'com.android.application' version '8.3.1' apply false
id 'com.android.library' version '8.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.9.23' apply false
}

project.ext.set("versionCode", 26)
project.ext.set("versionName", "0.9.11")
project.ext.set("versionCode", 30)
project.ext.set("versionName", "0.9.12")
project.ext.set("compileSdk", 34)
project.ext.set("targetSdk", 33)
project.ext.set("minSdk", 26)
Expand Down
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
dev_release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-dev-rules.pro'
}
}
compileOptions {
Expand Down
32 changes: 32 additions & 0 deletions common/proguard-dev-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-dontwarn **
-keep class **
-keepclassmembers class *{*;}
-keepattributes *

# --------------------------------------------------------------------
# REMOVE all debug log messages
# --------------------------------------------------------------------
-assumenosideeffects class android.util.Log {
public static *** v(...);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ object Constants {
const val SHARED_PREF_COLOR_OK = "color_ok"
//const val SHARED_PREF_PERMANENT_NOTIFICATION = "permanent_notification"
const val SHARED_PREF_PERMANENT_NOTIFICATION_ICON = "status_bar_notification_icon"
const val SHARED_PREF_PERMANENT_NOTIFICATION_COLORED_ICON = "status_bar_notification_colored_icon"
const val SHARED_PREF_PERMANENT_NOTIFICATION_EMPTY = "permanent_notification_empty"
const val SHARED_PREF_SECOND_PERMANENT_NOTIFICATION = "second_permanent_notification"
const val SHARED_PREF_SECOND_PERMANENT_NOTIFICATION_ICON = "second_status_bar_notification_icon"
Expand Down Expand Up @@ -105,6 +106,7 @@ object Constants {
const val SHARED_PREF_LIBRE_TOKEN="source_libre_token"
const val SHARED_PREF_LIBRE_TOKEN_EXPIRE="source_libre_token_expire"
const val SHARED_PREF_LIBRE_REGION="source_libre_region"
const val SHARED_PREF_LIBRE_PATIENT_ID="source_libre_patient_id"

const val SHARED_PREF_NIGHTSCOUT_ENABLED="src_ns_enabled"
const val SHARED_PREF_NIGHTSCOUT_URL="src_ns_url"
Expand All @@ -114,6 +116,9 @@ object Constants {

const val SHARED_PREF_DUMMY_VALUES = "dummy_values"

const val SHARED_PREF_LOCKSCREEN_WP_ENABLED = "lockscreen_enabled"
const val SHARED_PREF_LOCKSCREEN_WP_Y_POS = "lockscreen_y_pos"

// Android Auto
const val AA_MEDIA_ICON_STYLE_TREND = "trend"
const val AA_MEDIA_ICON_STYLE_GLUCOSE_TREND = "glucose_trend"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import de.michelinside.glucodatahandler.common.tasks.TimeTaskService
import de.michelinside.glucodatahandler.common.utils.GlucoDataUtils
import de.michelinside.glucodatahandler.common.notification.ChannelType
import de.michelinside.glucodatahandler.common.notification.Channels
import de.michelinside.glucodatahandler.common.utils.Utils

enum class AppSource {
NOT_SET,
Expand Down Expand Up @@ -63,8 +62,10 @@ abstract class GlucoDataService(source: AppSource) : WearableListenerService() {
}

fun start(source: AppSource, context: Context, cls: Class<*>, force: Boolean = false) {
if (!running || force) {
if (!running || !foreground) {
Log.v(LOG_ID, "start called (running: $running - foreground: $foreground)")
try {
isRunning = true
appSource = source
val serviceIntent = Intent(
context,
Expand All @@ -80,20 +81,30 @@ abstract class GlucoDataService(source: AppSource) : WearableListenerService() {
// default on wear and phone
true//sharedPref.getBoolean(Constants.SHARED_PREF_FOREGROUND_SERVICE, true)
)
context.startService(serviceIntent)
if (foreground) {
context.startService(serviceIntent)
} else {
Log.v(LOG_ID, "start foreground service")
context.startForegroundService(serviceIntent)
}
} catch (exc: Exception) {
Log.e(
LOG_ID,
"start exception: " + exc.message.toString()
)
isRunning = false
}
}
}

fun checkForConnectedNodes() {
fun checkForConnectedNodes(refreshDataOnly: Boolean = false) {
try {
if (connection!=null)
connection!!.checkForConnectedNodes()
Log.d(LOG_ID, "checkForConnectedNodes called for dataOnly=$refreshDataOnly - connection: ${connection!=null}")
if (connection!=null) {
if(!refreshDataOnly)
connection!!.checkForConnectedNodes()
connection!!.checkForNodesWithoutData()
}
} catch (exc: Exception) {
Log.e(
LOG_ID,
Expand All @@ -112,10 +123,10 @@ abstract class GlucoDataService(source: AppSource) : WearableListenerService() {
@RequiresApi(Build.VERSION_CODES.Q)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
try {
Log.d(LOG_ID, "onStartCommand called")
Log.v(LOG_ID, "onStartCommand called")
super.onStartCommand(intent, flags, startId)
val isForeground = true // intent?.getBooleanExtra(Constants.SHARED_PREF_FOREGROUND_SERVICE, true) --> always use foreground!!!
if (isForeground && !isForegroundService && Utils.checkPermission(this, android.Manifest.permission.POST_NOTIFICATIONS, Build.VERSION_CODES.TIRAMISU)) {
if (isForeground && !isForegroundService) {
Log.i(LOG_ID, "Starting service in foreground!")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
startForeground(NOTIFICATION_ID, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
Expand Down Expand Up @@ -199,6 +210,7 @@ abstract class GlucoDataService(source: AppSource) : WearableListenerService() {
super.onDestroy()
service = null
isRunning = false
isForegroundService = false
} catch (exc: Exception) {
Log.e(LOG_ID, "onDestroy exception: " + exc.toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,23 @@ object ReceiveData: SharedPreferences.OnSharedPreferenceChangeListener {
System.currentTimeMillis()

if(!isIobCob() || (newTime > iobCobTime && (newTime-iobCobTime) > 30000)) {
Log.i(LOG_ID, "Only IOB/COB changed: " + extras.getFloat(IOB, Float.NaN) + "/" + extras.getFloat(COB, Float.NaN))
iob = extras.getFloat(IOB, Float.NaN)
cob = extras.getFloat(COB, Float.NaN)
var iobCobChange = false
if(iob != extras.getFloat(IOB, Float.NaN) || cob != extras.getFloat(COB, Float.NaN)) {
Log.i(LOG_ID, "Only IOB/COB changed: " + extras.getFloat(IOB, Float.NaN) + "/" + extras.getFloat(COB, Float.NaN))
iob = extras.getFloat(IOB, Float.NaN)
cob = extras.getFloat(COB, Float.NaN)
iobCobChange = true
} else {
Log.d(LOG_ID, "Only IOB/COB time changed")
}
iobCobTime = newTime

// do not forward extras as interApp to prevent sending back to source...
val bundle: Bundle? = if(interApp) null else createExtras() // re-create extras to have all changed value inside for sending to receiver
InternalNotifier.notify(context, NotifySource.IOB_COB_CHANGE, bundle)
if(iobCobChange)
InternalNotifier.notify(context, NotifySource.IOB_COB_CHANGE, bundle)
else
InternalNotifier.notify(context, NotifySource.IOB_COB_TIME, bundle)
saveExtras(context)
}
}
Expand Down Expand Up @@ -622,7 +631,7 @@ object ReceiveData: SharedPreferences.OnSharedPreferenceChangeListener {

private fun saveExtras(context: Context) {
try {
Log.d(LOG_ID, "Saving extras")
Log.v(LOG_ID, "Saving extras")
// use own tag to prevent trigger onChange event at every time!
val sharedGlucosePref = context.getSharedPreferences(Constants.GLUCODATA_BROADCAST_ACTION, Context.MODE_PRIVATE)
with(sharedGlucosePref.edit()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.michelinside.glucodatahandler.common

import android.content.Context
import android.os.Handler
import android.util.Log
import de.michelinside.glucodatahandler.common.notifier.DataSource
import de.michelinside.glucodatahandler.common.notifier.InternalNotifier
import de.michelinside.glucodatahandler.common.notifier.NotifySource
Expand All @@ -13,6 +14,7 @@ enum class SourceState(val resId: Int) {
ERROR(R.string.source_state_error);
}
object SourceStateData {
private val LOG_ID = "GDH.SourceStateData"

var lastSource: DataSource = DataSource.NONE
var lastState: SourceState = SourceState.NONE
Expand All @@ -27,6 +29,10 @@ object SourceStateData {
lastSource = source
lastState = state

if(state == SourceState.ERROR) {
Log.e(LOG_ID, "Error state for source $source: $error" )
}

if (GlucoDataService.context != null) {
Handler(GlucoDataService.context!!.mainLooper).post {
InternalNotifier.notify(GlucoDataService.context!!, NotifySource.SOURCE_STATE_CHANGE, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class WearPhoneConnection : MessageClient.OnMessageReceivedListener, CapabilityC
val filter = mutableSetOf(
NotifySource.BROADCAST,
NotifySource.IOB_COB_CHANGE,
NotifySource.IOB_COB_TIME,
NotifySource.BATTERY_LEVEL) // to trigger re-start for the case of stopped by the system
if (sendSettings) {
filter.add(NotifySource.SETTINGS) // only send setting changes from phone to wear!
Expand Down Expand Up @@ -122,6 +123,19 @@ class WearPhoneConnection : MessageClient.OnMessageReceivedListener, CapabilityC
}
}

fun checkForNodesWithoutData() {
try {
Log.d(LOG_ID, "check for connected nodes without data")
connectedNodes.forEach { node ->
if (!nodeBatteryLevel.containsKey(node.key)) {
sendDataRequest(node.value.id)
}
}
} catch (exc: Exception) {
Log.e(LOG_ID, "checkForNodesWithoutData exception: " + exc.toString())
}
}

private fun setConnectedNodes(nodes: MutableSet<Node>) {
val curNodes = connectedNodes.keys.toSortedSet()
val newNodes = nodes.map { it.id }.toSortedSet()
Expand All @@ -141,13 +155,17 @@ class WearPhoneConnection : MessageClient.OnMessageReceivedListener, CapabilityC
}
}
}
val extras = ReceiveData.createExtras()
InternalNotifier.notify(context, NotifySource.CAPILITY_INFO, ReceiveData.createExtras())
sendMessage(NotifySource.CAPILITY_INFO, extras) // send data request for new node
sendDataRequest()
}
}

fun checkConnectedNode(nodeId: String) {
private fun sendDataRequest(filterReceiverId: String? = null) {
val extras = ReceiveData.createExtras()
InternalNotifier.notify(context, NotifySource.CAPILITY_INFO, ReceiveData.createExtras())
sendMessage(NotifySource.CAPILITY_INFO, extras, filterReceiverId = filterReceiverId) // send data request for new node
}

private fun checkConnectedNode(nodeId: String) {
if (!connectedNodes.containsKey(nodeId)) {
Log.i(LOG_ID, "Node with id " + nodeId + " not yet connected, check connection!")
checkForConnectedNodes()
Expand Down Expand Up @@ -180,10 +198,10 @@ class WearPhoneConnection : MessageClient.OnMessageReceivedListener, CapabilityC
else -> Constants.GLUCODATA_INTENT_MESSAGE_PATH
}

fun sendMessage(dataSource: NotifySource, extras: Bundle?, ignoreReceiverId: String? = null, filterReiverId: String? = null)
fun sendMessage(dataSource: NotifySource, extras: Bundle?, ignoreReceiverId: String? = null, filterReceiverId: String? = null)
{
try {
Log.v(LOG_ID, "sendMessage called for $dataSource filter receiver $filterReiverId ignoring receiver $ignoreReceiverId with extras $extras")
Log.v(LOG_ID, "sendMessage called for $dataSource filter receiver $filterReceiverId ignoring receiver $ignoreReceiverId with extras $extras")
if( nodesConnected && dataSource != NotifySource.NODE_BATTERY_LEVEL ) {
Log.d(LOG_ID, connectedNodes.size.toString() + " nodes found for sending message for " + dataSource.toString())
if (extras != null && dataSource != NotifySource.BATTERY_LEVEL && BatteryReceiver.batteryPercentage > 0) {
Expand All @@ -198,7 +216,7 @@ class WearPhoneConnection : MessageClient.OnMessageReceivedListener, CapabilityC
connectedNodes.forEach { node ->
Thread {
try {
if ((ignoreReceiverId == null && filterReiverId == null) || ignoreReceiverId != node.value.id || filterReiverId == node.value.id) {
if ((ignoreReceiverId == null && filterReceiverId == null) || ignoreReceiverId != node.value.id || filterReceiverId == node.value.id) {
if (dataSource == NotifySource.CAPILITY_INFO)
Thread.sleep(1000) // wait a bit after the connection has changed
sendMessage(node.value, getPath(dataSource), Utils.bundleToBytes(extras), dataSource)
Expand Down Expand Up @@ -391,7 +409,7 @@ class WearPhoneConnection : MessageClient.OnMessageReceivedListener, CapabilityC

override fun OnNotifyData(context: Context, dataSource: NotifySource, extras: Bundle?) {
try {
if (dataSource != NotifySource.IOB_COB_CHANGE || extras != null) { // do not send IOB change without extras
if ((dataSource != NotifySource.IOB_COB_CHANGE && dataSource != NotifySource.IOB_COB_TIME) || extras != null) { // do not send IOB change without extras
Log.d(LOG_ID, "OnNotifyData for source " + dataSource.toString() + " and extras " + extras.toString())
sendMessage(dataSource, extras)
}
Expand Down
Loading

0 comments on commit d6cc2d9

Please sign in to comment.