diff --git a/services/BUILD.gn b/services/BUILD.gn index 7d631495cbbb6e2048d75c0a52f679a37dda50b9..7bfa3b9b0b2c8f3d43e8abac508dce2a349f367d 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -19,6 +19,8 @@ config("inputmethod_services_native_config") { "include", "${inputmethod_path}/frameworks/inputmethod_ability/include", "${inputmethod_path}/frameworks/inputmethod_controller/include", + "//base/notification/ces_standard/frameworks/core/include", + "//base/notification/ces_standard/interfaces/innerkits/native/include", ] cflags_cc = [ "-fexceptions" ] @@ -28,6 +30,7 @@ ohos_shared_library("inputmethod_service") { sources = [ "${inputmethod_path}/frameworks/inputmethod_controller/src/input_client_proxy.cpp", "src/global.cpp", + "src/im_common_event_manager.cpp", "src/input_attribute.cpp", "src/input_channel.cpp", "src/input_control_channel_proxy.cpp", @@ -76,8 +79,12 @@ ohos_shared_library("inputmethod_service") { ] deps += [ "//base/miscservices/inputmethod/services/dialog/js:dialog_ime_js_files_etc" ] - - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "os_account_standard:os_account_innerkits", + ] subsystem_name = "miscservices" part_name = "inputmethod_native" diff --git a/services/include/im_common_event_manager.h b/services/include/im_common_event_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..73787ad4e6477006f2d3c2b16b7d881df5651c57 --- /dev/null +++ b/services/include/im_common_event_manager.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2021 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 FM_IMMS_PROJECT_IMCOMMONEVENTMANAGER_H +#define FM_IMMS_PROJECT_IMCOMMONEVENTMANAGER_H + +#include + +#include "common_event_subscriber.h" +#include "common_event_subscribe_info.h" +#include "common_event_data.h" +#include "matching_skills.h" + +namespace OHOS { +namespace MiscServices { + +class ImCommonEventManager : public RefBase { +public: + ImCommonEventManager(); + ~ImCommonEventManager(); + static sptr GetInstance(); + bool SubscribeEvent(const std::string &event); + bool UnsubscribeEvent(); + + class EventSubscriber : public EventFwk::CommonEventSubscriber { + public: + EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo) : EventFwk::CommonEventSubscriber(subscribeInfo) {} + void OnReceiveEvent(const EventFwk::CommonEventData &data); + void startUser(int32_t newUserId); + }; + +private: + static std::mutex instanceLock_; + static sptr instance_; +}; + +} // namespace MiscServices +} // namespace OHOS +#endif // FM_IMMS_PROJECT_IPLATFORMCALLBACK_H diff --git a/services/include/input_method_system_ability.h b/services/include/input_method_system_ability.h index 1b31c24dcd71d54ef640d9d39401709f43e5baab..5793dfa8b0db70ccff82a3f25837074064478cb5 100644 --- a/services/include/input_method_system_ability.h +++ b/services/include/input_method_system_ability.h @@ -93,6 +93,7 @@ namespace MiscServices { static std::mutex instanceLock_; static sptr instance_; static std::shared_ptr serviceHandler_; + int32_t userId_; }; } } diff --git a/services/src/im_common_event_manager.cpp b/services/src/im_common_event_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9be6d8ef051ca25153cc0c291f4e15a76bbe524 --- /dev/null +++ b/services/src/im_common_event_manager.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2021 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. + */ + +#include +#include "im_common_event_manager.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "global.h" +#include "ipc_skeleton.h" +#include "message_handler.h" +#include "input_method_system_ability_stub.h" +#include "os_account_manager.h" + +namespace OHOS { +namespace MiscServices { + using namespace MessageID; + sptr ImCommonEventManager::instance_; + std::mutex ImCommonEventManager::instanceLock_; + + /*! Constructor + */ + ImCommonEventManager::ImCommonEventManager() + { + + } + + /*! Destructor + */ + ImCommonEventManager::~ImCommonEventManager() + { + + } + + sptr ImCommonEventManager::GetInstance() + { + if (instance_ == nullptr) { + std::lock_guard autoLock(instanceLock_); + if (instance_ == nullptr) { + IMSA_HILOGI("ImCommonEventManager::GetInstance instance_ is nullptr"); + instance_ = new ImCommonEventManager(); + } + } + return instance_; + } + + bool ImCommonEventManager::SubscribeEvent(const std::string &event) + { + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(event); + + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + + // std::shared_ptr subscriber = + // std::make_shared(subscriberInfo, event); + std::shared_ptr subscriber = + std::make_shared(subscriberInfo); + + if (subscriber == nullptr) { + IMSA_HILOGI("ImCommonEventManager::SubscribeEvent subscriber is nullptr"); + return false; + } + + if (!EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber)) { + IMSA_HILOGI("ImCommonEventManager::SubscribeEvent fail"); + return false; + } + + return true; + } + + bool ImCommonEventManager::UnsubscribeEvent() + { + return true; + } + + void ImCommonEventManager::EventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data) + { + auto want = data.GetWant(); + std::string action = want.GetAction(); + IMSA_HILOGI("ImCommonEventManager::EventSubscriber data.GetCode = %{public}u", data.GetCode()); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + // do something + IMSA_HILOGI("ImCommonEventManager::EventSubscriber user switched!!!"); + startUser(data.GetCode()); + } + } + + void ImCommonEventManager::EventSubscriber::startUser(int newUserId) + { + IMSA_HILOGI("ImCommonEventManager::startUser 1"); + + MessageParcel *parcel = new MessageParcel(); + parcel->WriteInt32(newUserId); + + IMSA_HILOGI("ImCommonEventManager::startUser 2"); + Message *msg = new Message(MessageID::MSG_ID_USER_START, parcel); + MessageHandler::Instance()->SendMessage(msg); + IMSA_HILOGI("ImCommonEventManager::startUser 3"); + } +} +} \ No newline at end of file diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index c7d71e517040d4264e8f7ed98979e875050097c2..cc5740e68f3f33e1bf3c6750a149d644c2ed0be0 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -28,6 +28,8 @@ #include "ability_connect_callback_proxy.h" #include "sa_mgr_client.h" #include "application_info.h" +#include "common_event_support.h" +#include "im_common_event_manager.h" namespace OHOS { namespace MiscServices { @@ -177,7 +179,13 @@ namespace MiscServices { userSettings.insert(std::pair(MAIN_USER_ID, setting)); userSessions.insert(std::pair(MAIN_USER_ID, session)); + userId_ = MAIN_USER_ID; setting->Initialize(); + + sptr imCommonEventManager = ImCommonEventManager::GetInstance(); + if (imCommonEventManager->SubscribeEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED)) { + IMSA_HILOGI("InputMethodSystemAbility::Initialize subscribe service event success"); + } } void InputMethodSystemAbility::StartInputService(std::string imeId) @@ -533,12 +541,15 @@ namespace MiscServices { */ int32_t InputMethodSystemAbility::OnUserStarted(const Message *msg) { - IMSA_HILOGI("Start...\n"); + IMSA_HILOGI("InputMethodSystemAbility::OnUserStarted Start...\n"); if (msg->msgContent_ == nullptr) { IMSA_HILOGE("Aborted! %s\n", ErrorCode::ToString(ErrorCode::ERROR_BAD_PARAMETERS)); return ErrorCode::ERROR_BAD_PARAMETERS; } int32_t userId = msg->msgContent_->ReadInt32(); + userId_ = userId; + IMSA_HILOGI("InputMethodSystemAbility::OnUserStarted userId = %{public}u", userId); + PerUserSetting *setting = GetUserSetting(userId); if (setting != nullptr) { IMSA_HILOGE("Aborted! %s %d\n", ErrorCode::ToString(ErrorCode::ERROR_USER_ALREADY_STARTED), userId); @@ -546,10 +557,15 @@ namespace MiscServices { } setting = new PerUserSetting(userId); + setting->Initialize(); PerUserSession *session = new PerUserSession(userId); userSettings.insert(std::pair(userId, setting)); userSessions.insert(std::pair(userId, session)); + std::string defaultIme = ParaHandle::GetDefaultIme(); + StartInputService(defaultIme); + + IMSA_HILOGI("InputMethodSystemAbility::OnUserStarted End...[%d]\n", userId); return ErrorCode::NO_ERROR; } @@ -672,13 +688,17 @@ namespace MiscServices { { MessageParcel *data = msg->msgContent_; int32_t userId = data->ReadInt32(); - PerUserSetting *setting = GetUserSetting(userId); + PerUserSetting *setting = GetUserSetting(MAIN_USER_ID); + if (setting == nullptr) { + IMSA_HILOGE("InputMethodSystemAbility::OnHandleMessage Aborted! setting is nullptr"); + } if (setting == nullptr || setting->GetUserState() != UserState::USER_STATE_UNLOCKED) { - IMSA_HILOGE("InputMethodSystemAbility::OnHandleMessage Aborted! userId = %{public}d", userId); + IMSA_HILOGE("InputMethodSystemAbility::OnHandleMessage Aborted! userId = %{public}d,", userId); + IMSA_HILOGE("InputMethodSystemAbility::OnHandleMessage Aborted! userState = %{public}d,", setting->GetUserState()); return ErrorCode::ERROR_USER_NOT_UNLOCKED; } - std::map::const_iterator it = msgHandlers.find(userId); + std::map::const_iterator it = msgHandlers.find(MAIN_USER_ID); if (it != msgHandlers.end()) { MessageHandler *handler = it->second; handler->SendMessage(msg); diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index ecb3afed292b89b0b96fa434af211428a1ed72be..a67aa9cec786e24a9eeef46cb076b244da4fc5e0 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -293,7 +293,7 @@ namespace MiscServices { */ int32_t InputMethodSystemAbilityStub::getUserId(int32_t uid) { - return uid/USER_ID_CHANGE_VALUE; + return uid / USER_ID_CHANGE_VALUE; } } } \ No newline at end of file