forked from flutter-tizen/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[permission_handler] Refactor the C++ code (flutter-tizen#379)
* [permission_handler] Refactor the C++ code * Update the device limitations section in README
- Loading branch information
Showing
10 changed files
with
298 additions
and
551 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## NEXT | ||
|
||
* Code refactoring. | ||
|
||
## 1.2.0 | ||
|
||
* Update permission_handler to 8.3.0. | ||
|
164 changes: 62 additions & 102 deletions
164
packages/permission_handler/tizen/src/app_settings_manager.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,86 @@ | ||
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "app_settings_manager.h" | ||
|
||
#include <app_common.h> | ||
#include <app_control.h> | ||
#include <package_manager.h> | ||
|
||
#include <string> | ||
|
||
#include "log.h" | ||
|
||
namespace { | ||
constexpr char kPackageID[] = "pkgId"; | ||
constexpr char kSettingAppID[] = "com.samsung.clocksetting.apps"; | ||
|
||
class AppPermissions { | ||
public: | ||
AppPermissions() { Init(); } | ||
|
||
~AppPermissions() { Deinit(); } | ||
|
||
bool Launch(const std::string& package_name) { | ||
if (package_name.length() == 0) { | ||
return false; | ||
} | ||
|
||
int ret = app_control_add_extra_data(app_control_, kPackageID, | ||
package_name.c_str()); | ||
if (ret != APP_CONTROL_ERROR_NONE) { | ||
LOG_ERROR("Failed to add key[%s]:value[%s]", kPackageID, | ||
package_name.c_str()); | ||
return false; | ||
} | ||
|
||
ret = app_control_send_launch_request(app_control_, nullptr, nullptr); | ||
if (ret != APP_CONTROL_ERROR_NONE) { | ||
LOG_ERROR("Failed to send launch request setting"); | ||
return false; | ||
} | ||
return true; | ||
|
||
constexpr char kSettingAppId[] = "com.samsung.clocksetting.apps"; | ||
|
||
std::string GetPackageName() { | ||
char* app_id; | ||
int ret = app_get_id(&app_id); | ||
if (ret != APP_ERROR_NONE) { | ||
LOG_ERROR("The app ID is not found."); | ||
return ""; | ||
} | ||
|
||
private: | ||
void Init() { | ||
int ret = app_control_create(&app_control_); | ||
if (ret != APP_CONTROL_ERROR_NONE) { | ||
LOG_ERROR("Failed to create app control handle"); | ||
} | ||
|
||
ret = app_control_set_app_id(app_control_, kSettingAppID); | ||
if (ret != APP_CONTROL_ERROR_NONE) { | ||
LOG_ERROR("Failed to set app id[%s]", kSettingAppID); | ||
} | ||
package_info_h package_info; | ||
ret = package_info_create(app_id, &package_info); | ||
free(app_id); | ||
if (ret != PACKAGE_MANAGER_ERROR_NONE) { | ||
LOG_ERROR("Failed to create a package info handle."); | ||
return ""; | ||
} | ||
|
||
void Deinit() { | ||
if (app_control_) { | ||
app_control_destroy(app_control_); | ||
app_control_ = nullptr; | ||
} | ||
char* package_name; | ||
ret = package_info_get_package(package_info, &package_name); | ||
package_info_destroy(package_info); | ||
if (ret != PACKAGE_MANAGER_ERROR_NONE) { | ||
LOG_ERROR("Failed to get the package name."); | ||
return ""; | ||
} | ||
|
||
app_control_h app_control_{nullptr}; | ||
}; | ||
|
||
class PackageName { | ||
public: | ||
PackageName() { Init(); } | ||
|
||
~PackageName() { Deinit(); } | ||
|
||
char* Get() { return package_name_; } | ||
|
||
private: | ||
void Init() { | ||
int ret = app_get_id(&app_id_); | ||
if (ret != APP_ERROR_NONE || app_id_ == nullptr) { | ||
LOG_ERROR("Failed to get app id"); | ||
return; | ||
} | ||
|
||
ret = package_info_create(app_id_, &package_info_); | ||
if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info_ == nullptr) { | ||
LOG_ERROR("Failed to create package info handle"); | ||
} | ||
ret = package_info_get_package(package_info_, &package_name_); | ||
if (ret != PACKAGE_MANAGER_ERROR_NONE || package_name_ == nullptr) { | ||
LOG_ERROR("Failed to get package name"); | ||
} | ||
std::string result = std::string(package_name); | ||
free(package_name); | ||
|
||
return result; | ||
} | ||
|
||
} // namespace | ||
|
||
bool AppSettingsManager::OpenAppSettings() { | ||
std::string package_name = GetPackageName(); | ||
if (package_name.empty()) { | ||
return false; | ||
} | ||
|
||
void Deinit() { | ||
if (app_id_) { | ||
free(app_id_); | ||
app_id_ = nullptr; | ||
} | ||
|
||
if (package_info_) { | ||
package_info_destroy(package_info_); | ||
package_info_ = nullptr; | ||
} | ||
|
||
if (package_name_) { | ||
free(package_name_); | ||
package_name_ = nullptr; | ||
} | ||
app_control_h app_control; | ||
int ret = app_control_create(&app_control); | ||
if (ret != APP_CONTROL_ERROR_NONE) { | ||
LOG_ERROR("Failed to create an app control handle."); | ||
return false; | ||
} | ||
|
||
char* app_id_{nullptr}; | ||
package_info_h package_info_{nullptr}; | ||
char* package_name_{nullptr}; | ||
}; | ||
} // namespace | ||
ret = app_control_set_app_id(app_control, kSettingAppId); | ||
if (ret != APP_CONTROL_ERROR_NONE) { | ||
LOG_ERROR("Failed to set an app ID."); | ||
app_control_destroy(app_control); | ||
return false; | ||
} | ||
|
||
AppSettingsManager::AppSettingsManager() { | ||
app_permissions_ = std::make_unique<AppPermissions>(); | ||
PackageName pkg_name; | ||
char* name = pkg_name.Get(); | ||
if (name == nullptr) { | ||
return; | ||
ret = app_control_add_extra_data(app_control, "pkgId", package_name.c_str()); | ||
if (ret != APP_CONTROL_ERROR_NONE) { | ||
LOG_ERROR("Failed to add extra data."); | ||
app_control_destroy(app_control); | ||
return false; | ||
} | ||
package_name_ = std::string(name); | ||
} | ||
|
||
AppSettingsManager::~AppSettingsManager() {} | ||
ret = app_control_send_launch_request(app_control, nullptr, nullptr); | ||
app_control_destroy(app_control); | ||
if (ret != APP_CONTROL_ERROR_NONE) { | ||
LOG_ERROR("Failed to send a launch request."); | ||
return false; | ||
} | ||
|
||
bool AppSettingsManager::OpenAppSettings() { | ||
return app_permissions_->Launch(package_name_); | ||
return true; | ||
} |
24 changes: 8 additions & 16 deletions
24
packages/permission_handler/tizen/src/app_settings_manager.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,16 @@ | ||
#ifndef APP_SETTINGS_MANAGER_H_ | ||
#define APP_SETTINGS_MANAGER_H_ | ||
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <string> | ||
|
||
namespace { | ||
class AppPermissions; | ||
} | ||
#ifndef FLUTTER_PLUGIN_APP_SETTINGS_MANAGER_H_ | ||
#define FLUTTER_PLUGIN_APP_SETTINGS_MANAGER_H_ | ||
|
||
class AppSettingsManager { | ||
public: | ||
AppSettingsManager(); | ||
~AppSettingsManager(); | ||
AppSettingsManager() {} | ||
~AppSettingsManager() {} | ||
|
||
bool OpenAppSettings(); | ||
|
||
private: | ||
std::unique_ptr<AppPermissions> app_permissions_; | ||
std::string package_name_; | ||
}; | ||
|
||
#endif // APP_SETTINGS_MANAGER_H_ | ||
#endif // FLUTTER_PLUGIN_APP_SETTINGS_MANAGER_H_ |
Oops, something went wrong.