2 * Copyright 2018 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.
17 #include "UniversalSwitchPage.hpp"
20 #include "Singleton.hpp"
21 #include "setting-accessibility.h"
25 UniversalSwitchPage::UniversalSwitchPage()
26 : context_(Singleton<AppContext>::instance())
28 auto naviframe = context_.navContext_.getNaviframe();
29 auto genlist = Widget::make<Genlist>(naviframe);
30 genlist->setMode(ELM_LIST_COMPRESS);
31 genlist->setStyle("dialogue");
32 genlist->clear(); // TODO check if necessary
34 universalSwitchOffCheckbox_ = genlist->appendItem({"type1",
38 auto state = item->getState();
39 auto switch_count = setting_accessibility_universal_switch_dbus_config_get_switch_count(&context_.config);
40 if (state && switch_count == 0) {
41 this->displayAddSwitchPopup();
42 item->setState(false);
44 Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, state);
46 GenlistItem::WidgetType::toggle});
48 auto onUsVconfKeyChange = [this](auto state) {
49 universalSwitchOffCheckbox_->setText(state ? "IDS_ACCS_UNIVERSAL_SWITCH_ON" : "IDS_ACCS_UNIVERSAL_SWITCH_OFF");
50 universalSwitchOffCheckbox_->setState(state);
52 universalSwitchStateHandle_ = Singleton<VConfInterface>::instance().registerAndGet<bool>({VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE}, false, onUsVconfKeyChange);
54 genlist->appendItem({"multiline", {}, "IDS_ACCS_UNIVERSAL_SWITCH_COMMENT", GenlistItem::Type::regular});
56 auto prevBtn = Widget::make<Button>(naviframe, [this, naviframe]() {
57 naviframe->removeChild(usSettingsBtn_);
61 Button::BACK_BUTTON_ARROW_STYLE);
63 naviframe->pushBack("IDS_ACCS_UNIVERSAL_SWITCH", genlist, [this]() {
64 universalSwitchStateHandle_ = {};
65 stopUniversalSwitch();
66 universalSwitchOffCheckbox_ = nullptr;
70 std::string title_str = "<font_size=30>" + TranslatedString{"IDS_ACCS_SETTINGS_CAPS"}.str() + "</font_size>";
71 usSettingsBtn_ = Widget::make<Button>(naviframe, [this]() {
72 universalSwitchSettingsPage_ = std::make_unique<UniversalSwitchSettingsPage>();
75 "naviframe/title_right");
76 usSettingsBtn_->disable(true);
77 naviframe->setPartContent("title_right_btn", usSettingsBtn_);
79 startUniversalSwitch();
82 void UniversalSwitchPage::addNewSwitch()
84 addSwitchPage_ = std::make_unique<AddSwitchPage>();
85 addSwitchPage_->attachCallbackOnSwitchAttach(onNewSwitchAddition, this);
88 void UniversalSwitchPage::displayAddSwitchPopup()
90 auto popup = Widget::make<Popup>(context_.navContext_.getWindow());
91 popup->setText("To turn on Universal switch, tap ADD SWITCH and add at least one switch");
92 popup->setEextEventCallback(EEXT_CALLBACK_BACK, []() {});
94 auto addBtnCb = [popup, this]() {
96 auto parent = popup->getParent();
97 parent->removeChild(popup);
99 auto addBtn = Widget::make<Button>(popup, addBtnCb, "IDS_ACCS_UNIVERSAL_SWITCH_POPUP_ADD");
100 addBtn->setStyle("popup");
101 popup->setPartContent("button1", addBtn);
103 auto cancelBtnCb = [popup, this]() {
104 universalSwitchOffCheckbox_->setState(false);
105 DEBUG("Adding new switch refused");
106 auto parent = popup->getParent();
107 parent->removeChild(popup);
109 auto cancelBtn = Widget::make<Button>(popup, cancelBtnCb, "IDS_ACCS_UNIVERSAL_SWITCH_CANCEL");
110 cancelBtn->setStyle("popup");
111 popup->setPartContent("button2", cancelBtn);
114 void UniversalSwitchPage::onNewSwitchAddition(
116 const std::string &switch_id,
117 const std::string &switch_action,
118 const std::string &switch_name)
120 RETURN_IF(!data, "Input parameter is NULL");
121 auto self = static_cast<UniversalSwitchPage *>(data);
122 // TODO Try to use service which is independent from specific view
123 setting_accessibility_universal_switch_dbus_config_addSwitchConfigurationItem(
124 &self->context_.config, switch_id, switch_name, switch_action);
125 int switch_count = setting_accessibility_universal_switch_dbus_config_get_switch_count(&self->context_.config);
127 if (switch_count == 1)
128 Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, true);
130 DEBUG("New switch added. Id: %s, name: %s, action: %s. Switches count: %d",
133 switch_action.c_str(),
137 void UniversalSwitchPage::stopUniversalSwitch()
139 setting_accessibility_universal_switch_dbus_config_shutdown(&context_.config);
141 DEBUG("Stopping universal-switch");
142 Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE, false);
145 void UniversalSwitchPage::startUniversalSwitch()
147 DEBUG("Starting universal-switch");
148 Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE, true);
149 setting_accessibility_universal_switch_dbus_config_init(&context_.config, onUniversalSwitchStarting, this);
152 void UniversalSwitchPage::onUniversalSwitchStarting(void *data)
154 DEBUG("universal-switch started");
155 RETURN_IF(!data, "Data argument is NULL");
156 auto self = static_cast<UniversalSwitchPage *>(data);
158 self->universalSwitchOffCheckbox_->enable();
160 if (self->usSettingsBtn_)
161 self->usSettingsBtn_->disable(false);
164 void UniversalSwitchPage::onVconfKeyChange(keynode_t *node, void *user_data)
166 auto self = static_cast<UniversalSwitchPage *>(user_data);
168 auto state = vconf_keynode_get_bool(node);
169 auto prevState = self->universalSwitchOffCheckbox_->getState();
170 if (state >= 0 && (static_cast<bool>(state) != prevState)) {
171 self->universalSwitchOffCheckbox_->setText(
172 state ? "IDS_ACCS_UNIVERSAL_SWITCH_ON" : "IDS_ACCS_UNIVERSAL_SWITCH_OFF");
173 self->universalSwitchOffCheckbox_->setState(state);
175 ERROR("vconf_keynode_get_bool(node) failed");