Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop GDA #153

Merged
merged 28 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7483514
GDA: allow foreground mode
pachi81 Jun 5, 2024
35011fe
GDA: add follower summary
pachi81 Jun 5, 2024
0921d1f
GDA: fix datasync
pachi81 Jun 5, 2024
213de44
Update strings.xml PL
froster82 Jun 5, 2024
64438da
Update strings.xml small corrections to ENG
froster82 Jun 5, 2024
df4bbda
GDA: request notification permission
pachi81 Jun 6, 2024
d4faf55
GDA: help added
pachi81 Jun 6, 2024
cc05f1d
GDA: fix notification permission
pachi81 Jun 6, 2024
cc20d24
GDA: adjust help to current version
pachi81 Jun 6, 2024
2340fc9
GDA: fix time task
pachi81 Jun 6, 2024
ee86fde
Merge remote-tracking branch 'origin/develop_gda' into develop_gda
pachi81 Jun 6, 2024
4d7fe35
fix initial task execution
pachi81 Jun 7, 2024
defa8e0
Workaround for Samsung notification bug
pachi81 Jun 8, 2024
3ff69a9
v47 (1.0.1)
pachi81 Jun 8, 2024
4a903ce
GDA: uncaught ExceptionHandler
pachi81 Jun 8, 2024
41ce1b0
fix uncaught ExceptionHandler
pachi81 Jun 9, 2024
faa8e38
Change widget style summary
pachi81 Jun 9, 2024
95fadcf
Additional fix for notification
pachi81 Jun 9, 2024
1c0f1b3
v48
pachi81 Jun 9, 2024
7cfefa8
Update strings.xml PL
froster82 Jun 10, 2024
a4d9d50
fix snooze end detection
pachi81 Jun 10, 2024
5f09b45
AlarmNotification refactor
pachi81 Jun 10, 2024
d5aa118
Merge remote-tracking branch 'origin/develop_gda' into develop_gda
pachi81 Jun 10, 2024
afa91aa
v49
pachi81 Jun 10, 2024
ba871ac
Fix notification output
pachi81 Jun 10, 2024
ba24c66
v50
pachi81 Jun 10, 2024
f362038
GDA: adjust help
pachi81 Jun 10, 2024
b5c7853
GDA: v1.0
pachi81 Jun 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions auto/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
minSdk rootProject.minSdk
targetSdk rootProject.targetSdk
versionCode 1025
versionName "1.0-beta1"
versionName "1.0-beta2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -73,12 +73,12 @@ android {

