Skip to content

Commit

Permalink
Merge pull request #100 from SecUSo/features/backup-integration
Browse files Browse the repository at this point in the history
Features/backup integration
  • Loading branch information
coderPaddyS authored Sep 19, 2023
2 parents 9a71600 + f98ee06 commit 3e6f7d9
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 64 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 = https://github.com/SecUSo/privacy-friendly-backup-api.git
28 changes: 23 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 29
compileSdkVersion 33

testOptions {
unitTests.returnDefaultValues = true
}

defaultConfig {
applicationId "org.secuso.privacyfriendlysudoku"
minSdkVersion 16
targetSdkVersion 29
versionCode 12
versionName "3.0.3"
minSdkVersion 17
targetSdkVersion 33
versionCode 13
versionName "3.1"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
Expand Down Expand Up @@ -43,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")
}
}
}
25 changes: 22 additions & 3 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="org.secuso.privacyfriendlysudoku.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 @@ -43,7 +45,8 @@
android:name="org.secuso.privacyfriendlysudoku.ui.GameActivity"
android:label="@string/title_activity_game_view"
android:launchMode="singleTask"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/AppTheme.NoActionBar"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
Expand Down Expand Up @@ -75,6 +78,22 @@
android:enabled="true"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" />

<service
android:name="org.secuso.privacyfriendlysudoku.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,69 @@
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.FileUtil
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.File
import java.io.OutputStream
import java.io.OutputStreamWriter
import java.util.Arrays

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)

Log.d(TAG, "Writing files")
writer.name("files")
writer.beginObject()
for (path in listOf("stats", "saves", "level")) {
val dir = context.getDir(path, 0)
Log.d(TAG,"writing dir ${dir.path}")
writer.name(path)
FileUtil.writePath(writer, dir, false)
}
writer.endObject()

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"
}
}
Loading

0 comments on commit 3e6f7d9

Please sign in to comment.