Skip to content

Commit b71768a

Browse files
committed
create a new background thread only when needed
1 parent ec7621b commit b71768a

File tree

8 files changed

+34
-30
lines changed

8 files changed

+34
-30
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
1414
import com.simplemobiletools.commons.dialogs.FilePickerDialog
1515
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
1616
import com.simplemobiletools.commons.extensions.*
17-
import com.simplemobiletools.commons.helpers.LICENSE_RTL
18-
import com.simplemobiletools.commons.helpers.PERMISSION_READ_STORAGE
19-
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
20-
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
17+
import com.simplemobiletools.commons.helpers.*
2118
import com.simplemobiletools.commons.models.FAQItem
2219
import com.simplemobiletools.commons.models.FileDirItem
2320
import com.simplemobiletools.commons.models.RadioItem
@@ -360,7 +357,7 @@ class MainActivity : SimpleActivity() {
360357
private fun openFile() {
361358
FilePickerDialog(this, canAddShowHiddenButton = true) {
362359
openFile(it, true) {
363-
Thread {
360+
ensureBackgroundThread {
364361
val fileText = it.readText().trim()
365362
val checklistItems = fileText.parseChecklistItems()
366363
if (checklistItems != null) {
@@ -373,7 +370,7 @@ class MainActivity : SimpleActivity() {
373370
}
374371
}
375372
}
376-
}.start()
373+
}
377374
}
378375
}
379376
}
@@ -644,11 +641,11 @@ class MainActivity : SimpleActivity() {
644641
}
645642

646643
private fun doDeleteNote(note: Note, deleteFile: Boolean) {
647-
Thread {
644+
ensureBackgroundThread {
648645
notesDB.deleteNote(note)
649646
widgetsDB.deleteNoteWidgets(note.id!!)
650647
refreshNotes(note, deleteFile)
651-
}.start()
648+
}
652649
}
653650

654651
private fun refreshNotes(note: Note, deleteFile: Boolean) {

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/WidgetConfigureActivity.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.simplemobiletools.commons.dialogs.ColorPickerDialog
1515
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
1616
import com.simplemobiletools.commons.extensions.*
1717
import com.simplemobiletools.commons.helpers.IS_CUSTOMIZING_COLORS
18+
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
1819
import com.simplemobiletools.commons.models.RadioItem
1920
import com.simplemobiletools.notes.pro.R
2021
import com.simplemobiletools.notes.pro.adapters.ChecklistAdapter
@@ -150,9 +151,9 @@ class WidgetConfigureActivity : SimpleActivity() {
150151
views.setBackgroundColor(R.id.checklist_note_view, mBgColor)
151152
AppWidgetManager.getInstance(this).updateAppWidget(mWidgetId, views)
152153
val widget = Widget(null, mWidgetId, mCurrentNoteId)
153-
Thread {
154+
ensureBackgroundThread {
154155
widgetsDB.insertOrUpdate(widget)
155-
}.start()
156+
}
156157

157158
storeWidgetBackground()
158159
requestWidgetUpdate()

app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.simplemobiletools.commons.extensions.getFilenameFromPath
66
import com.simplemobiletools.commons.extensions.humanizePath
77
import com.simplemobiletools.commons.extensions.isMediaFile
88
import com.simplemobiletools.commons.extensions.setupDialogStuff
9+
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
910
import com.simplemobiletools.notes.pro.R
1011
import com.simplemobiletools.notes.pro.activities.SimpleActivity
1112
import com.simplemobiletools.notes.pro.extensions.notesDB
@@ -32,9 +33,9 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal
3233
activity.setupDialogStuff(view, this, R.string.import_folder) {
3334
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
3435
val updateFilesOnEdit = view.open_file_type.checkedRadioButtonId == R.id.open_file_update_file
35-
Thread {
36+
ensureBackgroundThread {
3637
saveFolder(updateFilesOnEdit)
37-
}.start()
38+
}
3839
}
3940
}
4041
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff
77
import com.simplemobiletools.commons.extensions.showKeyboard
88
import com.simplemobiletools.commons.extensions.toast
99
import com.simplemobiletools.commons.extensions.value
10+
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
1011
import com.simplemobiletools.notes.pro.R
1112
import com.simplemobiletools.notes.pro.extensions.config
1213
import com.simplemobiletools.notes.pro.extensions.notesDB
@@ -29,7 +30,7 @@ class NewNoteDialog(val activity: Activity, callback: (note: Note) -> Unit) {
2930
showKeyboard(view.note_title)
3031
getButton(BUTTON_POSITIVE).setOnClickListener {
3132
val title = view.note_title.value
32-
Thread {
33+
ensureBackgroundThread {
3334
when {
3435
title.isEmpty() -> activity.toast(R.string.no_title)
3536
activity.notesDB.getNoteIdWithTitle(title) != null -> activity.toast(R.string.title_taken)
@@ -41,7 +42,7 @@ class NewNoteDialog(val activity: Activity, callback: (note: Note) -> Unit) {
4142
dismiss()
4243
}
4344
}
44-
}.start()
45+
}
4546
}
4647
}
4748
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/RenameNoteDialog.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.simplemobiletools.notes.pro.dialogs
33
import android.content.DialogInterface.BUTTON_POSITIVE
44
import androidx.appcompat.app.AlertDialog
55
import com.simplemobiletools.commons.extensions.*
6+
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
67
import com.simplemobiletools.notes.pro.R
78
import com.simplemobiletools.notes.pro.activities.SimpleActivity
89
import com.simplemobiletools.notes.pro.extensions.config
@@ -26,9 +27,9 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, val current
2627
showKeyboard(view.note_title)
2728
getButton(BUTTON_POSITIVE).setOnClickListener {
2829
val title = view.note_title.value
29-
Thread {
30+
ensureBackgroundThread {
3031
newTitleConfirmed(title, this)
31-
}.start()
32+
}
3233
}
3334
}
3435
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/ChecklistFragment.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.view.ViewGroup
88
import com.google.gson.Gson
99
import com.google.gson.reflect.TypeToken
1010
import com.simplemobiletools.commons.extensions.*
11+
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
1112
import com.simplemobiletools.notes.pro.R
1213
import com.simplemobiletools.notes.pro.activities.SimpleActivity
1314
import com.simplemobiletools.notes.pro.adapters.ChecklistAdapter
@@ -113,7 +114,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
113114
}
114115

