TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / android / service / platform_service.cpp
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  */
17
18 #include "android/service/platform_service.h"
19 #include "anbox/rpc/channel.h"
20
21 #include "anbox_rpc.pb.h"
22 #include "anbox_bridge.pb.h"
23
24 #define LOG_TAG "Anboxd"
25 #include <cutils/log.h>
26
27 #include <utils/String8.h>
28
29 using namespace android;
30
31 namespace android {
32 PlatformService::PlatformService(const std::shared_ptr<anbox::PlatformApiStub> &platform_api_stub) :
33     platform_api_stub_(platform_api_stub) {
34 }
35
36 status_t PlatformService::boot_finished() {
37     platform_api_stub_->boot_finished();
38     return OK;
39 }
40
41 anbox::PlatformApiStub::WindowStateUpdate::Window PlatformService::unpack_window_state(const Parcel &data) {
42     bool has_surface = data.readByte() != 0;
43
44     String8 package_name(data.readString16());
45
46     auto frame_left = data.readInt32();
47     auto frame_top = data.readInt32();
48     auto frame_right = data.readInt32();
49     auto frame_bottom = data.readInt32();
50     auto task_id = data.readInt32();
51     auto stack_id = data.readInt32();
52     auto rotation_angle = data.readInt32();
53
54     return anbox::PlatformApiStub::WindowStateUpdate::Window{
55         -1, // Display id will be added by the caller
56         has_surface,
57         package_name.string(),
58         {frame_left, frame_top, frame_right, frame_bottom},
59         task_id,
60         stack_id,
61     };
62 }
63
64 status_t PlatformService::update_window_state(const Parcel &data) {
65     anbox::PlatformApiStub::WindowStateUpdate state;
66
67     const auto num_displays = data.readInt32();
68     for (auto n = 0; n < num_displays; n++) {
69         const auto display_id = data.readInt32();
70         const auto num_windows = data.readInt32();
71         for (auto m = 0; m < num_windows; m++) {
72             auto window = unpack_window_state(data);
73             window.display_id = display_id;
74             state.updated_windows.push_back(window);
75         }
76     }
77
78     const auto num_removed_windows = data.readInt32();
79     for (auto n = 0; n < num_removed_windows; n++) {
80         auto window = unpack_window_state(data);
81         state.removed_windows.push_back(window);
82     }
83
84     platform_api_stub_->update_window_state(state);
85
86     return OK;
87 }
88
89 status_t PlatformService::update_application_list(const Parcel &data) {
90     anbox::PlatformApiStub::ApplicationListUpdate update;
91     const auto num_packages = data.readInt32();
92     for (auto n = 0; n < num_packages; n++) {
93         String8 name(data.readString16());
94         String8 package_name(data.readString16());
95
96         auto p = anbox::PlatformApiStub::ApplicationListUpdate::Application{
97                 name.string(),
98                 package_name.string(),
99         };
100
101         String8 action(data.readString16());
102         String8 uri(data.readString16());
103         String8 type(data.readString16());
104         String8 component_package(data.readString16());
105         String8 component_class(data.readString16());
106
107         std::vector<std::string> categories;
108         unsigned int num_categories = data.readInt32();
109         for (int m = 0; m < num_categories; m++)
110             categories.push_back(String8(data.readString16()).string());
111
112         p.launch_intent.action = action;
113         p.launch_intent.uri = uri;
114         p.launch_intent.type = type;
115         p.launch_intent.package = component_package;
116         p.launch_intent.component = component_class;
117         p.launch_intent.categories = categories;
118
119         data.readByteVector(&p.icon);
120
121         // Send updates with icons separately to not overflow protobuf
122         if (p.icon.size() > 0) {
123           anbox::PlatformApiStub::ApplicationListUpdate with_icon_update;
124           with_icon_update.applications.push_back(p);
125           platform_api_stub_->update_application_list(with_icon_update);
126         } else {
127           update.applications.push_back(p);
128         }
129     }
130
131     const auto num_removed_packages = data.readInt32();
132     for (auto n = 0; n < num_removed_packages; n++) {
133       String8 package_name(data.readString16());
134       update.removed_applications.push_back(package_name.string());
135     }
136
137     platform_api_stub_->update_application_list(update);
138
139     return OK;
140 }
141
142 status_t PlatformService::set_clipboard_data(const Parcel &data) {
143     if (data.readInt32() == 0)
144       return OK;
145     String8 text(data.readString16());
146     const auto clip_data = anbox::PlatformApiStub::ClipboardData{text.string()};
147     platform_api_stub_->set_clipboard_data(clip_data);
148     return OK;
149 }
150
151 status_t PlatformService::get_clipboard_data(const Parcel &data, Parcel *reply) {
152     (void) data;
153     const auto clip_data = platform_api_stub_->get_clipboard_data();
154     if (clip_data.text.empty()) {
155       reply->writeInt32(0);
156       return OK;
157     }
158     reply->writeInt32(1);
159     reply->writeString16(String16(clip_data.text.c_str(), clip_data.text.length()));
160     return OK;
161 }
162 } // namespace android