• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.common.feature;
18 
19 import android.content.Context;
20 import com.android.tv.common.BuildConfig;
21 import com.android.tv.common.util.CommonUtils;
22 import java.util.Arrays;
23 
24 /** Static utilities for features. */
25 public class FeatureUtils {
26 
27     /**
28      * Returns a feature that is enabled if any of {@code features} is enabled.
29      *
30      * @param features the features to or
31      */
or(final Feature... features)32     public static Feature or(final Feature... features) {
33         return new Feature() {
34             @Override
35             public boolean isEnabled(Context context) {
36                 for (Feature f : features) {
37                     if (f.isEnabled(context)) {
38                         return true;
39                     }
40                 }
41                 return false;
42             }
43 
44             @Override
45             public String toString() {
46                 return "or(" + Arrays.asList(features) + ")";
47             }
48         };
49     }
50 
51     /**
52      * Returns a feature that is enabled if all of {@code features} is enabled.
53      *
54      * @param features the features to and
55      */
56     public static Feature and(final Feature... features) {
57         return new Feature() {
58             @Override
59             public boolean isEnabled(Context context) {
60                 for (Feature f : features) {
61                     if (!f.isEnabled(context)) {
62                         return false;
63                     }
64                 }
65                 return true;
66             }
67 
68             @Override
69             public String toString() {
70                 return "and(" + Arrays.asList(features) + ")";
71             }
72         };
73     }
74     /**
75      * A feature available in AOSP.
76      *
77      * @param googleFeature the feature used in non AOSP builds
78      * @param aospFeature the feature used in AOSP builds
79      */
80     public static Feature aospFeature(
81 // AOSP_Comment_Out             final Feature googleFeature,
82             final Feature aospFeature) {
83         /* Begin_AOSP_Comment_Out
84         if (!BuildConfig.AOSP) {
85             return googleFeature;
86         } else {
87             End_AOSP_Comment_Out */
88             return aospFeature;
89 // AOSP_Comment_Out         }
90     }
91 
92     /**
93      * Returns a feature that is opposite of the given {@code feature}.
94      *
95      * @param feature the feature to invert
96      */
97     public static Feature not(final Feature feature) {
98         return new Feature() {
99             @Override
100             public boolean isEnabled(Context context) {
101                 return !feature.isEnabled(context);
102             }
103 
104             @Override
105             public String toString() {
106                 return "not(" + feature + ")";
107             }
108         };
109     }
110 
111     /** A feature that is always enabled. */
112     public static final Feature ON =
113             new Feature() {
114                 @Override
115                 public boolean isEnabled(Context context) {
116                     return true;
117                 }
118 
119                 @Override
120                 public String toString() {
121                     return "on";
122                 }
123             };
124 
125     /** A feature that is always disabled. */
126     public static final Feature OFF =
127             new Feature() {
128                 @Override
129                 public boolean isEnabled(Context context) {
130                     return false;
131                 }
132 
133                 @Override
134                 public String toString() {
135                     return "off";
136                 }
137             };
138 
139     /** True if running in robolectric. */
140     public static final Feature ROBOLECTRIC =
141             new Feature() {
142                 @Override
143                 public boolean isEnabled(Context context) {
144                     return CommonUtils.isRoboTest();
145                 }
146 
147                 @Override
148                 public String toString() {
149                     return "isRobolecteric";
150                 }
151             };
152 
153     private FeatureUtils() {}
154 }
155