• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 android.media.tv.tuner;
18 
19 import android.annotation.IntDef;
20 import android.annotation.SystemApi;
21 import android.annotation.TestApi;
22 import android.util.Log;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * Utility class to check the currently running Tuner Hal implementation version.
29  *
30  * APIs that are not supported by the HAL implementation version would be no-op.
31  *
32  * @hide
33  */
34 @SystemApi
35 public final class TunerVersionChecker {
36     private static final String TAG = "TunerVersionChecker";
37 
TunerVersionChecker()38     private TunerVersionChecker() {}
39 
40     /** @hide */
41     @IntDef(prefix = "TUNER_VERSION_", value = {TUNER_VERSION_UNKNOWN, TUNER_VERSION_1_0,
42                                                 TUNER_VERSION_1_1})
43     @Retention(RetentionPolicy.SOURCE)
44     public @interface TunerVersion {}
45     /**
46      * Unknown Tuner version.
47      */
48     public static final int TUNER_VERSION_UNKNOWN = 0;
49     /**
50      * Tuner version 1.0.
51      */
52     public static final int TUNER_VERSION_1_0 = (1 << 16);
53     /**
54      * Tuner version 1.1.
55      */
56     public static final int TUNER_VERSION_1_1 = ((1 << 16) | 1);
57 
58     /**
59      * Get the current running Tuner version.
60      *
61      * @return Tuner version.
62      */
63     @TunerVersion
getTunerVersion()64     public static int getTunerVersion() {
65         return Tuner.getTunerVersion();
66     }
67 
68     /**
69      * Check if the current running Tuner version supports the given version.
70      *
71      * <p>Note that we treat different major versions as unsupported among each other. If any
72      * feature could be supported across major versions, please use
73      * {@link #isHigherOrEqualVersionTo(int)} to check.
74      *
75      * @param version the version to support.
76      *
77      * @return true if the current version is under the same major version as the given version
78      * and has higher or the same minor version as the given version.
79      * @hide
80      */
81     @TestApi
supportTunerVersion(@unerVersion int version)82     public static boolean supportTunerVersion(@TunerVersion int version) {
83         int currentVersion = Tuner.getTunerVersion();
84         return isHigherOrEqualVersionTo(version)
85                 && (getMajorVersion(version) == getMajorVersion(currentVersion));
86     }
87 
88     /**
89      * Check if the current running Tuner version is higher than or equal to a given version.
90      *
91      * @param version the version to compare.
92      *
93      * @return true if the current version is higher or equal to the support version.
94      * @hide
95      */
96     @TestApi
isHigherOrEqualVersionTo(@unerVersion int version)97     public static boolean isHigherOrEqualVersionTo(@TunerVersion int version) {
98         int currentVersion = Tuner.getTunerVersion();
99         return currentVersion >= version;
100     }
101 
102     /**
103      * Get the major version from a version number.
104      *
105      * @param version the version to be checked.
106      *
107      * @return the major version number.
108      * @hide
109      */
110     @TestApi
getMajorVersion(@unerVersion int version)111     public static int getMajorVersion(@TunerVersion int version) {
112         return ((version & 0xFFFF0000) >>> 16);
113     }
114 
115     /**
116      * Get the major version from a version number.
117      *
118      * @param version the version to be checked.
119      *
120      * @return the minor version number.
121      * @hide
122      */
123     @TestApi
getMinorVersion(@unerVersion int version)124     public static int getMinorVersion(@TunerVersion int version) {
125         return (version & 0xFFFF);
126     }
127 
128     /** @hide */
checkHigherOrEqualVersionTo( @unerVersion int version, String methodName)129     public static boolean checkHigherOrEqualVersionTo(
130             @TunerVersion int version, String methodName) {
131         if (!TunerVersionChecker.isHigherOrEqualVersionTo(version)) {
132             Log.e(TAG, "Current Tuner version "
133                     + TunerVersionChecker.getMajorVersion(Tuner.getTunerVersion()) + "."
134                     + TunerVersionChecker.getMinorVersion(Tuner.getTunerVersion())
135                     + " does not support " + methodName + ".");
136             return false;
137         }
138         return true;
139     }
140 
141     /** @hide */
checkSupportVersion(@unerVersion int version, String methodName)142     public static boolean checkSupportVersion(@TunerVersion int version, String methodName) {
143         if (!TunerVersionChecker.supportTunerVersion(version)) {
144             Log.e(TAG, "Current Tuner version "
145                     + TunerVersionChecker.getMajorVersion(Tuner.getTunerVersion()) + "."
146                     + TunerVersionChecker.getMinorVersion(Tuner.getTunerVersion())
147                     + " does not support " + methodName + ".");
148             return false;
149         }
150         return true;
151     }
152 }
153