Skip to content

Commit

Permalink
Adds basic backup code.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderPaddyS committed Jul 11, 2023
1 parent ad7b9db commit 793487a
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
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 = [email protected]:SecUSo/privacy-friendly-backup-api.git
21 changes: 19 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "org.secuso.privacyfriendlysudoku"
minSdkVersion 16
targetSdkVersion 33
versionCode 12
versionName "3.0.3"
versionCode 13
versionName "3.1"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
Expand Down Expand Up @@ -44,4 +44,21 @@ dependencies {
implementation 'androidx.legacy:legacy-support-core-ui:1.0.0'
implementation 'androidx.legacy:legacy-support-core-utils:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

// Backup
implementation project(path: ':backup-api')
def work_version = "2.4.0"
implementation "androidx.work:work-runtime:$work_version"
implementation "androidx.work:work-runtime-ktx:$work_version"
androidTestImplementation "androidx.work:work-testing:$work_version"
implementation 'androidx.sqlite:sqlite-ktx:2.3.1'

constraints {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") {
because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib")
}
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
}
}
}
22 changes: 20 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.secuso.privacyfriendlysudoku.ui.view">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:name="org.secuso.privacyfriendlysudoku.SudokuApp"
android:name=".PFSudoku"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="org.secuso.privacyfriendlysudoku.ui.SplashActivity"
android:theme="@style/SplashTheme">
android:theme="@style/SplashTheme"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down Expand Up @@ -75,6 +77,22 @@
android:enabled="true"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" />

<service
android:name=".backup.PFABackupService"
android:enabled="true"
android:exported="true"
android:process=":backup"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="org.secuso.privacyfriendlybackup.api.pfa.PFAAuthService" />
</intent-filter>
</service>

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />
</application>

</manifest>
51 changes: 51 additions & 0 deletions app/src/main/java/org/secuso/privacyfriendlysudoku/PFSudoku.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
This file is part of Privacy Friendly Sudoku.
Privacy Friendly Sudoku is free software:
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or any later version.
Privacy Friendly Sudoku is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Privacy Friendly Sudoku. If not, see <http://www.gnu.org/licenses/>.
*/
package org.secuso.privacyfriendlysudoku

import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import android.util.Log
import androidx.appcompat.app.AppCompatDelegate
import androidx.work.Configuration
import org.secuso.privacyfriendlysudoku.backup.BackupCreator
import org.secuso.privacyfriendlysudoku.backup.BackupRestorer
import org.secuso.privacyfriendlybackup.api.pfa.BackupManager

class PFSudoku : Application(), Configuration.Provider {
override fun onCreate() {
BackupManager.backupCreator = BackupCreator()
BackupManager.backupRestorer = BackupRestorer()
super.onCreate()
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// channels
val channel = NotificationChannel(CHANNEL_ID, "Default", NotificationManager.IMPORTANCE_LOW)
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager?.createNotificationChannel(channel)
}
}

override fun getWorkManagerConfiguration(): Configuration {
return Configuration.Builder().setMinimumLoggingLevel(Log.INFO).build()
}

companion object {
const val CHANNEL_ID = "sudoku.0"
}
}
48 changes: 0 additions & 48 deletions app/src/main/java/org/secuso/privacyfriendlysudoku/SudokuApp.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.secuso.privacyfriendlysudoku.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 org.secuso.privacyfriendlysudoku.controller.database.DatabaseHelper
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, DatabaseHelper.DATABASE_NAME).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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package org.secuso.privacyfriendlysudoku.backup

import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import android.util.JsonReader
import android.util.Log
import androidx.annotation.NonNull
import org.secuso.privacyfriendlybackup.api.backup.DatabaseUtil
import org.secuso.privacyfriendlybackup.api.backup.FileUtil
import org.secuso.privacyfriendlybackup.api.pfa.IBackupRestorer
import org.secuso.privacyfriendlysudoku.controller.database.DatabaseHelper
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)
Log.d(TAG, "succesfully read database")
db.setTransactionSuccessful()
db.endTransaction()
db.close()

reader.endObject()

// copy file to correct location
val actualDatabaseFile = context.getDatabasePath(DatabaseHelper.DATABASE_NAME)

DatabaseUtil.deleteRoomDatabase(context, DatabaseHelper.DATABASE_NAME)

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()
Log.d("preference", name)
when (name) {
"pref_schedule_exercise",
"pref_keep_screen_on_during_exercise",
"REPEAT_STATUS",
"pref_hide_default_exercise_sets",
"pref_schedule_exercise_daystrigger",
"pref_exercise_continuous",
"IsFirstTimeLaunch",
"pref_schedule_random_exercise",
"REPEAT_EXERCISES" -> preferences.putBoolean(name, reader.nextBoolean())
"pref_exercise_time" -> preferences.putString(name, reader.nextString())
"FirstLaunchManager.PREF_PICKER_SECONDS",
"FirstLaunchManager.PREF_PICKER_MINUTES",
"FirstLaunchManager.PREF_BREAK_PICKER_SECONDS",
"FirstLaunchManager.PREF_PICKER_HOURS",
"FirstLaunchManager.PREF_BREAK_PICKER_MINUTES" -> preferences.putInt(name, reader.nextInt())
"pref_schedule_exercise_days" -> preferences.putStringSet(name, readPreferenceSet(reader))
"WORK_TIME",
"PAUSE TIME",
"pref_schedule_exercise_time",
"DEFAULT_EXERCISE_SET" -> preferences.putLong(name, reader.nextLong())
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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.secuso.privacyfriendlysudoku.backup

import org.secuso.privacyfriendlybackup.api.pfa.PFAAuthService

class PFABackupService : PFAAuthService()
Loading

0 comments on commit 793487a

Please sign in to comment.