1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.tvleanback.data; 18 19 import android.content.Context; 20 import android.util.Log; 21 22 import com.example.android.tvleanback.R; 23 import com.example.android.tvleanback.model.Movie; 24 25 import org.json.JSONArray; 26 import org.json.JSONException; 27 import org.json.JSONObject; 28 29 import java.io.BufferedInputStream; 30 import java.io.BufferedReader; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import java.io.InputStreamReader; 34 import java.net.URLConnection; 35 import java.util.ArrayList; 36 import java.util.HashMap; 37 import java.util.List; 38 39 /* 40 * This class loads videos from a backend and saves them into a HashMap 41 */ 42 public class VideoProvider { 43 44 private static final String TAG = "VideoProvider"; 45 private static String TAG_MEDIA = "videos"; 46 private static String TAG_GOOGLE_VIDEOS = "googlevideos"; 47 private static String TAG_CATEGORY = "category"; 48 private static String TAG_STUDIO = "studio"; 49 private static String TAG_SOURCES = "sources"; 50 private static String TAG_DESCRIPTION = "description"; 51 private static String TAG_CARD_THUMB = "card"; 52 private static String TAG_BACKGROUND = "background"; 53 private static String TAG_TITLE = "title"; 54 55 private static HashMap<String, List<Movie>> sMovieList; 56 private static Context sContext; 57 private static String sPrefixUrl; 58 59 public static void setContext(Context context) { 60 if (sContext == null) 61 sContext = context; 62 } 63 64 public static HashMap<String, List<Movie>> getMovieList() { 65 return sMovieList; 66 } 67 68 public static HashMap<String, List<Movie>> buildMedia(Context ctx, String url) 69 throws JSONException { 70 if (null != sMovieList) { 71 return sMovieList; 72 } 73 sMovieList = new HashMap<String, List<Movie>>(); 74 75 JSONObject jsonObj = new VideoProvider().parseUrl(url); 76 JSONArray categories = jsonObj.getJSONArray(TAG_GOOGLE_VIDEOS); 77 if (null != categories) { 78 Log.d(TAG, "category #: " + categories.length()); 79 String title = new String(); 80 String videoUrl = new String(); 81 String bgImageUrl = new String(); 82 String cardImageUrl = new String(); 83 String studio = new String(); 84 for (int i = 0; i < categories.length(); i++) { 85 JSONObject category = categories.getJSONObject(i); 86 String category_name = category.getString(TAG_CATEGORY); 87 JSONArray videos = category.getJSONArray(TAG_MEDIA); 88 Log.d(TAG, 89 "category: " + i + " Name:" + category_name + " video length: " 90 + videos.length()); 91 List<Movie> categoryList = new ArrayList<Movie>(); 92 if (null != videos) { 93 for (int j = 0; j < videos.length(); j++) { 94 JSONObject video = videos.getJSONObject(j); 95 String description = video.getString(TAG_DESCRIPTION); 96 JSONArray videoUrls = video.getJSONArray(TAG_SOURCES); 97 if (null == videoUrls || videoUrls.length() == 0) { 98 continue; 99 } 100 title = video.getString(TAG_TITLE); 101 videoUrl = getVideoPrefix(category_name, videoUrls.getString(0)); 102 bgImageUrl = getThumbPrefix(category_name, title, 103 video.getString(TAG_BACKGROUND)); 104 cardImageUrl = getThumbPrefix(category_name, title, 105 video.getString(TAG_CARD_THUMB)); 106 studio = video.getString(TAG_STUDIO); 107 categoryList.add(buildMovieInfo(category_name, title, description, studio, 108 videoUrl, cardImageUrl, 109 bgImageUrl)); 110 } 111 sMovieList.put(category_name, categoryList); 112 } 113 } 114 } 115 return sMovieList; 116 } 117 118 private static Movie buildMovieInfo(String category, 119 String title, 120 String description, 121 String studio, 122 String videoUrl, 123 String cardImageUrl, 124 String bgImageUrl) { 125 Movie movie = new Movie(); 126 movie.setId(Movie.getCount()); 127 Movie.incrementCount(); 128 movie.setTitle(title); 129 movie.setDescription(description); 130 movie.setStudio(studio); 131 movie.setCategory(category); 132 movie.setCardImageUrl(cardImageUrl); 133 movie.setBackgroundImageUrl(bgImageUrl); 134 movie.setVideoUrl(videoUrl); 135 136 return movie; 137 } 138 139 private static String getVideoPrefix(String category, String videoUrl) { 140 String ret = ""; 141 ret = sPrefixUrl + category.replace(" ", "%20") + '/' + 142 videoUrl.replace(" ", "%20"); 143 return ret; 144 } 145 146 private static String getThumbPrefix(String category, String title, String imageUrl) { 147 String ret = ""; 148 149 ret = sPrefixUrl + category.replace(" ", "%20") + '/' + 150 title.replace(" ", "%20") + '/' + 151 imageUrl.replace(" ", "%20"); 152 return ret; 153 } 154 155 protected JSONObject parseUrl(String urlString) { 156 Log.d(TAG, "Parse URL: " + urlString); 157 InputStream is = null; 158 159 sPrefixUrl = sContext.getResources().getString(R.string.prefix_url); 160 161 try { 162 java.net.URL url = new java.net.URL(urlString); 163 URLConnection urlConnection = url.openConnection(); 164 is = new BufferedInputStream(urlConnection.getInputStream()); 165 BufferedReader reader = new BufferedReader(new InputStreamReader( 166 urlConnection.getInputStream(), "iso-8859-1"), 8); 167 StringBuilder sb = new StringBuilder(); 168 String line = null; 169 while ((line = reader.readLine()) != null) { 170 sb.append(line); 171 } 172 String json = sb.toString(); 173 return new JSONObject(json); 174 } catch (Exception e) { 175 Log.d(TAG, "Failed to parse the json for media list", e); 176 return null; 177 } finally { 178 if (null != is) { 179 try { 180 is.close(); 181 } catch (IOException e) { 182 Log.d(TAG, "JSON feed closed", e); 183 } 184 } 185 } 186 } 187 } 188