Skip to content

Commit

Permalink
add preference screen, fix- directory sort, minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Dec 30, 2024
1 parent c5e0641 commit 49e04ed
Show file tree
Hide file tree
Showing 24 changed files with 600 additions and 56 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.13.0-alpha08'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.preference:preference-ktx:1.2.1'

//glide
implementation("com.github.bumptech.glide:glide:4.16.0")
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
</intent-filter>
</activity>

<activity android:name=".activity.SettingsActivity"/>

<service
android:name=".service.ExtractArchiveService"
android:foregroundServiceType="dataSync"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2023 WirelessAlien <https://github.com/WirelessAlien>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.wirelessalien.zipxtract

import android.content.Context
import android.util.AttributeSet
import androidx.preference.EditTextPreference

class CustomEditTextPreference(context: Context, attrs: AttributeSet?) : EditTextPreference(context, attrs) {

private val basePath = "/storage/emulated/0/"

override fun getText(): String? {
val text = super.getText()
return if (text != null && text.startsWith(basePath)) {
text.removePrefix(basePath)
} else {
text
}
}

override fun setText(text: String?) {
super.setText(basePath + (text ?: ""))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2023 WirelessAlien <https://github.com/WirelessAlien>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.wirelessalien.zipxtract.activity

import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import androidx.activity.OnBackPressedDispatcher
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.appbar.MaterialToolbar
import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.fragment.SettingsFragment


class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)

// Set up the toolbar
val toolbar: MaterialToolbar = findViewById(R.id.toolbar)
toolbar.title = getString(R.string.action_settings)
setSupportActionBar(toolbar)

// Display the fragment as the main content.
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentCntainer, SettingsFragment())
.commit()

// Add back button to the activity
val actionBar = supportActionBar
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true)
actionBar.setHomeButtonEnabled(true)
}

// Handle back button press
OnBackPressedDispatcher().addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
finishActivity()
}
})
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
finishActivity()
return true
}
return super.onOptionsItemSelected(item)
}

