Skip to content

Commit

Permalink
optimize app::switch_app ignore same app switch
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Aug 7, 2024
1 parent ae9435b commit 517818f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions components/basic/include/maix_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ namespace maix::app
* @param app_id APP ID which will be started. app_id and idx must have one is valid.
* @param idx APP index. app_id and idx must have one is valid.
* @param start_param string type, will send to app, app can get this param by `app.get_start_param()`
* @attention If app id or idx the same as current app, do nothing.
* @maixpy maix.app.switch_app
*/
void switch_app(const string &app_id, int idx = -1, const std::string &start_param = "");
Expand Down
29 changes: 17 additions & 12 deletions components/basic/src/maix_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,26 +580,14 @@ namespace maix::app
log::error("switch app failed, app_id and idx must have one is valid\n");
return;
}
// inform this app to exit, code should check this flag by app::need_exit()
set_exit_flag(true);
vector<APP_Info> &apps_info = get_apps_info();

// write app_id to file, launcher will read this file and start the app
string path = get_app_start_info_path();
FILE *fp = fopen(path.c_str(), "w");
if (fp == NULL)
{
log::error("open app start info file failed: %s", path.c_str());
return;
}
std::string final_app_id = app_id;
std::string final_app_path = "";
if (idx >= 0)
{
if ((size_t)idx >= apps_info.size())
{
log::error("idx error, should < %lld, but %d", apps_info.size(), idx);
fclose(fp);
throw err::Exception(err::ERR_ARGS, "idx error");
}
final_app_id = apps_info[idx].id;
Expand All @@ -617,6 +605,23 @@ namespace maix::app
}
}
}
// if switch to current app, just return.
if(final_app_id == app_id())
{
return;
}

// inform this app to exit, code should check this flag by app::need_exit()
set_exit_flag(true);

// write app_id to file, launcher will read this file and start the app
string path = get_app_start_info_path();
FILE *fp = fopen(path.c_str(), "w");
if (fp == NULL)
{
log::error("open app start info file failed: %s", path.c_str());
return;
}
fprintf(fp, "%s\n%s\n%s\n", final_app_path.c_str(), final_app_id.c_str(), start_param.c_str());
fclose(fp);
// when this app exit, the launcher will check this file and start the app
Expand Down

0 comments on commit 517818f

Please sign in to comment.