Skip to content

Commit

Permalink
few fix
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Dec 22, 2024
1 parent 6157312 commit bca7157
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 72 deletions.
132 changes: 81 additions & 51 deletions app/src/main/java/com/wirelessalien/zipxtract/activity/MainActivity.kt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ object BroadcastConstants {
const val ACTION_ARCHIVE_ZIP_CANCEL = "ACTION_ZIP_ARCHIVE_CANCEL"
const val ACTION_ARCHIVE_TAR_CANCEL = "ACTION_TAR_ARCHIVE_CANCEL"
const val ACTION_ARCHIVE_SPLIT_ZIP_CANCEL = "ACTION_SPLIT_ZIP_ARCHIVE_CANCEL"
const val ACTION_EXTRACT_CS_CANCEL = "ACTION_CS_EXTRACTION_CANCEL"
const val ACTION_ARCHIVE_COMPLETE = "ACTION_ARCHIVE_COMPLETE"
const val ACTION_ARCHIVE_ERROR = "ACTION_ARCHIVE_ERROR"
const val ACTION_EXTRACTION_COMPLETE = "ACTION_EXTRACTION_COMPLETE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.core.app.NotificationCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.constant.BroadcastConstants
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_7Z_CANCEL
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
Expand Down Expand Up @@ -81,7 +82,7 @@ class Archive7zService : Service() {
val threadCount = intent.getIntExtra(EXTRA_THREAD_COUNT, 1)
val filesToArchive = intent.getStringArrayListExtra(EXTRA_FILES_TO_ARCHIVE) ?: return START_NOT_STICKY

if (intent.action == BroadcastConstants.ACTION_ARCHIVE_7Z_CANCEL) {
if (intent.action == ACTION_ARCHIVE_7Z_CANCEL) {
archiveJob?.cancel()
stopForegroundService()
stopSelf()
Expand All @@ -103,7 +104,7 @@ class Archive7zService : Service() {

private fun createCancelIntent(): PendingIntent {
val cancelIntent = Intent(this, Archive7zService::class.java).apply {
action = BroadcastConstants.ACTION_ARCHIVE_7Z_CANCEL
action = ACTION_ARCHIVE_7Z_CANCEL
}
return PendingIntent.getService(this, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
}
Expand Down Expand Up @@ -136,6 +137,7 @@ 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.wirelessalien.zipxtract.R
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.ACTION_ARCHIVE_SPLIT_ZIP_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ARCHIVE_NOTIFICATION_CHANNEL_ID
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_ERROR_MESSAGE
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -109,7 +110,7 @@ class ArchiveSplitZipService : Service() {

val splitSize = intent.getLongExtra(EXTRA_SPLIT_SIZE, 64)

if (intent.action == BroadcastConstants.ACTION_ARCHIVE_SPLIT_ZIP_CANCEL) {
if (intent.action == ACTION_ARCHIVE_SPLIT_ZIP_CANCEL) {
archiveJob?.cancel()
stopForegroundService()
stopSelf()
Expand All @@ -131,7 +132,7 @@ class ArchiveSplitZipService : Service() {

private fun createCancelIntent(): PendingIntent {
val cancelIntent = Intent(this, ArchiveSplitZipService::class.java).apply {
action = BroadcastConstants.ACTION_ARCHIVE_SPLIT_ZIP_CANCEL
action = ACTION_ARCHIVE_SPLIT_ZIP_CANCEL
}
return PendingIntent.getService(this, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
}
Expand Down Expand Up @@ -241,7 +242,7 @@ class ArchiveSplitZipService : Service() {
}

if (progressMonitor.result == ProgressMonitor.Result.SUCCESS) {
showCompletionNotification(getString(R.string.zip_creation_success))
showCompletionNotification()
sendLocalBroadcast(Intent(ACTION_ARCHIVE_COMPLETE))
} else {
showErrorNotification(getString(R.string.zip_creation_failed))
Expand Down Expand Up @@ -287,12 +288,11 @@ class ArchiveSplitZipService : Service() {
notificationManager.notify(NOTIFICATION_ID + 1, notification)
}

private fun showCompletionNotification(message: String) {
private fun showCompletionNotification() {
stopForegroundService()

val notification = NotificationCompat.Builder(this, ARCHIVE_NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.zip_creation_success))
.setContentText(message)
.setSmallIcon(R.drawable.ic_notification_icon)
.setAutoCancel(true)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class ArchiveTarService : Service() {

outArchive.close()
stopForegroundService()
showCompletionNotification()
sendLocalBroadcast(Intent(ACTION_ARCHIVE_COMPLETE))
}
} catch (e: SevenZipException) {
Expand All @@ -208,6 +209,19 @@ class ArchiveTarService : Service() {
EXTRA_PROGRESS, progress))
}

private fun showCompletionNotification() {
stopForegroundService()

val notification = NotificationCompat.Builder(this, ARCHIVE_NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.tar_creation_success))
.setSmallIcon(R.drawable.ic_notification_icon)
.setAutoCancel(true)
.build()

val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager.notify(ArchiveSplitZipService.NOTIFICATION_ID + 1, notification)
}

private fun showErrorNotification(error: String) {
stopForegroundService()
val notification = NotificationCompat.Builder(this, ARCHIVE_NOTIFICATION_CHANNEL_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.wirelessalien.zipxtract.R
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.ACTION_ARCHIVE_ZIP_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_ERROR_MESSAGE
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -73,7 +74,7 @@ class ArchiveZipService : Service() {

private fun createCancelIntent(): PendingIntent {
val cancelIntent = Intent(this, ArchiveZipService::class.java).apply {
action = BroadcastConstants.ACTION_ARCHIVE_ZIP_CANCEL
action = ACTION_ARCHIVE_ZIP_CANCEL
}
return PendingIntent.getService(this, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
}
Expand Down Expand Up @@ -113,7 +114,7 @@ class ArchiveZipService : Service() {

val filesToArchive = intent.getStringArrayListExtra(ArchiveSplitZipService.EXTRA_FILES_TO_ARCHIVE) ?: return START_NOT_STICKY

if (intent.action == BroadcastConstants.ACTION_ARCHIVE_ZIP_CANCEL) {
if (intent.action == ACTION_ARCHIVE_ZIP_CANCEL) {
archiveJob?.cancel()
stopForegroundService()
stopSelf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CompressCsArchiveService : Service() {
startForeground(NOTIFICATION_ID, createNotification(0))

compressionJob = CoroutineScope(Dispatchers.IO).launch {
compressArchive(File(filePath), compressionFormat)
compressArchive(filePath, compressionFormat)
}

return START_NOT_STICKY
Expand Down Expand Up @@ -131,7 +131,17 @@ class CompressCsArchiveService : Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}

private fun compressArchive(file: File, format: String) {
private fun compressArchive(filePath: String, format: String) {

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

val file = File(filePath)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var outputFile = if (format == CompressorStreamFactory.BZIP2) {
File(file.parent, "${file.name}.bz2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ExtractArchiveService : Service() {
startForeground(NOTIFICATION_ID, createNotification(0))

extractionJob = CoroutineScope(Dispatchers.IO).launch {
extractArchive(File(filePath), password)
extractArchive(filePath, password)
}

return START_NOT_STICKY
Expand Down Expand Up @@ -143,8 +143,17 @@ class ExtractArchiveService : Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}

private fun extractArchive(file: File, password: String?) {
private fun extractArchive(filePath: String, password: String?) {

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

val file = File(filePath)
if (file.extension.equals("zip", ignoreCase = true)) {
extractZipArchive(file, password)
return
Expand Down Expand Up @@ -266,7 +275,9 @@ class ExtractArchiveService : Service() {
try {
uos?.close()
extractedSize++
updateProgress((extractedSize * 100 / totalSize).toInt())
val progress = ((extractedSize.toDouble() / totalSize) * 100).toInt()
startForeground(NOTIFICATION_ID, createNotification(progress))
updateProgress(progress)
} catch (e: SevenZipException) {
e.printStackTrace()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_EXTRACTION_COMPLETE
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_EXTRACTION_ERROR
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_EXTRACTION_PROGRESS
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_EXTRACT_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_EXTRACT_CS_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRACTION_NOTIFICATION_CHANNEL_ID
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_ERROR_MESSAGE
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_PROGRESS
Expand Down Expand Up @@ -75,7 +75,7 @@ class ExtractCsArchiveService : Service() {
return START_NOT_STICKY
}

if (intent.action == ACTION_EXTRACT_CANCEL) {
if (intent.action == ACTION_EXTRACT_CS_CANCEL) {
extractionJob?.cancel()
stopForegroundService()
stopSelf()
Expand All @@ -85,7 +85,7 @@ class ExtractCsArchiveService : Service() {
startForeground(NOTIFICATION_ID, createNotification(0))

extractionJob = CoroutineScope(Dispatchers.IO).launch {
extractArchive(File(filePath))
extractArchive(filePath)
}

return START_NOT_STICKY
Expand All @@ -98,7 +98,7 @@ class ExtractCsArchiveService : Service() {

private fun createCancelIntent(): PendingIntent {
val cancelIntent = Intent(this, ExtractCsArchiveService::class.java).apply {
action = ACTION_EXTRACT_CANCEL
action = ACTION_EXTRACT_CS_CANCEL
}
return PendingIntent.getService(this, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT)
}
Expand Down Expand Up @@ -130,7 +130,16 @@ class ExtractCsArchiveService : Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}

private fun extractArchive(file: File) {
private fun extractArchive(filePath: String) {

if (filePath.isEmpty()) {
val errorMessage = getString(R.string.no_files_to_archive)
showErrorNotification(errorMessage)
sendLocalBroadcast(Intent(ACTION_EXTRACTION_ERROR).putExtra(EXTRA_ERROR_MESSAGE, errorMessage))
stopForegroundService()
return
}
val file = File(filePath)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
val outputFile = File(file.parent, file.nameWithoutExtension)
val fin: InputStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ class ExtractMultipart7zService : Service() {
}

private fun extractArchive(filePath: String) {

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

val file = File(filePath)
val parentDir = file.parentFile ?: return
val baseFileName = file.nameWithoutExtension
Expand Down Expand Up @@ -226,7 +235,9 @@ class ExtractMultipart7zService : Service() {
try {
uos?.close()
extractedSize++
updateProgress((extractedSize * 100 / totalSize).toInt())
val progress = ((extractedSize.toDouble() / totalSize) * 100).toInt()
startForeground(NOTIFICATION_ID, createNotification(progress))
updateProgress(progress)
} catch (e: IOException) {
e.printStackTrace()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ class ExtractMultipartZipService : Service() {
}

private suspend fun extractArchive(filePath: String, password: String?) {

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

val file = File(filePath)
val parentDir = file.parentFile ?: return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ class ExtractRarService : Service() {
}

private fun extractArchive(filePath: String) {

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

val file = File(filePath)
val parentDir = file.parentFile ?: return
val baseFileName = file.nameWithoutExtension
Expand Down Expand Up @@ -229,7 +238,9 @@ class ExtractRarService : Service() {
try {
uos?.close()
extractedSize++
updateProgress((extractedSize * 100 / totalSize).toInt())
val progress = ((extractedSize.toDouble() / totalSize) * 100).toInt()
startForeground(NOTIFICATION_ID, createNotification(progress))
updateProgress(progress)
} catch (e: IOException) {
e.printStackTrace()
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@
<string name="descending">Descending</string>
<string name="general_error_msg">Something went wrong</string>
<string name="no_files_to_archive">No files were provided</string>
<string name="tar_creation_success">Tar file created successfully</string>
<string name="extracting_progress">Extracting… %1$d %%</string>
<string name="compressing_progress">Compressing… %1$d %%</string>
</resources>

0 comments on commit bca7157

Please sign in to comment.