#ifndef TaskMngrService_h_
#define TaskMngrService_h_
+#include <rua_manager.h>
+
#include <string>
#include <vector>
-#include <app_info.h>
-#include <rua.h>
+#include <memory>
+
+#include "Logger.h"
namespace TaskMngr {
- struct AppInfo {
- std::string pkgid;
- std::string appid;
- std::string instanceId;
- std::string name;
- std::string icon;
- std::string uri;
- time_t launchTime;
+ class RuaInfo {
+ public:
+ explicit RuaInfo(rua_info_h raw) : m_Raw(raw) { }
+
+ ~RuaInfo()
+ {
+ if (m_Raw)
+ rua_info_destroy(m_Raw);
+ }
+
+ std::string getName();
+ std::string getIcon();
+ std::string getImage();
+
+ rua_info_h getRaw() const
+ {
+ return m_Raw;
+ }
+
+ private:
+ rua_info_h m_Raw = nullptr;
+ std::string m_Name;
+ std::string m_Icon;
+ std::string m_Image;
};
class TaskMngrService {
/**
* @brief Shutdowns given currently running application.
*
- * @param[in] appid The ID of the application
- * @param[in] instance_id The instance ID of the application
+ * @param[in] info The RUA information handle
*
* @return true or false on failure
*/
- bool killApp(const std::string &appid, const std::string &instanceId = std::string());
+ bool killApp(const RuaInfo& info);
+
/**
* @brief Resumes the given application.
*
- * @param[in] appid The ID of the application
- * @param[in] instanceId The instance ID of the application
- * @param[in] uri The URI of the data
+ * @param[in] info The RUA information handle
*
* @return true or false on failure
*/
- bool launchApp(const std::string &appid, const std::string &instanceId = std::string(), const std::string &uri = std::string());
+ bool launchApp(const RuaInfo& info);
/**
* @brief Get or create list of launched applications
*
* @return list with information about launched applications
*/
- std::vector<AppInfo> getAppInfo();
+ std::vector<std::shared_ptr<RuaInfo>> &getRuaInfo();
+ private:
+ bool deleteRua(const RuaInfo& info);
+ bool sendLaunchRequest(const RuaInfo& info);
+ static void OnAppControlResultCB(app_control_h request, app_control_error_e result, void *user_data);
private:
TaskMngrService(const TaskMngrService&) = delete;
TaskMngrService& operator=(TaskMngrService) = delete;
- void init();
- void finit();
- void filterCreate();
- void findAndAppendApp();
- bool appInfoCb(app_info_h app_info);
- void fillAppInfoList(char **table, int &rows, int &cols);
-
private:
- rua_rec m_RuaRecord;
- app_info_filter_h m_Filter;
- std::vector<AppInfo> m_AppInfo;
+ std::vector<std::shared_ptr<RuaInfo>> m_RuaInfo;
};
}
#include "Logger.h"
#include "Callback.h"
-#include <app_manager_extension.h>
#include <utility>
-#include <app_control.h>
-#include <app_manager.h>
using namespace TaskMngr;
-TaskMngrService::TaskMngrService()
- : m_RuaRecord()
- , m_Filter()
+std::string RuaInfo::getName()
{
- init();
+ if (!m_Name.empty())
+ return m_Name;
+
+ char* name = nullptr;
+ rua_info_get_instance_name(getRaw(), &name);
+ if (name == nullptr) {
+ rua_info_get_label(getRaw(), &name);
+ if (name == nullptr)
+ rua_info_get_app_id(getRaw(), &name);
+ }
+
+ std::unique_ptr<char, decltype(std::free)*> ptr(name, std::free);
+
+ m_Name = std::string(ptr.get());
+ return m_Name;
}
-TaskMngrService::~TaskMngrService()
+std::string RuaInfo::getIcon()
{
- finit();
+ if (!m_Icon.empty())
+ return m_Icon;
+
+ char* icon = nullptr;
+ rua_info_get_icon(getRaw(), &icon);
+ if (icon == nullptr)
+ return std::string("");
+
+ std::unique_ptr<char, decltype(std::free)*> ptr(icon, std::free);
+
+ m_Icon = std::string(ptr.get());
+ return m_Icon;
}
-bool TaskMngrService::killApp(const std::string &appid, const std::string &instanceId)
+std::string RuaInfo::getImage()
{
- TRACE;
- int ret = 0;
- bool running = false;
- app_context_h context = nullptr;
-
- if (instanceId.empty()) {
- app_manager_is_running(appid.c_str(), &running);
- if (running)
- app_manager_get_app_context(appid.c_str(), &context);
- } else {
- app_manager_get_app_context_by_instance_id(appid.c_str(), instanceId.c_str(), &context);
- }
-
- if (context) {
- app_manager_terminate_app(context);
- app_context_destroy(context);
- }
-
- LOG("terminate app = ", appid);
- ret = rua_delete_history_with_instance_id(appid.c_str(), instanceId.c_str());
-
- return !ret;
+ if (!m_Image.empty())
+ return m_Image;
+
+ char* image = nullptr;
+ rua_info_get_image(getRaw(), &image);
+ if (image == nullptr)
+ return std::string("");
+
+ std::unique_ptr<char, decltype(std::free)*> ptr(image, std::free);
+
+ m_Image = std::string(ptr.get());
+ return m_Image;
}
-bool TaskMngrService::launchApp(const std::string &appid, const std::string &instanceId, const std::string &uri)
+TaskMngrService::TaskMngrService() = default;
+TaskMngrService::~TaskMngrService() = default;
+
+bool TaskMngrService::killApp(const RuaInfo& info)
{
- TRACE;
- int ret = 0;
- app_control_h service = nullptr;
- app_context_h context = nullptr;
-
- if (!instanceId.empty())
- app_manager_get_app_context_by_instance_id(appid.c_str(), instanceId.c_str(), &context);
- else
- app_manager_get_app_context(appid.c_str(), &context);
-
- if (context) {
- LOG("Application ID is running ", appid);
- ret = app_manager_resume_app(context);
- app_context_destroy(context);
- if (ret == APP_MANAGER_ERROR_NONE)
- return true;
- else
- LOG("Failed to resume application ", appid);
- }
-
- app_control_create(&service);
- if (!service)
- return false;
-
- app_control_set_operation(service, APP_CONTROL_OPERATION_MAIN);
- app_control_set_app_id(service, appid.c_str());
-
- if (!uri.empty())
- app_control_set_uri(service, uri.c_str());
-
- ret = app_control_send_launch_request(service, nullptr, nullptr);
- app_control_destroy(service);
- if (ret != APP_CONTROL_ERROR_NONE) {
- LOG_ERROR("Failed to send launch request");
- return false;
- }
-
- return true;
+ rua_context_h context_inst;
+ int ret = rua_manager_get_rua_context_from_rua_info(info.getRaw(), &context_inst);
+ if (ret != RUA_ERROR_NONE) {
+ LOG_ERROR("Failed to get rua context from rua info. error(%d)", ret);
+ return false;
+ }
+
+ std::unique_ptr<std::remove_pointer<rua_context_h>::type, decltype(rua_context_destroy)*> handle(context_inst, rua_context_destroy);
+
+ bool running;
+ ret = rua_manager_is_running(handle.get(), &running);
+ if (ret != RUA_ERROR_NONE) {
+ LOG_ERROR("Failed to check running state. error(%d)", ret);
+ return false;
+ }
+
+ if (!running) {
+ LOG_WARN("The application is not running");
+ return deleteRua(info);
+ }
+
+ ret = rua_manager_terminate(handle.get());
+ if (ret != RUA_ERROR_NONE) {
+ LOG_ERROR("Failed to send terminate request. error(%d)", ret);
+ return false;
+ }
+
+ return deleteRua(info);
}
-std::vector<AppInfo> TaskMngrService::getAppInfo()
+bool TaskMngrService::launchApp(const RuaInfo& info)
{
- m_AppInfo.clear();
+ rua_context_h context_inst;
+ int ret = rua_manager_get_rua_context_from_rua_info(info.getRaw(), &context_inst);
+ if (ret != RUA_ERROR_NONE) {
+ LOG_ERROR("Failed to get rua context from rua info. error(%d)", ret);
+ return false;
+ }
- char **table = nullptr;
- int rows = 0;
- int cols = 0;
+ std::unique_ptr<std::remove_pointer<rua_context_h>::type, decltype(rua_context_destroy)*> handle(context_inst, rua_context_destroy);
- if (rua_history_load_db(&table, &rows, &cols)) {
- LOG_ERROR("rua_history_load_db failed!");
- }
+ bool running;
+ ret = rua_manager_is_running(handle.get(), &running);
+ if (ret != RUA_ERROR_NONE || !running)
+ return sendLaunchRequest(info);
- if (table) {
- fillAppInfoList(table, rows, cols);
- rua_history_unload_db(&table);
- }
+ ret = rua_manager_resume(handle.get());
+ if (ret != RUA_ERROR_NONE)
+ return sendLaunchRequest(info);
- return m_AppInfo;
+ return true;
}
-void TaskMngrService::init()
+std::vector<std::shared_ptr<RuaInfo>> &TaskMngrService::getRuaInfo()
{
- if (rua_init()) {
- LOG_ERROR("rua_init failed!");
- }
-
- filterCreate();
+ m_RuaInfo.clear();
+
+ int ret = rua_manager_foreach_rua_info(
+ [](rua_info_h info, void* user_data)->bool {
+ bool taskmanage;
+ int ret = rua_info_is_managed_by_task_manager(info, &taskmanage);
+ if (ret != RUA_ERROR_NONE) {
+ LOG_ERROR("Failed to check taskmanage. error(%d)", ret);
+ return false;
+ }
+
+ if (!taskmanage)
+ return true;
+
+ rua_info_h clone;
+ ret = rua_info_clone(info, &clone);
+ if (ret != RUA_ERROR_NONE) {
+ LOG_ERROR("Failed to clone rua info. error(%d)", ret);
+ return false;
+ }
+
+ TaskMngrService* taskmngr = static_cast<TaskMngrService*>(user_data);
+ taskmngr->m_RuaInfo.emplace_back(new RuaInfo(clone));
+ return true;
+ }, this);
+ if (ret != RUA_ERROR_NONE)
+ LOG_ERROR("Failed to retrieve rua info. error(%d)", ret);
+
+ return m_RuaInfo;
}
-void TaskMngrService::finit()
+bool TaskMngrService::deleteRua(const RuaInfo& info)
{
- app_info_filter_destroy(m_Filter);
- rua_fini();
-}
+ int ret = rua_manager_delete_rua_info(info.getRaw());
+ if (ret != RUA_ERROR_NONE) {
+ LOG_ERROR("Failed to delete rua info. error(%d)", ret);
+ return false;
+ }
-void TaskMngrService::filterCreate()
-{
- app_info_filter_create(&m_Filter);
- app_info_filter_add_bool(m_Filter, PACKAGE_INFO_PROP_APP_TASKMANAGE, true);
+ return true;
}
-void TaskMngrService::findAndAppendApp()
+bool TaskMngrService::sendLaunchRequest(const RuaInfo& info)
{
- app_info_filter_add_string(m_Filter, PACKAGE_INFO_PROP_APP_ID, m_RuaRecord.pkg_name);
- app_info_filter_foreach_appinfo(m_Filter, makeCbLast(&TaskMngrService::appInfoCb), this);
+ app_control_h app_control_inst;
+ int ret = rua_manager_get_app_control_from_rua_info(info.getRaw(), &app_control_inst);
+ if (ret != RUA_ERROR_NONE) {
+ LOG_ERROR("Failed to get app control from rua info. error(%d)", ret);
+ return false;
+ }
+
+ std::unique_ptr<std::remove_pointer<app_control_h>::type, decltype(app_control_destroy)*> handle(app_control_inst, app_control_destroy);
+
+ ret = app_control_send_launch_request_async(handle.get(), OnAppControlResultCB, nullptr, nullptr);
+ if (ret != APP_CONTROL_ERROR_NONE) {
+ LOG_ERROR("Failed to send launch request. error(%d)", ret);
+ return false;
+ }
+
+ return true;
}
-bool TaskMngrService::appInfoCb(app_info_h appInfo)
+void TaskMngrService::OnAppControlResultCB(app_control_h request, app_control_error_e result, void *user_data)
{
- AppInfo item;
- char *variable = nullptr;
- int ret = 0;
-
- app_info_get_package(appInfo, &variable);
- if (variable) {
- item.pkgid = variable;
- free(variable);
- variable = nullptr;
- }
-
- app_info_get_app_id(appInfo, &variable);
- if (variable) {
- item.appid = variable;
- free(variable);
- variable = nullptr;
- }
-
- if (m_RuaRecord.uri)
- item.uri = m_RuaRecord.uri;
-
- if (m_RuaRecord.instance_id)
- item.instanceId = m_RuaRecord.instance_id;
-
- if (m_RuaRecord.instance_name) {
- item.name = m_RuaRecord.instance_name;
- } else {
- app_info_get_label(appInfo, &variable);
- if (variable) {
- item.name = variable;
- free(variable);
- variable = nullptr;
- }
- }
-
- if (m_RuaRecord.image) {
- item.icon = m_RuaRecord.image; // snapshot
- } else if (m_RuaRecord.icon) {
- item.icon = m_RuaRecord.icon; // icon
- } else {
- ret = app_info_get_icon(appInfo, &variable); // icon
- if (variable) {
- item.icon = variable;
- free(variable);
- variable = nullptr;
- } else {
- LOG_WARN("icon is null");
- }
- }
- item.launchTime = m_RuaRecord.launch_time;
-
- if (item.name.empty() && item.icon.empty())
- return false;
-
- m_AppInfo.push_back(std::move(item));
-
- return true;
-}
+ char *id;
+ int ret = app_control_get_app_id(request, &id);
+ if (ret != APP_CONTROL_ERROR_NONE) {
+ LOG_ERROR("Failed to get application ID");
+ return;
+ }
-void TaskMngrService::fillAppInfoList(char **table, int &rows, int &cols)
-{
- for (int row = 0; row < rows; ++row) {
- rua_history_get_rec(&m_RuaRecord, table, rows, cols, row);
- findAndAppendApp();
- }
+ std::unique_ptr<char, decltype(std::free)*> appid(id, std::free);
+
+ LOG_INFO("app_id = %s, result = %d", appid.get(), result);
}