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