Skip to content

Commit

Permalink
add localization, refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Dec 22, 2024
1 parent e137a97 commit 628a04b
Show file tree
Hide file tree
Showing 28 changed files with 1,009 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ class MainActivity : AppCompatActivity(), FileAdapter.OnItemClickListener, FileA
fun startArchiveTarService(file: List<String>, archiveName: String) {
progressDialog.show()
val intent = Intent(this, ArchiveTarService::class.java).apply {
putExtra(ArchiveTarService.EXTRA_FILES_TO_ARCHIVE, ArrayList(file))
putStringArrayListExtra(ArchiveTarService.EXTRA_FILES_TO_ARCHIVE, ArrayList(file))
putExtra(ArchiveTarService.EXTRA_ARCHIVE_NAME, archiveName)
}
ContextCompat.startForegroundService(this, intent)
Expand Down Expand Up @@ -1059,7 +1059,7 @@ class MainActivity : AppCompatActivity(), FileAdapter.OnItemClickListener, FileA
putExtra(ArchiveZipService.EXTRA_IS_ENCRYPTED, isEncrypted)
putExtra(ArchiveZipService.EXTRA_ENCRYPTION_METHOD, encryptionMethod)
putExtra(ArchiveZipService.EXTRA_AES_STRENGTH, aesStrength)
putExtra(ArchiveZipService.EXTRA_FILES_TO_ARCHIVE, ArrayList(filesToArchive))
putStringArrayListExtra(ArchiveZipService.EXTRA_FILES_TO_ARCHIVE, ArrayList(filesToArchive))
}
ContextCompat.startForegroundService(this, intent)
}
Expand All @@ -1074,7 +1074,7 @@ class MainActivity : AppCompatActivity(), FileAdapter.OnItemClickListener, FileA
putExtra(ArchiveSplitZipService.EXTRA_IS_ENCRYPTED, isEncrypted)
putExtra(ArchiveSplitZipService.EXTRA_ENCRYPTION_METHOD, encryptionMethod)
putExtra(ArchiveSplitZipService.EXTRA_AES_STRENGTH, aesStrength)
putExtra(ArchiveSplitZipService.EXTRA_FILES_TO_ARCHIVE, ArrayList(filesToArchive))
putStringArrayListExtra(ArchiveSplitZipService.EXTRA_FILES_TO_ARCHIVE, ArrayList(filesToArchive))
putExtra(ArchiveSplitZipService.EXTRA_SPLIT_SIZE, splitSize)

}
Expand All @@ -1098,7 +1098,7 @@ class MainActivity : AppCompatActivity(), FileAdapter.OnItemClickListener, FileA
putExtra(Archive7zService.EXTRA_COMPRESSION_LEVEL, compressionLevel)
putExtra(Archive7zService.EXTRA_SOLID, solid)
putExtra(Archive7zService.EXTRA_THREAD_COUNT, threadCount)
putExtra(Archive7zService.EXTRA_FILES_TO_ARCHIVE, ArrayList(filesToArchive))
putStringArrayListExtra(Archive7zService.EXTRA_FILES_TO_ARCHIVE, ArrayList(filesToArchive))
}
ContextCompat.startForegroundService(this, intent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.wirelessalien.zipxtract.constant.BroadcastConstants
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_COMPLETE
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_ERROR
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ARCHIVE_NOTIFICATION_CHANNEL_ID
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_ERROR_MESSAGE
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_PROGRESS
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -78,7 +79,7 @@ class Archive7zService : Service() {
val compressionLevel = intent.getIntExtra(EXTRA_COMPRESSION_LEVEL, 1)
val solid = intent.getBooleanExtra(EXTRA_SOLID, false)
val threadCount = intent.getIntExtra(EXTRA_THREAD_COUNT, 1)
val filesToArchive = intent.getSerializableExtra(EXTRA_FILES_TO_ARCHIVE) as List<String>
val filesToArchive = intent.getStringArrayListExtra(EXTRA_FILES_TO_ARCHIVE) ?: return START_NOT_STICKY

if (intent.action == BroadcastConstants.ACTION_ARCHIVE_7Z_CANCEL) {
archiveJob?.cancel()
Expand Down Expand Up @@ -135,9 +136,23 @@ class Archive7zService : Service() {
}

private fun create7zFile(archiveName: String, password: String?, compressionLevel: Int, solid: Boolean, threadCount: Int, filesToArchive: List<String>) {
if (filesToArchive.isEmpty()) {
val errorMessage = getString(R.string.no_files_to_archive)
showErrorNotification(errorMessage)
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, errorMessage))
stopForegroundService()
return
}

try {
val baseDirectory = File(filesToArchive.first()).parentFile?.absolutePath ?: ""
val sevenZFile = File(baseDirectory, "$archiveName.7z")
var sevenZFile = File(baseDirectory, "$archiveName.7z")
var counter = 1

while (sevenZFile.exists()) {
sevenZFile = File(baseDirectory, "$archiveName ($counter).7z")
counter++
}

RandomAccessFile(sevenZFile, "rw").use { raf ->
val outArchive = SevenZip.openOutArchive7z()
Expand All @@ -146,7 +161,6 @@ class Archive7zService : Service() {
outArchive.setSolid(solid)
outArchive.setThreadCount(threadCount)
outArchive.setHeaderEncryption(true)
outArchive.isTrace = true

outArchive.createArchive(
RandomAccessFileOutStream(raf), filesToArchive.size,
Expand All @@ -164,7 +178,8 @@ class Archive7zService : Service() {
}

override fun setCompleted(complete: Long) {
val progress = ((complete.toDouble() / filesToArchive.size) * 100).toInt()
val totalSize = filesToArchive.sumOf { File(it).length() }
val progress = ((complete.toDouble() / totalSize) * 100).toInt()
startForeground(NOTIFICATION_ID, createNotification(progress))
updateProgress(progress)
}
Expand Down Expand Up @@ -198,16 +213,16 @@ class Archive7zService : Service() {
}
} catch (e: SevenZipException) {
e.printStackTrace()
showErrorNotification(": ${e.message}")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_PROGRESS, ": ${e.message}"))
showErrorNotification(e.message ?: getString(R.string.general_error_msg))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message))
} catch (e: IOException) {
e.printStackTrace()
showErrorNotification(": ${e.message}")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_PROGRESS, ": ${e.message}"))
showErrorNotification(e.message ?: getString(R.string.general_error_msg))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message))
} catch (e: OutOfMemoryError) {
e.printStackTrace()
showErrorNotification(": ${e.message}")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_PROGRESS, ": ${e.message}"))
showErrorNotification(e.message ?: getString(R.string.general_error_msg))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.wirelessalien.zipxtract.constant.BroadcastConstants
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_COMPLETE
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_ERROR
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ARCHIVE_NOTIFICATION_CHANNEL_ID
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_ERROR_MESSAGE
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -75,12 +76,37 @@ class ArchiveSplitZipService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val archiveName = intent?.getStringExtra(EXTRA_ARCHIVE_NAME) ?: return START_NOT_STICKY
val password = intent.getStringExtra(EXTRA_PASSWORD)
val compressionMethod = intent.getSerializableExtra(EXTRA_COMPRESSION_METHOD) as CompressionMethod
val compressionLevel = intent.getSerializableExtra(EXTRA_COMPRESSION_LEVEL) as CompressionLevel
val compressionMethod = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getSerializableExtra(EXTRA_COMPRESSION_METHOD, CompressionMethod::class.java)
} else {
@Suppress("DEPRECATION")
intent.getSerializableExtra(EXTRA_COMPRESSION_METHOD) as? CompressionMethod
} ?: CompressionMethod.STORE

