Merge "Fix image fetching." into pi-dev
diff --git a/services/core/jni/BroadcastRadio/Tuner.cpp b/services/core/jni/BroadcastRadio/Tuner.cpp
index 36d2994..a04697f 100644
--- a/services/core/jni/BroadcastRadio/Tuner.cpp
+++ b/services/core/jni/BroadcastRadio/Tuner.cpp
@@ -90,11 +90,12 @@
     bool mIsAudioConnected = false;
     Band mBand;
     wp<V1_0::IBroadcastRadio> mHalModule;
-    wp<V1_1::IBroadcastRadio> mHalModule11;
     sp<V1_0::ITuner> mHalTuner;
     sp<V1_1::ITuner> mHalTuner11;
     sp<HalDeathRecipient> mHalDeathRecipient;
 
+    sp<V1_1::IBroadcastRadio> getHalModule11() const;
+
 private:
     DISALLOW_COPY_AND_ASSIGN(TunerContext);
 };
@@ -143,6 +144,16 @@
     tunerCallback->hardwareFailure();
 }
 
+sp<V1_1::IBroadcastRadio> TunerContext::getHalModule11() const {
+    auto halModule = mHalModule.promote();
+    if (halModule == nullptr) {
+        ALOGE("HAL module is gone");
+        return nullptr;
+    }
+
+    return V1_1::IBroadcastRadio::castFrom(halModule).withDefault(nullptr);
+}
+
 // TODO(b/62713378): implement support for multiple tuners open at the same time
 static void notifyAudioService(TunerContext& ctx, bool connected) {
     if (!ctx.mWithAudio) return;
@@ -175,8 +186,6 @@
     }
 
     ctx.mHalModule = halModule;
-    ctx.mHalModule11 = V1_1::IBroadcastRadio::castFrom(halModule).withDefault(nullptr);
-
     ctx.mHalTuner = halTuner;
     ctx.mHalTuner11 = V1_1::ITuner::castFrom(halTuner).withDefault(nullptr);
     ALOGW_IF(ctx.mHalRev >= HalRevision::V1_1 && ctx.mHalTuner11 == nullptr,
@@ -388,18 +397,13 @@
     lock_guard<mutex> lk(gContextMutex);
     auto& ctx = getNativeContext(nativeContext);
 
-    if (ctx.mHalModule11 == nullptr) {
+    auto halModule = ctx.getHalModule11();
+    if (halModule == nullptr) {
         jniThrowException(env, "java/lang/IllegalStateException",
                 "Out-of-band images are not supported with HAL < 1.1");
         return nullptr;
     }
 
-    auto halModule = ctx.mHalModule11.promote();
-    if (halModule == nullptr) {
-        ALOGE("HAL module is gone");
-        return nullptr;
-    }
-
     JavaRef<jbyteArray> jRawImage = nullptr;
 
     auto hidlResult = halModule->getImage(id, [&](hidl_vec<uint8_t> rawImage) {
@@ -418,7 +422,7 @@
 
     if (convert::ThrowIfFailed(env, hidlResult)) return nullptr;
 
-    return jRawImage.get();
+    return jRawImage.release();
 }
 
 static bool nativeIsAnalogForced(JNIEnv *env, jobject obj, jlong nativeContext) {
diff --git a/services/core/jni/BroadcastRadio/convert.cpp b/services/core/jni/BroadcastRadio/convert.cpp
index 61b48c2..d2b7c7d 100644
--- a/services/core/jni/BroadcastRadio/convert.cpp
+++ b/services/core/jni/BroadcastRadio/convert.cpp
@@ -510,19 +510,16 @@
         jint status = 0;
         switch (item.type) {
             case MetadataType::INT:
-                ALOGV("metadata INT %d", key);
                 status = env->CallIntMethod(jMetadata.get(), gjni.RadioMetadata.putIntFromNative,
                         key, item.intValue);
                 break;
             case MetadataType::TEXT: {
-                ALOGV("metadata TEXT %d", key);
                 auto value = make_javastr(env, item.stringValue);
                 status = env->CallIntMethod(jMetadata.get(), gjni.RadioMetadata.putStringFromNative,
                         key, value.get());
                 break;
             }
             case MetadataType::RAW: {
-                ALOGV("metadata RAW %d", key);
                 auto len = item.rawValue.size();
                 if (len == 0) break;
                 auto value = make_javaref(env, env->NewByteArray(len));
@@ -537,7 +534,6 @@
                 break;
             }
             case MetadataType::CLOCK:
-                ALOGV("metadata CLOCK %d", key);
                 status = env->CallIntMethod(jMetadata.get(), gjni.RadioMetadata.putClockFromNative,
                         key, item.clockValue.utcSecondsSinceEpoch,
                         item.clockValue.timezoneOffsetInMinutes);