适配多用户

Signed-off-by: c00524366 <chenjiale5@hisilicon.com>
This commit is contained in:
c00524366
2022-02-15 11:24:17 +08:00
parent c9cc2ad15c
commit 5774215346
6 changed files with 199 additions and 7 deletions
+9 -2
View File
@@ -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 = [
"hiviewdfx_hilog_native:libhilog",
"ces_standard:cesfwk_core",
"ces_standard:cesfwk_innerkits",
"os_account_standard:os_account_innerkits",
]
subsystem_name = "miscservices"
part_name = "inputmethod_native"
@@ -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 <mutex>
#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<ImCommonEventManager> 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<ImCommonEventManager> instance_;
};
} // namespace MiscServices
} // namespace OHOS
#endif // FM_IMMS_PROJECT_IPLATFORMCALLBACK_H
@@ -93,6 +93,7 @@ namespace MiscServices {
static std::mutex instanceLock_;
static sptr<InputMethodSystemAbility> instance_;
static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
int32_t userId_;
};
}
}
+113
View File
@@ -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 <functional>
#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> ImCommonEventManager::instance_;
std::mutex ImCommonEventManager::instanceLock_;
/*! Constructor
*/
ImCommonEventManager::ImCommonEventManager()
{
}
/*! Destructor
*/
ImCommonEventManager::~ImCommonEventManager()
{
}
sptr<ImCommonEventManager> ImCommonEventManager::GetInstance()
{
if (instance_ == nullptr) {
std::lock_guard<std::mutex> 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<EventSubscriber> subscriber =
// std::make_shared<EventSubscriber>(subscriberInfo, event);
std::shared_ptr<EventSubscriber> subscriber =
std::make_shared<EventSubscriber>(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");
}
}
}
+24 -4
View File
@@ -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<int32_t, PerUserSetting*>(MAIN_USER_ID, setting));
userSessions.insert(std::pair<int32_t, PerUserSession*>(MAIN_USER_ID, session));
userId_ = MAIN_USER_ID;
setting->Initialize();
sptr<ImCommonEventManager> 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<int32_t, PerUserSetting*>(userId, setting));
userSessions.insert(std::pair<int32_t, PerUserSession*>(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<int32_t, MessageHandler*>::const_iterator it = msgHandlers.find(userId);
std::map<int32_t, MessageHandler*>::const_iterator it = msgHandlers.find(MAIN_USER_ID);
if (it != msgHandlers.end()) {
MessageHandler *handler = it->second;
handler->SendMessage(msg);
@@ -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;
}
}
}