Update managing layout information
[apps/agl-service-windowmanager.git] / src / policy_manager / policy_manager.cpp
index 2429cf2..6f12795 100644 (file)
@@ -44,6 +44,7 @@ typedef struct LayoutState {
     std::map<std::string, int> category_num;
 //    int category_num[stm::gStmCategoryNoNum];
     std::vector<AreaState> area_list;
+    std::map<std::string, std::string> last_invisibled_role;
 } LayoutState;
 
 typedef struct LayerState {
@@ -247,6 +248,7 @@ static int checkPolicy(sd_event_source *source, void *data) {
     std::string category = std::string(stm::gStmCategoryName[category_no]);
     std::string area = std::string(stm::gStmAreaName[area_no]);
 
+    // Update layers
     for (int layer_no = stm::gStmLayerNoMin;
          layer_no <= stm::gStmLayerNoMax; layer_no++) {
         const char* layer_name = stm::gStmLayerName[layer_no];
@@ -298,6 +300,7 @@ static int checkPolicy(sd_event_source *source, void *data) {
                 crr_layout_state = pm::g_prv_layers_car_stop[layer_name].layout_state;
 #endif
             }
+#if 0
             else if ("none" == crr_layout_name) {
                 // If current layout is "none",
                 // current areas is set with "none"
@@ -309,21 +312,25 @@ static int checkPolicy(sd_event_source *source, void *data) {
                 crr_layout_state.name = "none";
                 crr_layout_state.area_list.push_back(crr_area_state);
             }
+#endif
             else {
+                // Copy previous layout state for current
+                crr_layout_state = prv_layout_state;
+
                 if (prv_layout_name == crr_layout_name) {
-                    // If previous layout is same with current,
-                    // previous areas are copied to current
                     HMI_DEBUG("wm:lm", "Previous layout is same with current");
-                    crr_layout_state = prv_layout_state;
                 }
                 else {
                     // If previous layout is NOT same with current,
                     // current areas is set with default value
                     HMI_DEBUG("wm:lm", "Previous layout is NOT same with current");
-                    crr_layout_state = pm::g_default_layouts[crr_layout_name];
+                    crr_layout_state.name         = pm::g_default_layouts[crr_layout_name].name;
+                    crr_layout_state.category_num = pm::g_default_layouts[crr_layout_name].category_num;
+                    crr_layout_state.area_list    = pm::g_default_layouts[crr_layout_name].area_list;
                 }
 
                 // Update role in new area
+#if 0
 #if 1
                 if (crr_state.restriction_mode.is_changed) {
                     // Updating role is not necessary
@@ -339,18 +346,21 @@ static int checkPolicy(sd_event_source *source, void *data) {
 #endif
                 }
                 else {
-
+#endif
                     std::map<std::string, std::vector<pm::AreaState>> ctg_list;
                     for (int ctg_no=stm::gStmCategoryNoMin;
                          ctg_no<=stm::gStmCategoryNoMax; ctg_no++) {
                         const char* ctg = stm::gStmCategoryName[ctg_no];
                         HMI_DEBUG("wm:pm", "ctg:%s", ctg);
 
-                        // Create area list of previous/current app
+                        // Create area list of previous app
                         std::vector<pm::AreaState> area_list;
                         for (pm::AreaState area_state : prv_layout_state.area_list) {
                             if (std::string(ctg) == area_state.category) {
-                                HMI_DEBUG("wm:pm", "push area_state to prv list");
+                                // If there is the category which is same with new category in previous layout,
+                                // push it to area list
+                                HMI_DEBUG("wm:pm", "push area_state to prv list category:%s role:%s",
+                                    area_state.category.c_str(), area_state.role.c_str());
                                 area_list.push_back(area_state);
                             }
                         }
@@ -359,18 +369,30 @@ static int checkPolicy(sd_event_source *source, void *data) {
                         int crr_ctg_num = crr_layout_state.category_num[ctg];
                         HMI_DEBUG("wm:pm", "crr_ctg_num:%d prv_ctg_num:%d", crr_ctg_num, prv_ctg_num);
 
+                        // Remove state of area which is used no longer from area list
                         if (crr_ctg_num < prv_ctg_num) {
                             HMI_DEBUG("wm:pm", "crr_ctg_num < prv_ctg_num");
                             if (0 == strcmp(ctg, category.c_str())) {
-                                HMI_DEBUG("wm:pm", "category of requested role is same");
                                 for (auto i = area_list.begin(); i != area_list.end(); ++i) {
                                     if ((role == i->role) && (area == i->name)) {
-                                        HMI_DEBUG("wm:pm", "remove requested role data");
+                                        HMI_DEBUG("wm:pm", "store removed role:%s", i->role.c_str());
+                                        crr_layout_state.last_invisibled_role[category] = i->role;
+
+                                        HMI_DEBUG("wm:pm", "remove area state which have same role with requested role");
                                         area_list.erase(i);
+
+                                        prv_ctg_num--;
+
+                                        if (crr_ctg_num == prv_ctg_num) {
+                                            break;
+                                        }
                                     }
                                 }
                             }
                             else {
+                                HMI_DEBUG("wm:pm", "store removed role:%s", area_list.begin()->role.c_str());
+                                crr_layout_state.last_invisibled_role[ctg] = area_list.begin()->role;
+
                                 HMI_DEBUG("wm:pm", "remove the oldest area data");
                                 area_list.erase(area_list.begin());
                             }
@@ -380,6 +402,9 @@ static int checkPolicy(sd_event_source *source, void *data) {
                             if (0 == strcmp(ctg, category.c_str())) {
                                 HMI_DEBUG("wm:pm", "category of requested role is same");
                                 if ((crr_ctg_num == prv_ctg_num) && (0 != prv_ctg_num)) {
+                                    HMI_DEBUG("wm:pm", "store removed role:%s", area_list.begin()->role.c_str());
+                                    crr_layout_state.last_invisibled_role[ctg] = area_list.begin()->role;
+
                                     HMI_DEBUG("wm:pm", "remove the oldest area data for adding requested role");
                                     area_list.erase(area_list.begin());
                                 }
@@ -387,7 +412,7 @@ static int checkPolicy(sd_event_source *source, void *data) {
                         }
 
                         // Check
-                        for (pm::AreaState area_state : prv_layout_state.area_list) {
+                        for (pm::AreaState area_state : area_list) {
                             HMI_DEBUG("wm:pm", "area_list name:%s category:%s role:%s",
                                       area_state.name.c_str(),
                                       area_state.category.c_str(),
@@ -398,19 +423,26 @@ static int checkPolicy(sd_event_source *source, void *data) {
                     }
 
                     if (event == "activate") {
+                        HMI_DEBUG("wm:pm", "event is activate");
                         // First, update area for requested role
+                        bool request_for_this_layer = false;
                         bool updated = false;
                         for (pm::AreaState &as : crr_layout_state.area_list) {
-                            if ((as.category == category) && (as.name == area)) {
-                                as.role = role;
-                                updated = true;
-                                break;
+                            if (as.category == category) {
+                                request_for_this_layer = true;
+                                if (as.name == area) {
+                                    HMI_DEBUG("wm:pm", "update area for requested category:%s role:%s area:%s", category.c_str(), role.c_str(), area.c_str());
+                                    as.role = role;
+                                    updated = true;
+                                    break;
+                                }
                             }
                         }
 
                         // If NOT updated: there is not requested area in new layout, 
                         // requested role is used later.
-                        if (!updated) {
+                        if (request_for_this_layer && (!updated)) {
+                            HMI_DEBUG("wm:pm", "requested role is used later category:%s role:%s area:%s", category.c_str(), role.c_str(), area.c_str());
                             pm::AreaState area_state;
                             area_state.name = area;
                             area_state.category = category;
@@ -421,14 +453,26 @@ static int checkPolicy(sd_event_source *source, void *data) {
 
                     // Update areas
                     for (pm::AreaState &as : crr_layout_state.area_list) {
-
-                        // This conditional expression is useful in only activate
-                        if (as.role != role) {
-                            as.role = ctg_list[as.category].begin()->role;
-                            ctg_list[as.category].erase(ctg_list[as.category].begin());
+                        HMI_DEBUG("wm:pm", "current area info area:%s category:%s role:%s",
+                                  as.name.c_str(), as.category.c_str(), as.role.c_str());
+                        if ((as.role != role) || ("" == as.role)) {
+                            HMI_DEBUG("wm:pm", "update areas for category:%s", as.category.c_str());
+                            if (0 != ctg_list[as.category].size()) {
+                                HMI_DEBUG("wm:pm", "update role to %s by using area list this category",
+                                          ctg_list[as.category].begin()->role);
+                                as.role = ctg_list[as.category].begin()->role;
+                                ctg_list[as.category].erase(ctg_list[as.category].begin());
+                            }
+                            else {
+                                HMI_DEBUG("wm:pm", "there is no area list for this category, so update area by last invisibled role:", crr_layout_state.last_invisibled_role[as.category]);
+                                as.role = crr_layout_state.last_invisibled_role[as.category];
+                                crr_layout_state.last_invisibled_role.erase(as.category);
+                            }
                         }
                     }
+#if 0
                 }
+#endif
             }
             // Update current layout of this layer
             pm::g_crr_layers[layer_name].layout_state = crr_layout_state;
@@ -522,7 +566,11 @@ static int checkPolicy(sd_event_source *source, void *data) {
     json_tmp = json_object_new_object();
     addStateToJson("homescreen",
                    crr_state.layer[stm::gStmLayerNoHomescreen].is_changed,
+#if 1
+                   pm::g_crr_layers["homescreen"].layout_state.name.c_str(),
+#else
                    stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoHomescreen].state],
+#endif
                    &json_tmp);
     json_object_array_add(json_layer, json_tmp);
 
@@ -535,7 +583,11 @@ static int checkPolicy(sd_event_source *source, void *data) {
     json_tmp = json_object_new_object();
     addStateToJson("apps",
                    crr_state.layer[stm::gStmLayerNoApps].is_changed,
+#if 1
+                   pm::g_crr_layers["apps"].layout_state.name.c_str(),
+#else
                    stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoApps].state],
+#endif
                    &json_tmp);
     json_object_array_add(json_layer, json_tmp);
 
@@ -548,7 +600,11 @@ static int checkPolicy(sd_event_source *source, void *data) {
     json_tmp = json_object_new_object();
     addStateToJson("restriction",
                    crr_state.layer[stm::gStmLayerNoRestriction].is_changed,
+#if 1
+                   pm::g_crr_layers["restriction"].layout_state.name.c_str(),
+#else
                    stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoRestriction].state],
+#endif
                    &json_tmp);
     json_object_array_add(json_layer, json_tmp);
 
@@ -561,7 +617,11 @@ static int checkPolicy(sd_event_source *source, void *data) {
     json_tmp = json_object_new_object();
     addStateToJson("on_screen",
                    crr_state.layer[stm::gStmLayerNoOnScreen].is_changed,
+#if 1
+                   pm::g_crr_layers["on_screen"].layout_state.name.c_str(),
+#else
                    stm::gStmLayoutNo2Name[crr_state.layer[stm::gStmLayerNoOnScreen].state],
+#endif
                    &json_tmp);
     json_object_array_add(json_layer, json_tmp);
 
@@ -928,6 +988,12 @@ int PolicyManager::loadLayoutDb() {
         pm::g_default_layouts[layout] = layout_state;
     }
 
+    // initialize for none layout
+    pm::LayoutState none_layout_state;
+    memset(&none_layout_state, 0, sizeof(none_layout_state));
+    none_layout_state.name                 = "none";
+    pm::g_default_layouts["none"] = none_layout_state;
+
     // Check
     for(auto itr_layout = pm::g_default_layouts.begin();
       itr_layout != pm::g_default_layouts.end(); ++itr_layout) {