!104 新增公共事件监听

Merge pull request !104 from jinling/master
This commit is contained in:
openharmony_ci 2024-09-02 10:05:27 +00:00 committed by Gitee
commit 21c2c4b023
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 68 additions and 15 deletions

View File

@ -15,7 +15,8 @@
{ "name": "usual.event.USER_REMOVED" },
{ "name": "usual.event.CHARGING" },
{ "name": "usual.event.USER_UNLOCKED" },
{ "name": "COMMON_EVENT_RESTORE_START" }
{ "name": "COMMON_EVENT_RESTORE_START" },
{ "name": "USER_PIN_CREATED_EVENT" }
],
"timedevent": [
{

View File

@ -124,5 +124,8 @@ pub(crate) fn handle_common_event(reason: SystemAbilityOnDemandReason) {
listener::on_user_unlocked(reason.extra_data.code);
} else if reason_name == "loopevent" {
listener::on_schedule_wakeup();
} else if reason_name == "USER_PIN_CREATED_EVENT" {
logi!("[INFO]On user -{}- pin created.", reason.extra_data.code);
listener::on_user_unlocked(reason.extra_data.code);
}
}

View File

@ -30,7 +30,9 @@ using namespace OHOS::EventFwk;
const char * const APP_ID = "appId";
const char * const COMMON_EVENT_RESTORE_START = "COMMON_EVENT_RESTORE_START";
const char * const COMMON_EVENT_USER_PIN_CREATED = "USER_PIN_CREATED_EVENT";
const char * const BUNDLE_NAME = "bundleName";
const char * const PERMISSION_MANAGE_USER_IDM = "ohos.permission.MANAGE_USER_IDM";
void HandlePackageRemoved(const OHOS::AAFwk::Want &want, bool isSandBoxApp, OnPackageRemoved onPackageRemoved)
{
@ -53,6 +55,23 @@ void HandlePackageRemoved(const OHOS::AAFwk::Want &want, bool isSandBoxApp, OnPa
userId, appId.c_str(), appIndex);
}
void HandleAppRestore(const OHOS::AAFwk::Want &want, OnAppRestore onAppRestore)
{
if (onAppRestore != nullptr) {
int userId = want.GetIntParam(USER_ID, INVALID_USERID);
std::string bundleName = want.GetStringParam(BUNDLE_NAME);
int appIndex = want.GetIntParam(SANDBOX_APP_INDEX, -1);
if (appIndex == -1) {
LOGI("[INFO]Get app restore info failed, default as index 0.");
appIndex = 0;
}
onAppRestore(userId, reinterpret_cast<const uint8_t *>(bundleName.c_str()), appIndex);
LOGI("[INFO]Receive event: RESTORE_START.");
}
}
class SystemEventHandler : public CommonEventSubscriber {
public:
explicit SystemEventHandler(const CommonEventSubscribeInfo &subscribeInfo, const EventCallBack eventCallBack)
@ -84,20 +103,7 @@ public:
}
LOGI("[INFO]Receive event: CHARGING, start_time: %{public}ld", startTime);
} else if (action == COMMON_EVENT_RESTORE_START) {
if (this->eventCallBack.onAppRestore != nullptr) {
int userId = want.GetIntParam(USER_ID, INVALID_USERID);
std::string bundleName = want.GetStringParam(BUNDLE_NAME);
int appIndex = want.GetIntParam(SANDBOX_APP_INDEX, -1);
if (appIndex == -1) {
LOGI("[INFO]Get app restore info failed, default as index 0.");
appIndex = 0;
}
this->eventCallBack.onAppRestore(userId,
reinterpret_cast<const uint8_t *>(bundleName.c_str()), appIndex);
}
LOGI("[INFO]Receive event: RESTORE_START, start_time: %{public}ld", startTime);
HandleAppRestore(want, this->eventCallBack.onAppRestore);
} else if (action == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
if (this->eventCallBack.onUserUnlocked != nullptr) {
int userId = data.GetCode();
@ -105,6 +111,12 @@ public:
this->eventCallBack.onUserUnlocked(userId);
}
LOGI("[INFO]Receive event: USER_UNLOCKED, start_time: %{public}ld", startTime);
} else if (action == COMMON_EVENT_USER_PIN_CREATED) {
if (this->eventCallBack.onUserUnlocked != nullptr) {
int userId = data.GetCode();
this->eventCallBack.onUserUnlocked(userId);
}
LOGI("[INFO]Receive event: USER_PIN_CREATED_EVENT, start_time: %{public}ld", startTime);
} else {
LOGW("[WARNING]Receive unknown event: %{public}s", action.c_str());
}
@ -114,10 +126,44 @@ private:
};
std::shared_ptr<SystemEventHandler> g_eventHandler = nullptr;
std::shared_ptr<SystemEventHandler> g_pinEventHandler = nullptr;
bool SubscribePinEvent(const EventCallBack eventCallBack)
{
MatchingSkills matchingSkills;
matchingSkills.AddEvent(COMMON_EVENT_USER_PIN_CREATED);
CommonEventSubscribeInfo info(matchingSkills);
info.SetPermission(PERMISSION_MANAGE_USER_IDM);
if (g_pinEventHandler == nullptr) {
g_pinEventHandler = std::shared_ptr<SystemEventHandler>(
new (std::nothrow) SystemEventHandler(info, eventCallBack));
if (g_pinEventHandler == nullptr) {
LOGE("[FATAL]Asset pin event handler is nullptr.");
return false;
}
}
return CommonEventManager::SubscribeCommonEvent(g_pinEventHandler);
}
bool UnSubscribePinEvent(void)
{
if (g_pinEventHandler == nullptr) {
LOGW("Asset pin event handler is nullptr, no need to unsubscribe.");
return false;
}
bool res = CommonEventManager::UnSubscribeCommonEvent(g_pinEventHandler);
g_pinEventHandler = nullptr;
return res;
}
}
bool SubscribeSystemEvent(const EventCallBack eventCallBack)
{
bool ret = SubscribePinEvent(eventCallBack);
LOGI("Subscribe pin event result: %d", ret);
MatchingSkills matchingSkills;
matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
@ -141,6 +187,9 @@ bool SubscribeSystemEvent(const EventCallBack eventCallBack)
bool UnSubscribeSystemEvent(void)
{
bool ret = UnSubscribePinEvent();
LOGI("UnSubscribe pin event result: %d", ret);
if (g_eventHandler == nullptr) {
LOGW("Asset system event handler is nullptr, no need to unsubscribe.");
return false;