cameraservice: Migrate all internal String8/String16s to std::string
String8 and String16 are deprecated classes. It is recommended to use
std::string or std::u16string wherever possible. String16 is the native
string class for aidl, but Strings marked @utf8InCpp can use std::string
directly.
This patch standardizes libcameraservice's use of strings to
std::string, which is capable of storing utf-8 strings. This makes the
code more readable and potentially reduces the number of string copies
to a minimum.
A new set of string utils is added to frameworks/av/camera to aid this
migration.
Change-Id: I59330ac03c8a52b6c21a2388bba0c143e68af4cf
Bug: 265487852
Test: Presubmit, ran CtsCameraTestCases on Cuttlefish, adb shell dumpsys media camera and observed output
diff --git a/services/camera/libcameraservice/common/CameraProviderManager.cpp b/services/camera/libcameraservice/common/CameraProviderManager.cpp
index 1a6e341..23051ef 100644
--- a/services/camera/libcameraservice/common/CameraProviderManager.cpp
+++ b/services/camera/libcameraservice/common/CameraProviderManager.cpp
@@ -43,6 +43,7 @@
#include <hwbinder/IPCThreadState.h>
#include <utils/Trace.h>
#include <ui/PublicFormat.h>
+#include <camera/StringUtils.h>
#include "api2/HeicCompositeStream.h"
#include "device3/ZoomRatioMapper.h"
@@ -143,7 +144,7 @@
String16(aidlHalServiceDescriptor));
for (const auto &aidlInstance : aidlProviders) {
std::string aidlServiceName =
- getFullAidlProviderName(std::string(String8(aidlInstance).c_str()));
+ getFullAidlProviderName(toStdString(aidlInstance));
auto res = sm->registerForNotifications(String16(aidlServiceName.c_str()), this);
if (res != OK) {
ALOGE("%s Unable to register for notifications with AIDL service manager",
@@ -774,14 +775,14 @@
primaryMap = &mCameraProviderByCameraId;
alternateMap = &mTorchProviderByCameraId;
}
- auto id = cameraId.c_str();
- (*primaryMap)[id] = provider;
- auto search = alternateMap->find(id);
+
+ (*primaryMap)[cameraId] = provider;
+ auto search = alternateMap->find(cameraId);
if (search != alternateMap->end()) {
ALOGW("%s: Camera device %s is using both torch mode and camera mode simultaneously. "
- "That should not be possible", __FUNCTION__, id);
+ "That should not be possible", __FUNCTION__, cameraId.c_str());
}
- ALOGV("%s: Camera device %s connected", __FUNCTION__, id);
+ ALOGV("%s: Camera device %s connected", __FUNCTION__, cameraId.c_str());
}
void CameraProviderManager::removeRef(DeviceMode usageType, const std::string &cameraId) {
@@ -796,7 +797,7 @@
providerMap = &mCameraProviderByCameraId;
}
std::lock_guard<std::mutex> lock(mProviderInterfaceMapLock);
- auto search = providerMap->find(cameraId.c_str());
+ auto search = providerMap->find(cameraId);
if (search != providerMap->end()) {
// Drop the reference to this ICameraProvider. This is safe to do immediately (without an
// added delay) because hwservicemanager guarantees to hold the reference for at least five
@@ -805,7 +806,7 @@
// restart it. An example when this could happen is switching from a front-facing to a
// rear-facing camera. If the HAL were to exit during the camera switch, the camera could
// appear janky to the user.
- providerMap->erase(cameraId.c_str());
+ providerMap->erase(cameraId);
IPCThreadState::self()->flushCommands();
} else {
ALOGE("%s: Asked to remove reference for camera %s, but no reference to it was found. This "
@@ -823,7 +824,7 @@
{
std::lock_guard<std::mutex> lock(mInterfaceMutex);
- res = addAidlProviderLocked(String8(name).c_str());
+ res = addAidlProviderLocked(toStdString(name));
}
sp<StatusListener> listener = getStatusListener();
@@ -2035,14 +2036,14 @@
status_t CameraProviderManager::removeProvider(const std::string& provider) {
std::lock_guard<std::mutex> providerLock(mProviderLifecycleLock);
std::unique_lock<std::mutex> lock(mInterfaceMutex);
- std::vector<String8> removedDeviceIds;
+ std::vector<std::string> removedDeviceIds;
status_t res = NAME_NOT_FOUND;
std::string removedProviderName;
for (auto it = mProviders.begin(); it != mProviders.end(); it++) {
if ((*it)->mProviderInstance == provider) {
removedDeviceIds.reserve((*it)->mDevices.size());
for (auto& deviceInfo : (*it)->mDevices) {
- removedDeviceIds.push_back(String8(deviceInfo->mId.c_str()));
+ removedDeviceIds.push_back(deviceInfo->mId);
}
removedProviderName = (*it)->mProviderName;
mProviders.erase(it);
@@ -2182,7 +2183,7 @@
return OK;
}
-void CameraProviderManager::ProviderInfo::removeDevice(std::string id) {
+void CameraProviderManager::ProviderInfo::removeDevice(const std::string &id) {
for (auto it = mDevices.begin(); it != mDevices.end(); it++) {
if ((*it)->mId == id) {
mUniqueCameraIds.erase(id);
@@ -2222,8 +2223,7 @@
ALOGV("%s: notify device not_present: %s",
__FUNCTION__,
deviceName.c_str());
- listener->onDeviceStatusChanged(String8(id.c_str()),
- CameraDeviceStatus::NOT_PRESENT);
+ listener->onDeviceStatusChanged(id, CameraDeviceStatus::NOT_PRESENT);
mLock.lock();
}
}
@@ -2324,8 +2324,7 @@
CameraDeviceStatus internalNewStatus = newStatus;
if (!mInitialized) {
mCachedStatus.emplace_back(false /*isPhysicalCameraStatus*/,
- cameraDeviceName.c_str(), std::string().c_str(),
- internalNewStatus);
+ cameraDeviceName, std::string(), internalNewStatus);
return;
}
@@ -2339,7 +2338,7 @@
// Call without lock held to allow reentrancy into provider manager
if (listener != nullptr) {
- listener->onDeviceStatusChanged(String8(id.c_str()), internalNewStatus);
+ listener->onDeviceStatusChanged(id, internalNewStatus);
}
}
@@ -2415,8 +2414,7 @@
}
// Call without lock held to allow reentrancy into provider manager
if (listener != nullptr) {
- listener->onDeviceStatusChanged(String8(id.c_str()),
- String8(physicalId.c_str()), newStatus);
+ listener->onDeviceStatusChanged(id, physicalId, newStatus);
}
return;
}
@@ -2467,7 +2465,7 @@
}
*id = cameraId;
- *physicalId = physicalCameraDeviceName.c_str();
+ *physicalId = physicalCameraDeviceName;
return OK;
}
@@ -2511,7 +2509,7 @@
// findDeviceInfo, which should be holding mLock while iterating through
// each provider's devices).
if (listener != nullptr) {
- listener->onTorchStatusChanged(String8(id.c_str()), newStatus, systemCameraKind);
+ listener->onTorchStatusChanged(id, newStatus, systemCameraKind);
}
return;
}