1 /*
2 * Copyright (C) 2018 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 #include <mutex>
18 #include <unistd.h>
19
20 #include <binder/Binder.h>
21 #include <binder/IServiceManager.h>
22 #include <sensorprivacy/SensorPrivacyManager.h>
23
24 #include <utils/SystemClock.h>
25
26 namespace android {
27
SensorPrivacyManager()28 SensorPrivacyManager::SensorPrivacyManager()
29 {
30 }
31
getService()32 sp<hardware::ISensorPrivacyManager> SensorPrivacyManager::getService()
33 {
34 std::lock_guard<Mutex> scoped_lock(mLock);
35 sp<hardware::ISensorPrivacyManager> service = mService;
36 if (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
37 sp<IBinder> binder = defaultServiceManager()->waitForService(String16("sensor_privacy"));
38 mService = interface_cast<hardware::ISensorPrivacyManager>(binder);
39 }
40 return mService;
41 }
42
supportsSensorToggle(int toggleType,int sensor)43 bool SensorPrivacyManager::supportsSensorToggle(int toggleType, int sensor) {
44 if (mSupportedCache.find(sensor) == mSupportedCache.end()) {
45 sp<hardware::ISensorPrivacyManager> service = getService();
46 if (service != nullptr) {
47 bool result;
48 service->supportsSensorToggle(toggleType, sensor, &result);
49 mSupportedCache[sensor] = result;
50 return result;
51 }
52 // if the SensorPrivacyManager is not available then assume sensor privacy feature isn't
53 // supported
54 return false;
55 }
56 return mSupportedCache[sensor];
57 }
58
addSensorPrivacyListener(const sp<hardware::ISensorPrivacyListener> & listener)59 void SensorPrivacyManager::addSensorPrivacyListener(
60 const sp<hardware::ISensorPrivacyListener>& listener)
61 {
62 sp<hardware::ISensorPrivacyManager> service = getService();
63 if (service != nullptr) {
64 service->addSensorPrivacyListener(listener);
65 }
66 }
67
addToggleSensorPrivacyListener(const sp<hardware::ISensorPrivacyListener> & listener)68 status_t SensorPrivacyManager::addToggleSensorPrivacyListener(
69 const sp<hardware::ISensorPrivacyListener>& listener)
70 {
71 sp<hardware::ISensorPrivacyManager> service = getService();
72 if (service != nullptr) {
73 return service->addToggleSensorPrivacyListener(listener)
74 .transactionError();
75 }
76 return UNEXPECTED_NULL;
77 }
78
removeSensorPrivacyListener(const sp<hardware::ISensorPrivacyListener> & listener)79 void SensorPrivacyManager::removeSensorPrivacyListener(
80 const sp<hardware::ISensorPrivacyListener>& listener)
81 {
82 sp<hardware::ISensorPrivacyManager> service = getService();
83 if (service != nullptr) {
84 service->removeSensorPrivacyListener(listener);
85 }
86 }
87
removeToggleSensorPrivacyListener(const sp<hardware::ISensorPrivacyListener> & listener)88 void SensorPrivacyManager::removeToggleSensorPrivacyListener(
89 const sp<hardware::ISensorPrivacyListener>& listener)
90 {
91 sp<hardware::ISensorPrivacyManager> service = getService();
92 if (service != nullptr) {
93 service->removeToggleSensorPrivacyListener(listener);
94 }
95 }
96
isSensorPrivacyEnabled()97 bool SensorPrivacyManager::isSensorPrivacyEnabled()
98 {
99 sp<hardware::ISensorPrivacyManager> service = getService();
100 if (service != nullptr) {
101 bool result;
102 service->isSensorPrivacyEnabled(&result);
103 return result;
104 }
105 // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
106 return false;
107 }
108
isToggleSensorPrivacyEnabled(int sensor)109 bool SensorPrivacyManager::isToggleSensorPrivacyEnabled(int sensor)
110 {
111 sp<hardware::ISensorPrivacyManager> service = getService();
112 if (service != nullptr) {
113 bool result;
114 service->isCombinedToggleSensorPrivacyEnabled(sensor, &result);
115 return result;
116 }
117 // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
118 return false;
119 }
120
isToggleSensorPrivacyEnabled(int toggleType,int sensor)121 bool SensorPrivacyManager::isToggleSensorPrivacyEnabled(int toggleType, int sensor)
122 {
123 sp<hardware::ISensorPrivacyManager> service = getService();
124 if (service != nullptr) {
125 bool result;
126 service->isToggleSensorPrivacyEnabled(toggleType, sensor, &result);
127 return result;
128 }
129 // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
130 return false;
131 }
132
isToggleSensorPrivacyEnabled(int toggleType,int sensor,bool & returnVal)133 status_t SensorPrivacyManager::isToggleSensorPrivacyEnabled(int toggleType, int sensor,
134 bool &returnVal)
135 {
136 sp<hardware::ISensorPrivacyManager> service = getService();
137 if (service != nullptr) {
138 binder::Status res = service->isToggleSensorPrivacyEnabled(toggleType, sensor, &returnVal);
139 return res.transactionError();
140 }
141 // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
142 returnVal = false;
143 return UNKNOWN_ERROR;
144 }
145
getToggleSensorPrivacyState(int toggleType,int sensor)146 int SensorPrivacyManager::getToggleSensorPrivacyState(int toggleType, int sensor)
147 {
148 sp<hardware::ISensorPrivacyManager> service = getService();
149 if (service != nullptr) {
150 int result;
151 service->getToggleSensorPrivacyState(toggleType, sensor, &result);
152 return result;
153 }
154 // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
155 return DISABLED;
156 }
157
getCameraPrivacyAllowlist()158 std::vector<String16> SensorPrivacyManager::getCameraPrivacyAllowlist(){
159 sp<hardware::ISensorPrivacyManager> service = getService();
160 std::vector<String16> result;
161 if (service != nullptr) {
162 service->getCameraPrivacyAllowlist(&result);
163 return result;
164 }
165 return result;
166 }
167
isCameraPrivacyEnabled(String16 packageName)168 bool SensorPrivacyManager::isCameraPrivacyEnabled(String16 packageName){
169 sp<hardware::ISensorPrivacyManager> service = getService();
170 if (service != nullptr) {
171 bool result;
172 service->isCameraPrivacyEnabled(packageName, &result);
173 return result;
174 }
175 return false;
176 }
177
linkToDeath(const sp<IBinder::DeathRecipient> & recipient)178 status_t SensorPrivacyManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient)
179 {
180 sp<hardware::ISensorPrivacyManager> service = getService();
181 if (service != nullptr) {
182 return IInterface::asBinder(service)->linkToDeath(recipient);
183 }
184 return INVALID_OPERATION;
185 }
186
unlinkToDeath(const sp<IBinder::DeathRecipient> & recipient)187 status_t SensorPrivacyManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient)
188 {
189 sp<hardware::ISensorPrivacyManager> service = getService();
190 if (service != nullptr) {
191 return IInterface::asBinder(service)->unlinkToDeath(recipient);
192 }
193 return INVALID_OPERATION;
194 }
195
196 }; // namespace android
197