~aleteoryx/muditaos

ref: fbe3d8df0b72ca84edccf604e2f885b34043968b muditaos/module-apps/application-call/windows/CallWindow.cpp -rw-r--r-- 12.7 KiB
fbe3d8df — rrandomsky [MOS-644] Displaying number entered on the keypad during a call 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ApplicationCall.hpp"
#include "CallAppStyle.hpp"
#include "CallSwitchData.hpp"
#include "CallWindow.hpp"
#include "StateIcons.hpp"

#include <application-messages/data/SMSdata.hpp>
#include <GuiTimer.hpp>
#include <i18n/i18n.hpp>
#include <InputEvent.hpp>
#include <text/modes/InputMode.hpp>
#include <Label.hpp>
#include <log/log.hpp>
#include <Margins.hpp>
#include <service-appmgr/Controller.hpp>

#include <functional>
#include <memory>

namespace gui
{
    using namespace callAppStyle;
    using namespace callAppStyle::callWindow;
    using namespace app::call;

    CallWindow::CallWindow(app::ApplicationCommon *app, app::call::CallWindowContract::Presenter &presenter)
        : gui::AppWindow{app, gui::window::name::call}, presenter{presenter}
    {
        presenter.attach(this);
        presenter.attachCallbacks();
        buildInterface();
    }

    CallWindow::~CallWindow() noexcept
    {
        presenter.clearModel();
    }

    void CallWindow::rebuild()
    {
        destroyInterface();
        buildInterface();
    }

    void CallWindow::buildInterface()
    {
        AppWindow::buildInterface();

        navBar->setActive(nav_bar::Side::Center, true);
        navBar->setActive(nav_bar::Side::Right, true);

        navBar->setText(nav_bar::Side::Center, utils::translate(style::strings::common::select));
        navBar->setText(nav_bar::Side::Right, utils::translate(style::strings::common::back));
        navBar->setText(gui::nav_bar::Side::Center, utils::translate(strings::message));

        imageCircleTop = new gui::Image(this, imageCircleTop::x, imageCircleTop::y, 0, 0, imageCircleTop::name);
        imageCircleBottom =
            new gui::Image(this, imageCircleBottom::x, imageCircleBottom::y, 0, 0, imageCircleBottom::name);

        durationLabel = new gui::Label(this, durationLabel::x, durationLabel::y, durationLabel::w, durationLabel::h);
        durationLabel->setFilled(false);
        durationLabel->setBorderColor(gui::ColorNoColor);
        durationLabel->setFont(style::window::font::mediumlight);
        durationLabel->setAlignment(
            gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Bottom));

