Fix drm aidl listeners

This is a part of change to fix cts test issue on drm framework by using clearkey aidl as the backend plugin.

Test: atest
CtsMediaDrmTestCases: android.media.drm.cts.NativeMediaDrmClearkeyTest#testClearKeyPlaybackCenc
Bug: 206804036
Change-Id: I2ad8a863dc1804e74d0fc40b933473546ed39b4a
diff --git a/drm/libmediadrm/include/mediadrm/CryptoHal.h b/drm/libmediadrm/include/mediadrm/CryptoHal.h
index 5be59f0..32a6741 100644
--- a/drm/libmediadrm/include/mediadrm/CryptoHal.h
+++ b/drm/libmediadrm/include/mediadrm/CryptoHal.h
@@ -54,6 +54,6 @@
     DISALLOW_EVIL_CONSTRUCTORS(CryptoHal);
 };
 
-}
+}  // namespace android
 
-#endif
\ No newline at end of file
+#endif  // CRYPTO_HAL_H_
\ No newline at end of file
diff --git a/drm/libmediadrm/include/mediadrm/CryptoHalAidl.h b/drm/libmediadrm/include/mediadrm/CryptoHalAidl.h
index a25b091..fc7f7fb 100644
--- a/drm/libmediadrm/include/mediadrm/CryptoHalAidl.h
+++ b/drm/libmediadrm/include/mediadrm/CryptoHalAidl.h
@@ -89,4 +89,4 @@
 
 }  // namespace android
 
-#endif  // CRYPTO_HAL_H_
+#endif // CRYPTO_HAL_AIDL_H_
diff --git a/drm/libmediadrm/include/mediadrm/DrmHal.h b/drm/libmediadrm/include/mediadrm/DrmHal.h
index bb58585..f5e75ac 100644
--- a/drm/libmediadrm/include/mediadrm/DrmHal.h
+++ b/drm/libmediadrm/include/mediadrm/DrmHal.h
@@ -120,10 +120,10 @@
 
 private:
     sp<IDrm> mDrmHalHidl;
-    std::shared_ptr<IDrm> mDrmHalAidl;
+    sp<IDrm> mDrmHalAidl;
     DISALLOW_EVIL_CONSTRUCTORS(DrmHal);
 };
 
 } // namespace android
 
-#endif
\ No newline at end of file
+#endif  // DRM_HAL_H_
\ No newline at end of file
diff --git a/drm/libmediadrm/include/mediadrm/DrmHalAidl.h b/drm/libmediadrm/include/mediadrm/DrmHalAidl.h
index 6720734..3feda90 100644
--- a/drm/libmediadrm/include/mediadrm/DrmHalAidl.h
+++ b/drm/libmediadrm/include/mediadrm/DrmHalAidl.h
@@ -17,16 +17,16 @@
 #ifndef DRM_HAL_AIDL_H_
 #define DRM_HAL_AIDL_H_
 
+#include <memory>
 #include <aidl/android/hardware/drm/BnDrmPluginListener.h>
 #include <aidl/android/hardware/drm/IDrmFactory.h>
 #include <aidl/android/hardware/drm/IDrmPlugin.h>
 #include <aidl/android/media/BnResourceManagerClient.h>
 #include <mediadrm/DrmMetrics.h>
 #include <mediadrm/DrmSessionManager.h>
+#include <mediadrm/DrmHalListener.h>
 #include <mediadrm/IDrm.h>
-#include <memory>
 
-using ::aidl::android::hardware::drm::BnDrmPluginListener;
 using IDrmPluginAidl = ::aidl::android::hardware::drm::IDrmPlugin;
 using IDrmFactoryAidl = ::aidl::android::hardware::drm::IDrmFactory;
 using EventTypeAidl = ::aidl::android::hardware::drm::EventType;
