blob: 4497cafa587f6fadda2986fee79d67e73e28842e [file] [log] [blame]
Galia Peycheva8f04b302021-04-27 13:25:38 +02001/*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "TunnelModeEnabledReporter"
19#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
21#include <algorithm>
22
23#include "Layer.h"
24#include "SurfaceFlinger.h"
25#include "TunnelModeEnabledReporter.h"
26
27namespace android {
28
Robert Carr3e2a2992021-06-11 13:42:55 -070029TunnelModeEnabledReporter::TunnelModeEnabledReporter() {}
Galia Peycheva8f04b302021-04-27 13:25:38 +020030
31void TunnelModeEnabledReporter::updateTunnelModeStatus() {
Robert Carr3e2a2992021-06-11 13:42:55 -070032 bool tunnelModeEnabled = mTunnelModeCount > 0;
Galia Peycheva8f04b302021-04-27 13:25:38 +020033 dispatchTunnelModeEnabled(tunnelModeEnabled);
34}
35
36void TunnelModeEnabledReporter::dispatchTunnelModeEnabled(bool tunnelModeEnabled) {
37 std::vector<sp<gui::ITunnelModeEnabledListener>> localListeners;
38 {
39 std::scoped_lock lock(mMutex);
40 if (mTunnelModeEnabled == tunnelModeEnabled) {
41 return;
42 }
43 mTunnelModeEnabled = tunnelModeEnabled;
44
45 std::transform(mListeners.begin(), mListeners.end(), std::back_inserter(localListeners),
46 [](const std::pair<wp<IBinder>, sp<gui::ITunnelModeEnabledListener>>&
47 entry) { return entry.second; });
48 }
49
50 for (sp<gui::ITunnelModeEnabledListener>& listener : localListeners) {
51 listener->onTunnelModeEnabledChanged(tunnelModeEnabled);
52 }
53}
54
55void TunnelModeEnabledReporter::binderDied(const wp<IBinder>& who) {
56 std::scoped_lock lock(mMutex);
57 mListeners.erase(who);
58}
59
60void TunnelModeEnabledReporter::addListener(const sp<gui::ITunnelModeEnabledListener>& listener) {
61 sp<IBinder> asBinder = IInterface::asBinder(listener);
62 asBinder->linkToDeath(this);
63 bool tunnelModeEnabled = false;
64 {
65 std::scoped_lock lock(mMutex);
66 mListeners.emplace(wp<IBinder>(asBinder), listener);
67 tunnelModeEnabled = mTunnelModeEnabled;
68 }
69 listener->onTunnelModeEnabledChanged(tunnelModeEnabled);
70}
71
72void TunnelModeEnabledReporter::removeListener(
73 const sp<gui::ITunnelModeEnabledListener>& listener) {
74 std::lock_guard lock(mMutex);
75 mListeners.erase(wp<IBinder>(IInterface::asBinder(listener)));
76}
77
78} // namespace android