• 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_",
42             value = {TUNER_VERSION_UNKNOWN, TUNER_VERSION_1_0, TUNER_VERSION_1_1,
43                     TUNER_VERSION_2_0})
44     @Retention(RetentionPolicy.SOURCE)
45     public @interface TunerVersion {}
46     /**
47      * Unknown Tuner version.
48      */
49     public static final int TUNER_VERSION_UNKNOWN = 0;
50     /**
51      * Tuner version 1.0.
52      */
53     public static final int TUNER_VERSION_1_0 = (1 << 16);
54     /**
55      * Tuner version 1.1.
56      */
57     public static final int TUNER_VERSION_1_1 = ((1 << 16) | 1);
58     /**
59      * Tuner version 2.0.
60      */
61     public static final int TUNER_VERSION_2_0 = (2 << 16);
62     /**
63      * Tuner version 3.0.
64      */
65     public static final int TUNER_VERSION_3_0 = (3 << 16);
66 
67     /**
68      * Get the current running Tuner version.
69      *
70      * @return Tuner version.
71      */
72     @TunerVersion
getTunerVersion()73     public static int getTunerVersion() {
74         return Tuner.getTunerVersion();
75     }
76 
77     /**
78      * Check if the current running Tuner version supports the given version.
79      *
80      * <p>Note that we treat different major versions as unsupported among each other. If any
81      * feature could be supported across major versions, please use
82      * {@link #isHigherOrEqualVersionTo(int)} to check.
83      *
84      * @param version the version to support.
85      *
86      * @return true if the current version is under the same major version as the given version
87      * and has higher or the same minor version as the given version.
88      * @hide
89      */
90     @TestApi
supportTunerVersion(@unerVersion int version)91     public static boolean supportTunerVersion(@TunerVersion int version) {
92         int currentVersion = Tuner.getTunerVersion();
93         return isHigherOrEqualVersionTo(version)
94                 && (getMajorVersion(version) == getMajorVersion(currentVersion));
95     }
96 
97     /**
98      * Check if the current running Tuner version is higher than or equal to a given version.
99      *
100      * @param version the version to compare.
101      *
102      * @return true if the current version is higher or equal to the support version.
103      * @hide
104      */
105     @TestApi
isHigherOrEqualVersionTo(@unerVersion int version)106     public static boolean isHigherOrEqualVersionTo(@TunerVersion int version) {
107         int currentVersion = Tuner.getTunerVersion();
108         return currentVersion >= version;
109     }
110 
111     /**
112      * Get the major version from a version number.
113      *
114      * @param version the version to be checked.
115      *
116      * @return the major version number.
117      * @hide
118      */
119     @TestApi
getMajorVersion(@unerVersion int version)120     public static int getMajorVersion(@TunerVersion int version) {
121         return ((version & 0xFFFF0000) >>> 16);
122     }
123 
124     /**
125      * Get the major version from a version number.
126      *
127      * @param version the version to be checked.
128      *
129      * @return the minor version number.
130      * @hide
131      */
132     @TestApi
getMinorVersion(@unerVersion int version)133     public static int getMinorVersion(@TunerVersion int version) {
134         return (version & 0xFFFF);
135     }
136 
137     /** @hide */
checkHigherOrEqualVersionTo( @unerVersion int version, String methodName)138     public static boolean checkHigherOrEqualVersionTo(
139             @TunerVersion int version, String methodName) {
140         if (!TunerVersionChecker.isHigherOrEqualVersionTo(version)) {
141             Log.e(TAG, "Current Tuner version "
142                     + TunerVersionChecker.getMajorVersion(Tuner.getTunerVersion()) + "."
143                     + TunerVersionChecker.getMinorVersion(Tuner.getTunerVersion())
144                     + " does not support " + methodName + ".");
145             return false;
146         }
147         return true;
148     }
149 
150     /** @hide */
checkSupportVersion(@unerVersion int version, String methodName)151     public static boolean checkSupportVersion(@TunerVersion int version, String methodName) {
152         if (!TunerVersionChecker.supportTunerVersion(version)) {
153             Log.e(TAG, "Current Tuner version "
154                     + TunerVersionChecker.getMajorVersion(Tuner.getTunerVersion()) + "."
155                     + TunerVersionChecker.getMinorVersion(Tuner.getTunerVersion())
156                     + " does not support " + methodName + ".");
157             return false;
158         }
159         return true;
160     }
161 }
162