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