1 /*
2  * Copyright (C) 2017 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.google.android.tv.partner.support;
18 
19 import android.content.ContentUris;
20 import android.net.Uri;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24 
25 /**
26  * The contract between the EPG provider and applications. Contains definitions for the supported
27  * URIs and columns.
28  *
29  * <h3>Overview</h3>
30  *
31  * <p>
32  *
33  * <p>EpgContract defines a basic database of EPG data for third party inputs. The information is
34  * stored in {@link Lineups} and {@link EpgInputs} tables.
35  *
36  * <p>
37  *
38  * <ul>
39  *   <li>A row in the {@link Lineups} table represents information TV Lineups for postal codes.
40  *   <li>A row in the {@link EpgInputs} table represents partner or 3rd party inputs with the lineup
41  *       that input uses.
42  * </ul>
43  */
44 public final class EpgContract {
45 
46     /** The authority for the EPG provider. */
47     public static final String AUTHORITY = "com.google.android.tv.data.epg";
48 
49     /** The delimiter between channel numbers. */
50     public static final String CHANNEL_NUMBER_DELIMITER = ", ";
51 
52     /**
53      * A constant of the key for intent to indicate whether cloud EPG is used.
54      *
55      * <p>Value type: Boolean
56      */
57     public static final String EXTRA_USE_CLOUD_EPG = "com.google.android.tv.extra.USE_CLOUD_EPG";
58 
59     /**
60      * Returns the list of channels as a CSV string.
61      *
62      * <p>Any commas in the original channels are converted to periods.
63      */
toChannelString(List<String> channels)64     public static String toChannelString(List<String> channels) {
65         // TODO use a StringJoiner if we set the min SDK to N
66         StringBuilder result = new StringBuilder();
67         for (String channel : channels) {
68             channel = channel.replace(",", ".");
69             if (result.length() > 0) {
70                 result.append(CHANNEL_NUMBER_DELIMITER);
71             }
72             result.append(channel);
73         }
74         return result.toString();
75     }
76 
77     /** Returns a list of channels. */
toChannelList(String channelString)78     public static List<String> toChannelList(String channelString) {
79         return channelString == null
80                 ? Collections.EMPTY_LIST
81                 : Arrays.asList(channelString.split(CHANNEL_NUMBER_DELIMITER));
82     }
83 
84     /** Column definitions for the EPG lineups table. */
85     public static final class Lineups {
86 
87         /** Base content:// style URI for all lines in a postal code. */
88         private static final Uri LINEUPS_IN_POSTAL_CODE_URI =
89                 Uri.parse("content://" + AUTHORITY + "/lineups/postal_code");
90 
91         /** The MIME type of a directory of TV channels. */
92         public static final String CONTENT_TYPE = "vnd.android.cursor.dir/lineup";
93 
94         /** The MIME type of a single TV channel. */
95         public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/lineup";
96 
97         /** The content:// style URI all lineups in a postal code. */
uriForPostalCode(String postalCode)98         public static Uri uriForPostalCode(String postalCode) {
99             return LINEUPS_IN_POSTAL_CODE_URI.buildUpon().appendPath(postalCode).build();
100         }
101 
102         /**
103          * The line up id.
104          *
105          * <p>This is a opaque string that should not be parsed.
106          *
107          * <p>Type: TEXT
108          */
109         public static final String COLUMN_ID = "_ID";
110 
111         /**
112          * The line up name that is displayed to the user.
113          *
114          * <p>Type: TEXT
115          */
116         public static final String COLUMN_NAME = "NAME";
117 
118         /**
119          * The channel numbers that are supported by this lineup that is displayed to the user.
120          *
121          * <p>Comma separated value of channel numbers
122          *
123          * <p>Type: TEXT
124          */
125         public static final String COLUMN_CHANNELS = "CHANNELS";
126 
127         /**
128          * The line up type.
129          *
130          * <p>Type: TEXT
131          */
132         public static final String COLUMN_TYPE = "TYPE";
133 
Lineups()134         private Lineups() {}
135     }
136 
137     /** Column definitions for the EPG inputs table. */
138     public static final class EpgInputs {
139 
140         /** The content:// style URI for this table. */
141         public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/epg_input");
142 
143         /** The MIME type of a directory of TV channels. */
144         public static final String CONTENT_TYPE = "vnd.android.cursor.dir/epg_input";
145 
146         /** The MIME type of a single TV channel. */
147         public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/epg_input";
148 
149         /**
150          * Builds a URI that points to a specific input.
151          *
152          * @param rowId The ID of the input to point to.
153          */
buildUri(long rowId)154         public static final Uri buildUri(long rowId) {
155             return ContentUris.withAppendedId(CONTENT_URI, rowId);
156         }
157 
158         /**
159          * The unique ID for a row.
160          *
161          * <p>Type: INTEGER (long)
162          */
163         public static final String COLUMN_ID = "_ID";
164 
165         /**
166          * The name of the package that owns the current row.
167          *
168          * <p>The EPG provider fills in this column with the name of the package that provides the
169          * initial data of the row. If the package is later uninstalled, the rows it owns are
170          * automatically removed from the tables.
171          *
172          * <p>Type: TEXT
173          */
174         public static final String COLUMN_PACKAGE_NAME = "PACKAGE_NAME";
175 
176         /**
177          * The ID of the TV input service that provides this TV channel.
178          *
179          * <p>Use {@link android.media.tv.TvContract#buildInputId} to build the ID.
180          *
181          * <p>This is a required field.
182          *
183          * <p>Type: TEXT
184          */
185         public static final String COLUMN_INPUT_ID = "INPUT_ID";
186         /**
187          * The line up id.
188          *
189          * <p>This is a opaque string that should not be parsed.
190          *
191          * <p>This is a required field.
192          *
193          * <p>Type: TEXT
194          */
195         public static final String COLUMN_LINEUP_ID = "LINEUP_ID";
196 
EpgInputs()197         private EpgInputs() {}
198     }
199 
EpgContract()200     private EpgContract() {}
201 }
202