2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <ewk_chromium.h>
18 #include <watch_app.h>
19 #include <appcore-watch.h>
24 #include "common/application_data.h"
25 #include "common/app_control.h"
26 #include "common/app_db.h"
27 #include "common/command_line.h"
28 #include "common/logger.h"
29 #include "common/profiler.h"
30 #include "runtime/common/constants.h"
31 #include "runtime/browser/native_watch_window.h"
32 #include "runtime/browser/preload_manager.h"
33 #include "runtime/browser/runtime.h"
34 #include "runtime/browser/watch_runtime.h"
40 static NativeWindow* CreateNativeWindow() {
42 NativeWindow* window = NULL;
43 auto cached = PreloadManager::GetInstance()->GetCachedNativeWindow();
44 if (cached != nullptr) {
47 window = new NativeWatchWindow();
55 WatchRuntime::WatchRuntime(common::ApplicationData* app_data)
61 WatchRuntime::~WatchRuntime() {
66 delete native_window_;
70 bool WatchRuntime::OnCreate() {
71 STEP_PROFILE_END("watch_app_main -> OnCreate");
72 STEP_PROFILE_END("Start -> OnCreate");
73 STEP_PROFILE_START("OnCreate -> URL Set");
75 common::CommandLine* cmd = common::CommandLine::ForCurrentProcess();
76 std::string appid = cmd->GetAppIdFromCommandLine(kRuntimeExecName);
78 // Init AppDB for Runtime
79 common::AppDB* appdb = common::AppDB::GetInstance();
80 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeName, "xwalk-tizen");
81 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeAppID, appid);
82 if (app_data_->setting_info()->background_support_enabled()) {
83 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBackgroundSupport, "true");
85 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBackgroundSupport, "false");
87 appdb->Remove(kAppDBRuntimeSection, kAppDBRuntimeBundle);
89 // Init WebApplication
90 native_window_ = CreateNativeWindow();
91 STEP_PROFILE_START("WebApplication Create");
92 application_ = new WebApplication(native_window_, app_data_);
93 STEP_PROFILE_END("WebApplication Create");
94 application_->set_terminator([](){ watch_app_exit(); });
96 setlocale(LC_ALL, "");
97 bindtextdomain(kTextDomainRuntime, kTextLocalePath);
102 void WatchRuntime::OnTerminate() {
105 application_ = nullptr;
107 if (native_window_) {
108 delete native_window_;
109 native_window_ = nullptr;
113 void WatchRuntime::OnPause() {
114 if (application_->launched()) {
115 application_->Suspend();
119 void WatchRuntime::OnResume() {
120 if (application_->launched()) {
121 application_->Resume();
125 void WatchRuntime::OnAppControl(app_control_h app_control) {
127 std::unique_ptr<common::AppControl>
128 appcontrol(new common::AppControl(app_control));
129 common::AppDB* appdb = common::AppDB::GetInstance();
130 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBundle,
131 appcontrol->encoded_bundle());
132 if (application_->launched()) {
133 application_->AppControl(std::move(appcontrol));
135 application_->Launch(std::move(appcontrol));
139 void WatchRuntime::OnLanguageChanged(const std::string& language) {
141 application_->OnLanguageChanged();
142 elm_language_set(language.c_str());
146 void WatchRuntime::OnLowMemory() {
148 application_->OnLowMemory();
152 void WatchRuntime::OnTimeTick(watch_time_h watch_time) {
153 //do not fire tick event for normal clock for web app
156 int ret = watch_time_get_utc_timestamp(watch_time, &time);
158 LOGGER(DEBUG) << "time : " << time;
159 application_->OnTimeTick(time);
161 LOGGER(DEBUG) << "Fail to get utc time. skip send time tick event";
166 void WatchRuntime::OnAmbientTick(watch_time_h watch_time) {
168 int ret = watch_time_get_utc_timestamp(watch_time, &time);
170 LOGGER(DEBUG) << "time : " << time;
171 application_->OnAmbientTick(time);
173 LOGGER(ERROR) << "Fail to get utc time. skip send ambient tick event";
177 void WatchRuntime::OnAmbientChanged(bool ambient_mode) {
178 application_->OnAmbientChanged(ambient_mode);
181 int WatchRuntime::Exec(int argc, char* argv[]) {
182 watch_app_lifecycle_callback_s ops =
183 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
186 ops.create = [](int width, int height, void *data) -> bool {
187 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
189 LOGGER(ERROR) << "Runtime has not been created.";
192 return runtime->OnCreate();
196 ops.terminate = [](void* data) -> void {
197 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
199 LOGGER(ERROR) << "Runtime has not been created.";
202 runtime->OnTerminate();
206 ops.pause = [](void* data) -> void {
207 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
209 LOGGER(ERROR) << "Runtime has not been created.";
216 ops.resume = [](void* data) -> void {
217 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
219 LOGGER(ERROR) << "Runtime has not been created.";
226 ops.app_control = [](app_control_h app_control, void* data) -> void {
227 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
229 LOGGER(ERROR) << "Runtime has not been created.";
232 runtime->OnAppControl(app_control);
236 ops.time_tick = [](watch_time_h watch_time, void* data) -> void {
237 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
239 LOGGER(ERROR) << "Runtime has not been created.";
242 runtime->OnTimeTick(watch_time);
246 ops.ambient_tick = [](watch_time_h watch_time, void* data) -> void {
247 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
249 LOGGER(ERROR) << "Runtime has not been created.";
252 runtime->OnAmbientTick(watch_time);
256 ops.ambient_changed = [](bool ambient_mode, void* data) -> void {
257 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
259 LOGGER(ERROR) << "Runtime has not been created.";
262 // To render web application on ambient mode
268 runtime->OnAmbientChanged(ambient_mode);
271 // language changed callback
272 auto language_changed = [](app_event_info_h event_info, void* user_data) {
274 if (app_event_get_language(event_info, &str) == 0 && str != NULL) {
275 std::string language = std::string(str);
277 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(user_data);
278 runtime->OnLanguageChanged(language);
282 auto low_memory = [](app_event_info_h /*event_info*/, void* user_data) {
283 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(user_data);
284 runtime->OnLowMemory();
287 app_event_handler_h ev_handle;
288 watch_app_add_event_handler(&ev_handle,
289 APP_EVENT_LANGUAGE_CHANGED,
292 watch_app_add_event_handler(&ev_handle,
293 APP_EVENT_LOW_MEMORY,
296 STEP_PROFILE_START("watch_app_main -> OnCreate");
298 return watch_app_main(argc, argv, &ops, this);
301 } // namespace runtime