diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index 4509f6291..a15438cc9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -98,18 +98,18 @@ class EventActivity : SimpleActivity() { (mAttendeePlaceholder as LayerDrawable).findDrawableByLayerId(R.id.attendee_circular_background).applyColorFilter(config.primaryColor) val eventId = intent.getLongExtra(EVENT_ID, 0L) - Thread { + ensureBackgroundThread { val event = eventsDB.getEventWithId(eventId) if (eventId != 0L && event == null) { finish() - return@Thread + return@ensureBackgroundThread } val localEventType = eventTypesDB.getEventTypeWithId(config.lastUsedLocalEventTypeId) runOnUiThread { gotEvent(savedInstanceState, localEventType, event) } - }.start() + } } private fun gotEvent(savedInstanceState: Bundle?, localEventType: EventType?, event: Event?) { @@ -377,12 +377,12 @@ class EventActivity : SimpleActivity() { } private fun checkAttendees() { - Thread { + ensureBackgroundThread { fillAvailableContacts() runOnUiThread { updateAttendees() } - }.start() + } } private fun handleNotificationAvailability(callback: () -> Unit) { @@ -716,7 +716,7 @@ class EventActivity : SimpleActivity() { } private fun updateEventType() { - Thread { + ensureBackgroundThread { val eventType = eventTypesDB.getEventTypeWithId(mEventTypeId) if (eventType != null) { runOnUiThread { @@ -724,7 +724,7 @@ class EventActivity : SimpleActivity() { event_type_color.setFillWithStroke(eventType.color, config.backgroundColor) } } - }.start() + } } private fun updateCalDAVCalendar() { @@ -783,7 +783,7 @@ class EventActivity : SimpleActivity() { } else { event_caldav_calendar_email.text = currentCalendar.accountName - Thread { + ensureBackgroundThread { val calendarColor = eventsHelper.getEventTypeWithCalDAVCalendarId(currentCalendar.id)?.color ?: currentCalendar.color @@ -798,7 +798,7 @@ class EventActivity : SimpleActivity() { setPadding(paddingLeft, 0, paddingRight, 0) } } - }.start() + } } } @@ -826,7 +826,7 @@ class EventActivity : SimpleActivity() { private fun deleteEvent() { DeleteEventDialog(this, arrayListOf(mEvent.id!!), mEvent.repeatInterval > 0) { - Thread { + ensureBackgroundThread { when (it) { DELETE_SELECTED_OCCURRENCE -> eventsHelper.addEventRepetitionException(mEvent.id!!, mEventOccurrenceTS, true) DELETE_FUTURE_OCCURRENCES -> eventsHelper.addEventRepeatLimit(mEvent.id!!, mEventOccurrenceTS) @@ -835,7 +835,7 @@ class EventActivity : SimpleActivity() { runOnUiThread { finish() } - }.start() + } } } @@ -851,9 +851,9 @@ class EventActivity : SimpleActivity() { } private fun saveCurrentEvent() { - Thread { + ensureBackgroundThread { saveEvent() - }.start() + } } private fun saveEvent() { @@ -982,13 +982,13 @@ class EventActivity : SimpleActivity() { private fun showEditRepeatingEventDialog() { EditRepeatingEventDialog(this) { if (it) { - Thread { + ensureBackgroundThread { eventsHelper.updateEvent(mEvent, true, true) { finish() } - }.start() + } } else { - Thread { + ensureBackgroundThread { eventsHelper.addEventRepetitionException(mEvent.id!!, mEventOccurrenceTS, true) mEvent.apply { parentId = id!!.toLong() @@ -1001,7 +1001,7 @@ class EventActivity : SimpleActivity() { eventsHelper.insertEvent(mEvent, true, true) { finish() } - }.start() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt index 710f41ada..87b7979e0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt @@ -350,7 +350,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { val uri = intent.data if (uri.authority == "com.android.calendar") { if (uri.path.startsWith("/events")) { - Thread { + ensureBackgroundThread { // intents like content://com.android.calendar/events/1756 val eventId = uri.lastPathSegment val id = eventsDB.getEventIdWithLastImportId("%-$eventId") @@ -362,7 +362,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { } else { toast(R.string.unknown_error_occurred) } - }.start() + } } else if (intent?.extras?.getBoolean("DETAIL_VIEW", false) == true) { // clicking date on a third party widget: content://com.android.calendar/time/1507309245683 val timestamp = uri.pathSegments.last() @@ -449,7 +449,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { val items = getHolidayRadioItems() RadioGroupDialog(this, items) { toast(R.string.importing) - Thread { + ensureBackgroundThread { val holidays = getString(R.string.holidays) var eventTypeId = eventsHelper.getEventTypeIdWithTitle(holidays) if (eventTypeId == -1L) { @@ -464,7 +464,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { updateViewPager() } } - }.start() + } } } @@ -473,7 +473,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { if (it) { SetRemindersDialog(this) { val reminders = it - Thread { + ensureBackgroundThread { addContactEvents(true, reminders) { if (it > 0) { toast(R.string.birthdays_added) @@ -482,7 +482,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { toast(R.string.no_birthdays) } } - }.start() + } } } else { toast(R.string.no_contacts_permission) @@ -495,7 +495,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { if (it) { SetRemindersDialog(this) { val reminders = it - Thread { + ensureBackgroundThread { addContactEvents(false, reminders) { if (it > 0) { toast(R.string.anniversaries_added) @@ -504,7 +504,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { toast(R.string.no_anniversaries) } } - }.start() + } } } else { toast(R.string.no_contacts_permission) @@ -755,7 +755,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { private fun exportEvents() { FilePickerDialog(this, pickFile = false, showFAB = true) { ExportEventsDialog(this, it) { exportPastEvents, file, eventTypes -> - Thread { + ensureBackgroundThread { val events = eventsHelper.getEventsToExport(exportPastEvents, eventTypes) if (events.isEmpty()) { toast(R.string.no_entries_for_exporting) @@ -768,7 +768,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { }) } } - }.start() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/ManageEventTypesActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/ManageEventTypesActivity.kt index ba6c42b89..4f797fa40 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/ManageEventTypesActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/ManageEventTypesActivity.kt @@ -11,6 +11,7 @@ import com.simplemobiletools.calendar.pro.interfaces.DeleteEventTypesListener import com.simplemobiletools.calendar.pro.models.EventType import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.updateTextColors +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import kotlinx.android.synthetic.main.activity_manage_event_types.* import java.util.* @@ -59,9 +60,9 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteEventTypesListener { } } - Thread { + ensureBackgroundThread { eventsHelper.deleteEventTypes(eventTypes, deleteEvents) - }.start() + } return true } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt index 32b8e1ebe..076407c24 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SettingsActivity.kt @@ -102,14 +102,14 @@ class SettingsActivity : SimpleActivity() { private fun checkPrimaryColor() { if (config.primaryColor != mStoredPrimaryColor) { - Thread { + ensureBackgroundThread { val eventTypes = eventsHelper.getEventTypesSync() if (eventTypes.filter { it.caldavCalendarId == 0 }.size == 1) { val eventType = eventTypes.first { it.caldavCalendarId == 0 } eventType.color = config.primaryColor eventsHelper.insertOrUpdateEventTypeSync(eventType) } - }.start() + } } } @@ -195,13 +195,13 @@ class SettingsActivity : SimpleActivity() { settings_manage_synced_calendars_holder.beGone() settings_caldav_pull_to_refresh_holder.beGone() - Thread { + ensureBackgroundThread { config.getSyncedCalendarIdsAsList().forEach { calDAVHelper.deleteCalDAVCalendarEvents(it.toLong()) } eventTypesDB.deleteEventTypesWithCalendarId(config.getSyncedCalendarIdsAsList()) updateDefaultEventTypeText() - }.start() + } } } @@ -222,7 +222,7 @@ class SettingsActivity : SimpleActivity() { toast(R.string.syncing) } - Thread { + ensureBackgroundThread { if (newCalendarIds.isNotEmpty()) { val existingEventTypeNames = eventsHelper.getEventTypesSync().map { it.getDisplayTitle().toLowerCase() } as ArrayList getSyncedCalDAVCalendars().forEach { @@ -253,7 +253,7 @@ class SettingsActivity : SimpleActivity() { eventTypesDB.deleteEventTypesWithCalendarId(removedCalendarIds) updateDefaultEventTypeText() - }.start() + } } } @@ -630,7 +630,7 @@ class SettingsActivity : SimpleActivity() { settings_default_event_type.text = getString(R.string.last_used_one) } } else { - Thread { + ensureBackgroundThread { val eventType = eventTypesDB.getEventTypeWithId(config.defaultEventTypeId) if (eventType != null) { config.lastUsedCaldavCalendarId = eventType.caldavCalendarId @@ -641,7 +641,7 @@ class SettingsActivity : SimpleActivity() { config.defaultEventTypeId = -1 updateDefaultEventTypeText() } - }.start() + } } } @@ -694,13 +694,13 @@ class SettingsActivity : SimpleActivity() { handlePermission(PERMISSION_READ_STORAGE) { if (it) { FilePickerDialog(this) { - Thread { + ensureBackgroundThread { try { parseFile(it) } catch (e: Exception) { showErrorToast(e) } - }.start() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SimpleActivity.kt index 4db43dbb9..dba3343f6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SimpleActivity.kt @@ -8,6 +8,7 @@ import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.refreshCalDAVCalendars import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.helpers.ensureBackgroundThread open class SimpleActivity : BaseSimpleActivity() { val CALDAV_REFRESH_DELAY = 3000L @@ -40,12 +41,12 @@ open class SimpleActivity : BaseSimpleActivity() { fun Context.syncCalDAVCalendars(callback: () -> Unit) { calDAVRefreshCallback = callback - Thread { + ensureBackgroundThread { val uri = CalendarContract.Calendars.CONTENT_URI contentResolver.unregisterContentObserver(calDAVSyncObserver) contentResolver.registerContentObserver(uri, false, calDAVSyncObserver) refreshCalDAVCalendars(config.caldavSyncedCalendarIds, true) - }.start() + } } // caldav refresh content observer triggers multiple times in a row at updating, so call the callback only a few seconds after the (hopefully) last one @@ -55,11 +56,11 @@ open class SimpleActivity : BaseSimpleActivity() { if (!selfChange) { calDAVRefreshHandler.removeCallbacksAndMessages(null) calDAVRefreshHandler.postDelayed({ - Thread { + ensureBackgroundThread { unregisterObserver() calDAVRefreshCallback?.invoke() calDAVRefreshCallback = null - }.start() + } }, CALDAV_REFRESH_DELAY) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SnoozeReminderActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SnoozeReminderActivity.kt index 13122fe1a..0caf7acb1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SnoozeReminderActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SnoozeReminderActivity.kt @@ -7,13 +7,14 @@ import com.simplemobiletools.calendar.pro.extensions.eventsDB import com.simplemobiletools.calendar.pro.extensions.rescheduleReminder import com.simplemobiletools.calendar.pro.helpers.EVENT_ID import com.simplemobiletools.commons.extensions.showPickSecondsDialogHelper +import com.simplemobiletools.commons.helpers.ensureBackgroundThread class SnoozeReminderActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) showPickSecondsDialogHelper(config.snoozeTime, true, cancelCallback = { dialogCancelled() }) { - Thread { + ensureBackgroundThread { val eventId = intent.getLongExtra(EVENT_ID, 0L) val event = eventsDB.getEventWithId(eventId) config.snoozeTime = it / 60 @@ -21,7 +22,7 @@ class SnoozeReminderActivity : AppCompatActivity() { runOnUiThread { finishActivity() } - }.start() + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/DayEventsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/DayEventsAdapter.kt index 56edcac04..f142e6334 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/DayEventsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/DayEventsAdapter.kt @@ -20,6 +20,7 @@ import com.simplemobiletools.commons.extensions.adjustAlpha import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.beInvisible import com.simplemobiletools.commons.extensions.beInvisibleIf +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.views.MyRecyclerView import kotlinx.android.synthetic.main.event_item_day_view.view.* @@ -142,7 +143,7 @@ class DayEventsAdapter(activity: SimpleActivity, val events: ArrayList, r DeleteEventDialog(activity, eventIds, hasRepeatableEvent) { it -> events.removeAll(eventsToDelete) - Thread { + ensureBackgroundThread { val nonRepeatingEventIDs = eventsToDelete.asSequence().filter { it.repeatInterval == 0 }.mapNotNull { it.id }.toMutableList() activity.eventsHelper.deleteEvents(nonRepeatingEventIDs, true) @@ -151,7 +152,7 @@ class DayEventsAdapter(activity: SimpleActivity, val events: ArrayList, r activity.runOnUiThread { removeSelectedItems(positions) } - }.start() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/EventListAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/EventListAdapter.kt index 27f34ce9d..73f52fd9c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/EventListAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/adapters/EventListAdapter.kt @@ -20,6 +20,7 @@ import com.simplemobiletools.commons.extensions.adjustAlpha import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.beInvisible import com.simplemobiletools.commons.extensions.beInvisibleIf +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener import com.simplemobiletools.commons.views.MyRecyclerView import kotlinx.android.synthetic.main.event_list_item.view.* @@ -208,7 +209,7 @@ class EventListAdapter(activity: SimpleActivity, var listItems: ArrayList Unit) { @@ -45,9 +46,9 @@ class EditEventTypeDialog(val activity: Activity, var eventType: EventType? = nu activity.setupDialogStuff(view, this, if (isNewEvent) R.string.add_new_type else R.string.edit_type) { showKeyboard(view.type_title) getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { - Thread { + ensureBackgroundThread { eventTypeConfirmed(view.type_title.value, this) - }.start() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt index 0a65e2144..ce8811dc9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/ImportEventsDialog.kt @@ -13,6 +13,7 @@ import com.simplemobiletools.calendar.pro.helpers.REGULAR_EVENT_TYPE_ID import com.simplemobiletools.commons.extensions.setFillWithStroke import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import kotlinx.android.synthetic.main.dialog_import_events.view.* class ImportEventsDialog(val activity: SimpleActivity, val path: String, val callback: (refreshView: Boolean) -> Unit) { @@ -21,7 +22,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal val config = activity.config init { - Thread { + ensureBackgroundThread { if (activity.eventTypesDB.getEventTypeWithId(config.lastUsedLocalEventTypeId) == null) { config.lastUsedLocalEventTypeId = REGULAR_EVENT_TYPE_ID } @@ -42,7 +43,7 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal activity.runOnUiThread { initDialog() } - }.start() + } } private fun initDialog() { @@ -69,25 +70,25 @@ class ImportEventsDialog(val activity: SimpleActivity, val path: String, val cal getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(null) activity.toast(R.string.importing) - Thread { + ensureBackgroundThread { val overrideFileEventTypes = view.import_events_checkbox.isChecked val result = IcsImporter(activity).importEvents(path, currEventTypeId, currEventTypeCalDAVCalendarId, overrideFileEventTypes) handleParseResult(result) dismiss() - }.start() + } } } } } private fun updateEventType(view: ViewGroup) { - Thread { + ensureBackgroundThread { val eventType = activity.eventTypesDB.getEventTypeWithId(currEventTypeId) activity.runOnUiThread { view.import_event_type_title.text = eventType!!.getDisplayTitle() view.import_event_type_color.setFillWithStroke(eventType.color, activity.config.backgroundColor) } - }.start() + } } private fun handleParseResult(result: IcsImporter.ImportResult) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventCalendarDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventCalendarDialog.kt index cb052db45..f01100d6d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventCalendarDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/dialogs/SelectEventCalendarDialog.kt @@ -14,6 +14,7 @@ import com.simplemobiletools.calendar.pro.models.CalDAVCalendar import com.simplemobiletools.commons.extensions.setFillWithStroke import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.updateTextColors +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import kotlinx.android.synthetic.main.dialog_select_radio_group.view.* import kotlinx.android.synthetic.main.radio_button_with_color.view.* @@ -26,7 +27,7 @@ class SelectEventCalendarDialog(val activity: Activity, val calendars: List) { - Thread { + ensureBackgroundThread { val file = getTempFile() if (file == null) { toast(R.string.unknown_error_occurred) - return@Thread + return@ensureBackgroundThread } val events = eventsDB.getEventsWithIds(ids) as ArrayList @@ -30,7 +31,7 @@ fun BaseSimpleActivity.shareEvents(ids: List) { sharePathIntent(file.absolutePath, BuildConfig.APPLICATION_ID) } } - }.start() + } } fun BaseSimpleActivity.getTempFile(): File? { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index a3a75ea17..f73c05670 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -33,10 +33,7 @@ import com.simplemobiletools.calendar.pro.receivers.CalDAVSyncReceiver import com.simplemobiletools.calendar.pro.receivers.NotificationReceiver import com.simplemobiletools.calendar.pro.services.SnoozeService import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.SILENT -import com.simplemobiletools.commons.helpers.WEEK_SECONDS -import com.simplemobiletools.commons.helpers.YEAR_SECONDS -import com.simplemobiletools.commons.helpers.isOreoPlus +import com.simplemobiletools.commons.helpers.* import org.joda.time.DateTime import org.joda.time.DateTimeZone import org.joda.time.LocalDate @@ -334,10 +331,10 @@ fun Context.getSyncedCalDAVCalendars() = calDAVHelper.getCalDAVCalendars(config. fun Context.recheckCalDAVCalendars(callback: () -> Unit) { if (config.caldavSync) { - Thread { + ensureBackgroundThread { calDAVHelper.refreshCalendars(false, callback) updateWidgets() - }.start() + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index a285e5fbe..3f488321e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -7,6 +7,7 @@ import com.simplemobiletools.calendar.pro.R import com.simplemobiletools.calendar.pro.extensions.* import com.simplemobiletools.calendar.pro.models.Event import com.simplemobiletools.calendar.pro.models.EventType +import com.simplemobiletools.commons.helpers.ensureBackgroundThread class EventsHelper(val context: Context) { private val config = context.config @@ -14,7 +15,7 @@ class EventsHelper(val context: Context) { private val eventTypesDB = context.eventTypesDB fun getEventTypes(activity: Activity, showWritableOnly: Boolean, callback: (notes: ArrayList) -> Unit) { - Thread { + ensureBackgroundThread { var eventTypes = ArrayList() try { eventTypes = eventTypesDB.getEventTypes().toMutableList() as ArrayList @@ -32,18 +33,18 @@ class EventsHelper(val context: Context) { activity.runOnUiThread { callback(eventTypes) } - }.start() + } } fun getEventTypesSync() = eventTypesDB.getEventTypes().toMutableList() as ArrayList fun insertOrUpdateEventType(activity: Activity, eventType: EventType, callback: ((newEventTypeId: Long) -> Unit)? = null) { - Thread { + ensureBackgroundThread { val eventTypeId = insertOrUpdateEventTypeSync(eventType) activity.runOnUiThread { callback?.invoke(eventTypeId) } - }.start() + } } fun insertOrUpdateEventTypeSync(eventType: EventType): Long { @@ -132,10 +133,10 @@ class EventsHelper(val context: Context) { } fun deleteAllEvents() { - Thread { + ensureBackgroundThread { val eventIds = eventsDB.getEventIds().toMutableList() deleteEvents(eventIds, true) - }.start() + } } fun deleteEvent(id: Long, deleteFromCalDAV: Boolean) = deleteEvents(arrayListOf(id), deleteFromCalDAV) @@ -189,14 +190,14 @@ class EventsHelper(val context: Context) { } fun doEventTypesContainEvents(eventTypeIds: ArrayList, callback: (contain: Boolean) -> Unit) { - Thread { + ensureBackgroundThread { val eventIds = eventsDB.getEventIdsByEventType(eventTypeIds) callback(eventIds.isNotEmpty()) - }.start() + } } fun getEventsWithSearchQuery(text: String, activity: Activity, callback: (searchedText: String, events: List) -> Unit) { - Thread { + ensureBackgroundThread { val searchQuery = "%$text%" val events = eventsDB.getEventsForSearch(searchQuery) val displayEventTypes = config.displayEventTypes @@ -204,12 +205,12 @@ class EventsHelper(val context: Context) { activity.runOnUiThread { callback(text, filteredEvents) } - }.start() + } } fun addEventRepetitionException(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean) { - Thread { - val parentEvent = eventsDB.getEventWithId(parentEventId) ?: return@Thread + ensureBackgroundThread { + val parentEvent = eventsDB.getEventWithId(parentEventId) ?: return@ensureBackgroundThread var repetitionExceptions = parentEvent.repetitionExceptions repetitionExceptions.add(Formatter.getDayCodeFromTS(occurrenceTS)) repetitionExceptions = repetitionExceptions.distinct().toMutableList() as ArrayList @@ -219,13 +220,13 @@ class EventsHelper(val context: Context) { if (addToCalDAV && config.caldavSync) { context.calDAVHelper.insertEventRepeatException(parentEvent, occurrenceTS) } - }.start() + } } fun getEvents(fromTS: Long, toTS: Long, eventId: Long = -1L, applyTypeFilter: Boolean = true, callback: (events: ArrayList) -> Unit) { - Thread { + ensureBackgroundThread { getEventsSync(fromTS, toTS, eventId, applyTypeFilter, callback) - }.start() + } } fun getEventsSync(fromTS: Long, toTS: Long, eventId: Long = -1L, applyTypeFilter: Boolean, callback: (events: ArrayList) -> Unit) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsExporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsExporter.kt index 1c7355b3c..ca0c994bc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsExporter.kt @@ -10,6 +10,7 @@ import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.getFileOutputStream import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.writeLn +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.models.FileDirItem import java.io.BufferedWriter import java.io.File @@ -31,7 +32,7 @@ class IcsExporter { return@getFileOutputStream } - Thread { + ensureBackgroundThread { calendars = activity.calDAVHelper.getCalDAVCalendars("", false) if (showExportingToast) { activity.toast(R.string.exporting) @@ -76,7 +77,7 @@ class IcsExporter { eventsFailed > 0 -> EXPORT_PARTIAL else -> EXPORT_OK }) - }.start() + } } } @@ -89,7 +90,7 @@ class IcsExporter { writeLn("$ACTION$DISPLAY") } else { writeLn("$ACTION$EMAIL") - val attendee = calendars.firstOrNull { it.id == event.getCalDAVCalendarId()}?.accountName + val attendee = calendars.firstOrNull { it.id == event.getCalDAVCalendarId() }?.accountName if (attendee != null) { writeLn("$ATTENDEE$MAILTO$attendee") } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt index 20a73617e..876df8bd6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/BootCompletedReceiver.kt @@ -6,16 +6,17 @@ import android.content.Intent import com.simplemobiletools.calendar.pro.extensions.notifyRunningEvents import com.simplemobiletools.calendar.pro.extensions.recheckCalDAVCalendars import com.simplemobiletools.calendar.pro.extensions.scheduleAllEvents +import com.simplemobiletools.commons.helpers.ensureBackgroundThread class BootCompletedReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { - Thread { + ensureBackgroundThread { context.apply { scheduleAllEvents() notifyRunningEvents() recheckCalDAVCalendars {} } - }.start() + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt index 58e905633..59a11a018 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt @@ -11,6 +11,7 @@ import com.simplemobiletools.calendar.pro.extensions.updateListWidget import com.simplemobiletools.calendar.pro.helpers.EVENT_ID import com.simplemobiletools.calendar.pro.helpers.Formatter import com.simplemobiletools.calendar.pro.helpers.REMINDER_NOTIFICATION +import com.simplemobiletools.commons.helpers.ensureBackgroundThread class NotificationReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { @@ -18,9 +19,9 @@ class NotificationReceiver : BroadcastReceiver() { val wakelock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "simplecalendar:notificationreceiver") wakelock.acquire(3000) - Thread { + ensureBackgroundThread { handleIntent(context, intent) - }.start() + } } private fun handleIntent(context: Context, intent: Intent) {