Format some code

This commit is contained in:
Naveen Singh
2025-03-17 01:27:20 +05:30
parent 5619b55120
commit 545fc855ed
2 changed files with 388 additions and 88 deletions

View File

@@ -2,7 +2,6 @@ package org.fossify.notes.activities
import android.accounts.NetworkErrorException
import android.annotation.SuppressLint
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
@@ -27,10 +26,62 @@ import android.webkit.WebViewClient
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.net.toUri
import androidx.viewpager.widget.ViewPager
import org.fossify.commons.dialogs.*
import org.fossify.commons.extensions.*
import org.fossify.commons.helpers.*
import org.fossify.commons.dialogs.ConfirmationAdvancedDialog
import org.fossify.commons.dialogs.ConfirmationDialog
import org.fossify.commons.dialogs.FilePickerDialog
import org.fossify.commons.dialogs.RadioGroupDialog
import org.fossify.commons.dialogs.SecurityDialog
import org.fossify.commons.extensions.appLaunched
import org.fossify.commons.extensions.appLockManager
import org.fossify.commons.extensions.applyColorFilter
import org.fossify.commons.extensions.baseConfig
import org.fossify.commons.extensions.beVisibleIf
import org.fossify.commons.extensions.checkWhatsNew
import org.fossify.commons.extensions.clearBackgroundSpans
import org.fossify.commons.extensions.convertToBitmap
import org.fossify.commons.extensions.deleteFile
import org.fossify.commons.extensions.fadeIn
import org.fossify.commons.extensions.fadeOut
import org.fossify.commons.extensions.getContrastColor
import org.fossify.commons.extensions.getCurrentFormattedDateTime
import org.fossify.commons.extensions.getDocumentFile
import org.fossify.commons.extensions.getFilenameFromContentUri
import org.fossify.commons.extensions.getFilenameFromPath
import org.fossify.commons.extensions.getProperBackgroundColor
import org.fossify.commons.extensions.getProperPrimaryColor
import org.fossify.commons.extensions.getProperStatusBarColor
import org.fossify.commons.extensions.getRealPathFromURI
import org.fossify.commons.extensions.handleDeletePasswordProtection
import org.fossify.commons.extensions.hasPermission
import org.fossify.commons.extensions.hideKeyboard
import org.fossify.commons.extensions.highlightText
import org.fossify.commons.extensions.isMediaFile
import org.fossify.commons.extensions.launchMoreAppsFromUsIntent
import org.fossify.commons.extensions.needsStupidWritePermissions
import org.fossify.commons.extensions.onGlobalLayout
import org.fossify.commons.extensions.onPageChangeListener
import org.fossify.commons.extensions.onTextChangeListener
import org.fossify.commons.extensions.performSecurityCheck
import org.fossify.commons.extensions.searchMatches
import org.fossify.commons.extensions.shortcutManager
import org.fossify.commons.extensions.showErrorToast
import org.fossify.commons.extensions.showKeyboard
import org.fossify.commons.extensions.toast
import org.fossify.commons.extensions.updateTextColors
import org.fossify.commons.extensions.value
import org.fossify.commons.extensions.viewBinding
import org.fossify.commons.helpers.LICENSE_RTL
import org.fossify.commons.helpers.PERMISSION_READ_STORAGE
import org.fossify.commons.helpers.PERMISSION_WRITE_STORAGE
import org.fossify.commons.helpers.PROTECTION_NONE
import org.fossify.commons.helpers.REAL_FILE_PATH
import org.fossify.commons.helpers.SHOW_ALL_TABS
import org.fossify.commons.helpers.ensureBackgroundThread
import org.fossify.commons.helpers.isNougatMR1Plus
import org.fossify.commons.helpers.isQPlus
import org.fossify.commons.models.FAQItem
import org.fossify.commons.models.FileDirItem
import org.fossify.commons.models.RadioItem
@@ -41,15 +92,33 @@ import org.fossify.notes.R
import org.fossify.notes.adapters.NotesPagerAdapter
import org.fossify.notes.databases.NotesDatabase
import org.fossify.notes.databinding.ActivityMainBinding
import org.fossify.notes.dialogs.*
import org.fossify.notes.extensions.*
import org.fossify.notes.dialogs.DeleteNoteDialog
import org.fossify.notes.dialogs.ExportFileDialog
import org.fossify.notes.dialogs.ImportFolderDialog
import org.fossify.notes.dialogs.NewNoteDialog
import org.fossify.notes.dialogs.OpenFileDialog
import org.fossify.notes.dialogs.OpenNoteDialog
import org.fossify.notes.dialogs.RenameNoteDialog
import org.fossify.notes.dialogs.SortChecklistDialog
import org.fossify.notes.extensions.config
import org.fossify.notes.extensions.getPercentageFontSize
import org.fossify.notes.extensions.notesDB
import org.fossify.notes.extensions.parseChecklistItems
import org.fossify.notes.extensions.updateWidgets
import org.fossify.notes.extensions.widgetsDB
import org.fossify.notes.fragments.TextFragment
import org.fossify.notes.helpers.*
import org.fossify.notes.helpers.MIME_TEXT_PLAIN
import org.fossify.notes.helpers.MyMovementMethod
import org.fossify.notes.helpers.NEW_CHECKLIST
import org.fossify.notes.helpers.NEW_TEXT_NOTE
import org.fossify.notes.helpers.NotesHelper
import org.fossify.notes.helpers.OPEN_NOTE_ID
import org.fossify.notes.helpers.SHORTCUT_NEW_CHECKLIST
import org.fossify.notes.helpers.SHORTCUT_NEW_TEXT_NOTE
import org.fossify.notes.models.Note
import org.fossify.notes.models.NoteType
import java.io.File
import java.nio.charset.Charset
import java.util.Arrays
class MainActivity : SimpleActivity() {
private val EXPORT_FILE_SYNC = 1
@@ -91,7 +160,12 @@ class MainActivity : SimpleActivity() {
setupOptionsMenu()
refreshMenuItems()
updateMaterialActivityViews(binding.mainCoordinator, null, useTransparentNavigation = false, useTopSearchMenu = false)
updateMaterialActivityViews(
mainCoordinatorLayout = binding.mainCoordinator,
nestedView = null,
useTransparentNavigation = false,
useTopSearchMenu = false
)
searchQueryET = findViewById(org.fossify.commons.R.id.search_query)
searchPrevBtn = findViewById(org.fossify.commons.R.id.search_previous)
@@ -194,9 +268,12 @@ class MainActivity : SimpleActivity() {
findItem(R.id.remove_done_items).isVisible = isCurrentItemChecklist
findItem(R.id.sort_checklist).isVisible = isCurrentItemChecklist
findItem(R.id.import_folder).isVisible = !isQPlus()
findItem(R.id.lock_note).isVisible = mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && !mCurrentNote.isLocked())
findItem(R.id.unlock_note).isVisible = mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && mCurrentNote.isLocked())
findItem(R.id.more_apps_from_us).isVisible = !resources.getBoolean(org.fossify.commons.R.bool.hide_google_relations)
findItem(R.id.lock_note).isVisible =
mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && !mCurrentNote.isLocked())
findItem(R.id.unlock_note).isVisible =
mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && mCurrentNote.isLocked())
findItem(R.id.more_apps_from_us).isVisible =
!resources.getBoolean(org.fossify.commons.R.bool.hide_google_relations)
saveNoteButton = findItem(R.id.save_note)
saveNoteButton!!.isVisible =
@@ -266,11 +343,11 @@ class MainActivity : SimpleActivity() {
override fun onBackPressed() {
if (!config.autosaveNotes && mAdapter?.anyHasUnsavedChanges() == true) {
ConfirmationAdvancedDialog(
this,
"",
R.string.unsaved_changes_warning,
org.fossify.commons.R.string.save,
org.fossify.commons.R.string.discard
activity = this,
message = "",
messageId = R.string.unsaved_changes_warning,
positive = org.fossify.commons.R.string.save,
negative = org.fossify.commons.R.string.discard
) {
if (it) {
mAdapter?.saveAllFragmentTexts()
@@ -297,14 +374,23 @@ class MainActivity : SimpleActivity() {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK && resultData != null && resultData.data != null) {
importUri(resultData.data!!)
} else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null && mNotes.isNotEmpty()) {
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
applicationContext.contentResolver.takePersistableUriPermission(resultData.data!!, takeFlags)
} else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == RESULT_OK && resultData != null && resultData.data != null && mNotes.isNotEmpty()) {
val takeFlags =
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
applicationContext.contentResolver.takePersistableUriPermission(
resultData.data!!, takeFlags
)
showExportFilePickUpdateDialog(resultData.dataString!!, getCurrentNoteValue())
}
}
private fun isCurrentItemChecklist() = if (::mCurrentNote.isInitialized) mCurrentNote.type == NoteType.TYPE_CHECKLIST else false
private fun isCurrentItemChecklist(): Boolean {
return if (::mCurrentNote.isInitialized) {
mCurrentNote.type == NoteType.TYPE_CHECKLIST
} else {
false
}
}
@SuppressLint("NewApi")
private fun checkShortcuts() {
@@ -314,7 +400,7 @@ class MainActivity : SimpleActivity() {
val newChecklist = getNewChecklistShortcut(appIconColor)
try {
shortcutManager.dynamicShortcuts = Arrays.asList(newTextNote, newChecklist)
shortcutManager.dynamicShortcuts = listOf(newTextNote, newChecklist)
config.lastHandledShortcutColor = appIconColor
} catch (ignored: Exception) {
}
@@ -325,8 +411,12 @@ class MainActivity : SimpleActivity() {
private fun getNewTextNoteShortcut(appIconColor: Int): ShortcutInfo {
val shortLabel = getString(R.string.text_note)
val longLabel = getString(R.string.new_text_note)
val drawable = resources.getDrawable(org.fossify.commons.R.drawable.shortcut_plus)
(drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_plus_background).applyColorFilter(appIconColor)
val drawable = AppCompatResources.getDrawable(
this, org.fossify.commons.R.drawable.shortcut_plus
)
(drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_plus_background)
.applyColorFilter(appIconColor)
val bmp = drawable.convertToBitmap()
val intent = Intent(this, MainActivity::class.java)
@@ -344,8 +434,9 @@ class MainActivity : SimpleActivity() {
private fun getNewChecklistShortcut(appIconColor: Int): ShortcutInfo {
val shortLabel = getString(R.string.checklist)
val longLabel = getString(R.string.new_checklist)
val drawable = resources.getDrawable(R.drawable.shortcut_check)
(drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_plus_background).applyColorFilter(appIconColor)
val drawable = AppCompatResources.getDrawable(this, R.drawable.shortcut_check)
(drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_plus_background)
.applyColorFilter(appIconColor)
val bmp = drawable.convertToBitmap()
val intent = Intent(this, MainActivity::class.java)
@@ -376,10 +467,26 @@ class MainActivity : SimpleActivity() {
val file = File(realPath)
handleUri(Uri.fromFile(file))
} else if (intent.getBooleanExtra(NEW_TEXT_NOTE, false)) {
val newTextNote = Note(null, getCurrentFormattedDateTime(), "", NoteType.TYPE_TEXT, "", PROTECTION_NONE, "")
val newTextNote = Note(
null,
getCurrentFormattedDateTime(),
"",
NoteType.TYPE_TEXT,
"",
PROTECTION_NONE,
""
)
addNewNote(newTextNote)
} else if (intent.getBooleanExtra(NEW_CHECKLIST, false)) {
val newChecklist = Note(null, getCurrentFormattedDateTime(), "", NoteType.TYPE_CHECKLIST, "", PROTECTION_NONE, "")
val newChecklist = Note(
null,
getCurrentFormattedDateTime(),
"",
NoteType.TYPE_CHECKLIST,
"",
PROTECTION_NONE,
""
)
addNewNote(newChecklist)
} else {
handleUri(data!!)
@@ -582,7 +689,8 @@ class MainActivity : SimpleActivity() {
private fun getWantedNoteIndex(wantedNoteId: Long?): Int {
intent.removeExtra(OPEN_NOTE_ID)
val noteIdToOpen = if (wantedNoteId == null || wantedNoteId == -1L) config.currentNoteId else wantedNoteId
val noteIdToOpen =
if (wantedNoteId == null || wantedNoteId == -1L) config.currentNoteId else wantedNoteId
return getNoteIndexWithId(noteIdToOpen)
}
@@ -613,7 +721,12 @@ class MainActivity : SimpleActivity() {
}
}
private fun displayNewNoteDialog(value: String = "", title: String? = null, path: String = "", setChecklistAsDefault: Boolean = false) {
private fun displayNewNoteDialog(
value: String = "",
title: String? = null,
path: String = "",
setChecklistAsDefault: Boolean = false,
) {
NewNoteDialog(this, title, setChecklistAsDefault) {
it.value = value
it.path = path
@@ -644,15 +757,38 @@ class MainActivity : SimpleActivity() {
val licenses = LICENSE_RTL
val faqItems = arrayListOf(
FAQItem(org.fossify.commons.R.string.faq_1_title_commons, org.fossify.commons.R.string.faq_1_text_commons),
FAQItem(
org.fossify.commons.R.string.faq_1_title_commons,
org.fossify.commons.R.string.faq_1_text_commons
),
FAQItem(R.string.faq_1_title, R.string.faq_1_text)
)
if (!resources.getBoolean(org.fossify.commons.R.bool.hide_google_relations)) {
faqItems.add(FAQItem(org.fossify.commons.R.string.faq_2_title_commons, org.fossify.commons.R.string.faq_2_text_commons))
faqItems.add(FAQItem(org.fossify.commons.R.string.faq_6_title_commons, org.fossify.commons.R.string.faq_6_text_commons))
faqItems.add(FAQItem(org.fossify.commons.R.string.faq_7_title_commons, org.fossify.commons.R.string.faq_7_text_commons))
faqItems.add(FAQItem(org.fossify.commons.R.string.faq_10_title_commons, org.fossify.commons.R.string.faq_10_text_commons))
faqItems.add(
FAQItem(
org.fossify.commons.R.string.faq_2_title_commons,
org.fossify.commons.R.string.faq_2_text_commons
)
)
faqItems.add(
FAQItem(
org.fossify.commons.R.string.faq_6_title_commons,
org.fossify.commons.R.string.faq_6_text_commons
)
)
faqItems.add(
FAQItem(
org.fossify.commons.R.string.faq_7_title_commons,
org.fossify.commons.R.string.faq_7_text_commons
)
)
faqItems.add(
FAQItem(
org.fossify.commons.R.string.faq_10_title_commons,
org.fossify.commons.R.string.faq_10_text_commons
)
)
}
startAboutActivity(R.string.app_name, licenses, BuildConfig.VERSION_NAME, faqItems, true)
@@ -688,10 +824,23 @@ class MainActivity : SimpleActivity() {
val checklistItems = fileText.parseChecklistItems()
if (checklistItems != null) {
val title = it.absolutePath.getFilenameFromPath().substringBeforeLast('.')
val note = Note(null, title, fileText, NoteType.TYPE_CHECKLIST, "", PROTECTION_NONE, "")
val note = Note(
null,
title,
fileText,
NoteType.TYPE_CHECKLIST,
"",
PROTECTION_NONE,
""
)
runOnUiThread {
OpenFileDialog(this, it.path) {
displayNewNoteDialog(note.value, title = it.title, it.path, setChecklistAsDefault = true)
displayNewNoteDialog(
note.value,
title = it.title,
it.path,
setChecklistAsDefault = true
)
}
}
} else {
@@ -768,7 +917,9 @@ class MainActivity : SimpleActivity() {
private fun addNoteFromUri(uri: Uri, filename: String? = null) {
val noteTitle = when {
filename?.isEmpty() == false -> filename
uri.toString().startsWith("content://") -> getFilenameFromContentUri(uri) ?: getNewNoteTitle()
uri.toString().startsWith("content://") -> getFilenameFromContentUri(uri)
?: getNewNoteTitle()
else -> getNewNoteTitle()
}
@@ -782,7 +933,8 @@ class MainActivity : SimpleActivity() {
true
} else {
try {
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
val takeFlags =
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
applicationContext.contentResolver.takePersistableUriPermission(uri, takeFlags)
true
} catch (e: Exception) {
@@ -816,7 +968,15 @@ class MainActivity : SimpleActivity() {
val fileText = it.readText().trim()
val checklistItems = fileText.parseChecklistItems()
val note = if (checklistItems != null) {
Note(null, title.substringBeforeLast('.'), fileText, NoteType.TYPE_CHECKLIST, "", PROTECTION_NONE, "")
Note(
null,
title.substringBeforeLast('.'),
fileText,
NoteType.TYPE_CHECKLIST,
"",
PROTECTION_NONE,
""
)
} else {
Note(null, title, "", NoteType.TYPE_TEXT, path, PROTECTION_NONE, "")
}
@@ -889,7 +1049,8 @@ class MainActivity : SimpleActivity() {
private fun exportAsFile() {
ExportFileDialog(this, mCurrentNote) {
val textToExport = if (mCurrentNote.type == NoteType.TYPE_TEXT) getCurrentNoteText() else mCurrentNote.value
val textToExport =
if (mCurrentNote.type == NoteType.TYPE_TEXT) getCurrentNoteText() else mCurrentNote.value
if (textToExport == null || textToExport.isEmpty()) {
toast(org.fossify.commons.R.string.unknown_error_occurred)
} else if (mCurrentNote.type == NoteType.TYPE_TEXT) {
@@ -908,7 +1069,12 @@ class MainActivity : SimpleActivity() {
RadioGroupDialog(this, items) {
val syncFile = it as Int == EXPORT_FILE_SYNC
tryExportNoteValueToFile(exportPath, mCurrentNote.title, textToExport, true) { exportedSuccessfully ->
tryExportNoteValueToFile(
exportPath,
mCurrentNote.title,
textToExport,
true
) { exportedSuccessfully ->
if (exportedSuccessfully) {
if (syncFile) {
mCurrentNote.path = exportPath
@@ -918,16 +1084,26 @@ class MainActivity : SimpleActivity() {
mCurrentNote.value = textToExport
}
getPagerAdapter().updateCurrentNoteData(binding.viewPager.currentItem, mCurrentNote.path, mCurrentNote.value)
getPagerAdapter().updateCurrentNoteData(
binding.viewPager.currentItem,
mCurrentNote.path,
mCurrentNote.value
)
NotesHelper(this).insertOrUpdateNote(mCurrentNote)
}
}
}
}
fun tryExportNoteValueToFile(path: String, title: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
fun tryExportNoteValueToFile(
path: String,
title: String,
content: String,
showSuccessToasts: Boolean,
callback: ((success: Boolean) -> Unit)? = null,
) {
if (path.startsWith("content://")) {
exportNoteValueToUri(Uri.parse(path), title, content, showSuccessToasts, callback)
exportNoteValueToUri(path.toUri(), title, content, showSuccessToasts, callback)
} else {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
@@ -937,7 +1113,12 @@ class MainActivity : SimpleActivity() {
}
}
private fun exportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
private fun exportNoteValueToFile(
path: String,
content: String,
showSuccessToasts: Boolean,
callback: ((success: Boolean) -> Unit)? = null,
) {
try {
if (File(path).isDirectory) {
toast(org.fossify.commons.R.string.name_taken)
@@ -980,7 +1161,13 @@ class MainActivity : SimpleActivity() {
}
}
private fun exportNoteValueToUri(uri: Uri, title: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
private fun exportNoteValueToUri(
uri: Uri,
title: String,
content: String,
showSuccessToasts: Boolean,
callback: ((success: Boolean) -> Unit)? = null,
) {
try {
val outputStream = contentResolver.openOutputStream(uri, "rwt")
outputStream!!.bufferedWriter().use { out ->
@@ -1012,7 +1199,8 @@ class MainActivity : SimpleActivity() {
try {
val webView = WebView(this)
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest) = false
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest) =
false
override fun onPageFinished(view: WebView, url: String) {
createWebPrintJob(view)
@@ -1040,7 +1228,8 @@ class MainActivity : SimpleActivity() {
private fun getPagerAdapter() = binding.viewPager.adapter as NotesPagerAdapter
private fun getCurrentNoteText() = getPagerAdapter().getCurrentNoteViewText(binding.viewPager.currentItem)
private fun getCurrentNoteText() =
getPagerAdapter().getCurrentNoteViewText(binding.viewPager.currentItem)
private fun getCurrentNoteValue(): String {
return if (mCurrentNote.type == NoteType.TYPE_TEXT) {
@@ -1062,12 +1251,14 @@ class MainActivity : SimpleActivity() {
}
}
private fun addTextToCurrentNote(text: String) = getPagerAdapter().appendText(binding.viewPager.currentItem, text)
private fun addTextToCurrentNote(text: String) =
getPagerAdapter().appendText(binding.viewPager.currentItem, text)
private fun saveCurrentNote(force: Boolean, callback: ((note: Note) -> Unit)? = null) {
getPagerAdapter().saveCurrentNote(binding.viewPager.currentItem, force, callback)
if (mCurrentNote.type == NoteType.TYPE_CHECKLIST) {
mCurrentNote.value = getPagerAdapter().getNoteChecklistItems(binding.viewPager.currentItem) ?: ""
mCurrentNote.value =
getPagerAdapter().getNoteChecklistItems(binding.viewPager.currentItem) ?: ""
}
}
@@ -1100,7 +1291,8 @@ class MainActivity : SimpleActivity() {
private fun doDeleteNote(note: Note, deleteFile: Boolean) {
ensureBackgroundThread {
val currentNoteIndex = mNotes.indexOf(note)
val noteToRefresh = mNotes[if (currentNoteIndex > 0) currentNoteIndex - 1 else currentNoteIndex + 1]
val noteToRefresh =
mNotes[if (currentNoteIndex > 0) currentNoteIndex - 1 else currentNoteIndex + 1]
notesDB.deleteNote(note)
widgetsDB.deleteNoteWidgets(note.id!!)
@@ -1170,7 +1362,8 @@ class MainActivity : SimpleActivity() {
}
private fun shareText() {
val text = if (mCurrentNote.type == NoteType.TYPE_TEXT) getCurrentNoteText() else mCurrentNote.value
val text =
if (mCurrentNote.type == NoteType.TYPE_TEXT) getCurrentNoteText() else mCurrentNote.value
if (text.isNullOrEmpty()) {
toast(R.string.cannot_share_empty_text)
return
@@ -1192,14 +1385,16 @@ class MainActivity : SimpleActivity() {
val manager = getSystemService(ShortcutManager::class.java)
if (manager.isRequestPinShortcutSupported) {
val note = mCurrentNote
val drawable = resources.getDrawable(R.drawable.shortcut_note).mutate()
val drawable = AppCompatResources.getDrawable(this, R.drawable.shortcut_note)?.mutate()
val appIconColor = baseConfig.appIconColor
(drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_plus_background).applyColorFilter(appIconColor)
(drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_plus_background)
.applyColorFilter(appIconColor)
val intent = Intent(this, SplashActivity::class.java)
intent.action = Intent.ACTION_VIEW
intent.putExtra(OPEN_NOTE_ID, note.id)
intent.flags = intent.flags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
intent.flags =
intent.flags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
val shortcut = ShortcutInfo.Builder(this, note.hashCode().toString())
.setShortLabel(mCurrentNote.title)
@@ -1212,7 +1407,13 @@ class MainActivity : SimpleActivity() {
}
private fun lockNote() {
ConfirmationDialog(this, "", R.string.locking_warning, org.fossify.commons.R.string.ok, org.fossify.commons.R.string.cancel) {
ConfirmationDialog(
this,
"",
R.string.locking_warning,
org.fossify.commons.R.string.ok,
org.fossify.commons.R.string.cancel
) {
SecurityDialog(this, "", SHOW_ALL_TABS) { hash, type, success ->
if (success) {
mCurrentNote.protectionHash = hash

View File

@@ -15,16 +15,58 @@ import org.fossify.commons.dialogs.ConfirmationDialog
import org.fossify.commons.dialogs.PermissionRequiredDialog
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.extensions.beGone
import org.fossify.commons.extensions.beVisible
import org.fossify.commons.extensions.beVisibleIf
import org.fossify.commons.extensions.getProperPrimaryColor
import org.fossify.commons.extensions.openRequestExactAlarmSettings
import org.fossify.commons.extensions.showErrorToast
import org.fossify.commons.extensions.toast
import org.fossify.commons.extensions.updateTextColors
import org.fossify.commons.extensions.viewBinding
import org.fossify.commons.helpers.IS_CUSTOMIZING_COLORS
import org.fossify.commons.helpers.NavigationIcon
import org.fossify.commons.helpers.PROTECTION_FINGERPRINT
import org.fossify.commons.helpers.SHOW_ALL_TABS
import org.fossify.commons.helpers.ensureBackgroundThread
import org.fossify.commons.helpers.isOreoPlus
import org.fossify.commons.helpers.isQPlus
import org.fossify.commons.helpers.isRPlus
import org.fossify.commons.helpers.isSPlus
import org.fossify.commons.helpers.isTiramisuPlus
import org.fossify.commons.models.RadioItem
import org.fossify.notes.BuildConfig
import org.fossify.notes.R
import org.fossify.notes.databinding.ActivitySettingsBinding
import org.fossify.notes.dialogs.ExportNotesDialog
import org.fossify.notes.dialogs.ManageAutoBackupsDialog
import org.fossify.notes.extensions.*
import org.fossify.notes.helpers.*
import org.fossify.notes.extensions.cancelScheduledAutomaticBackup
import org.fossify.notes.extensions.config
import org.fossify.notes.extensions.requestUnlockNotes
import org.fossify.notes.extensions.scheduleNextAutomaticBackup
import org.fossify.notes.extensions.updateWidgets
import org.fossify.notes.extensions.widgetsDB
import org.fossify.notes.helpers.CUSTOMIZED_WIDGET_BG_COLOR
import org.fossify.notes.helpers.CUSTOMIZED_WIDGET_ID
import org.fossify.notes.helpers.CUSTOMIZED_WIDGET_KEY_ID
import org.fossify.notes.helpers.CUSTOMIZED_WIDGET_NOTE_ID
import org.fossify.notes.helpers.CUSTOMIZED_WIDGET_SHOW_TITLE
import org.fossify.notes.helpers.CUSTOMIZED_WIDGET_TEXT_COLOR
import org.fossify.notes.helpers.FONT_SIZE_100_PERCENT
import org.fossify.notes.helpers.FONT_SIZE_125_PERCENT
import org.fossify.notes.helpers.FONT_SIZE_150_PERCENT
import org.fossify.notes.helpers.FONT_SIZE_175_PERCENT
import org.fossify.notes.helpers.FONT_SIZE_200_PERCENT
import org.fossify.notes.helpers.FONT_SIZE_250_PERCENT
import org.fossify.notes.helpers.FONT_SIZE_300_PERCENT
import org.fossify.notes.helpers.FONT_SIZE_50_PERCENT
import org.fossify.notes.helpers.FONT_SIZE_60_PERCENT
import org.fossify.notes.helpers.FONT_SIZE_75_PERCENT
import org.fossify.notes.helpers.FONT_SIZE_90_PERCENT
import org.fossify.notes.helpers.GRAVITY_CENTER
import org.fossify.notes.helpers.GRAVITY_END
import org.fossify.notes.helpers.GRAVITY_START
import org.fossify.notes.helpers.NotesHelper
import org.fossify.notes.models.Note
import org.fossify.notes.models.Widget
import java.util.Locale
@@ -47,7 +89,12 @@ class SettingsActivity : SimpleActivity() {
super.onCreate(savedInstanceState)
setContentView(binding.root)
updateMaterialActivityViews(binding.settingsCoordinator, binding.settingsHolder, useTransparentNavigation = true, useTopSearchMenu = false)
updateMaterialActivityViews(
mainCoordinatorLayout = binding.settingsCoordinator,
nestedView = binding.settingsHolder,
useTransparentNavigation = true,
useTopSearchMenu = false
)
setupMaterialScrollListener(binding.settingsNestedScrollview, binding.settingsToolbar)
}
@@ -105,18 +152,19 @@ class SettingsActivity : SimpleActivity() {
}
}
private val saveDocument = registerForActivityResult(ActivityResultContracts.CreateDocument(notesFileType)) { uri ->
if (uri != null) {
toast(org.fossify.commons.R.string.exporting)
NotesHelper(this).getNotes { notes ->
requestUnlockNotes(notes) { unlockedNotes ->
val notLockedNotes = notes.filterNot { it.isLocked() }
val notesToExport = unlockedNotes + notLockedNotes
exportNotes(notesToExport, uri)
private val saveDocument =
registerForActivityResult(ActivityResultContracts.CreateDocument(notesFileType)) { uri ->
if (uri != null) {
toast(org.fossify.commons.R.string.exporting)
NotesHelper(this).getNotes { notes ->
requestUnlockNotes(notes) { unlockedNotes ->
val notLockedNotes = notes.filterNot { it.isLocked() }
val notesToExport = unlockedNotes + notLockedNotes
exportNotes(notesToExport, uri)
}
}
}
}
}
private fun setupCustomizeColors() {
binding.settingsColorCustomizationHolder.setOnClickListener {
@@ -136,9 +184,13 @@ class SettingsActivity : SimpleActivity() {
private fun setupLanguage() {
binding.settingsLanguage.text = Locale.getDefault().displayLanguage
binding.settingsLanguageHolder.beVisibleIf(isTiramisuPlus())
binding.settingsLanguageHolder.setOnClickListener {
launchChangeAppLanguageIntent()
if (isTiramisuPlus()) {
binding.settingsLanguageHolder.beVisible()
binding.settingsLanguageHolder.setOnClickListener {
launchChangeAppLanguageIntent()
}
} else {
binding.settingsLanguageHolder.beGone()
}
}
@@ -241,7 +293,9 @@ class SettingsActivity : SimpleActivity() {
private fun setupGravity() {
binding.settingsGravity.text = getGravityText()
binding.settingsGravityHolder.setOnClickListener {
val items = listOf(GRAVITY_START, GRAVITY_CENTER, GRAVITY_END).map { RadioItem(it, getGravityOptionLabel(it)) }
val items = listOf(GRAVITY_START, GRAVITY_CENTER, GRAVITY_END).map {
RadioItem(it, getGravityOptionLabel(it))
}
RadioGroupDialog(this@SettingsActivity, ArrayList(items), config.gravity) {
config.gravity = it as Int
binding.settingsGravity.text = getGravityText()
@@ -251,13 +305,15 @@ class SettingsActivity : SimpleActivity() {
}
private fun getGravityOptionLabel(gravity: Int): String {
val leftToRightDirection = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_LTR
val leftToRightDirection = TextUtilsCompat
.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_LTR
val leftRightLabels = listOf(R.string.left, R.string.right)
val startEndLabels = if (leftToRightDirection) {
leftRightLabels
} else {
leftRightLabels.reversed()
}
return getString(
when (gravity) {
GRAVITY_START -> startEndLabels.first()
@@ -436,8 +492,17 @@ class SettingsActivity : SimpleActivity() {
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 ->
val tabToShow = if (config.isAppPasswordProtectionOn) {
config.appProtectionType
} else {
SHOW_ALL_TABS
}
SecurityDialog(
activity = this,
requiredHash = config.appPasswordHash,
showTabIndex = tabToShow
) { hash, type, success ->
if (success) {
val hasPasswordProtection = config.isAppPasswordProtectionOn
binding.settingsAppPasswordProtection.isChecked = !hasPasswordProtection
@@ -446,9 +511,20 @@ class SettingsActivity : SimpleActivity() {
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) { }
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(
activity = this,
message = "",
messageId = confirmationTextId,
positive = org.fossify.commons.R.string.ok,
negative = 0
) { }
}
}
}
@@ -456,21 +532,44 @@ class SettingsActivity : SimpleActivity() {
}
private fun setupNoteDeletionPasswordProtection() {
binding.settingsNoteDeletionPasswordProtection.isChecked = config.isDeletePasswordProtectionOn
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 ->
val tabToShow = if (config.isDeletePasswordProtectionOn) {
config.deleteProtectionType
} else {
SHOW_ALL_TABS
}
SecurityDialog(
activity = this,
requiredHash = config.deletePasswordHash,
showTabIndex = tabToShow
) { hash, type, success ->
if (success) {
val hasPasswordProtection = config.isDeletePasswordProtectionOn
binding.settingsNoteDeletionPasswordProtection.isChecked = !hasPasswordProtection
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) { }
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(
activity = this,
message = "",
messageId = confirmationTextId,
positive = org.fossify.commons.R.string.ok,
negative = 0
) { }
}
}
}