Home | History | Annotate | Download | only in rich
      1 /*
      2  * Copyright 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.sampletvinput.rich;
     18 
     19 import android.content.ContentResolver;
     20 import android.content.Context;
     21 import android.net.Uri;
     22 import android.util.Log;
     23 import android.util.Xml;
     24 
     25 import com.example.android.sampletvinput.R;
     26 import com.example.android.sampletvinput.rich.RichTvInputService.ChannelInfo;
     27 import com.example.android.sampletvinput.rich.RichTvInputService.TvInput;
     28 
     29 import org.xmlpull.v1.XmlPullParser;
     30 import org.xmlpull.v1.XmlPullParserException;
     31 
     32 import java.io.IOException;
     33 import java.io.InputStream;
     34 import java.net.URL;
     35 import java.net.URLConnection;
     36 import java.util.List;
     37 
     38 /**
     39  * Static helper methods for fetching the channel feed.
     40  */
     41 public class RichFeedUtil {
     42     private static final String TAG = "RichFeedUtil";
     43     private static List<ChannelInfo> sSampleChannels;
     44     private static TvInput sTvInput;
     45 
     46     private static final boolean USE_LOCAL_XML_FEED = false;
     47 
     48     private RichFeedUtil() {
     49     }
     50 
     51     /**
     52      * Returns the channel metadata for {@link RichTvInputService}. Note that this will block until
     53      * the channel feed has been retrieved.
     54      */
     55     public static List<ChannelInfo> getRichChannels(Context context) {
     56         Uri catalogUri =
     57                 USE_LOCAL_XML_FEED ?
     58                         Uri.parse("android.resource://" + context.getPackageName() + "/"
     59                                 + R.raw.rich_tv_inputs_tif)
     60                         : Uri.parse(context.getResources().getString(R.string.rich_input_feed_url));
     61         if (sSampleChannels != null) {
     62             return sSampleChannels;
     63         }
     64 
     65         InputStream inputStream = null;
     66         try {
     67             if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(catalogUri.getScheme())) {
     68                 inputStream = context.getContentResolver().openInputStream(catalogUri);
     69             } else {
     70                 URLConnection urlConnection = new URL(catalogUri.toString()).openConnection();
     71                 inputStream = urlConnection.getInputStream();
     72             }
     73 
     74             XmlPullParser parser = Xml.newPullParser();
     75             parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
     76             parser.setInput(inputStream, null);
     77             sTvInput = ChannelXMLParser.parseTvInput(parser);
     78             sSampleChannels = ChannelXMLParser.parseChannelXML(parser);
     79         } catch (IOException e) {
     80             Log.e(TAG, "Error in fetching " + catalogUri, e);
     81         } catch (XmlPullParserException e) {
     82             Log.e(TAG, "Error in parsing " + catalogUri, e);
     83         } finally {
     84             if (inputStream != null) {
     85                 try {
     86                     inputStream.close();
     87                 } catch (IOException e) {
     88                     // Do nothing.
     89                 }
     90             }
     91         }
     92         return sSampleChannels;
     93     }
     94 
     95     public static TvInput getTvInput(Context context) {
     96         if (sTvInput == null) {
     97             getRichChannels(context);
     98         }
     99         return sTvInput;
    100     }
    101 }
    102