-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from SecUSo/feature/Backup-Integration
Feature/backup integration
- Loading branch information
Showing
72 changed files
with
552 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "libs/privacy-friendly-backup-api"] | ||
path = libs/privacy-friendly-backup-api | ||
url = https://github.com/SecUSo/privacy-friendly-backup-api.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...friendlyshoppinglist/secuso/org/privacyfriendlyshoppinglist/PFAShoppingListApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package privacyfriendlyshoppinglist.secuso.org.privacyfriendlyshoppinglist | ||
|
||
|
||
import android.app.Application | ||
import android.util.Log | ||
import androidx.work.Configuration | ||
import org.secuso.privacyfriendlybackup.api.pfa.BackupManager | ||
import privacyfriendlyshoppinglist.secuso.org.privacyfriendlyshoppinglist.backup.BackupCreator | ||
import privacyfriendlyshoppinglist.secuso.org.privacyfriendlyshoppinglist.backup.BackupRestorer | ||
|
||
class PFAShoppingListApplication : Application(), Configuration.Provider { | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
BackupManager.backupCreator = BackupCreator() | ||
BackupManager.backupRestorer = BackupRestorer() | ||
} | ||
|
||
override fun getWorkManagerConfiguration(): Configuration { | ||
return Configuration.Builder().setMinimumLoggingLevel(Log.INFO).build() | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...rivacyfriendlyshoppinglist/secuso/org/privacyfriendlyshoppinglist/backup/BackupCreator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package privacyfriendlyshoppinglist.secuso.org.privacyfriendlyshoppinglist.backup | ||
|
||
import android.content.Context | ||
import android.preference.PreferenceManager | ||
import android.util.JsonWriter | ||
import android.util.Log | ||
import org.secuso.privacyfriendlybackup.api.backup.DatabaseUtil.getSupportSQLiteOpenHelper | ||
import org.secuso.privacyfriendlybackup.api.backup.DatabaseUtil.writeDatabase | ||
import org.secuso.privacyfriendlybackup.api.backup.PreferenceUtil.writePreferences | ||
import org.secuso.privacyfriendlybackup.api.pfa.IBackupCreator | ||
import privacyfriendlyshoppinglist.secuso.org.privacyfriendlyshoppinglist.framework.persistence.DB | ||
import java.io.OutputStream | ||
import java.io.OutputStreamWriter | ||
|
||
class BackupCreator : IBackupCreator { | ||
override fun writeBackup(context: Context, outputStream: OutputStream): Boolean { | ||
Log.d(TAG, "createBackup() started") | ||
val outputStreamWriter = OutputStreamWriter(outputStream, Charsets.UTF_8) | ||
val writer = JsonWriter(outputStreamWriter) | ||
writer.setIndent("") | ||
|
||
try { | ||
writer.beginObject() | ||
|
||
Log.d(TAG, "Writing database") | ||
writer.name("database") | ||
|
||
val database = getSupportSQLiteOpenHelper(context, DB.APP.dbName).readableDatabase | ||
|
||
writeDatabase(writer, database) | ||
database.close() | ||
|
||
Log.d(TAG, "Writing preferences") | ||
writer.name("preferences") | ||
|
||
val pref = PreferenceManager.getDefaultSharedPreferences(context.applicationContext) | ||
writePreferences(writer, pref) | ||
|
||
writer.endObject() | ||
writer.close() | ||
} catch (e: Exception) { | ||
Log.e(TAG, "Error occurred", e) | ||
e.printStackTrace() | ||
return false | ||
} | ||
|
||
Log.d(TAG, "Backup created successfully") | ||
return true | ||
} | ||
|
||
companion object { | ||
const val TAG = "PFABackupCreator" | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
...ivacyfriendlyshoppinglist/secuso/org/privacyfriendlyshoppinglist/backup/BackupRestorer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package privacyfriendlyshoppinglist.secuso.org.privacyfriendlyshoppinglist.backup | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.preference.PreferenceManager | ||
import android.util.JsonReader | ||
import android.util.Log | ||
import org.secuso.privacyfriendlybackup.api.backup.DatabaseUtil | ||
import org.secuso.privacyfriendlybackup.api.backup.FileUtil | ||
import org.secuso.privacyfriendlybackup.api.pfa.IBackupRestorer | ||
import privacyfriendlyshoppinglist.secuso.org.privacyfriendlyshoppinglist.framework.persistence.DB | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.io.InputStreamReader | ||
import kotlin.system.exitProcess | ||
|
||
|
||
class BackupRestorer : IBackupRestorer { | ||
@Throws(IOException::class) | ||
private fun readDatabase(reader: JsonReader, context: Context) { | ||
reader.beginObject() | ||
val n1: String = reader.nextName() | ||
if (n1 != "version") { | ||
throw RuntimeException("Unknown value $n1") | ||
} | ||
val version: Int = reader.nextInt() | ||
val n2: String = reader.nextName() | ||
if (n2 != "content") { | ||
throw RuntimeException("Unknown value $n2") | ||
} | ||
|
||
Log.d(TAG, "Restoring database...") | ||
val restoreDatabaseName = "restoreDatabase" | ||
|
||
// delete if file already exists | ||
val restoreDatabaseFile = context.getDatabasePath(restoreDatabaseName) | ||
if (restoreDatabaseFile.exists()) { | ||
DatabaseUtil.deleteRoomDatabase(context, restoreDatabaseName) | ||
} | ||
|
||
// create new restore database | ||
val db = DatabaseUtil.getSupportSQLiteOpenHelper(context, restoreDatabaseName, version).writableDatabase | ||
|
||
db.beginTransaction() | ||
db.version = version | ||
|
||
Log.d(TAG, "Copying database contents...") | ||
DatabaseUtil.readDatabaseContent(reader, db) | ||
db.setTransactionSuccessful() | ||
db.endTransaction() | ||
db.close() | ||
|
||
reader.endObject() | ||
|
||
// copy file to correct location | ||
val actualDatabaseFile = context.getDatabasePath(DB.APP.dbName) | ||
|
||
DatabaseUtil.deleteRoomDatabase(context, DB.APP.dbName) | ||
|
||
FileUtil.copyFile(restoreDatabaseFile, actualDatabaseFile) | ||
Log.d(TAG, "Database Restored") | ||
|
||
// delete restore database | ||
DatabaseUtil.deleteRoomDatabase(context, restoreDatabaseName) | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun readPreferences(reader: JsonReader, preferences: SharedPreferences.Editor) { | ||
reader.beginObject() | ||
while (reader.hasNext()) { | ||
val name: String = reader.nextName() | ||
when (name) { | ||
"workoutMode", | ||
"privacyfriendlyshoppinglist.secuso.org.privacyfriendlyshoppinglist.pref.sort_asc_dec_key" -> preferences.putBoolean(name, reader.nextBoolean()) | ||
"privacyfriendlyshoppinglist.secuso.org.privacyfriendlyshoppinglist.pref.sort_by_key" -> preferences.putString(name, reader.nextString()) | ||
else -> throw RuntimeException("Unknown preference $name") | ||
} | ||
} | ||
reader.endObject() | ||
} | ||
|
||
private fun readPreferenceSet(reader: JsonReader): Set<String> { | ||
val preferenceSet = mutableSetOf<String>() | ||
|
||
reader.beginArray() | ||
while (reader.hasNext()) { | ||
preferenceSet.add(reader.nextString()); | ||
} | ||
reader.endArray() | ||
return preferenceSet | ||
} | ||
|
||
override fun restoreBackup(context: Context, restoreData: InputStream): Boolean { | ||
return try { | ||
val isReader = InputStreamReader(restoreData) | ||
val reader = JsonReader(isReader) | ||
val preferences = PreferenceManager.getDefaultSharedPreferences(context).edit() | ||
|
||
// START | ||
reader.beginObject() | ||
while (reader.hasNext()) { | ||
val type: String = reader.nextName() | ||
when (type) { | ||
"database" -> readDatabase(reader, context) | ||
"preferences" -> readPreferences(reader, preferences) | ||
else -> throw RuntimeException("Can not parse type $type") | ||
} | ||
} | ||
reader.endObject() | ||
preferences.commit() | ||
|
||
exitProcess(0) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
false | ||
} | ||
} | ||
|
||
companion object { | ||
const val TAG = "PFABackupRestorer" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...acyfriendlyshoppinglist/secuso/org/privacyfriendlyshoppinglist/backup/PFABackupService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package privacyfriendlyshoppinglist.secuso.org.privacyfriendlyshoppinglist.backup | ||
|
||
import org.secuso.privacyfriendlybackup.api.pfa.PFAAuthService | ||
|
||
class PFABackupService : PFAAuthService() |
2 changes: 1 addition & 1 deletion
2
...ndlyshoppinglist/secuso/org/privacyfriendlyshoppinglist/framework/ui/AbstractAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...yshoppinglist/secuso/org/privacyfriendlyshoppinglist/framework/ui/AbstractViewHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.