val compressionLevel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getSerializableExtra(EXTRA_COMPRESSION_LEVEL, CompressionLevel::class.java)
} else {
@Suppress("DEPRECATION")
intent.getSerializableExtra(EXTRA_COMPRESSION_LEVEL) as? CompressionLevel
} ?: CompressionLevel.NO_COMPRESSION

val isEncrypted = intent.getBooleanExtra(EXTRA_IS_ENCRYPTED, false)
val encryptionMethod = intent.getSerializableExtra(EXTRA_ENCRYPTION_METHOD) as EncryptionMethod?
val aesStrength = intent.getSerializableExtra(EXTRA_AES_STRENGTH) as AesKeyStrength?
val filesToArchive = intent.getSerializableExtra(EXTRA_FILES_TO_ARCHIVE) as List<String>

val encryptionMethod = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getSerializableExtra(EXTRA_ENCRYPTION_METHOD, EncryptionMethod::class.java)
} else {
@Suppress("DEPRECATION")
intent.getSerializableExtra(EXTRA_ENCRYPTION_METHOD) as? EncryptionMethod
}

val aesStrength = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getSerializableExtra(EXTRA_AES_STRENGTH, AesKeyStrength::class.java)
} else {
@Suppress("DEPRECATION")
intent.getSerializableExtra(EXTRA_AES_STRENGTH) as? AesKeyStrength
}
val filesToArchive = intent.getStringArrayListExtra(EXTRA_FILES_TO_ARCHIVE) ?: return START_NOT_STICKY

val splitSize = intent.getLongExtra(EXTRA_SPLIT_SIZE, 64)

if (intent.action == BroadcastConstants.ACTION_ARCHIVE_SPLIT_ZIP_CANCEL) {
Expand Down Expand Up @@ -148,6 +174,15 @@ class ArchiveSplitZipService : Service() {
selectedFiles: List<String>,
splitSize: Long
) {

if (selectedFiles.isEmpty()) {
val errorMessage = getString(R.string.no_files_to_archive)
showErrorNotification(errorMessage)
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, errorMessage))
stopForegroundService()
return
}

