Skip to content

Commit

Permalink
Merge pull request #179 from pachi81/1.2
Browse files Browse the repository at this point in the history
GDA 1.2
  • Loading branch information
pachi81 authored Nov 16, 2024
2 parents 679aa32 + 8ab142c commit f546973
Show file tree
Hide file tree
Showing 21 changed files with 199 additions and 43 deletions.
4 changes: 2 additions & 2 deletions auto/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "de.michelinside.glucodataauto"
minSdk rootProject.minSdk
targetSdk rootProject.targetSdk
versionCode 1027
versionName "1.1.7"
versionCode 1028
versionName "1.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class CarMediaBrowserService: MediaBrowserServiceCompat(), NotifierInterface, Sh
private lateinit var session: MediaSessionCompat
private lateinit var mediaController: MediaControllerCompat
private var curMediaItem = MEDIA_ROOT_ID
private var playBackState = PlaybackState.STATE_NONE
private var lastGlucoseTime = 0L

companion object {
var active = false
Expand Down Expand Up @@ -70,7 +72,10 @@ class CarMediaBrowserService: MediaBrowserServiceCompat(), NotifierInterface, Sh
try {
if(curMediaItem == MEDIA_GLUCOSE_ID) {
// Current song is ready, but paused, so start playing the music.
CarMediaPlayer.play(applicationContext, !sharedPref.getBoolean(Constants.AA_MEDIA_PLAYER_SPEAK_VALUES, false))
if(sharedPref.getBoolean(Constants.AA_MEDIA_PLAYER_SPEAK_VALUES, false))
CarMediaPlayer.play(applicationContext, !sharedPref.getBoolean(Constants.AA_MEDIA_PLAYER_SPEAK_VALUES, false))
else
session.setPlaybackState(buildState(PlaybackState.STATE_PLAYING))
} else if(curMediaItem == MEDIA_NOTIFICATION_TOGGLE_ID) {
Log.d(LOG_ID, "Toggle notification")
with(sharedPref.edit()) {
Expand All @@ -92,24 +97,20 @@ class CarMediaBrowserService: MediaBrowserServiceCompat(), NotifierInterface, Sh
override fun onStop() {
Log.i(LOG_ID, "onStop called playing")
try {
CarMediaPlayer.stop()
if(CarMediaPlayer.hasCallback())
CarMediaPlayer.stop()
else
session.setPlaybackState(buildState(PlaybackState.STATE_STOPPED))
} catch (exc: Exception) {
Log.e(LOG_ID, "onStop exception: " + exc.message.toString() )
}
}
})

session.setPlaybackState(buildState(PlaybackState.STATE_STOPPED))
CarMediaPlayer.setCallback(object : CarMediaPlayerCallback() {
override fun onPlay() {
Log.d(LOG_ID, "onPlay called")
session.setPlaybackState(buildState(PlaybackState.STATE_PLAYING))
}
override fun onStop() {
Log.d(LOG_ID, "onStop called")
session.setPlaybackState(buildState(PlaybackState.STATE_STOPPED))
}
})

// set callback depending on the current speak value to prevent speaking for values in background as affect on state!
onSharedPreferenceChanged(sharedPref, Constants.AA_MEDIA_PLAYER_SPEAK_VALUES)

sessionToken = session.sessionToken
mediaController = MediaControllerCompat(this, session.sessionToken)
Expand Down Expand Up @@ -198,6 +199,32 @@ class CarMediaBrowserService: MediaBrowserServiceCompat(), NotifierInterface, Sh
Constants.SHARED_PREF_CAR_MEDIA_ICON_STYLE -> {
notifyChildrenChanged(MEDIA_ROOT_ID)
}
Constants.AA_MEDIA_PLAYER_SPEAK_VALUES -> {
if(sharedPref.getBoolean(Constants.AA_MEDIA_PLAYER_SPEAK_VALUES, false)) {
CarMediaPlayer.setCallback(object : CarMediaPlayerCallback() {
override fun onPlay() {
Log.d(LOG_ID, "callback play called")
setGlucose() // update duration for playing
session.setPlaybackState(buildState(PlaybackState.STATE_PLAYING))
}
override fun onStop() {
Log.d(LOG_ID, "callback onStop called")
session.setPlaybackState(buildState(PlaybackState.STATE_STOPPED))
}
})
session.setPlaybackState(buildState(PlaybackState.STATE_STOPPED))
} else {
CarMediaPlayer.setCallback(null)
}
notifyChildrenChanged(MEDIA_ROOT_ID)
}
Constants.AA_MEDIA_PLAYER_DURATION -> {
setGlucose() // update duration for playing
if(playBackState==PlaybackState.STATE_PLAYING) {
// reset duration
session.setPlaybackState(buildState(PlaybackState.STATE_PLAYING))
}
}
}
} catch (exc: Exception) {
Log.e(LOG_ID, "onSharedPreferenceChanged exception: " + exc.message.toString() )
Expand Down Expand Up @@ -260,9 +287,15 @@ class CarMediaBrowserService: MediaBrowserServiceCompat(), NotifierInterface, Sh
MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE,
ReceiveData.getElapsedTimeMinuteAsString(this)
)
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, getDuration())
.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, getIcon(400)!!)
.build()
)
if(playBackState == PlaybackState.STATE_PLAYING && lastGlucoseTime < ReceiveData.time) {
// update position
session.setPlaybackState(buildState(playBackState))
}
lastGlucoseTime = ReceiveData.time
} else {
session.setPlaybackState(buildState(PlaybackState.STATE_NONE))
}
Expand Down Expand Up @@ -334,17 +367,37 @@ class CarMediaBrowserService: MediaBrowserServiceCompat(), NotifierInterface, Sh
}

