Commit 4dd2dadd authored by Geoffrey Métais's avatar Geoffrey Métais

Get default icon with a context

Reduce usage of VLCApplication.getAppContext()
parent 0bb7c56f
......@@ -92,7 +92,7 @@ public class HistoryAdapter extends DiffUtilAdapter<MediaWrapper, HistoryAdapter
final MediaWrapper media = getItem(position);
boolean isSelected = media.hasStateFlags(MediaLibraryItem.FLAG_SELECTED);
holder.binding.setMedia(media);
holder.binding.setCover(ImageLoaderKt.getIconDrawableMediaWrapper(media.getType()));
holder.binding.setCover(ImageLoaderKt.getMediaIconDrawable(holder.itemView.getContext(), media.getType()));
holder.selectView(isSelected);
}
......
package org.videolan.vlc.gui;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
......@@ -10,6 +9,8 @@ import org.videolan.vlc.databinding.SearchItemBinding;
import org.videolan.vlc.gui.helpers.SelectorViewHolder;
import org.videolan.vlc.gui.helpers.UiTools;
import androidx.recyclerview.widget.RecyclerView;
public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.ViewHolder> {
......@@ -30,7 +31,7 @@ public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapte
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (TextUtils.isEmpty(mDataList[position].getArtworkMrl()))
holder.binding.setCover(UiTools.getDefaultCover(mDataList[position]));
holder.binding.setCover(UiTools.getDefaultCover(holder.itemView.getContext(), mDataList[position]));
holder.binding.setItem(mDataList[position]);
}
......
......@@ -49,6 +49,7 @@ import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.paging.PagedList;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
......@@ -71,8 +72,11 @@ public class AudioBrowserAdapter extends PagedListAdapter<MediaLibraryItem, Audi
multiSelectHelper = new MultiSelectHelper<>(this, Constants.UPDATE_SELECTION);
mIEventsHandler = eventsHandler;
mType = type;
mDefaultCover = ImageLoaderKt.getIconDrawable(type);
mHasSections = sections;
Context ctx = null;
if (eventsHandler instanceof Context) ctx = (Context) eventsHandler;
else if (eventsHandler instanceof Fragment) ctx = ((Fragment)eventsHandler).getContext();
mDefaultCover = ctx != null ? ImageLoaderKt.getAudioIconDrawable(ctx, type) : null;
}
public AudioBrowserAdapter(int type, IEventsHandler eventsHandler) {
......
package org.videolan.vlc.gui.helpers
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
......@@ -55,16 +56,18 @@ fun loadImage(v: View, item: MediaLibraryItem?) {
else AppScope.launch { getImage(v, findInLibrary(item, isMedia, isGroup), binding) }
}
fun getIconDrawable(type: Int): BitmapDrawable? = when (type) {
MediaLibraryItem.TYPE_ALBUM -> UiTools.Resources.DEFAULT_COVER_ALBUM_DRAWABLE
MediaLibraryItem.TYPE_ARTIST -> UiTools.Resources.DEFAULT_COVER_ARTIST_DRAWABLE
MediaLibraryItem.TYPE_MEDIA -> UiTools.Resources.DEFAULT_COVER_AUDIO_DRAWABLE
else -> null
fun getAudioIconDrawable(context: Context?, type: Int): BitmapDrawable? = context?.let {
when (type) {
MediaLibraryItem.TYPE_ALBUM -> UiTools.getDefaultAlbumDrawable(it)
MediaLibraryItem.TYPE_ARTIST -> UiTools.getDefaultArtistDrawable(it)
MediaLibraryItem.TYPE_MEDIA -> UiTools.getDefaultAudioDrawable(it)
else -> null
}
}
fun getIconDrawableMediaWrapper(type: Int): BitmapDrawable? = when (type) {
MediaWrapper.TYPE_AUDIO -> UiTools.Resources.DEFAULT_COVER_AUDIO_DRAWABLE
MediaWrapper.TYPE_VIDEO -> UiTools.Resources.DEFAULT_COVER_VIDEO_DRAWABLE
fun getMediaIconDrawable(context: Context, type: Int): BitmapDrawable? = when (type) {
MediaWrapper.TYPE_AUDIO -> UiTools.getDefaultAudioDrawable(context)
MediaWrapper.TYPE_VIDEO -> UiTools.getDefaultVideoDrawable(context)
else -> null
}
......@@ -87,7 +90,7 @@ fun placeHolderImageView(v: View, item: MediaLibraryItem?) {
if (item == null) {
v.background = ContextCompat.getDrawable(v.context, R.drawable.rounded_corners_grey)
} else {
v.background = UiTools.Resources.DEFAULT_COVER_AUDIO_DRAWABLE
v.background = UiTools.getDefaultAudioDrawable(v.context)
}
}
......
......@@ -22,19 +22,28 @@
*/
package org.videolan.vlc.gui.helpers;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import org.videolan.vlc.BR;
import org.videolan.vlc.R;
import androidx.core.content.ContextCompat;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;
public class SelectorViewHolder<T extends ViewDataBinding> extends RecyclerView.ViewHolder implements View.OnFocusChangeListener {
public T binding = null;
private final int ITEM_FOCUS_ON;
private final int ITEM_FOCUS_OFF;
private final int ITEM_SELECTION_ON;
public SelectorViewHolder(T vdb) {
super(vdb.getRoot());
binding = vdb;
ITEM_FOCUS_OFF = ContextCompat.getColor(vdb.getRoot().getContext(), R.color.transparent);
ITEM_FOCUS_ON = ContextCompat.getColor(vdb.getRoot().getContext(), R.color.orange500transparent);
ITEM_SELECTION_ON = ContextCompat.getColor(vdb.getRoot().getContext(), R.color.orange200transparent);
itemView.setOnFocusChangeListener(this);
}
......@@ -43,7 +52,7 @@ public class SelectorViewHolder<T extends ViewDataBinding> extends RecyclerView.
}
private void setViewBackground(boolean focus, boolean selected) {
final int color = focus ? UiTools.Resources.ITEM_FOCUS_ON : selected ? UiTools.Resources.ITEM_SELECTION_ON : UiTools.Resources.ITEM_FOCUS_OFF;
final int color = focus ? ITEM_FOCUS_ON : selected ? ITEM_SELECTION_ON : ITEM_FOCUS_OFF;
binding.setVariable(BR.bgColor, color);
}
......
......@@ -96,17 +96,39 @@ import androidx.fragment.app.FragmentActivity;
public class UiTools {
private static final String TAG = "VLC/UiTools";
private static BitmapDrawable DEFAULT_COVER_VIDEO_DRAWABLE;
private static BitmapDrawable DEFAULT_COVER_AUDIO_DRAWABLE;
private static BitmapDrawable DEFAULT_COVER_ALBUM_DRAWABLE;
private static BitmapDrawable DEFAULT_COVER_ARTIST_DRAWABLE;
public static BitmapDrawable getDefaultVideoDrawable(Context context) {
if (DEFAULT_COVER_VIDEO_DRAWABLE == null) {
final Bitmap DEFAULT_COVER_VIDEO = BitmapCache.getFromResource(context.getResources(), R.drawable.ic_no_thumbnail_1610);
DEFAULT_COVER_VIDEO_DRAWABLE = new BitmapDrawable(context.getResources(), DEFAULT_COVER_VIDEO);
}
return DEFAULT_COVER_VIDEO_DRAWABLE;
}
public static BitmapDrawable getDefaultAudioDrawable(Context context) {
if (DEFAULT_COVER_AUDIO_DRAWABLE == null) {
final Bitmap DEFAULT_COVER_AUDIO = BitmapCache.getFromResource(context.getResources(), R.drawable.ic_no_song);
DEFAULT_COVER_AUDIO_DRAWABLE = new BitmapDrawable(context.getResources(), DEFAULT_COVER_AUDIO);
}
return DEFAULT_COVER_AUDIO_DRAWABLE;
}
public static class Resources {
private static final Bitmap DEFAULT_COVER_VIDEO = BitmapCache.getFromResource(VLCApplication.getAppResources(), R.drawable.ic_no_thumbnail_1610);
private static final Bitmap DEFAULT_COVER_AUDIO = BitmapCache.getFromResource(VLCApplication.getAppResources(), R.drawable.ic_no_song);
public static final int ITEM_FOCUS_OFF = ContextCompat.getColor(VLCApplication.getAppContext(), R.color.transparent);
public static final int ITEM_FOCUS_ON = ContextCompat.getColor(VLCApplication.getAppContext(), R.color.orange500transparent);
public static final int ITEM_SELECTION_ON = ContextCompat.getColor(VLCApplication.getAppContext(), R.color.orange200transparent);
public static final BitmapDrawable DEFAULT_COVER_ARTIST_DRAWABLE = new BitmapDrawable(VLCApplication.getAppResources(), BitmapCache.getFromResource(VLCApplication.getAppResources(), R.drawable.ic_no_artist));
public static final BitmapDrawable DEFAULT_COVER_ALBUM_DRAWABLE = new BitmapDrawable(VLCApplication.getAppResources(), BitmapCache.getFromResource(VLCApplication.getAppResources(), R.drawable.ic_no_album));
public static final BitmapDrawable DEFAULT_COVER_VIDEO_DRAWABLE = new BitmapDrawable(VLCApplication.getAppResources(), DEFAULT_COVER_VIDEO);
public static final BitmapDrawable DEFAULT_COVER_AUDIO_DRAWABLE = new BitmapDrawable(VLCApplication.getAppResources(), DEFAULT_COVER_AUDIO);
public static BitmapDrawable getDefaultAlbumDrawable(Context context) {
if (DEFAULT_COVER_ALBUM_DRAWABLE == null) {
DEFAULT_COVER_ALBUM_DRAWABLE = new BitmapDrawable(context.getResources(), BitmapCache.getFromResource(context.getResources(), R.drawable.ic_no_album));
}
return DEFAULT_COVER_ALBUM_DRAWABLE;
}
public static BitmapDrawable getDefaultArtistDrawable(Context context) {
if (DEFAULT_COVER_ARTIST_DRAWABLE == null) {
DEFAULT_COVER_ARTIST_DRAWABLE = new BitmapDrawable(context.getResources(), BitmapCache.getFromResource(context.getResources(), R.drawable.ic_no_artist));
}
return DEFAULT_COVER_ARTIST_DRAWABLE;
}
private static final Handler sHandler = new Handler(Looper.getMainLooper());
......@@ -293,17 +315,17 @@ public class UiTools {
throw new IllegalThreadStateException();
}
public static BitmapDrawable getDefaultCover(MediaLibraryItem item) {
public static BitmapDrawable getDefaultCover(Context context, MediaLibraryItem item) {
switch (item.getItemType()) {
case MediaLibraryItem.TYPE_ARTIST:
return Resources.DEFAULT_COVER_ARTIST_DRAWABLE;
return getDefaultArtistDrawable(context);
case MediaLibraryItem.TYPE_ALBUM:
return Resources.DEFAULT_COVER_ALBUM_DRAWABLE;
return getDefaultAlbumDrawable(context);
case MediaLibraryItem.TYPE_MEDIA:
if (((MediaWrapper)item).getType() == MediaWrapper.TYPE_VIDEO)
return Resources.DEFAULT_COVER_VIDEO_DRAWABLE;
return getDefaultVideoDrawable(context);
default:
return Resources.DEFAULT_COVER_AUDIO_DRAWABLE;
return getDefaultAudioDrawable(context);
}
}
......
......@@ -29,6 +29,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import org.jetbrains.annotations.NotNull;
import org.videolan.libvlc.util.AndroidUtil;
import org.videolan.medialibrary.Tools;
import org.videolan.medialibrary.media.MediaWrapper;
......@@ -123,8 +124,8 @@ public class VideoListAdapter extends DiffUtilAdapter<MediaWrapper, VideoListAda
}
@Override
public void onViewRecycled(ViewHolder holder) {
holder.binding.setVariable(BR.cover, UiTools.Resources.DEFAULT_COVER_VIDEO_DRAWABLE);
public void onViewRecycled(@NotNull ViewHolder holder) {
holder.binding.setVariable(BR.cover, UiTools.getDefaultVideoDrawable(holder.itemView.getContext()));
}
@Override
......@@ -219,7 +220,7 @@ public class VideoListAdapter extends DiffUtilAdapter<MediaWrapper, VideoListAda
super(binding);
overlay = itemView.findViewById(R.id.ml_item_overlay);
binding.setVariable(BR.holder, this);
binding.setVariable(BR.cover, UiTools.Resources.DEFAULT_COVER_VIDEO_DRAWABLE);
binding.setVariable(BR.cover, UiTools.getDefaultVideoDrawable(itemView.getContext()));
if (AndroidUtil.isMarshMallowOrLater) itemView.setOnContextClickListener(new View.OnContextClickListener() {
@Override
public boolean onContextClick(View v) {
......
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