Skip to content

Commit 190d18c

Browse files
authored
[package_info_plus] Minor refactoring (#368)
1 parent 4b20eb6 commit 190d18c

File tree

1 file changed

+52
-59
lines changed

1 file changed

+52
-59
lines changed

packages/package_info_plus/tizen/src/package_info_plus_tizen_plugin.cc

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "log.h"
1717

18+
namespace {
19+
1820
class PackageInfoPlusTizenPlugin : public flutter::Plugin {
1921
public:
2022
static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) {
@@ -41,89 +43,80 @@ class PackageInfoPlusTizenPlugin : public flutter::Plugin {
4143
void HandleMethodCall(
4244
const flutter::MethodCall<flutter::EncodableValue> &method_call,
4345
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
44-
LOG_INFO("method : %s", method_call.method_name().data());
45-
std::string replay = "";
46+
const auto &method_name = method_call.method_name();
4647

47-
if (method_call.method_name().compare("getAll") != 0) {
48-
result->Error("-1", "Not supported method");
48+
if (method_name != "getAll") {
49+
result->NotImplemented();
4950
return;
5051
}
5152

52-
char *app_id = nullptr;
53-
char *pkg_name = nullptr;
54-
char *version = nullptr;
55-
char *label = nullptr;
56-
package_info_h package_info = nullptr;
57-
flutter::EncodableMap msg;
53+
flutter::EncodableMap map;
5854

59-
int ret = app_get_id(&app_id);
60-
if (ret != APP_ERROR_NONE || app_id == nullptr) {
61-
result->Error(std::to_string(ret), "Failed to get app_id",
55+
char *id = nullptr;
56+
int ret = app_get_id(&id);
57+
if (ret != APP_ERROR_NONE) {
58+
result->Error(std::to_string(ret), "Failed to get find the app ID.",
6259
flutter::EncodableValue(get_error_message(ret)));
63-
goto cleanup;
60+
return;
6461
}
65-
LOG_INFO("app id : %s\n", app_id);
62+
auto app_id = std::string(id);
63+
free(id);
6664

67-
ret = package_info_create(app_id, &package_info);
68-
if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == nullptr) {
69-
result->Error(std::to_string(ret), "Failed to create package_info",
65+
package_info_h package_info = nullptr;
66+
ret = package_info_create(app_id.c_str(), &package_info);
67+
if (ret != PACKAGE_MANAGER_ERROR_NONE) {
68+
result->Error(std::to_string(ret),
69+
"Failed to create a package_info handle.",
7070
flutter::EncodableValue(get_error_message(ret)));
71-
goto cleanup;
71+
return;
7272
}
7373

74+
char *label = nullptr;
7475
ret = package_info_get_label(package_info, &label);
75-
if (ret != PACKAGE_MANAGER_ERROR_NONE || label == nullptr) {
76-
result->Error(std::to_string(ret), "Failed to get app name",
76+
if (ret != PACKAGE_MANAGER_ERROR_NONE) {
77+
result->Error(std::to_string(ret), "Failed to get the app label.",
7778
flutter::EncodableValue(get_error_message(ret)));
78-
goto cleanup;
79+
package_info_destroy(package_info);
80+
return;
7981
}
80-
LOG_INFO("package label : %s\n", label);
81-
82-
ret = package_info_get_package(package_info, &pkg_name);
83-
if (ret != PACKAGE_MANAGER_ERROR_NONE || pkg_name == nullptr) {
84-
result->Error(std::to_string(ret), "Failed to get package name",
82+
map[flutter::EncodableValue("appName")] =
83+
flutter::EncodableValue(std::string(label));
84+
free(label);
85+
86+
char *package_name = nullptr;
87+
ret = package_info_get_package(package_info, &package_name);
88+
if (ret != PACKAGE_MANAGER_ERROR_NONE) {
89+
result->Error(std::to_string(ret), "Failed to get the package name.",
8590
flutter::EncodableValue(get_error_message(ret)));
86-
goto cleanup;
91+
package_info_destroy(package_info);
92+
return;
8793
}
88-
LOG_INFO("package name : %s\n", pkg_name);
94+
map[flutter::EncodableValue("packageName")] =
95+
flutter::EncodableValue(std::string(package_name));
96+
free(package_name);
8997

98+
char *version = nullptr;
9099
ret = package_info_get_version(package_info, &version);
91-
if (ret != PACKAGE_MANAGER_ERROR_NONE || version == nullptr) {
92-
result->Error(std::to_string(ret), "Failed to get version",
100+
if (ret != PACKAGE_MANAGER_ERROR_NONE) {
101+
result->Error(std::to_string(ret), "Failed to get the package version.",
93102
flutter::EncodableValue(get_error_message(ret)));
94-
goto cleanup;
95-
}
96-
LOG_INFO("package version : %s\n", version);
97-
98-
msg.insert(std::pair<flutter::EncodableValue, flutter::EncodableValue>(
99-
"appName", std::string(label)));
100-
msg.insert(std::pair<flutter::EncodableValue, flutter::EncodableValue>(
101-
"packageName", std::string(pkg_name)));
102-
msg.insert(std::pair<flutter::EncodableValue, flutter::EncodableValue>(
103-
"version", std::string(version)));
104-
msg.insert(std::pair<flutter::EncodableValue, flutter::EncodableValue>(
105-
"buildNumber", "Not supported property"));
106-
result->Success(flutter::EncodableValue(msg));
107-
108-
cleanup:
109-
if (app_id) {
110-
free(app_id);
111-
}
112-
if (label) {
113-
free(label);
114-
}
115-
if (pkg_name) {
116-
free(pkg_name);
117-
}
118-
if (version) {
119-
free(version);
120-
}
121-
if (package_info) {
122103
package_info_destroy(package_info);
104+
return;
123105
}
106+
map[flutter::EncodableValue("version")] =
107+
flutter::EncodableValue(std::string(version));
108+
free(version);
109+
package_info_destroy(package_info);
110+
111+
map[flutter::EncodableValue("buildNumber")] =
112+
flutter::EncodableValue("Not supported property");
113+
114+
result->Success(flutter::EncodableValue(map));
124115
}
125116
};
126117

118+
} // namespace
119+
127120
void PackageInfoPlusTizenPluginRegisterWithRegistrar(
128121
FlutterDesktopPluginRegistrarRef registrar) {
129122
PackageInfoPlusTizenPlugin::RegisterWithRegistrar(

0 commit comments

Comments
 (0)