From c521c7f998a962240b63630f9ea1a7fe166e8285 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Mon, 11 Apr 2022 15:14:15 +0200 Subject: [PATCH] Open default volume on application startup --- .../sushi/hardcore/droidfs/MainActivity.kt | 85 +++++++++++++++---- .../main/res/layout/dialog_open_volume.xml | 8 +- app/src/main/res/menu/main_activity.xml | 6 ++ app/src/main/res/values-es/strings.xml | 1 - app/src/main/res/values-pt-rBR/strings.xml | 1 - app/src/main/res/values-ru/strings.xml | 1 - app/src/main/res/values/strings.xml | 4 +- 7 files changed, 84 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt index b53a75f..5003aa8 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt @@ -33,15 +33,21 @@ import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder import sushi.hardcore.droidfs.widgets.EditTextDialog import java.io.File import java.util.* +import kotlin.NoSuchElementException class MainActivity : BaseActivity() { + companion object { + const val DEFAULT_VOLUME_KEY = "default_volume" + } + private lateinit var binding: ActivityMainBinding private lateinit var volumeDatabase: VolumeDatabase private lateinit var volumeAdapter: VolumeAdapter private var fingerprintProtector: FingerprintProtector? = null private var usfFingerprint: Boolean = false private var usfKeepOpen: Boolean = false + private var defaultVolumeName: String? = null private var addVolume = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> when (result.resultCode) { AddVolumeActivity.RESULT_VOLUME_ADDED -> onVolumeAdded() @@ -116,6 +122,15 @@ class MainActivity : BaseActivity() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 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 { bindService(it, object : ServiceConnection { 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) { if (volumeAdapter.selectedItems.isEmpty()) 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 { return when (item.itemId) { android.R.id.home -> { @@ -243,6 +272,11 @@ class MainActivity : BaseActivity() { }) true } + R.id.remove_default_open -> { + unsetDefaultVolume() + unselect(volumeAdapter.selectedItems.first()) + true + } R.id.copy -> { val position = volumeAdapter.selectedItems.elementAt(0) val volume = volumeAdapter.volumes[position] @@ -287,24 +321,21 @@ class MainActivity : BaseActivity() { menuInflater.inflate(R.menu.main_activity, menu) menu.findItem(R.id.settings).isVisible = !pickMode && !dropMode val isSelecting = volumeAdapter.selectedItems.isNotEmpty() - menu.findItem(R.id.select_all).isVisible = isSelecting && !pickMode && !dropMode - menu.findItem(R.id.remove).isVisible = isSelecting && !pickMode && !dropMode - var showForgetPassword = isSelecting - if (isSelecting) { - for (volume in volumeAdapter.selectedItems.map { i -> volumeAdapter.volumes[i] }) { - if (volume.encryptedHash == null) { - showForgetPassword = false - break - } - } - } - 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.select_all).isVisible = isSelecting + menu.findItem(R.id.remove).isVisible = isSelecting + menu.findItem(R.id.forget_password).isVisible = + isSelecting && + !volumeAdapter.selectedItems.any { i -> volumeAdapter.volumes[i].encryptedHash == null } + val onlyOneSelected = volumeAdapter.selectedItems.size == 1 + val onlyOneAndWriteable = + onlyOneSelected && + volumeAdapter.volumes[volumeAdapter.selectedItems.first()].canWrite(filesDir.path) 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)) { - isVisible = !pickMode && !dropMode && volumeAdapter.selectedItems.size == 1 + isVisible = onlyOneSelected if (isVisible) { setTitle(if (volumeAdapter.volumes[volumeAdapter.selectedItems.elementAt(0)].isHidden) R.string.copy_hidden_volume @@ -390,6 +421,13 @@ class MainActivity : BaseActivity() { if (success) { volumeDatabase.renameVolume(volume.name, newDBName) unselect(position) + if (volume.name == defaultVolumeName) { + with (sharedPrefs.edit()) { + putString(DEFAULT_VOLUME_KEY, newDBName) + apply() + } + defaultVolumeName = newDBName + } } else { 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) { + 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) dialogBinding.editPassword.text.getChars(0, password.size, password, 0) // openVolumeWithPassword is responsible for wiping the password @@ -457,8 +507,9 @@ class MainActivity : BaseActivity() { if (!usfFingerprint || fingerprintProtector == null || volume.encryptedHash != null) { dialogBinding.checkboxSavePassword.visibility = View.GONE } + dialogBinding.checkboxDefaultOpen.isChecked = defaultVolumeName == volume.name val dialog = CustomAlertDialogBuilder(this, themeValue) - .setTitle(R.string.open_dialog_title) + .setTitle(getString(R.string.open_dialog_title, volume.shortName)) .setView(dialogBinding.root) .setNegativeButton(R.string.cancel, null) .setPositiveButton(R.string.open) { _, _ -> diff --git a/app/src/main/res/layout/dialog_open_volume.xml b/app/src/main/res/layout/dialog_open_volume.xml index 0082c5d..23cbdf7 100644 --- a/app/src/main/res/layout/dialog_open_volume.xml +++ b/app/src/main/res/layout/dialog_open_volume.xml @@ -12,6 +12,7 @@ android:inputType="textPassword" android:maxLines="1" android:autofillHints="password" + android:hint="@string/password" android:imeOptions="actionDone"> @@ -20,7 +21,12 @@ android:id="@+id/checkbox_save_password" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_gravity="center" android:text="@string/fingerprint_save_checkbox_text"/> + + \ No newline at end of file diff --git a/app/src/main/res/menu/main_activity.xml b/app/src/main/res/menu/main_activity.xml index 5386ec9..cd78add 100644 --- a/app/src/main/res/menu/main_activity.xml +++ b/app/src/main/res/menu/main_activity.xml @@ -34,6 +34,12 @@ android:visible="false" android:title="@string/change_password"/> + + Añadir volumen Selecciona el directorio Volumen ya guardado - Introduce tu contraseña: Eliminar Ajustes Seleccionar todo diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 2eead71..e523c65 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -179,7 +179,6 @@ +%d segundos -%d segundos O volume já está salvo - Digite sua senha: Remover Configurações Selecionar tudo diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 21326c0..b0812fe 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -178,7 +178,6 @@ Добавить том Выбрать папку Том уже сохранён - Введите пароль: Удалить Настройки Выбрать все diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8422c8b..2d78ac2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -184,7 +184,7 @@ Add volume Pick directory Volume already saved - Enter your password: + Opening %s: Remove Settings Select All @@ -237,4 +237,6 @@ %d files 1 folder %d folders + Open this volume when launching the application + Don\'t open by default