blob: 57c74ee565df5c7f68f762178d2afa56f29e8b88 [file] [log] [blame]
Michael Groover5e1f60b2018-12-04 22:34:29 -08001/*
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
26namespace android {
27
28SensorPrivacyManager::SensorPrivacyManager()
29{
30}
31
32sp<hardware::ISensorPrivacyManager> SensorPrivacyManager::getService()
33{
34 std::lock_guard<Mutex> scoped_lock(mLock);
Michael Groover5e1f60b2018-12-04 22:34:29 -080035 sp<hardware::ISensorPrivacyManager> service = mService;
Yifei Zhang8d669dc2023-06-02 13:31:05 -070036 if (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
37 sp<IBinder> binder = defaultServiceManager()->waitForService(String16("sensor_privacy"));
38 mService = interface_cast<hardware::ISensorPrivacyManager>(binder);
Michael Groover5e1f60b2018-12-04 22:34:29 -080039 }
Yifei Zhang8d669dc2023-06-02 13:31:05 -070040 return mService;
Michael Groover5e1f60b2018-12-04 22:34:29 -080041}
42
Evan Severson6f3e1c02022-01-27 10:43:40 -080043bool SensorPrivacyManager::supportsSensorToggle(int toggleType, int sensor) {
Evan Severson24ef7b12021-04-20 16:43:03 -070044 if (mSupportedCache.find(sensor) == mSupportedCache.end()) {
45 sp<hardware::ISensorPrivacyManager> service = getService();
46 if (service != nullptr) {
47 bool result;
Evan Severson6f3e1c02022-01-27 10:43:40 -080048 service->supportsSensorToggle(toggleType, sensor, &result);
Evan Severson24ef7b12021-04-20 16:43:03 -070049 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
Michael Groover5e1f60b2018-12-04 22:34:29 -080059void 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
Evan Severson6f3e1c02022-01-27 10:43:40 -080068status_t SensorPrivacyManager::addToggleSensorPrivacyListener(
Evan Severson5b8fe032021-01-06 09:15:18 -080069 const sp<hardware::ISensorPrivacyListener>& listener)
70{
71 sp<hardware::ISensorPrivacyManager> service = getService();
72 if (service != nullptr) {
Evan Severson6f3e1c02022-01-27 10:43:40 -080073 return service->addToggleSensorPrivacyListener(listener)
Evan Severson9287c502021-02-02 13:24:55 -080074 .transactionError();
Evan Severson5b8fe032021-01-06 09:15:18 -080075 }
Evan Severson9287c502021-02-02 13:24:55 -080076 return UNEXPECTED_NULL;
Evan Severson5b8fe032021-01-06 09:15:18 -080077}
78
Michael Groover5e1f60b2018-12-04 22:34:29 -080079void 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
Evan Severson6f3e1c02022-01-27 10:43:40 -080088void SensorPrivacyManager::removeToggleSensorPrivacyListener(
Evan Severson7bfa35e2021-05-19 16:10:29 -070089 const sp<hardware::ISensorPrivacyListener>& listener)
90{
91 sp<hardware::ISensorPrivacyManager> service = getService();
92 if (service != nullptr) {
Evan Severson6f3e1c02022-01-27 10:43:40 -080093 service->removeToggleSensorPrivacyListener(listener);
Evan Severson7bfa35e2021-05-19 16:10:29 -070094 }
95}
96
Michael Groover5e1f60b2018-12-04 22:34:29 -080097bool 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
Evan Severson6f3e1c02022-01-27 10:43:40 -0800109bool SensorPrivacyManager::isToggleSensorPrivacyEnabled(int sensor)
Evan Severson5b8fe032021-01-06 09:15:18 -0800110{
Evan Severson6f3e1c02022-01-27 10:43:40 -0800111 sp<hardware::ISensorPrivacyManager> service = getService();
Evan Severson5b8fe032021-01-06 09:15:18 -0800112 if (service != nullptr) {
113 bool result;
Evan Severson6f3e1c02022-01-27 10:43:40 -0800114 service->isCombinedToggleSensorPrivacyEnabled(sensor, &result);
Evan Severson5b8fe032021-01-06 09:15:18 -0800115 return result;
116 }
117 // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
118 return false;
119}
120
Evan Severson6f3e1c02022-01-27 10:43:40 -0800121bool 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
133status_t SensorPrivacyManager::isToggleSensorPrivacyEnabled(int toggleType, int sensor,
Evan Severson9287c502021-02-02 13:24:55 -0800134 bool &returnVal)
135{
136 sp<hardware::ISensorPrivacyManager> service = getService();
137 if (service != nullptr) {
Evan Severson6f3e1c02022-01-27 10:43:40 -0800138 binder::Status res = service->isToggleSensorPrivacyEnabled(toggleType, sensor, &returnVal);
Evan Severson9287c502021-02-02 13:24:55 -0800139 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
Michael Groover56d63022019-01-03 21:12:02 -0800146status_t SensorPrivacyManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient)
147{
148 sp<hardware::ISensorPrivacyManager> service = getService();
149 if (service != nullptr) {
150 return IInterface::asBinder(service)->linkToDeath(recipient);
151 }
152 return INVALID_OPERATION;
153}
154
155status_t SensorPrivacyManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient)
156{
157 sp<hardware::ISensorPrivacyManager> service = getService();
158 if (service != nullptr) {
159 return IInterface::asBinder(service)->unlinkToDeath(recipient);
160 }
161 return INVALID_OPERATION;
162}
163
Michael Groover5e1f60b2018-12-04 22:34:29 -0800164}; // namespace android