-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad7b9db
commit 793487a
Showing
11 changed files
with
300 additions
and
55 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 = [email protected]: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
51 changes: 51 additions & 0 deletions
51
app/src/main/java/org/secuso/privacyfriendlysudoku/PFSudoku.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,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
48
app/src/main/java/org/secuso/privacyfriendlysudoku/SudokuApp.java
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
app/src/main/java/org/secuso/privacyfriendlysudoku/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,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" | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
app/src/main/java/org/secuso/privacyfriendlysudoku/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,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" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/org/secuso/privacyfriendlysudoku/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 org.secuso.privacyfriendlysudoku.backup | ||
|
||
import org.secuso.privacyfriendlybackup.api.pfa.PFAAuthService | ||
|
||
class PFABackupService : PFAAuthService() |
Oops, something went wrong.