Skip to content

Commit

Permalink
Added the ability to send pfa error back to backup app
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamuno committed Aug 23, 2022
1 parent af64403 commit de5ed1f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,25 @@ object PfaApi {

object BackupApi {
// API Version
const val API_VERSION = 1
const val API_VERSION = 2

// Connect Actions
const val BACKUP_CONNECT_ACTION = "org.secuso.privacyfriendlybackup.services.BackupService"

// Command Actions
const val ACTION_SEND_MESSENGER = "BackupApi.ACTION_SEND_MESSENGER"
const val ACTION_SEND_ERROR = "BackupApi.ACTION_SEND_ERROR"

// Extras
const val EXTRA_MESSENGER = "BackupApi.EXTRA_MESSENGER"
const val EXTRA_ERROR = "BackupApi.EXTRA_ERROR"

// Messenger Commands
const val MESSAGE_BACKUP = 1
const val MESSAGE_RESTORE = 2
const val MESSAGE_DONE = 3
const val MESSAGE_ERROR = 4

// Error Types
const val ERROR_GENERIC = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import java.io.OutputStream
* @author Christopher Beckmann
*/
interface IBackupCreator {
fun writeBackup(context: Context, outputStream: OutputStream)
fun writeBackup(context: Context, outputStream: OutputStream) : Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.os.Messenger
import android.os.ParcelFileDescriptor
Expand Down Expand Up @@ -57,7 +58,7 @@ class BackupApiConnection(
}
}

fun send(action : String) {
fun send(action : String, extras : Bundle? = null) {
if(!isBound()) {
mBackupApiListener?.onError(
PfaError(
Expand All @@ -69,6 +70,7 @@ class BackupApiConnection(
}
val result : Intent = mService!!.send(Intent().apply {
putExtra(EXTRA_API_VERSION, mApiVersion)
extras?.let { putExtras(extras) }
setAction(action)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.secuso.privacyfriendlybackup.api.worker

import android.content.ContentValues.TAG
import android.content.Context
import android.os.*
import android.util.Log
Expand All @@ -10,26 +9,22 @@ import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.secuso.privacyfriendlybackup.api.IBackupService
import org.secuso.privacyfriendlybackup.api.common.BackupApi
import org.secuso.privacyfriendlybackup.api.pfa.BackupDataStore
import org.secuso.privacyfriendlybackup.api.common.BackupApi.ACTION_SEND_MESSENGER
import org.secuso.privacyfriendlybackup.api.common.BackupApi.MESSAGE_BACKUP
import org.secuso.privacyfriendlybackup.api.common.BackupApi.MESSAGE_DONE
import org.secuso.privacyfriendlybackup.api.common.BackupApi.MESSAGE_ERROR
import org.secuso.privacyfriendlybackup.api.common.BackupApi.MESSAGE_RESTORE
import org.secuso.privacyfriendlybackup.api.common.CommonApiConstants
import org.secuso.privacyfriendlybackup.api.common.PfaApi.EXTRA_CONNECT_PACKAGE_NAME
import org.secuso.privacyfriendlybackup.api.common.PfaError
import org.secuso.privacyfriendlybackup.api.pfa.BackupManager
import org.secuso.privacyfriendlybackup.api.util.BackupApiConnection
import org.secuso.privacyfriendlybackup.api.util.readString
import java.io.InputStream
import java.io.OutputStream
import java.lang.Exception
import java.lang.ref.WeakReference
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.coroutines.CoroutineContext

/**
* @author Christopher Beckmann
Expand Down Expand Up @@ -90,13 +85,20 @@ class ConnectBackupWorker(val context : Context, params: WorkerParameters) : Cor
Log.d(TAG, "handleBackup() started")
backupInProgress.set(true)

if(checkAndSendError()) {
errorOccurred = true
workDone = true
return
}

Log.d(TAG, "Retrieve backup from storage")
var backupData = BackupDataStore.getBackupData(context)

// no backup data available
Log.d(TAG, "Check if backup data is available")
if (backupData == null) {
Log.d(TAG, "ERROR: Backup data is null")
sendError()
errorOccurred = true
workDone = true
return
Expand All @@ -123,8 +125,29 @@ class ConnectBackupWorker(val context : Context, params: WorkerParameters) : Cor
Log.d(TAG, "handleBackup() finished")
}

private fun checkAndSendError() : Boolean {
// check if an error occured and we need to send it to the backup app
if(inputData.getInt(CommonApiConstants.RESULT_CODE, CommonApiConstants.RESULT_CODE_SUCCESS) == CommonApiConstants.RESULT_CODE_ERROR) {
sendError()
return true
}
return false
}

private fun sendError() {
mConnection.send(BackupApi.ACTION_SEND_ERROR, Bundle().apply {
putInt(BackupApi.EXTRA_ERROR, BackupApi.ERROR_GENERIC)
})
}

fun handleRestore() {
restoreInProgress.set(true)

if(checkAndSendError()) {
errorOccurred = true
workDone = true
}

val stream = mConnection.getRestoreData()

var restoreData : String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import android.util.Log
import androidx.work.Data
import androidx.work.Worker
import androidx.work.WorkerParameters
import org.secuso.privacyfriendlybackup.api.common.BackupApi
import org.secuso.privacyfriendlybackup.api.common.CommonApiConstants
import org.secuso.privacyfriendlybackup.api.common.PfaApi
import org.secuso.privacyfriendlybackup.api.pfa.BackupDataStore
import org.secuso.privacyfriendlybackup.api.pfa.BackupManager
import java.io.ByteArrayInputStream
Expand All @@ -21,13 +24,21 @@ class CreateBackupWorker(val context : Context, params: WorkerParameters) : Work

Log.d("PFA BackupWorker", "creating backup...")
val outStream = ByteArrayOutputStream()
BackupManager.backupCreator?.writeBackup(context, outStream) ?: return Result.failure()
val success = BackupManager.backupCreator?.writeBackup(context, outStream) ?: return Result.success(Data.Builder().apply {
putInt(CommonApiConstants.RESULT_CODE, CommonApiConstants.RESULT_CODE_ERROR)
}.build())

if(!success) {
return Result.success(Data.Builder().apply {
putInt(CommonApiConstants.RESULT_CODE, CommonApiConstants.RESULT_CODE_ERROR)
}.build())
}
Log.d("PFA BackupWorker", "backup created")
outStream.close()

BackupDataStore.saveBackupData(context, ByteArrayInputStream(outStream.toByteArray()))

return Result.success()
return Result.success(Data.EMPTY)
}

}

0 comments on commit de5ed1f

Please sign in to comment.