Commit 3a1b9e1a authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Medialibrary: Nullity check for search results

parent 19da32d8
......@@ -173,59 +173,74 @@ jobject
convertSearchAggregateObject(JNIEnv* env, fields *fields, medialibrary::SearchAggregate const& searchAggregatePtr)
{
//Albums
jobjectArray albums = (jobjectArray) env->NewObjectArray(searchAggregatePtr.albums->count(), fields->Album.clazz, NULL);
jobjectArray albums = nullptr;
int index = -1;
for(medialibrary::AlbumPtr const& album : searchAggregatePtr.albums->all()) {
jobject item = convertAlbumObject(env, fields, album);
env->SetObjectArrayElement(albums, ++index, item);
env->DeleteLocalRef(item);
if (searchAggregatePtr.albums != nullptr) {
albums = (jobjectArray) env->NewObjectArray(searchAggregatePtr.albums->count(), fields->Album.clazz, NULL);
for(medialibrary::AlbumPtr const& album : searchAggregatePtr.albums->all()) {
jobject item = convertAlbumObject(env, fields, album);
env->SetObjectArrayElement(albums, ++index, item);
env->DeleteLocalRef(item);
}
}
//Artists
jobjectArray artists = (jobjectArray) env->NewObjectArray(searchAggregatePtr.artists->count(), fields->Artist.clazz, NULL);
index = -1;
for(medialibrary::ArtistPtr const& artist : searchAggregatePtr.artists->all()) {
jobject item = convertArtistObject(env, fields, artist);
env->SetObjectArrayElement(artists, ++index, item);
env->DeleteLocalRef(item);
jobjectArray artists = nullptr;
if (searchAggregatePtr.artists != nullptr) {
index = -1;
artists = (jobjectArray) env->NewObjectArray(searchAggregatePtr.artists->count(), fields->Artist.clazz, NULL);
for(medialibrary::ArtistPtr const& artist : searchAggregatePtr.artists->all()) {
jobject item = convertArtistObject(env, fields, artist);
env->SetObjectArrayElement(artists, ++index, item);
env->DeleteLocalRef(item);
}
}
//Genres
jobjectArray genres = (jobjectArray) env->NewObjectArray(searchAggregatePtr.genres->count(), fields->Genre.clazz, NULL);
index = -1;
for(medialibrary::GenrePtr const& genre : searchAggregatePtr.genres->all()) {
jobject item = convertGenreObject(env, fields, genre);
env->SetObjectArrayElement(genres, ++index, item);
env->DeleteLocalRef(item);
jobjectArray genres = nullptr;
if (searchAggregatePtr.genres != nullptr) {
index = -1;
genres = (jobjectArray) env->NewObjectArray(searchAggregatePtr.genres->count(), fields->Genre.clazz, NULL);
for(medialibrary::GenrePtr const& genre : searchAggregatePtr.genres->all()) {
jobject item = convertGenreObject(env, fields, genre);
env->SetObjectArrayElement(genres, ++index, item);
env->DeleteLocalRef(item);
}
}
//Playlists
jobjectArray playlists = (jobjectArray) env->NewObjectArray(searchAggregatePtr.playlists->count(), fields->Playlist.clazz, NULL);
index = -1;
for(medialibrary::PlaylistPtr const& playlist : searchAggregatePtr.playlists->all()) {
jobject item = convertPlaylistObject(env, fields, playlist);
env->SetObjectArrayElement(playlists, ++index, item);
env->DeleteLocalRef(item);
jobjectArray playlists = nullptr;
if (searchAggregatePtr.playlists != nullptr) {
index = -1;
playlists = (jobjectArray) env->NewObjectArray(searchAggregatePtr.playlists->count(), fields->Playlist.clazz, NULL);
for(medialibrary::PlaylistPtr const& playlist : searchAggregatePtr.playlists->all()) {
jobject item = convertPlaylistObject(env, fields, playlist);
env->SetObjectArrayElement(playlists, ++index, item);
env->DeleteLocalRef(item);
}
}
//Media
std::vector<medialibrary::MediaPtr> videos = {};
std::vector<medialibrary::MediaPtr> tracks = {};
for(medialibrary::MediaPtr const& media : searchAggregatePtr.media->all()) {
if (media->subType() == medialibrary::IMedia::SubType::AlbumTrack) tracks.push_back(media);
else videos.push_back(media);
}
jobjectArray videoList = (jobjectArray) env->NewObjectArray(videos.size(), fields->MediaWrapper.clazz, NULL);
index = -1;
for(medialibrary::MediaPtr const& media : videos) {
jobject item = mediaToMediaWrapper(env, fields, media);
env->SetObjectArrayElement(videoList, ++index, item);
env->DeleteLocalRef(item);
}
jobjectArray tracksList = (jobjectArray) env->NewObjectArray(tracks.size(), fields->MediaWrapper.clazz, NULL);
index = -1;
for(medialibrary::MediaPtr const& media : tracks) {
jobject item = mediaToMediaWrapper(env, fields, media);
env->SetObjectArrayElement(tracksList, ++index, item);
env->DeleteLocalRef(item);
jobjectArray videoList = nullptr;
jobjectArray tracksList = nullptr;
if (searchAggregatePtr.media != nullptr) {
for(medialibrary::MediaPtr const& media : searchAggregatePtr.media->all()) {
if (media->subType() == medialibrary::IMedia::SubType::AlbumTrack) tracks.push_back(media);
else videos.push_back(media);
}
videoList = (jobjectArray) env->NewObjectArray(videos.size(), fields->MediaWrapper.clazz, NULL);
index = -1;
for(medialibrary::MediaPtr const& media : videos) {
jobject item = mediaToMediaWrapper(env, fields, media);
env->SetObjectArrayElement(videoList, ++index, item);
env->DeleteLocalRef(item);
}
tracksList = (jobjectArray) env->NewObjectArray(tracks.size(), fields->MediaWrapper.clazz, NULL);
index = -1;
for(medialibrary::MediaPtr const& media : tracks) {
jobject item = mediaToMediaWrapper(env, fields, media);
env->SetObjectArrayElement(tracksList, ++index, item);
env->DeleteLocalRef(item);
}
}
return env->NewObject(fields->SearchAggregate.clazz, fields->SearchAggregate.initID,
albums, artists, genres, videoList, tracksList, playlists);
}
......
package org.videolan.medialibrary.media;
import androidx.annotation.Nullable;
import org.videolan.medialibrary.Tools;
import org.videolan.medialibrary.interfaces.media.AbstractAlbum;
import org.videolan.medialibrary.interfaces.media.AbstractArtist;
......@@ -35,26 +37,32 @@ public class SearchAggregate {
this.playlists = playlists;
}
@Nullable
public AbstractAlbum[] getAlbums() {
return albums;
}
@Nullable
public AbstractArtist[] getArtists() {
return artists;
}
@Nullable
public AbstractGenre[] getGenres() {
return genres;
}
@Nullable
public AbstractMediaWrapper[] getVideos() {
return videos;
}
@Nullable
public AbstractMediaWrapper[] getTracks() {
return tracks;
}
@Nullable
public AbstractPlaylist[] getPlaylists() {
return playlists;
}
......@@ -66,32 +74,32 @@ public class SearchAggregate {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (albums.length > 0) {
if (!Tools.isArrayEmpty(albums)) {
sb.append("Albums:\n");
for (AbstractAlbum album : albums)
sb.append(album.getTitle()).append("\n");
}
if (artists.length > 0) {
if (!Tools.isArrayEmpty(artists)) {
sb.append("Artists:\n");
for (AbstractArtist artist : artists)
sb.append(artist.getTitle()).append("\n");
}
if (genres.length > 0) {
if (!Tools.isArrayEmpty(genres)) {
sb.append("Genres:\n");
for (AbstractGenre genre : genres)
sb.append(genre.getTitle()).append("\n");
}
if (tracks.length > 0) {
if (!Tools.isArrayEmpty(tracks)) {
sb.append("Tracks:\n");
for (AbstractMediaWrapper m : tracks)
sb.append(m.getTitle()).append("\n");
}
if (videos.length > 0) {
if (!Tools.isArrayEmpty(videos)) {
sb.append("Videos:\n");
for (AbstractMediaWrapper m : videos)
sb.append(m.getTitle()).append("\n");
}
if (playlists.length > 0) {
if (!Tools.isArrayEmpty(playlists)) {
sb.append("Playlists:\n");
for (AbstractPlaylist playlist : playlists)
sb.append(playlist.getTitle()).append("\n");
......
......@@ -2,6 +2,7 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android" >
<data>
<import type="android.view.View" />
<import type="org.videolan.medialibrary.Tools" />
<variable
name="searchAggregate"
type="org.videolan.medialibrary.media.SearchAggregate" />
......@@ -70,56 +71,56 @@
android:layout_marginLeft="10dp"
android:text="@string/albums"
android:textAppearance="@style/Result.Title"
android:visibility="@{searchAggregate.albums.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.albums) ? View.GONE : View.VISIBLE}"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/albums_results"
style="@style/Result.List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/background_default"
android:visibility="@{searchAggregate.albums.length == 0 ? View.GONE : View.VISIBLE}" />
android:visibility="@{Tools.isArrayEmpty(searchAggregate.albums) ? View.GONE : View.VISIBLE}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="@style/Result.Title"
android:text="@string/artists"
android:visibility="@{searchAggregate.artists.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.artists) ? View.GONE : View.VISIBLE}"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/artists_results"
style="@style/Result.List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/background_default"
android:visibility="@{searchAggregate.artists.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.artists) ? View.GONE : View.VISIBLE}"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="@style/Result.Title"
android:text="@string/genres"
android:visibility="@{searchAggregate.genres.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.genres) ? View.GONE : View.VISIBLE}"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/genres_results"
style="@style/Result.List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/background_default"
android:visibility="@{searchAggregate.genres.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.genres) ? View.GONE : View.VISIBLE}"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="@style/Result.Title"
android:text="@string/playlists"
android:visibility="@{searchAggregate.playlists.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.playlists) ? View.GONE : View.VISIBLE}"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/playlists_results"
style="@style/Result.List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/background_default"
android:visibility="@{searchAggregate.playlists.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.playlists) ? View.GONE : View.VISIBLE}"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......@@ -154,28 +155,28 @@
android:layout_marginLeft="10dp"
android:textAppearance="@style/Result.Title"
android:text="@string/videos"
android:visibility="@{searchAggregate.videos.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.videos) ? View.GONE : View.VISIBLE}"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/others_results"
style="@style/Result.List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/background_default"
android:visibility="@{searchAggregate.videos.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.videos) ? View.GONE : View.VISIBLE}"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="@style/Result.Title"
android:text="@string/songs"
android:visibility="@{searchAggregate.tracks.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.tracks) ? View.GONE : View.VISIBLE}"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/songs_results"
style="@style/Result.List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/background_default"
android:visibility="@{searchAggregate.tracks.length == 0 ? View.GONE : View.VISIBLE}"/>
android:visibility="@{Tools.isArrayEmpty(searchAggregate.tracks) ? View.GONE : View.VISIBLE}"/>
</LinearLayout>
</ScrollView>
......
......@@ -108,10 +108,10 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
if (!isActive) return@launch
if (tracks.isNullOrEmpty() && items.isNullOrEmpty() && query?.length ?: 0 > 2) playbackService.medialibrary.search(query)?.run {
when {
!albums.isNullOrEmpty() -> tracks = albums[0].tracks
!artists.isNullOrEmpty() -> tracks = artists[0].tracks
!playlists.isNullOrEmpty() -> tracks = playlists[0].tracks
!genres.isNullOrEmpty() -> tracks = genres[0].tracks
!albums.isNullOrEmpty() -> tracks = albums!![0].tracks
!artists.isNullOrEmpty() -> tracks = artists!![0].tracks
!playlists.isNullOrEmpty() -> tracks = playlists!![0].tracks
!genres.isNullOrEmpty() -> tracks = genres!![0].tracks
}
}
if (!isActive) return@launch
......
......@@ -71,12 +71,12 @@ open class SearchActivity : BaseActivity(), TextWatcher, TextView.OnEditorAction
if (!isStarted()) return@launch
binding.searchAggregate = searchAggregate
if (searchAggregate != null) {
(binding.albumsResults.adapter as SearchResultAdapter).add(searchAggregate.albums.filterNotNull().toTypedArray())
(binding.artistsResults.adapter as SearchResultAdapter).add(searchAggregate.artists.filterNotNull().toTypedArray())
(binding.genresResults.adapter as SearchResultAdapter).add(searchAggregate.genres.filterNotNull().toTypedArray())
(binding.playlistsResults.adapter as SearchResultAdapter).add(searchAggregate.playlists.filterNotNull().toTypedArray())
(binding.othersResults.adapter as SearchResultAdapter).add(searchAggregate.videos.filterNotNull().toTypedArray())
(binding.songsResults.adapter as SearchResultAdapter).add(searchAggregate.tracks.filterNotNull().toTypedArray())
searchAggregate.albums?.filterNotNull()?.let { (binding.albumsResults.adapter as SearchResultAdapter).add(it.toTypedArray()) }
searchAggregate.artists?.filterNotNull()?.let { (binding.artistsResults.adapter as SearchResultAdapter).add(it.toTypedArray()) }
searchAggregate.genres?.filterNotNull()?.let { (binding.genresResults.adapter as SearchResultAdapter).add(it.toTypedArray()) }
searchAggregate.playlists?.filterNotNull()?.let { (binding.playlistsResults.adapter as SearchResultAdapter).add(it.toTypedArray()) }
searchAggregate.videos?.filterNotNull()?.let { (binding.othersResults.adapter as SearchResultAdapter).add(it.toTypedArray()) }
searchAggregate.tracks?.filterNotNull()?.let { (binding.songsResults.adapter as SearchResultAdapter).add(it.toTypedArray()) }
}
}
}
......
......@@ -31,16 +31,11 @@ import androidx.leanback.app.SearchSupportFragment
import androidx.leanback.widget.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import org.videolan.medialibrary.Tools
import org.videolan.medialibrary.interfaces.media.AbstractAlbum
import org.videolan.medialibrary.interfaces.media.AbstractArtist
import org.videolan.medialibrary.interfaces.media.AbstractGenre
import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
import org.videolan.medialibrary.media.*
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.tools.coroutineScope
import org.videolan.vlc.R
import org.videolan.vlc.util.getFromMl
import java.util.*
private const val TAG = "SearchFragment"
private const val REQUEST_SPEECH = 1
......@@ -87,22 +82,22 @@ class SearchFragment : SearchSupportFragment(), SearchSupportFragment.SearchResu
val empty = searchAggregate == null || searchAggregate.isEmpty
updateEmtyView(empty)
if (searchAggregate == null || empty) return@launch
val mediaEmpty = empty || (Tools.isArrayEmpty(searchAggregate.tracks) && Tools.isArrayEmpty(searchAggregate.videos))
val mediaEmpty = empty || (searchAggregate.tracks.isNullOrEmpty() && searchAggregate.videos.isNullOrEmpty())
val cp = CardPresenter(requireActivity())
val videoAdapter = ArrayObjectAdapter(cp)
if (!mediaEmpty) videoAdapter.addAll(0, Arrays.asList(*searchAggregate.videos))
if (!mediaEmpty) searchAggregate.videos?.let { videoAdapter.addAll(0, listOf(*it)) }
// val episodesAdapter = ArrayObjectAdapter(cp)
// if (!mediaEmpty) episodesAdapter.addAll(0, Arrays.asList(searchAggregate.mediaSearchAggregate.episodes))
// val moviesAdapter = ArrayObjectAdapter(cp)
// if (!mediaEmpty) moviesAdapter.addAll(0, Arrays.asList(searchAggregate.mediaSearchAggregate.movies))
val songsAdapter = ArrayObjectAdapter(cp)
if (!mediaEmpty) songsAdapter.addAll(0, Arrays.asList(*searchAggregate.tracks))
if (!mediaEmpty) searchAggregate.tracks?.let { songsAdapter.addAll(0, listOf(*it)) }
val artistsAdapter = ArrayObjectAdapter(cp)
if (!empty) artistsAdapter.addAll(0, Arrays.asList<AbstractArtist>(*searchAggregate.artists))
if (!empty) searchAggregate.artists?.let { artistsAdapter.addAll(0, listOf(*it)) }
val albumsAdapter = ArrayObjectAdapter(cp)
if (!empty) albumsAdapter.addAll(0, Arrays.asList<AbstractAlbum>(*searchAggregate.albums))
if (!empty) searchAggregate.albums?.let { albumsAdapter.addAll(0, listOf(*it)) }
val genresAdapter = ArrayObjectAdapter(cp)
if (!empty) genresAdapter.addAll(0, Arrays.asList<AbstractGenre>(*searchAggregate.genres))
if (!empty) searchAggregate.genres?.let { genresAdapter.addAll(0, listOf(*it)) }
if (!mediaEmpty && videoAdapter.size() > 0)
rowsAdapter.add(ListRow(HeaderItem(0, resources.getString(R.string.videos)), videoAdapter))
// if (!mediaEmpty && episodesAdapter.size() > 0)
......
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