@@ -183,7 +183,7 @@ class MainActivity : SimpleActivity() {
183
183
findItem(R .id.unlock_note).isVisible = mNotes.isNotEmpty() && (::mCurrentNote.isInitialized && mCurrentNote.isLocked())
184
184
185
185
saveNoteButton = findItem(R .id.save_note)
186
- saveNoteButton!! .isVisible =
186
+ saveNoteButton? .isVisible =
187
187
! config.autosaveNotes && showSaveButton && (::mCurrentNote.isInitialized && mCurrentNote.type == NoteType .TYPE_TEXT .value)
188
188
}
189
189
@@ -270,17 +270,24 @@ class MainActivity : SimpleActivity() {
270
270
271
271
override fun onActivityResult (requestCode : Int , resultCode : Int , resultData : Intent ? ) {
272
272
super .onActivityResult(requestCode, resultCode, resultData)
273
- if (requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK && resultData != null && resultData.data != null ) {
274
- importUri(resultData.data!! )
275
- } else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity .RESULT_OK && resultData != null && resultData.data != null && mNotes.isNotEmpty()) {
276
- val takeFlags = Intent .FLAG_GRANT_READ_URI_PERMISSION or Intent .FLAG_GRANT_WRITE_URI_PERMISSION
277
- applicationContext.contentResolver.takePersistableUriPermission(resultData.data!! , takeFlags)
278
- showExportFilePickUpdateDialog(resultData.dataString!! , getCurrentNoteValue())
279
- } else if (requestCode == PICK_EXPORT_NOTES_INTENT && resultCode == Activity .RESULT_OK && resultData != null && resultData.data != null ) {
280
- val outputStream = contentResolver.openOutputStream(resultData.data!! )
281
- exportNotesTo(outputStream)
282
- } else if (requestCode == PICK_IMPORT_NOTES_INTENT && resultCode == Activity .RESULT_OK && resultData != null && resultData.data != null ) {
283
- importNotesFrom(resultData.data!! )
273
+ resultData?.data?.let {
274
+ when {
275
+ requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK -> {
276
+ importUri(it)
277
+ }
278
+ requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity .RESULT_OK && mNotes.isNotEmpty() -> {
279
+ val takeFlags = Intent .FLAG_GRANT_READ_URI_PERMISSION or Intent .FLAG_GRANT_WRITE_URI_PERMISSION
280
+ applicationContext.contentResolver.takePersistableUriPermission(it, takeFlags)
281
+ showExportFilePickUpdateDialog(resultData.dataString ? : " " , getCurrentNoteValue())
282
+ }
283
+ requestCode == PICK_EXPORT_NOTES_INTENT && resultCode == Activity .RESULT_OK -> {
284
+ val outputStream = contentResolver.openOutputStream(it)
285
+ exportNotesTo(outputStream)
286
+ }
287
+ requestCode == PICK_IMPORT_NOTES_INTENT && resultCode == Activity .RESULT_OK -> {
288
+ importNotesFrom(it)
289
+ }
290
+ }
284
291
}
285
292
}
286
293
@@ -352,17 +359,22 @@ class MainActivity : SimpleActivity() {
352
359
val realPath = intent.getStringExtra(REAL_FILE_PATH )
353
360
val isFromHistory = intent.flags and Intent .FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0
354
361
if (! isFromHistory) {
355
- if (realPath != null && hasPermission(PERMISSION_READ_STORAGE )) {
356
- val file = File (realPath)
357
- handleUri(Uri .fromFile(file))
358
- } else if (intent.getBooleanExtra(NEW_TEXT_NOTE , false )) {
359
- val newTextNote = Note (null , getCurrentFormattedDateTime(), " " , NoteType .TYPE_TEXT .value, " " , PROTECTION_NONE , " " )
360
- addNewNote(newTextNote)
361
- } else if (intent.getBooleanExtra(NEW_CHECKLIST , false )) {
362
- val newChecklist = Note (null , getCurrentFormattedDateTime(), " " , NoteType .TYPE_CHECKLIST .value, " " , PROTECTION_NONE , " " )
363
- addNewNote(newChecklist)
364
- } else {
365
- handleUri(data!! )
362
+ when {
363
+ realPath != null && hasPermission(PERMISSION_READ_STORAGE ) -> {
364
+ val file = File (realPath)
365
+ handleUri(Uri .fromFile(file))
366
+ }
367
+ intent.getBooleanExtra(NEW_TEXT_NOTE , false ) -> {
368
+ val newTextNote = Note (null , getCurrentFormattedDateTime(), " " , NoteType .TYPE_TEXT .value, " " , PROTECTION_NONE , " " )
369
+ addNewNote(newTextNote)
370
+ }
371
+ intent.getBooleanExtra(NEW_CHECKLIST , false ) -> {
372
+ val newChecklist = Note (null , getCurrentFormattedDateTime(), " " , NoteType .TYPE_CHECKLIST .value, " " , PROTECTION_NONE , " " )
373
+ addNewNote(newChecklist)
374
+ }
375
+ else -> {
376
+ handleUri(data!! )
377
+ }
366
378
}
367
379
}
368
380
intent.removeCategory(Intent .CATEGORY_DEFAULT )
@@ -393,15 +405,15 @@ class MainActivity : SimpleActivity() {
393
405
if (it as Int == 0 ) {
394
406
displayNewNoteDialog(text)
395
407
} else {
396
- updateSelectedNote(notes[it - 1 ].id!! )
408
+ updateSelectedNote(notes[it - 1 ].id ? : - 1 )
397
409
addTextToCurrentNote(if (mCurrentNote.value.isEmpty()) text else " \n $text " )
398
410
}
399
411
}
400
412
}
401
413
}
402
414
403
415
private fun handleUri (uri : Uri ) {
404
- NotesHelper (this ).getNoteIdWithPath(uri.path!! ) {
416
+ NotesHelper (this ).getNoteIdWithPath(uri.path ? : " " ) {
405
417
if (it != null && it > 0L ) {
406
418
updateSelectedNote(it)
407
419
return @getNoteIdWithPath
@@ -425,11 +437,11 @@ class MainActivity : SimpleActivity() {
425
437
view_pager.apply {
426
438
adapter = mAdapter
427
439
currentItem = getWantedNoteIndex(wantedNoteId)
428
- config.currentNoteId = mCurrentNote.id!!
440
+ config.currentNoteId = mCurrentNote.id ? : - 1
429
441
430
442
onPageChangeListener {
431
443
mCurrentNote = mNotes[it]
432
- config.currentNoteId = mCurrentNote.id!!
444
+ config.currentNoteId = mCurrentNote.id ? : - 1
433
445
refreshMenuItems()
434
446
}
435
447
}
@@ -461,7 +473,7 @@ class MainActivity : SimpleActivity() {
461
473
view_pager.onPageChangeListener {
462
474
currentTextFragment?.removeTextWatcher()
463
475
currentNotesView()?.let { noteView ->
464
- noteView.text!! .clearBackgroundSpans()
476
+ noteView.text? .clearBackgroundSpans()
465
477
}
466
478
467
479
closeSearch()
@@ -481,7 +493,7 @@ class MainActivity : SimpleActivity() {
481
493
private fun searchTextChanged (text : String ) {
482
494
currentNotesView()?.let { noteView ->
483
495
currentTextFragment?.removeTextWatcher()
484
- noteView.text!! .clearBackgroundSpans()
496
+ noteView.text? .clearBackgroundSpans()
485
497
486
498
if (text.isNotBlank() && text.length > 1 ) {
487
499
searchMatches = noteView.value.searchMatches(text)
@@ -720,7 +732,7 @@ class MainActivity : SimpleActivity() {
720
732
721
733
private fun importUri (uri : Uri ) {
722
734
when (uri.scheme) {
723
- " file" -> openPath(uri.path!! )
735
+ " file" -> openPath(uri.path ? : " " )
724
736
" content" -> {
725
737
val realPath = getRealPathFromURI(uri)
726
738
if (hasPermission(PERMISSION_READ_STORAGE )) {
@@ -750,8 +762,8 @@ class MainActivity : SimpleActivity() {
750
762
}
751
763
752
764
val inputStream = contentResolver.openInputStream(uri)
753
- val content = inputStream?.bufferedReader().use { it!! .readText() }
754
- val checklistItems = content.parseChecklistItems()
765
+ val content = inputStream?.bufferedReader().use { it? .readText() }
766
+ val checklistItems = content? .parseChecklistItems()
755
767
756
768
// if we got here by some other app invoking the file open intent, we have no permission for updating the original file itself
757
769
// we can do it only after using "Export as file" or "Open file" from our app
@@ -769,7 +781,7 @@ class MainActivity : SimpleActivity() {
769
781
770
782
val noteType = if (checklistItems != null ) NoteType .TYPE_CHECKLIST .value else NoteType .TYPE_TEXT .value
771
783
if (! canSyncNoteWithFile) {
772
- val note = Note (null , noteTitle, content, noteType, " " , PROTECTION_NONE , " " )
784
+ val note = Note (null , noteTitle, content ? : " " , noteType, " " , PROTECTION_NONE , " " )
773
785
displayNewNoteDialog(note.value, title = noteTitle, " " )
774
786
} else {
775
787
val items = arrayListOf (
@@ -780,7 +792,7 @@ class MainActivity : SimpleActivity() {
780
792
RadioGroupDialog (this , items) {
781
793
val syncFile = it as Int == IMPORT_FILE_SYNC
782
794
val path = if (syncFile) uri.toString() else " "
783
- val note = Note (null , noteTitle, content, noteType, " " , PROTECTION_NONE , " " )
795
+ val note = Note (null , noteTitle, content ? : " " , noteType, " " , PROTECTION_NONE , " " )
784
796
displayNewNoteDialog(note.value, title = noteTitle, path)
785
797
}
786
798
}
@@ -942,7 +954,7 @@ class MainActivity : SimpleActivity() {
942
954
943
955
private fun importNotesFrom (uri : Uri ) {
944
956
when (uri.scheme) {
945
- " file" -> importNotes(uri.path!! , uri.path!! .getFilenameFromPath())
957
+ " file" -> importNotes(uri.path ? : " " , uri.path? .getFilenameFromPath() ? : " " )
946
958
" content" -> {
947
959
val tempFile = getTempFile(" messages" , " backup.txt" )
948
960
if (tempFile == null ) {
@@ -954,7 +966,7 @@ class MainActivity : SimpleActivity() {
954
966
val filename = getFilenameFromUri(uri)
955
967
val inputStream = contentResolver.openInputStream(uri)
956
968
val out = FileOutputStream (tempFile)
957
- inputStream!! .copyTo(out )
969
+ inputStream? .copyTo(out )
958
970
importNotes(tempFile.absolutePath, filename)
959
971
} catch (e: Exception ) {
960
972
showErrorToast(e)
@@ -1077,14 +1089,16 @@ class MainActivity : SimpleActivity() {
1077
1089
getDocumentFile(path) ? : return @handleSAFDialog
1078
1090
} else {
1079
1091
val parent = getDocumentFile(File (path).parent) ? : return @handleSAFDialog
1080
- parent.createFile(" " , path.getFilenameFromPath())!!
1092
+ parent.createFile(" " , path.getFilenameFromPath())
1081
1093
}
1082
1094
1083
- contentResolver.openOutputStream(document.uri)!! .apply {
1084
- val byteArray = content.toByteArray(Charset .forName(" UTF-8" ))
1085
- write(byteArray, 0 , byteArray.size)
1086
- flush()
1087
- close()
1095
+ document?.uri?.let { it1 ->
1096
+ contentResolver.openOutputStream(it1)?.apply {
1097
+ val byteArray = content.toByteArray(Charset .forName(" UTF-8" ))
1098
+ write(byteArray, 0 , byteArray.size)
1099
+ flush()
1100
+ close()
1101
+ }
1088
1102
}
1089
1103
1090
1104
if (showSuccessToasts) {
@@ -1110,8 +1124,8 @@ class MainActivity : SimpleActivity() {
1110
1124
private fun exportNoteValueToUri (uri : Uri , title : String , content : String , showSuccessToasts : Boolean , callback : ((success: Boolean ) -> Unit )? = null) {
1111
1125
try {
1112
1126
val outputStream = contentResolver.openOutputStream(uri, " rwt" )
1113
- outputStream!! .bufferedWriter().use { out ->
1114
- out .write(content)
1127
+ outputStream? .bufferedWriter().use { out ->
1128
+ out ? .write(content)
1115
1129
}
1116
1130
if (showSuccessToasts) {
1117
1131
noteExportedSuccessfully(title)
@@ -1217,7 +1231,7 @@ class MainActivity : SimpleActivity() {
1217
1231
private fun doDeleteNote (note : Note , deleteFile : Boolean ) {
1218
1232
ensureBackgroundThread {
1219
1233
notesDB.deleteNote(note)
1220
- widgetsDB.deleteNoteWidgets(note.id!! )
1234
+ widgetsDB.deleteNoteWidgets(note.id ? : - 1 )
1221
1235
refreshNotes(note, deleteFile)
1222
1236
}
1223
1237
}
@@ -1226,9 +1240,9 @@ class MainActivity : SimpleActivity() {
1226
1240
NotesHelper (this ).getNotes {
1227
1241
mNotes = it
1228
1242
val firstNoteId = mNotes[0 ].id
1229
- updateSelectedNote(firstNoteId!! )
1243
+ updateSelectedNote(firstNoteId ? : - 1 )
1230
1244
if (config.widgetNoteId == note.id) {
1231
- config.widgetNoteId = mCurrentNote.id!!
1245
+ config.widgetNoteId = mCurrentNote.id ? : - 1
1232
1246
updateWidgets()
1233
1247
}
1234
1248
0 commit comments