115116
private fun saveNote(refreshIndex: Int = -1) {
116-
Thread {
117+
ensureBackgroundThread {
117118
if (note != null && context != null) {
118119
if (refreshIndex != -1) {
119120
view.checklist_list.post {
@@ -125,7 +126,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
125126
context?.notesDB?.insertOrUpdate(note!!)
126127
context?.updateWidgets()
127128
}
128-
}.start()
129+
}
129130
}
130131

131132
override fun saveChecklist() {

app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/MyWidgetProvider.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.net.Uri
99
import android.widget.RemoteViews
1010
import com.simplemobiletools.commons.extensions.getLaunchIntent
1111
import com.simplemobiletools.commons.extensions.setBackgroundColor
12+
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
1213
import com.simplemobiletools.notes.pro.R
1314
import com.simplemobiletools.notes.pro.activities.SplashActivity
1415
import com.simplemobiletools.notes.pro.extensions.config
@@ -26,7 +27,7 @@ class MyWidgetProvider : AppWidgetProvider() {
2627

2728
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
2829
super.onUpdate(context, appWidgetManager, appWidgetIds)
29-
Thread {
30+
ensureBackgroundThread {
3031
context.widgetsDB.getWidgets().forEach {
3132
val views = RemoteViews(context.packageName, R.layout.widget)
3233
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
@@ -46,15 +47,15 @@ class MyWidgetProvider : AppWidgetProvider() {
4647
appWidgetManager.updateAppWidget(it.widgetId, views)
4748
appWidgetManager.notifyAppWidgetViewDataChanged(it.widgetId, R.id.notes_widget_listview)
4849
}
49-
}.start()
50+
}
5051
}
5152

5253
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
5354
super.onDeleted(context, appWidgetIds)
54-
Thread {
55+
ensureBackgroundThread {
5556
appWidgetIds.forEach {
5657
context.widgetsDB.deleteWidgetId(it)
5758
}
58-
}.start()
59+
}
5960
}
6061
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.simplemobiletools.notes.pro.helpers
33
import android.content.Context
44
import android.os.Handler
55
import android.os.Looper
6+
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
67
import com.simplemobiletools.notes.pro.R
78
import com.simplemobiletools.notes.pro.extensions.config
89
import com.simplemobiletools.notes.pro.extensions.notesDB
@@ -11,7 +12,7 @@ import java.io.File
1112

1213
class NotesHelper(val context: Context) {
1314
fun getNotes(callback: (notes: ArrayList<Note>) -> Unit) {
14-
Thread {
15+
ensureBackgroundThread {
1516
// make sure the initial note has enough time to be precreated
1617
if (context.config.appRunCount <= 1) {
1718
context.notesDB.getNotes()
@@ -39,33 +40,33 @@ class NotesHelper(val context: Context) {
3940
Handler(Looper.getMainLooper()).post {
4041
callback(notes)
4142
}
42-
}.start()
43+
}
4344
}
4445

4546
fun getNoteWithId(id: Long, callback: (note: Note?) -> Unit) {
46-
Thread {
47+
ensureBackgroundThread {
4748
val note = context.notesDB.getNoteWithId(id)
4849
Handler(Looper.getMainLooper()).post {
4950
callback(note)
5051
}
51-
}.start()
52+
}
5253
}
5354

5455
fun getNoteIdWithPath(path: String, callback: (id: Long?) -> Unit) {
55-
Thread {
56+
ensureBackgroundThread {
5657
val id = context.notesDB.getNoteIdWithPath(path)
5758
Handler(Looper.getMainLooper()).post {
5859
callback(id)
5960
}
60-
}.start()
61+
}
6162
}
6263

6364
fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) {
64-
Thread {
65+
ensureBackgroundThread {
6566
val noteId = context.notesDB.insertOrUpdate(note)
6667
Handler(Looper.getMainLooper()).post {
6768
callback?.invoke(noteId)
6869
}
69-
}.start()
70+
}
7071
}
7172
}

0 commit comments

Comments
 (0)