private fun finishActivity() {
finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ object BroadcastConstants {
const val ACTION_ARCHIVE_PROGRESS = "ACTION_ARCHIVE_PROGRESS"
const val EXTRA_PROGRESS = "progress"
const val EXTRA_ERROR_MESSAGE = "error_message"

const val PREFERENCE_EXTRACT_DIR_PATH = "key_extract_path"
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import androidx.core.content.FileProvider
import androidx.core.view.MenuHost
import androidx.core.view.MenuProvider
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Lifecycle
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.LinearLayoutManager
Expand All @@ -58,9 +57,9 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.progressindicator.LinearProgressIndicator
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.textfield.TextInputEditText
import com.wirelessalien.zipxtract.AboutFragment
import com.wirelessalien.zipxtract.BuildConfig
import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.activity.SettingsActivity
import com.wirelessalien.zipxtract.adapter.FileAdapter
import com.wirelessalien.zipxtract.constant.BroadcastConstants
import com.wirelessalien.zipxtract.databinding.FragmentArchiveBinding
Expand All @@ -81,7 +80,7 @@ class ArchiveFragment : Fragment(), FileAdapter.OnItemClickListener {

private lateinit var binding: FragmentArchiveBinding
private lateinit var adapter: FileAdapter
private val archiveExtensions = listOf("rar", "zip", "7z", "tar", "gz", "bz2", "xz", "lz4", "lzma", "sz")
private val archiveExtensions = listOf("rar", "r00", "001", "7z", "7z.001", "zip", "tar", "gz", "bz2", "xz", "lz4", "lzma", "sz")
private lateinit var eProgressDialog: AlertDialog
private lateinit var progressText: TextView
private lateinit var eProgressBar: LinearProgressIndicator
Expand All @@ -100,6 +99,8 @@ class ArchiveFragment : Fragment(), FileAdapter.OnItemClickListener {

private val extractionReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (!isAdded) return

when (intent?.action) {
BroadcastConstants.ACTION_EXTRACTION_COMPLETE -> {
eProgressDialog.dismiss()
Expand Down Expand Up @@ -216,20 +217,9 @@ class ArchiveFragment : Fragment(), FileAdapter.OnItemClickListener {
sortAscending = false
editor.putBoolean("sortAscending", sortAscending)
}
R.id.menu_about -> {
val fragmentManager = parentFragmentManager
val newFragment = AboutFragment()
if (isLargeLayout) {
// Show the fragment as a dialog.
newFragment.show(fragmentManager, "AboutFragment")
} else {
// Show the fragment fullscreen.
val transaction = fragmentManager.beginTransaction()
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
transaction.add(android.R.id.content, newFragment)
.addToBackStack(null)
.commit()
}
R.id.menu_settings -> {
val intent = Intent(requireContext(), SettingsActivity::class.java)
startActivity(intent)
}
}
editor.apply()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ import com.google.android.material.progressindicator.LinearProgressIndicator
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.transition.MaterialSharedAxis
import com.wirelessalien.zipxtract.AboutFragment
import com.wirelessalien.zipxtract.BuildConfig
import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.activity.SettingsActivity
import com.wirelessalien.zipxtract.adapter.FileAdapter
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_COMPLETE
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_ERROR
Expand Down Expand Up @@ -142,6 +142,8 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On

private val extractionReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (!isAdded) return

when (intent?.action) {
ACTION_EXTRACTION_COMPLETE -> {
val dirPath = intent.getStringExtra(EXTRA_DIR_PATH)
Expand Down Expand Up @@ -303,20 +305,9 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
sortAscending = false
editor.putBoolean("sortAscending", sortAscending)
}
R.id.menu_about -> {
val fragmentManager = parentFragmentManager
val newFragment = AboutFragment()
if (isLargeLayout) {
// Show the fragment as a dialog.
newFragment.show(fragmentManager, "AboutFragment")
} else {
// Show the fragment fullscreen.
val transaction = fragmentManager.beginTransaction()
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
transaction.add(android.R.id.content, newFragment)
.addToBackStack(null)
.commit()
}
R.id.menu_settings -> {
val intent = Intent(requireContext(), SettingsActivity::class.java)
startActivity(intent)
}
}
editor.apply()
Expand Down Expand Up @@ -428,13 +419,13 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
}

private fun updateCurrentPathChip() {
val internalStorage = "Internal Storage"
val internalStorage = getString(R.string.internal_storage)
val sdCardPath = getSdCardPath()
val basePath = Environment.getExternalStorageDirectory().absolutePath
val currentPath = currentPath ?: basePath
val displayPath = when {
currentPath.startsWith(basePath) -> currentPath.replace(basePath, internalStorage)
sdCardPath != null && currentPath.startsWith(sdCardPath) -> currentPath.replace(sdCardPath, "SD Card")
sdCardPath != null && currentPath.startsWith(sdCardPath) -> currentPath.replace(sdCardPath, getString(R.string.sd_card))
else -> currentPath
}.split("/")

Expand Down Expand Up @@ -1263,17 +1254,32 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
private suspend fun getFiles(): ArrayList<File> = withContext(Dispatchers.IO) {
val files = ArrayList<File>()
val directories = ArrayList<File>()

val directory = File(currentPath ?: Environment.getExternalStorageDirectory().absolutePath)

if (!directory.canRead()) {
withContext(Dispatchers.Main) {
binding.statusTextView.text = getString(R.string.access_denied)
binding.statusTextView.visibility = View.VISIBLE
}
return@withContext files
}

val fileList = directory.listFiles()

if (fileList != null) {
for (file in fileList) {
if (file.isDirectory) {
directories.add(file)
} else {
files.add(file)
}
if (fileList == null || fileList.isEmpty()) {
withContext(Dispatchers.Main) {
binding.statusTextView.text = getString(R.string.directory_empty)
binding.statusTextView.visibility = View.VISIBLE
}
return@withContext files
}

for (file in fileList) {
if (file.isDirectory) {
directories.add(file)
} else {
files.add(file)
}
}

Expand All @@ -1282,9 +1288,18 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
directories.sortBy { it.name }
files.sortBy { it.name }
}
SortBy.SORT_BY_SIZE -> files.sortBy { it.length() }
SortBy.SORT_BY_MODIFIED -> files.sortBy { getFileTimeOfCreation(it) }
SortBy.SORT_BY_EXTENSION -> files.sortBy { it.extension }
SortBy.SORT_BY_SIZE -> {
directories.sortBy { it.length() }
files.sortBy { it.length() }
}
SortBy.SORT_BY_MODIFIED -> {
directories.sortBy { getFileTimeOfCreation(it) }
files.sortBy { getFileTimeOfCreation(it) }
}
SortBy.SORT_BY_EXTENSION -> {
directories.sortBy { it.extension }
files.sortBy { it.extension }
}
}

if (!sortAscending) {
Expand All @@ -1296,6 +1311,10 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
combinedList.addAll(directories)
combinedList.addAll(files)

withContext(Dispatchers.Main) {
binding.statusTextView.visibility = View.GONE
}

combinedList
}

Expand All @@ -1313,6 +1332,11 @@ class MainFragment : Fragment(), FileAdapter.OnItemClickListener, FileAdapter.On
CoroutineScope(Dispatchers.IO).launch {
val fullFileList = getFiles()
withContext(Dispatchers.Main) {
if (fullFileList.isEmpty()) {
binding.statusTextView.visibility = View.VISIBLE
} else {
binding.statusTextView.visibility = View.GONE
}
adapter.updateFilesAndFilter(fullFileList)
}
}
Expand Down
Loading

0 comments on commit 49e04ed

Please sign in to comment.