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 appdb->Remove(kAppDBRuntimeSection, kAppDBRuntimeBundle);
84 // Init WebApplication
85 native_window_ = CreateNativeWindow();
86 STEP_PROFILE_START("WebApplication Create");
87 application_ = new WebApplication(native_window_, app_data_);
88 STEP_PROFILE_END("WebApplication Create");
89 application_->set_terminator([](){ watch_app_exit(); });
91 setlocale(LC_ALL, "");
92 bindtextdomain(kTextDomainRuntime, kTextLocalePath);
97 void WatchRuntime::OnTerminate() {
100 application_ = nullptr;
102 if (native_window_) {
103 delete native_window_;
104 native_window_ = nullptr;
108 void WatchRuntime::OnPause() {
109 if (application_->launched()) {
110 application_->Suspend();
114 void WatchRuntime::OnResume() {
115 if (application_->launched()) {
116 application_->Resume();
120 void WatchRuntime::OnAppControl(app_control_h app_control) {
122 std::unique_ptr<common::AppControl>
123 appcontrol(new common::AppControl(app_control));
124 common::AppDB* appdb = common::AppDB::GetInstance();
125 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBundle,
126 appcontrol->encoded_bundle());
127 if (application_->launched()) {
128 application_->AppControl(std::move(appcontrol));
130 application_->Launch(std::move(appcontrol));
134 void WatchRuntime::OnLanguageChanged(const std::string& language) {
136 application_->OnLanguageChanged();
137 elm_language_set(language.c_str());
141 void WatchRuntime::OnLowMemory() {
143 application_->OnLowMemory();
147 void WatchRuntime::OnTimeTick(watch_time_h watch_time) {
148 //do not fire tick event for normal clock for web app
151 int ret = watch_time_get_utc_timestamp(watch_time, &time);
153 LOGGER(DEBUG) << "time : " << time;
154 application_->OnTimeTick(time);
156 LOGGER(DEBUG) << "Fail to get utc time. skip send time tick event";
161 void WatchRuntime::OnAmbientTick(watch_time_h watch_time) {
163 int ret = watch_time_get_utc_timestamp(watch_time, &time);
165 LOGGER(DEBUG) << "time : " << time;
166 application_->OnAmbientTick(time);
168 LOGGER(ERROR) << "Fail to get utc time. skip send ambient tick event";
172 void WatchRuntime::OnAmbientChanged(bool ambient_mode) {
173 application_->OnAmbientChanged(ambient_mode);
176 int WatchRuntime::Exec(int argc, char* argv[]) {
177 watch_app_lifecycle_callback_s ops =
178 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
181 ops.create = [](int width, int height, void *data) -> bool {
182 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
184 LOGGER(ERROR) << "Runtime has not been created.";
187 return runtime->OnCreate();
191 ops.terminate = [](void* data) -> void {
192 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
194 LOGGER(ERROR) << "Runtime has not been created.";
197 runtime->OnTerminate();
201 ops.pause = [](void* data) -> void {
202 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
204 LOGGER(ERROR) << "Runtime has not been created.";
211 ops.resume = [](void* data) -> void {
212 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
214 LOGGER(ERROR) << "Runtime has not been created.";
221 ops.app_control = [](app_control_h app_control, void* data) -> void {
222 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
224 LOGGER(ERROR) << "Runtime has not been created.";
227 runtime->OnAppControl(app_control);
231 ops.time_tick = [](watch_time_h watch_time, void* data) -> void {
232 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
234 LOGGER(ERROR) << "Runtime has not been created.";
237 runtime->OnTimeTick(watch_time);
241 ops.ambient_tick = [](watch_time_h watch_time, void* data) -> void {
242 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
244 LOGGER(ERROR) << "Runtime has not been created.";
247 runtime->OnAmbientTick(watch_time);
251 ops.ambient_changed = [](bool ambient_mode, void* data) -> void {
252 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
254 LOGGER(ERROR) << "Runtime has not been created.";
257 // To render web application on ambient mode
263 runtime->OnAmbientChanged(ambient_mode);
266 // language changed callback
267 auto language_changed = [](app_event_info_h event_info, void* user_data) {
269 if (app_event_get_language(event_info, &str) == 0 && str != NULL) {
270 std::string language = std::string(str);
272 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(user_data);
273 runtime->OnLanguageChanged(language);
277 auto low_memory = [](app_event_info_h /*event_info*/, void* user_data) {
278 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(user_data);
279 runtime->OnLowMemory();
282 app_event_handler_h ev_handle;
283 watch_app_add_event_handler(&ev_handle,
284 APP_EVENT_LANGUAGE_CHANGED,
287 watch_app_add_event_handler(&ev_handle,
288 APP_EVENT_LOW_MEMORY,
291 STEP_PROFILE_START("watch_app_main -> OnCreate");
293 return watch_app_main(argc, argv, &ops, this);
296 } // namespace runtime