Refactor Tuner JNI and move the interaction with ILnb into LnbClient

Test: atest android.media.tv.tuner.cts on Cuttlefish
Bug: 174095851
Change-Id: I3903f62949aec0dac61388b356e7d7850a5a044c
diff --git a/media/jni/tuner/TunerClient.cpp b/media/jni/tuner/TunerClient.cpp
index 67a559c..69cd3d3 100644
--- a/media/jni/tuner/TunerClient.cpp
+++ b/media/jni/tuner/TunerClient.cpp
@@ -180,11 +180,49 @@
     return NULL;
 }
 
-sp<LnbClient> TunerClient::openLnb(int /*lnbHandle*/) {
+sp<LnbClient> TunerClient::openLnb(int lnbHandle) {
+    if (mTunerService != NULL) {
+        // TODO: handle error code
+        /*shared_ptr<ITunerLnb> tunerLnb;
+        mTunerService->openLnb(demuxHandle, &tunerLnb);
+        return new LnbClient(tunerLnb);*/
+    }
+
+    if (mTuner != NULL) {
+        int id = getResourceIdFromHandle(lnbHandle, LNB);
+        // TODO: pending aidl interface
+        sp<LnbClient> lnbClient = new LnbClient();
+        sp<ILnb> hidlLnb = openHidlLnbById(id);
+        if (hidlLnb != NULL) {
+            lnbClient->setHidlLnb(hidlLnb);
+            lnbClient->setId(id);
+            return lnbClient;
+        }
+    }
+
     return NULL;
 }
 
-sp<LnbClient> TunerClient::openLnbByName(string /*lnbName*/) {
+sp<LnbClient> TunerClient::openLnbByName(string lnbName) {
+    if (mTunerService != NULL) {
+        // TODO: handle error code
+        /*shared_ptr<ITunerLnb> tunerLnb;
+        mTunerService->openLnbByName(lnbName, &tunerLnb);
+        return new LnbClient(tunerLnb);*/
+    }
+
+    if (mTuner != NULL) {
+        // TODO: pending aidl interface
+        sp<LnbClient> lnbClient = new LnbClient();
+        LnbId id;
+        sp<ILnb> hidlLnb = openHidlLnbByName(lnbName, id);
+        if (hidlLnb != NULL) {
+            lnbClient->setHidlLnb(hidlLnb);
+            lnbClient->setId(id);
+            return lnbClient;
+        }
+    }
+
     return NULL;
 }
 
@@ -249,6 +287,37 @@
     return demux;
 }
 
+sp<ILnb> TunerClient::openHidlLnbById(int id) {
+    sp<ILnb> lnb;
+    Result res;
+
+    mTuner->openLnbById(id, [&](Result r, const sp<ILnb>& lnbSp) {
+        res = r;
+        lnb = lnbSp;
+    });
+    if (res != Result::SUCCESS || lnb == nullptr) {
+        ALOGE("Failed to open lnb by id");
+        return NULL;
+    }
+    return lnb;
+}
+
+sp<ILnb> TunerClient::openHidlLnbByName(string name, LnbId& lnbId) {
+    sp<ILnb> lnb;
+    Result res;
+
+    mTuner->openLnbByName(name, [&](Result r, LnbId id, const sp<ILnb>& lnbSp) {
+        res = r;
+        lnb = lnbSp;
+        lnbId = id;
+    });
+    if (res != Result::SUCCESS || lnb == nullptr) {
+        ALOGE("Failed to open lnb by name");
+        return NULL;
+    }
+    return lnb;
+}
+
 FrontendInfo TunerClient::FrontendInfoAidlToHidl(TunerServiceFrontendInfo aidlFrontendInfo) {
     FrontendInfo hidlFrontendInfo {
         .type = static_cast<FrontendType>(aidlFrontendInfo.type),