Skip to content

Commit 1431590

Browse files
amitnjrestyled-commits
authored andcommitted
Fix the issue of unreleased JNI references. (#23655)
* Fix the issue of unreleased JNI references. * Restyled by clang-format Co-authored-by: Restyled.io <commits@restyled.io>
1 parent f50fc87 commit 1431590

File tree

6 files changed

+45
-34
lines changed

6 files changed

+45
-34
lines changed

examples/tv-app/android/include/content-launcher/AppContentLauncherManager.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,27 @@ CHIP_ERROR AppContentLauncherManager::HandleGetAcceptHeaderList(AttributeValueEn
8585
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetAcceptHeaderList");
8686
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::ContentLauncher::Id,
8787
chip::app::Clusters::ContentLauncher::Attributes::AcceptHeader::Id);
88-
const char * resStr = mAttributeDelegate->Read(aPath);
89-
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response %s", resStr);
88+
std::string resStr = mAttributeDelegate->Read(aPath);
89+
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetAcceptHeaderList response %s", resStr.c_str());
9090

91-
if (resStr != nullptr && *resStr != 0)
91+
if (resStr.length() != 0)
9292
{
9393
Json::Reader reader;
9494
Json::Value value;
9595
if (reader.parse(resStr, value))
9696
{
9797
std::string attrId = to_string(chip::app::Clusters::ContentLauncher::Attributes::AcceptHeader::Id);
98-
ChipLogProgress(
99-
Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response parsing done. reading attr %s",
100-
attrId.c_str());
98+
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetAcceptHeaderList response parsing done. reading attr %s",
99+
attrId.c_str());
101100
if (value[attrId].isArray())
102101
{
103102
mAcceptHeaderList.clear();
104103
for (Json::Value & entry : value[attrId])
105104
{
106-
mAcceptHeaderList.push_back(entry.asString());
105+
if (entry.isString())
106+
{
107+
mAcceptHeaderList.push_back(entry.asString());
108+
}
107109
}
108110
}
109111
}
@@ -124,10 +126,10 @@ uint32_t AppContentLauncherManager::HandleGetSupportedStreamingProtocols()
124126
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols");
125127
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::ContentLauncher::Id,
126128
chip::app::Clusters::ContentLauncher::Attributes::SupportedStreamingProtocols::Id);
127-
const char * resStr = mAttributeDelegate->Read(aPath);
128-
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response %s", resStr);
129+
std::string resStr = mAttributeDelegate->Read(aPath);
130+
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response %s", resStr.c_str());
129131

