modify code

Signed-off-by: zhaolinglan <zhaolinglan@huawei.com>
This commit is contained in:
zhaolinglan 2022-11-10 14:54:04 +08:00
parent 577091a217
commit 61c07c7e2b
7 changed files with 90 additions and 55 deletions

View File

@ -4,6 +4,7 @@
"path" : ["/system/bin/sa_main", "/system/profile/inputmethod_service.xml"],
"uid" : "inputmethod",
"gid" : ["inputmethod", "shell"],
"permission" : ["ohos.permission.INPUT_MONITORING"],
"caps" : [],
"secon" : "u:r:inputmethod_service:s0"
}

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef INPUTMETHOD_IMF_INPUT_EVENT_CALLBACK_H
#define INPUTMETHOD_IMF_INPUT_EVENT_CALLBACK_H
#include <input_manager.h>
#include <key_event.h>
#include "global.h"
namespace OHOS {
namespace MiscServices {
class InputEventCallback : public MMI::IInputEventConsumer {
virtual void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const;
virtual void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const;
virtual void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const;
};
void InputEventCallback::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
{
IMSA_HILOGI("OnInputEvent");
auto pressKeys = keyEvent->GetPressedKeys();
for (const auto key : pressKeys) {
IMSA_HILOGI("OnInputEvent key code: %{public}d", key);
}
}
void InputEventCallback::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
{
}
void InputEventCallback::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
{
}
} // namespace MiscServices
} // namespace OHOS
#endif //INPUTMETHOD_IMF_INPUT_EVENT_CALLBACK_H

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -16,15 +16,15 @@
#include "keyboard_event.h"
#include <global.h>
#include <input_manager.h>
#include <key_event.h>
#include <memory>
#include "global.h"
#include "input_event_callback.h"
namespace OHOS::MiscServices {
namespace OHOS {
namespace MiscServices {
using namespace MMI;
KeyboardEvent &KeyboardEvent::GetInstance()
@ -33,6 +33,19 @@ KeyboardEvent &KeyboardEvent::GetInstance()
return keyboardEvent;
}
int32_t KeyboardEvent::InitKeyEventMonitor()
{
IMSA_HILOGI("KeyboardEvent::InitKeyMonitor");
std::shared_ptr<InputEventCallback> callback = std::make_shared<InputEventCallback>();
int32_t monitorId =
InputManager::GetInstance()->AddMonitor(std::static_pointer_cast<MMI::IInputEventConsumer>(callback));
if (monitorId < 0) {
IMSA_HILOGE("Add monitor failed, id: %{public}d", monitorId);
return ErrorCode::ERROR_STATUS_UNKNOWN_ERROR;
}
return ErrorCode::NO_ERROR;
}
int32_t KeyboardEvent::SubscribeKeyboardEvent(const CombineKey &combine, KeyHandle handle)
{
std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
@ -53,4 +66,5 @@ int32_t KeyboardEvent::SubscribeKeyboardEvent(const CombineKey &combine, KeyHand
IMSA_HILOGI("subscribe %{public}d key event %{public}s", combine.finalKey, ret >= 0 ? "OK" : "ERROR");
return ret >= 0 ? ErrorCode::NO_ERROR : ErrorCode::ERROR_SUBSCRIBE_KEYBOARD_EVENT;
}
} // namespace OHOS::MiscServices
} // namespace MiscServices
} // namespace OHOS

View File

