2020-07-17 16:35:39 +02:00
|
|
|
package sushi.hardcore.droidfs.adapters
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.BaseAdapter
|
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.TextView
|
|
|
|
import sushi.hardcore.droidfs.R
|
2020-11-04 18:42:04 +01:00
|
|
|
import sushi.hardcore.droidfs.util.PathUtils
|
|
|
|
import sushi.hardcore.droidfs.Volume
|
|
|
|
import sushi.hardcore.droidfs.VolumeDatabase
|
2020-07-17 16:35:39 +02:00
|
|
|
import sushi.hardcore.droidfs.util.WidgetUtil
|
2020-07-27 16:20:52 +02:00
|
|
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
|
2020-08-05 14:06:54 +02:00
|
|
|
import sushi.hardcore.droidfs.widgets.NonScrollableColoredBorderListView
|
2020-11-04 18:42:04 +01:00
|
|
|
import java.io.File
|
2020-07-17 16:35:39 +02:00
|
|
|
|
2020-11-04 18:42:04 +01:00
|
|
|
class SavedVolumesAdapter(private val context: Context, private val volumeDatabase: VolumeDatabase) : BaseAdapter() {
|
2020-07-17 16:35:39 +02:00
|
|
|
private val inflater: LayoutInflater = LayoutInflater.from(context)
|
2020-08-05 14:06:54 +02:00
|
|
|
private lateinit var nonScrollableColoredBorderListView: NonScrollableColoredBorderListView
|
2020-07-17 16:35:39 +02:00
|
|
|
|
2020-11-04 18:42:04 +01:00
|
|
|
override fun getCount(): Int {
|
|
|
|
return volumeDatabase.getVolumes().size
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 18:42:04 +01:00
|
|
|
override fun getItem(position: Int): Volume {
|
|
|
|
return volumeDatabase.getVolumes()[position]
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 18:42:04 +01:00
|
|
|
override fun getItemId(position: Int): Long {
|
|
|
|
return 0
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 18:42:04 +01:00
|
|
|
private fun deletePasswordHash(volume: Volume){
|
|
|
|
volumeDatabase.removeHash(volume)
|
|
|
|
volume.hash = null
|
|
|
|
volume.iv = null
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 18:42:04 +01:00
|
|
|
private fun deleteVolumeData(volume: Volume, parent: ViewGroup){
|
|
|
|
volumeDatabase.removeVolume(volume)
|
|
|
|
refresh(parent)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
2020-08-05 14:06:54 +02:00
|
|
|
if (!::nonScrollableColoredBorderListView.isInitialized){
|
|
|
|
nonScrollableColoredBorderListView = parent as NonScrollableColoredBorderListView
|
|
|
|
}
|
2020-07-17 16:35:39 +02:00
|
|
|
val view: View = convertView ?: inflater.inflate(R.layout.adapter_saved_volume, parent, false)
|
2020-11-04 18:42:04 +01:00
|
|
|
val volumeNameTextView = view.findViewById<TextView>(R.id.volume_name_textview)
|
2020-07-17 16:35:39 +02:00
|
|
|
val currentVolume = getItem(position)
|
2020-11-04 18:42:04 +01:00
|
|
|
volumeNameTextView.text = currentVolume.name
|
|
|
|
val deleteImageView = view.findViewById<ImageView>(R.id.delete_imageview)
|
|
|
|
deleteImageView.setOnClickListener {
|
2020-07-27 16:20:52 +02:00
|
|
|
val dialog = ColoredAlertDialogBuilder(context)
|
2020-07-17 16:35:39 +02:00
|
|
|
dialog.setTitle(R.string.warning)
|
2020-11-04 18:42:04 +01:00
|
|
|
if (currentVolume.isHidden){
|
|
|
|
if (currentVolume.hash != null) {
|
|
|
|
dialog.setMessage(R.string.hidden_volume_delete_question_hash)
|
|
|
|
dialog.setPositiveButton(R.string.password_hash){ _, _ ->
|
|
|
|
deletePasswordHash(currentVolume)
|
|
|
|
}
|
|
|
|
dialog.setNegativeButton(R.string.password_hash_and_path){ _, _ ->
|
|
|
|
deleteVolumeData(currentVolume, parent)
|
|
|
|
}
|
|
|
|
dialog.setNeutralButton(R.string.whole_volume){ _, _ ->
|
|
|
|
PathUtils.recursiveRemoveDirectory(File(PathUtils.pathJoin(context.filesDir.path, currentVolume.name)))
|
|
|
|
deleteVolumeData(currentVolume, parent)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dialog.setMessage(R.string.hidden_volume_delete_question)
|
|
|
|
dialog.setPositiveButton(R.string.path_only){ _, _ ->
|
|
|
|
deleteVolumeData(currentVolume, parent)
|
|
|
|
}
|
|
|
|
dialog.setNegativeButton(R.string.whole_volume){ _, _ ->
|
|
|
|
PathUtils.recursiveRemoveDirectory(File(PathUtils.pathJoin(context.filesDir.path, currentVolume.name)))
|
|
|
|
deleteVolumeData(currentVolume, parent)
|
|
|
|
}
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-04 18:42:04 +01:00
|
|
|
if (currentVolume.hash != null) {
|
|
|
|
dialog.setMessage(R.string.delete_hash_or_all)
|
|
|
|
dialog.setNegativeButton(R.string.password_hash_and_path) { _, _ ->
|
|
|
|
deleteVolumeData(currentVolume, parent)
|
|
|
|
}
|
|
|
|
dialog.setPositiveButton(R.string.password_hash) { _, _ ->
|
|
|
|
deletePasswordHash(currentVolume)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dialog.setMessage(R.string.ask_delete_volume_path)
|
|
|
|
dialog.setPositiveButton(R.string.ok) {_, _ ->
|
|
|
|
deleteVolumeData(currentVolume, parent)
|
|
|
|
}
|
|
|
|
dialog.setNegativeButton(R.string.cancel, null)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dialog.show()
|
|
|
|
}
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun refresh(parent: ViewGroup) {
|
|
|
|
notifyDataSetChanged()
|
|
|
|
if (count == 0){
|
2020-11-04 18:42:04 +01:00
|
|
|
WidgetUtil.hideWithPadding(parent)
|
2020-08-05 14:06:54 +02:00
|
|
|
} else {
|
|
|
|
nonScrollableColoredBorderListView.layoutParams.height = nonScrollableColoredBorderListView.computeHeight()
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|