private fun buildState(state: Int): PlaybackStateCompat? {
Log.d(LOG_ID, "buildState called for state $state - pos: ${CarMediaPlayer.currentPosition}")
val duration = getDuration()
val position = if(state==PlaybackState.STATE_PLAYING) getPosition() else 0L
Log.d(LOG_ID, "buildState called for state $state - pos: ${position}/${duration}")
playBackState = state
val bundleWithDuration = if (duration == 0L) null else Bundle().apply {
putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration) // duration in Millisekunden
}
return PlaybackStateCompat.Builder().setActions(
PlaybackStateCompat.ACTION_PLAY or PlaybackStateCompat.ACTION_STOP)
.setState(
state,
CarMediaPlayer.currentPosition.toLong(),
position,
1f,
SystemClock.elapsedRealtime()
)
).setExtras(bundleWithDuration)
.build()
}

private fun getPosition(): Long {
return if(CarMediaPlayer.hasCallback())
CarMediaPlayer.currentPosition
else
System.currentTimeMillis()-ReceiveData.receiveTime
}

private fun getDuration(): Long {
return if(CarMediaPlayer.hasCallback())
CarMediaPlayer.duration
else
sharedPref.getInt(Constants.AA_MEDIA_PLAYER_DURATION, 0) * 60000L
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,22 @@ object CarMediaPlayer: NotifierInterface {
this.callback = callback
}

val currentPosition: Int get() {
if(enabled) {
return player.currentPosition
fun hasCallback() : Boolean {
return this.callback != null
}

val currentPosition: Long get() {
if(isPlaying) {
return player.currentPosition.toLong()
}
return 0
return 0L
}

val duration: Long get() {
if(isPlaying) {
return player.duration.toLong()
}
return 0L
}

val isPlaying: Boolean get() {
Expand Down Expand Up @@ -139,17 +150,20 @@ object CarMediaPlayer: NotifierInterface {
var uri: String? = null
var requestAudioFocus = false
if(!playSilent) {
file = TextToSpeechUtils.getAsFile(ReceiveData.getAsText(context, false, false))
file = TextToSpeechUtils.getAsFile(ReceiveData.getAsText(context, false, false), context)
if(file != null) {
uri = file!!.absolutePath
last_speak_time = ReceiveData.time
requestAudioFocus = true
} else {
Log.w(LOG_ID, "TTS file could not be created!")
}
} else { // play silent
}
if(uri.isNullOrEmpty()) { // play silent
uri = "android.resource://" + context.packageName + "/" + R.raw.silence
requestAudioFocus = false
}
if(!uri.isNullOrEmpty()) {
if(uri.isNotEmpty()) {
Log.d(LOG_ID, "onPlay uri: $uri")
player.reset()
player.setDataSource(context, Uri.parse(uri))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.michelinside.glucodataauto.preferences

import android.content.SharedPreferences
import android.util.Log
import androidx.preference.SeekBarPreference
import androidx.preference.SwitchPreferenceCompat
import de.michelinside.glucodataauto.R
import de.michelinside.glucodatahandler.common.Constants
Expand All @@ -21,4 +23,17 @@ class GDAMediaSettingsFragment: SettingsFragmentBase(R.xml.pref_gda_media) {
}
}


override fun updateEnableStates(sharedPreferences: SharedPreferences) {
try {
Log.v(LOG_ID, "updateEnableStates called")
val intervalPref = findPreference<SeekBarPreference>(Constants.AA_MEDIA_PLAYER_DURATION)
if(intervalPref != null) {
intervalPref.isEnabled = !sharedPreferences.getBoolean(Constants.AA_MEDIA_PLAYER_SPEAK_VALUES, false)
updateEnablePrefs.add(Constants.AA_MEDIA_PLAYER_SPEAK_VALUES)
}
} catch (exc: Exception) {
Log.e(LOG_ID, "updateEnableStates exception: " + exc.toString())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import de.michelinside.glucodataauto.R

abstract class SettingsFragmentBase(private val prefResId: Int) : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
protected val LOG_ID = "GDH.AA.SettingsFragmentBase"
private val updateEnablePrefs = mutableSetOf<String>()
protected val updateEnablePrefs = mutableSetOf<String>()

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
Log.d(LOG_ID, "onCreatePreferences called")
Expand Down
10 changes: 10 additions & 0 deletions auto/src/main/res/xml/pref_gda_media.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,15 @@
android:summaryOn="@string/pref_aa_media_player_speak_values_on_summary"
android:summaryOff="@string/pref_aa_media_player_speak_values_off_summary"
app:iconSpaceReserved="false" />
<androidx.preference.SeekBarPreference
android:defaultValue="0"
app:showSeekBarValue="true"
app:seekBarIncrement="1"
app:min="0"
android:max="15"
android:key="aa_media_player_duration"
android:title="@string/source_interval"
android:summary="@string/pref_aa_media_player_duration_summary"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ plugins {
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

project.ext.set("versionCode", 78)
project.ext.set("versionName", "1.2")
project.ext.set("versionCode", 79)
project.ext.set("versionName", "1.2.1")
project.ext.set("compileSdk", 34)
project.ext.set("targetSdk", 34)
project.ext.set("minSdk", 26)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ object Constants {
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_LIBRE_USER_ID="source_libre_user_id"

const val SHARED_PREF_DEXCOM_SHARE_ENABLED="source_dexcom_share_enabled"
const val SHARED_PREF_DEXCOM_SHARE_USER="source_dexcom_share_user"
Expand Down Expand Up @@ -256,4 +257,5 @@ object Constants {
const val AA_MEDIA_PLAYER_SPEAK_ALARM_ONLY = "aa_media_player_speak_alarm_only"
const val AA_MEDIA_PLAYER_SPEAK_INTERVAL = "aa_media_player_speak_interval"
const val AA_MEDIA_PLAYER_SPEAK_TEST = "aa_media_player_speak_test"
const val AA_MEDIA_PLAYER_DURATION = "aa_media_player_duration"
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ object ReceiveData: SharedPreferences.OnSharedPreferenceChangeListener {
var rate: Float = 0.0F
var alarm: Int = 0
var time: Long = 0
var receiveTime: Long = 0
var timeDiff: Long = 0
var rateLabel: String? = null
var source: DataSource = DataSource.NONE
Expand Down Expand Up @@ -440,6 +441,7 @@ object ReceiveData: SharedPreferences.OnSharedPreferenceChangeListener {
extras.toString() +
" - timestamp: " + DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT).format(Date(extras.getLong(TIME)))
)
receiveTime = System.currentTimeMillis()
source = dataSource
sensorID = extras.getString(SERIAL) //Name of sensor
rate = extras.getFloat(RATE) //Rate of change of glucose. See libre and dexcom label functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import de.michelinside.glucodatahandler.common.utils.Utils

class InternalActionReceiver: BroadcastReceiver() {

private val LOG_ID = "GDH.GlucoDataActionReceiver"
private val LOG_ID = "GDH.InternalActionReceiver"
override fun onReceive(context: Context, intent: Intent) {
try {
Log.d(LOG_ID, "Action received: ${intent.action} - bundle: ${Utils.dumpBundle(intent.extras)}")
Expand Down
Loading

0 comments on commit f546973

Please sign in to comment.