improvement
[apps/agl-service-homescreen.git] / src / hs-apprecover.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 #include "hs-apprecover.h"
18 #include "hs-appinfo.h"
19 #include "hs-proxy.h"
20 #include "hs-clientmanager.h"
21
22
23 HS_AppRecover* HS_AppRecover::me = nullptr;
24
25 /**
26  * screen_update event handler
27  *
28  * #### Parameters
29  * - api : the api
30  * - event : received event name
31  * - object : received json object
32  *
33  * #### Return
34  * 0 : event can transfer to others
35  * 1 : event not transfer to others
36  */
37 int on_screen_update_event(afb_api_t api, const char *event, struct json_object *object)
38 {
39     HS_AppRecover::instance()->screenUpdated(object);
40     return 0;
41 }
42
43 /**
44  * get instance
45  *
46  * #### Parameters
47  *  - Nothing
48  *
49  * #### Return
50  * HS_AppRecover instance pointer
51  *
52  */
53 HS_AppRecover* HS_AppRecover::instance(void)
54 {
55     if(me == nullptr)
56         me = new HS_AppRecover();
57
58     return me;
59 }
60
61 /**
62  * HS_AppRecover initialize function
63  *
64  * #### Parameters
65  *  - api : the api serving the request
66  *
67  * #### Return
68  * 0 : init success
69  * 1 : init fail
70  *
71  */
72 int HS_AppRecover::init(afb_api_t api)
73 {
74     setEventHook("windowmanager/screenUpdated", on_screen_update_event);
75     HS_WmProxy wm_proxy;
76     wm_proxy.subscribe(api, HS_WmProxy::Event_ScreenUpdated);
77     return 0;
78 }
79
80 /**
81  * starting recover applications
82  *
83  * #### Parameters
84  *  - api : the api
85  *  - map : recover app map
86  *
87  * #### Return
88  * None
89  *
90  */
91 void HS_AppRecover::startRecovery(afb_api_t api, recover_map &map)
92 {
93     for(auto &key : HS_Config::keys_recover_type) {
94         for(auto &m : map[key]){
95             struct app_recover_info recover_info = {
96                 .recover_type = key,
97                 .visibility = m.visibility,
98                 .after = m.after
99             };
100             m_recover_apps_list[m.appid] = std::move(recover_info);
101
102             // recover application
103             auto it = m_recovering_set.find(m.appid);
104             if(it == m_recovering_set.end()) {
105                 m_recovering_set.insert(m.appid);
106                 std::string &after = m_recover_apps_list[m.appid].after;
107                 if(!after.empty()) {
108                     auto w = m_wait_recover_set.find(m_recover_apps_list[m.appid].after);
109                     if(w != m_wait_recover_set.end()) {
110                         m_wait_recover_set[after].insert(m.appid);
111                     }
112                     else {
113                         std::set<std::string> new_set;
114                         new_set.insert(m.appid);
115                         m_wait_recover_set[after] = std::move(new_set);
116                     }
117                     continue;   // don't immediately start application, wait until after applicaiton started.
118                 }
119                 startApplication(api, m.appid);
120             }
121         }
122     }
123 }
124
125 /**
126  * register started applications
127  *
128  * #### Parameters
129  *  - appid : application id liked "dashboard"
130  *
131  * #### Return
132  * false : not recover app
133  * true : recover app
134  * 
135  */
136 bool HS_AppRecover::registerRecoveredApp(afb_api_t api, const std::string &appid)
137 {
138     bool ret = false;
139     if(m_recovering_set.empty()) {
140         return ret;
141     }
142
143     AFB_INFO("recover appid=[%s].", appid.c_str());
144     auto it = m_recovering_set.find(appid);
145     if(it != m_recovering_set.end()) {
146         m_recovering_set.erase(appid);
147         auto ip = m_recover_apps_list.find(appid);
148         if(ip != m_recover_apps_list.end()
149         && ip->second.visibility) {
150             HS_ClientManager::instance()->pushEvent("showWindow", nullptr, appid);
151         }
152         ret = true;
153     }
154
155     // check wait recover application
156     auto w = m_wait_recover_set.find(appid);
157     if(w != m_wait_recover_set.end()) {
158         for(auto &ref : m_wait_recover_set[appid]) {
159             startApplication(api, ref);
160         }
161         m_wait_recover_set.erase(appid);
162     }
163     return ret;
164 }
165
166 /**
167  * screenUpdated event handler
168  *
169  * #### Parameters
170  *  - obj : screenUpdate object
171  *
172  * #### Return
173  * None
174  * 
175  */
176 void HS_AppRecover::screenUpdated(struct json_object *obj)
177 {
178     if(m_lastmode_list.empty()) {
179         AFB_NOTICE("init lastmode is null, so don't record lastmode.");
180         return;
181     }
182
183     std::set<std::string> s_mode;
184     struct json_object *ids_obj;
185     if(json_object_object_get_ex(obj, key_ids.c_str(), &ids_obj)) {
186         if(json_object_get_type(ids_obj) == json_type_array) {
187             int array_len = json_object_array_length(ids_obj);
188             for (int i = 0; i < array_len; ++i) {
189                 struct json_object *j_id = json_object_array_get_idx(ids_obj, i);
190                 std::string appid = json_object_get_string(j_id);
191                 if(!isHomescreenApp(appid)) {
192                     s_mode.insert(appid);
193                 }
194             }
195             if(!s_mode.empty()) {
196                 updateLastmode(s_mode);
197             }
198         }
199         else {
200             AFB_WARNING("screenUpdated list isn't array.");
201         }
202     }
203 }
204
205 /**
206  * start application
207  *
208  * #### Parameters
209  *  - api : the api
210  *  - appid : application id liked "dashboard"
211  *
212  * #### Return
213  * None
214  *
215  */
216 void HS_AppRecover::startApplication(afb_api_t api, const std::string &appid)
217 {
218     HS_AfmMainProxy afm_proxy;
219     afm_proxy.start(api,  HS_AppInfo::instance()->getAppProperty(appid, _keyId));
220 }
221
222 /**
223  * update lastmode file
224  *
225  * #### Parameters
226  *  - set : new lastmode set
227  *
228  * #### Return
229  * None
230  * 
231  */
232 void HS_AppRecover::updateLastmode(std::set<std::string> &set)
233 {
234     if(set.size() == m_lastmode_list.size()) {
235         bool is_same = true;
236         for(auto &m : set) {
237             auto it = m_lastmode_list.find(m);
238             if(it == m_lastmode_list.end()) {
239                 is_same = false;
240                 break;
241             }
242         }
243         if(is_same) {   // lastmode aren't changed
244             return;
245         }
246     }
247
248     m_lastmode_list.swap(set);
249     struct json_object *arr_obj = json_object_new_array();
250     for(auto &it : m_lastmode_list) {
251         struct json_object *j_obj = json_object_new_object();
252         json_object_object_add(j_obj, HS_Config::key_appid.c_str(), json_object_new_string(it.c_str()));
253         json_object_object_add(j_obj, HS_Config::key_visibility.c_str(), json_object_new_string("visible"));
254         json_object_array_add(arr_obj, j_obj);
255     }
256
257     auto path = HS_Config::root_dir + "/etc/" + HS_Config::lastmode_json;
258     if(writeJsonFile(path.c_str(), arr_obj) < 0) {
259         AFB_ERROR("write lastmode error.");
260     }
261 }
262
263 /**
264  * judge whether app is Homescreen app
265  *
266  * #### Parameters
267  *  - appid : application id
268  *
269  * #### Return
270  * true : homescreen app
271  * false : not homescreen app
272  *
273  */
274 bool HS_AppRecover::isHomescreenApp(const std::string &appid) const
275 {
276     auto it = m_recover_apps_list.find(appid);
277     if(it != m_recover_apps_list.end()
278     && it->second.recover_type == HS_Config::keys_recover_type[0]) {
279             return true;
280     }
281     return false;
282 }