TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / bridge / platform_api_skeleton.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 "anbox/bridge/platform_api_skeleton.h"
19 #include "anbox/application/database.h"
20 #include "anbox/platform/base_platform.h"
21 #include "anbox/wm/manager.h"
22 #include "anbox/wm/window_state.h"
23 #include "anbox/logger.h"
24
25 #if defined(Status)
26 #undef Status
27 #endif // defined(Status)
28
29 #include "anbox_bridge.pb.h"
30
31 #ifdef USE_PROTOBUF_CALLBACK_HEADER
32 #include <google/protobuf/stubs/callback.h>
33 #endif
34
35 namespace anbox {
36 namespace bridge {
37 PlatformApiSkeleton::PlatformApiSkeleton(
38     const std::shared_ptr<rpc::PendingCallCache> &pending_calls,
39     const std::shared_ptr<platform::BasePlatform> &platform,
40     const std::shared_ptr<wm::Manager> &window_manager,
41     const std::shared_ptr<application::Database> &app_db)
42     : pending_calls_(pending_calls),
43       platform_(platform),
44       window_manager_(window_manager),
45       app_db_(app_db) {}
46
47 PlatformApiSkeleton::~PlatformApiSkeleton() {}
48
49
50 void PlatformApiSkeleton::set_clipboard_data(anbox::protobuf::bridge::ClipboardData const *request,
51                                              anbox::protobuf::rpc::Void *response,
52                                              google::protobuf::Closure *done) {
53   (void)response;
54
55   if (request->has_text())
56     platform_->set_clipboard_data(platform::BasePlatform::ClipboardData{request->text()});
57
58   done->Run();
59 }
60
61 void PlatformApiSkeleton::get_clipboard_data(anbox::protobuf::rpc::Void const *request,
62                                              anbox::protobuf::bridge::ClipboardData *response,
63                                              google::protobuf::Closure *done) {
64   (void)request;
65
66   auto data = platform_->get_clipboard_data();
67   if (!data.text.empty())
68     response->set_text(data.text);
69
70   done->Run();
71 }
72
73 void PlatformApiSkeleton::handle_boot_finished_event(const anbox::protobuf::bridge::BootFinishedEvent&) {
74   if (boot_finished_handler_)
75     boot_finished_handler_();
76 }
77
78 void PlatformApiSkeleton::handle_window_state_update_event(const anbox::protobuf::bridge::WindowStateUpdateEvent &event) {
79   auto convert_window_state = [](
80       const ::anbox::protobuf::bridge::WindowStateUpdateEvent_WindowState
81           &window) {
82     return wm::WindowState(
83         wm::Display::Id(window.display_id()), window.has_surface(),
84         graphics::Rect(window.frame_left(), window.frame_top(),
85                        window.frame_right(), window.frame_bottom()),
86         window.package_name(), wm::Task::Id(window.task_id()),
87         wm::Stack::Id(window.stack_id()));
88   };
89
90   wm::WindowState::List updated;
91   for (int n = 0; n < event.windows_size(); n++) {
92     const auto window = event.windows(n);
93     updated.push_back(convert_window_state(window));
94   }
95
96   wm::WindowState::List removed;
97   for (int n = 0; n < event.removed_windows_size(); n++) {
98     const auto window = event.removed_windows(n);
99     removed.push_back(convert_window_state(window));
100   }
101
102   window_manager_->apply_window_state_update(updated, removed);
103 }
104
105 void PlatformApiSkeleton::handle_application_list_update_event(const anbox::protobuf::bridge::ApplicationListUpdateEvent &event) {
106   for (int n = 0; n < event.removed_applications_size(); n++) {
107     application::Database::Item item;
108
109     const auto app = event.removed_applications(n);
110     item.package = app.package();
111
112     if (item.package.empty())
113       continue;
114
115     app_db_->remove(item);
116   }
117
118   for (int n = 0; n < event.applications_size(); n++) {
119     application::Database::Item item;
120
121     const auto app = event.applications(n);
122     item.name = app.name();
123     item.package = app.package();
124
125     const auto li = app.launch_intent();
126     item.launch_intent.action = li.action();
127     item.launch_intent.uri = li.uri();
128     item.launch_intent.type = li.uri();
129     item.launch_intent.package = li.package();
130     item.launch_intent.component = li.component();
131
132     for (int m = 0; m < li.categories_size(); m++)
133       item.launch_intent.categories.push_back(li.categories(m));
134
135     item.icon = std::vector<char>(app.icon().begin(), app.icon().end());
136
137     if (item.package.empty())
138       continue;
139
140     app_db_->store_or_update(item);
141   }
142 }
143
144 void PlatformApiSkeleton::register_boot_finished_handler(const std::function<void()> &action) {
145   boot_finished_handler_ = action;
146 }
147 }  // namespace bridge
148 }  // namespace anbox