        enteredDigitsLabel = new gui::Label(
            this, enteredDigitsLabel::x, enteredDigitsLabel::y, enteredDigitsLabel::w, enteredDigitsLabel::h);
        enteredDigitsLabel->setFilled(false);
        enteredDigitsLabel->setBorderColor(gui::ColorNoColor);
        enteredDigitsLabel->setEdges(gui::RectangleEdge::None);
        enteredDigitsLabel->setTextEllipsisType(gui::TextEllipsis::Left);
        enteredDigitsLabel->setFont(style::window::font::mediumbold);
        enteredDigitsLabel->setAlignment(
            gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Bottom));
        enteredDigitsLabel->setCursorStartPosition(CursorStartPosition::DocumentEnd);

        numberLabel = new gui::Label(this, numberLabel::x, numberLabel::y, numberLabel::w, numberLabel::h);
        numberLabel->setFilled(false);
        numberLabel->setBorderColor(gui::ColorNoColor);
        numberLabel->setFont(style::window::font::largelight);
        numberLabel->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Top));

        iconsBox = new HBox(
            this, style::window::default_left_margin, iconsBox::y, style::window::default_body_width, iconsBox::h);
        iconsBox->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Top));
        iconsBox->setEdges(RectangleEdge::None);

        microphoneIcon                    = new MicrophoneIcon(iconsBox);
        microphoneIcon->activatedCallback = [=]([[maybe_unused]] gui::Item &item) {
            microphoneIcon->setNext();
            LOG_INFO("Microphone %s", static_cast<bool>(microphoneIcon->get()) ? "activated" : "deactivated");

            microphoneIcon->get() == MicrophoneIconState::MUTED ? presenter.muteCall() : presenter.unmuteCall();

            return true;
        };

        speakerIcon                    = new SpeakerIcon(iconsBox);
        speakerIcon->activatedCallback = [=]([[maybe_unused]] gui::Item &item) {
            speakerIcon->setNext();
            LOG_INFO("Speaker %s", static_cast<bool>(speakerIcon->get()) ? "activated" : "deactivated");

            switch (speakerIcon->get()) {
            case SpeakerIconState::SPEAKER: {
                presenter.turnLoudspeakerOff();
            } break;
            case SpeakerIconState::SPEAKERON: {
                presenter.turnLoudspeakerOn();
            } break;
            // case SpeakerIconState::BLUETOOTH: {
            //     // TODO: need implementation
            // } break;
            default:
                break;
            }

            return true;
        };

        sendSmsIcon                    = new gui::SendSmsIcon(iconsBox);
        sendSmsIcon->activatedCallback = [=]([[maybe_unused]] gui::Item &item) {
            LOG_INFO("Send message template and reject the call");
            auto msg = std::make_unique<SMSSendTemplateRequest>(presenter.getPhoneNumber().getView(),
                                                                SMSSendTemplateRequest::AutolockBehavior::Prevent);
            msg->ignoreCurrentWindowOnStack = true;
            msg->nameOfSenderApplication    = application->GetName();
            return app::manager::Controller::sendAction(application,
                                                        app::manager::actions::ShowSmsTemplates,
                                                        std::move(msg),
                                                        app::manager::OnSwitchBehaviour::RunInBackground);
        };

        sendSmsIcon->setVisible(false);
        microphoneIcon->setVisible(false);
        speakerIcon->setVisible(false);

        // define navigation between icons
        microphoneIcon->setNavigationItem(NavigationDirection::LEFT, speakerIcon);
        microphoneIcon->setNavigationItem(NavigationDirection::RIGHT, speakerIcon);

        speakerIcon->setNavigationItem(NavigationDirection::LEFT, microphoneIcon);
        speakerIcon->setNavigationItem(NavigationDirection::RIGHT, microphoneIcon);
    }

    void CallWindow::destroyInterface()
    {
        erase();
    }

    status_bar::Configuration CallWindow::configureStatusBar(status_bar::Configuration appConfiguration)
    {
        appConfiguration.enable(status_bar::Indicator::NetworkAccessTechnology);
        return appConfiguration;
    }

    void CallWindow::updateDuration(const UTF8 &text, bool isVisible)
    {
        if (durationLabel != nullptr) {
            durationLabel->setText(text);
            durationLabel->setVisible(isVisible);
        }
    }

    void CallWindow::updateEnteredNumber(const char &newCharacter, bool isVisible)
    {
        if (enteredDigitsLabel != nullptr && isVisible) {
            auto newText = enteredDigitsLabel->getText();
            newText.insert(&newCharacter);
            enteredDigitsLabel->setText(newText);
            enteredDigitsLabel->setVisible(isVisible);
        }
    }

    void CallWindow::setEnteredNumberVisible(bool isVisible)
    {
        if (enteredDigitsLabel != nullptr) {
            enteredDigitsLabel->setVisible(isVisible);
        }
    }

    void CallWindow::onBeforeShow([[maybe_unused]] ShowMode mode, SwitchData *data)
    {
        presenter.buildLayout();

        if (const auto switchData = dynamic_cast<SMSTemplateSent *>(data); switchData != nullptr) {
            presenter.hangUpCall();
            presenter.sendSms(switchData->getText());
            return;
        }
    }

    bool CallWindow::onInput(const InputEvent &inputEvent)
    {
        bool handled       = false;
        const auto keyCode = inputEvent.getKeyCode();

        // process only if key is released
        // InputEvent::State::keyReleasedLong is necessary for KeyCode::KEY_RF to properly abort the active call
        if (inputEvent.isKeyRelease()) {
            const auto code = translator.handle(inputEvent.getRawKey(), InputMode({InputMode::phone}).get());

            switch (keyCode) {
            case KeyCode::KEY_LF:
                handled = presenter.handleLeftButton();
                break;
            case KeyCode::KEY_RF:
                handled = presenter.handleRightButton();
                break;
            case KeyCode::HEADSET_OK:
                handled = presenter.handleHeadsetOk();
                break;
            default:
                break;
            }

            if (!handled && code != 0) {
                handled = presenter.handleDigitButton(code);
            }
        }

        if (handled) {
            application->refreshWindow(RefreshModes::GUI_REFRESH_FAST);
            return true;
        }

        if (keyCode == KeyCode::KEY_RF) {
            return true;
        }

        return AppWindow::onInput(inputEvent);
    }

    void CallWindow::connectTimerOnExit()
    {
        timerCallback = [this]([[maybe_unused]] Item &item, [[maybe_unused]] sys::Timer &timer) {
            LOG_DEBUG("Delayed exit timer callback");
            presenter.handleDelayedViewClose();
            application->popCurrentWindow();
            app::manager::Controller::switchBack(application);
            return true;
        };
        delayedExitTimer =
            app::GuiTimerFactory::createSingleShotTimer(application, this, "DelayedExitTimer", getDelayedStopTime());
        delayedExitTimer.start();
    }

    void CallWindow::refreshWindow()
    {
        application->refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
    }

    void CallWindow::setNavBarForActiveCall()
    {
        navBar->setActive(gui::nav_bar::Side::Left, false);
        navBar->setActive(gui::nav_bar::Side::Center, false);
        navBar->setText(gui::nav_bar::Side::Right, utils::translate(strings::endcall), true);
        navBar->setText(gui::nav_bar::Side::Center, utils::translate(style::strings::common::Switch), true);
    }

    void CallWindow::setNavBarForIncomingCall()
    {
        navBar->setText(gui::nav_bar::Side::Left, utils::translate(strings::answer), true);
        navBar->setText(gui::nav_bar::Side::Right, utils::translate(strings::reject), true);
    }

    void CallWindow::clearNavBar()
    {
        navBar->setActive(gui::nav_bar::Side::Left, false);
        navBar->setActive(gui::nav_bar::Side::Center, false);
        navBar->setActive(gui::nav_bar::Side::Right, false);
    }

    void CallWindow::setIncomingCallLayout(bool isValidCallerId)
    {
        navBar->setText(gui::nav_bar::Side::Left, utils::translate(strings::answer), true);
        navBar->setText(gui::nav_bar::Side::Right, utils::translate(strings::reject), true);

        microphoneIcon->setVisible(false);
        speakerIcon->setVisible(false);

        if (isValidCallerId) {
            navBar->setActive(gui::nav_bar::Side::Center, false);
            sendSmsIcon->setVisible(false);
            setFocusItem(nullptr);
        }
        else {
            navBar->setActive(gui::nav_bar::Side::Center, true);
            navBar->setText(gui::nav_bar::Side::Center, utils::translate(style::strings::common::send), true);
            sendSmsIcon->setVisible(true);
            setFocusItem(sendSmsIcon);
        }

        iconsBox->resizeItems();
    }

    void CallWindow::setActiveCallLayout()
    {
        navBar->setActive(gui::nav_bar::Side::Left, false);
        navBar->setActive(gui::nav_bar::Side::Center, false);
        navBar->setText(gui::nav_bar::Side::Right, utils::translate(strings::endcall), true);
        navBar->setText(gui::nav_bar::Side::Center, utils::translate(style::strings::common::Switch), true);

        sendSmsIcon->setVisible(false);
        microphoneIcon->setVisible(true);
        speakerIcon->setVisible(true);

        iconsBox->resizeItems();

        setFocusItem(microphoneIcon);
    }

    void CallWindow::setCallEndedLayout(bool delayedClose)
    {
        navBar->setActive(gui::nav_bar::Side::Left, false);
        navBar->setActive(gui::nav_bar::Side::Center, false);
        navBar->setActive(gui::nav_bar::Side::Right, false);

        sendSmsIcon->setVisible(false);
        microphoneIcon->setVisible(false);
        speakerIcon->setVisible(false);

        iconsBox->resizeItems();

        setFocusItem(nullptr);
        if (delayedClose) {
            connectTimerOnExit();
        }
    }

    void CallWindow::updateNumber(const UTF8 &text)
    {
        numberLabel->setText(text);
    }

    gui::SpeakerIconState CallWindow::getSpeakerIconState()
    {
        return speakerIcon->get();
    }

    void CallWindow::setSpeakerIconState(const gui::SpeakerIconState &icon)
    {
        speakerIcon->set(icon);
    }
} /* namespace gui */