try {
val zipParameters = ZipParameters().apply {
this.compressionMethod = compressionMethod
Expand Down Expand Up @@ -180,7 +215,7 @@ class ArchiveSplitZipService : Service() {

selectedFiles.forEach { filePath ->
val file = File(filePath)
val destFile = File(renamedTempDir, file.relativeTo(baseDirectory).path)
val destFile = File(renamedTempDir, file.relativeTo(baseDirectory!!).path)
if (file.isDirectory) {
destFile.mkdirs()
file.copyRecursively(destFile, overwrite = true)
Expand All @@ -206,27 +241,27 @@ class ArchiveSplitZipService : Service() {
}

if (progressMonitor.result == ProgressMonitor.Result.SUCCESS) {
showCompletionNotification("$archiveName.zip created successfully")
showCompletionNotification(getString(R.string.zip_creation_success))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_COMPLETE))
} else {
showErrorNotification(getString(R.string.zip_creation_failed))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra("error_message", "Archive creation failed: ${progressMonitor.result}"))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, progressMonitor.result))
}

if (archiveJob?.isCancelled == true) throw ZipException("Extraction cancelled")
if (archiveJob?.isCancelled == true) throw ZipException(getString(R.string.operation_cancelled))

renamedTempDir.deleteRecursively()

} catch (e: ZipException) {
e.printStackTrace()
showErrorNotification("Archive creation failed: ${e.message}")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra("error_message", "Archive creation failed: ${e.message}"))
showErrorNotification(e.message ?: getString(R.string.general_error_msg))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message))
return
}
} catch (e: Exception) {
e.printStackTrace()
showErrorNotification("Archive creation failed: ${e.message}")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra("error_message", "Archive creation failed: ${e.message}"))
showErrorNotification(e.message ?: getString(R.string.general_error_msg))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message))
}
}

Expand All @@ -242,9 +277,9 @@ class ArchiveSplitZipService : Service() {
private fun showErrorNotification(error: String) {
stopForegroundService()
val notification = NotificationCompat.Builder(this, ARCHIVE_NOTIFICATION_CHANNEL_ID)
.setContentTitle("Archive Creation Failed")
.setContentTitle(getString(R.string.zip_creation_failed))
.setContentText(error)
.setSmallIcon(R.drawable.ic_folder_zip)
.setSmallIcon(R.drawable.ic_notification_icon)
.setAutoCancel(true)
.build()

Expand All @@ -256,7 +291,7 @@ class ArchiveSplitZipService : Service() {
stopForegroundService()

val notification = NotificationCompat.Builder(this, ARCHIVE_NOTIFICATION_CHANNEL_ID)
.setContentTitle("Archive Creation Complete")
.setContentTitle(getString(R.string.zip_creation_success))
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification_icon)
.setAutoCancel(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_CO
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_ERROR
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_TAR_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ARCHIVE_NOTIFICATION_CHANNEL_ID
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_ERROR_MESSAGE
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_PROGRESS
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -69,7 +70,7 @@ class ArchiveTarService : Service() {

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val archiveName = intent?.getStringExtra(EXTRA_ARCHIVE_NAME) ?: return START_NOT_STICKY
val filesToArchive = intent.getSerializableExtra(EXTRA_FILES_TO_ARCHIVE) as List<String>
val filesToArchive = intent.getStringArrayListExtra(EXTRA_FILES_TO_ARCHIVE) ?: return START_NOT_STICKY

if (intent.action == ACTION_ARCHIVE_TAR_CANCEL) {
archiveJob?.cancel()
Expand Down Expand Up @@ -126,9 +127,23 @@ class ArchiveTarService : Service() {
}

private fun createTarFile(archiveName: String, filesToArchive: List<String>) {

if (filesToArchive.isEmpty()) {
val errorMessage = getString(R.string.no_files_to_archive)
showErrorNotification(errorMessage)
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, errorMessage))
stopForegroundService()
return
}
try {
val baseDirectory = File(filesToArchive.first()).parentFile?.absolutePath ?: ""
val tarFile = File(baseDirectory, "$archiveName.tar")
var counter = 1

while (tarFile.exists()) {
tarFile.renameTo(File(baseDirectory, "$archiveName ($counter).tar"))
counter++
}

RandomAccessFile(tarFile, "rw").use { raf ->
val outArchive = SevenZip.openOutArchiveTar()
Expand Down Expand Up @@ -171,16 +186,16 @@ class ArchiveTarService : Service() {
}
} catch (e: SevenZipException) {
e.printStackTrace()
showErrorNotification(": ${e.message}")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_PROGRESS, ": ${e.message}"))
showErrorNotification(e.message ?: getString(R.string.general_error_msg))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message))
} catch (e: IOException) {
e.printStackTrace()
showErrorNotification(": ${e.message}")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_PROGRESS, ": ${e.message}"))
showErrorNotification(e.message ?: getString(R.string.general_error_msg))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message))
} catch (e: OutOfMemoryError) {
e.printStackTrace()
showErrorNotification(": ${e.message}")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_PROGRESS, ": ${e.message}"))
showErrorNotification(e.message ?: getString(R.string.general_error_msg))
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message))
}
}

Expand Down
Loading

0 comments on commit 628a04b

Please sign in to comment.