revise pkg actions(changedCB)
authorYoungjae Shin <yj99.shin@samsung.com>
Mon, 9 Mar 2020 02:57:48 +0000 (11:57 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 19 Mar 2020 04:30:37 +0000 (13:30 +0900)
pkg/PkgAction.h
pkg/PkgEnableSupportMode.cpp
pkg/PkgEnableSupportMode.h
pkg/PkgFactory.cpp
pkg/PkgFactory.h
pkg/PkgPlugin.cpp
pkg/tizen_pkg_rule.xml
unittests/mdsp_test_pkg.cpp
unittests/mode/tizen_Power-Save_mode.xml

index b5cb7db3c003ee5f3e5e14a489412b9da8dc7169..30eb892a4173d340002caf35f751f702d5c15d59 100644 (file)
@@ -27,16 +27,12 @@ public:
                : PluginAction(name)
        {
        }
-       virtual ~PkgAction() = default;
+       ~PkgAction() = default;
 
        virtual int set(int val)
        {
                return MODES_ERROR_NOT_SUPPORTED;
        }
-       virtual int get(int *val)
-       {
-               return MODES_ERROR_NOT_SUPPORTED;
-       }
 };
 
 MODES_NAMESPACE_END
index dae6ffab0db754fff9dcb409c4666d9ffb73de3d..bfcf38d25272118433a5958dc5e8630718ac01da 100644 (file)
 
 MODES_NAMESPACE_USE;
 
-const std::string PkgStartSupportMode::NAME = "startSupportMode";
-int PkgStartSupportMode::appModeVal = 0;
+const std::string PkgSupportMode::NAME = "supportMode";
 
-PkgStartSupportMode::PkgStartSupportMode()
-       : PkgAction(NAME)
+PkgSupportMode::PkgSupportMode()
+       : PkgAction(NAME), requestVal(0)
 {
 }
 
-int PkgStartSupportMode::app_list_cb(const pkgmgrinfo_appinfo_h handle, void *userData)
-{
-       std::list<std::string> *appList = (std::list<std::string>*)userData;
-
-       int ret;
-       int appSupportMode = 0;
-       ret = pkgmgrinfo_appinfo_get_support_mode(handle, &appSupportMode);
-       if (PMINFO_R_OK != ret)
-               ERR("pkgmgrinfo_appinfo_get_support_mode() Fail");
-
-
-       if (appSupportMode != appModeVal) {
-               char *appid = NULL;
-               ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
-               if (PMINFO_R_OK != ret) {
-                       ERR("pkgmgrinfo_appinfo_get_appid() Fail");
-                       return 0;
-               }
-
-               appList->push_back(appid);
-       }
-       return 0;
-}
-
-int PkgStartSupportMode::set(int val)
+int PkgSupportMode::set(int val)
 {
        int ret;
        pkgmgrinfo_appinfo_filter_h filter;
 
        ret = pkgmgrinfo_appinfo_filter_create(&filter);
-       if (ret != PMINFO_R_OK)
-               return ret;
+       if (ret != PMINFO_R_OK) {
+               ERR("pkgmgrinfo_appinfo_filter_create() Fail(%d)", ret);
+               return MODES_ERROR_SYSTEM;
+       }
 
        switch (val) {
        case ULTRA_POWER_SAVING:
-               appModeVal = APP_SUPPORT_MODE_ULTRA_POWER_SAVING_VAL;
-               break;
        case COOL_DOWN:
-               appModeVal = APP_SUPPORT_MODE_COOL_DOWN_VAL;
-               break;
        case SCREEN_READER:
-               appModeVal = APP_SUPPORT_MODE_SCREEN_READER_VAL;
+               requestVal = val;
                break;
        default:
-               ERR("invalid Mode(%d)", val);
+               ERR("Unknown Mode(%d)", val);
                return MODES_ERROR_INVALID_PARAMETER;
        }
 
-       pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, app_list_cb, &appList);
+       pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, appListCB, this);
        pkgmgrinfo_appinfo_filter_destroy(filter);
 
        int nApps = 0;
-       const char *appIDs[appList.size()];
-       for (auto it = appList.begin(); it != appList.end(); it++, nApps++) {
+       const char *appIDs[inactiveApps.size()];
+       for (auto it = inactiveApps.begin(); it != inactiveApps.end(); it++, nApps++) {
                appIDs[nApps] = it->c_str();
-               DBG("Deactivate App(%s)", appIDs[nApps]);
+               INFO("Deactivate App(%s)", appIDs[nApps]);
        }
 
        pkgmgr_client *pc = pkgmgr_client_new(PC_REQUEST);
 
        int reqId = pkgmgr_client_deactivate_apps(pc, appIDs, nApps, NULL, NULL);
+       if (reqId < 0)
+               ERR("pkgmgr_client_deactivate_apps() Fail(%d)", reqId);
        DBG("Request id[%d] count[%d]", reqId, nApps);
 
        pkgmgr_client_free(pc);
@@ -101,53 +76,79 @@ int PkgStartSupportMode::set(int val)
        return MODES_ERROR_NONE;
 }
 
