1 /*
2  * Copyright 2021 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.internal;
18 
19 import androidx.annotation.VisibleForTesting;
20 
21 import org.jspecify.annotations.NonNull;
22 
23 /**
24  * The client version of the Extensions-Interface that CameraX extension library uses.
25  */
26 public class ClientVersion {
27     // Current version of vendor library implementation that the CameraX extension supports. This
28     // needs to be increased along with the version of vendor library interface.
29     private static ClientVersion sCurrent = new ClientVersion("1.5.0");
30 
getCurrentVersion()31     public static @NonNull ClientVersion getCurrentVersion() {
32         return sCurrent;
33     }
34 
35     /**
36      * Overrides the client version for testing.
37      */
38     @VisibleForTesting
setCurrentVersion(@onNull ClientVersion clientVersion)39     public static void setCurrentVersion(@NonNull ClientVersion clientVersion) {
40         sCurrent = clientVersion;
41     }
42 
43     private final Version mVersion;
44 
getVersion()45     public @NonNull Version getVersion() {
46         return mVersion;
47     }
48 
ClientVersion(@onNull String versionString)49     public ClientVersion(@NonNull String versionString) {
50         mVersion = Version.parse(versionString);
51     }
52 
53     /**
54      * Check if the client version meets the minimum compatible version requirement. This implies
55      * that the client version is equal to or newer than the version.
56      *
57      * <p> The compatible version is comprised of the major and minor version numbers. The patch
58      * number is ignored.
59      *
60      * @param version The minimum compatible version required
61      * @return True if the client version meets the minimum version requirement and False
62      * otherwise.
63      */
isMinimumCompatibleVersion(@onNull Version version)64     public static boolean isMinimumCompatibleVersion(@NonNull Version version) {
65         return ClientVersion.getCurrentVersion().mVersion
66                 .compareTo(version.getMajor(), version.getMinor()) >= 0;
67     }
68 
69     /**
70      * Check if the client version meets the maximum compatible version requirement. This implies
71      * that the client version is equal to or older than the version.
72      *
73      * <p> The compatible version is comprised of the major and minor version numbers. The patch
74      * number is ignored.
75      *
76      * @param version The minimum compatible version required
77      * @return True if the client version meets the maximum version requirement and False
78      * otherwise.
79      */
isMaximumCompatibleVersion(@onNull Version version)80     public static boolean isMaximumCompatibleVersion(@NonNull Version version) {
81         return ClientVersion.getCurrentVersion().mVersion
82                 .compareTo(version.getMajor(), version.getMinor()) <= 0;
83     }
84 
85     /**
86      * Gets this version number as string.
87      *
88      * @return the string of the version in a form of MAJOR.MINOR.PATCH-description.
89      */
toVersionString()90     public @NonNull String toVersionString() {
91         return mVersion.toString();
92     }
93 }
94