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 | |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 58 | bool SensorPrivacyManager::supportsSensorToggle(int toggleType, int sensor) { |
Evan Severson | 24ef7b1 | 2021-04-20 16:43:03 -0700 | [diff] [blame] | 59 | if (mSupportedCache.find(sensor) == mSupportedCache.end()) { |
| 60 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 61 | if (service != nullptr) { |
| 62 | bool result; |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 63 | service->supportsSensorToggle(toggleType, sensor, &result); |
Evan Severson | 24ef7b1 | 2021-04-20 16:43:03 -0700 | [diff] [blame] | 64 | mSupportedCache[sensor] = result; |
| 65 | return result; |
| 66 | } |
| 67 | // if the SensorPrivacyManager is not available then assume sensor privacy feature isn't |
| 68 | // supported |
| 69 | return false; |
| 70 | } |
| 71 | return mSupportedCache[sensor]; |
| 72 | } |
| 73 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 74 | void SensorPrivacyManager::addSensorPrivacyListener( |
| 75 | const sp<hardware::ISensorPrivacyListener>& listener) |
| 76 | { |
| 77 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 78 | if (service != nullptr) { |
| 79 | service->addSensorPrivacyListener(listener); |
| 80 | } |
| 81 | } |
| 82 | |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 83 | status_t SensorPrivacyManager::addToggleSensorPrivacyListener( |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 84 | const sp<hardware::ISensorPrivacyListener>& listener) |
| 85 | { |
| 86 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 87 | if (service != nullptr) { |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 88 | return service->addToggleSensorPrivacyListener(listener) |
Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 89 | .transactionError(); |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 90 | } |
Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 91 | return UNEXPECTED_NULL; |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 94 | void SensorPrivacyManager::removeSensorPrivacyListener( |
| 95 | const sp<hardware::ISensorPrivacyListener>& listener) |
| 96 | { |
| 97 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 98 | if (service != nullptr) { |
| 99 | service->removeSensorPrivacyListener(listener); |
| 100 | } |
| 101 | } |
| 102 | |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 103 | void SensorPrivacyManager::removeToggleSensorPrivacyListener( |
Evan Severson | 7bfa35e | 2021-05-19 16:10:29 -0700 | [diff] [blame] | 104 | const sp<hardware::ISensorPrivacyListener>& listener) |
| 105 | { |
| 106 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 107 | if (service != nullptr) { |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 108 | service->removeToggleSensorPrivacyListener(listener); |
Evan Severson | 7bfa35e | 2021-05-19 16:10:29 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 112 | bool SensorPrivacyManager::isSensorPrivacyEnabled() |
| 113 | { |
| 114 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 115 | if (service != nullptr) { |
| 116 | bool result; |
| 117 | service->isSensorPrivacyEnabled(&result); |
| 118 | return result; |
| 119 | } |
| 120 | // if the SensorPrivacyManager is not available then assume sensor privacy is disabled |
| 121 | return false; |
| 122 | } |
| 123 | |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 124 | bool SensorPrivacyManager::isToggleSensorPrivacyEnabled(int sensor) |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 125 | { |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 126 | sp<hardware::ISensorPrivacyManager> service = getService(); |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 127 | if (service != nullptr) { |
| 128 | bool result; |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 129 | service->isCombinedToggleSensorPrivacyEnabled(sensor, &result); |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 130 | return result; |
| 131 | } |
| 132 | // if the SensorPrivacyManager is not available then assume sensor privacy is disabled |
| 133 | return false; |
| 134 | } |
| 135 | |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 136 | bool SensorPrivacyManager::isToggleSensorPrivacyEnabled(int toggleType, int sensor) |
| 137 | { |
| 138 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 139 | if (service != nullptr) { |
| 140 | bool result; |
| 141 | service->isToggleSensorPrivacyEnabled(toggleType, sensor, &result); |
| 142 | return result; |
| 143 | } |
| 144 | // if the SensorPrivacyManager is not available then assume sensor privacy is disabled |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | status_t SensorPrivacyManager::isToggleSensorPrivacyEnabled(int toggleType, int sensor, |
Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 149 | bool &returnVal) |
| 150 | { |
| 151 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 152 | if (service != nullptr) { |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 153 | binder::Status res = service->isToggleSensorPrivacyEnabled(toggleType, sensor, &returnVal); |
Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 154 | return res.transactionError(); |
| 155 | } |
| 156 | // if the SensorPrivacyManager is not available then assume sensor privacy is disabled |
| 157 | returnVal = false; |
| 158 | return UNKNOWN_ERROR; |
| 159 | } |
| 160 | |
Michael Groover | 56d6302 | 2019-01-03 21:12:02 -0800 | [diff] [blame] | 161 | status_t SensorPrivacyManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) |
| 162 | { |
| 163 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 164 | if (service != nullptr) { |
| 165 | return IInterface::asBinder(service)->linkToDeath(recipient); |
| 166 | } |
| 167 | return INVALID_OPERATION; |
| 168 | } |
| 169 | |
| 170 | status_t SensorPrivacyManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) |
| 171 | { |
| 172 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 173 | if (service != nullptr) { |
| 174 | return IInterface::asBinder(service)->unlinkToDeath(recipient); |
| 175 | } |
| 176 | return INVALID_OPERATION; |
| 177 | } |
| 178 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 179 | }; // namespace android |