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