Skip to content

Commit

Permalink
remove cancel intent
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Dec 23, 2024
1 parent 3761558 commit 7c7040a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class MainActivity : AppCompatActivity() {
}
}

// Load the default fragment
if (savedInstanceState == null) {
bottomNavigationView.selectedItemId = R.id.home
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,19 @@ class FileAdapter(private val context: Context, private val mainFragment: MainFr
}

private fun getFileTimeOfCreation(file: File): Long {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val attr = Files.readAttributes(file.toPath(), BasicFileAttributes::class.java)
attr.lastModifiedTime().toMillis()
} else {
file.lastModified()
return try {
if (file.exists()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val attr = Files.readAttributes(file.toPath(), BasicFileAttributes::class.java)
attr.lastModifiedTime().toMillis()
} else {
file.lastModified()
}
} else {
System.currentTimeMillis()
}
} catch (e: NoSuchFileException) {
System.currentTimeMillis()
}
}

Expand Down
152 changes: 20 additions & 132 deletions app/src/main/java/com/wirelessalien/zipxtract/fragment/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package com.wirelessalien.zipxtract.fragment
import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.app.ActivityManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -59,6 +58,7 @@ import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Lifecycle
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.transition.TransitionInflater
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.button.MaterialButton
import com.google.android.material.chip.Chip
Expand All @@ -68,22 +68,13 @@ import com.google.android.material.textfield.TextInputEditText
import com.wirelessalien.zipxtract.BuildConfig
import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.adapter.FileAdapter
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.ACTION_ARCHIVE_PROGRESS
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_SPLIT_ZIP_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_TAR_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_ZIP_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_COMPRESS_CANCEL
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.ACTION_MULTI_7Z_EXTRACTION_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_MULTI_ZIP_EXTRACTION_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_RAR_EXTRACTION_CANCEL
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_DIR_PATH
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_ERROR_MESSAGE
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_PROGRESS
import com.wirelessalien.zipxtract.databinding.FragmentMainBinding
Expand Down Expand Up @@ -136,7 +127,8 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
private var searchRunnable: Runnable? = null
private lateinit var eProgressDialog: AlertDialog
private lateinit var aProgressDialog: AlertDialog
private lateinit var progressText: TextView
private lateinit var aProgressText: TextView
private lateinit var eProgressText: TextView
private var areFabsVisible: Boolean = false
private lateinit var eProgressBar: LinearProgressIndicator
private lateinit var aProgressBar: LinearProgressIndicator
Expand All @@ -147,6 +139,8 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
ACTION_EXTRACTION_COMPLETE -> {
val dirPath = intent.getStringExtra(EXTRA_DIR_PATH)

unselectAllFiles()
eProgressDialog.dismiss()
Toast.makeText(requireContext(), getString(R.string.extraction_success), Toast.LENGTH_SHORT).show()
Expand All @@ -161,9 +155,11 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
val progress = intent.getIntExtra(EXTRA_PROGRESS, 0)
updateProgressBar(progress)
eProgressBar.progress = progress
progressText.text = getString(R.string.extracting_progress, progress)
eProgressText.text = getString(R.string.extracting_progress, progress)
}
ACTION_ARCHIVE_COMPLETE -> {
val dirPath = intent.getStringExtra(EXTRA_DIR_PATH)

unselectAllFiles()
aProgressDialog.dismiss()
Toast.makeText(requireContext(), getString(R.string.compression_success), Toast.LENGTH_SHORT).show()
Expand All @@ -178,7 +174,7 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
val progress = intent.getIntExtra(EXTRA_PROGRESS, 0)
updateProgressBar(progress)
aProgressBar.progress = progress
progressText.text = getString(R.string.compressing_progress, progress)
aProgressText.text = getString(R.string.compressing_progress, progress)
}
}
}
Expand All @@ -194,6 +190,13 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
updateActionModeTitle()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val inflater = TransitionInflater.from(requireContext())
enterTransition = inflater.inflateTransition(R.transition.fade_through)
exitTransition = inflater.inflateTransition(R.transition.fade_through)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentMainBinding.inflate(inflater, container, false)
return binding.root
Expand Down Expand Up @@ -432,70 +435,12 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
actionMode?.finish()
}

@Suppress("DEPRECATION")
private fun archiveProgressDialog() {
val aDialogView = layoutInflater.inflate(R.layout.progress_dialog_archive, null)
aProgressBar = aDialogView.findViewById(R.id.progressBar)
progressText = aDialogView.findViewById(R.id.progressText)
val cancelButton = aDialogView.findViewById<Button>(R.id.cancelButton)
aProgressText = aDialogView.findViewById(R.id.progressText)
val backgroundButton = aDialogView.findViewById<Button>(R.id.backgroundButton)

cancelButton.setOnClickListener {
val activityManager = requireContext().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager

val isArchive7zServiceRunning = activityManager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == Archive7zService::class.java.name }

if (isArchive7zServiceRunning) {
val cancelExtractIntent = Intent(requireContext(), Archive7zService::class.java).apply {
action = ACTION_ARCHIVE_7Z_CANCEL
}
requireContext().startService(cancelExtractIntent)
}

val isArchiveZipServiceRunning = activityManager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == ArchiveZipService::class.java.name }

if (isArchiveZipServiceRunning) {
val cancelZipIntent = Intent(requireContext(), ArchiveZipService::class.java).apply {
action = ACTION_ARCHIVE_ZIP_CANCEL
}
requireContext().startService(cancelZipIntent)
}

