Skip to content
Closed
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
36 changes: 18 additions & 18 deletions AnkiDroid/src/main/java/com/ichi2/anki/BackupManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ open class BackupManager {
Regex("(?:collection|backup)-((\\d{4})-(\\d{2})-(\\d{2})-(\\d{2})[.-](\\d{2}))(?:\\.\\d{2})?.colpkg")
}

private val legacyDateFormat = SimpleDateFormat("yyyy-MM-dd-HH-mm")
private val newDateFormat = SimpleDateFormat("yyyy-MM-dd-HH.mm")

fun getBackupDirectory(ankidroidDir: File): File {
val directory = File(ankidroidDir, BACKUP_SUFFIX)
if (!directory.isDirectory && !directory.mkdirs()) {
Expand All @@ -61,12 +58,12 @@ open class BackupManager {
return directory
}

fun enoughDiscSpace(path: File): Boolean = getFreeDiscSpace(path) >= MIN_FREE_SPACE * 1024 * 1024
fun enoughDiskSpace(path: File): Boolean = getFreeDiskSpace(path) >= MIN_FREE_SPACE * 1024 * 1024

/**
* Get free disc space in bytes from path to Collection
* Get free disk space in bytes from path to Collection
*/
fun getFreeDiscSpace(file: File): Long = getFreeDiskSpace(file, (MIN_FREE_SPACE * 1024 * 1024).toLong())
fun getFreeDiskSpace(file: File): Long = getFreeDiskSpace(file, (MIN_FREE_SPACE * 1024 * 1024).toLong())

/**
* Run the sqlite3 command-line-tool (if it exists) on the collection to dump to a text file
Expand All @@ -82,7 +79,7 @@ open class BackupManager {
col.close()

// repair file
val execString = "sqlite3 $colPath .dump | sqlite3 $colPath.tmp"
val execString = "sqlite3 \"$colPath\" .dump | sqlite3 \"$colPath.tmp\""
Timber.i("repairCollection - Execute: %s", execString)
try {
val cmd = arrayOf("/system/bin/sh", "-c", execString)
Expand Down Expand Up @@ -142,7 +139,7 @@ open class BackupManager {
// move all connected files (like journals, directories...) too
val colName = colFile.name
val directory = File(colFile.parent!!)
for (f in directory.listFiles()!!) {
for (f in directory.listFiles().orEmpty()) {
if (f.name.startsWith(colName) &&
!f.renameTo(File(getBrokenDirectory(colFile.parentFile!!), f.name.replace(colName, movedFilename)))
) {
Expand All @@ -163,8 +160,10 @@ open class BackupManager {
/**
* @return date in string if it matches backup naming pattern or null if not
*/
fun parseBackupTimeString(timeString: String): Date? =
try {
fun parseBackupTimeString(timeString: String): Date? {
val legacyDateFormat = SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.US)
val newDateFormat = SimpleDateFormat("yyyy-MM-dd-HH.mm", Locale.US)
return try {
legacyDateFormat.parse(timeString)
} catch (_: ParseException) {
try {
Expand All @@ -173,6 +172,7 @@ open class BackupManager {
null
}
}
}

/**
* @return date in fileName if it matches backup naming pattern or null if not
Expand All @@ -184,7 +184,7 @@ open class BackupManager {
* in order of creation.
*/
fun getBackups(colFile: File): Array<File> {
val files = getBackupDirectory(colFile.parentFile!!).listFiles() ?: arrayOf()
val files = getBackupDirectory(colFile.parentFile!!).listFiles().orEmpty()
val backups =
files
.mapNotNull { file ->
Expand Down Expand Up @@ -219,8 +219,8 @@ open class BackupManager {

fun removeDir(dir: File): Boolean {
if (dir.isDirectory) {
val files = dir.listFiles()
for (aktFile in files!!) {
val files = dir.listFiles().orEmpty()
for (aktFile in files) {
removeDir(aktFile)
}
}
Expand All @@ -235,13 +235,13 @@ open class BackupManager {
* in format such as "02 Nov 2022" instead of "11/2/22" or "2/11/22", which can be confusing.
*/
class LocalizedUnambiguousBackupTimeFormatter {
private val formatter =
SimpleDateFormat(
DateFormat.getBestDateTimePattern(Locale.getDefault(), "dd MMM yyyy HH:mm"),
)

fun getTimeOfBackupAsText(file: File): String {
val backupDate = BackupManager.getBackupDate(file.name) ?: return file.name
val formatter =
SimpleDateFormat(
DateFormat.getBestDateTimePattern(Locale.getDefault(), "dd MMM yyyy HH:mm"),
Locale.getDefault(),
)
return formatter.format(backupDate)
}
}
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ open class DeckPicker :
preferences: SharedPreferences,
skip: Int,
) {
if (!BackupManager.enoughDiscSpace(CollectionHelper.getCurrentAnkiDroidDirectory(this))) {
if (!BackupManager.enoughDiskSpace(CollectionHelper.getCurrentAnkiDroidDirectory(this))) {
Timber.i("Not enough space to do backup")
showDialogFragment(DeckPickerNoSpaceLeftDialog.newInstance())
} else if (preferences.getBoolean("noSpaceLeft", false)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DeckPickerBackupNoSpaceLeftDialog : AnalyticsDialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog {
super.onCreate(savedInstanceState)
val res = resources
val space = BackupManager.getFreeDiscSpace(CollectionHelper.getCollectionPath(requireActivity()))
val space = BackupManager.getFreeDiskSpace(CollectionHelper.getCollectionPath(requireActivity()))
return AlertDialog
.Builder(requireContext())
.create {
Expand Down
Loading