1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "swap_input.h"
17
18#include "input_manager.h"
19#include "multimode_manager.h"
20#include "wukong_define.h"
21#include "report.h"
22namespace OHOS {
23namespace WuKong {
24SwapInput::SwapInput() : InputAction()
25{
26    std::shared_ptr<MultimodeInputMsg> multimodeInputMsg = std::make_shared<MultimodeInputMsg>();
27    multimodeInputMsg->inputType_ = INPUTTYPE_SWAPINPUT;
28    inputedMsgObject_ = multimodeInputMsg;
29}
30
31SwapInput::~SwapInput()
32{
33}
34
35ErrCode SwapInput::OrderInput(const std::shared_ptr<SpcialTestObject>& specialTestObject)
36{
37    ErrCode result;
38    static bool isBack = true;
39    SwapParam* swapPtr = static_cast<SwapParam*>(specialTestObject.get());
40    if (swapPtr == nullptr) {
41        return OHOS::ERR_INVALID_VALUE;
42    }
43    int xSrcPosition = swapPtr->startX_;
44    int ySrcPosition = swapPtr->startY_;
45    int xDstPosition = swapPtr->endX_;
46    int yDstPosition = swapPtr->endY_;
47    if (swapPtr->isGoBack_) {
48        isBack = !isBack;
49        swapPtr->isBack_ = isBack;
50    }
51    if (isBack) {
52        xSrcPosition = swapPtr->endX_;
53        ySrcPosition = swapPtr->endY_;
54        xDstPosition = swapPtr->startX_;
55        yDstPosition = swapPtr->startY_;
56    }
57    result = MultimodeManager::GetInstance()->IntervalSwap(xSrcPosition, ySrcPosition, xDstPosition, yDstPosition);
58    return result;
59}
60
61ErrCode SwapInput::RandomInput()
62{
63    int32_t screenWidth = -1;
64    int32_t screenHeight = -1;
65    ErrCode result = WuKongUtil::GetInstance()->GetScreenSize(screenWidth, screenHeight);
66    if (result != OHOS::ERR_OK) {
67        return result;
68    }
69    int xSrcPosition = rand() % screenWidth;
70    int ySrcPosition = rand() % screenHeight;
71    int xDstPosition = rand() % screenWidth;
72    int yDstPosition = rand() % screenHeight;
73    INFO_LOG_STR("Swap: (%d, %d) -> (%d, %d)", xSrcPosition, ySrcPosition, xDstPosition, yDstPosition);
74    result = MultimodeManager::GetInstance()->IntervalSwap(xSrcPosition, ySrcPosition, xDstPosition, yDstPosition);
75    if (result != OHOS::ERR_OK) {
76        return result;
77    }
78    Report::GetInstance()->SyncInputInfo(inputedMsgObject_);
79    return result;
80}
81
82InputType SwapInput::GetInputInfo()
83{
84    return INPUTTYPE_SWAPINPUT;
85}
86}  // namespace WuKong
87}  // namespace OHOS
88