diff options
author | Kai Sommerfeld <kai.sommerfeld@gmx.com> | 2022-07-05 22:42:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-05 22:42:03 +0200 |
commit | 93cc06098f33591fd18fa162a9279724837b839b (patch) | |
tree | 8f1032286b164f29b48269a11c8d2349e49a5d90 | |
parent | 05a60a2cb3a9587490e5fd588dccf1f06a68883a (diff) | |
parent | f6958857fdbf428d14b22bc456c09faaee7467d3 (diff) |
Merge pull request #21616 from joseluismarti/max-recommendations
[Android] Distribute suggestion channel items according to available content
-rw-r--r-- | tools/android/packaging/xbmc/src/XBMCJsonRPC.java.in | 162 |
1 files changed, 99 insertions, 63 deletions
diff --git a/tools/android/packaging/xbmc/src/XBMCJsonRPC.java.in b/tools/android/packaging/xbmc/src/XBMCJsonRPC.java.in index 90b982c5af..a298561a2d 100644 --- a/tools/android/packaging/xbmc/src/XBMCJsonRPC.java.in +++ b/tools/android/packaging/xbmc/src/XBMCJsonRPC.java.in @@ -1,8 +1,9 @@ package @APP_PACKAGE@; import java.io.InputStream; -import java.util.List; import java.util.ArrayList; +import java.util.List; +import java.util.HashSet; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -62,7 +63,7 @@ public class XBMCJsonRPC private static String TAG = "@APP_NAME@json"; private String m_xbmc_web_url = "http://localhost:8080"; - private java.util.HashSet<Integer> mRecomendationIds = new java.util.HashSet<Integer>(); + private HashSet<Integer> mRecomendationIds = new HashSet<Integer>(); private int MAX_RECOMMENDATIONS = 3; @@ -1181,7 +1182,11 @@ public class XBMCJsonRPC { List<Media> medias = new ArrayList<Media>(); + // count the number of categories with data + int categories = 0; + JsonObject rep = request_object(RECOMMENDATION_MOVIES_JSON); + JsonArray movies = new JsonArray(); if (rep != null && rep.has("result")) { try @@ -1189,34 +1194,18 @@ public class XBMCJsonRPC JsonObject results = rep.getAsJsonObject("result"); if (results.has("movies")) { - JsonArray movies = results.get("movies").getAsJsonArray(); - - int count = 0; - for (int i = 0; i < movies.size() && count < MAX_RECOMMENDATIONS; ++i) + movies = results.get("movies").getAsJsonArray(); + if (movies.size() > 0) { - try - { - JsonObject details = movies.get(i).getAsJsonObject(); - Movie med = createMovieFromJson(details); - if (med != null) - { - medias.add(med); - ++count; - } - } - catch (Exception e) - { - continue; - } + ++categories; } } - } catch (Exception e) - { - e.printStackTrace(); + } catch (Exception e) { } } rep = request_object(RECOMMENDATIONS_SHOWS_JSON); + JsonArray tvshows = new JsonArray(); if (rep != null && rep.has("result")) { try @@ -1224,34 +1213,18 @@ public class XBMCJsonRPC JsonObject results = rep.getAsJsonObject("result"); if (results.has("tvshows")) { - JsonArray tvshows = results.get("tvshows").getAsJsonArray(); - - int count = 0; - for (int i = 0; i < tvshows.size() && count < MAX_RECOMMENDATIONS; ++i) + tvshows = results.get("tvshows").getAsJsonArray(); + if (tvshows.size() > 0) { - try - { - JsonObject tvshow = tvshows.get(i).getAsJsonObject(); - TVShow med = createTVShowFromJson(tvshow); - if (med != null) - { - medias.add(med); - ++count; - } - } - catch (Exception e) - { - continue; - } + ++categories; } } - } catch (Exception e) - { - e.printStackTrace(); + } catch (Exception e) { } } rep = request_object(RECOMMENDATIONS_ALBUMS_JSON); + JsonArray albums = new JsonArray(); if (rep != null && rep.has("result")) { try @@ -1259,30 +1232,93 @@ public class XBMCJsonRPC JsonObject results = rep.getAsJsonObject("result"); if (results.has("albums")) { - JsonArray albums = results.get("albums").getAsJsonArray(); + albums = results.get("albums").getAsJsonArray(); + if (albums.size() > 0) + { + ++categories; + } + } + } catch (Exception e) { + } + } + + // distribution by the number of categories with data + if (categories == 1) + { + MAX_RECOMMENDATIONS = 10; // 1x10 items + } + else if (categories == 2) + { + MAX_RECOMMENDATIONS = 5; // 2x5 items + } + else + { + MAX_RECOMMENDATIONS = 3; // 3x3 items + } + + if (movies.size() > 0) + { + int count = 0; + for (int i = 0; i < movies.size() && count < MAX_RECOMMENDATIONS; ++i) + { + try + { + JsonObject details = movies.get(i).getAsJsonObject(); + Movie med = createMovieFromJson(details); + if (med != null) + { + medias.add(med); + ++count; + } + } + catch (Exception e) + { + continue; + } + } + } - int count = 0; - for (int i = 0; i < albums.size() && count < MAX_RECOMMENDATIONS; ++i) + if (tvshows.size() > 0) + { + int count = 0; + for (int i = 0; i < tvshows.size() && count < MAX_RECOMMENDATIONS; ++i) + { + try + { + JsonObject tvshow = tvshows.get(i).getAsJsonObject(); + TVShow med = createTVShowFromJson(tvshow); + if (med != null) { - try - { - JsonObject album = albums.get(i).getAsJsonObject(); - Album med = createAlbumFromJson(album); - if (med != null) - { - medias.add(med); - ++count; - } - } - catch (Exception e) - { - continue; - } + medias.add(med); + ++count; } } - } catch (Exception e) + catch (Exception e) + { + continue; + } + } + } + + if (albums.size() > 0) + { + int count = 0; + for (int i = 0; i < albums.size() && count < MAX_RECOMMENDATIONS; ++i) { - e.printStackTrace(); + try + { + JsonObject album = albums.get(i).getAsJsonObject(); + Album med = createAlbumFromJson(album); + if (med != null) + { + medias.add(med); + ++count; + } + } + catch (Exception e) + { + continue; + } } } |