cedf7510e5b497b80b77fe14e2ab7d09226dce7f
[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     HS_WmProxy wm_proxy;
75     wm_proxy.subscribe(api, HS_WmProxy::Event_ScreenUpdated);
76     setEventHook("windowmanager/screenUpdated", on_screen_update_event);
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     HS_AfmMainProxy afm_proxy;
94     for(auto &key : HS_Config::keys_recover_type) {
95         for(auto &m : map[key]){
96             struct app_recover_info recover_info;
97             recover_info.recover_type = key;
98             recover_info.visibility = m.visibility;
99             m_recover_apps_list[m.appid] = std::move(recover_info);
100             if(key == HS_Config::keys_recover_type[1]) {
101                 m_lastmode_list.insert(m.appid);
102             }
103
104             // recover application
105             m_recovering_set.insert(m.appid);
106             afm_proxy.start(api,  HS_AppInfo::instance()->getAppProperty(m.appid, _keyId));
107         }
108     }
109 }
110
111 /**
112  * register started applications
113  *
114  * #### Parameters
115  *  - appid : application id liked "dashboard"
116  *
117  * #### Return
118  * None
119  * 
120  */
121 void HS_AppRecover::registerRecoveredApp(const std::string &appid)
122 {
123     if(m_recovering_set.empty()) {
124         return;
125     }
126
127     auto it = m_recovering_set.find(appid);
128     if(it != m_recovering_set.end()) {
129         m_recovering_set.erase(appid);
130         auto ip = m_recover_apps_list.find(appid);
131         if(ip != m_recover_apps_list.end()
132         && ip->second.visibility) {
133             HS_ClientManager::instance()->pushEvent("showWindow", nullptr, appid);
134         }
135     }
136 }
137
138 /**
139  * screenUpdated event handler
140  *
141  * #### Parameters
142  *  - obj : screenUpdate object
143  *
144  * #### Return
145  * None
146  * 
147  */
148 void HS_AppRecover::screenUpdated(struct json_object *obj)
149 {
150     std::set<std::string> s_mode;
151     struct json_object *ids_obj;
152     if(json_object_object_get_ex(obj, key_ids.c_str(), &ids_obj)) {
153         if(json_object_get_type(ids_obj) == json_type_array) {
154             int array_len = json_object_array_length(ids_obj);
155             for (int i = 0; i < array_len; ++i) {
156                 struct json_object *j_id = json_object_array_get_idx(ids_obj, i);
157                 std::string appid = json_object_get_string(j_id);
158                 if(!isHomescreenApp(appid)) {
159                     s_mode.insert(appid);
160                 }
161             }
162             if(!s_mode.empty()) {
163                 updateLastmode(s_mode);
164             }
165         }
166         else {
167             AFB_WARNING("screenUpdated list isn't array.");
168         }
169     }
170 }
171
172 /**
173  * update lastmode file
174  *
175  * #### Parameters
176  *  - set : new lastmode set
177  *
178  * #### Return
179  * None
180  * 
181  */
182 void HS_AppRecover::updateLastmode(std::set<std::string> &set)
183 {
184     if(set.size() == m_lastmode_list.size()) {
185         bool is_same = true;
186         for(auto &m : set) {
187             auto it = m_lastmode_list.find(m);
188             if(it == m_lastmode_list.end()) {
189                 is_same = false;
190                 break;
191             }
192         }
193         if(is_same) {   // lastmode aren't changed
194             return;
195         }
196     }
197
198     m_lastmode_list.swap(set);
199     struct json_object *arr_obj = json_object_new_array();
200     for(auto &it : m_lastmode_list) {
201         struct json_object *j_obj = json_object_new_object();
202         json_object_object_add(j_obj, HS_Config::key_appid.c_str(), json_object_new_string(it.c_str()));
203         json_object_object_add(j_obj, HS_Config::key_visibility.c_str(), json_object_new_string("visible"));
204         json_object_array_add(arr_obj, j_obj);
205     }
206
207     auto path = HS_Config::root_dir + "/etc/" + HS_Config::lastmode_json;
208     if(writeJsonFile(path.c_str(), arr_obj) < 0) {
209         AFB_ERROR("write lastmode error.");
210     }
211 }
212
213 /**
214  * judge whether app is Homescreen app
215  *
216  * #### Parameters
217  *  - appid : application id
218  *
219  * #### Return
220  * true : homescreen app
221  * false : not homescreen app
222  *
223  */
224 bool HS_AppRecover::isHomescreenApp(const std::string &appid) const
225 {
226     auto it = m_recover_apps_list.find(appid);
227     if(it != m_recover_apps_list.end()
228     && it->second.recover_type == HS_Config::keys_recover_type[0]) {
229             return true;
230     }
231     return false;
232 }