Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7b3b578d authored by Amy Zhang's avatar Amy Zhang
Browse files

Add TunerVersionChecker to expose the Tuner HAL implementation version

This can help Tuner java/cts get the information of the Tuner HAL
implementation version and notify the client on the availability of
the APIs in different versions or test with different expected result.

Users can all access getTunerVersion to get the information.

Test: atest android.media.tv.tuner.cts
Bug: 158816517
Bug: 164449999
Change-Id: I0b6e4c226e696ca7f94dff7d2c9a59f57af7e13a
parent b13e3042
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -5073,6 +5073,13 @@ package android.media.tv.tuner {
    method public void onResourceLost(@NonNull android.media.tv.tuner.Tuner);
  }
  public final class TunerVersionChecker {
    method public static int getTunerVersion();
    field public static final int TUNER_VERSION_1_0 = 65536; // 0x10000
    field public static final int TUNER_VERSION_1_1 = 65537; // 0x10001
    field public static final int TUNER_VERSION_UNKNOWN = 0; // 0x0
  }
}
package android.media.tv.tuner.dvr {
+15 −0
Original line number Diff line number Diff line
@@ -2132,6 +2132,21 @@ package android.media.tv {

}

package android.media.tv.tuner {

  public final class TunerVersionChecker {
    method public static int getMajorVersion(int);
    method public static int getMinorVersion(int);
    method public static int getTunerVersion();
    method public static boolean isHigherOrEqualVersionTo(int);
    method public static boolean supportTunerVersion(int);
    field public static final int TUNER_VERSION_1_0 = 65536; // 0x10000
    field public static final int TUNER_VERSION_1_1 = 65537; // 0x10001
    field public static final int TUNER_VERSION_UNKNOWN = 0; // 0x0
  }

}

package android.metrics {

  public class LogMaker {
+19 −0
Original line number Diff line number Diff line
@@ -223,6 +223,7 @@ public class Tuner implements AutoCloseable {
    private final Context mContext;
    private final TunerResourceManager mTunerResourceManager;
    private final int mClientId;
    private static int sTunerVersion = TunerVersionChecker.TUNER_VERSION_UNKNOWN;

    private Frontend mFrontend;
    private EventHandler mHandler;
@@ -274,6 +275,14 @@ public class Tuner implements AutoCloseable {
    public Tuner(@NonNull Context context, @Nullable String tvInputSessionId,
            @TvInputService.PriorityHintUseCaseType int useCase) {
        nativeSetup();
        sTunerVersion = nativeGetTunerVersion();
        if (sTunerVersion == TunerVersionChecker.TUNER_VERSION_UNKNOWN) {
            Log.e(TAG, "Unknown Tuner version!");
        } else {
            Log.d(TAG, "Current Tuner version is "
                    + TunerVersionChecker.getMajorVersion(sTunerVersion) + "."
                    + TunerVersionChecker.getMinorVersion(sTunerVersion) + ".");
        }
        mContext = context;
        mTunerResourceManager = (TunerResourceManager)
                context.getSystemService(Context.TV_TUNER_RESOURCE_MGR_SERVICE);
@@ -313,6 +322,11 @@ public class Tuner implements AutoCloseable {
        mTunerResourceManager.setFrontendInfoList(infos);
    }

    /** @hide */
    public static int getTunerVersion() {
        return sTunerVersion;
    }

    /** @hide */
    public List<Integer> getFrontendIds() {
        return nativeGetFrontendIds();
@@ -435,6 +449,11 @@ public class Tuner implements AutoCloseable {
     */
    private native void nativeSetup();

    /**
     * Native method to get all frontend IDs.
     */
    private native int nativeGetTunerVersion();

    /**
     * Native method to get all frontend IDs.
     */
+153 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.media.tv.tuner;

import android.annotation.IntDef;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.util.Log;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Utility class to check the currently running Tuner Hal implementation version.
 *
 * APIs that are not supported by the HAL implementation version would be no-op.
 *
 * @hide
 */
@TestApi
@SystemApi
public final class TunerVersionChecker {
    private static final String TAG = "TunerVersionChecker";

    private TunerVersionChecker() {}

    /** @hide */
    @IntDef(prefix = "TUNER_VERSION_", value = {TUNER_VERSION_UNKNOWN, TUNER_VERSION_1_0,
                                                TUNER_VERSION_1_1})
    @Retention(RetentionPolicy.SOURCE)
    public @interface TunerVersion {}
    /**
     * Unknown Tuner version.
     */
    public static final int TUNER_VERSION_UNKNOWN = 0;
    /**
     * Tuner version 1.0.
     */
    public static final int TUNER_VERSION_1_0 = (1 << 16);
    /**
     * Tuner version 1.1.
     */
    public static final int TUNER_VERSION_1_1 = ((1 << 16) | 1);

    /**
     * Get the current running Tuner version.
     *
     * @return Tuner version.
     */
    @TunerVersion
    public static int getTunerVersion() {
        return Tuner.getTunerVersion();
    }

    /**
     * Check if the current running Tuner version supports the given version.
     *
     * <p>Note that we treat different major versions as unsupported among each other. If any
     * feature could be supported across major versions, please use
     * {@link #isHigherOrEqualVersionTo(int)} to check.
     *
     * @param version the version to support.
     *
     * @return true if the current version is under the same major version as the given version
     * and has higher or the same minor version as the given version.
     * @hide
     */
    @TestApi
    public static boolean supportTunerVersion(@TunerVersion int version) {
        int currentVersion = Tuner.getTunerVersion();
        return isHigherOrEqualVersionTo(version)
                && (getMajorVersion(version) == getMajorVersion(currentVersion));
    }

    /**
     * Check if the current running Tuner version is higher than or equal to a given version.
     *
     * @param version the version to compare.
     *
     * @return true if the current version is higher or equal to the support version.
     * @hide
     */
    @TestApi
    public static boolean isHigherOrEqualVersionTo(@TunerVersion int version) {
        int currentVersion = Tuner.getTunerVersion();
        return currentVersion >= version;
    }

    /**
     * Get the major version from a version number.
     *
     * @param version the version to be checked.
     *
     * @return the major version number.
     * @hide
     */
    @TestApi
    public static int getMajorVersion(@TunerVersion int version) {
        return ((version & 0xFFFF0000) >>> 16);
    }

    /**
     * Get the major version from a version number.
     *
     * @param version the version to be checked.
     *
     * @return the minor version number.
     * @hide
     */
    @TestApi
    public static int getMinorVersion(@TunerVersion int version) {
        return (version & 0xFFFF);
    }

    /** @hide */
    public static boolean checkHigherOrEqualVersionTo(
            @TunerVersion int version, String methodName) {
        if (!TunerVersionChecker.isHigherOrEqualVersionTo(version)) {
            Log.e(TAG, "Current Tuner version "
                    + TunerVersionChecker.getMajorVersion(Tuner.getTunerVersion()) + "."
                    + TunerVersionChecker.getMinorVersion(Tuner.getTunerVersion())
                    + " does not support " + methodName + ".");
            return false;
        }
        return true;
    }

    /** @hide */
    public static boolean checkSupportVersion(@TunerVersion int version, String methodName) {
        if (!TunerVersionChecker.supportTunerVersion(version)) {
            Log.e(TAG, "Current Tuner version "
                    + TunerVersionChecker.getMajorVersion(Tuner.getTunerVersion()) + "."
                    + TunerVersionChecker.getMinorVersion(Tuner.getTunerVersion())
                    + " does not support " + methodName + ".");
            return false;
        }
        return true;
    }
}
+17 −1
Original line number Diff line number Diff line
@@ -941,6 +941,7 @@ Return<void> FrontendCallback::onScanMessage(FrontendScanMessageType type, const

sp<ITuner> JTuner::mTuner;
sp<::android::hardware::tv::tuner::V1_1::ITuner> JTuner::mTuner_1_1;
int JTuner::mTunerVersion = 0;

JTuner::JTuner(JNIEnv *env, jobject thiz)
    : mClass(NULL) {
@@ -971,7 +972,8 @@ JTuner::~JTuner() {
}

sp<ITuner> JTuner::getTunerService() {
    if (mTuner == nullptr && mTuner_1_1 == nullptr) {
    if (mTuner == nullptr) {
        mTunerVersion = 0;
        mTuner_1_1 = ::android::hardware::tv::tuner::V1_1::ITuner::getService();

        if (mTuner_1_1 == nullptr) {
@@ -979,14 +981,22 @@ sp<ITuner> JTuner::getTunerService() {
            mTuner = ITuner::getService();
            if (mTuner == nullptr) {
                ALOGW("Failed to get tuner 1.0 service.");
            } else {
                mTunerVersion = 1 << 16;
            }
        } else {
            mTuner = static_cast<sp<ITuner>>(mTuner_1_1);
            mTunerVersion = ((1 << 16) | 1);
         }
     }
     return mTuner;
}

jint JTuner::getTunerVersion() {
    ALOGD("JTuner::getTunerVersion()");
    return (jint) mTunerVersion;
}

jobject JTuner::getFrontendIds() {
    ALOGD("JTuner::getFrontendIds()");
    mTuner->getFrontendIds([&](Result, const hidl_vec<FrontendId>& frontendIds) {
@@ -2536,6 +2546,11 @@ static void android_media_tv_Tuner_native_setup(JNIEnv *env, jobject thiz) {
    setTuner(env,thiz, tuner);
}

static jint android_media_tv_Tuner_native_get_tuner_version(JNIEnv *env, jobject thiz) {
    sp<JTuner> tuner = getTuner(env, thiz);
    return tuner->getTunerVersion();
}

static jobject android_media_tv_Tuner_get_frontend_ids(JNIEnv *env, jobject thiz) {
    sp<JTuner> tuner = getTuner(env, thiz);
    return tuner->getFrontendIds();
@@ -3749,6 +3764,7 @@ static void android_media_tv_Tuner_media_event_finalize(JNIEnv* env, jobject med
static const JNINativeMethod gTunerMethods[] = {
    { "nativeInit", "()V", (void *)android_media_tv_Tuner_native_init },
    { "nativeSetup", "()V", (void *)android_media_tv_Tuner_native_setup },
    { "nativeGetTunerVersion", "()I", (void *)android_media_tv_Tuner_native_get_tuner_version },
    { "nativeGetFrontendIds", "()Ljava/util/List;",
            (void *)android_media_tv_Tuner_get_frontend_ids },
    { "nativeOpenFrontendByHandle", "(I)Landroid/media/tv/tuner/Tuner$Frontend;",
Loading