File: android_api_skeleton.cpp

package info (click to toggle)
anbox 0.0~git20210106-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 6,916 kB
  • sloc: cpp: 50,603; ansic: 5,056; sh: 1,150; xml: 850; java: 780; python: 460; makefile: 35; lisp: 7
file content (176 lines) | stat: -rw-r--r-- 5,497 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#define LOG_TAG "Anboxd"

#include "android/service/android_api_skeleton.h"

#include "anbox_rpc.pb.h"
#include "anbox_bridge.pb.h"

#include <core/posix/exec.h>
#include <core/posix/child_process.h>

#include <binder/IServiceManager.h>

#include <string>
#include <sstream>

namespace {
std::map<std::string,std::string> common_env = {
    {"ANDROID_DATA", "/data"},
    {"ANDROID_ROOT", "/system"},
};
}

namespace anbox {
AndroidApiSkeleton::AndroidApiSkeleton() {
}

AndroidApiSkeleton::~AndroidApiSkeleton() {
}

void AndroidApiSkeleton::wait_for_process(core::posix::ChildProcess &process,
                                   anbox::protobuf::rpc::Void *response) {
    const auto result = process.wait_for(core::posix::wait::Flags::untraced);
    if (result.status != core::posix::wait::Result::Status::exited ||
        result.detail.if_exited.status != core::posix::exit::Status::success) {
        response->set_error("Failed to execute process");
        // FIXME once we add proper error codes/domains we need to add structured error
        // info to the response here.
    }
}

void AndroidApiSkeleton::connect_services() {
    if (!activity_manager_.get()) {
        auto am = android::defaultServiceManager()->getService(android::String16("activity"));
        if (am.get())
            activity_manager_ = new android::BpActivityManager(am);
    }
}

void AndroidApiSkeleton::launch_application(anbox::protobuf::bridge::LaunchApplication const *request,
                                anbox::protobuf::rpc::Void *response,
                                google::protobuf::Closure *done) {
    (void) response;

    auto intent = request->intent();

    std::vector<std::string> argv = {
        "/system/bin/am",
        "start",
    };

    if (request->has_stack()) {
      argv.push_back("--stack");
      argv.push_back(std::to_string(request->stack()));
    }

    if (request->has_launch_bounds()) {
        argv.push_back("--launch-bounds");
        std::stringstream launch_bounds;
        launch_bounds << request->launch_bounds().left() << " "
                      << request->launch_bounds().top() << " "
                      << request->launch_bounds().right() << " "
                      << request->launch_bounds().bottom();
        argv.push_back(launch_bounds.str());
    }

    if (intent.has_action()) {
        argv.push_back("-a");
        argv.push_back(intent.action());
    }

    if (intent.has_uri()) {
        argv.push_back("-d");
        argv.push_back(intent.uri());
    }

    if (intent.has_type()) {
        argv.push_back("-t");
        argv.push_back(intent.type());
    }

    std::string component;
    if (intent.has_package())
        component += intent.package();
    if (!component.empty() && intent.has_component()) {
        component += "/";
        component += intent.component();
    }

    if (!component.empty())
        argv.push_back(component);

    ALOGI("Launch am with the following arguments: ");
    std::string test;
    for (const auto &a : argv) {
        test += a;
        test += " ";
    }
    ALOGI("%s", test.c_str());

    auto process = core::posix::exec("/system/bin/sh", argv, common_env, core::posix::StandardStream::empty);
        wait_for_process(process, response);

    done->Run();
}

void AndroidApiSkeleton::set_focused_task(anbox::protobuf::bridge::SetFocusedTask const *request,
                                          anbox::protobuf::rpc::Void *response,
                                          google::protobuf::Closure *done) {
    connect_services();

    if (activity_manager_.get())
        activity_manager_->setFocusedTask(request->id());
    else
        response->set_error("ActivityManager is not available");

    done->Run();
}

void AndroidApiSkeleton::remove_task(anbox::protobuf::bridge::RemoveTask const *request,
                                     anbox::protobuf::rpc::Void *response,
                                     google::protobuf::Closure *done) {
  connect_services();

  if (activity_manager_.get())
    activity_manager_->removeTask(request->id());
  else
    response->set_error("ActivityManager is not available");

  done->Run();

}

void AndroidApiSkeleton::resize_task(anbox::protobuf::bridge::ResizeTask const *request,
                                     anbox::protobuf::rpc::Void *response,
                                     google::protobuf::Closure *done) {
  connect_services();

  if (activity_manager_.get()) {
    auto r = request->rect();
    activity_manager_->resizeTask(request->id(),
                                  anbox::graphics::Rect{r.left(), r.top(), r.right(), r.bottom()},
                                  request->resize_mode());
  } else {
    response->set_error("ActivityManager is not available");
  }

  done->Run();
}
} // namespace anbox