• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.experiments;
18 
19 import android.support.annotation.VisibleForTesting;
20 import com.android.tv.common.BuildConfig;
21 
22 import com.google.common.base.Supplier;
23 
24 /** Experiments return values based on user, device and other criteria. */
25 public final class ExperimentFlag<T> {
26 
27     // NOTE: sAllowOverrides IS NEVER USED in the non AOSP version.
28     private static boolean sAllowOverrides = false;
29 
30     @VisibleForTesting
initForTest()31     public static void initForTest() {
32         /* Begin_AOSP_Comment_Out
33         if (!BuildConfig.AOSP) {
34             PhenotypeFlag.initForTest();
35             return;
36         }
37         End_AOSP_Comment_Out */
38         sAllowOverrides = true;
39     }
40 
41     /** Returns a boolean experiment */
createFlag( boolean defaultValue)42     public static ExperimentFlag<Boolean> createFlag(
43 // AOSP_Comment_Out             Supplier<Boolean> phenotypeFlag,
44             boolean defaultValue) {
45         return new ExperimentFlag<>(
46 // AOSP_Comment_Out                 phenotypeFlag,
47                 defaultValue);
48     }
49 
50     private final T mDefaultValue;
51 // AOSP_Comment_Out     private final Supplier<T> mPhenotypeFlag;
52 
53 // AOSP_Comment_Out     // NOTE: mOverrideValue IS NEVER USED in the non AOSP version.
54     private T mOverrideValue = null;
55     // mOverridden IS NEVER USED in the non AOSP version.
56     private boolean mOverridden = false;
57 
ExperimentFlag( T defaultValue)58     private ExperimentFlag(
59 // AOSP_Comment_Out             Supplier<T> phenotypeFlag,
60             // NOTE: defaultValue IS NEVER USED in the non AOSP version.
61             T defaultValue) {
62         mDefaultValue = defaultValue;
63 // AOSP_Comment_Out         mPhenotypeFlag = phenotypeFlag;
64     }
65 
66     /** Returns value for this experiment */
get()67     public T get() {
68         /* Begin_AOSP_Comment_Out
69         if (!BuildConfig.AOSP) {
70             return mPhenotypeFlag.get();
71         }
72         End_AOSP_Comment_Out */
73         return sAllowOverrides && mOverridden ? mOverrideValue : mDefaultValue;
74     }
75 
76     @VisibleForTesting
override(T t)77     public void override(T t) {
78 
79         if (sAllowOverrides) {
80             mOverridden = true;
81             mOverrideValue = t;
82         }
83     }
84 
85     @VisibleForTesting
resetOverride()86     public void resetOverride() {
87         mOverridden = false;
88     }
89 
90     /* Begin_AOSP_Comment_Out
91     @VisibleForTesting
92     T getAospDefaultValueForTesting() {
93         return mDefaultValue;
94     }
95     End_AOSP_Comment_Out */
96 }
97