Open default volume on application startup

This commit is contained in:
Matéo Duparc 2022-04-11 15:14:15 +02:00
parent 1d13dfbde3
commit c521c7f998
Signed by untrusted user: hardcoresushi
GPG Key ID: AFE384344A45E13A
7 changed files with 84 additions and 22 deletions

View File

@ -33,15 +33,21 @@ import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder
import sushi.hardcore.droidfs.widgets.EditTextDialog import sushi.hardcore.droidfs.widgets.EditTextDialog
import java.io.File import java.io.File
import java.util.* import java.util.*
import kotlin.NoSuchElementException
class MainActivity : BaseActivity() { class MainActivity : BaseActivity() {
companion object {
const val DEFAULT_VOLUME_KEY = "default_volume"
}
private lateinit var binding: ActivityMainBinding private lateinit var binding: ActivityMainBinding
private lateinit var volumeDatabase: VolumeDatabase private lateinit var volumeDatabase: VolumeDatabase
private lateinit var volumeAdapter: VolumeAdapter private lateinit var volumeAdapter: VolumeAdapter
private var fingerprintProtector: FingerprintProtector? = null private var fingerprintProtector: FingerprintProtector? = null
private var usfFingerprint: Boolean = false private var usfFingerprint: Boolean = false
private var usfKeepOpen: Boolean = false private var usfKeepOpen: Boolean = false
private var defaultVolumeName: String? = null
private var addVolume = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> private var addVolume = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
when (result.resultCode) { when (result.resultCode) {
AddVolumeActivity.RESULT_VOLUME_ADDED -> onVolumeAdded() AddVolumeActivity.RESULT_VOLUME_ADDED -> onVolumeAdded()
@ -116,6 +122,15 @@ class MainActivity : BaseActivity() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fingerprintProtector = FingerprintProtector.new(this, themeValue, volumeDatabase) fingerprintProtector = FingerprintProtector.new(this, themeValue, volumeDatabase)
} }
defaultVolumeName = sharedPrefs.getString(DEFAULT_VOLUME_KEY, null)
defaultVolumeName?.let { name ->
try {
val (position, volume) = volumeAdapter.volumes.withIndex().first { it.value.name == name }
openVolume(volume, position)
} catch (e: NoSuchElementException) {
unsetDefaultVolume()
}
}
Intent(this, FileOperationService::class.java).also { Intent(this, FileOperationService::class.java).also {
bindService(it, object : ServiceConnection { bindService(it, object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) { override fun onServiceConnected(className: ComponentName, service: IBinder) {
@ -126,6 +141,12 @@ class MainActivity : BaseActivity() {
} }
} }
override fun onStart() {
super.onStart()
// refresh this in case another instance of MainActivity changes its value
defaultVolumeName = sharedPrefs.getString(DEFAULT_VOLUME_KEY, null)
}
private fun onVolumeItemClick(volume: Volume, position: Int) { private fun onVolumeItemClick(volume: Volume, position: Int) {
if (volumeAdapter.selectedItems.isEmpty()) if (volumeAdapter.selectedItems.isEmpty())
openVolume(volume, position) openVolume(volume, position)
@ -206,6 +227,14 @@ class MainActivity : BaseActivity() {
} }
} }
private fun unsetDefaultVolume() {
with (sharedPrefs.edit()) {
remove(DEFAULT_VOLUME_KEY)
apply()
}
defaultVolumeName = null
}
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) { return when (item.itemId) {
android.R.id.home -> { android.R.id.home -> {
@ -243,6 +272,11 @@ class MainActivity : BaseActivity() {
}) })
true true
} }
R.id.remove_default_open -> {
unsetDefaultVolume()
unselect(volumeAdapter.selectedItems.first())
true
}
R.id.copy -> { R.id.copy -> {
val position = volumeAdapter.selectedItems.elementAt(0) val position = volumeAdapter.selectedItems.elementAt(0)
val volume = volumeAdapter.volumes[position] val volume = volumeAdapter.volumes[position]
@ -287,24 +321,21 @@ class MainActivity : BaseActivity() {
menuInflater.inflate(R.menu.main_activity, menu) menuInflater.inflate(R.menu.main_activity, menu)
menu.findItem(R.id.settings).isVisible = !pickMode && !dropMode menu.findItem(R.id.settings).isVisible = !pickMode && !dropMode
val isSelecting = volumeAdapter.selectedItems.isNotEmpty() val isSelecting = volumeAdapter.selectedItems.isNotEmpty()
menu.findItem(R.id.select_all).isVisible = isSelecting && !pickMode && !dropMode menu.findItem(R.id.select_all).isVisible = isSelecting
menu.findItem(R.id.remove).isVisible = isSelecting && !pickMode && !dropMode menu.findItem(R.id.remove).isVisible = isSelecting
var showForgetPassword = isSelecting menu.findItem(R.id.forget_password).isVisible =
if (isSelecting) { isSelecting &&
for (volume in volumeAdapter.selectedItems.map { i -> volumeAdapter.volumes[i] }) { !volumeAdapter.selectedItems.any { i -> volumeAdapter.volumes[i].encryptedHash == null }
if (volume.encryptedHash == null) { val onlyOneSelected = volumeAdapter.selectedItems.size == 1
showForgetPassword = false val onlyOneAndWriteable =
break onlyOneSelected &&
} volumeAdapter.volumes[volumeAdapter.selectedItems.first()].canWrite(filesDir.path)
}
}
menu.findItem(R.id.forget_password).isVisible = showForgetPassword && !pickMode
val onlyOneAndWriteable = !pickMode && !dropMode &&
volumeAdapter.selectedItems.size == 1 &&
volumeAdapter.volumes[volumeAdapter.selectedItems.elementAt(0)].canWrite(filesDir.path)
menu.findItem(R.id.change_password).isVisible = onlyOneAndWriteable menu.findItem(R.id.change_password).isVisible = onlyOneAndWriteable
menu.findItem(R.id.remove_default_open).isVisible =
onlyOneSelected &&
volumeAdapter.volumes[volumeAdapter.selectedItems.first()].name == defaultVolumeName
with(menu.findItem(R.id.copy)) { with(menu.findItem(R.id.copy)) {
isVisible = !pickMode && !dropMode && volumeAdapter.selectedItems.size == 1 isVisible = onlyOneSelected
if (isVisible) { if (isVisible) {
setTitle(if (volumeAdapter.volumes[volumeAdapter.selectedItems.elementAt(0)].isHidden) setTitle(if (volumeAdapter.volumes[volumeAdapter.selectedItems.elementAt(0)].isHidden)
R.string.copy_hidden_volume R.string.copy_hidden_volume
@ -390,6 +421,13 @@ class MainActivity : BaseActivity() {
if (success) { if (success) {
volumeDatabase.renameVolume(volume.name, newDBName) volumeDatabase.renameVolume(volume.name, newDBName)
unselect(position) unselect(position)
if (volume.name == defaultVolumeName) {
with (sharedPrefs.edit()) {
putString(DEFAULT_VOLUME_KEY, newDBName)
apply()
}
defaultVolumeName = newDBName
}
} else { } else {
Toast.makeText(this, R.string.volume_rename_failed, Toast.LENGTH_SHORT).show() Toast.makeText(this, R.string.volume_rename_failed, Toast.LENGTH_SHORT).show()
} }
@ -441,6 +479,18 @@ class MainActivity : BaseActivity() {
} }
private fun onPasswordSubmitted(volume: Volume, position: Int, dialogBinding: DialogOpenVolumeBinding) { private fun onPasswordSubmitted(volume: Volume, position: Int, dialogBinding: DialogOpenVolumeBinding) {
if (dialogBinding.checkboxDefaultOpen.isChecked xor (defaultVolumeName == volume.name)) {
with (sharedPrefs.edit()) {
defaultVolumeName = if (dialogBinding.checkboxDefaultOpen.isChecked) {
putString(DEFAULT_VOLUME_KEY, volume.name)
volume.name
} else {
remove(DEFAULT_VOLUME_KEY)
null
}
apply()
}
}
val password = CharArray(dialogBinding.editPassword.text.length) val password = CharArray(dialogBinding.editPassword.text.length)
dialogBinding.editPassword.text.getChars(0, password.size, password, 0) dialogBinding.editPassword.text.getChars(0, password.size, password, 0)
// openVolumeWithPassword is responsible for wiping the password // openVolumeWithPassword is responsible for wiping the password
@ -457,8 +507,9 @@ class MainActivity : BaseActivity() {
if (!usfFingerprint || fingerprintProtector == null || volume.encryptedHash != null) { if (!usfFingerprint || fingerprintProtector == null || volume.encryptedHash != null) {
dialogBinding.checkboxSavePassword.visibility = View.GONE dialogBinding.checkboxSavePassword.visibility = View.GONE
} }
dialogBinding.checkboxDefaultOpen.isChecked = defaultVolumeName == volume.name
val dialog = CustomAlertDialogBuilder(this, themeValue) val dialog = CustomAlertDialogBuilder(this, themeValue)
.setTitle(R.string.open_dialog_title) .setTitle(getString(R.string.open_dialog_title, volume.shortName))
.setView(dialogBinding.root) .setView(dialogBinding.root)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.open) { _, _ -> .setPositiveButton(R.string.open) { _, _ ->

View File

@ -12,6 +12,7 @@
android:inputType="textPassword" android:inputType="textPassword"
android:maxLines="1" android:maxLines="1"
android:autofillHints="password" android:autofillHints="password"
android:hint="@string/password"
android:imeOptions="actionDone"> android:imeOptions="actionDone">
<requestFocus/> <requestFocus/>
</EditText> </EditText>
@ -20,7 +21,12 @@
android:id="@+id/checkbox_save_password" android:id="@+id/checkbox_save_password"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/fingerprint_save_checkbox_text"/> android:text="@string/fingerprint_save_checkbox_text"/>
<CheckBox
android:id="@+id/checkbox_default_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_open"/>
</LinearLayout> </LinearLayout>

View File

@ -34,6 +34,12 @@
android:visible="false" android:visible="false"
android:title="@string/change_password"/> android:title="@string/change_password"/>
<item
android:id="@+id/remove_default_open"
app:showAsAction="never"
android:visible="false"
android:title="@string/remove_default_open"/>
<item <item
android:id="@+id/copy" android:id="@+id/copy"
app:showAsAction="never" app:showAsAction="never"

View File

@ -184,7 +184,6 @@
<string name="add_volume">Añadir volumen</string> <string name="add_volume">Añadir volumen</string>
<string name="pick_directory">Selecciona el directorio</string> <string name="pick_directory">Selecciona el directorio</string>
<string name="volume_alread_saved">Volumen ya guardado</string> <string name="volume_alread_saved">Volumen ya guardado</string>
<string name="open_dialog_title">Introduce tu contraseña:</string>
<string name="remove">Eliminar</string> <string name="remove">Eliminar</string>
<string name="settings">Ajustes</string> <string name="settings">Ajustes</string>
<string name="select_all">Seleccionar todo</string> <string name="select_all">Seleccionar todo</string>

View File

@ -179,7 +179,6 @@
<string name="seek_seconds_forward">+%d segundos</string> <string name="seek_seconds_forward">+%d segundos</string>
<string name="seek_seconds_backward">-%d segundos</string> <string name="seek_seconds_backward">-%d segundos</string>
<string name="volume_alread_saved">O volume já está salvo</string> <string name="volume_alread_saved">O volume já está salvo</string>
<string name="open_dialog_title">Digite sua senha:</string>
<string name="remove">Remover</string> <string name="remove">Remover</string>
<string name="settings">Configurações</string> <string name="settings">Configurações</string>
<string name="select_all">Selecionar tudo</string> <string name="select_all">Selecionar tudo</string>

View File

@ -178,7 +178,6 @@
<string name="add_volume">Добавить том</string> <string name="add_volume">Добавить том</string>
<string name="pick_directory">Выбрать папку</string> <string name="pick_directory">Выбрать папку</string>
<string name="volume_alread_saved">Том уже сохранён</string> <string name="volume_alread_saved">Том уже сохранён</string>
<string name="open_dialog_title">Введите пароль:</string>
<string name="remove">Удалить</string> <string name="remove">Удалить</string>
<string name="settings">Настройки</string> <string name="settings">Настройки</string>
<string name="select_all">Выбрать все</string> <string name="select_all">Выбрать все</string>

View File

@ -184,7 +184,7 @@
<string name="add_volume">Add volume</string> <string name="add_volume">Add volume</string>
<string name="pick_directory">Pick directory</string> <string name="pick_directory">Pick directory</string>
<string name="volume_alread_saved">Volume already saved</string> <string name="volume_alread_saved">Volume already saved</string>
<string name="open_dialog_title">Enter your password:</string> <string name="open_dialog_title">Opening %s:</string>
<string name="remove">Remove</string> <string name="remove">Remove</string>
<string name="settings">Settings</string> <string name="settings">Settings</string>
<string name="select_all">Select All</string> <string name="select_all">Select All</string>
@ -237,4 +237,6 @@
<string name="multiple_files">%d files</string> <string name="multiple_files">%d files</string>
<string name="one_folder">1 folder</string> <string name="one_folder">1 folder</string>
<string name="multiple_folders">%d folders</string> <string name="multiple_folders">%d folders</string>
<string name="default_open">Open this volume when launching the application</string>
<string name="remove_default_open">Don\'t open by default</string>
</resources> </resources>