1 /* 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 27 namespace android { 28 TunnelModeEnabledReporter()29TunnelModeEnabledReporter::TunnelModeEnabledReporter() {} 30 updateTunnelModeStatus()31void TunnelModeEnabledReporter::updateTunnelModeStatus() { 32 bool tunnelModeEnabled = mTunnelModeCount > 0; 33 dispatchTunnelModeEnabled(tunnelModeEnabled); 34 } 35 dispatchTunnelModeEnabled(bool tunnelModeEnabled)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 binderDied(const wp<IBinder> & who)55void TunnelModeEnabledReporter::binderDied(const wp<IBinder>& who) { 56 std::scoped_lock lock(mMutex); 57 mListeners.erase(who); 58 } 59 addListener(const sp<gui::ITunnelModeEnabledListener> & listener)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 removeListener(const sp<gui::ITunnelModeEnabledListener> & listener)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 79