val isArchiveSplitZipServiceRunning = activityManager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == ArchiveSplitZipService::class.java.name }

if (isArchiveSplitZipServiceRunning) {
val cancelZipIntent = Intent(requireContext(), ArchiveSplitZipService::class.java).apply {
action = ACTION_ARCHIVE_SPLIT_ZIP_CANCEL
}
requireContext().startService(cancelZipIntent)
}

val isArchiveCompressServiceRunning = activityManager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == CompressCsArchiveService::class.java.name }

if (isArchiveCompressServiceRunning) {
val cancelZipIntent = Intent(requireContext(), CompressCsArchiveService::class.java).apply {
action = ACTION_COMPRESS_CANCEL
}
requireContext().startService(cancelZipIntent)
}

val isArchiveTarServiceRunning = activityManager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == ArchiveTarService::class.java.name }

if (isArchiveTarServiceRunning) {
val cancelZipIntent = Intent(requireContext(), ArchiveTarService::class.java).apply {
action = ACTION_ARCHIVE_TAR_CANCEL
}
requireContext().startService(cancelZipIntent)
}

aProgressDialog.dismiss()
}

backgroundButton.setOnClickListener {
aProgressDialog.dismiss()
}
Expand All @@ -510,66 +455,9 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
private fun extractProgressDialog() {
val ePDialogView = layoutInflater.inflate(R.layout.progress_dialog_extract, null)
eProgressBar = ePDialogView.findViewById(R.id.progressBar)
progressText = ePDialogView.findViewById(R.id.progressText)
val cancelButton = ePDialogView.findViewById<Button>(R.id.cancelButton)
eProgressText = ePDialogView.findViewById(R.id.progressText)
val backgroundButton = ePDialogView.findViewById<Button>(R.id.backgroundButton)

cancelButton.setOnClickListener {
val activityManager = requireContext().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager

val isExtractArchiveServiceRunning = activityManager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == ExtractArchiveService::class.java.name }

if (isExtractArchiveServiceRunning) {
val cancelExtractIntent = Intent(requireContext(), ExtractArchiveService::class.java).apply {
action = ACTION_EXTRACT_CANCEL
}
requireContext().startService(cancelExtractIntent)
}

val isExtractRarServiceRunning = activityManager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == ExtractRarService::class.java.name }

if (isExtractRarServiceRunning) {
val cancelRarIntent = Intent(requireContext(), ExtractRarService::class.java).apply {
action = ACTION_RAR_EXTRACTION_CANCEL
}
requireContext().startService(cancelRarIntent)
}

val isExtractMultipart7zServiceRunning = activityManager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == ExtractMultipart7zService::class.java.name }

if (isExtractMultipart7zServiceRunning) {
val cancelRarIntent = Intent(requireContext(), ExtractMultipart7zService::class.java).apply {
action = ACTION_MULTI_7Z_EXTRACTION_CANCEL
}
requireContext().startService(cancelRarIntent)
}

val isExtractMultipartZipServiceRunning = activityManager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == ExtractMultipartZipService::class.java.name }

if (isExtractMultipartZipServiceRunning) {
val cancelRarIntent = Intent(requireContext(), ExtractMultipartZipService::class.java).apply {
action = ACTION_MULTI_ZIP_EXTRACTION_CANCEL
}
requireContext().startService(cancelRarIntent)
}

val isExtractCsArchiveServiceRunning = activityManager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == ExtractCsArchiveService::class.java.name }

if (isExtractCsArchiveServiceRunning) {
val cancelRarIntent = Intent(requireContext(), ExtractCsArchiveService::class.java).apply {
action = ACTION_EXTRACT_CS_CANCEL
}
requireContext().startService(cancelRarIntent)
}

eProgressDialog.dismiss()
}

backgroundButton.setOnClickListener {
eProgressDialog.dismiss()
}
Expand Down Expand Up @@ -1013,7 +901,7 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
val dialogView = layoutInflater.inflate(R.layout.password_input_dialog, null)
val passwordEditText = dialogView.findViewById<TextInputEditText>(R.id.passwordInput)

MaterialAlertDialogBuilder(requireContext())
MaterialAlertDialogBuilder(requireContext(), R.style.MaterialDialog)
.setTitle(getString(R.string.enter_password))
.setView(dialogView)
.setPositiveButton(getString(R.string.ok)) { _, _ ->
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_archive.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
Expand Down Expand Up @@ -51,6 +52,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:orientation="horizontal"
app:singleLine="true"/>
</HorizontalScrollView>
Expand All @@ -61,7 +63,7 @@
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="-5dp"
android:layout_marginTop="5dp"
tools:listitem="@layout/item_file" />
</LinearLayout>

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/menu/menu_action.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
<item
android:id="@+id/m_archive_zip"
android:title="@string/create_zip"
app:showAsAction="ifRoom" />
app:showAsAction="never" />

<item
android:id="@+id/m_archive_7z"
android:title="@string/compress_sevenz"
app:showAsAction="ifRoom" />
app:showAsAction="never" />

<item
android:id="@+id/m_archive_tar"
android:title="@string/compress_tar"
app:showAsAction="ifRoom" />
app:showAsAction="never" />

<item
android:id="@+id/menu_action_select_all"
Expand Down

0 comments on commit 7c7040a

Please sign in to comment.