create new threads only when needed

This commit is contained in:
tibbi 2019-07-03 12:30:09 +02:00
parent 2fb754babd
commit 0210fb4204
7 changed files with 39 additions and 36 deletions

View file

@ -19,10 +19,7 @@ import androidx.core.view.MenuItemCompat
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.LICENSE_OTTO
import com.simplemobiletools.commons.helpers.LICENSE_PICASSO
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.interfaces.RecyclerScrollCallback
import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.commons.models.FileDirItem
@ -341,11 +338,11 @@ class MainActivity : SimpleActivity(), SongListListener {
if (config.currentPlaylist == ALL_SONGS_PLAYLIST_ID) {
toast(R.string.all_songs_cannot_be_deleted)
} else {
Thread {
ensureBackgroundThread {
val playlist = playlistDAO.getPlaylistWithId(config.currentPlaylist)
runOnUiThread {
RemovePlaylistDialog(this, playlist) {
Thread {
ensureBackgroundThread {
if (it) {
val paths = getPlaylistSongs(config.currentPlaylist).map { it.path }
val files = paths.map { FileDirItem(it, it.getFilenameFromPath()) } as ArrayList<FileDirItem>
@ -359,20 +356,20 @@ class MainActivity : SimpleActivity(), SongListListener {
deletePlaylists(arrayListOf(playlist))
}
playlistChanged(ALL_SONGS_PLAYLIST_ID)
}.start()
}
}
}
}.start()
}
}
}
private fun openPlaylist() {
Thread {
ensureBackgroundThread {
val playlists = playlistDAO.getAll() as ArrayList<Playlist>
runOnUiThread {
showPlaylists(playlists)
}
}.start()
}
}
private fun showPlaylists(playlists: ArrayList<Playlist>) {
@ -399,11 +396,11 @@ class MainActivity : SimpleActivity(), SongListListener {
private fun addFolderToPlaylist() {
FilePickerDialog(this, getFilePickerInitialPath(), pickFile = false) {
toast(R.string.fetching_songs)
Thread {
ensureBackgroundThread {
val folderSongs = getFolderSongs(File(it))
RoomHelper(applicationContext).addSongsToPlaylist(folderSongs)
sendIntent(REFRESH_LIST)
}.start()
}
}
}
@ -423,7 +420,7 @@ class MainActivity : SimpleActivity(), SongListListener {
private fun addFileToPlaylist() {
FilePickerDialog(this, getFilePickerInitialPath()) {
Thread {
ensureBackgroundThread {
lastFilePickerPath = it
if (it.isAudioFast()) {
RoomHelper(applicationContext).addSongToPlaylist(it)
@ -431,15 +428,15 @@ class MainActivity : SimpleActivity(), SongListListener {
} else {
toast(R.string.invalid_file_format)
}
}.start()
}
}
}
private fun createPlaylistFromFolder() {
FilePickerDialog(this, getFilePickerInitialPath(), pickFile = false) {
Thread {
ensureBackgroundThread {
createPlaylistFrom(it)
}.start()
}
}
}

View file

@ -3,6 +3,7 @@ package com.simplemobiletools.musicplayer.activities
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.adapters.PlaylistsAdapter
import com.simplemobiletools.musicplayer.dialogs.NewPlaylistDialog
@ -20,7 +21,7 @@ class PlaylistsActivity : SimpleActivity(), RefreshPlaylistsListener {
}
private fun getPlaylists() {
Thread {
ensureBackgroundThread {
val playlists = playlistDAO.getAll() as ArrayList<Playlist>
runOnUiThread {
PlaylistsAdapter(this@PlaylistsActivity, playlists, this@PlaylistsActivity, playlists_list) {
@ -30,7 +31,7 @@ class PlaylistsActivity : SimpleActivity(), RefreshPlaylistsListener {
playlists_list.adapter = this
}
}
}.start()
}
}
override fun refreshItems() {

View file

@ -5,6 +5,7 @@ import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.musicplayer.R
@ -115,7 +116,7 @@ class PlaylistsAdapter(activity: SimpleActivity, val playlists: ArrayList<Playli
playlists.removeAll(playlistsToDelete)
Thread {
ensureBackgroundThread {
val isDeletingCurrentPlaylist = ids.contains(activity.config.currentPlaylist)
activity.deletePlaylists(playlistsToDelete)
activity.runOnUiThread {
@ -125,7 +126,7 @@ class PlaylistsAdapter(activity: SimpleActivity, val playlists: ArrayList<Playli
removeSelectedItems(positions)
}
}
}.start()
}
}
private fun getItemWithKey(key: Int): Playlist? = playlists.firstOrNull { it.id == key }

View file

@ -9,6 +9,7 @@ import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.PropertiesDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView
@ -186,12 +187,12 @@ class SongAdapter(activity: SimpleActivity, var songs: ArrayList<Song>, val list
private fun askConfirmDelete() {
ConfirmationDialog(activity) {
Thread {
ensureBackgroundThread {
deleteSongs()
activity.runOnUiThread {
finishActMode()
}
}.start()
}
}
}
@ -284,13 +285,13 @@ class SongAdapter(activity: SimpleActivity, var songs: ArrayList<Song>, val list
songs.removeAll(removeSongs)
positions.sortDescending()
removeSelectedItems(positions)
Thread {
ensureBackgroundThread {
activity.songsDAO.removeSongsFromPlaylists(removeSongs)
if (songs.isEmpty()) {
listener.refreshItems()
}
}.start()
}
}
private fun getFirstSelectedItemPath() = getSelectedSongs().firstOrNull()?.path ?: ""
@ -357,10 +358,10 @@ class SongAdapter(activity: SimpleActivity, var songs: ArrayList<Song>, val list
if (songs.isNotEmpty() && currentSong != null) {
selectedKeys.add(currentSong!!.path.hashCode())
activity.sendIntent(NEXT)
Thread {
ensureBackgroundThread {
deleteSongs()
selectedKeys.clear()
}.start()
}
}
}
}

View file

@ -6,6 +6,7 @@ import android.provider.MediaStore
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.songsDAO
import com.simplemobiletools.musicplayer.models.Song
@ -69,13 +70,13 @@ class EditDialog(val activity: BaseSimpleActivity, val song: Song, val callback:
}
private fun storeEditedSong(song: Song, oldPath: String, newPath: String) {
Thread {
ensureBackgroundThread {
try {
activity.songsDAO.updateSongInfo(newPath, song.artist, song.title, oldPath)
} catch (e: Exception) {
activity.showErrorToast(e)
}
}.start()
}
}
private fun updateContentResolver(context: Context, songID: Long, newSongTitle: String, newSongArtist: String) {

View file

@ -6,6 +6,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.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.getPlaylistIdWithTitle
import com.simplemobiletools.musicplayer.extensions.playlistDAO
@ -33,7 +34,7 @@ class NewPlaylistDialog(val activity: Activity, var playlist: Playlist? = null,
showKeyboard(view.new_playlist_title)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val title = view.new_playlist_title.value
Thread {
ensureBackgroundThread {
val playlistIdWithTitle = activity.getPlaylistIdWithTitle(title)
var isPlaylistTitleTaken = isNewPlaylist && playlistIdWithTitle != -1
if (!isPlaylistTitleTaken) {
@ -42,10 +43,10 @@ class NewPlaylistDialog(val activity: Activity, var playlist: Playlist? = null,
if (title.isEmpty()) {
activity.toast(R.string.empty_name)
return@Thread
return@ensureBackgroundThread
} else if (isPlaylistTitleTaken) {
activity.toast(R.string.playlist_name_exists)
return@Thread
return@ensureBackgroundThread
}
playlist!!.title = title
@ -63,7 +64,7 @@ class NewPlaylistDialog(val activity: Activity, var playlist: Playlist? = null,
} else {
activity.toast(R.string.unknown_error_occurred)
}
}.start()
}
}
}
}

View file

@ -31,6 +31,7 @@ import androidx.core.app.NotificationCompat
import androidx.media.session.MediaButtonReceiver
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.activities.MainActivity
@ -182,12 +183,12 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
private fun handleInit() {
mIsThirdPartyIntent = false
Thread {
ensureBackgroundThread {
if (!mIsServiceInitialized) {
initService()
}
initSongs()
}.start()
}
}
private fun handleInitPath(intent: Intent) {
@ -245,7 +246,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
private fun handleRefreshList(intent: Intent) {
mSongs.clear()
Thread {
ensureBackgroundThread {
getSortedSongs()
Handler(Looper.getMainLooper()).post {
mBus!!.post(Events.PlaylistUpdated(mSongs))
@ -255,7 +256,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
mPlayOnPrepare = false
setupNextSong()
}
}.start()
}
}
private fun handleSetProgress(intent: Intent) {