blob: 7bddee60a53b600569e4ab6ae19a8e784d52561f [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);
35 int64_t startTime = 0;
36 sp<hardware::ISensorPrivacyManager> service = mService;
37 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
38 sp<IBinder> binder = defaultServiceManager()->checkService(String16("sensor_privacy"));
39 if (binder == nullptr) {
40 // Wait for the sensor privacy service to come back...
41 if (startTime == 0) {
42 startTime = uptimeMillis();
43 ALOGI("Waiting for sensor privacy service");
44 } else if ((uptimeMillis() - startTime) > 1000000) {
45 ALOGW("Waiting too long for sensor privacy service, giving up");
46 service = nullptr;
47 break;
48 }
49 usleep(25000);
50 } else {
51 service = interface_cast<hardware::ISensorPrivacyManager>(binder);
52 mService = service;
53 }
54 }
55 return service;
56}
57
58void SensorPrivacyManager::addSensorPrivacyListener(
59 const sp<hardware::ISensorPrivacyListener>& listener)
60{
61 sp<hardware::ISensorPrivacyManager> service = getService();
62 if (service != nullptr) {
63 service->addSensorPrivacyListener(listener);
64 }
65}
66
Evan Severson5b8fe032021-01-06 09:15:18 -080067void SensorPrivacyManager::addIndividualSensorPrivacyListener(int userId, int sensor,
68 const sp<hardware::ISensorPrivacyListener>& listener)
69{
70 sp<hardware::ISensorPrivacyManager> service = getService();
71 if (service != nullptr) {
72 service->addIndividualSensorPrivacyListener(userId, sensor, listener);
73 }
74}
75
Michael Groover5e1f60b2018-12-04 22:34:29 -080076void SensorPrivacyManager::removeSensorPrivacyListener(
77 const sp<hardware::ISensorPrivacyListener>& listener)
78{
79 sp<hardware::ISensorPrivacyManager> service = getService();
80 if (service != nullptr) {
81 service->removeSensorPrivacyListener(listener);
82 }
83}
84
85bool SensorPrivacyManager::isSensorPrivacyEnabled()
86{
87 sp<hardware::ISensorPrivacyManager> service = getService();
88 if (service != nullptr) {
89 bool result;
90 service->isSensorPrivacyEnabled(&result);
91 return result;
92 }
93 // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
94 return false;
95}
96
Evan Severson5b8fe032021-01-06 09:15:18 -080097bool SensorPrivacyManager::isIndividualSensorPrivacyEnabled(int userId, int sensor)
98{
99 sp<hardware::ISensorPrivacyManager> service = getService();
100 if (service != nullptr) {
101 bool result;
102 service->isIndividualSensorPrivacyEnabled(userId, sensor, &result);
103 return result;
104 }
105 // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
106 return false;
107}
108
Michael Groover56d63022019-01-03 21:12:02 -0800109status_t SensorPrivacyManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient)
110{
111 sp<hardware::ISensorPrivacyManager> service = getService();
112 if (service != nullptr) {
113 return IInterface::asBinder(service)->linkToDeath(recipient);
114 }
115 return INVALID_OPERATION;
116}
117
118status_t SensorPrivacyManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient)
119{
120 sp<hardware::ISensorPrivacyManager> service = getService();
121 if (service != nullptr) {
122 return IInterface::asBinder(service)->unlinkToDeath(recipient);
123 }
124 return INVALID_OPERATION;
125}
126
Michael Groover5e1f60b2018-12-04 22:34:29 -0800127}; // namespace android