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 <watch_app.h>
18 #include <appcore-watch.h>
23 #include "common/application_data.h"
24 #include "common/app_control.h"
25 #include "common/app_db.h"
26 #include "common/command_line.h"
27 #include "common/logger.h"
28 #include "common/profiler.h"
29 #include "runtime/common/constants.h"
30 #include "runtime/browser/native_watch_window.h"
31 #include "runtime/browser/preload_manager.h"
32 #include "runtime/browser/runtime.h"
33 #include "runtime/browser/watch_runtime.h"
39 static NativeWindow* CreateNativeWindow() {
41 NativeWindow* window = NULL;
42 auto cached = PreloadManager::GetInstance()->GetCachedNativeWindow();
43 if (cached != nullptr) {
46 window = new NativeWatchWindow();
54 WatchRuntime::WatchRuntime(common::ApplicationData* app_data)
60 WatchRuntime::~WatchRuntime() {
65 delete native_window_;
69 bool WatchRuntime::OnCreate() {
70 STEP_PROFILE_END("watch_app_main -> OnCreate");
71 STEP_PROFILE_END("Start -> OnCreate");
72 STEP_PROFILE_START("OnCreate -> URL Set");
74 common::CommandLine* cmd = common::CommandLine::ForCurrentProcess();
75 std::string appid = cmd->GetAppIdFromCommandLine(kRuntimeExecName);
77 // Init AppDB for Runtime
78 common::AppDB* appdb = common::AppDB::GetInstance();
79 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeName, "xwalk-tizen");
80 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeAppID, appid);
81 if (app_data_->setting_info()->background_support_enabled()) {
82 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBackgroundSupport, "true");
84 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBackgroundSupport, "false");
86 appdb->Remove(kAppDBRuntimeSection, kAppDBRuntimeBundle);
88 // Init WebApplication
89 native_window_ = CreateNativeWindow();
90 STEP_PROFILE_START("WebApplication Create");
91 application_ = new WebApplication(native_window_, app_data_);
92 STEP_PROFILE_END("WebApplication Create");
93 application_->set_terminator([](){ watch_app_exit(); });
95 setlocale(LC_ALL, "");
96 bindtextdomain(kTextDomainRuntime, kTextLocalePath);
101 void WatchRuntime::OnTerminate() {
104 application_ = nullptr;
106 if (native_window_) {
107 delete native_window_;
108 native_window_ = nullptr;
112 void WatchRuntime::OnPause() {
113 if (application_->launched()) {
114 application_->Suspend();
118 void WatchRuntime::OnResume() {
119 if (application_->launched()) {
120 application_->Resume();
124 void WatchRuntime::OnAppControl(app_control_h app_control) {
126 std::unique_ptr<common::AppControl>
127 appcontrol(new common::AppControl(app_control));
128 common::AppDB* appdb = common::AppDB::GetInstance();
129 appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBundle,
130 appcontrol->encoded_bundle());
131 if (application_->launched()) {
132 application_->AppControl(std::move(appcontrol));
134 application_->Launch(std::move(appcontrol));
138 void WatchRuntime::OnLanguageChanged(const std::string& language) {
140 application_->OnLanguageChanged();
141 elm_language_set(language.c_str());
145 void WatchRuntime::OnLowMemory() {
147 application_->OnLowMemory();
151 void WatchRuntime::OnTimeTick(watch_time_h watch_time) {
152 //do not fire tick event for normal clock for web app
155 int ret = watch_time_get_utc_timestamp(watch_time, &time);
157 LOGGER(DEBUG) << "time : " << time;
158 application_->OnTimeTick(time);
160 LOGGER(DEBUG) << "Fail to get utc time. skip send time tick event";
165 void WatchRuntime::OnAmbientTick(watch_time_h watch_time) {
167 int ret = watch_time_get_utc_timestamp(watch_time, &time);
169 LOGGER(DEBUG) << "time : " << time;
170 application_->OnAmbientTick(time);
172 LOGGER(ERROR) << "Fail to get utc time. skip send ambient tick event";
176 void WatchRuntime::OnAmbientChanged(bool ambient_mode) {
177 application_->OnAmbientChanged(ambient_mode);
180 int WatchRuntime::Exec(int argc, char* argv[]) {
181 watch_app_lifecycle_callback_s ops =
182 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
185 ops.create = [](int width, int height, void *data) -> bool {
186 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
188 LOGGER(ERROR) << "Runtime has not been created.";
191 return runtime->OnCreate();
195 ops.terminate = [](void* data) -> void {
196 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
198 LOGGER(ERROR) << "Runtime has not been created.";
201 runtime->OnTerminate();
205 ops.pause = [](void* data) -> void {
206 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
208 LOGGER(ERROR) << "Runtime has not been created.";
215 ops.resume = [](void* data) -> void {
216 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
218 LOGGER(ERROR) << "Runtime has not been created.";
225 ops.app_control = [](app_control_h app_control, void* data) -> void {
226 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
228 LOGGER(ERROR) << "Runtime has not been created.";
231 runtime->OnAppControl(app_control);
235 ops.time_tick = [](watch_time_h watch_time, void* data) -> void {
236 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
238 LOGGER(ERROR) << "Runtime has not been created.";
241 runtime->OnTimeTick(watch_time);
245 ops.ambient_tick = [](watch_time_h watch_time, void* data) -> void {
246 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
248 LOGGER(ERROR) << "Runtime has not been created.";
251 runtime->OnAmbientTick(watch_time);
255 ops.ambient_changed = [](bool ambient_mode, void* data) -> void {
256 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
258 LOGGER(ERROR) << "Runtime has not been created.";
261 // To render web application on ambient mode
267 runtime->OnAmbientChanged(ambient_mode);
270 // language changed callback
271 auto language_changed = [](app_event_info_h event_info, void* user_data) {
273 if (app_event_get_language(event_info, &str) == 0 && str != NULL) {
274 std::string language = std::string(str);
276 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(user_data);
277 runtime->OnLanguageChanged(language);
281 auto low_memory = [](app_event_info_h /*event_info*/, void* user_data) {
282 WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(user_data);
283 runtime->OnLowMemory();
286 app_event_handler_h ev_handle;
287 watch_app_add_event_handler(&ev_handle,
288 APP_EVENT_LANGUAGE_CHANGED,
291 watch_app_add_event_handler(&ev_handle,
292 APP_EVENT_LOW_MEMORY,
295 STEP_PROFILE_START("watch_app_main -> OnCreate");
297 return watch_app_main(argc, argv, &ops, this);
300 } // namespace runtime