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 */
16package android.support.media.tv;
17
18import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
19
20import android.media.tv.TvContentRating;
21import android.support.annotation.RestrictTo;
22import android.text.TextUtils;
23
24/**
25 * Static helper methods for working with {@link android.media.tv.TvContract}.
26 * @hide
27 */
28@RestrictTo(LIBRARY_GROUP)
29public class TvContractUtils {
30
31    private static final String TAG = "TvContractUtils";
32    private static final boolean DEBUG = false;
33    private static final String DELIMITER = ",";
34
35    /**
36     * Parses a string of comma-separated ratings into an array of {@link TvContentRating}.
37     *
38     * @param commaSeparatedRatings String containing various ratings, separated by commas.
39     * @return An array of TvContentRatings.
40     */
41    public static TvContentRating[] stringToContentRatings(String commaSeparatedRatings) {
42        if (TextUtils.isEmpty(commaSeparatedRatings)) {
43            return null;
44        }
45        String[] ratings = commaSeparatedRatings.split("\\s*,\\s*");
46        TvContentRating[] contentRatings = new TvContentRating[ratings.length];
47        for (int i = 0; i < contentRatings.length; ++i) {
48            contentRatings[i] = TvContentRating.unflattenFromString(ratings[i]);
49        }
50        return contentRatings;
51    }
52
53    /**
54     * Flattens an array of {@link TvContentRating} into a String to be inserted into a database.
55     *
56     * @param contentRatings An array of TvContentRatings.
57     * @return A comma-separated String of ratings.
58     */
59    public static String contentRatingsToString(TvContentRating[] contentRatings) {
60        if (contentRatings == null || contentRatings.length == 0) {
61            return null;
62        }
63        StringBuilder ratings = new StringBuilder(contentRatings[0].flattenToString());
64        for (int i = 1; i < contentRatings.length; ++i) {
65            ratings.append(DELIMITER);
66            ratings.append(contentRatings[i].flattenToString());
67        }
68        return ratings.toString();
69    }
70
71    /**
72     * Parses a string of comma-separated audio languages into an array of audio language strings.
73     *
74     * @param commaSeparatedString String containing audio languages, separated by commas.
75     * @return An array of audio language.
76     */
77    public static String[] stringToAudioLanguages(String commaSeparatedString) {
78        if (TextUtils.isEmpty(commaSeparatedString)) {
79            return null;
80        }
81        return commaSeparatedString.split("\\s*,\\s*");
82    }
83
84    /**
85     * Concatenate an array of audio languages into a String to be inserted into a database.
86     *
87     * @param audioLanguages An array of audio languages.
88     * @return A comma-separated String of audio languages.
89     */
90    public static String audioLanguagesToString(String[] audioLanguages) {
91        if (audioLanguages == null || audioLanguages.length == 0) {
92            return null;
93        }
94        StringBuilder ratings = new StringBuilder(audioLanguages[0]);
95        for (int i = 1; i < audioLanguages.length; ++i) {
96            ratings.append(DELIMITER);
97            ratings.append(audioLanguages[i]);
98        }
99        return ratings.toString();
100    }
101
102    private TvContractUtils() {
103    }
104}
105