blob: 8a18efa3ae238431baeb64f6b85402d51129f906 [file] [log] [blame]
Ytai Ben-Tsvi987d12c2020-03-24 17:35:44 -07001#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
9namespace android {
10
11using media::ICaptureStateListener;
12
13class CaptureStateNotifier::DeathRecipient : public IBinder::DeathRecipient {
14public:
15 DeathRecipient(CaptureStateNotifier* notifier) : mNotifier(notifier) {}
16
17 void binderDied(const wp<IBinder>&) override {
18 mNotifier->binderDied();
19 }
20
21private:
22 CaptureStateNotifier* const mNotifier;
23};
24
25CaptureStateNotifier::CaptureStateNotifier(bool initialActive) {
26 mActive = initialActive;
27}
28
29CaptureStateNotifier::~CaptureStateNotifier() {
30 LOG_ALWAYS_FATAL_IF(mListener != nullptr);
31}
32
33bool 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
44void 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
52void CaptureStateNotifier::binderDied() {
53 std::lock_guard<std::mutex> _l(mMutex);
54 mListener.clear();
55 ALOGI("Listener binder died");
56}
57
58} // namespace android