Implements WatchBase
[platform/core/appfw/widget-viewer.git] / watch-holder-base / src / ambient_listener.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include <string>
17
18 #include <bundle_cpp.h>
19 #include <bundle.h>
20 #include <aul.h>
21 #include <aul_app_com.h>
22 #include <dlog.h>
23
24 #include "common.hh"
25 #include "ambient_listener.hh"
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30
31 #define LOG_TAG "WATCH_HOLDER"
32
33 using namespace tizen_base;
34 using namespace std;
35
36 namespace watch_holder {
37
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");
42   }
43
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");
47   }
48 }
49
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;
54
55   if (!envelope) {
56     LOGE("Bad bundle data from application");
57     return -1;
58   }
59
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());
70   return 0;
71 }
72
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__");
80   if (mode.empty())
81     return 0;
82
83   tizen_base::Bundle extra_data;
84   bool enter;
85   try {
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);
90       return 0;
91     }
92     enter = (state == AMBIENT_ENTER);
93   } catch (const std::exception& e) {
94     LOGE("Exception (%s)", e.what());
95     return 0;
96   }
97   listener->OnAmbientChanged(enter, extra_data);
98   LOGI("Handling AOD ambient changed(%d) done", (int)enter);
99   return 0;
100 }
101
102 AmbientListener::~AmbientListener() {
103   if (ambient_changed_signal_conn_)
104     aul_app_com_leave(ambient_changed_signal_conn_);
105
106   if (ambient_event_signal_conn_)
107     aul_app_com_leave(ambient_event_signal_conn_);
108 }
109
110 }  // namespace watch_holder