Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Password Protection (whole app and note deletion) #21

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion app/src/main/kotlin/org/fossify/notes/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class MainActivity : SimpleActivity() {

private val binding by viewBinding(ActivityMainBinding::inflate)

private var mIsPasswordProtectionPending = false
private var mWasProtectionHandled = false

override fun onCreate(savedInstanceState: Bundle?) {
isMaterialActivity = true
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -121,6 +124,23 @@ class MainActivity : SimpleActivity() {

checkAppOnSDCard()
setupSearchButtons()

mIsPasswordProtectionPending = config.isAppPasswordProtectionOn

if (savedInstanceState == null) {
binding.viewPager.beGoneIf(mIsPasswordProtectionPending)
if (mIsPasswordProtectionPending && !mWasProtectionHandled) {
handleAppPasswordProtection {
mWasProtectionHandled = it
if (it) {
mIsPasswordProtectionPending = false
binding.viewPager.beVisible()
} else {
finish()
}
}
}
}
}

override fun onResume() {
Expand Down Expand Up @@ -1069,7 +1089,13 @@ class MainActivity : SimpleActivity() {

private fun displayDeleteNotePrompt() {
DeleteNoteDialog(this, mCurrentNote) {
deleteNote(it, mCurrentNote)
if (config.isDeletePasswordProtectionOn) {
handleDeletePasswordProtection {
deleteNote(it, mCurrentNote)
}
} else {
deleteNote(it, mCurrentNote)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import androidx.core.view.ViewCompat
import kotlinx.serialization.SerializationException
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.fossify.commons.dialogs.ConfirmationDialog
import org.fossify.commons.dialogs.RadioGroupDialog
import org.fossify.commons.dialogs.SecurityDialog
import org.fossify.commons.extensions.*
import org.fossify.commons.helpers.*
import org.fossify.commons.models.RadioItem
Expand Down Expand Up @@ -62,6 +64,8 @@ class SettingsActivity : SimpleActivity() {
setupNotesImport()
setupEnableAutomaticBackups()
setupManageAutomaticBackups()
setupAppPasswordProtection()
setupNoteDeletionPasswordProtection()
updateTextColors(binding.settingsNestedScrollview)

arrayOf(
Expand Down Expand Up @@ -395,4 +399,48 @@ class SettingsActivity : SimpleActivity() {
binding.settingsEnableAutomaticBackups.isChecked = enable
binding.settingsManageAutomaticBackupsHolder.beVisibleIf(enable)
}

private fun setupAppPasswordProtection() {
binding.settingsAppPasswordProtection.isChecked = config.isAppPasswordProtectionOn
binding.settingsAppPasswordProtectionHolder.setOnClickListener {
val tabToShow = if (config.isAppPasswordProtectionOn) config.appProtectionType else SHOW_ALL_TABS
SecurityDialog(this, config.appPasswordHash, tabToShow) { hash, type, success ->
if (success) {
val hasPasswordProtection = config.isAppPasswordProtectionOn
binding.settingsAppPasswordProtection.isChecked = !hasPasswordProtection
config.isAppPasswordProtectionOn = !hasPasswordProtection
config.appPasswordHash = if (hasPasswordProtection) "" else hash
config.appProtectionType = type

if (config.isAppPasswordProtectionOn) {
val confirmationTextId = if (config.appProtectionType == PROTECTION_FINGERPRINT)
org.fossify.commons.R.string.fingerprint_setup_successfully else org.fossify.commons.R.string.protection_setup_successfully
ConfirmationDialog(this, "", confirmationTextId, org.fossify.commons.R.string.ok, 0) { }
}
}
}
}
}

private fun setupNoteDeletionPasswordProtection() {
binding.settingsNoteDeletionPasswordProtection.isChecked = config.isDeletePasswordProtectionOn
binding.settingsNoteDeletionPasswordProtectionHolder.setOnClickListener {
val tabToShow = if (config.isDeletePasswordProtectionOn) config.deleteProtectionType else SHOW_ALL_TABS
SecurityDialog(this, config.deletePasswordHash, tabToShow) { hash, type, success ->
if (success) {
val hasPasswordProtection = config.isDeletePasswordProtectionOn
binding.settingsNoteDeletionPasswordProtection.isChecked = !hasPasswordProtection
config.isDeletePasswordProtectionOn = !hasPasswordProtection
config.deletePasswordHash = if (hasPasswordProtection) "" else hash
config.deleteProtectionType = type

if (config.isDeletePasswordProtectionOn) {
val confirmationTextId = if (config.deleteProtectionType == PROTECTION_FINGERPRINT)
org.fossify.commons.R.string.fingerprint_setup_successfully else org.fossify.commons.R.string.protection_setup_successfully
ConfirmationDialog(this, "", confirmationTextId, org.fossify.commons.R.string.ok, 0) { }
}
}
}
}
}
}
45 changes: 45 additions & 0 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,51 @@
android:id="@+id/settings_saving_divider"
layout="@layout/divider" />

<include
android:id="@+id/settings_extended_details_divider"
layout="@layout/divider" />

<TextView
android:id="@+id/settings_security_label"
style="@style/SettingsSectionLabelStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/security" />

<RelativeLayout
android:id="@+id/settings_app_password_protection_holder"
style="@style/SettingsHolderCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<org.fossify.commons.views.MyAppCompatCheckbox
android:id="@+id/settings_app_password_protection"
style="@style/SettingsCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/password_protect_whole_app" />

</RelativeLayout>

<RelativeLayout
android:id="@+id/settings_note_deletion_password_protection_holder"
style="@style/SettingsHolderCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<org.fossify.commons.views.MyAppCompatCheckbox
android:id="@+id/settings_note_deletion_password_protection"
style="@style/SettingsCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/password_protect_note_deletion" />

</RelativeLayout>

<include
android:id="@+id/settings_security_divider"
layout="@layout/divider" />

<TextView
android:id="@+id/settings_migrating_label"
style="@style/SettingsSectionLabelStyle"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<string name="use_incognito_mode">Use Incognito mode of keyboards</string>
<string name="move_done_checklist_items">Move done checklist items to the bottom</string>
<string name="add_new_checklist_items_top">Add new checklist items at the top</string>
<string name="password_protect_note_deletion">Password protect note deletion</string>

<!-- Checklists -->
<string name="checklist">Checklist</string>
Expand Down