1 #define LOG_TAG "CaptureStateNotifier" 2 3 #include "CaptureStateNotifier.h" 4 5 #include <android/media/ICaptureStateListener.h> 6 #include <binder/IBinder.h> 7 #include <utils/Log.h> 8 9 namespace android { 10 11 using media::ICaptureStateListener; 12 13 class CaptureStateNotifier::DeathRecipient : public IBinder::DeathRecipient { 14 public: DeathRecipient(CaptureStateNotifier * notifier)15 DeathRecipient(CaptureStateNotifier* notifier) : mNotifier(notifier) {} 16 binderDied(const wp<IBinder> &)17 void binderDied(const wp<IBinder>&) override { 18 mNotifier->binderDied(); 19 } 20 21 private: 22 CaptureStateNotifier* const mNotifier; 23 }; 24 CaptureStateNotifier(bool initialActive)25CaptureStateNotifier::CaptureStateNotifier(bool initialActive) 26 : mDeathRecipient(new DeathRecipient(this)), mActive( 27 initialActive) {} 28 ~CaptureStateNotifier()29CaptureStateNotifier::~CaptureStateNotifier() { 30 LOG_ALWAYS_FATAL_IF(mListener != nullptr); 31 } 32 RegisterListener(const sp<ICaptureStateListener> & listener)33bool CaptureStateNotifier::RegisterListener(const sp<ICaptureStateListener>& listener) { 34 std::lock_guard<std::mutex> _l(mMutex); 35 LOG_ALWAYS_FATAL_IF(mListener != nullptr); 36 LOG_ALWAYS_FATAL_IF(listener == nullptr); 37 38 ALOGI("Registering a listener"); 39 sp<IBinder> binder = IInterface::asBinder(listener); 40 if (binder != nullptr) { 41 status_t status = binder->linkToDeath(mDeathRecipient); 42 if (status == NO_ERROR) { 43 mListener = listener; 44 } else { 45 ALOGE("Failed to register death listener: %u", status); 46 } 47 } else { 48 ALOGE("Listener failed to cast to a binder."); 49 } 50 return mActive; 51 } 52 setCaptureState(bool active)53void CaptureStateNotifier::setCaptureState(bool active) { 54 std::lock_guard<std::mutex> _l(mMutex); 55 mActive = active; 56 if (mListener) { 57 mListener->setCaptureState(active); 58 } 59 } 60 binderDied()61void CaptureStateNotifier::binderDied() { 62 std::lock_guard<std::mutex> _l(mMutex); 63 mListener.clear(); 64 ALOGI("Listener binder died"); 65 } 66 67 } // namespace android