@ -16,12 +16,16 @@
#ifndef IMF_KEYBOARD_EVENT_H
#define IMF_KEYBOARD_EVENT_H
#include <key_event.h>
#include <cstdint>
#include <functional>
#include <set>
namespace OHOS ::MiscServices {
#include "global.h"
namespace OHOS {
namespace MiscServices {
struct CombineKey {
std::set<int32_t> preKeys;
int32_t finalKey;
@ -35,6 +39,7 @@ class KeyboardEvent {
public:
static KeyboardEvent &GetInstance();
int32_t SubscribeKeyboardEvent(const CombineKey &combine, KeyHandle handle);
int32_t InitKeyEventMonitor();
private:
static constexpr int32_t PRESS_KEY_DELAY_MS = 200;
@ -50,5 +55,6 @@ struct KeyboardEventHandler {
KeyHandle handle;
};
} // namespace OHOS::MiscServices
} // namespace MiscServices
} // namespace OHOS
#endif // IMF_KEYBOARD_EVENT_H

View File

@ -37,7 +37,7 @@ public:
~ImCommonEventManager();
static sptr<ImCommonEventManager> GetInstance();
bool SubscribeEvent(const std::string &event);
bool SubscribeKeyboardEvent(const std::vector<KeyboardEventHandler> &handlers);
bool SubscribeKeyboardEvent();
bool UnsubscribeEvent();
class EventSubscriber : public EventFwk::CommonEventSubscriber {

View File

@ -17,7 +17,6 @@
#include <utility>
#include "../adapter/keyboard/keyboard_event.h"
#include "global.h"
#include "input_method_system_ability_stub.h"
#include "ipc_skeleton.h"
@ -91,19 +90,17 @@ bool ImCommonEventManager::SubscribeEvent(const std::string &event)
return true;
}
bool ImCommonEventManager::SubscribeKeyboardEvent(const std::vector<KeyboardEventHandler> &handlers)
bool ImCommonEventManager::SubscribeKeyboardEvent()
{
IMSA_HILOGI("ImCommonEventManager::SubscribeKeyboardEvent");
auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (abilityManager == nullptr) {
IMSA_HILOGE("SubscribeEvent abilityManager is nullptr");
return false;
}
sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([handlers]() {
for (const auto &handler : handlers) {
int32_t ret = KeyboardEvent::GetInstance().SubscribeKeyboardEvent(handler.combine, handler.handle);
IMSA_HILOGI("subscribe %{public}d key event %{public}s", handler.combine.finalKey,
ret == ErrorCode::NO_ERROR ? "OK" : "ERROR");
}
sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([]() {
int32_t ret = KeyboardEvent::GetInstance().InitKeyEventMonitor();
IMSA_HILOGI("InitKeyEventMonitor init monitor %{public}s", ret == ErrorCode::NO_ERROR ? "success" : "failed");
});
if (listener == nullptr) {
IMSA_HILOGE("SubscribeEvent listener is nullptr");

View File

@ -16,8 +16,8 @@
#include "input_method_system_ability.h"
#include <global.h>
#include <utils.h>
#include <key_event.h>
#include <utils.h>
#include "../adapter/keyboard/keyboard_event.h"
#include "ability_connect_callback_proxy.h"
@ -185,7 +185,7 @@ namespace MiscServices {
StartInputService(defaultIme);
StartUserIdListener();
int32_t ret = SubscribeKeyboardEvent();
IMSA_HILOGI("subscribe key event ret %{public}d", ret);
IMSA_HILOGI("subscribe key event ret %{public}s", ret == ErrorCode::NO_ERROR ? "success" : "failed");
return ErrorCode::NO_ERROR;
}
@ -1446,43 +1446,9 @@ namespace MiscServices {
int32_t InputMethodSystemAbility::SubscribeKeyboardEvent()
{
ImCommonEventManager::GetInstance()->SubscribeKeyboardEvent(
{ { {
.preKeys = {},
.finalKey = MMI::KeyEvent::KEYCODE_CAPS_LOCK,
},
[this]() { SwitchByCombinedKey(CombineKeyCode::COMBINE_KEYCODE_CAPS); } },
{ {
.preKeys = {},
.finalKey = MMI::KeyEvent::KEYCODE_SHIFT_LEFT,
},
[this]() { SwitchByCombinedKey(CombineKeyCode::COMBINE_KEYCODE_SHIFT); } },
{ {
.preKeys = {},
.finalKey = MMI::KeyEvent::KEYCODE_SHIFT_RIGHT,
},
[this]() { SwitchByCombinedKey(CombineKeyCode::COMBINE_KEYCODE_SHIFT); } },
{ {
.preKeys = { MMI::KeyEvent::KEYCODE_CTRL_LEFT },
.finalKey = MMI::KeyEvent::KEYCODE_SHIFT_LEFT,
},
[this]() { SwitchByCombinedKey(CombineKeyCode::COMBINE_KEYCODE_CTRL_SHIFT); } },
{ {
.preKeys = { MMI::KeyEvent::KEYCODE_CTRL_LEFT },
.finalKey = MMI::KeyEvent::KEYCODE_SHIFT_RIGHT,
},
[this]() { SwitchByCombinedKey(CombineKeyCode::COMBINE_KEYCODE_CTRL_SHIFT); } },
{ {
.preKeys = { MMI::KeyEvent::KEYCODE_CTRL_RIGHT },
.finalKey = MMI::KeyEvent::KEYCODE_SHIFT_LEFT,
},
[this]() { SwitchByCombinedKey(CombineKeyCode::COMBINE_KEYCODE_CTRL_SHIFT); } },
{ {
.preKeys = { MMI::KeyEvent::KEYCODE_CTRL_RIGHT },
.finalKey = MMI::KeyEvent::KEYCODE_SHIFT_RIGHT,
},
[this]() { SwitchByCombinedKey(CombineKeyCode::COMBINE_KEYCODE_CTRL_SHIFT); } } });
return 0;
IMSA_HILOGI("InputMethodSystemAbility::SubscribeKeyboardEvent");
bool ret = ImCommonEventManager::GetInstance()->SubscribeKeyboardEvent();
return ret ? ErrorCode::NO_ERROR : ErrorCode::ERROR_SERVICE_START_FAILED;
}
} // namespace MiscServices
} // namespace OHOS