Synchronize application icons with the host

This commit is contained in:
Simon Fels 2017-01-06 18:33:27 +01:00
commit 5c7b0adf8c
11 changed files with 103 additions and 41 deletions

View file

@ -17,6 +17,7 @@
#include "anbox/application/launcher_storage.h"
#include "anbox/utils.h"
#include "anbox/logger.h"
#include <algorithm>
#include <fstream>
@ -26,19 +27,25 @@ namespace fs = boost::filesystem;
namespace anbox {
namespace application {
LauncherStorage::LauncherStorage(const fs::path &path) : path_(path) {}
LauncherStorage::LauncherStorage(const fs::path &path,
const boost::filesystem::path &icon_path) : path_(path), icon_path_(icon_path) {
}
LauncherStorage::~LauncherStorage() {}
void LauncherStorage::add(const Item &item) {
if (!fs::exists(path_)) fs::create_directories(path_);
if (!fs::exists(icon_path_)) fs::create_directories(icon_path_);
auto package_name = item.package;
std::replace(package_name.begin(), package_name.end(), '.', '-');
const auto item_path =
path_ / utils::string_format("anbox-%s.desktop", package_name);
std::string exec = "anbox launch ";
const auto item_icon_path =
icon_path_ / utils::string_format("anbox-%s.png", package_name);
std::string exec = utils::string_format("%s launch ", utils::process_get_exe_path(getpid()));
if (!item.launch_intent.action.empty())
exec += utils::string_format("--action=%s ", item.launch_intent.action);
@ -62,7 +69,11 @@ void LauncherStorage::add(const Item &item) {
<< "Exec=" << exec << std::endl
<< "Terminal=false" << std::endl
<< "Type=Application" << std::endl
<< "Encoding=UTF-8" << std::endl;
<< "Icon=" << item_icon_path.string() << std::endl;
f.close();
f = std::ofstream(item_icon_path.string());
f.write(item.icon.data(), item.icon.size());
f.close();
}
} // namespace application

View file

@ -29,19 +29,22 @@ namespace anbox {
namespace application {
class LauncherStorage {
public:
LauncherStorage(const boost::filesystem::path &path);
LauncherStorage(const boost::filesystem::path &path,
const boost::filesystem::path &icon_path);
~LauncherStorage();
struct Item {
std::string name;
std::string package;
android::Intent launch_intent;
std::vector<char> icon;
};
void add(const Item &item);
private:
boost::filesystem::path path_;
boost::filesystem::path icon_path_;
};
} // namespace application
} // namespace anbox

View file

@ -89,6 +89,11 @@ void PlatformApiSkeleton::handle_application_list_update_event(
for (int m = 0; m < li.categories_size(); m++)
item.launch_intent.categories.push_back(li.categories(m));
item.icon = std::vector<char>(app.icon().begin(), app.icon().end());
if (item.package.empty())
continue;
// If the item is already stored it will be updated
launcher_storage_->add(item);
}

View file

@ -114,7 +114,8 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory)
auto window_manager = std::make_shared<wm::Manager>(policy);
auto launcher_storage = std::make_shared<application::LauncherStorage>(
xdg::data().home() / "applications");
xdg::data().home() / "applications" / "anbox",
xdg::data().home() / "anbox" / "icons");
auto gl_server =
std::make_shared<graphics::GLRendererServer>(window_manager);

View file

@ -74,6 +74,7 @@ message ApplicationListUpdateEvent {
required string name = 1;
required string package = 2;
optional Intent launch_intent = 3;
optional bytes icon = 4;
}
repeated Application applications = 1;
}

View file

@ -158,5 +158,10 @@ std::string prefix_dir_from_env(const std::string &path,
return result;
}
std::string process_get_exe_path(const pid_t &pid) {
auto exe_path = string_format("/proc/%d/exe", pid);
return boost::filesystem::read_symlink(exe_path).string();
}
} // namespace utils
} // namespace anbox

View file

@ -48,6 +48,8 @@ void ensure_paths(const std::vector<std::string> &paths);
std::string prefix_dir_from_env(const std::string &path,
const std::string &env_var);
std::string process_get_exe_path(const pid_t &pid);
template <typename... Types>
static std::string string_format(const std::string &fmt_str, Types &&... args);
} // namespace utils