Commit 3118519d authored by Geoffrey Métais's avatar Geoffrey Métais

runIO() util method for Java I/O calls

parent 4b086a62
......@@ -84,7 +84,7 @@ public class InfoActivity extends AudioPlayerContainerActivity implements View.O
? savedInstanceState.getInt(TAG_FAB_VISIBILITY) : -1;
if (!TextUtils.isEmpty(mItem.getArtworkMrl())) {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(mItem.getArtworkMrl()), 0);
......
......@@ -125,7 +125,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
final int fabVisibility = savedInstanceState != null ? savedInstanceState.getInt(TAG_FAB_VISIBILITY) : -1;
if (!TextUtils.isEmpty(mPlaylist.getArtworkMrl())) {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(mPlaylist.getArtworkMrl()), 0);
......@@ -347,7 +347,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
}
protected void deleteMedia(final MediaLibraryItem mw) {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final LinkedList<String> foldersToReload = new LinkedList<>();
......
......@@ -68,7 +68,7 @@ public class SearchActivity extends AppCompatActivity implements TextWatcher, Te
}
private void performSearh(final String query) {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final SearchAggregate searchAggregate = mMedialibrary.search(query);
......
......@@ -150,7 +150,7 @@ public class FileBrowserFragment extends BaseBrowserFragment {
final MenuItem item = menu.findItem(R.id.ml_menu_save);
if (item == null) return;
item.setVisible(!isRootDirectory() && getMrl().startsWith("file"));
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final boolean isFavorite = getMrl() != null && getBrowserFavRepository().browserFavExists(Uri.parse(getMrl()));
......
......@@ -176,7 +176,7 @@ public abstract class MediaBrowserFragment<T extends SortableModel> extends Frag
}
protected void deleteMedia(final MediaLibraryItem mw, final boolean refresh, final Runnable failCB) {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final LinkedList<String> foldersToReload = new LinkedList<>();
......
......@@ -81,7 +81,7 @@ public class NetworkBrowserFragment extends BaseBrowserFragment {
super.onPrepareOptionsMenu(menu);
final MenuItem item = menu.findItem(R.id.ml_menu_save);
item.setVisible(!isRootDirectory());
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final boolean isFavorite = getMrl() != null && getBrowserFavRepository().browserFavExists(Uri.parse(getMrl()));
......
......@@ -155,7 +155,7 @@ public class NetworkServerDialog extends DialogFragment implements AdapterView.O
mEditAddress.getText().toString() : mEditServername.getText().toString();
Uri uri = Uri.parse(mUrl.getText().toString());
if (mUri != null) {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
mBrowserFavRepository.deleteBrowserFav(mUri);
......
......@@ -145,7 +145,7 @@ public class SavePlaylistDialog extends DialogFragment implements View.OnClickLi
}
private void savePlaylist() {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
public void run() {
final String name = mEditText.getText().toString().trim();
final boolean addTracks = !Tools.isArrayEmpty(mNewTrack);
......
......@@ -13,11 +13,9 @@ import android.support.v4.view.ViewCompat
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.Main
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.vlc.BR
......@@ -41,13 +39,13 @@ fun loadImage(v: View, item: MediaLibraryItem?) {
val cacheKey = if (isGroup) "group: ${item.title}" else ThumbnailsProvider.getMediaCacheKey(isMedia, item)
val bitmap = if (cacheKey !== null) sBitmapCache.getBitmapFromMemCache(cacheKey) else null
if (bitmap !== null) updateImageView(bitmap, v, binding)
else launch(UI, CoroutineStart.UNDISPATCHED) { getImage(v, findInLibrary(item, isMedia, isGroup), binding) }
else GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) { getImage(v, findInLibrary(item, isMedia, isGroup), binding) }
}
@BindingAdapter("imageUri")
fun downloadIcon(v: View, imageUri: Uri?) {
if (imageUri != null && imageUri.scheme == "http") launch(UI, CoroutineStart.UNDISPATCHED) {
val image = withContext(IO) { HttpImageLoader.downloadBitmap(imageUri.toString()) }
if (imageUri != null && imageUri.scheme == "http") GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED) {
val image = withContext(Dispatchers.IO) { HttpImageLoader.downloadBitmap(imageUri.toString()) }
updateImageView(image, v, DataBindingUtil.findBinding(v))
}
}
......@@ -69,7 +67,7 @@ private suspend fun getImage(v: View, item: MediaLibraryItem, binding: ViewDataB
binding?.removeOnRebindCallback(rebindCallbacks!!)
}
private suspend fun obtainBitmap(item: MediaLibraryItem, width: Int) = withContext(IO) {
private suspend fun obtainBitmap(item: MediaLibraryItem, width: Int) = withContext(Dispatchers.IO) {
if (item.itemType == MediaLibraryItem.TYPE_MEDIA) ThumbnailsProvider.getMediaThumbnail(item as MediaWrapper, width)
else AudioUtil.readCoverBitmap(Uri.decode(item.artworkMrl), width)
}
......@@ -105,7 +103,7 @@ private suspend fun findInLibrary(item: MediaLibraryItem, isMedia: Boolean, isGr
val isMediaFile = type == MediaWrapper.TYPE_AUDIO || type == MediaWrapper.TYPE_VIDEO
val uri = mw.uri
if (!isMediaFile && !(type == MediaWrapper.TYPE_DIR && "upnp" == uri.scheme)) return item
if (isMediaFile && "file" == uri.scheme) return withContext(IO) { sMedialibrary.getMedia(uri) } ?: item
if (isMediaFile && "file" == uri.scheme) return withContext(Dispatchers.IO) { sMedialibrary.getMedia(uri) } ?: item
}
return item
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ import org.videolan.vlc.util.WorkersKt;
public class MedialibraryUtils {
public static void removeDir(final String path) {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
VLCApplication.getMLInstance().removeFolder(path);
......
......@@ -107,7 +107,7 @@ public class PreferencesAdvanced extends BasePreferenceFragment implements Share
if (VLCApplication.getMLInstance().isWorking())
UiTools.snacker(getView(), getString(R.string.settings_ml_block_scan));
else {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final Runnable dump = new Runnable() {
......
......@@ -152,7 +152,7 @@ public class MediaItemDetailsFragment extends DetailsSupportFragment implements
Toast.makeText(VLCApplication.getAppContext(), R.string.favorite_added, Toast.LENGTH_SHORT).show();
break;
case ID_FAVORITE_DELETE:
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
mBrowserFavRepository.deleteBrowserFav(Uri.parse(mMedia.getLocation()));
......@@ -193,7 +193,7 @@ public class MediaItemDetailsFragment extends DetailsSupportFragment implements
selector.addClassPresenter(ListRow.class,
new ListRowPresenter());
mRowsAdapter = new ArrayObjectAdapter(selector);
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final Bitmap cover = media.getType() == MediaWrapper.TYPE_AUDIO || media.getType() == MediaWrapper.TYPE_VIDEO
......
......@@ -157,7 +157,7 @@ public class AudioPlayerActivity extends BaseTvActivity implements PlaybackServi
}
private void updateBackground() {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(mCurrentCoverArt), mBinding.albumCover.getWidth());
......
......@@ -48,7 +48,7 @@ public abstract class AudioMediaSwitcher extends FlingViewGroup {
final String artMrl = service.getCoverArt();
final String prevArtMrl = service.getPrevCoverArt();
final String nextArtMrl = service.getNextCoverArt();
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final Bitmap coverCurrent = artMrl != null ? AudioUtil.readCoverBitmap(Uri.decode(artMrl), 512) : null;
......
......@@ -243,7 +243,7 @@ public class FileUtils {
}
private static void asyncRecursiveDelete(final File fileOrDirectory, final Callback callback) {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
public void run() {
if (!fileOrDirectory.exists() || !fileOrDirectory.canWrite())
return;
......
......@@ -60,7 +60,7 @@ public class VLCInstance {
// TODO change LibVLC signature to accept a List instead of an ArrayList
sLibVLC = new LibVLC(context, VLCOptions.getLibOptions());
WorkersKt.runBackground(sCopyLua);
WorkersKt.runIO(sCopyLua);
}
return sLibVLC;
}
......
......@@ -2,17 +2,20 @@ package org.videolan.vlc.util
import android.os.Looper
import android.os.Process
import kotlinx.coroutines.experimental.Runnable
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.Main
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import java.util.concurrent.ThreadFactory
fun runBackground(runnable: Runnable) {
if (Looper.myLooper() != Looper.getMainLooper()) runnable.run()
else launch { runnable.run() }
else GlobalScope.launch(Dispatchers.Default) { runnable.run() }
}
fun runOnMainThread(runnable: Runnable) {
if (Looper.myLooper() == Looper.getMainLooper()) runnable.run()
else launch(UI) { runnable.run() }
GlobalScope.launch(Dispatchers.Main.immediate) { runnable.run() }
}
fun runIO(runnable: Runnable) {
GlobalScope.launch(Dispatchers.IO) { runnable.run() }
}
......@@ -118,7 +118,7 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
} else if (ACTION_WIDGET_UPDATE_COVER.equals(action)) {
final String artworkMrl = intent.getStringExtra("artworkMrl");
if (!TextUtils.isEmpty(artworkMrl)) {
WorkersKt.runBackground(new Runnable() {
WorkersKt.runIO(new Runnable() {
@Override
public void run() {
final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(artworkMrl), 320);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment