| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 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 |  | 
|  | 28 | SensorPrivacyManager::SensorPrivacyManager() | 
|  | 29 | { | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | sp<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 |  | 
|  | 58 | void 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 Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 67 | status_t SensorPrivacyManager::addIndividualSensorPrivacyListener(int userId, int sensor, | 
| Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 68 | const sp<hardware::ISensorPrivacyListener>& listener) | 
|  | 69 | { | 
|  | 70 | sp<hardware::ISensorPrivacyManager> service = getService(); | 
|  | 71 | if (service != nullptr) { | 
| Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 72 | return service->addIndividualSensorPrivacyListener(userId, sensor, listener) | 
|  | 73 | .transactionError(); | 
| Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 74 | } | 
| Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 75 | return UNEXPECTED_NULL; | 
| Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 78 | void SensorPrivacyManager::removeSensorPrivacyListener( | 
|  | 79 | const sp<hardware::ISensorPrivacyListener>& listener) | 
|  | 80 | { | 
|  | 81 | sp<hardware::ISensorPrivacyManager> service = getService(); | 
|  | 82 | if (service != nullptr) { | 
|  | 83 | service->removeSensorPrivacyListener(listener); | 
|  | 84 | } | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | bool SensorPrivacyManager::isSensorPrivacyEnabled() | 
|  | 88 | { | 
|  | 89 | sp<hardware::ISensorPrivacyManager> service = getService(); | 
|  | 90 | if (service != nullptr) { | 
|  | 91 | bool result; | 
|  | 92 | service->isSensorPrivacyEnabled(&result); | 
|  | 93 | return result; | 
|  | 94 | } | 
|  | 95 | // if the SensorPrivacyManager is not available then assume sensor privacy is disabled | 
|  | 96 | return false; | 
|  | 97 | } | 
|  | 98 |  | 
| Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 99 | bool SensorPrivacyManager::isIndividualSensorPrivacyEnabled(int userId, int sensor) | 
|  | 100 | { | 
|  | 101 | sp<hardware::ISensorPrivacyManager> service = getService(); | 
|  | 102 | if (service != nullptr) { | 
|  | 103 | bool result; | 
|  | 104 | service->isIndividualSensorPrivacyEnabled(userId, sensor, &result); | 
|  | 105 | return result; | 
|  | 106 | } | 
|  | 107 | // if the SensorPrivacyManager is not available then assume sensor privacy is disabled | 
|  | 108 | return false; | 
|  | 109 | } | 
|  | 110 |  | 
| Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 111 | status_t SensorPrivacyManager::isIndividualSensorPrivacyEnabled(int userId, int sensor, | 
|  | 112 | bool &returnVal) | 
|  | 113 | { | 
|  | 114 | sp<hardware::ISensorPrivacyManager> service = getService(); | 
|  | 115 | if (service != nullptr) { | 
|  | 116 | binder::Status res = service->isIndividualSensorPrivacyEnabled(userId, sensor, &returnVal); | 
|  | 117 | return res.transactionError(); | 
|  | 118 | } | 
|  | 119 | // if the SensorPrivacyManager is not available then assume sensor privacy is disabled | 
|  | 120 | returnVal = false; | 
|  | 121 | return UNKNOWN_ERROR; | 
|  | 122 | } | 
|  | 123 |  | 
| Michael Groover | 56d6302 | 2019-01-03 21:12:02 -0800 | [diff] [blame] | 124 | status_t SensorPrivacyManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) | 
|  | 125 | { | 
|  | 126 | sp<hardware::ISensorPrivacyManager> service = getService(); | 
|  | 127 | if (service != nullptr) { | 
|  | 128 | return IInterface::asBinder(service)->linkToDeath(recipient); | 
|  | 129 | } | 
|  | 130 | return INVALID_OPERATION; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | status_t SensorPrivacyManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) | 
|  | 134 | { | 
|  | 135 | sp<hardware::ISensorPrivacyManager> service = getService(); | 
|  | 136 | if (service != nullptr) { | 
|  | 137 | return IInterface::asBinder(service)->unlinkToDeath(recipient); | 
|  | 138 | } | 
|  | 139 | return INVALID_OPERATION; | 
|  | 140 | } | 
|  | 141 |  | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 142 | }; // namespace android |