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); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 35 | sp<hardware::ISensorPrivacyManager> service = mService; |
Yifei Zhang | 8d669dc | 2023-06-02 13:31:05 -0700 | [diff] [blame] | 36 | if (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) { |
| 37 | sp<IBinder> binder = defaultServiceManager()->waitForService(String16("sensor_privacy")); |
| 38 | mService = interface_cast<hardware::ISensorPrivacyManager>(binder); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 39 | } |
Yifei Zhang | 8d669dc | 2023-06-02 13:31:05 -0700 | [diff] [blame] | 40 | return mService; |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 43 | bool SensorPrivacyManager::supportsSensorToggle(int toggleType, int sensor) { |
Evan Severson | 24ef7b1 | 2021-04-20 16:43:03 -0700 | [diff] [blame] | 44 | if (mSupportedCache.find(sensor) == mSupportedCache.end()) { |
| 45 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 46 | if (service != nullptr) { |
| 47 | bool result; |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 48 | service->supportsSensorToggle(toggleType, sensor, &result); |
Evan Severson | 24ef7b1 | 2021-04-20 16:43:03 -0700 | [diff] [blame] | 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 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 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 | |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 68 | status_t SensorPrivacyManager::addToggleSensorPrivacyListener( |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 69 | const sp<hardware::ISensorPrivacyListener>& listener) |
| 70 | { |
| 71 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 72 | if (service != nullptr) { |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 73 | return service->addToggleSensorPrivacyListener(listener) |
Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 74 | .transactionError(); |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 75 | } |
Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 76 | return UNEXPECTED_NULL; |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 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 | |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 88 | void SensorPrivacyManager::removeToggleSensorPrivacyListener( |
Evan Severson | 7bfa35e | 2021-05-19 16:10:29 -0700 | [diff] [blame] | 89 | const sp<hardware::ISensorPrivacyListener>& listener) |
| 90 | { |
| 91 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 92 | if (service != nullptr) { |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 93 | service->removeToggleSensorPrivacyListener(listener); |
Evan Severson | 7bfa35e | 2021-05-19 16:10:29 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 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 | |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 109 | bool SensorPrivacyManager::isToggleSensorPrivacyEnabled(int sensor) |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 110 | { |
Jyoti Bhayana | 76256ad | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 111 | sp<hardware::ISensorPrivacyManager> service = getService(); |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 112 | if (service != nullptr) { |
| 113 | bool result; |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 114 | service->isCombinedToggleSensorPrivacyEnabled(sensor, &result); |
Evan Severson | 5b8fe03 | 2021-01-06 09:15:18 -0800 | [diff] [blame] | 115 | return result; |
| 116 | } |
| 117 | // if the SensorPrivacyManager is not available then assume sensor privacy is disabled |
| 118 | return false; |
| 119 | } |
| 120 | |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 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 | |
| 133 | status_t SensorPrivacyManager::isToggleSensorPrivacyEnabled(int toggleType, int sensor, |
Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 134 | bool &returnVal) |
| 135 | { |
| 136 | sp<hardware::ISensorPrivacyManager> service = getService(); |
| 137 | if (service != nullptr) { |
Evan Severson | 6f3e1c0 | 2022-01-27 10:43:40 -0800 | [diff] [blame] | 138 | binder::Status res = service->isToggleSensorPrivacyEnabled(toggleType, sensor, &returnVal); |
Evan Severson | 9287c50 | 2021-02-02 13:24:55 -0800 | [diff] [blame] | 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 | |
Jyoti Bhayana | 76256ad | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 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 | |
Jyoti Bhayana | 6424ede | 2024-02-27 15:33:59 -0800 | [diff] [blame] | 158 | std::vector<String16> SensorPrivacyManager::getCameraPrivacyAllowlist(){ |
Jyoti Bhayana | 76256ad | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 159 | sp<hardware::ISensorPrivacyManager> service = getService(); |
Jyoti Bhayana | 6424ede | 2024-02-27 15:33:59 -0800 | [diff] [blame] | 160 | std::vector<String16> result; |
Jyoti Bhayana | 76256ad | 2024-02-11 13:19:29 +0000 | [diff] [blame] | 161 | if (service != nullptr) { |
| 162 | service->getCameraPrivacyAllowlist(&result); |
| 163 | return result; |
| 164 | } |
| 165 | return result; |
| 166 | } |
| 167 | |
| 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 | |
Michael Groover | 56d6302 | 2019-01-03 21:12:02 -0800 | [diff] [blame] | 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 | |
| 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 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 196 | }; // namespace android |