• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.support.annotation.VisibleForTesting;
21 import android.text.TextUtils;
22 import android.util.Log;
23 import com.android.tv.common.BuildConfig;
24 import com.android.tv.common.experiments.Experiments;
25 import com.android.tv.common.flags.CloudEpgFlags;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31 
32 /** Checks if a package or a input is white listed. */
33 public final class EpgInputWhiteList {
34     private static final boolean DEBUG = false;
35     private static final String TAG = "EpgInputWhiteList";
36     private static final String QA_DEV_INPUTS =
37             "com.example.partnersupportsampletvinput/.SampleTvInputService,"
38                     + "com.android.tv.tuner.sample.dvb/.tvinput.SampleDvbTunerTvInputService";
39 
40     /** Returns the package portion of a inputId */
41     @Nullable
getPackageFromInput(@ullable String inputId)42     public static String getPackageFromInput(@Nullable String inputId) {
43         return inputId == null ? null : inputId.substring(0, inputId.indexOf("/"));
44     }
45 
46     private final CloudEpgFlags cloudEpgFlags;
47 
EpgInputWhiteList(CloudEpgFlags cloudEpgFlags)48     public EpgInputWhiteList(CloudEpgFlags cloudEpgFlags) {
49         this.cloudEpgFlags = cloudEpgFlags;
50     }
51 
isInputWhiteListed(String inputId)52     public boolean isInputWhiteListed(String inputId) {
53         return getWhiteListedInputs().contains(inputId);
54     }
55 
isPackageWhiteListed(String packageName)56     public boolean isPackageWhiteListed(String packageName) {
57         if (DEBUG) Log.d(TAG, "isPackageWhiteListed " + packageName);
58         Set<String> whiteList = getWhiteListedInputs();
59         for (String good : whiteList) {
60             try {
61                 String goodPackage = getPackageFromInput(good);
62                 if (goodPackage.equals(packageName)) {
63                     return true;
64                 }
65             } catch (Exception e) {
66                 if (DEBUG) Log.d(TAG, "Error parsing package name of " + good, e);
67                 continue;
68             }
69         }
70         return false;
71     }
72 
getWhiteListedInputs()73     private Set<String> getWhiteListedInputs() {
74         Set<String> result = toInputSet(cloudEpgFlags.thirdPartyEpgInputsCsv());
75         if (BuildConfig.ENG || Experiments.ENABLE_QA_FEATURES.get()) {
76             HashSet<String> moreInputs = new HashSet<>(toInputSet(QA_DEV_INPUTS));
77             if (result.isEmpty()) {
78                 result = moreInputs;
79             } else {
80                 result.addAll(moreInputs);
81             }
82         }
83         if (DEBUG) Log.d(TAG, "getWhiteListedInputs " + result);
84         return result;
85     }
86 
87     @VisibleForTesting
toInputSet(String value)88     static Set<String> toInputSet(String value) {
89         if (TextUtils.isEmpty(value)) {
90             return Collections.emptySet();
91         }
92         List<String> strings = Arrays.asList(value.split(","));
93         Set<String> result = new HashSet<>(strings.size());
94         for (String s : strings) {
95             String trimmed = s.trim();
96             if (!TextUtils.isEmpty(trimmed)) {
97                 result.add(trimmed);
98             }
99         }
100         return result;
101     }
102 }
103