2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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.
18 #include <bundle_cpp.h>
21 #include <aul_app_com.h>
25 #include "ambient_listener.hh"
31 #define LOG_TAG "WATCH_HOLDER"
33 using namespace tizen_base;
36 namespace watch_holder {
38 AmbientListener::AmbientListener() {
39 if (aul_app_com_create("watch.ambientchange", nullptr, OnAmbientChangedSignal,
40 this, &ambient_changed_signal_conn_) != AUL_R_OK) {
41 LOGE("Failed to listen watch.ambientchange signal");
44 if (aul_app_com_create("aod.ambientevent", nullptr, OnReceiveSignal,
45 this, &ambient_event_signal_conn_) != AUL_R_OK) {
46 LOGE("Failed to listen aod.ambientevent signal");
50 int AmbientListener::OnReceiveSignal(const char* endpoint,
51 aul_app_com_result_e e, bundle* envelope, void* user_data) {
52 LOGI("Receive aod ambient event");
53 AmbientListener* listener = (AmbientListener*)user_data;
56 LOGE("Bad bundle data from application");
60 tizen_base::Bundle b = tizen_base::Bundle(envelope, true, true);
61 std::string event_type = b.GetString("__APP_AMBIENT_EVENT__");
62 LOGI("event type (%s)", event_type.c_str());
63 int type = stoi(event_type);
64 std::string sender = b.GetString("__APP_AMBIENT_SENDER__");
65 b.Delete("__APP_AMBIENT_EVENT__");
66 b.Delete("__APP_AMBIENT_SENDER__");
67 listener->OnAmbientEvent((EventType)type, sender, b);
68 LOGI("Handling AOD ambient event(%d) from (%s) done",
69 type, sender.c_str());
73 int AmbientListener::OnAmbientChangedSignal(const char *endpoint,
74 aul_app_com_result_e e, bundle *envelope, void *user_data) {
75 LOGI("Receive ambient change event");
76 tizen_base::Bundle data(envelope, false, false);
77 AmbientListener* listener = (AmbientListener*)user_data;
78 std::string mode = data.GetString("__AMBIENT_MODE__");
79 std::string extra = data.GetString("__AMBIENT_EXTRA__");
83 tizen_base::Bundle extra_data;
86 extra_data = tizen_base::Bundle(extra);
87 AmbientState state = (AmbientState)stoi(mode);
88 if (state != AMBIENT_ENTER && state != AMBIENT_LEAVE) {
89 LOGW("Invalid state (%d)", (int)state);
92 enter = (state == AMBIENT_ENTER);
93 } catch (const std::exception& e) {
94 LOGE("Exception (%s)", e.what());
97 listener->OnAmbientChanged(enter, extra_data);
98 LOGI("Handling AOD ambient changed(%d) done", (int)enter);
102 AmbientListener::~AmbientListener() {
103 if (ambient_changed_signal_conn_)
104 aul_app_com_leave(ambient_changed_signal_conn_);
106 if (ambient_event_signal_conn_)
107 aul_app_com_leave(ambient_event_signal_conn_);
110 } // namespace watch_holder