Home | History | Annotate | Download | only in feature
      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.util.CommonUtils;
     21 import java.util.Arrays;
     22 
     23 /** Static utilities for features. */
     24 public class FeatureUtils {
     25 
     26     /**
     27      * Returns a feature that is enabled if any of {@code features} is enabled.
     28      *
     29      * @param features the features to or
     30      */
     31     public static Feature OR(final Feature... features) {
     32         return new Feature() {
     33             @Override
     34             public boolean isEnabled(Context context) {
     35                 for (Feature f : features) {
     36                     if (f.isEnabled(context)) {
     37                         return true;
     38                     }
     39                 }
     40                 return false;
     41             }
     42 
     43             @Override
     44             public String toString() {
     45                 return "or(" + Arrays.asList(features) + ")";
     46             }
     47         };
     48     }
     49 
     50     /**
     51      * Returns a feature that is enabled if all of {@code features} is enabled.
     52      *
     53      * @param features the features to and
     54      */
     55     public static Feature AND(final Feature... features) {
     56         return new Feature() {
     57             @Override
     58             public boolean isEnabled(Context context) {
     59                 for (Feature f : features) {
     60                     if (!f.isEnabled(context)) {
     61                         return false;
     62                     }
     63                 }
     64                 return true;
     65             }
     66 
     67             @Override
     68             public String toString() {
     69                 return "and(" + Arrays.asList(features) + ")";
     70             }
     71         };
     72     }
     73 
     74     /** A feature that is always enabled. */
     75     public static final Feature ON =
     76             new Feature() {
     77                 @Override
     78                 public boolean isEnabled(Context context) {
     79                     return true;
     80                 }
     81 
     82                 @Override
     83                 public String toString() {
     84                     return "on";
     85                 }
     86             };
     87 
     88     /** A feature that is always disabled. */
     89     public static final Feature OFF =
     90             new Feature() {
     91                 @Override
     92                 public boolean isEnabled(Context context) {
     93                     return false;
     94                 }
     95 
     96                 @Override
     97                 public String toString() {
     98                     return "off";
     99                 }
    100             };
    101 
    102     /** True if running in robolectric. */
    103     public static final Feature ROBOLECTRIC =
    104             new Feature() {
    105                 @Override
    106                 public boolean isEnabled(Context context) {
    107                     return CommonUtils.isRoboTest();
    108                 }
    109 
    110                 @Override
    111                 public String toString() {
    112                     return "isRobolecteric";
    113                 }
    114             };
    115 
    116     private FeatureUtils() {}
    117 }
    118