don't use area info
[apps/agl-service-homescreen.git] / src / hs-config.cpp
1 /*
2  * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include "hs-config.h"
19
20 const std::array<std::string, 3> HS_Config::keys_recover_type = {   // based on hs-conf.json
21     "hs-apps",
22     "default-lastmode",
23     "normal-apps"
24 };
25
26 /**
27  * read configuration file to memory
28  *
29  * #### Parameters
30  *  - Nothing
31  *
32  * #### Return
33  * None
34  *
35  */
36 int HS_Config::readConfig(void)
37 {
38     auto rootdir = std::string(getenv("AFM_APP_INSTALL_DIR"));
39     auto path = rootdir + "/etc/" + hs_conf_json;
40     if(readJsonFile(path.c_str(), &m_hs_conf) < 0) {
41         AFB_ERROR("read %s failed.", hs_conf_json.c_str());
42         return -1;
43     }
44
45     path = rootdir + "/etc/" + lastmode_json;
46     if(readJsonFile(path.c_str(), &m_lastmode) < 0) {
47         AFB_ERROR("read %s failed.", lastmode_json.c_str());
48         m_lastmode = nullptr;
49     }
50
51     return parseConfig();
52 }
53
54 /**
55  * parse configuration file contents
56  *
57  * #### Parameters
58  *  - Nothing
59  *
60  * #### Return
61  * 0 : success
62  * 1 : fail
63  *
64  */
65 int HS_Config::parseConfig(void)
66 {
67     struct json_object *handshake_obj, *times_obj, *sleep_obj;
68     if(json_object_object_get_ex(m_hs_conf, key_handshake.c_str(), &handshake_obj)
69     && json_object_object_get_ex(handshake_obj, key_times.c_str(), &times_obj)
70     && json_object_object_get_ex(handshake_obj, key_sleep.c_str(), &sleep_obj)) {
71         m_handshake_info.times = json_object_get_int(times_obj);
72         m_handshake_info.sleep = json_object_get_int(sleep_obj);
73     }
74     else {
75         AFB_WARNING("get handshake info error, use default value.");
76     }
77
78     struct json_object *recover_obj;
79     if(json_object_object_get_ex(m_hs_conf, key_recover.c_str(), &recover_obj)) {
80         for(auto &m : keys_recover_type) {
81             struct json_object *obj;
82             if(json_object_object_get_ex(recover_obj, m.c_str(), &obj)) {
83                 if(json_object_get_type(obj) != json_type_array ) {
84                     continue;
85                 }
86                 m_recover_map[m] = std::move(getRecoverAppInfo(obj));
87             }
88         }
89
90         if(m_recover_map.empty()) {
91             AFB_ERROR("get homescreen recover list failed.");
92             return -1;
93         }
94     }
95     else {
96         AFB_ERROR("get homescreen recover object failed.");
97         return -1;
98     }
99
100     if(json_object_get_type(m_lastmode) == json_type_array ) {
101         struct std::vector<recover_app_info> v_lastmode = std::move(getRecoverAppInfo(m_lastmode));
102         if(!v_lastmode.empty()) {   // got saving lastmode isn't null, instead of default lastmode
103             m_recover_map[keys_recover_type[1]] = std::move(v_lastmode);
104         }
105     }
106
107     return 0;
108 }
109
110 /**
111  * get recover application information
112  * appid, visibility
113  *
114  * #### Parameters
115  *  - obj : application information
116  *
117  * #### Return
118  * recover_app_info vector
119  *
120  */
121 std::vector<struct recover_app_info> HS_Config::getRecoverAppInfo(struct json_object *obj)
122 {
123     int array_len = json_object_array_length(obj);
124     std::vector<struct recover_app_info> v_app_info;
125     for (int i = 0; i < array_len; ++i) {
126         struct json_object *info_obj = json_object_array_get_idx(obj, i);
127         struct recover_app_info info;
128         struct json_object *value_obj;
129         if(json_object_object_get_ex(info_obj, key_appid.c_str(), &value_obj)) {
130             info.appid = json_object_get_string(value_obj);
131         }
132         else {
133             AFB_ERROR("recover infomation doesn't include appid.");
134             v_app_info.clear();
135             return v_app_info;
136         }
137         if(json_object_object_get_ex(info_obj, key_visibility.c_str(), &value_obj)) {
138             std::string visibility = json_object_get_string(value_obj);
139             info.visibility = (visibility == "visible") ? true:false;
140         }
141         else {
142             info.visibility = false;
143         }
144         v_app_info.push_back(info);
145     }
146
147     return v_app_info;
148 }