From 7fb94453c5f4219327a706a03ce552223d5dfd5a Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 9 Oct 2022 23:16:47 +0200 Subject: [PATCH] adding some crashfixes --- app/build.gradle | 2 +- .../clock/helpers/DBHelper.kt | 2 +- .../clock/services/StopwatchService.kt | 21 ++++++++++--------- .../clock/services/TimerService.kt | 11 ++++++++-- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0ae4e57a..124cc1db 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -64,7 +64,7 @@ android { } dependencies { - implementation 'com.github.SimpleMobileTools:Simple-Commons:e79ef36c10' + implementation 'com.github.SimpleMobileTools:Simple-Commons:2e9ca234a7' implementation 'com.facebook.stetho:stetho:1.5.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'com.shawnlin:number-picker:2.4.6' diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DBHelper.kt index 4bf466da..fa00d23f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DBHelper.kt @@ -41,7 +41,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } override fun onCreate(db: SQLiteDatabase) { - db.execSQL("CREATE TABLE $ALARMS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_TIME_IN_MINUTES INTEGER, $COL_DAYS INTEGER, " + + db.execSQL("CREATE TABLE IF NOT EXISTS $ALARMS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_TIME_IN_MINUTES INTEGER, $COL_DAYS INTEGER, " + "$COL_IS_ENABLED INTEGER, $COL_VIBRATE INTEGER, $COL_SOUND_TITLE TEXT, $COL_SOUND_URI TEXT, $COL_LABEL TEXT)") insertInitialAlarms(db) } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/services/StopwatchService.kt b/app/src/main/kotlin/com/simplemobiletools/clock/services/StopwatchService.kt index 5c3888b1..8fc7e0d0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/services/StopwatchService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/services/StopwatchService.kt @@ -17,6 +17,8 @@ import com.simplemobiletools.clock.helpers.STOPWATCH_RUNNING_NOTIF_ID import com.simplemobiletools.clock.helpers.Stopwatch import com.simplemobiletools.clock.helpers.Stopwatch.State import com.simplemobiletools.clock.helpers.Stopwatch.UpdateListener +import com.simplemobiletools.commons.helpers.isNougatPlus +import com.simplemobiletools.commons.helpers.isOreoPlus import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode @@ -70,10 +72,13 @@ class StopwatchService : Service() { val channelId = "simple_alarm_stopwatch" val label = getString(R.string.stopwatch) val importance = NotificationManager.IMPORTANCE_DEFAULT - NotificationChannel(channelId, label, importance).apply { - setSound(null, null) - notificationManager.createNotificationChannel(this) + if (isOreoPlus()) { + NotificationChannel(channelId, label, importance).apply { + setSound(null, null) + notificationManager.createNotificationChannel(this) + } } + return NotificationCompat.Builder(this, channelId) .setContentTitle(title) .setContentText(contentText) @@ -88,12 +93,8 @@ class StopwatchService : Service() { private fun updateNotification(totalTime: Long) { val formattedDuration = totalTime.getFormattedDuration() - notificationBuilder.setContentTitle(formattedDuration) - .setContentText(getString(R.string.stopwatch)) - notificationManager.notify( - STOPWATCH_RUNNING_NOTIF_ID, - notificationBuilder.build() - ) + notificationBuilder.setContentTitle(formattedDuration).setContentText(getString(R.string.stopwatch)) + notificationManager.notify(STOPWATCH_RUNNING_NOTIF_ID, notificationBuilder.build()) } private val updateListener = object : UpdateListener { @@ -104,7 +105,7 @@ class StopwatchService : Service() { } override fun onStateChanged(state: State) { - if (state == State.STOPPED) { + if (state == State.STOPPED && isNougatPlus()) { stopForeground(STOP_FOREGROUND_REMOVE) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/services/TimerService.kt b/app/src/main/kotlin/com/simplemobiletools/clock/services/TimerService.kt index 6f37c320..124b4eec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/services/TimerService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/services/TimerService.kt @@ -6,7 +6,9 @@ import android.app.NotificationManager import android.app.Service import android.content.Context import android.content.Intent +import android.os.Handler import android.os.IBinder +import android.os.Looper import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat import com.simplemobiletools.clock.R @@ -51,7 +53,10 @@ class TimerService : Service() { firstTimer.label.isNotEmpty() -> getString(R.string.timer_single_notification_label_msg, firstTimer.label) else -> resources.getQuantityString(R.plurals.timer_notification_msg, runningTimers.size, runningTimers.size) } - startForeground(TIMER_RUNNING_NOTIF_ID, notification(formattedDuration, contextText, firstTimer.id!!)) + + Handler(Looper.getMainLooper()).post { + startForeground(TIMER_RUNNING_NOTIF_ID, notification(formattedDuration, contextText, firstTimer.id!!)) + } } else { stopService() } @@ -116,7 +121,9 @@ class TimerService : Service() { } fun startTimerService(context: Context) { - ContextCompat.startForegroundService(context, Intent(context, TimerService::class.java)) + Handler(Looper.getMainLooper()).post { + ContextCompat.startForegroundService(context, Intent(context, TimerService::class.java)) + } } object TimerStopService