@@ -34,9 +34,7 @@
 using ::aidl::android::hardware::drm::Uuid;
 
 namespace android {
-struct DrmHalAidl : public IDrm,
-                    public BnDrmPluginListener,
-                    std::enable_shared_from_this<BnDrmPluginListener> {
+struct DrmHalAidl : public IDrm{
     struct DrmSessionClient;
     DrmHalAidl();
     virtual ~DrmHalAidl();
@@ -107,7 +105,7 @@
                                            bool* required) const;
     virtual status_t setPlaybackId(Vector<uint8_t> const& sessionId, const char* playbackId);
     virtual status_t getLogMessages(Vector<drm::V1_4::LogMessage>& logs) const;
-    // Methods of IDrmPluginListenerAidl
+
     ::ndk::ScopedAStatus onEvent(EventTypeAidl in_eventType,
                                  const std::vector<uint8_t>& in_sessionId,
                                  const std::vector<uint8_t>& in_data);
@@ -117,17 +115,14 @@
                                       const std::vector<KeyStatusAidl>& in_keyStatusList,
                                       bool in_hasNewUsableKey);
     ::ndk::ScopedAStatus onSessionLostState(const std::vector<uint8_t>& in_sessionId);
-
   private:
     static Mutex mLock;
-    sp<IDrmClient> mListener;
-    mutable Mutex mEventLock;
-    mutable Mutex mNotifyLock;
+    mutable MediaDrmMetrics mMetrics;
+    std::shared_ptr<DrmHalListener> mListener;
     const std::vector<std::shared_ptr<IDrmFactoryAidl>> mFactories;
     std::shared_ptr<IDrmPluginAidl> mPlugin;
     std::vector<std::shared_ptr<IDrmFactoryAidl>> makeDrmFactories();
     status_t mInitCheck;
-    mutable MediaDrmMetrics mMetrics;
     std::vector<std::shared_ptr<DrmSessionClient>> mOpenSessions;
     void cleanup();
     void closeOpenSessions();
@@ -140,4 +135,4 @@
 
 }  // namespace android
 
-#endif
\ No newline at end of file
+#endif // DRM_HAL_AIDL_H_
\ No newline at end of file
diff --git a/drm/libmediadrm/include/mediadrm/DrmHalHidl.h b/drm/libmediadrm/include/mediadrm/DrmHalHidl.h
index 91dc700..94ef285 100644
--- a/drm/libmediadrm/include/mediadrm/DrmHalHidl.h
+++ b/drm/libmediadrm/include/mediadrm/DrmHalHidl.h
@@ -17,10 +17,6 @@
 #ifndef DRM_HAL_HIDL_H_
 #define DRM_HAL_HIDL_H_
 
-#include <android/hardware/drm/1.0/IDrmFactory.h>
-#include <android/hardware/drm/1.0/IDrmPlugin.h>
-#include <android/hardware/drm/1.1/IDrmFactory.h>
-#include <android/hardware/drm/1.1/IDrmPlugin.h>
 #include <android/hardware/drm/1.2/IDrmFactory.h>
 #include <android/hardware/drm/1.2/IDrmPlugin.h>
 #include <android/hardware/drm/1.2/IDrmPluginListener.h>
@@ -255,4 +251,4 @@
 
 }  // namespace android
 
-#endif  // DRM_HAL_H_
+#endif // DRM_HAL_HIDL_H_
diff --git a/drm/libmediadrm/include/mediadrm/DrmHalListener.h b/drm/libmediadrm/include/mediadrm/DrmHalListener.h
new file mode 100644
index 0000000..22361ad
--- /dev/null
+++ b/drm/libmediadrm/include/mediadrm/DrmHalListener.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2022 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.
+ */
+
+#ifndef DRM_HAL_LISTENER_H_
+#define DRM_HAL_LISTENER_H_
+
+#include <aidl/android/hardware/drm/BnDrmPluginListener.h>
+#include <mediadrm/DrmMetrics.h>
+#include <mediadrm/IDrmClient.h>
+
+using EventTypeAidl = ::aidl::android::hardware::drm::EventType;
+using KeyStatusAidl = ::aidl::android::hardware::drm::KeyStatus;
+using aidl::android::hardware::drm::BnDrmPluginListener;
+
+namespace android {
+struct DrmHalListener : public BnDrmPluginListener {
+    explicit DrmHalListener(MediaDrmMetrics* mMetrics);
+    ~DrmHalListener();
+    ::ndk::ScopedAStatus onEvent(EventTypeAidl in_eventType,
+                                 const std::vector<uint8_t>& in_sessionId,
+                                 const std::vector<uint8_t>& in_data);
+    ::ndk::ScopedAStatus onExpirationUpdate(const std::vector<uint8_t>& in_sessionId,
+                                            int64_t in_expiryTimeInMS);
+    ::ndk::ScopedAStatus onKeysChange(const std::vector<uint8_t>& in_sessionId,
+                                      const std::vector<KeyStatusAidl>& in_keyStatusList,
+                                      bool in_hasNewUsableKey);
+    ::ndk::ScopedAStatus onSessionLostState(const std::vector<uint8_t>& in_sessionId);
+    void setListener(sp<IDrmClient> listener);
+private:
+    mutable MediaDrmMetrics* mMetrics;
+    sp<IDrmClient> mListener;
+    mutable Mutex mEventLock;
+    mutable Mutex mNotifyLock;
+};
+} // namespace android
+
+#endif  // DRM_HAL_LISTENER_H_
\ No newline at end of file