Skip to content

Commit

Permalink
Merge pull request #1390 from hussainmohd-a/v055e_rel
Browse files Browse the repository at this point in the history
V055h
  • Loading branch information
hussainmohd-a authored Apr 26, 2024
2 parents 406fa4e + 09d6437 commit d5b0428
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 41 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ dependencies {
fullImplementation 'com.github.kirich1409:viewbindingpropertydelegate-noreflection:1.5.9'

// from: https://jitpack.io/#celzero/firestack
download 'com.github.celzero:firestack:99521f3ca9@aar'
implementation 'com.github.celzero:firestack:99521f3ca9@aar'
download 'com.github.celzero:firestack:1f812ec9e6@aar'
implementation 'com.github.celzero:firestack:1f812ec9e6@aar'

// Work manager
implementation('androidx.work:work-runtime-ktx:2.9.0') {
Expand Down
54 changes: 24 additions & 30 deletions app/src/main/java/com/celzero/bravedns/backup/BackupAgent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
import android.os.SystemClock
import android.util.Log
import androidx.preference.PreferenceManager
import androidx.work.Worker
import androidx.work.WorkerParameters
Expand Down Expand Up @@ -92,18 +91,16 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :

val prefsBackupFile = File(tempDir, SHARED_PREFS_BACKUP_FILE_NAME)

if (DEBUG)
Log.d(
Logger.d(
LOG_TAG_BACKUP_RESTORE,
"backup process, temp file dir: ${tempDir.path}, prefs backup file: ${prefsBackupFile.path}"
)
processCompleted = saveSharedPreferencesToFile(context, prefsBackupFile)

if (processCompleted) {
if (DEBUG)
Log.d(LOG_TAG_BACKUP_RESTORE, "shared pref backup is added to the temp dir")
Logger.d(LOG_TAG_BACKUP_RESTORE, "shared pref backup is added to the temp dir")
} else {
Log.w(
Logger.w(
LOG_TAG_BACKUP_RESTORE,
"failed to add shared pref to temp backup dir, return failure"
)
Expand All @@ -113,9 +110,9 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :
processCompleted = saveDatabasesToFile(tempDir.path)

if (processCompleted) {
if (DEBUG) Log.d(LOG_TAG_BACKUP_RESTORE, "database backup is added to the temp dir")
Logger.d(LOG_TAG_BACKUP_RESTORE, "database backup is added to the temp dir")
} else {
Log.w(
Logger.w(
LOG_TAG_BACKUP_RESTORE,
"failed to add database to temp backup dir, return failure"
)
Expand All @@ -125,15 +122,15 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :
processCompleted = createMetaData(tempDir)

if (processCompleted) {
if (DEBUG) Log.d(LOG_TAG_BACKUP_RESTORE, "metadata is added to the temp dir")
Logger.d(LOG_TAG_BACKUP_RESTORE, "metadata is added to the temp dir")
} else {
Log.w(LOG_TAG_BACKUP_RESTORE, "failed to create metadata file, return failure")
Logger.w(LOG_TAG_BACKUP_RESTORE, "failed to create metadata file, return failure")
return false
}

return zipAndCopyToDestination(tempDir, backupFileUri)
} catch (e: Exception) {
Log.e(
Logger.e(
LOG_TAG_BACKUP_RESTORE,
"exception during backup process, reason? ${e.message}",
e
Expand All @@ -149,11 +146,11 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :
}

private fun createMetaData(backupDir: File): Boolean {
if (DEBUG) Log.d(LOG_TAG_BACKUP_RESTORE, "creating meta data file, path: ${backupDir.path}")
Logger.d(LOG_TAG_BACKUP_RESTORE, "creating meta data file, path: ${backupDir.path}")
// check if the file exists already, if yes, delete it
val file = File(backupDir, METADATA_FILENAME)
if (file.exists()) {
if (DEBUG) Log.d(LOG_TAG_BACKUP_RESTORE, "metadata file exists, deleting it")
Logger.d(LOG_TAG_BACKUP_RESTORE, "metadata file exists, deleting it")
file.delete()
filesPathToZip.remove(file.absolutePath)
}
Expand All @@ -169,7 +166,7 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :
filesPathToZip.add(metadataFile.absolutePath)
return true
} catch (e: Exception) {
Log.e(
Logger.e(
LOG_TAG_BACKUP_RESTORE,
"exception while creating meta data file, ${e.message}",
e
Expand All @@ -194,7 +191,7 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :
private fun zipAndCopyToDestination(tempDir: File, destUri: Uri): Boolean {
val bZipSucceeded: Boolean = zip(filesPathToZip, tempDir.path)

Log.i(
Logger.i(
LOG_TAG_BACKUP_RESTORE,
"backup zip completed, is success? $bZipSucceeded, proceed to copy $destUri"
)
Expand All @@ -211,21 +208,21 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :
// write access to the destination dir.
val copySucceeded: Boolean = copyWithStream(inputStream, outputStream)
return if (copySucceeded) {
Log.i(
Logger.i(
LOG_TAG_BACKUP_RESTORE,
"Copy completed, delete the temp dir ${tempZipFile.path}"
)
deleteResidue(tempZipFile)
true
} else {
Log.w(
Logger.w(
LOG_TAG_BACKUP_RESTORE,
"copy failed to destination dir, path: ${zipFileUri.path}"
)
false
}
} else {
Log.w(LOG_TAG_BACKUP_RESTORE, "backup zip failed, do not proceed")
Logger.w(LOG_TAG_BACKUP_RESTORE, "backup zip failed, do not proceed")
return false
}
}
Expand All @@ -234,8 +231,7 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :
val files = getRethinkDatabase(context)?.listFiles() ?: return false

for (f in files) {
if (DEBUG)
Log.d(
Logger.d(
LOG_TAG_BACKUP_RESTORE,
"file ${f.name} found in database dir (${f.absolutePath})"
)
Expand All @@ -248,8 +244,7 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :
}*/
val databaseFile =
backUpFile(f.absolutePath, constructDbFileName(path, f.name)) ?: return false
if (DEBUG)
Log.d(LOG_TAG_BACKUP_RESTORE, "file ${databaseFile.name} added to backup dir")
Logger.i(LOG_TAG_BACKUP_RESTORE, "file ${databaseFile.name} added to backup dir")
filesPathToZip.add(databaseFile.absolutePath)
}

Expand All @@ -263,16 +258,15 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :
private fun saveSharedPreferencesToFile(context: Context, prefFile: File): Boolean {
var output: ObjectOutputStream? = null

if (DEBUG)
Log.d(LOG_TAG_BACKUP_RESTORE, "begin shared pref copy, file path:${prefFile.path}")
Logger.i(LOG_TAG_BACKUP_RESTORE, "begin shared pref copy, file path:${prefFile.path}")
val sharedPrefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)

try {
output = ObjectOutputStream(FileOutputStream(prefFile))
val allPrefs = sharedPrefs.all
output.writeObject(allPrefs)
} catch (e: Exception) {
Log.e(LOG_TAG_BACKUP_RESTORE, "exception during shared pref backup, ${e.message}", e)
Logger.e(LOG_TAG_BACKUP_RESTORE, "exception during shared pref backup, ${e.message}", e)
return false
} finally {
try {
Expand All @@ -290,7 +284,7 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :

private fun backUpFile(backupFilePath: String?, destFilePath: String?): File? {
if (backupFilePath == null || destFilePath == null) {
Log.w(
Logger.w(
LOG_TAG_BACKUP_RESTORE,
"invalid backup info during db backup, file: $backupFilePath, destination: $destFilePath"
)
Expand All @@ -304,7 +298,7 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :

private fun zip(files: List<String>, zipDirectory: String): Boolean {
val outputFileName = zipDirectory + File.separator + TEMP_ZIP_FILE_NAME
if (DEBUG) Log.d(LOG_TAG_BACKUP_RESTORE, "files: $files, output: $outputFileName")
Logger.d(LOG_TAG_BACKUP_RESTORE, "files: $files, output: $outputFileName")
return try {
val dest = FileOutputStream(outputFileName)
val out = ZipOutputStream(BufferedOutputStream(dest))
Expand All @@ -321,14 +315,14 @@ class BackupAgent(val context: Context, workerParams: WorkerParameters) :
out.write(data, 0, count)
}
origin.close()
if (DEBUG) Log.d(LOG_TAG_BACKUP_RESTORE, "$file added to zip, path: $file")
Logger.d(LOG_TAG_BACKUP_RESTORE, "$file added to zip, path: $file")
}
out.close()
out.close()
Log.i(LOG_TAG_BACKUP_RESTORE, "$files added to zip")
Logger.i(LOG_TAG_BACKUP_RESTORE, "$files added to zip")
true
} catch (e: Exception) {
Log.e(LOG_TAG_BACKUP_RESTORE, "error while adding files to zip dir, ${e.message}", e)
Logger.e(LOG_TAG_BACKUP_RESTORE, "error while adding files to zip dir, ${e.message}", e)
false
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/celzero/bravedns/data/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.celzero.bravedns.data
import Logger
import Logger.LOG_TAG_VPN
import android.content.Context
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.celzero.bravedns.R
Expand Down Expand Up @@ -397,7 +396,7 @@ internal constructor(
BraveMode.DNS.mode,
BraveMode.DNS_FIREWALL.mode -> determineTunDnsMode()
else -> {
Log.wtf(LOG_TAG_VPN, "Invalid brave mode: ${persistentState.braveMode}")
Logger.e(LOG_TAG_VPN, "invalid brave mode: ${persistentState.braveMode}")
TunDnsMode.NONE
}
}
Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/com/celzero/bravedns/util/Utilities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ import com.google.gson.JsonParser
import inet.ipaddr.HostName
import inet.ipaddr.IPAddress
import inet.ipaddr.IPAddressString
import kotlinx.coroutines.launch
import okio.HashingSink
import okio.blackholeSink
import okio.buffer
import okio.source
import java.io.File
import java.io.IOException
import java.io.InputStream
Expand All @@ -82,6 +77,11 @@ import java.util.Date
import java.util.Locale
import java.util.concurrent.TimeUnit
import kotlin.math.ln
import kotlinx.coroutines.launch
import okio.HashingSink
import okio.blackholeSink
import okio.buffer
import okio.source

object Utilities {

Expand Down Expand Up @@ -391,8 +391,13 @@ object Utilities {
return vpnService?.isAlwaysOn == true
}

val alwaysOn = Settings.Secure.getString(context.contentResolver, "always_on_vpn_app")
return context.packageName == alwaysOn
return try {
val alwaysOn = Settings.Secure.getString(context.contentResolver, "always_on_vpn_app")
context.packageName == alwaysOn
} catch (e: Exception) {
Logger.e(LOG_TAG_VPN, "Failure while retrieving Settings.Secure value ${e.message}", e)
false
}
}

// This function is not supported from version 12 onwards.
Expand Down

0 comments on commit d5b0428

Please sign in to comment.