Skip to content

Commit

Permalink
Merge pull request #153 from pachi81/develop_gda
Browse files Browse the repository at this point in the history
Develop GDA
  • Loading branch information
pachi81 authored Jun 10, 2024
2 parents 8ec6c07 + b5c7853 commit df872f4
Show file tree
Hide file tree
Showing 44 changed files with 804 additions and 185 deletions.
8 changes: 4 additions & 4 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 1025
versionName "1.0-beta1"
versionCode 1026
versionName "1.0"

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
3 changes: 2 additions & 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 Expand Up @@ -106,6 +106,7 @@
<receiver android:name="de.michelinside.glucodatahandler.common.tasks.TimeAlarmReceiver" />
<receiver android:name="de.michelinside.glucodatahandler.common.tasks.SourceAlarmReceiver" />
<receiver android:name="de.michelinside.glucodatahandler.common.receiver.InternalActionReceiver" />
<receiver android:name="de.michelinside.glucodatahandler.common.notification.AlarmSnoozeEndReceiver" />

<service
android:name=".android_auto.CarMediaBrowserService"
Expand Down
55 changes: 55 additions & 0 deletions auto/src/main/java/de/michelinside/glucodataauto/Dialogs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.michelinside.glucodataauto

import android.app.UiModeManager
import android.content.Context
import android.content.DialogInterface
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import de.michelinside.glucodatahandler.common.Constants
import de.michelinside.glucodatahandler.common.R


object Dialogs {
fun showOkDialog(context: Context, titleResId: Int, messageResId: Int, okListener: DialogInterface.OnClickListener?) {
MaterialAlertDialogBuilder(context)
.setTitle(context.resources.getString(titleResId))
.setMessage(context.resources.getString(messageResId))
.setPositiveButton(context.resources.getText(R.string.button_ok), okListener)
.show()
}
fun showOkCancelDialog(context: Context, titleResId: Int, messageResId: Int, okListener: DialogInterface.OnClickListener?) {
MaterialAlertDialogBuilder(context)
.setTitle(context.resources.getString(titleResId))
.setMessage(context.resources.getString(messageResId))
.setPositiveButton(context.resources.getText(R.string.button_ok), okListener)
.setNegativeButton(context.resources.getText(R.string.button_cancel), null)
.show()
}

fun updateColorScheme(context: Context) {
val sharedPref = context.getSharedPreferences(Constants.SHARED_PREF_TAG, Context.MODE_PRIVATE)
val colorScheme = sharedPref.getString(Constants.SHARED_PREF_APP_COLOR_SCHEME, "")
// This will be the top level handling of theme
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// If you don't want to adapt the device's theme settings, uncomment the snippet below
val uiModeManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager
uiModeManager.setApplicationNightMode(
when (colorScheme) {
"light" -> UiModeManager.MODE_NIGHT_NO // User set this explicitly
"dark" -> UiModeManager.MODE_NIGHT_YES // User set this explicitly
else -> UiModeManager.MODE_NIGHT_AUTO // Follow the device Dark Theme settings when not define yet by user
}
)
} else {
AppCompatDelegate.setDefaultNightMode(
when (colorScheme) {
"light" -> AppCompatDelegate.MODE_NIGHT_NO // User set this explicitly
"dark" -> AppCompatDelegate.MODE_NIGHT_YES // User set this explicitly
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM // For Android 10 and 11, follow the device Dark Theme settings when not define yet by user
}
)

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.car.app.connection.CarConnection
import de.michelinside.glucodataauto.android_auto.CarMediaBrowserService
import de.michelinside.glucodataauto.android_auto.CarNotification
import de.michelinside.glucodatahandler.common.Constants
import de.michelinside.glucodatahandler.common.GdhUncaughtExecptionHandler
import de.michelinside.glucodatahandler.common.GlucoDataService
import de.michelinside.glucodatahandler.common.ReceiveData
import de.michelinside.glucodatahandler.common.notification.ChannelType
Expand All @@ -25,6 +26,7 @@ import de.michelinside.glucodatahandler.common.tasks.TimeTaskService
import de.michelinside.glucodatahandler.common.utils.PackageUtils

class GlucoDataServiceAuto: Service() {

companion object {
private const val LOG_ID = "GDH.AA.GlucoDataServiceAuto"
private var isForegroundService = false
Expand All @@ -35,44 +37,53 @@ 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)
CarNotification.initNotification(context)
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 +97,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 +174,16 @@ 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")
GdhUncaughtExecptionHandler.init()
super.onStartCommand(intent, flags, startId)
val isForeground = intent.getBooleanExtra(Constants.SHARED_PREF_FOREGROUND_SERVICE, false)
GlucoDataService.context = applicationContext
ReceiveData.initData(applicationContext)
CarNotification.initNotification(this)
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 +196,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 +220,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
Loading

0 comments on commit df872f4

Please sign in to comment.