Ytai Ben-Tsvi | 987d12c | 2020-03-24 17:35:44 -0700 | [diff] [blame^] | 1 | #include "CaptureStateNotifier.h" |
| 2 | |
| 3 | #define LOG_TAG "CaptureStateNotifier" |
| 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: |
| 15 | DeathRecipient(CaptureStateNotifier* notifier) : mNotifier(notifier) {} |
| 16 | |
| 17 | void binderDied(const wp<IBinder>&) override { |
| 18 | mNotifier->binderDied(); |
| 19 | } |
| 20 | |
| 21 | private: |
| 22 | CaptureStateNotifier* const mNotifier; |
| 23 | }; |
| 24 | |
| 25 | CaptureStateNotifier::CaptureStateNotifier(bool initialActive) { |
| 26 | mActive = initialActive; |
| 27 | } |
| 28 | |
| 29 | CaptureStateNotifier::~CaptureStateNotifier() { |
| 30 | LOG_ALWAYS_FATAL_IF(mListener != nullptr); |
| 31 | } |
| 32 | |
| 33 | bool CaptureStateNotifier::RegisterListener(const sp<ICaptureStateListener>& listener) { |
| 34 | std::lock_guard<std::mutex> _l(mMutex); |
| 35 | LOG_ALWAYS_FATAL_IF(mListener != nullptr); |
| 36 | |
| 37 | ALOGI("Registering a listener"); |
| 38 | mListener = listener; |
| 39 | sp<IBinder> binder = IInterface::asBinder(mListener); |
| 40 | binder->linkToDeath(new DeathRecipient(this)); |
| 41 | return mActive; |
| 42 | } |
| 43 | |
| 44 | void CaptureStateNotifier::setCaptureState(bool active) { |
| 45 | std::lock_guard<std::mutex> _l(mMutex); |
| 46 | mActive = active; |
| 47 | if (mListener) { |
| 48 | mListener->setCaptureState(active); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void CaptureStateNotifier::binderDied() { |
| 53 | std::lock_guard<std::mutex> _l(mMutex); |
| 54 | mListener.clear(); |
| 55 | ALOGI("Listener binder died"); |
| 56 | } |
| 57 | |
| 58 | } // namespace android |