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

Fixed restore actions not completing correctly and encryption api support #9

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion BackupAPI
12 changes: 7 additions & 5 deletions BackupApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

def pfaFile = rootProject.file('pfa.properties')

android {
signingConfigs {
'pfa-key' {
def pfaFile = rootProject.file('pfa.properties')

if (pfaFile.exists()) {
if (pfaFile.exists()) {
'pfa-key' {
def pfa = new Properties()
pfa.load(new FileInputStream(pfaFile))

Expand Down Expand Up @@ -48,7 +48,9 @@ android {
}
debug {
debuggable true
signingConfig signingConfigs.'pfa-key'
if (pfaFile.exists()) {
signingConfig signingConfigs.'pfa-key'
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions BackupApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
package="org.secuso.privacyfriendlybackup">

<queries>
<package android:name="org.sufficientlysecure.keychain" />
<intent>
<action android:name="org.secuso.privacyfriendlybackup.api.pfa.PFAAuthService" />
</intent>
<intent>
<action android:name="org.openintents.openpgp.IOpenPgpService2" />
</intent>
</queries>

<application
Expand Down Expand Up @@ -105,8 +109,7 @@
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.backup.*" />
</intent-filter>
</activity>
<activity android:name=".ui.inspection.DataInspectionActivity"
android:parentActivityName=""/>
<activity android:name=".ui.inspection.DataInspectionActivity"/>
<activity android:name=".ui.main.MainActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.secuso.privacyfriendlybackup.data

import android.content.Context
import androidx.preference.PreferenceManager
import org.secuso.privacyfriendlybackup.BackupApplication
import org.secuso.privacyfriendlybackup.data.room.BackupDatabase
import org.secuso.privacyfriendlybackup.data.room.model.BackupJob
import org.secuso.privacyfriendlybackup.data.room.model.PFAJob
Expand Down Expand Up @@ -72,11 +73,13 @@ class BackupJobManager private constructor(
nextJob = if(isEncryptionEnabled) encryptId else storeId
)
db.backupJobDao().insert(pfaBackupJob)
(context.applicationContext as BackupApplication).schedulePeriodicWork()
}

suspend fun cancelAllJobs(packageName: String) {
db.backupJobDao().deleteAllForPackage(packageName)
db.pfaJobDao().deleteAllForPackage(packageName)
(context.applicationContext as BackupApplication).schedulePeriodicWork()
}

/**
Expand Down Expand Up @@ -131,5 +134,6 @@ class BackupJobManager private constructor(
dataId = if(metadata._id != -1L) metadata._id else metadataId
)
db.backupJobDao().insert(loadJob)
(context.applicationContext as BackupApplication).schedulePeriodicWork()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ApplicationOverviewFragment : BaseFragment(), ApplicationAdapter.ManageLis

setHasOptionsMenu(true)

(requireActivity().applicationContext as BackupApplication).schedulePeriodicWork()

savedInstanceState ?: return

val savedOldMode = savedInstanceState.getString("oldMode")
Expand Down Expand Up @@ -62,7 +64,7 @@ class ApplicationOverviewFragment : BaseFragment(), ApplicationAdapter.ManageLis

displayNoElementsImage(data.isEmpty())

(requireActivity().application as BackupApplication).schedulePeriodicWork()
//(requireActivity().application as BackupApplication).schedulePeriodicWork()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.secuso.privacyfriendlybackup.ui.common

import android.animation.Animator
import android.animation.ValueAnimator
import android.content.Context
import android.content.res.Configuration
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
Expand Down Expand Up @@ -56,11 +57,11 @@ abstract class BaseFragment : Fragment() {
}
}

protected fun playColorAnimationRes(@ColorRes colorFromId: Int, @ColorRes colorToId: Int, applyAnimation : (a : ValueAnimator) -> Unit = this::defaultFadeAnimation) : Animator {
protected fun playColorAnimationRes(@ColorRes colorFromId: Int, @ColorRes colorToId: Int, duration : Long = 250, applyAnimation : (a : ValueAnimator) -> Unit = this::defaultFadeAnimation) : Animator {
val colorFrom = ContextCompat.getColor(requireContext(), colorFromId)
val colorTo = ContextCompat.getColor(requireContext(), colorToId)

return playColorAnimation(colorFrom, colorTo, 250, applyAnimation)
return playColorAnimation(colorFrom, colorTo, duration, applyAnimation)
}

protected fun playColorAnimation(@ColorInt colorFrom: Int, @ColorInt colorTo: Int, duration : Long = 250, applyAnimation : (a : ValueAnimator) -> Unit = this::defaultFadeAnimation) : Animator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.secuso.privacyfriendlybackup.ui.main.MainActivity
* in a [MainActivity].
*/
class DisplayMenuItemActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_display_menu_item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import androidx.work.*
import org.openintents.openpgp.util.OpenPgpApi
import org.secuso.privacyfriendlybackup.BackupApplication
import org.secuso.privacyfriendlybackup.preference.PreferenceKeys.PREF_ENCRYPTION_KEY
import org.secuso.privacyfriendlybackup.preference.PreferenceKeys.PREF_ENCRYPTION_PASSPHRASE
import org.secuso.privacyfriendlybackup.worker.EncryptionWorker
Expand Down Expand Up @@ -88,6 +89,9 @@ class UserInteractionRequiredActivity : AppCompatActivity() {

edit.apply()

// restart the encryption worker
(applicationContext as BackupApplication).schedulePeriodicWork()

// after the answer was received, there is no need to display this translucent activity
finish()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ class DataInspectionActivity : AppCompatActivity() {
setContentView(R.layout.data_inspection_activity)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

viewModel = ViewModelProvider(this).get(DataInspectionViewModel::class.java)
viewModel = ViewModelProvider(this)[DataInspectionViewModel::class.java]

if (savedInstanceState == null) {

val fragment = DataInspectionFragment.newInstance()
fragment.arguments = Bundle().apply {
putLong(EXTRA_DATA_ID, id)
Expand All @@ -57,8 +56,4 @@ class DataInspectionActivity : AppCompatActivity() {
override fun onBackPressed() {
finish()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.secuso.privacyfriendlybackup.ui.inspection

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.PorterDuff
Expand All @@ -11,20 +12,23 @@ import android.view.*
import android.widget.CheckBox
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import androidx.preference.PreferenceManager
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.data_inspection_fragment.*
import kotlinx.android.synthetic.main.item_application_job.*
import org.openintents.openpgp.OpenPgpSignatureResult
import org.secuso.privacyfriendlybackup.R
import org.secuso.privacyfriendlybackup.preference.PreferenceKeys
import org.secuso.privacyfriendlybackup.ui.backup.BackupOverviewFragment
import java.io.FileNotFoundException


Expand All @@ -46,6 +50,8 @@ class DataInspectionFragment : Fragment() {

private lateinit var viewModel: DataInspectionViewModel

//private lateinit var test : ActivityResultLauncher<>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
Expand Down Expand Up @@ -194,7 +200,7 @@ class DataInspectionFragment : Fragment() {
super.onActivityCreated(savedInstanceState)

val activity = requireActivity()
viewModel = ViewModelProvider(activity).get(DataInspectionViewModel::class.java)
viewModel = ViewModelProvider(activity)[DataInspectionViewModel::class.java]

encryptionEnabled = PreferenceManager.getDefaultSharedPreferences(requireActivity()).getBoolean(PreferenceKeys.PREF_ENCRYPTION_ENABLE, false)

Expand Down Expand Up @@ -222,10 +228,10 @@ class DataInspectionFragment : Fragment() {
data_inspection_load_status_name.setText(it.descriptionRes)
data_inspection_load_status_image.setColorFilter(ContextCompat.getColor(requireActivity(), it.colorRes))
when(it) {
LoadStatus.UNKNOWN -> {
LoadStatus.UNKNOWN, null -> {
data_inspection_load_status.visibility = View.GONE
}
LoadStatus.ERROR -> {
LoadStatus.ERROR_INVALID_JSON, LoadStatus.ERROR -> {
data_inspection_load_status.visibility = View.VISIBLE
}
LoadStatus.LOADING -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class BackupJobManagerWorker(val context: Context, params: WorkerParameters) : C
val processedJobs = jobs.filter { it.active }
val unprocessedJobs = jobs.filter { !it.active }

val jobsToCheck = jobs.toMutableList()

for(job in processedJobs) {
val workInfoFuture = WorkManager.getInstance(context).getWorkInfosByTag(job.getWorkerTag())
val workInfoList = workInfoFuture.get()
Expand All @@ -57,6 +59,9 @@ class BackupJobManagerWorker(val context: Context, params: WorkerParameters) : C
// if it failed - try again
job.active = false
jobDao.update(job)

deleteFollowingJobs(job, jobsToCheck)
// TODO: Show Toast - Job Failed?
}
}
}
Expand Down Expand Up @@ -126,17 +131,35 @@ class BackupJobManagerWorker(val context: Context, params: WorkerParameters) : C
}
}


coroutineScope {
launch {
delay(1500L)
(context.applicationContext as BackupApplication).schedulePeriodicWork()
// continue calling the service if there are still jobs left to do
if(jobsToCheck.isNotEmpty()) {
coroutineScope {
launch {
delay(1500L)
(context.applicationContext as BackupApplication).schedulePeriodicWork()
}
}
}

return Result.success()
}

private fun deleteFollowingJobs(job: BackupJob, jobsToCheck: MutableList<BackupJob>, jobsToDelete : MutableList<BackupJob> = mutableListOf<BackupJob>()) {
jobsToDelete.add(job)

if(job.nextJob == null) {
return
}

for(j in jobsToCheck) {
if(j._id == job.nextJob) {
deleteFollowingJobs(j, jobsToCheck, jobsToDelete)
}
}

jobsToCheck.removeAll(jobsToDelete)
}

private fun enqueueEncryptionWork(job : BackupJob) {
val encryptionWork = createEncryptionWorkRequest(job, true, pref)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class EncryptionWorker(val context: Context, params: WorkerParameters) : Corouti
context,
requestCode,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
setContentIntent(pendingIntent)
}.build()
Expand Down