Ytai Ben-Tsvi | 987d12c | 2020-03-24 17:35:44 -0700 | [diff] [blame] | 1 | #define LOG_TAG "CaptureStateNotifier" |
| 2 | |
Ytai Ben-Tsvi | 95ec1b8 | 2020-04-02 18:08:36 -0700 | [diff] [blame] | 3 | #include "CaptureStateNotifier.h" |
| 4 | |
Ytai Ben-Tsvi | 987d12c | 2020-03-24 17:35:44 -0700 | [diff] [blame] | 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 | |
Ytai Ben-Tsvi | 95ec1b8 | 2020-04-02 18:08:36 -0700 | [diff] [blame] | 25 | CaptureStateNotifier::CaptureStateNotifier(bool initialActive) |
| 26 | : mDeathRecipient(new DeathRecipient(this)), mActive( |
| 27 | initialActive) {} |
Ytai Ben-Tsvi | 987d12c | 2020-03-24 17:35:44 -0700 | [diff] [blame] | 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); |
Ytai Ben-Tsvi | 95ec1b8 | 2020-04-02 18:08:36 -0700 | [diff] [blame] | 36 | LOG_ALWAYS_FATAL_IF(listener == nullptr); |
Ytai Ben-Tsvi | 987d12c | 2020-03-24 17:35:44 -0700 | [diff] [blame] | 37 | |
| 38 | ALOGI("Registering a listener"); |
Ytai Ben-Tsvi | 95ec1b8 | 2020-04-02 18:08:36 -0700 | [diff] [blame] | 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 | } |
Ytai Ben-Tsvi | 987d12c | 2020-03-24 17:35:44 -0700 | [diff] [blame] | 50 | return mActive; |
| 51 | } |
| 52 | |
| 53 | void 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 | |
| 61 | void CaptureStateNotifier::binderDied() { |
| 62 | std::lock_guard<std::mutex> _l(mMutex); |
| 63 | mListener.clear(); |
| 64 | ALOGI("Listener binder died"); |
| 65 | } |
| 66 | |
| 67 | } // namespace android |