create a new background thread only when needed

This commit is contained in:
tibbi 2019-08-19 22:38:59 +02:00
parent ec7621b988
commit b71768a0dc
8 changed files with 34 additions and 30 deletions

View File

@ -14,10 +14,7 @@ import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.LICENSE_RTL
import com.simplemobiletools.commons.helpers.PERMISSION_READ_STORAGE
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.models.RadioItem
@ -360,7 +357,7 @@ class MainActivity : SimpleActivity() {
private fun openFile() {
FilePickerDialog(this, canAddShowHiddenButton = true) {
openFile(it, true) {
Thread {
ensureBackgroundThread {
val fileText = it.readText().trim()
val checklistItems = fileText.parseChecklistItems()
if (checklistItems != null) {
@ -373,7 +370,7 @@ class MainActivity : SimpleActivity() {
}
}
}
}.start()
}
}
}
}
@ -644,11 +641,11 @@ class MainActivity : SimpleActivity() {
}
private fun doDeleteNote(note: Note, deleteFile: Boolean) {
Thread {
ensureBackgroundThread {
notesDB.deleteNote(note)
widgetsDB.deleteNoteWidgets(note.id!!)
refreshNotes(note, deleteFile)
}.start()
}
}
private fun refreshNotes(note: Note, deleteFile: Boolean) {

View File

@ -15,6 +15,7 @@ import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.IS_CUSTOMIZING_COLORS
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.adapters.ChecklistAdapter
@ -150,9 +151,9 @@ class WidgetConfigureActivity : SimpleActivity() {
views.setBackgroundColor(R.id.checklist_note_view, mBgColor)
AppWidgetManager.getInstance(this).updateAppWidget(mWidgetId, views)
val widget = Widget(null, mWidgetId, mCurrentNoteId)
Thread {
ensureBackgroundThread {
widgetsDB.insertOrUpdate(widget)
}.start()
}
storeWidgetBackground()
requestWidgetUpdate()

View File

@ -6,6 +6,7 @@ import com.simplemobiletools.commons.extensions.getFilenameFromPath
import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.commons.extensions.isMediaFile
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.activities.SimpleActivity
import com.simplemobiletools.notes.pro.extensions.notesDB
@ -32,9 +33,9 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal
activity.setupDialogStuff(view, this, R.string.import_folder) {
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val updateFilesOnEdit = view.open_file_type.checkedRadioButtonId == R.id.open_file_update_file
Thread {
ensureBackgroundThread {
saveFolder(updateFilesOnEdit)
}.start()
}
}
}
}

View File

@ -7,6 +7,7 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.showKeyboard
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.notesDB
@ -29,7 +30,7 @@ class NewNoteDialog(val activity: Activity, callback: (note: Note) -> Unit) {
showKeyboard(view.note_title)
getButton(BUTTON_POSITIVE).setOnClickListener {
val title = view.note_title.value
Thread {
ensureBackgroundThread {
when {
title.isEmpty() -> activity.toast(R.string.no_title)
activity.notesDB.getNoteIdWithTitle(title) != null -> activity.toast(R.string.title_taken)
@ -41,7 +42,7 @@ class NewNoteDialog(val activity: Activity, callback: (note: Note) -> Unit) {
dismiss()
}
}
}.start()
}
}
}
}

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.notes.pro.dialogs
import android.content.DialogInterface.BUTTON_POSITIVE
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.activities.SimpleActivity
import com.simplemobiletools.notes.pro.extensions.config
@ -26,9 +27,9 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, val current
showKeyboard(view.note_title)
getButton(BUTTON_POSITIVE).setOnClickListener {
val title = view.note_title.value
Thread {
ensureBackgroundThread {
newTitleConfirmed(title, this)
}.start()
}
}
}
}

View File

@ -8,6 +8,7 @@ import android.view.ViewGroup
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.activities.SimpleActivity
import com.simplemobiletools.notes.pro.adapters.ChecklistAdapter
@ -113,7 +114,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
}
private fun saveNote(refreshIndex: Int = -1) {
Thread {
ensureBackgroundThread {
if (note != null && context != null) {
if (refreshIndex != -1) {
view.checklist_list.post {
@ -125,7 +126,7 @@ class ChecklistFragment : NoteFragment(), ChecklistItemsListener {
context?.notesDB?.insertOrUpdate(note!!)
context?.updateWidgets()
}
}.start()
}
}
override fun saveChecklist() {

View File

@ -9,6 +9,7 @@ import android.net.Uri
import android.widget.RemoteViews
import com.simplemobiletools.commons.extensions.getLaunchIntent
import com.simplemobiletools.commons.extensions.setBackgroundColor
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.activities.SplashActivity
import com.simplemobiletools.notes.pro.extensions.config
@ -26,7 +27,7 @@ class MyWidgetProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
Thread {
ensureBackgroundThread {
context.widgetsDB.getWidgets().forEach {
val views = RemoteViews(context.packageName, R.layout.widget)
views.setBackgroundColor(R.id.notes_widget_holder, context.config.widgetBgColor)
@ -46,15 +47,15 @@ class MyWidgetProvider : AppWidgetProvider() {
appWidgetManager.updateAppWidget(it.widgetId, views)
appWidgetManager.notifyAppWidgetViewDataChanged(it.widgetId, R.id.notes_widget_listview)
}
}.start()
}
}
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
super.onDeleted(context, appWidgetIds)
Thread {
ensureBackgroundThread {
appWidgetIds.forEach {
context.widgetsDB.deleteWidgetId(it)
}
}.start()
}
}
}

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.notes.pro.helpers
import android.content.Context
import android.os.Handler
import android.os.Looper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.notesDB
@ -11,7 +12,7 @@ import java.io.File
class NotesHelper(val context: Context) {
fun getNotes(callback: (notes: ArrayList<Note>) -> Unit) {
Thread {
ensureBackgroundThread {
// make sure the initial note has enough time to be precreated
if (context.config.appRunCount <= 1) {
context.notesDB.getNotes()
@ -39,33 +40,33 @@ class NotesHelper(val context: Context) {
Handler(Looper.getMainLooper()).post {
callback(notes)
}
}.start()
}
}
fun getNoteWithId(id: Long, callback: (note: Note?) -> Unit) {
Thread {
ensureBackgroundThread {
val note = context.notesDB.getNoteWithId(id)
Handler(Looper.getMainLooper()).post {
callback(note)
}
}.start()
}
}
fun getNoteIdWithPath(path: String, callback: (id: Long?) -> Unit) {
Thread {
ensureBackgroundThread {
val id = context.notesDB.getNoteIdWithPath(path)
Handler(Looper.getMainLooper()).post {
callback(id)
}
}.start()
}
}
fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) {
Thread {
ensureBackgroundThread {
val noteId = context.notesDB.insertOrUpdate(note)
Handler(Looper.getMainLooper()).post {
callback?.invoke(noteId)
}
}.start()
}
}
}