Home | History | Annotate | Download | only in leanback
      1 /*
      2  * Copyright (C) 2013 Google Inc. All Rights Reserved.
      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.leanback;
     18 
     19 import android.content.Context;
     20 import android.util.Log;
     21 
     22 import org.json.JSONArray;
     23 import org.json.JSONException;
     24 import org.json.JSONObject;
     25 
     26 import java.io.BufferedInputStream;
     27 import java.io.BufferedReader;
     28 import java.io.IOException;
     29 import java.io.InputStream;
     30 import java.io.InputStreamReader;
     31 import java.net.URLConnection;
     32 import java.util.ArrayList;
     33 import java.util.HashMap;
     34 import java.util.List;
     35 
     36 /*
     37  * This class loads videos from a backend and saves them into a HashMap
     38  */
     39 public class VideoProvider {
     40 
     41     private static final String TAG = "VideoProvider";
     42     private static String TAG_MEDIA = "videos";
     43     private static String TAG_GOOGLE_VIDEOS = "googlevideos";
     44     private static String TAG_CATEGORY = "category";
     45     private static String TAG_STUDIO = "studio";
     46     private static String TAG_SOURCES = "sources";
     47     private static String TAG_DESCRIPTION = "description";
     48     private static String TAG_CARD_THUMB = "card";
     49     private static String TAG_BACKGROUND = "background";
     50     private static String TAG_TITLE = "title";
     51 
     52     private static HashMap<String, List<Movie>> mMovieList;
     53     private static Context mContext;
     54     private static String mPrefixUrl;
     55 
     56     public static void setContext(Context context) {
     57         if (mContext == null)
     58             mContext = context;
     59     }
     60 
     61     protected JSONObject parseUrl(String urlString) {
     62         Log.d(TAG, "Parse URL: " + urlString);
     63         InputStream is = null;
     64 
     65         mPrefixUrl = mContext.getResources().getString(R.string.prefix_url);
     66 
     67         try {
     68             java.net.URL url = new java.net.URL(urlString);
     69             URLConnection urlConnection = url.openConnection();
     70             is = new BufferedInputStream(urlConnection.getInputStream());
     71             BufferedReader reader = new BufferedReader(new InputStreamReader(
     72                     urlConnection.getInputStream(), "iso-8859-1"), 8);
     73             StringBuilder sb = new StringBuilder();
     74             String line = null;
     75             while ((line = reader.readLine()) != null) {
     76                 sb.append(line);
     77             }
     78             String json = sb.toString();
     79             return new JSONObject(json);
     80         } catch (Exception e) {
     81             Log.d(TAG, "Failed to parse the json for media list", e);
     82             return null;
     83         } finally {
     84             if (null != is) {
     85                 try {
     86                     is.close();
     87                 } catch (IOException e) {
     88                     Log.d(TAG, "JSON feed closed", e);
     89                 }
     90             }
     91         }
     92     }
     93 
     94     public static HashMap<String, List<Movie>> getMovieList() {
     95         return mMovieList;
     96     }
     97 
     98     public static HashMap<String, List<Movie>> buildMedia(Context ctx, String url)
     99             throws JSONException {
    100         if (null != mMovieList) {
    101             return mMovieList;
    102         }
    103         mMovieList = new HashMap<String, List<Movie>>();
    104 
    105         JSONObject jsonObj = new VideoProvider().parseUrl(url);
    106         JSONArray categories = jsonObj.getJSONArray(TAG_GOOGLE_VIDEOS);
    107         if (null != categories) {
    108             Log.d(TAG, "category #: " + categories.length());
    109             String title = new String();
    110             String videoUrl = new String();
    111             String bgImageUrl = new String();
    112             String cardImageUrl = new String();
    113             String studio = new String();
    114             for (int i = 0; i < categories.length(); i++) {
    115                 JSONObject category = categories.getJSONObject(i);
    116                 String category_name = category.getString(TAG_CATEGORY);
    117                 JSONArray videos = category.getJSONArray(TAG_MEDIA);
    118                 Log.d(TAG,
    119                         "category: " + i + " Name:" + category_name + " video length: "
    120                                 + videos.length());
    121                 List<Movie> categoryList = new ArrayList<Movie>();
    122                 if (null != videos) {
    123                     for (int j = 0; j < videos.length(); j++) {
    124                         JSONObject video = videos.getJSONObject(j);
    125                         String description = video.getString(TAG_DESCRIPTION);
    126                         JSONArray videoUrls = video.getJSONArray(TAG_SOURCES);
    127                         if (null == videoUrls || videoUrls.length() == 0) {
    128                             continue;
    129                         }
    130                         title = video.getString(TAG_TITLE);
    131                         videoUrl = getVideoPrefix(category_name, videoUrls.getString(0));
    132                         bgImageUrl = getThumbPrefix(category_name, title,
    133                                 video.getString(TAG_BACKGROUND));
    134                         cardImageUrl = getThumbPrefix(category_name, title,
    135                                 video.getString(TAG_CARD_THUMB));
    136                         studio = video.getString(TAG_STUDIO);
    137                         categoryList.add(buildMovieInfo(category_name, title, description, studio,
    138                                 videoUrl, cardImageUrl,
    139                                 bgImageUrl));
    140                     }
    141                     mMovieList.put(category_name, categoryList);
    142                 }
    143             }
    144         }
    145         return mMovieList;
    146     }
    147 
    148     private static Movie buildMovieInfo(String category, String title,
    149             String description, String studio, String videoUrl, String cardImageUrl,
    150             String bgImageUrl) {
    151         Movie movie = new Movie();
    152         movie.setId(Movie.getCount());
    153         Movie.incCount();
    154         movie.setTitle(title);
    155         movie.setDescription(description);
    156         movie.setStudio(studio);
    157         movie.setCategory(category);
    158         movie.setCardImageUrl(cardImageUrl);
    159         movie.setBackgroundImageUrl(bgImageUrl);
    160         movie.setVideoUrl(videoUrl);
    161 
    162         return movie;
    163     }
    164 
    165     private static String getVideoPrefix(String category, String videoUrl) {
    166         String ret = "";
    167         ret = mPrefixUrl + category.replace(" ", "%20") + '/' +
    168                 videoUrl.replace(" ", "%20");
    169         return ret;
    170     }
    171 
    172     private static String getThumbPrefix(String category, String title, String imageUrl) {
    173         String ret = "";
    174 
    175         ret = mPrefixUrl + category.replace(" ", "%20") + '/' +
    176                 title.replace(" ", "%20") + '/' +
    177                 imageUrl.replace(" ", "%20");
    178         return ret;
    179     }
    180 }
    181