2 * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "hs-apprecover.h"
18 #include "hs-appinfo.h"
20 #include "hs-clientmanager.h"
23 HS_AppRecover* HS_AppRecover::me = nullptr;
26 * screen_update event handler
30 * - event : received event name
31 * - object : received json object
34 * 0 : event can transfer to others
35 * 1 : event not transfer to others
37 int on_screen_update_event(afb_api_t api, const char *event, struct json_object *object)
39 HS_AppRecover::instance()->screenUpdated(object);
50 * HS_AppRecover instance pointer
53 HS_AppRecover* HS_AppRecover::instance(void)
56 me = new HS_AppRecover();
62 * HS_AppRecover initialize function
65 * - api : the api serving the request
72 int HS_AppRecover::init(afb_api_t api)
74 setEventHook("windowmanager/screenUpdated", on_screen_update_event);
76 wm_proxy.subscribe(api, HS_WmProxy::Event_ScreenUpdated);
81 * starting recover applications
85 * - map : recover app map
91 void HS_AppRecover::startRecovery(afb_api_t api, recover_map &map)
93 for(auto &key : HS_Config::keys_recover_type) {
94 for(auto &m : map[key]){
95 struct app_recover_info recover_info;
96 recover_info.recover_type = key;
97 recover_info.visibility = m.visibility;
98 recover_info.after = m.after;
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);
103 // recover application
104 auto it = m_recovering_set.find(m.appid);
105 if(it == m_recovering_set.end()) {
106 m_recovering_set.insert(m.appid);
107 std::string &after = m_recover_apps_list[m.appid].after;
109 auto w = m_wait_recover_set.find(m_recover_apps_list[m.appid].after);
110 if(w != m_wait_recover_set.end()) {
111 m_wait_recover_set[after].insert(m.appid);
114 std::set<std::string> new_set;
115 new_set.insert(m.appid);
116 m_wait_recover_set[after] = std::move(new_set);
118 continue; // don't immediately start application, wait until after applicaiton started.
120 startApplication(api, m.appid);
127 * register started applications
130 * - appid : application id liked "dashboard"
133 * false : not recover app
137 bool HS_AppRecover::registerRecoveredApp(afb_api_t api, const std::string &appid)
140 if(m_recovering_set.empty()) {
144 AFB_INFO("recover appid=[%s].", appid.c_str());
145 auto it = m_recovering_set.find(appid);
146 if(it != m_recovering_set.end()) {
147 m_recovering_set.erase(appid);
148 auto ip = m_recover_apps_list.find(appid);
149 if(ip != m_recover_apps_list.end()
150 && ip->second.visibility) {
151 HS_ClientManager::instance()->pushEvent("showWindow", nullptr, appid);
156 // check wait recover application
157 auto w = m_wait_recover_set.find(appid);
158 if(w != m_wait_recover_set.end()) {
159 for(auto &ref : m_wait_recover_set[appid]) {
160 startApplication(api, ref);
162 m_wait_recover_set.erase(appid);
168 * screenUpdated event handler
171 * - obj : screenUpdate object
177 void HS_AppRecover::screenUpdated(struct json_object *obj)
179 std::set<std::string> s_mode;
180 struct json_object *ids_obj;
181 if(json_object_object_get_ex(obj, key_ids.c_str(), &ids_obj)) {
182 if(json_object_get_type(ids_obj) == json_type_array) {
183 int array_len = json_object_array_length(ids_obj);
184 for (int i = 0; i < array_len; ++i) {
185 struct json_object *j_id = json_object_array_get_idx(ids_obj, i);
186 std::string appid = json_object_get_string(j_id);
187 if(!isHomescreenApp(appid)) {
188 s_mode.insert(appid);
191 if(!s_mode.empty()) {
192 updateLastmode(s_mode);
196 AFB_WARNING("screenUpdated list isn't array.");
206 * - appid : application id liked "dashboard"
212 void HS_AppRecover::startApplication(afb_api_t api, const std::string &appid)
214 HS_AfmMainProxy afm_proxy;
215 afm_proxy.start(api, HS_AppInfo::instance()->getAppProperty(appid, _keyId));
219 * update lastmode file
222 * - set : new lastmode set
228 void HS_AppRecover::updateLastmode(std::set<std::string> &set)
230 if(set.size() == m_lastmode_list.size()) {
233 auto it = m_lastmode_list.find(m);
234 if(it == m_lastmode_list.end()) {
239 if(is_same) { // lastmode aren't changed
244 m_lastmode_list.swap(set);
245 struct json_object *arr_obj = json_object_new_array();
246 for(auto &it : m_lastmode_list) {
247 struct json_object *j_obj = json_object_new_object();
248 json_object_object_add(j_obj, HS_Config::key_appid.c_str(), json_object_new_string(it.c_str()));
249 json_object_object_add(j_obj, HS_Config::key_visibility.c_str(), json_object_new_string("visible"));
250 json_object_array_add(arr_obj, j_obj);
253 auto path = HS_Config::root_dir + "/etc/" + HS_Config::lastmode_json;
254 if(writeJsonFile(path.c_str(), arr_obj) < 0) {
255 AFB_ERROR("write lastmode error.");
260 * judge whether app is Homescreen app
263 * - appid : application id
266 * true : homescreen app
267 * false : not homescreen app
270 bool HS_AppRecover::isHomescreenApp(const std::string &appid) const
272 auto it = m_recover_apps_list.find(appid);
273 if(it != m_recover_apps_list.end()
274 && it->second.recover_type == HS_Config::keys_recover_type[0]) {