1 /*
2  * Copyright 2023 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 androidx.camera.extensions.impl;
18 
19 import org.jspecify.annotations.NonNull;
20 
21 /**
22  * An internal utility class that allows tests to specify whether to enable basic extender or
23  * advanced extender of this testlib. If OEM implementation exists on the device, the
24  * implementation type is always {@link ImplementationType#OEM_IMPL} and can't be changed to
25  * other types.
26  */
27 public class ExtensionsTestlibControl {
28     public enum ImplementationType {
29         OEM_IMPL,
30         TESTLIB_ADVANCED,
31         TESTLIB_BASIC
32     }
33 
34     private static ExtensionsTestlibControl sInstance;
35     private static Object sLock = new Object();
36     private volatile ImplementationType mImplementationType = ImplementationType.TESTLIB_BASIC;
37 
ExtensionsTestlibControl()38     private ExtensionsTestlibControl() {
39         mImplementationType = doesOEMImplementationExist()
40                 ? ImplementationType.OEM_IMPL : ImplementationType.TESTLIB_BASIC;
41     }
42 
43     /**
44      * Gets the singleton instance.
45      */
getInstance()46     public static @NonNull ExtensionsTestlibControl getInstance() {
47         synchronized (sLock) {
48             if (sInstance == null) {
49                 sInstance = new ExtensionsTestlibControl();
50             }
51             return sInstance;
52         }
53     }
54 
55     /**
56      * Set the implementation type.
57      *
58      * <p>When OEM implementation exists on the device, the only possible type is
59      * {@link ImplementationType#OEM_IMPL}. Setting the implementation type to
60      * {@link ImplementationType#TESTLIB_BASIC} or {@link ImplementationType#TESTLIB_ADVANCED}
61      *  when OEM implementation exist will throw an {@link IllegalArgumentException}.
62      *
63      * <p>When OEM implementation doesn't exist on the device, it is allowed to set it to
64      * {@link ImplementationType#TESTLIB_BASIC} or {@link ImplementationType#TESTLIB_ADVANCED}.
65      * Setting it to {@link ImplementationType#OEM_IMPL} in this case will throw an
66      * {@link IllegalArgumentException}.
67      */
setImplementationType(@onNull ImplementationType type)68     public void setImplementationType(@NonNull ImplementationType type) {
69         if (mImplementationType != ImplementationType.OEM_IMPL) { // OEM impl doesn't exist
70             if (type == ImplementationType.OEM_IMPL) {
71                 throw new IllegalArgumentException("OEM_IMPL is not supported on this device.");
72             }
73         } else { // OEM impl exists
74             if (type != ImplementationType.OEM_IMPL) {
75                 throw new IllegalArgumentException("Can't change the implementation type because "
76                         + "OEM implementation exists on the device");
77             }
78         }
79 
80         mImplementationType = type;
81     }
82 
doesOEMImplementationExist()83     private boolean doesOEMImplementationExist() {
84         try {
85             new ExtensionVersionImpl().checkTestlibRunning();
86             return false;
87         } catch (NoSuchMethodError e) { // checkTestlibRunning doesn't exist in OEM implementation.
88             return true;
89         }
90     }
91 
92     /**
93      * Gets the implementation type;
94      */
getImplementationType()95     public @NonNull ImplementationType getImplementationType() {
96         return mImplementationType;
97     }
98 }
99