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.android.tv.data.epg; 18 19 import android.support.annotation.Nullable; 20 import android.text.TextUtils; 21 import android.util.Log; 22 23 import com.android.tv.common.BuildConfig; 24 25 import com.google.common.collect.ImmutableSet; 26 27 import com.android.tv.common.flags.CloudEpgFlags; 28 import com.android.tv.common.flags.LegacyFlags; 29 30 import java.util.List; 31 32 import javax.inject.Inject; 33 34 /** Checks if a package or a input is white listed. */ 35 public final class EpgInputWhiteList { 36 private static final boolean DEBUG = false; 37 private static final String TAG = "EpgInputWhiteList"; 38 private static final ImmutableSet<String> QA_DEV_INPUTS = 39 ImmutableSet.of( 40 "com.example.partnersupportsampletvinput/.SampleTvInputService", 41 "com.android.tv.tuner.sample.dvb/.tvinput.SampleDvbTunerTvInputService"); 42 private final LegacyFlags mLegacyFlags; 43 44 /** Returns the package portion of a inputId */ 45 @Nullable getPackageFromInput(@ullable String inputId)46 public static String getPackageFromInput(@Nullable String inputId) { 47 return inputId == null ? null : inputId.substring(0, inputId.indexOf("/")); 48 } 49 50 private final CloudEpgFlags mCloudEpgFlags; 51 52 @Inject EpgInputWhiteList(CloudEpgFlags cloudEpgFlags, LegacyFlags legacyFlags)53 public EpgInputWhiteList(CloudEpgFlags cloudEpgFlags, LegacyFlags legacyFlags) { 54 mCloudEpgFlags = cloudEpgFlags; 55 mLegacyFlags = legacyFlags; 56 } 57 isInputWhiteListed(String inputId)58 public boolean isInputWhiteListed(String inputId) { 59 return getWhiteListedInputs().contains(inputId); 60 } 61 isPackageWhiteListed(String packageName)62 public boolean isPackageWhiteListed(String packageName) { 63 if (DEBUG) Log.d(TAG, "isPackageWhiteListed " + packageName); 64 ImmutableSet<String> whiteList = getWhiteListedInputs(); 65 for (String good : whiteList) { 66 try { 67 String goodPackage = getPackageFromInput(good); 68 if (goodPackage.equals(packageName)) { 69 return true; 70 } 71 } catch (Exception e) { 72 if (DEBUG) Log.d(TAG, "Error parsing package name of " + good, e); 73 continue; 74 } 75 } 76 return false; 77 } 78 getWhiteListedInputs()79 private ImmutableSet<String> getWhiteListedInputs() { 80 ImmutableSet<String> result = 81 toInputSet(mCloudEpgFlags.thirdPartyEpgInputs().getElementList()); 82 if (BuildConfig.ENG || mLegacyFlags.enableQaFeatures()) { 83 if (result.isEmpty()) { 84 result = QA_DEV_INPUTS; 85 } else { 86 result = 87 ImmutableSet.<String>builder().addAll(result).addAll(QA_DEV_INPUTS).build(); 88 } 89 } 90 if (DEBUG) Log.d(TAG, "getWhiteListedInputs " + result); 91 return result; 92 } 93 toInputSet(List<String> strings)94 private static ImmutableSet<String> toInputSet(List<String> strings) { 95 if (strings.isEmpty()) { 96 return ImmutableSet.of(); 97 } 98 ImmutableSet.Builder<String> result = ImmutableSet.builder(); 99 for (String s : strings) { 100 String trimmed = s.trim(); 101 if (!TextUtils.isEmpty(trimmed)) { 102 result.add(trimmed); 103 } 104 } 105 return result.build(); 106 } 107 } 108