Skip to content

Commit

Permalink
fixed crashing on backup
Browse files Browse the repository at this point in the history
  • Loading branch information
suyash01 committed May 8, 2024
1 parent 01f082e commit b940d7f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 33 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ android {
applicationId = "com.suyash.creditmanager"
minSdk = 26
targetSdk = 34
versionCode = 11
versionName = "1.0.10"
versionCode = 12
versionName = "1.0.11"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />

Expand Down Expand Up @@ -36,6 +37,10 @@
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider>

<service android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync"
tools:node="merge"/>

</application>

Expand Down
18 changes: 8 additions & 10 deletions app/src/main/java/com/suyash/creditmanager/CreditManagerApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ class CreditManagerApp : Application(), Configuration.Provider {
override fun onCreate() {
super.onCreate()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,
"Backup",
NotificationManager.IMPORTANCE_DEFAULT
)
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
val channel = NotificationChannel(
CHANNEL_ID,
"Backup",
NotificationManager.IMPORTANCE_DEFAULT
)
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}

override val workManagerConfiguration: Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.suyash.creditmanager.data.backup

import android.annotation.SuppressLint
import android.app.NotificationManager
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.net.Uri
import androidx.core.app.NotificationCompat
import androidx.core.net.toUri
import androidx.hilt.work.HiltWorker
import androidx.work.CoroutineWorker
import androidx.work.ExistingWorkPolicy
import androidx.work.ForegroundInfo
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkInfo
import androidx.work.WorkManager
Expand Down Expand Up @@ -42,13 +43,10 @@ class BackupWorker @AssistedInject constructor(
private val txnCategoryUseCase: TxnCategoryUseCase
) : CoroutineWorker(appContext, workerParams) {

override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
val notificationManager =
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
private val notificationManager =
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager

override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
val fileUri = inputData.getString(LOCATION_URI_KEY)?.toUri()
?: return@withContext Result.failure()

Expand All @@ -58,11 +56,7 @@ class BackupWorker @AssistedInject constructor(
when (type) {
"BACKUP" -> {
// Backup
val foregroundInfo = ForegroundInfo(
NOTIFICATION_ID,
notification.setContentTitle("Backup in progress").build()
)
setForeground(foregroundInfo)
setNotificationStart("Backup in progress")
val backupData = BackupData(
creditCards = creditCardUseCases.getCreditCards().first(),
emis = emiUseCases.getEMIs().first(),
Expand All @@ -73,12 +67,8 @@ class BackupWorker @AssistedInject constructor(
}

"RESTORE" -> {
val foregroundInfo = ForegroundInfo(
NOTIFICATION_ID,
notification.setContentTitle("Restore in progress").build()
)
setForeground(foregroundInfo)
// Restore
setNotificationStart("Restore in progress")
val restoredBackupData = readBackupDataFromJsonFile(fileUri)
restoreData(restoredBackupData)
}
Expand All @@ -87,10 +77,37 @@ class BackupWorker @AssistedInject constructor(
} catch (e: Exception) {
Result.failure()
} finally {
notificationManager.cancel(NOTIFICATION_ID)
setNotificationEnd(
"${type.lowercase().replaceFirstChar { it.uppercaseChar() }} Completed"
)
}
}

@SuppressLint("MissingPermission")
private fun setNotificationStart(msg: String) {
val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(msg)
.setAutoCancel(false)
.setOngoing(true)
.build()

notificationManager.notify(NOTIFICATION_ID, notification)
}

private fun setNotificationEnd(msg: String) {
val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(msg)
.setAutoCancel(true)
.setOngoing(false)
.build()

notificationManager.notify(NOTIFICATION_ID, notification)
}

data class BackupData(
val creditCards: List<CreditCard>,
val emis: List<EMI>,
Expand Down Expand Up @@ -153,7 +170,7 @@ class BackupWorker @AssistedInject constructor(

companion object {
private const val CHANNEL_ID = "BackupChannel"
private const val NOTIFICATION_ID = 123
private const val NOTIFICATION_ID = 1
private const val TAG_AUTO = "BackupWorker"
private const val TAG_MANUAL = "$TAG_AUTO:manual"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package com.suyash.creditmanager.presentation

import android.Manifest
import android.os.Build
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.core.app.ActivityCompat
import com.suyash.creditmanager.presentation.main.CreditManager
import com.suyash.creditmanager.ui.theme.CreditManagerTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@ExperimentalMaterial3Api
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
0
)
}

setContent {
CreditManagerTheme {
Surface(
Expand Down

0 comments on commit b940d7f

Please sign in to comment.