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

Allow for exclusion of key-value pairs and use SupportSQLiteDatabase interface to be able to support other SQLiteDatabase implementations #1

Merged
merged 1 commit into from
Jul 21, 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
1 change: 1 addition & 0 deletions BackupAPI/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {

implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.sqlite:sqlite-ktx:2.1.0'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import android.content.Context
import android.database.Cursor.*
import android.database.sqlite.SQLiteDatabase
import android.util.JsonReader
import android.util.JsonWriter
import android.util.JsonToken
import android.util.JsonWriter
import androidx.core.database.getBlobOrNull
import androidx.core.database.getFloatOrNull
import androidx.core.database.getIntOrNull
import androidx.core.database.getStringOrNull
import androidx.sqlite.db.SupportSQLiteDatabase
import org.secuso.privacyfriendlybackup.api.util.toBase64
import java.io.StringReader
import java.io.StringWriter

/**
Expand All @@ -27,7 +27,7 @@ import java.io.StringWriter
object DatabaseUtil {

@JvmStatic
fun writeDatabase(writer: JsonWriter, db: SQLiteDatabase) {
fun writeDatabase(writer: JsonWriter, db: SupportSQLiteDatabase) {
writer.beginObject()
writer.name("version").value(db.version)
writer.name("content")
Expand All @@ -36,7 +36,7 @@ object DatabaseUtil {
}

@JvmStatic
fun writeDatabaseContent(writer: JsonWriter, db : SQLiteDatabase) {
fun writeDatabaseContent(writer: JsonWriter, db : SupportSQLiteDatabase) {
writer.beginArray()
val tableInfo = getTables(db)
for(table in tableInfo) {
Expand All @@ -56,10 +56,10 @@ object DatabaseUtil {
}

@JvmStatic
fun getTables(db : SQLiteDatabase) : List<Pair<String, String?>> {
fun getTables(db : SupportSQLiteDatabase) : List<Pair<String, String?>> {
val resultList = ArrayList<Pair<String, String?>>()

db.query("sqlite_master", arrayOf("name", "sql"), "type = ?", arrayOf("table"), null, null, null).use { cursor ->
db.query("SELECT name, sql FROM sqlite_master WHERE type='table'").use { cursor ->
cursor.moveToFirst()
while(!cursor.isAfterLast) {
val name = cursor.getStringOrNull(cursor.getColumnIndex("name")) ?: ""
Expand All @@ -72,9 +72,9 @@ object DatabaseUtil {
}

@JvmStatic
fun writeTable(writer: JsonWriter, db : SQLiteDatabase, table: String) {
fun writeTable(writer: JsonWriter, db : SupportSQLiteDatabase, table: String) {
writer.beginArray()
db.query(table, null, null, null, null, null, null).use { cursor ->
db.query("SELECT * FROM $table").use { cursor ->
cursor.moveToFirst()
while(!cursor.isAfterLast) {
writer.beginObject()
Expand Down Expand Up @@ -113,7 +113,7 @@ object DatabaseUtil {
}

@JvmStatic
fun readDatabaseContent(reader: JsonReader, db: SQLiteDatabase) {
fun readDatabaseContent(reader: JsonReader, db: SupportSQLiteDatabase) {
reader.beginArray()

while(reader.hasNext()) {
Expand All @@ -124,7 +124,7 @@ object DatabaseUtil {
}

@JvmStatic
fun readTable(reader: JsonReader, db: SQLiteDatabase) {
fun readTable(reader: JsonReader, db: SupportSQLiteDatabase) {
reader.beginObject()

// tableName
Expand All @@ -147,7 +147,7 @@ object DatabaseUtil {
}

@JvmStatic
fun readValues(reader: JsonReader, db: SQLiteDatabase, tableName: String) {
fun readValues(reader: JsonReader, db: SupportSQLiteDatabase, tableName: String) {
reader.beginArray()
while(reader.hasNext()) {
reader.beginObject()
Expand All @@ -163,7 +163,7 @@ object DatabaseUtil {
}
cv.put(name, value)
}
db.insert(tableName, null, cv)
db.insert(tableName, SQLiteDatabase.CONFLICT_NONE, cv)
reader.endObject()
}
reader.endArray()
Expand All @@ -182,14 +182,14 @@ object DatabaseUtil {

}

fun SQLiteDatabase.toJSON() : String {
fun SupportSQLiteDatabase.toJSON() : String {
val writer = JsonWriter(StringWriter())
writer.setIndent("")
DatabaseUtil.writeDatabase(writer, this)
return writer.toString()
}

fun SQLiteDatabase.toReadableJSON() : String {
fun SupportSQLiteDatabase.toReadableJSON() : String {
val writer = JsonWriter(StringWriter())
writer.setIndent(" ")
DatabaseUtil.writeDatabase(writer, this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ import android.util.JsonWriter
object PreferenceUtil {
@JvmStatic
fun writePreferences(writer: JsonWriter, pref: SharedPreferences) {
writePreferences(writer, pref, emptyArray())
}

@JvmStatic
fun writePreferences(writer: JsonWriter, pref: SharedPreferences, excludedKeys: Array<String>) {
writer.beginObject()
for((key, value) in pref.all) {
// Continue if key-value pair should be excluded from the backup
if(key in excludedKeys) {
continue
}

writer.name(key)

// App should know the types of the keys when it sees them.
Expand Down