dependencies {
implementation 'androidx.core:core-ktx:1.13.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.joaomgcd:taskerpluginlibrary:0.4.4'
implementation project(path: ':common')
implementation "androidx.car.app:app:1.2.0"
implementation "androidx.car.app:app:1.4.0"
implementation "androidx.preference:preference:1.2.1"
implementation "com.jaredrummler:colorpicker:1.1.0"
implementation "androidx.media:media:1.7.0"
Expand Down
2 changes: 1 addition & 1 deletion auto/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

<queries>
<package android:name="tk.glucodata" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,52 @@ class GlucoDataServiceAuto: Service() {
private var dataSyncCount = 0
val connected: Boolean get() = car_connected || CarMediaBrowserService.active
fun init(context: Context) {
Log.v(LOG_ID, "init called: init=$init")
if(!init) {
Log.v(LOG_ID, "init called")
GlucoDataService.context = context
//TimeTaskService.useWorker = true
//SourceTaskService.useWorker = true
ReceiveData.initData(context)
CarConnection(context.applicationContext).type.observeForever(GlucoDataServiceAuto::onConnectionStateUpdated)
startService(context, false)
init = true
}
}

private fun setForeground(context: Context, foreground: Boolean) {
private fun startService(context: Context, foreground: Boolean) {
try {
val sharedPref = context.getSharedPreferences(Constants.SHARED_PREF_TAG, Context.MODE_PRIVATE)
val isForeground = foreground || sharedPref.getBoolean(Constants.SHARED_PREF_FOREGROUND_SERVICE, false)
val serviceIntent = Intent(context, GlucoDataServiceAuto::class.java)
serviceIntent.putExtra(Constants.SHARED_PREF_FOREGROUND_SERVICE, isForeground)
if (isForeground)
context.startForegroundService(serviceIntent)
else
context.startService(serviceIntent)
} catch (exc: Exception) {
Log.e(LOG_ID, "startService exception: " + exc.message.toString() + "\n" + exc.stackTraceToString())
}
}

fun setForeground(context: Context, foreground: Boolean) {
try {
Log.v(LOG_ID, "setForeground called " + foreground)
if (isForegroundService != foreground) {
val serviceIntent = Intent(context, GlucoDataServiceAuto::class.java)
serviceIntent.putExtra(Constants.SHARED_PREF_FOREGROUND_SERVICE, foreground)
if (foreground)
context.startForegroundService(serviceIntent)
else
context.startService(serviceIntent)
startService(context, foreground)
}
} catch (exc: Exception) {
Log.e(LOG_ID, "setForeground exception: " + exc.toString())
Log.e(LOG_ID, "setForeground exception: " + exc.message.toString() + "\n" + exc.stackTraceToString())
}
}

fun start(context: Context) {
try {
if(!running) {
init(context)
Log.i(LOG_ID, "starting")
CarNotification.enable(context)
startDataSync(context)
setForeground(context, true)
running = true
}
} catch (exc: Exception) {
Log.e(LOG_ID, "start exception: " + exc.toString())
Log.e(LOG_ID, "start exception: " + exc.message.toString() + "\n" + exc.stackTraceToString())
}
}

Expand All @@ -86,34 +94,36 @@ class GlucoDataServiceAuto: Service() {
running = false
}
} catch (exc: Exception) {
Log.e(LOG_ID, "stop exception: " + exc.toString())
Log.e(LOG_ID, "stop exception: " + exc.message.toString() + "\n" + exc.stackTraceToString())
}
}

fun startDataSync(context: Context) {
try {
Log.i(LOG_ID, "starting datasync - count=$dataSyncCount")
if (dataSyncCount == 0) {
Log.d(LOG_ID, "startDataSync count: $dataSyncCount")
TimeTaskService.run(context)
SourceTaskService.run(context)
sendStateBroadcast(context, true)
Log.i(LOG_ID, "Datasync started")
}
dataSyncCount++
} catch (exc: Exception) {
Log.e(LOG_ID, "startDataSync exception: " + exc.toString())
Log.e(LOG_ID, "startDataSync exception: " + exc.message.toString() + "\n" + exc.stackTraceToString())
}
}

fun stopDataSync(context: Context) {
try {
dataSyncCount--
Log.i(LOG_ID, "stopping datasync - count=$dataSyncCount")
if (dataSyncCount == 0) {
Log.d(LOG_ID, "stopDataSync")
sendStateBroadcast(context, false)
BackgroundWorker.stopAllWork(context)
Log.i(LOG_ID, "Datasync stopped")
}
} catch (exc: Exception) {
Log.e(LOG_ID, "stopDataSync exception: " + exc.toString())
Log.e(LOG_ID, "stopDataSync exception: " + exc.message.toString() + "\n" + exc.stackTraceToString())
}
}

Expand Down Expand Up @@ -161,11 +171,14 @@ class GlucoDataServiceAuto: Service() {
}

@RequiresApi(Build.VERSION_CODES.Q)
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
try {
Log.d(LOG_ID, "onStartCommand called")
super.onStartCommand(intent, flags, startId)
val isForeground = intent.getBooleanExtra(Constants.SHARED_PREF_FOREGROUND_SERVICE, false)
GlucoDataService.context = applicationContext
ReceiveData.initData(applicationContext)
val sharedPref = getSharedPreferences(Constants.SHARED_PREF_TAG, Context.MODE_PRIVATE)
val isForeground = (if(intent != null) intent.getBooleanExtra(Constants.SHARED_PREF_FOREGROUND_SERVICE, false) else false) || sharedPref.getBoolean(Constants.SHARED_PREF_FOREGROUND_SERVICE, false)
if (isForeground && !isForegroundService) {
Log.i(LOG_ID, "Starting service in foreground!")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
Expand All @@ -178,12 +191,12 @@ class GlucoDataServiceAuto: Service() {
Log.i(LOG_ID, "Stopping service in foreground!")
stopForeground(STOP_FOREGROUND_REMOVE)
}
CarConnection(applicationContext).type.observeForever(GlucoDataServiceAuto::onConnectionStateUpdated)
} catch (exc: Exception) {
Log.e(LOG_ID, "onStartCommand exception: " + exc.toString())
Log.e(LOG_ID, "onStartCommand exception: " + exc.message.toString() + "\n" + exc.stackTraceToString())
}
if (isForegroundService)
return START_STICKY // keep alive
return START_NOT_STICKY

return START_STICKY // keep alive
}

override fun onDestroy() {
Expand All @@ -202,7 +215,7 @@ class GlucoDataServiceAuto: Service() {
val pendingIntent = PackageUtils.getAppIntent(this, MainActivity::class.java, 11, false)

return Notification.Builder(this, ChannelType.ANDROID_AUTO_FOREGROUND.channelId)
.setContentTitle(getString(de.michelinside.glucodatahandler.common.R.string.activity_main_car_connected_label))
.setContentTitle(getString(de.michelinside.glucodatahandler.common.R.string.gda_foreground_info))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setOngoing(true)
Expand Down
30 changes: 8 additions & 22 deletions auto/src/main/java/de/michelinside/glucodataauto/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
private lateinit var btnSources: Button
private lateinit var txtNoData: TextView
private lateinit var sharedPref: SharedPreferences
private var menuOpen = false
private var notificationIcon: MenuItem? = null
private val LOG_ID = "GDH.AA.Main"
private var requestNotificationPermission = false
Expand Down Expand Up @@ -141,20 +140,19 @@ class MainActivity : AppCompatActivity(), NotifierInterface {

override fun onPause() {
try {
Log.d(LOG_ID, "onPause called")
super.onPause()
InternalNotifier.remNotifier(this, this)
if(!menuOpen)
GlucoDataServiceAuto.stopDataSync(this)
Log.v(LOG_ID, "onPause called")
GlucoDataServiceAuto.stopDataSync(this)
} catch (exc: Exception) {
Log.e(LOG_ID, "onPause exception: " + exc.message.toString() )
}
}

override fun onResume() {
try {
Log.d(LOG_ID, "onResume called")
super.onResume()
Log.v(LOG_ID, "onResume called")
update()
InternalNotifier.addNotifier( this, this, mutableSetOf(
NotifySource.BROADCAST,
Expand All @@ -165,7 +163,7 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
NotifySource.NODE_BATTERY_LEVEL,
NotifySource.SETTINGS,
NotifySource.CAR_CONNECTION,
NotifySource.OBSOLETE_VALUE,
NotifySource.TIME_VALUE,
NotifySource.SOURCE_STATE_CHANGE))
checkExactAlarmPermission()
checkBatteryOptimization()
Expand All @@ -175,21 +173,12 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
requestNotificationPermission = false
txtNotificationPermission.visibility = View.GONE
}
if(!menuOpen)
GlucoDataServiceAuto.startDataSync(this)
menuOpen = false
GlucoDataServiceAuto.startDataSync(this)
} catch (exc: Exception) {
Log.e(LOG_ID, "onResume exception: " + exc.message.toString() )
}
}

private val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
Log.d(LOG_ID, "Notification permission allowed: $isGranted")
}

fun requestPermission() : Boolean {
requestNotificationPermission = false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Expand Down Expand Up @@ -257,16 +246,16 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
try {
val pm = getSystemService(POWER_SERVICE) as PowerManager
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
Log.w(LOG_ID, "Battery optimization is inactive")
Log.w(LOG_ID, "Battery optimization is active")
txtBatteryOptimization.visibility = View.VISIBLE
txtBatteryOptimization.setOnClickListener {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
intent.data = Uri.parse("package:$packageName")
startActivity(intent)
}
} else {
txtBatteryOptimization.visibility = View.GONE
Log.i(LOG_ID, "Battery optimization is active")
Log.i(LOG_ID, "Battery optimization is inactive")
}
} catch (exc: Exception) {
Log.e(LOG_ID, "checkBatteryOptimization exception: " + exc.message.toString() )
Expand Down Expand Up @@ -305,20 +294,17 @@ class MainActivity : AppCompatActivity(), NotifierInterface {
Log.v(LOG_ID, "onOptionsItemSelected for " + item.itemId.toString())
when(item.itemId) {
R.id.action_settings -> {
menuOpen = true
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
return true
}
R.id.action_sources -> {
menuOpen = true
val intent = Intent(this, SettingsActivity::class.java)
intent.putExtra(SettingsActivity.FRAGMENT_EXTRA, SettingsFragmentClass.SORUCE_FRAGMENT.value)
startActivity(intent)
return true
}
R.id.action_alarms -> {
menuOpen = true
val intent = Intent(this, SettingsActivity::class.java)
intent.putExtra(SettingsActivity.FRAGMENT_EXTRA, SettingsFragmentClass.ALARM_FRAGMENT.value)
startActivity(intent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package de.michelinside.glucodataauto.preferences

import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import androidx.preference.*
import de.michelinside.glucodataauto.BuildConfig
import de.michelinside.glucodataauto.GlucoDataServiceAuto
import de.michelinside.glucodataauto.R
import de.michelinside.glucodatahandler.common.Constants


class SettingsFragment : PreferenceFragmentCompat() {
class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
private val LOG_ID = "GDH.AA.SettingsFragment"

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
Expand All @@ -27,4 +29,38 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}


override fun onResume() {
Log.d(LOG_ID, "onResume called")
try {
super.onResume()
preferenceManager.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)
} catch (exc: Exception) {
Log.e(LOG_ID, "onResume exception: " + exc.toString())
}
}

override fun onPause() {
Log.d(LOG_ID, "onPause called")
try {
super.onPause()
preferenceManager.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(this)
} catch (exc: Exception) {
Log.e(LOG_ID, "onPause exception: " + exc.toString())
}
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
Log.d(LOG_ID, "onSharedPreferenceChanged called for " + key)
try {
when(key) {
Constants.SHARED_PREF_FOREGROUND_SERVICE -> {
GlucoDataServiceAuto.setForeground(requireContext(), sharedPreferences!!.getBoolean(Constants.SHARED_PREF_FOREGROUND_SERVICE, false))
}
}
} catch (exc: Exception) {
Log.e(LOG_ID, "onSharedPreferenceChanged exception: " + exc.toString())
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class SourceFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPre
preferenceManager.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)
updateEnableStates(preferenceManager.sharedPreferences!!)
InternalNotifier.addNotifier(requireContext(), this, mutableSetOf(NotifySource.PATIENT_DATA_CHANGED))
//GlucoDataServiceAuto.startDataSync(requireContext())
super.onResume()
} catch (exc: Exception) {
Log.e(LOG_ID, "onResume exception: " + exc.toString())
Expand All @@ -72,6 +73,7 @@ class SourceFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPre
try {
preferenceManager.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(this)
InternalNotifier.remNotifier(requireContext(), this)
//GlucoDataServiceAuto.stopDataSync(requireContext())
super.onPause()
} catch (exc: Exception) {
Log.e(LOG_ID, "onPause exception: " + exc.toString())
Expand Down
2 changes: 1 addition & 1 deletion auto/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/activity_main_battery_optimization_disabled"
android:text="@string/gda_battery_optimization_disabled"
android:textSize="16sp"
android:textAlignment="center" />
</LinearLayout>
Expand Down
2 changes: 1 addition & 1 deletion auto/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/activity_main_battery_optimization_disabled"
android:text="@string/gda_battery_optimization_disabled"
android:textSize="16sp"
android:textAlignment="center" />
</LinearLayout>
Expand Down
6 changes: 6 additions & 0 deletions auto/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="foreground_service"
android:summary="@string/gda_foreground_summary"
android:title="@string/activity_switch_foreground"
app:iconSpaceReserved="false" />
<Preference
android:key="pref_cat_android_auto"
android:title="@string/pref_cat_android_auto"
Expand Down
Loading