-int PkgStartSupportMode::get(int *val)
-{
-       if (val)
-               *val = appModeVal;
-
-       return MODES_ERROR_NONE;
-}
-
-
-void PkgStartSupportMode::undo()
+void PkgSupportMode::undo()
 {
        //If the appList is empty, it will be ignored at subroutines.
-
        int nApps = 0;
-       const char *appIDs[appList.size()];
-       for (auto it = appList.begin(); it != appList.end(); it++, nApps++) {
+       const char *appIDs[inactiveApps.size()];
+       for (auto it = inactiveApps.begin(); it != inactiveApps.end(); it++, nApps++) {
                appIDs[nApps] = it->c_str();
-               DBG("Activate App(%s)", appIDs[nApps]);
+               INFO("Activate App(%s)", appIDs[nApps]);
        }
 
        pkgmgr_client *pc = pkgmgr_client_new(PC_REQUEST);
 
        int reqId = pkgmgr_client_activate_apps(pc, appIDs, nApps, NULL, NULL);
-       WARN("Request id[%d] count[%d]", reqId, nApps);
+       if (reqId < 0)
+               ERR("pkgmgr_client_activate_apps() Fail(%d)", reqId);
+       DBG("Request id[%d] count[%d]", reqId, nApps);
 
        pkgmgr_client_free(pc);
 
        //The caller(Plugin undo function) includes destruction of this instance.
-       //Therefore, appList will be cleared by the destructor.
+       //Therefore, appList will be automatically cleared by the destructor.
 }
 
-std::string PkgStartSupportMode::serialize()
+std::string PkgSupportMode::serialize()
 {
        std::ostringstream ostr;
-       for (auto it = appList.begin(); it != appList.end(); it++)
+       for (auto it = inactiveApps.begin(); it != inactiveApps.end(); it++)
                ostr << *it << delimiter;
 
        return ostr.str();
 }
 
-int PkgStartSupportMode::parse(const std::string &data)
+int PkgSupportMode::parse(const std::string &data)
 {
        size_t pos;
        size_t start = 0;
        while ((pos = data.find(delimiter, start)) != std::string::npos) {
-               appList.push_back(data.substr(start, pos - start));
+               inactiveApps.push_back(data.substr(start, pos - start));
                start = pos + 1;
        }
        return MODES_ERROR_NONE;
 }
+
+int PkgSupportMode::setChangedCallback(valueChangedCB callback, void *userData)
+{
+       ERR("Not Support Changed Callback");
+       return MODES_ERROR_NOT_SUPPORTED;
+}
+
+void PkgSupportMode::unSetChangedCallback(valueChangedCB callback, void *userData)
+{
+       ERR("Not Support Changed Callback");
+}
+
+int PkgSupportMode::appListCB(const pkgmgrinfo_appinfo_h handle, void *userData)
+{
+       PkgSupportMode *action = (PkgSupportMode*)userData;
+
+       RETV_IF(nullptr == userData, 0);
+
+       int appSupportMode = 0;
+       int ret = pkgmgrinfo_appinfo_get_support_mode(handle, &appSupportMode);
+       if (PMINFO_R_OK != ret)
+               ERR("pkgmgrinfo_appinfo_get_support_mode() Fail");
+
+       if (appSupportMode != action->requestVal) {
+               char *appid;
+               ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
+               if (PMINFO_R_OK != ret) {
+                       ERR("pkgmgrinfo_appinfo_get_appid() Fail");
+                       return 0;
+               }
+               action->inactiveApps.push_back(appid);
+       }
+       return 0;
+}
index a9a075b0698a9b03d617f260bfee271e079c0cfd..c66fb170d7e8334f58ef7c9e89c7c443b664337e 100644 (file)
 
 MODES_NAMESPACE_BEGIN
 
-class PkgStartSupportMode : public PkgAction {
+class PkgSupportMode : public PkgAction {
 public:
-       static const std::string NAME;
-       PkgStartSupportMode();
+       PkgSupportMode();
 
        int set(int val) override;
-       int get(int *val) override;
        void undo() override;
        std::string serialize() override;
        int parse(const std::string &data) override;
+       int setChangedCallback(valueChangedCB callback, void *userData) override;
+       void unSetChangedCallback(valueChangedCB callback, void *userData) override;
+
+       static const std::string NAME;
 private:
-       enum AppSupportMode {
-               ULTRA_POWER_SAVING = 1,
-               COOL_DOWN = 2,
-               SCREEN_READER = 4
+       enum SupportModeType {
+               ULTRA_POWER_SAVING = APP_SUPPORT_MODE_ULTRA_POWER_SAVING_VAL,
+               COOL_DOWN = APP_SUPPORT_MODE_COOL_DOWN_VAL,
+               SCREEN_READER = APP_SUPPORT_MODE_SCREEN_READER_VAL
        };
-       const char delimiter = '#';
+       static int appListCB(const pkgmgrinfo_appinfo_h handle, void *userData);
 
-       static int app_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data);
-       static int appModeVal;
-       std::list<std::string> appList;
+       const char delimiter = '#';
+       int requestVal;
+       std::list<std::string> inactiveApps;
 };
 
 MODES_NAMESPACE_END
index e279d5e5a147ea07f555a3430edc7665477e6d0d..0183e3f089ff533e54124bfdd1cd87c17f87e696 100644 (file)
@@ -21,21 +21,21 @@ MODES_NAMESPACE_USE;
 
 PkgFactory::PkgFactory()
 {
-       actionMap[PkgStartSupportMode::NAME] = PKG_ACT_STARTSUPPORTMODE;
+       actionMap[PkgSupportMode::NAME] = PKG_ACT_SUPPORTMODE;
 }
 
 PkgAction* PkgFactory::createAction(const std::string &key)
 {
        auto search = actionMap.find(key);
-       if (search == actionMap.end()) {
+       if (actionMap.end() == search) {
                ERR("No PkgAction(%s)", key.c_str());
                return nullptr;
        }
 
        PkgAction *action;
        switch (search->second) {
-       case PKG_ACT_STARTSUPPORTMODE:
-               action = new PkgStartSupportMode();
+       case PKG_ACT_SUPPORTMODE:
+               action = new PkgSupportMode();
                break;
        default:
                action = nullptr;
index ecbc41db9db78ceff6907d9c736ab7c13fcccc44..4503735833ca1023f3e1bb9056f5a343ef774d6a 100644 (file)
@@ -30,7 +30,7 @@ public:
        void destroyAction(PkgAction *action);
 private:
        enum actionKey{
-               PKG_ACT_STARTSUPPORTMODE
+               PKG_ACT_SUPPORTMODE
        };
 
        std::map<std::string, enum actionKey> actionMap;
index b833c641777f39f26fc37c6818bb355a308b9d92..fa80c899dfeb6f862aa774d1dcf98748ff8d7a2c 100644 (file)
@@ -26,7 +26,7 @@ MODES_NAMESPACE_USE;
 class PkgPlugin : public Plugin {
 public:
        PkgPlugin();
-       ~PkgPlugin();
+       ~PkgPlugin() = default;
 
        int set(const std::string &key, int val, PluginAction **piAction) override;
        void undo(PluginAction *piAction) override;
@@ -50,17 +50,11 @@ PkgPlugin::PkgPlugin()
        setName("pkg");
 }
 
-PkgPlugin::~PkgPlugin()
-{
-}
-
 int PkgPlugin::set(const std::string &key, int val, PluginAction **piAction)
 {
        PkgAction *action = pkgFactory.createAction(key);
        RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str());
 
-       DBG("Action(%s) set(%d)", key.c_str(), val);
-
        int ret = action->set(val);
        if (piAction)
                *piAction = action;
@@ -74,8 +68,6 @@ void PkgPlugin::undo(PluginAction *piAction)
        PkgAction *action = static_cast<PkgAction*>(piAction);
        RET_IF(nullptr == piAction);
 
-       DBG("Action(%s) undo", action->getName().c_str());
-
        action->undo();
        pkgFactory.destroyAction(action);
 }
index 58b6055c652182da46542fb00935a588630cac5f..d4dc50abf805314f6bd35b6346b0b1c3c560591b 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <tizenModes xmlns="http://www.tizen.org" version="6.0">
   <actionRule>
-    <rule name="pkg.startSupportMode" type="int" since="6.0" plugin="pkg">
+    <rule name="pkg.supportMode" type="int" since="6.0" plugin="pkg">
       <alias name="ULTRA_POWER_SAVING">1</alias>
       <alias name="COOL_DOWN">2</alias>
       <alias name="SCREEN_READER">4</alias>
index 57fb3de4b0763ab9a4bb18e3aa4bb2ef868d20ab..f8d441b202e00d18c19fde414bd7ff3a467ab21e 100644 (file)
@@ -57,7 +57,7 @@ GMainLoop *PkgPluginTest::loop = NULL;
 TEST_F(PkgPluginTest, setUndoTest)
 {
        PluginAction *action;
-       int ret = plugin->set("startSupportMode", 1, &action);
+       int ret = plugin->set("supportMode", 1, &action);
        EXPECT_EQ(MODES_ERROR_NONE, ret);
 
        g_idle_add(pkgPluginIdler, plugin);
index a113a2e5125d08661a041078044bb1d7f242b2b8..e8531ef36a779f7270078be9bd8334607646e442 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <tizenModes xmlns="http://www.tizen.org" version="6.0">
   <mode name="Power-Save" type="normal">
-    <action rule="pkg.startSupportMode" restrict="lock">ULTRA_POWER_SAVING</action>
+    <action rule="pkg.supportMode" restrict="lock">ULTRA_POWER_SAVING</action>
     <action rule="wifi.power" restrict="lock">off</action>
     <action rule="bluetooth.power" restrict="lock">off</action>
     <action rule="display.brightness" restrict="lock">30</action>