blob: 135e0a2bb961db20f2ca1fe937d31b9ec0c9c70a [file] [log] [blame]
Ytai Ben-Tsvi987d12c2020-03-24 17:35:44 -07001#define LOG_TAG "CaptureStateNotifier"
2
Ytai Ben-Tsvi95ec1b82020-04-02 18:08:36 -07003#include "CaptureStateNotifier.h"
4
Ytai Ben-Tsvi987d12c2020-03-24 17:35:44 -07005#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
Ytai Ben-Tsvi95ec1b82020-04-02 18:08:36 -070025CaptureStateNotifier::CaptureStateNotifier(bool initialActive)
26 : mDeathRecipient(new DeathRecipient(this)), mActive(
27 initialActive) {}
Ytai Ben-Tsvi987d12c2020-03-24 17:35:44 -070028
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);
Ytai Ben-Tsvi95ec1b82020-04-02 18:08:36 -070036 LOG_ALWAYS_FATAL_IF(listener == nullptr);
Ytai Ben-Tsvi987d12c2020-03-24 17:35:44 -070037
38 ALOGI("Registering a listener");
Ytai Ben-Tsvi95ec1b82020-04-02 18:08:36 -070039 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-Tsvi987d12c2020-03-24 17:35:44 -070050 return mActive;
51}
52
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
61void CaptureStateNotifier::binderDied() {
62 std::lock_guard<std::mutex> _l(mMutex);
63 mListener.clear();
64 ALOGI("Listener binder died");
65}
66
67} // namespace android