2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 #define LOG_TAG "Anboxd"
20 #include "android/service/android_api_skeleton.h"
22 #include "anbox_rpc.pb.h"
23 #include "anbox_bridge.pb.h"
25 #include <core/posix/exec.h>
26 #include <core/posix/child_process.h>
28 #include <binder/IServiceManager.h>
34 std::map<std::string,std::string> common_env = {
35 {"ANDROID_DATA", "/data"},
36 {"ANDROID_ROOT", "/system"},
41 AndroidApiSkeleton::AndroidApiSkeleton() {
44 AndroidApiSkeleton::~AndroidApiSkeleton() {
47 void AndroidApiSkeleton::wait_for_process(core::posix::ChildProcess &process,
48 anbox::protobuf::rpc::Void *response) {
49 const auto result = process.wait_for(core::posix::wait::Flags::untraced);
50 if (result.status != core::posix::wait::Result::Status::exited ||
51 result.detail.if_exited.status != core::posix::exit::Status::success) {
52 response->set_error("Failed to execute process");
53 // FIXME once we add proper error codes/domains we need to add structured error
54 // info to the response here.
58 void AndroidApiSkeleton::connect_services() {
59 if (!activity_manager_.get()) {
60 auto am = android::defaultServiceManager()->getService(android::String16("activity"));
62 activity_manager_ = new android::BpActivityManager(am);
66 void AndroidApiSkeleton::launch_application(anbox::protobuf::bridge::LaunchApplication const *request,
67 anbox::protobuf::rpc::Void *response,
68 google::protobuf::Closure *done) {
71 auto intent = request->intent();
73 std::vector<std::string> argv = {
78 if (request->has_stack()) {
79 argv.push_back("--stack");
80 argv.push_back(std::to_string(request->stack()));
83 if (request->has_launch_bounds()) {
84 argv.push_back("--launch-bounds");
85 std::stringstream launch_bounds;
86 launch_bounds << request->launch_bounds().left() << " "
87 << request->launch_bounds().top() << " "
88 << request->launch_bounds().right() << " "
89 << request->launch_bounds().bottom();
90 argv.push_back(launch_bounds.str());
93 if (intent.has_action()) {
95 argv.push_back(intent.action());
98 if (intent.has_uri()) {
100 argv.push_back(intent.uri());
103 if (intent.has_type()) {
104 argv.push_back("-t");
105 argv.push_back(intent.type());
108 std::string component;
109 if (intent.has_package())
110 component += intent.package();
111 if (!component.empty() && intent.has_component()) {
113 component += intent.component();
116 if (!component.empty())
117 argv.push_back(component);
119 ALOGI("Launch am with the following arguments: ");
121 for (const auto &a : argv) {
125 ALOGI("%s", test.c_str());
127 auto process = core::posix::exec("/system/bin/sh", argv, common_env, core::posix::StandardStream::empty);
128 wait_for_process(process, response);
133 void AndroidApiSkeleton::set_focused_task(anbox::protobuf::bridge::SetFocusedTask const *request,
134 anbox::protobuf::rpc::Void *response,
135 google::protobuf::Closure *done) {
138 if (activity_manager_.get())
139 activity_manager_->setFocusedTask(request->id());
141 response->set_error("ActivityManager is not available");
146 void AndroidApiSkeleton::remove_task(anbox::protobuf::bridge::RemoveTask const *request,
147 anbox::protobuf::rpc::Void *response,
148 google::protobuf::Closure *done) {
151 if (activity_manager_.get())
152 activity_manager_->removeTask(request->id());
154 response->set_error("ActivityManager is not available");
160 void AndroidApiSkeleton::resize_task(anbox::protobuf::bridge::ResizeTask const *request,
161 anbox::protobuf::rpc::Void *response,
162 google::protobuf::Closure *done) {
165 if (activity_manager_.get()) {
166 auto r = request->rect();
167 activity_manager_->resizeTask(request->id(),
168 anbox::graphics::Rect{r.left(), r.top(), r.right(), r.bottom()},
169 request->resize_mode());
171 response->set_error("ActivityManager is not available");