130-
if (resStr == nullptr || *resStr == 0)
132+
if (resStr.length() == 0)
131133
{
132134
return mSupportedStreamingProtocols;
133135
}
@@ -141,7 +143,7 @@ uint32_t AppContentLauncherManager::HandleGetSupportedStreamingProtocols()
141143
std::string attrId = to_string(chip::app::Clusters::ContentLauncher::Attributes::SupportedStreamingProtocols::Id);
142144
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSupportedStreamingProtocols response parsing done. reading attr %s",
143145
attrId.c_str());
144-
if (!value[attrId].empty())
146+
if (!value[attrId].empty() && value[attrId].isInt())
145147
{
146148
uint32_t supportedStreamingProtocols = static_cast<uint32_t>(value[attrId].asInt());
147149
mSupportedStreamingProtocols = supportedStreamingProtocols;

examples/tv-app/android/include/media-playback/AppMediaPlaybackManager.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ uint64_t AppMediaPlaybackManager::HandleMediaRequestGetAttribute(chip::Attribute
130130
{
131131
ChipLogProgress(Zcl, "Received AppMediaPlaybackManager::HandleMediaRequestGetAttribute:%d", attributeId);
132132
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::MediaPlayback::Id, attributeId);
133-
const char * resStr = mAttributeDelegate->Read(aPath);
134-
ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleMediaRequestGetAttribute response %s", resStr);
133+
std::string resStr = mAttributeDelegate->Read(aPath);
134+
ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleMediaRequestGetAttribute response %s", resStr.c_str());
135135

136136
uint64_t ret = std::numeric_limits<uint64_t>::max();
137-
if (resStr != nullptr && *resStr != 0)
137+
if (resStr.length() != 0)
138138
{
139139
Json::Reader reader;
140140
Json::Value value;
@@ -143,7 +143,7 @@ uint64_t AppMediaPlaybackManager::HandleMediaRequestGetAttribute(chip::Attribute
143143
std::string attrId = to_string(attributeId);
144144
ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleMediaRequestGetAttribute response parsing done. reading attr %s",
145145
attrId.c_str());
146-
if (!value[attrId].empty())
146+
if (!value[attrId].empty() && value[attrId].isUInt())
147147
{
148148
ret = static_cast<uint64_t>(value[attrId].asUInt());
149149
return ret;
@@ -177,9 +177,10 @@ CHIP_ERROR AppMediaPlaybackManager::HandleGetSampledPosition(AttributeValueEncod
177177
ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleGetSampledPosition");
178178
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::MediaPlayback::Id,
179179
chip::app::Clusters::MediaPlayback::Attributes::SampledPosition::Id);
180-
const char * resStr = mAttributeDelegate->Read(aPath);
180+
std::string resStr = mAttributeDelegate->Read(aPath);
181+
ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleGetSampledPosition response %s", resStr.c_str());
181182

182-
if (resStr != nullptr && *resStr != 0)
183+
if (resStr.length() != 0)
183184
{
184185
Json::Reader reader;
185186
Json::Value value;
@@ -188,13 +189,14 @@ CHIP_ERROR AppMediaPlaybackManager::HandleGetSampledPosition(AttributeValueEncod
188189
std::string attrId = to_string(chip::app::Clusters::MediaPlayback::Attributes::SampledPosition::Id);
189190
ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSampledPosition response parsing done. reading attr %s",
190191
attrId.c_str());
191-
if (!value[attrId].empty())
192+
if (!value[attrId].empty() && value[attrId].isObject())
192193
{
193194
std::string updatedAt = to_string(
194195
static_cast<uint32_t>(chip::app::Clusters::MediaPlayback::Structs::PlaybackPosition::Fields::kUpdatedAt));
195196
std::string position = to_string(
196197
static_cast<uint32_t>(chip::app::Clusters::MediaPlayback::Structs::PlaybackPosition::Fields::kPosition));
197-
if (!value[attrId][updatedAt].empty() && !value[attrId][position].empty())
198+
if (!value[attrId][updatedAt].empty() && !value[attrId][position].empty() && value[attrId][updatedAt].isUInt() &&
199+
value[attrId][position].isUInt())
198200
{
199201
// valid response
200202
response.updatedAt = value[attrId][updatedAt].asUInt();

examples/tv-app/android/include/target-navigator/TargetNavigatorManager.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ CHIP_ERROR TargetNavigatorManager::HandleGetTargetList(AttributeValueEncoder & a
3939
{
4040
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::TargetNavigator::Id,
4141
chip::app::Clusters::TargetNavigator::Attributes::TargetList::Id);
42-
const char * resStr = mAttributeDelegate->Read(aPath);
43-
ChipLogProgress(Zcl, "TargetNavigatorManager::HandleNavigateTarget response %s", resStr);
42+
std::string resStr = mAttributeDelegate->Read(aPath);
43+
ChipLogProgress(Zcl, "TargetNavigatorManager::HandleNavigateTarget response %s", resStr.c_str());
4444

45-
if (resStr != nullptr && *resStr != 0)
45+
if (resStr.length() != 0)
4646
{
4747
Json::Reader reader;
4848
Json::Value value;
@@ -104,10 +104,10 @@ uint8_t TargetNavigatorManager::HandleGetCurrentTarget()
104104
{
105105
chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::TargetNavigator::Id,
106106
chip::app::Clusters::TargetNavigator::Attributes::TargetList::Id);
107-
const char * resStr = mAttributeDelegate->Read(aPath);
108-
ChipLogProgress(Zcl, "TargetNavigatorManager::HandleGetCurrentTarget response %s", resStr);
107+
std::string resStr = mAttributeDelegate->Read(aPath);
108+
ChipLogProgress(Zcl, "TargetNavigatorManager::HandleGetCurrentTarget response %s", resStr.c_str());
109109

110-
if (resStr != nullptr && *resStr != 0)
110+
if (resStr.length() != 0)
111111
{
112112
Json::Reader reader;
113113
Json::Value value;

examples/tv-app/android/java/ContentAppAttributeDelegate.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ namespace AppPlatform {
3434

3535
using LaunchResponseType = chip::app::Clusters::ContentLauncher::Commands::LaunchResponse::Type;
3636

37-
const char * ContentAppAttributeDelegate::Read(const chip::app::ConcreteReadAttributePath & aPath)
37+
std::string ContentAppAttributeDelegate::Read(const chip::app::ConcreteReadAttributePath & aPath)
3838
{
3939
if (aPath.mEndpointId < FIXED_ENDPOINT_COUNT)
4040
{
41+
// returning blank string causes the caller to default to output required by the tests
4142
return "";
4243
}
43-
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
4444

45+
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
4546
ChipLogProgress(Zcl, "ContentAppAttributeDelegate::Read being called for endpoint %d cluster %d attribute %d",
4647
aPath.mEndpointId, aPath.mClusterId, aPath.mAttributeId);
4748

@@ -53,11 +54,15 @@ const char * ContentAppAttributeDelegate::Read(const chip::app::ConcreteReadAttr
5354
ChipLogError(Zcl, "Java exception in ContentAppAttributeDelegate::Read");
5455
env->ExceptionDescribe();
5556
env->ExceptionClear();
57+
// returning blank string causes the caller to default to output required by the tests
5658
return "";
5759
}
5860
const char * respStr = env->GetStringUTFChars(resp, 0);
61+
std::string retStr(respStr);
62+
env->ReleaseStringUTFChars(resp, respStr);
63+
env->DeleteLocalRef(resp);
5964
ChipLogProgress(Zcl, "ContentAppAttributeDelegate::Read got response %s", respStr);
60-
return respStr;
65+
return retStr;
6166
}
6267

6368
} // namespace AppPlatform

examples/tv-app/android/java/ContentAppAttributeDelegate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ContentAppAttributeDelegate
5151
env->DeleteGlobalRef(mContentAppEndpointManager);
5252
}
5353

54-
const char * Read(const chip::app::ConcreteReadAttributePath & aPath);
54+
std::string Read(const chip::app::ConcreteReadAttributePath & aPath);
5555

5656
private:
5757
void InitializeJNIObjects(jobject manager)

examples/tv-app/android/java/ContentAppCommandDelegate.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerCo
6060
return;
6161
}
6262

63-
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
64-
Json::Value value = json["value"];
65-
UtfString jsonString(env, JsonToString(value).c_str());
63+
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
64+
Json::Value value = json["value"];
65+
std::string payload = JsonToString(value);
66+
UtfString jsonString(env, payload.c_str());
6667

67-
ChipLogProgress(Zcl, "ContentAppCommandDelegate::InvokeCommand send command being called with payload %s",
68-
JsonToString(json).c_str());
68+
ChipLogProgress(Zcl, "ContentAppCommandDelegate::InvokeCommand send command being called with payload %s", payload.c_str());
6969

7070
jstring resp = (jstring) env->CallObjectMethod(
7171
mContentAppEndpointManager, mSendCommandMethod, static_cast<jint>(handlerContext.mRequestPath.mEndpointId),
@@ -82,6 +82,8 @@ void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerCo
8282
const char * respStr = env->GetStringUTFChars(resp, 0);
8383
ChipLogProgress(Zcl, "ContentAppCommandDelegate::InvokeCommand got response %s", respStr);
8484
FormatResponseData(handlerContext, respStr);
85+
env->ReleaseStringUTFChars(resp, respStr);
86+
env->DeleteLocalRef(resp);
8587
}
8688
else
8789
{

0 commit comments

Comments
 (0)