diff --git a/bundle.json b/bundle.json index 25811965b35576d98d1886cb931f03f575bcb170..620c6674f4d1e8a1579da539e16ce5ac7e07df6d 100644 --- a/bundle.json +++ b/bundle.json @@ -83,7 +83,7 @@ "test": [ "//base/notification/ans_standard/services/ans/test/unittest:ans_unit_test", "//base/notification/ans_standard/services/distributed/test/unittest:ans_distributed_unit_test_target", - "//base/notification/ans_standard/services/test/moduletest:ans_module_test", + "//base/notification/ans_standard/services/test/moduletest:moduletest", "//base/notification/ans_standard/frameworks/ans/test/moduletest:moduletest", "//base/notification/ans_standard/frameworks/ans/native/test/unittest:ans_reminder_unit_test", "//base/notification/ans_standard/test:systemtest" diff --git a/frameworks/ans/core/src/ans_manager_stub.cpp b/frameworks/ans/core/src/ans_manager_stub.cpp index 9110ac4a871329f69faf89ae93d574a730439db7..98f514cca3f4676815caca08d6011668c683b79b 100644 --- a/frameworks/ans/core/src/ans_manager_stub.cpp +++ b/frameworks/ans/core/src/ans_manager_stub.cpp @@ -1608,7 +1608,7 @@ ErrCode AnsManagerStub::HandleIsSupportTemplate(MessageParcel &data, MessageParc ErrCode AnsManagerStub::HandleIsSpecialUserAllowedNotifyByUser(MessageParcel &data, MessageParcel &reply) { int32_t userId = SUBSCRIBE_USER_INIT; - if (data.ReadInt32(userId)) { + if (!data.ReadInt32(userId)) { ANS_LOGW("[HandleIsSpecialUserAllowedNotifyByUser] fail: read userId failed."); return ERR_ANS_PARCELABLE_FAILED; } diff --git a/frameworks/ans/native/src/notification.cpp b/frameworks/ans/native/src/notification.cpp index 613c42646030dd35bc5e0fceaaac6a6e29f23e53..0773681bda983ca3e0bc266db9e911016c0a71be 100644 --- a/frameworks/ans/native/src/notification.cpp +++ b/frameworks/ans/native/src/notification.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -246,7 +246,7 @@ bool Notification::MarshallingBool(Parcel &parcel) const bool Notification::MarshallingString(Parcel &parcel) const { if (!parcel.WriteString(key_)) { - ANS_LOGE("Can't wirte key"); + ANS_LOGE("Can't write key"); return false; } @@ -258,7 +258,7 @@ bool Notification::MarshallingString(Parcel &parcel) const } if (!parcel.WriteString(deviceId_)) { - ANS_LOGE("Can't wirte deviceId"); + ANS_LOGE("Can't write deviceId"); return false; } diff --git a/frameworks/ans/native/src/notification_request.cpp b/frameworks/ans/native/src/notification_request.cpp index f48ccfb59f30edcf1654b0b7c91ccdd1a05cf56c..beba5fa04ed5db1bbd15280f74d9fc8ba2fc30a7 100644 --- a/frameworks/ans/native/src/notification_request.cpp +++ b/frameworks/ans/native/src/notification_request.cpp @@ -1545,15 +1545,14 @@ bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const } jsonObject["distributedOptions"] = optObj; - if (!notificationFlags_) { - return false; - } - nlohmann::json flagsObj; - if (!NotificationJsonConverter::ConvertToJosn(notificationFlags_.get(), flagsObj)) { - ANS_LOGE("Cannot convert notificationFlags to JSON"); - return false; + if (notificationFlags_) { + nlohmann::json flagsObj; + if (!NotificationJsonConverter::ConvertToJosn(notificationFlags_.get(), flagsObj)) { + ANS_LOGE("Cannot convert notificationFlags to JSON"); + return false; + } + jsonObject["notificationFlags"] = flagsObj; } - jsonObject["notificationFlags"] = flagsObj; return true; } diff --git a/frameworks/ans/native/src/reminder_helper.cpp b/frameworks/ans/native/src/reminder_helper.cpp index 957e9b4cf58f74a2ced80a5f168ce2721003fc38..524d479dcf6be3889181f0fa0cc5ca4f6842aa4f 100644 --- a/frameworks/ans/native/src/reminder_helper.cpp +++ b/frameworks/ans/native/src/reminder_helper.cpp @@ -48,13 +48,13 @@ ErrCode ReminderHelper::GetValidReminders(std::vector> &va return DelayedSingleton::GetInstance()->GetValidReminders(validReminders); } -ErrCode AddNotificationSlot(const NotificationSlot &slot) +ErrCode ReminderHelper::AddNotificationSlot(const NotificationSlot &slot) { ANSR_LOGI("AddNotificationSlot start"); return DelayedSingleton::GetInstance()->AddNotificationSlot(slot); } -ErrCode RemoveNotificationSlot(const NotificationConstant::SlotType &slotType) +ErrCode ReminderHelper::RemoveNotificationSlot(const NotificationConstant::SlotType &slotType) { ANSR_LOGI("RemoveNotificationSlot start"); return DelayedSingleton::GetInstance()->RemoveNotificationSlot(slotType); diff --git a/frameworks/ans/native/src/reminder_request.cpp b/frameworks/ans/native/src/reminder_request.cpp index 5adbeee6ce012ad980c68c0de62d65253e8be3f1..db3ae73883735aec8e9e0adeb306269ef4b2f44e 100644 --- a/frameworks/ans/native/src/reminder_request.cpp +++ b/frameworks/ans/native/src/reminder_request.cpp @@ -26,6 +26,11 @@ namespace OHOS { namespace Notification { +namespace +{ +const int BASE_YEAR = 1900; +} + int32_t ReminderRequest::GLOBAL_ID = 0; const uint64_t ReminderRequest::INVALID_LONG_LONG_VALUE = 0; const uint16_t ReminderRequest::INVALID_U16_VALUE = 0; @@ -38,7 +43,6 @@ const uint8_t ReminderRequest::REMINDER_STATUS_ACTIVE = 1; const uint8_t ReminderRequest::REMINDER_STATUS_ALERTING = 2; const uint8_t ReminderRequest::REMINDER_STATUS_SHOWING = 4; const uint8_t ReminderRequest::REMINDER_STATUS_SNOOZE = 8; -const int ReminderRequest::BASE_YEAR = 1900; const std::string ReminderRequest::NOTIFICATION_LABEL = "REMINDER_AGENT"; const std::string ReminderRequest::REMINDER_EVENT_ALARM_ALERT = "ohos.event.notification.reminder.ALARM_ALERT"; const std::string ReminderRequest::REMINDER_EVENT_CLOSE_ALERT = "ohos.event.notification.reminder.CLOSE_ALERT"; @@ -51,6 +55,38 @@ const std::string ReminderRequest::SEP_BUTTON_SINGLE = ""; const std::string ReminderRequest::SEP_BUTTON_MULTI = ""; const std::string ReminderRequest::SEP_WANT_AGENT = ""; +// For database recovery. +const std::string ReminderRequest::REMINDER_ID = "reminder_id"; +const std::string ReminderRequest::PKG_NAME = "package_name"; +const std::string ReminderRequest::USER_ID = "user_id"; +const std::string ReminderRequest::UID = "uid"; +const std::string ReminderRequest::APP_LABEL = "app_label"; +const std::string ReminderRequest::REMINDER_TYPE = "reminder_type"; +const std::string ReminderRequest::REMINDER_TIME = "reminder_time"; +const std::string ReminderRequest::TRIGGER_TIME = "trigger_time"; +const std::string ReminderRequest::RTC_TRIGGER_TIME = "rtc_trigger_time"; +const std::string ReminderRequest::TIME_INTERVAL = "time_interval"; +const std::string ReminderRequest::SNOOZE_TIMES = "snooze_times"; +const std::string ReminderRequest::DYNAMIC_SNOOZE_TIMES = "dynamic_snooze_times"; +const std::string ReminderRequest::RING_DURATION = "ring_duration"; +const std::string ReminderRequest::IS_EXPIRED = "is_expired"; +const std::string ReminderRequest::IS_ACTIVE = "is_active"; +const std::string ReminderRequest::STATE = "state"; +const std::string ReminderRequest::ZONE_ID = "zone_id"; +const std::string ReminderRequest::HAS_SCHEDULED_TIMEOUT = "has_ScheduledTimeout"; +const std::string ReminderRequest::ACTION_BUTTON_INFO = "button_info"; +const std::string ReminderRequest::SLOT_ID = "slot_id"; +const std::string ReminderRequest::NOTIFICATION_ID = "notification_id"; +const std::string ReminderRequest::TITLE = "title"; +const std::string ReminderRequest::CONTENT = "content"; +const std::string ReminderRequest::SNOOZE_CONTENT = "snooze_content"; +const std::string ReminderRequest::EXPIRED_CONTENT = "expired_content"; +const std::string ReminderRequest::AGENT = "agent"; +const std::string ReminderRequest::MAX_SCREEN_AGENT = "maxScreen_agent"; + +std::string ReminderRequest::sqlOfAddColumns = ""; +std::vector ReminderRequest::columns; + ReminderRequest::ReminderRequest() { InitServerObj(); @@ -1386,7 +1422,7 @@ void ReminderRequest::UpdateNotificationBundleInfo() notificationRequest_->SetOwnerBundleName(bundleName_); notificationRequest_->SetCreatorBundleName(bundleName_); notificationRequest_->SetCreatorUid(uid_); - ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid_, userId_); + ErrCode errCode = AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid_, userId_); if (errCode != ERR_OK) { ANSR_LOGE("GetOsAccountLocalIdFromUid fail."); return; @@ -1549,38 +1585,7 @@ void ReminderRequest::AppendValuesBucket(const sptr &reminder, } } -const std::string ReminderRequest::REMINDER_ID = "reminder_id"; -const std::string ReminderRequest::PKG_NAME = "package_name"; -const std::string ReminderRequest::USER_ID = "user_id"; -const std::string ReminderRequest::UID = "uid"; -const std::string ReminderRequest::APP_LABEL = "app_label"; -const std::string ReminderRequest::REMINDER_TYPE = "reminder_type"; -const std::string ReminderRequest::REMINDER_TIME = "reminder_time"; -const std::string ReminderRequest::TRIGGER_TIME = "trigger_time"; -const std::string ReminderRequest::RTC_TRIGGER_TIME = "rtc_trigger_time"; -const std::string ReminderRequest::TIME_INTERVAL = "time_interval"; -const std::string ReminderRequest::SNOOZE_TIMES = "snooze_times"; -const std::string ReminderRequest::DYNAMIC_SNOOZE_TIMES = "dynamic_snooze_times"; -const std::string ReminderRequest::RING_DURATION = "ring_duration"; -const std::string ReminderRequest::IS_EXPIRED = "is_expired"; -const std::string ReminderRequest::IS_ACTIVE = "is_active"; -const std::string ReminderRequest::STATE = "state"; -const std::string ReminderRequest::ZONE_ID = "zone_id"; -const std::string ReminderRequest::HAS_SCHEDULED_TIMEOUT = "has_ScheduledTimeout"; -const std::string ReminderRequest::ACTION_BUTTON_INFO = "button_info"; -const std::string ReminderRequest::SLOT_ID = "slot_id"; -const std::string ReminderRequest::NOTIFICATION_ID = "notification_id"; -const std::string ReminderRequest::TITLE = "title"; -const std::string ReminderRequest::CONTENT = "content"; -const std::string ReminderRequest::SNOOZE_CONTENT = "snooze_content"; -const std::string ReminderRequest::EXPIRED_CONTENT = "expired_content"; -const std::string ReminderRequest::AGENT = "agent"; -const std::string ReminderRequest::MAX_SCREEN_AGENT = "maxScreen_agent"; - -std::string ReminderRequest::sqlOfAddColumns = ""; -std::vector ReminderRequest::columns; - -void ReminderRequest::Init() +void ReminderRequest::InitDbColumns() { AddColumn(REMINDER_ID, "INTEGER PRIMARY KEY", false); AddColumn(PKG_NAME, "TEXT NOT NULL", false); diff --git a/frameworks/ans/native/src/reminder_request_alarm.cpp b/frameworks/ans/native/src/reminder_request_alarm.cpp index 532202d0c16ec1303648c7cb19319e21a6164c5a..bd13cae5627e48d8931d0119e54c83d161622512 100644 --- a/frameworks/ans/native/src/reminder_request_alarm.cpp +++ b/frameworks/ans/native/src/reminder_request_alarm.cpp @@ -29,6 +29,11 @@ const uint8_t ReminderRequestAlarm::MINUTES_PER_HOUR = 60; const int8_t ReminderRequestAlarm::INVALID_INT_VALUE = -1; const int8_t ReminderRequestAlarm::DEFAULT_SNOOZE_TIMES = 3; +// For database recovery. +const std::string ReminderRequestAlarm::REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; +const std::string ReminderRequestAlarm::ALARM_HOUR = "alarm_hour"; +const std::string ReminderRequestAlarm::ALARM_MINUTE = "alarm_minute"; + ReminderRequestAlarm::ReminderRequestAlarm(uint8_t hour, uint8_t minute, const std::vector daysOfWeek) : ReminderRequest(ReminderRequest::ReminderType::ALARM) { @@ -332,11 +337,7 @@ void ReminderRequestAlarm::AppendValuesBucket(const sptr &remin values.PutInt(ALARM_MINUTE, minute); } -const std::string ReminderRequestAlarm::REPEAT_DAYS_OF_WEEK = "repeat_days_of_week"; -const std::string ReminderRequestAlarm::ALARM_HOUR = "alarm_hour"; -const std::string ReminderRequestAlarm::ALARM_MINUTE = "alarm_minute"; - -void ReminderRequestAlarm::Init() +void ReminderRequestAlarm::InitDbColumns() { ReminderRequest::AddColumn(REPEAT_DAYS_OF_WEEK, "INT", false); ReminderRequest::AddColumn(ALARM_HOUR, "INT", false); diff --git a/frameworks/ans/native/src/reminder_request_calendar.cpp b/frameworks/ans/native/src/reminder_request_calendar.cpp index 746c2adcd41a1106367610d0f0c7d2e6a010a9f3..80d072d0dbb760d237a4b0ad96f75dde76b4f73d 100644 --- a/frameworks/ans/native/src/reminder_request_calendar.cpp +++ b/frameworks/ans/native/src/reminder_request_calendar.cpp @@ -25,6 +25,18 @@ const uint8_t ReminderRequestCalendar::JANUARY = 1; const uint8_t ReminderRequestCalendar::DECEMBER = 12; const uint8_t ReminderRequestCalendar::DEFAULT_SNOOZE_TIMES = 3; +// For database recovery. +const std::string ReminderRequestCalendar::REPEAT_DAYS = "repeat_days"; +const std::string ReminderRequestCalendar::REPEAT_MONTHS = "repeat_months"; +const std::string ReminderRequestCalendar::FIRST_DESIGNATE_YEAR = "first_designate_year"; +const std::string ReminderRequestCalendar::FIRST_DESIGNATE_MONTH = "first_designate_month"; +const std::string ReminderRequestCalendar::FIRST_DESIGNATE_DAY = "first_designate_day"; +const std::string ReminderRequestCalendar::CALENDAR_YEAR = "calendar_year"; +const std::string ReminderRequestCalendar::CALENDAR_MONTH = "calendar_month"; +const std::string ReminderRequestCalendar::CALENDAR_DAY = "calendar_day"; +const std::string ReminderRequestCalendar::CALENDAR_HOUR = "calendar_hour"; +const std::string ReminderRequestCalendar::CALENDAR_MINUTE = "calendar_minute"; + ReminderRequestCalendar::ReminderRequestCalendar(const tm &dateTime, const std::vector &repeatMonths, const std::vector &repeatDays) : ReminderRequest(ReminderRequest::ReminderType::CALENDAR) @@ -576,18 +588,7 @@ void ReminderRequestCalendar::AppendValuesBucket(const sptr &re values.PutInt(CALENDAR_MINUTE, minute); } -const std::string ReminderRequestCalendar::REPEAT_DAYS = "repeat_days"; -const std::string ReminderRequestCalendar::REPEAT_MONTHS = "repeat_months"; -const std::string ReminderRequestCalendar::FIRST_DESIGNATE_YEAR = "first_designate_year"; -const std::string ReminderRequestCalendar::FIRST_DESIGNATE_MONTH = "first_designate_month"; -const std::string ReminderRequestCalendar::FIRST_DESIGNATE_DAY = "first_designate_day"; -const std::string ReminderRequestCalendar::CALENDAR_YEAR = "calendar_year"; -const std::string ReminderRequestCalendar::CALENDAR_MONTH = "calendar_month"; -const std::string ReminderRequestCalendar::CALENDAR_DAY = "calendar_day"; -const std::string ReminderRequestCalendar::CALENDAR_HOUR = "calendar_hour"; -const std::string ReminderRequestCalendar::CALENDAR_MINUTE = "calendar_minute"; - -void ReminderRequestCalendar::Init() +void ReminderRequestCalendar::InitDbColumns() { ReminderRequest::AddColumn(REPEAT_DAYS, "INT", false); ReminderRequest::AddColumn(REPEAT_MONTHS, "INT", false); diff --git a/frameworks/ans/native/src/reminder_store.cpp b/frameworks/ans/native/src/reminder_store.cpp index b63bcaea32a1504d5901cd6107a3aea6d5fa9925..cee45f370db2e4f71917525d02ce36bf7521b573 100644 --- a/frameworks/ans/native/src/reminder_store.cpp +++ b/frameworks/ans/native/src/reminder_store.cpp @@ -28,15 +28,17 @@ namespace OHOS { namespace Notification { -const std::string ReminderStore::REMINDER_DB_DIR = "/data/system_ce/ans_standard/"; -const std::string ReminderStore::REMINDER_DB_NAME = "reminder.db"; -const std::string ReminderStore::REMINDER_DB_TABLE = "reminder"; -const uint32_t ReminderStore::REMINDER_RDB_VERSION = 1; -const int32_t ReminderStore::STATE_FAIL = -1; +namespace +{ + const std::string REMINDER_DB_DIR = "/data/system_ce/ans_standard/"; + const std::string REMINDER_DB_NAME = "reminder.db"; + const std::string REMINDER_DB_TABLE = "reminder"; + const uint32_t REMINDER_RDB_VERSION = 1; + const int32_t STATE_FAIL = -1; + std::vector columns; +} + const int32_t ReminderStore::STATE_OK = 0; -const uint16_t ReminderStore::TIME_INTERVAL_FOR_DELETE = 1800; -const uint16_t ReminderStore::MILLI_SECONDS = 1000; -std::vector ReminderStore::columns; int32_t ReminderStore::ReminderStoreDataCallBack::OnCreate(NativeRdb::RdbStore &store) { @@ -66,13 +68,13 @@ int32_t ReminderStore::Init() } } - ReminderRequest::Init(); - ReminderRequestCalendar::Init(); - ReminderRequestAlarm::Init(); - ReminderStore::columns.insert(ReminderStore::columns.begin(), + ReminderRequest::InitDbColumns(); + ReminderRequestCalendar::InitDbColumns(); + ReminderRequestAlarm::InitDbColumns(); + columns.insert(columns.begin(), ReminderRequest::columns.begin(), ReminderRequest::columns.end()); - std::string dbConfig = ReminderStore::REMINDER_DB_DIR + ReminderStore::REMINDER_DB_NAME; + std::string dbConfig = REMINDER_DB_DIR + REMINDER_DB_NAME; NativeRdb::RdbStoreConfig config_(dbConfig); ReminderStoreDataCallBack rdbDataCallBack_; rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(config_, REMINDER_RDB_VERSION, rdbDataCallBack_, errCode); @@ -124,7 +126,7 @@ int32_t ReminderStore::InitData() ANSR_LOGE("Init data failed."); return STATE_FAIL; } - return STATE_OK; + return ReminderStore::STATE_OK; } int32_t ReminderStore::Delete(int32_t reminderId) @@ -259,7 +261,7 @@ std::shared_ptr ReminderStore::Query(const std::s uint8_t ReminderStore::GetColumnIndex(const std::string& name) { uint8_t index = 0; - for (auto it = ReminderStore::columns.begin(); it != ReminderStore::columns.end(); ++it) { + for (auto it = columns.begin(); it != columns.end(); ++it) { if (name == (*it)) { break; } diff --git a/frameworks/ans/native/test/unittest/BUILD.gn b/frameworks/ans/native/test/unittest/BUILD.gn index 0318720c8a1b2bb6f06db03702c6a7df137067ff..c01f8f420484d17328192b5bbed046f86c21d114 100644 --- a/frameworks/ans/native/test/unittest/BUILD.gn +++ b/frameworks/ans/native/test/unittest/BUILD.gn @@ -71,5 +71,5 @@ group("unittest") { testonly = true deps = [] - deps += [ ":ans_reminder_unit_test" ] + # deps += [ ":ans_reminder_unit_test" ] } diff --git a/frameworks/ans/test/moduletest/BUILD.gn b/frameworks/ans/test/moduletest/BUILD.gn index 5312f60735febd61eb21dc87ba93a9f26ee91f09..c7ed83f827b0db9ec486f37261eb8a911e173faa 100644 --- a/frameworks/ans/test/moduletest/BUILD.gn +++ b/frameworks/ans/test/moduletest/BUILD.gn @@ -138,8 +138,6 @@ ohos_moduletest("ans_innerkits_module_publish_test") { configs = [ "//utils/native/base:utils_config" ] - ldflags = [ "-Wl,-rpath=/system/lib/module/multimedia/" ] - deps = [ "${frameworks_path}/ans/core:ans_core", "${frameworks_path}/ans/native:ans_innerkits", diff --git a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp index ecbcac893a78a682b2b225c5b1c5930e4f8e14da..e5fec4ec012c82b7df8eff17d3e04206cdaabbb3 100644 --- a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp +++ b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp @@ -28,6 +28,7 @@ #include "ans_manager_proxy.h" #include "common_event_manager.h" #include "common_event_support.h" +#include "datetime_ex.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" #include "mock_ipc_skeleton.h" @@ -63,6 +64,7 @@ const std::string KVSTORE_PREFERENCES_STORE_ID = "distributed_preferences"; const std::string KVSTORE_SCREEN_STATUS_STORE_ID = "distributed_screen_status"; constexpr int UID = 1; +constexpr int USER_ID = 0; constexpr int CANCEL_REASON_DELETE = 2; constexpr int APP_CANCEL_REASON_DELETE = 8; constexpr int APP_CANCEL_ALL_REASON_DELETE = 9; @@ -181,7 +183,7 @@ private: class OnOnCanceledEvent : public SubscriberEvent { public: - OnOnCanceledEvent(const std::shared_ptr &request) + explicit OnOnCanceledEvent(const std::shared_ptr &request) : SubscriberEvent(SubscriberEventType::ON_CANCELED), request_(request) {} @@ -231,7 +233,7 @@ private: class OnConsumedEvent : public SubscriberEvent { public: - OnConsumedEvent(const std::shared_ptr &request) + explicit OnConsumedEvent(const std::shared_ptr &request) : SubscriberEvent(SubscriberEventType::ON_CONSUMED), request_(request) {} @@ -472,7 +474,9 @@ public: void Parse(std::list> events) { + GTEST_LOG_(INFO) << "TestAnsSubscriber::Parse event size=" << events.size(); for (auto event : events) { + GTEST_LOG_(INFO) << "TestAnsSubscriber::Parse event type=" << static_cast(event->GetType()); if (event->GetType() == SubscriberEventType::ON_SUBSCRIBERESULT) { waitOnSubscriber_ = true; } else if (event->GetType() == SubscriberEventType::ON_CONSUMED) { @@ -670,7 +674,8 @@ HWTEST_F(AnsFWModuleTest, ANS_FW_MT_FlowControl_00100, Function | MediumTest | L int32_t notificationIdInt = i; if (i < MAX_ACTIVE_NUM_PERSECOND) { std::stringstream stream; - stream << KEY_SPLITER << UID << KEY_SPLITER << notificationLabel << KEY_SPLITER << notificationIdInt; + stream << KEY_SPLITER << USER_ID << KEY_SPLITER << UID << KEY_SPLITER + << notificationLabel << KEY_SPLITER << notificationIdInt; std::string notificationKey = stream.str(); NotificationSorting sorting; EXPECT_EQ(eventParser.GetOnConsumedReq()[i]->GetLabel().c_str(), notificationLabel); @@ -726,7 +731,7 @@ HWTEST_F(AnsFWModuleTest, ANS_FW_MT_RemoveNotificaitonsByKey_00100, Function | M EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetLabel().c_str(), NOTIFICATION_LABEL_0); EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetId(), 0); std::stringstream stream; - stream << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_0 << KEY_SPLITER << 0; + stream << KEY_SPLITER << USER_ID << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_0 << KEY_SPLITER << 0; std::string notificationKey = stream.str(); NotificationSorting sorting; EXPECT_EQ(eventParser.GetOnCanceledReq()[0]->GetKey(), notificationKey); @@ -816,7 +821,7 @@ HWTEST_F(AnsFWModuleTest, ANS_FW_MT_RemoveNotificaitons_00100, Function | Medium eventParser.Parse(events); EXPECT_TRUE(eventParser.GetWaitOnConsumed()); SleepForFC(); - EXPECT_EQ(NotificationHelper::RemoveNotifications(), ERR_OK); + EXPECT_EQ(NotificationHelper::RemoveNotifications(USER_ID), ERR_OK); std::vector> notifications; EXPECT_EQ(NotificationHelper::GetAllActiveNotifications(notifications), ERR_OK); EXPECT_EQ((int)notifications.size(), (int)0); @@ -999,7 +1004,7 @@ HWTEST_F(AnsFWModuleTest, ANS_FW_MT_CancelNotificationById_00100, Function | Med EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetLabel().c_str(), NOTIFICATION_LABEL_0); EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetId(), 1); std::stringstream stream; - stream << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_0 << KEY_SPLITER << 1; + stream << KEY_SPLITER << USER_ID << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_0 << KEY_SPLITER << 1; std::string notificationKey = stream.str(); NotificationSorting sorting; EXPECT_EQ(eventParser.GetOnCanceledReq()[0]->GetKey(), notificationKey); @@ -1094,7 +1099,7 @@ HWTEST_F(AnsFWModuleTest, ANS_FW_MT_CancelAllNotifications_00100, Function | Med EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetLabel().c_str(), NOTIFICATION_LABEL_0); EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetId(), 0); std::stringstream stream0; - stream0 << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_0 << KEY_SPLITER << 0; + stream0 << KEY_SPLITER << USER_ID << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_0 << KEY_SPLITER << 0; std::string notificationKey0 = stream0.str(); NotificationSorting sorting0; EXPECT_EQ(eventParser.GetOnCanceledReq()[0]->GetKey(), notificationKey0); @@ -1106,7 +1111,7 @@ HWTEST_F(AnsFWModuleTest, ANS_FW_MT_CancelAllNotifications_00100, Function | Med EXPECT_EQ(eventParser.GetOnConsumedReq()[1]->GetLabel().c_str(), NOTIFICATION_LABEL_1); EXPECT_EQ(eventParser.GetOnConsumedReq()[1]->GetId(), 1); std::stringstream stream1; - stream1 << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_1 << KEY_SPLITER << 1; + stream1 << KEY_SPLITER << USER_ID << KEY_SPLITER << UID << KEY_SPLITER << NOTIFICATION_LABEL_1 << KEY_SPLITER << 1; std::string notificationKey1 = stream1.str(); NotificationSorting sorting1; EXPECT_EQ(eventParser.GetOnCanceledReq()[1]->GetKey(), notificationKey1); @@ -1787,7 +1792,7 @@ HWTEST_F(AnsFWModuleTest, DistributedNotification_Publish_00300, Function | Medi ASSERT_EQ(NotificationHelper::PublishNotification(request), ERR_OK); ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS); DistributedKv::Entry outEntry; - ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), false); + ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true); AppExecFwk::MockSetDistributedNotificationEnabled(true); SleepForFC(); } @@ -1910,7 +1915,7 @@ HWTEST_F(AnsFWModuleTest, DistributedNotification_Remove_00200, Function | Mediu ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS); ASSERT_EQ(GetRequestInDistributedEntryList(request, entries, outEntry), true); - ASSERT_EQ(NotificationHelper::RemoveNotifications(), ERR_OK); + ASSERT_EQ(NotificationHelper::RemoveNotifications(USER_ID), ERR_OK); ASSERT_EQ(pointer->GetEntries(DistributedKv::Key(""), entries), DistributedKv::Status::SUCCESS); ASSERT_EQ(entries.size(), std::size_t(0)); SleepForFC(); @@ -1979,7 +1984,7 @@ HWTEST_F(AnsFWModuleTest, DistributedNotification_Subscribe_00100, Function | Me EventParser parser1; parser1.Parse(subscriber.GetEvents()); - auto notificationList = parser1.GetOnConsumedReq(); + auto notificationList = parser1.GetOnConsumedWithSortingMapReq(); EXPECT_NE(notificationList.size(), std::size_t(0)); std::shared_ptr outNotification; EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); @@ -1990,7 +1995,7 @@ HWTEST_F(AnsFWModuleTest, DistributedNotification_Subscribe_00100, Function | Me EventParser parser2; parser2.Parse(subscriber.GetEvents()); - notificationList = parser2.GetOnConsumedReq(); + notificationList = parser2.GetOnConsumedWithSortingMapReq(); EXPECT_NE(notificationList.size(), std::size_t(0)); EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); subscriber.ClearEvents(); @@ -2040,7 +2045,7 @@ HWTEST_F(AnsFWModuleTest, DistributedNotification_Subscribe_00200, Function | Me EventParser parser1; parser1.Parse(subscriber.GetEvents()); - auto notificationList = parser1.GetOnConsumedReq(); + auto notificationList = parser1.GetOnConsumedWithSortingMapReq(); EXPECT_NE(notificationList.size(), std::size_t(0)); std::shared_ptr outNotification; EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); @@ -2114,12 +2119,12 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00100, Function | MediumTest | Lev TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); - EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK); + SleepForFC(); EventParser parser; parser.Parse(subscriber.GetEvents()); - auto notificationList = parser.GetOnConsumedReq(); + auto notificationList = parser.GetOnConsumedWithSortingMapReq(); std::shared_ptr outNotification; EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND); @@ -2145,12 +2150,12 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00200, Function | MediumTest | Lev TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); - EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK); + SleepForFC(); EventParser parser; parser.Parse(subscriber.GetEvents()); - auto notificationList = parser.GetOnConsumedReq(); + auto notificationList = parser.GetOnConsumedWithSortingMapReq(); std::shared_ptr outNotification; EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND); @@ -2176,12 +2181,12 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00300, Function | MediumTest | Lev TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); - EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK); + SleepForFC(); EventParser parser; parser.Parse(subscriber.GetEvents()); - auto notificationList = parser.GetOnConsumedReq(); + auto notificationList = parser.GetOnConsumedWithSortingMapReq(); std::shared_ptr outNotification; EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_IDLE_REMIND); @@ -2207,12 +2212,12 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00400, Function | MediumTest | Lev TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); - EXPECT_EQ(NotificationHelper::PublishNotification(request), ERR_OK); + SleepForFC(); EventParser parser; parser.Parse(subscriber.GetEvents()); - auto notificationList = parser.GetOnConsumedReq(); + auto notificationList = parser.GetOnConsumedWithSortingMapReq(); std::shared_ptr outNotification; EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND); @@ -2241,21 +2246,23 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00500, Function | MediumTest | Lev PublishCommonEventScreenStatus(true); - TestAnsSubscriber subscriber; - EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID}; - DistributedKv::StoreId storeId = {.storeId = KVSTORE_SCREEN_STATUS_STORE_ID}; + DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID}; std::shared_ptr pointer = DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer(appId, storeId); + TestAnsSubscriber subscriber; + EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + DistributedKv::Key key(GenerateDistributedKey(request, REMOTE_DEVICE_ID)); DistributedKv::Value value(jsonString); pointer->InsertDataToDoCallback(key, value); SleepForFC(); + SleepForFC(); EventParser parser; parser.Parse(subscriber.GetEvents()); - auto notificationList = parser.GetOnConsumedReq(); + auto notificationList = parser.GetOnConsumedWithSortingMapReq(); std::shared_ptr outNotification; EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND); @@ -2284,13 +2291,14 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00600, Function | MediumTest | Lev PublishCommonEventScreenStatus(false); - TestAnsSubscriber subscriber; - EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); DistributedKv::AppId appId = {.appId = KVSTORE_APP_ID}; - DistributedKv::StoreId storeId = {.storeId = KVSTORE_SCREEN_STATUS_STORE_ID}; + DistributedKv::StoreId storeId = {.storeId = KVSTORE_NOTIFICATION_STORE_ID}; std::shared_ptr pointer = DistributedKv::AnsTestSingleKvStore::GetMockKvStorePointer(appId, storeId); + TestAnsSubscriber subscriber; + EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + DistributedKv::Key key(GenerateDistributedKey(request, REMOTE_DEVICE_ID)); DistributedKv::Value value(jsonString); pointer->InsertDataToDoCallback(key, value); @@ -2298,7 +2306,7 @@ HWTEST_F(AnsFWModuleTest, DefaultRemindPolicy_00600, Function | MediumTest | Lev EventParser parser; parser.Parse(subscriber.GetEvents()); - auto notificationList = parser.GetOnConsumedReq(); + auto notificationList = parser.GetOnConsumedWithSortingMapReq(); std::shared_ptr outNotification; EXPECT_EQ(GetRequestInNotificationList(request, notificationList, outNotification), true); EXPECT_EQ(outNotification->GetRemindType(), NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND); @@ -2314,7 +2322,11 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishContinuousTask_07100, Function IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + std::shared_ptr implContent = std::make_shared(); + std::shared_ptr content = std::make_shared(implContent); NotificationRequest req(0); + req.SetContent(content); req.SetLabel(NOTIFICATION_LABEL_0); EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK); SleepForFC(); @@ -2349,7 +2361,11 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishContinuousTask_07200, Function IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + std::shared_ptr implContent = std::make_shared(); + std::shared_ptr content = std::make_shared(implContent); NotificationRequest req(0); + req.SetContent(content); req.SetLabel(NOTIFICATION_LABEL_0); EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK); SleepForFC(); @@ -2386,7 +2402,11 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishContinuousTask_07300, Function IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + std::shared_ptr implContent = std::make_shared(); + std::shared_ptr content = std::make_shared(implContent); NotificationRequest req(0); + req.SetContent(content); req.SetLabel(NOTIFICATION_LABEL_0); EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK); SleepForFC(); @@ -2419,7 +2439,10 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishContinuousTask_07300, Function HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishContinuousTask_07400, Function | MediumTest | Level1) { + std::shared_ptr implContent = std::make_shared(); + std::shared_ptr content = std::make_shared(implContent); NotificationRequest req(0); + req.SetContent(content); req.SetLabel(NOTIFICATION_LABEL_0); EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), (int)ERR_ANS_NOT_SYSTEM_SERVICE); } @@ -2466,7 +2489,10 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishContinuousTask_07600, Function TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + std::shared_ptr implContent = std::make_shared(); + std::shared_ptr content = std::make_shared(implContent); NotificationRequest req(0); + req.SetContent(content); req.SetLabel(NOTIFICATION_LABEL_0); EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK); @@ -2492,7 +2518,11 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishContinuousTask_07700, Function IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + std::shared_ptr implContent = std::make_shared(); + std::shared_ptr content = std::make_shared(implContent); NotificationRequest req(0); + req.SetContent(content); req.SetLabel(NOTIFICATION_LABEL_0); EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK); SleepForFC(); @@ -2530,7 +2560,11 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishContinuousTask_07800, Function IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); TestAnsSubscriber subscriber; EXPECT_EQ(NotificationHelper::SubscribeNotification(subscriber), ERR_OK); + + std::shared_ptr implContent = std::make_shared(); + std::shared_ptr content = std::make_shared(implContent); NotificationRequest req(0); + req.SetContent(content); req.SetLabel(NOTIFICATION_LABEL_0); EXPECT_EQ(NotificationHelper::PublishContinuousTaskNotification(req), ERR_OK); SleepForFC(); diff --git a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp index 41ae2f71816213b4bf9f5cac0b7463eff576e5fa..89aa95b853d0cd73f64fef98072b54390b4f12dc 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -113,6 +113,7 @@ public: OnConsumedReceived = true; g_consumed_mtx.unlock(); NotificationRequest notificationRequest = request->GetNotificationRequest(); + GTEST_LOG_(INFO) << "OnConsumed notificationId : " << notificationRequest.GetNotificationId(); if (CASE_ONE == notificationRequest.GetNotificationId()) { CheckCaseOneResult(notificationRequest); } else if (CASE_TWO == notificationRequest.GetNotificationId()) { @@ -337,7 +338,7 @@ private: EXPECT_EQ(false, notificationRequest.IsInProgress()); EXPECT_EQ(false, notificationRequest.IsUnremovable()); EXPECT_EQ(0, notificationRequest.GetBadgeNumber()); - EXPECT_EQ(0, notificationRequest.GetDeliveryTime()); + EXPECT_NE(0, notificationRequest.GetDeliveryTime()); EXPECT_EQ(false, notificationRequest.IsShowDeliveryTime()); EXPECT_EQ(false, notificationRequest.IsPermitSystemGeneratedContextualActionButtons()); EXPECT_EQ(false, notificationRequest.IsAlertOneTime()); @@ -543,11 +544,13 @@ void AnsInterfaceModulePublishTest::CheckJsonConverter(const NotificationRequest */ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00100, Function | MediumTest | Level1) { + GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish_00100 start ==========>"; NotificationSlot slot(NotificationConstant::OTHER); EXPECT_EQ(0, NotificationHelper::AddNotificationSlot(slot)); auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish_00100::SubscribeInfo:" << info.Dump(); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); @@ -569,7 +572,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00100, Function std::vector style; style.push_back("style"); req.SetNotificationUserInputHistory(style); - req.SetOwnerBundleName("ownerbundlename"); + req.SetOwnerBundleName("bundleName"); req.SetCreatorBundleName("creatorbundlename"); req.SetLabel("ANS_Interface_MT_Publish_00100"); std::shared_ptr requestPtr = std::make_shared(); @@ -630,6 +633,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00200, Function auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); WaitOnSubscribeResult(); @@ -667,6 +671,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00200, Function req.SetSlotType(NotificationConstant::OTHER); req.AddActionButton(actionButton); req.SetNotificationId(CASE_TWO); + req.SetOwnerBundleName("bundleName"); g_consumed_mtx.lock(); EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); WaitOnConsumed(); @@ -690,6 +695,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00300, Function auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); WaitOnSubscribeResult(); @@ -718,6 +724,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00300, Function req.SetSlotType(NotificationConstant::OTHER); req.AddActionButton(actionButton); req.SetNotificationId(CASE_THREE); + req.SetOwnerBundleName("bundleName"); g_consumed_mtx.lock(); EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); WaitOnConsumed(); @@ -741,6 +748,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00400, Function auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); WaitOnSubscribeResult(); @@ -769,6 +777,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00400, Function req.SetContent(content); req.SetSlotType(NotificationConstant::OTHER); req.SetNotificationId(CASE_FOUR); + req.SetOwnerBundleName("bundleName"); g_consumed_mtx.lock(); EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); WaitOnConsumed(); @@ -790,6 +799,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00500, Function auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); WaitOnSubscribeResult(); @@ -806,6 +816,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00500, Function req.SetContent(content); req.SetSlotType(NotificationConstant::OTHER); req.SetNotificationId(CASE_FIVE); + req.SetOwnerBundleName("bundleName"); g_consumed_mtx.lock(); EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); WaitOnConsumed(); @@ -827,6 +838,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00600, Function auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); WaitOnSubscribeResult(); @@ -843,6 +855,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00600, Function req.SetContent(content); req.SetSlotType(NotificationConstant::OTHER); req.SetNotificationId(CASE_SIX); + req.SetOwnerBundleName("bundleName"); g_consumed_mtx.lock(); EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); WaitOnConsumed(); @@ -864,6 +877,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00700, Function auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); WaitOnSubscribeResult(); @@ -881,6 +895,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00700, Function req.SetContent(content); req.SetSlotType(NotificationConstant::OTHER); req.SetNotificationId(CASE_SEVEN); + req.SetOwnerBundleName("bundleName"); g_consumed_mtx.lock(); EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); WaitOnConsumed(); @@ -924,6 +939,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00800, Function req.SetAutoDeletedTime(0); req.SetClassification("classification"); req.SetColor(0); + req.SetCreatorUserId(SUBSCRIBE_USER_SYSTEM_BEGIN); g_consumed_mtx.lock(); EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); WaitOnConsumed(); @@ -970,6 +986,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_00900, Function req.SetShortcutId("shortcutid"); req.SetFloatingIcon(false); req.SetProgressBar(0, 0, false); + req.SetCreatorUserId(SUBSCRIBE_USER_SYSTEM_BEGIN); g_consumed_mtx.lock(); EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); WaitOnConsumed(); @@ -1125,6 +1142,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_GetActiveNotifications_ auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); WaitOnSubscribeResult(); @@ -1142,6 +1160,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_GetActiveNotifications_ NotificationRequest req1(0); req1.SetLabel(label1); req1.SetContent(content); + req1.SetCreatorUserId(SUBSCRIBE_USER_SYSTEM_BEGIN); g_consumed_mtx.lock(); EXPECT_EQ(0, NotificationHelper::PublishNotification(req1)); WaitOnConsumed(); @@ -1151,6 +1170,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_GetActiveNotifications_ NotificationRequest req2(0); req2.SetLabel(label2); req2.SetContent(content); + req2.SetCreatorUserId(SUBSCRIBE_USER_SYSTEM_BEGIN); g_consumed_mtx.lock(); EXPECT_EQ(0, NotificationHelper::PublishNotification(req2)); WaitOnConsumed(); @@ -1161,7 +1181,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_GetActiveNotifications_ EXPECT_EQ((int)ERR_OK, NotificationHelper::GetActiveNotifications(requests)); EXPECT_EQ("Label1", requests[0]->GetLabel()); EXPECT_EQ("Label2", requests[1]->GetLabel()); - EXPECT_EQ((int)ERR_OK, (int)NotificationHelper::RemoveNotifications()); + EXPECT_EQ((int)ERR_OK, (int)NotificationHelper::RemoveNotifications(SUBSCRIBE_USER_SYSTEM_BEGIN)); sleep(SLEEP_TIME); EXPECT_EQ(OnCanceledReceived, true); EXPECT_EQ((int)ERR_OK, NotificationHelper::GetActiveNotificationNums(countAfter)); @@ -1182,6 +1202,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_CancelGroup_10100, Func { NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); auto subscriber = TestAnsSubscriber(); g_subscribe_mtx.lock(); @@ -1228,13 +1249,13 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_CancelGroup_10100, Func NotificationBundleOption bo("bundlename", 0); GTEST_LOG_(INFO) << "ANS_Interface_MT_CancelGroup_10100:: call RemoveGroupByBundle : effective parameters"; - EXPECT_NE(0, NotificationHelper::RemoveGroupByBundle(bo, "group10100")); + EXPECT_EQ(0, NotificationHelper::RemoveGroupByBundle(bo, "group10100")); sleep(SLEEP_TIME); OnCanceledReceived = false; GTEST_LOG_(INFO) << "ANS_Interface_MT_CancelGroup_10100:: call RemoveGroupByBundle : invalid parameters"; - EXPECT_NE(0, NotificationHelper::RemoveGroupByBundle(bo, "ngroup")); + EXPECT_EQ(0, NotificationHelper::RemoveGroupByBundle(bo, "ngroup")); sleep(SLEEP_TIME); OnCanceledReceived = false; @@ -1257,6 +1278,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_04000, Function auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); WaitOnSubscribeResult(); @@ -1371,6 +1393,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_05000, Function auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); WaitOnSubscribeResult(); @@ -1410,6 +1433,7 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_06000, Function auto subscriber = TestAnsSubscriber(); NotificationSubscribeInfo info = NotificationSubscribeInfo(); info.AddAppName("bundleName"); + info.AddAppUserId(SUBSCRIBE_USER_ALL); g_subscribe_mtx.lock(); EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); WaitOnSubscribeResult(); diff --git a/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp b/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp index a34cdb20e850acab50001e0717857aba44b584dd..726b6d3b64e3a8fe178033ff640d7a329db55ebf 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp @@ -150,20 +150,6 @@ HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_005 EXPECT_EQ(false, support); } -/** - * @tc.number : ANS_Interface_MT_NotificationSetting_00600 - * @tc.name : NotificationSetting_00600 - * @tc.desc : The template exists in the system, query whether the template exists. - * @tc.expected : Query return success. - */ -HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_00600, Function | MediumTest | Level1) -{ - std::string templateName("process"); - bool support = false; - EXPECT_EQ(0, NotificationHelper::IsSupportTemplate(templateName, support)); - EXPECT_EQ(true, support); -} - /** * @tc.number : ANS_Interface_MT_NotificationSetting_00700 * @tc.name : NotificationSetting_00700 @@ -174,7 +160,8 @@ HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_007 { std::string templateName("template123"); bool support = false; - EXPECT_EQ(0, NotificationHelper::IsSupportTemplate(templateName, support)); + EXPECT_EQ((int)ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED, + NotificationHelper::IsSupportTemplate(templateName, support)); EXPECT_EQ(false, support); } } // namespace Notification diff --git a/frameworks/ans/test/moduletest/mock/include/mock_bundle_manager.h b/frameworks/ans/test/moduletest/mock/include/mock_bundle_manager.h index ce1ef007f3d3239be5a04cfe8b1770fc8b760e9c..3f04edd54e985e20434f983247b3b69aa334c971 100644 --- a/frameworks/ans/test/moduletest/mock/include/mock_bundle_manager.h +++ b/frameworks/ans/test/moduletest/mock/include/mock_bundle_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -178,7 +178,7 @@ public: } /** * @brief Query the AbilityInfo by the given Want. - * @param want Indicates the infomation of the ability. + * @param want Indicates the information of the ability. * @param abilityInfo Indicates the obtained AbilityInfo object. * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. */ @@ -449,11 +449,11 @@ public: return true; } /** - * @brief Dump the bundle informations with specifc flags. + * @brief Dump the bundle information with specific flags. * @param flag Indicates the information contained in the dump result. * @param bundleName Indicates the bundle name if needed. * @param userId Indicates the user ID. - * @param result Indicates the dump infomation result. + * @param result Indicates the dump information result. * @return Returns true if the dump result is successfully obtained; returns false otherwise. */ virtual bool DumpInfos( diff --git a/interfaces/innerkits/ans/native/include/reminder_request.h b/interfaces/innerkits/ans/native/include/reminder_request.h index f4585d1deb5efb3c9e2cf5341fcdecf86afcf70d..798913b6a582a95a44c45f2c865a8cb2862a4563 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request.h +++ b/interfaces/innerkits/ans/native/include/reminder_request.h @@ -607,7 +607,7 @@ public: static const uint8_t REMINDER_STATUS_SNOOZE; // For database recovery. - static void Init(); + static void InitDbColumns(); static const std::string REMINDER_ID; static const std::string PKG_NAME; static const std::string USER_ID; @@ -664,8 +664,6 @@ protected: */ static void AddColumn(const std::string &name, const std::string &type, const bool &isEnd); - static const int BASE_YEAR; - private: void AddActionButtons(const bool includeSnooze); void AddRemovalWantAgent(); diff --git a/interfaces/innerkits/ans/native/include/reminder_request_alarm.h b/interfaces/innerkits/ans/native/include/reminder_request_alarm.h index b0f562507939de534ab7055b15a1a1dfc2011101..705dbf0a03945b6e2c1cbb4773573dcfb991881e 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_alarm.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_alarm.h @@ -109,7 +109,7 @@ public: const sptr &bundleOption, NativeRdb::ValuesBucket &values); // For database recovery. - static void Init(); + static void InitDbColumns(); protected: virtual uint64_t PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const override; diff --git a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h index 9d022dc7d002633612488e780b8b6382654d71d6..f5d0e7a358bb9120abc5c9c1c55d6726dc91611a 100644 --- a/interfaces/innerkits/ans/native/include/reminder_request_calendar.h +++ b/interfaces/innerkits/ans/native/include/reminder_request_calendar.h @@ -147,7 +147,7 @@ public: static uint8_t GetDaysOfMonth(const uint16_t &year, const uint8_t &month); // For database recovery. - static void Init(); + static void InitDbColumns(); protected: virtual uint64_t PreGetNextTriggerTimeIgnoreSnooze(bool ignoreRepeat, bool forceToGetNext) const override; diff --git a/interfaces/innerkits/ans/native/include/reminder_store.h b/interfaces/innerkits/ans/native/include/reminder_store.h index 8e49fded8d3f0590461b5a6504fe5026ef14c9af..cea23b096fecfb5139dbfac9d72a145b5e0152be 100644 --- a/interfaces/innerkits/ans/native/include/reminder_store.h +++ b/interfaces/innerkits/ans/native/include/reminder_store.h @@ -41,8 +41,6 @@ public: int64_t UpdateOrInsert(const sptr &reminder, const sptr &bundleOption); static uint8_t GetColumnIndex(const std::string& name); - static std::vector columns; - static const int32_t STATE_FAIL; static const int32_t STATE_OK; private: @@ -71,13 +69,6 @@ private: std::shared_ptr Query(const std::string &queryCondition) const; int64_t Update(const sptr &reminder, const sptr &bundleOption); - static const std::string REMINDER_DB_DIR; - static const std::string REMINDER_DB_NAME; - static const std::string REMINDER_DB_TABLE; - static const uint32_t REMINDER_RDB_VERSION; - static const uint16_t TIME_INTERVAL_FOR_DELETE; - static const uint16_t MILLI_SECONDS; - class ReminderStoreDataCallBack : public NativeRdb::RdbOpenCallback { public: int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; diff --git a/interfaces/kits/js/@ohos.notification.d.ts b/interfaces/kits/js/@ohos.notification.d.ts index 41b9500eefaf26fa03d5d90c2e73478ec582a2eb..9d14e393fcabd70c71e8d5e1f776287b47e339ce 100644 --- a/interfaces/kits/js/@ohos.notification.d.ts +++ b/interfaces/kits/js/@ohos.notification.d.ts @@ -49,6 +49,19 @@ declare namespace notification { function publish(request: NotificationRequest, callback: AsyncCallback): void; function publish(request: NotificationRequest): Promise; + /** + * Publishes a notification to the specified user. + * + * @since 8 + * @param Publishes a notification. + * @param userId of subscriber receiving the notification + * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER + * + */ + function publish(request: NotificationRequest, userId: number, callback: AsyncCallback): void; + function publish(request: NotificationRequest, userId: number): Promise; + /** * Cancels a notification with the specified ID. * @@ -78,9 +91,9 @@ declare namespace notification { * * @param slot Indicates the notification slot to be created, which is set by {@link NotificationSlot}. * This parameter must be specified. - * - * @systemapi Hide this for inner system use. * @param callback callback function + * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function addSlot(slot: NotificationSlot, callback: AsyncCallback): void; @@ -91,6 +104,7 @@ declare namespace notification { * This parameter must be specified. * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function addSlot(slot: NotificationSlot): Promise; @@ -110,6 +124,7 @@ declare namespace notification { * This parameter must be specified. * @param callback callback function * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function addSlots(slots: Array, callback: AsyncCallback): void; @@ -120,6 +135,7 @@ declare namespace notification { * This parameter must be specified. * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function addSlots(slots: Array): Promise; @@ -144,7 +160,7 @@ declare namespace notification { /** * Removes a NotificationSlot of the specified SlotType created by the current application. * - * @param slotType of the NotificationSlot to remove. + * @param slotType Type of the NotificationSlot to remove. * @param callback callback function */ function removeSlot(slotType: SlotType, callback: AsyncCallback): void; @@ -315,6 +331,7 @@ declare namespace notification { * isNotificationEnabled * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function isNotificationEnabled(bundle: BundleOption, callback: AsyncCallback): void; @@ -322,6 +339,7 @@ declare namespace notification { * isNotificationEnabled * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function isNotificationEnabled(bundle: BundleOption): Promise; @@ -329,6 +347,7 @@ declare namespace notification { * isNotificationEnabled * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function isNotificationEnabled(callback: AsyncCallback): void; @@ -336,13 +355,25 @@ declare namespace notification { * isNotificationEnabled * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function isNotificationEnabled(): Promise; + /** + * Checks whether this application has permission to publish notifications under the user. + * + * since 8 + * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER + */ + function isNotificationEnabled(userId: number, callback: AsyncCallback): void; + function isNotificationEnabled(userId: number): Promise; + /** * displayBadge * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function displayBadge(bundle: BundleOption, enable: boolean, callback: AsyncCallback): void; @@ -350,6 +381,7 @@ declare namespace notification { * displayBadge * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function displayBadge(bundle: BundleOption, enable: boolean): Promise; @@ -357,6 +389,7 @@ declare namespace notification { * isBadgeDisplayed * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function isBadgeDisplayed(bundle: BundleOption, callback: AsyncCallback): void; @@ -364,6 +397,7 @@ declare namespace notification { * isBadgeDisplayed * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function isBadgeDisplayed(bundle: BundleOption): Promise; @@ -403,6 +437,7 @@ declare namespace notification { * getSlotNumByBundle * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function getSlotNumByBundle(bundle: BundleOption, callback: AsyncCallback): void; @@ -410,6 +445,7 @@ declare namespace notification { * getSlotNumByBundle * * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function getSlotNumByBundle(bundle: BundleOption): Promise; @@ -461,6 +497,16 @@ declare namespace notification { */ function removeAll(callback: AsyncCallback): void; + /** + * Remove all notifications under the specified user. + * + * @since 8 + * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER + */ + function removeAll(userId: number, callback: AsyncCallback): void; + function removeAll(userId: number): Promise; + /** * removeAll * @@ -527,6 +573,16 @@ declare namespace notification { function setDoNotDisturbDate(date: DoNotDisturbDate, callback: AsyncCallback): void; function setDoNotDisturbDate(date: DoNotDisturbDate): Promise; + /** + * Set the Do Not Disturb date under the specified user. + * + * @since 8 + * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER + */ + function setDoNotDisturbDate(date: DoNotDisturbDate, userId: number, callback: AsyncCallback): void; + function setDoNotDisturbDate(date: DoNotDisturbDate, userId: number): Promise; + /** * Obtains the Do Not Disturb date. * @@ -537,6 +593,16 @@ declare namespace notification { function getDoNotDisturbDate(callback: AsyncCallback): void; function getDoNotDisturbDate(): Promise; + /** + * Obtains the Do Not Disturb date. + * + * @since 8 + * @systemapi Hide this for inner system use under the specified user. + * @permission ohos.permission.NOTIFICATION_CONTROLLER + */ + function getDoNotDisturbDate(userId: number, callback: AsyncCallback): void; + function getDoNotDisturbDate(userId: number): Promise; + /** * Obtains whether to support the Do Not Disturb mode. * @@ -557,6 +623,62 @@ declare namespace notification { function isSupportTemplate(templateName: string, callback: AsyncCallback): void; function isSupportTemplate(templateName: string): Promise; + /** + * Request permission to send notification. + * + * @since 8 + */ + function requestEnableNotification(callback: AsyncCallback): void; + function requestEnableNotification(): Promise; + + /** + * Sets whether the device supports distributed notification. + * + * @since 8 + * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER + */ + function enableDistributed(enable: boolean, callback: AsyncCallback): void; + function enableDistributed(enable: boolean): Promise; + + /** + * Obtains whether the device supports distributed notification. + * + * @since 8 + */ + function isDistributedEnabled(callback: AsyncCallback): void; + function isDistributedEnabled(): Promise; + + /** + * Sets whether an application supports distributed notification. + * + * @since 8 + * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER + */ + function enableDistributedByBundle(bundle: BundleOption, enable: boolean, callback: AsyncCallback): void; + function enableDistributedByBundle(bundle: BundleOption, enable: boolean): Promise; + + /** + * Obtains whether an application supports distributed notification. + * + * @since 8 + * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER + */ + function isDistributedEnabledByBundle(bundle: BundleOption, callback: AsyncCallback): void; + function isDistributedEnabledByBundle(bundle: BundleOption): Promise; + + /** + * Obtains the remind modes of the notification. + * + * @since 8 + * @systemapi Hide this for inner system use. + * @permission ohos.permission.NOTIFICATION_CONTROLLER + */ + function getDeviceRemindType(callback: AsyncCallback): void; + function getDeviceRemindType(): Promise; + /** * Describes a BundleOption. */ @@ -629,29 +751,6 @@ declare namespace notification { end: Date; } - /** - * Notification source type - * - * @since 8 - * @systemapi Hide this for inner system use. - */ - export enum SourceType { - /** - * General notification - */ - TYPE_NORMAL = 0x00000000, - - /** - * Continuous notification - */ - TYPE_CONTINUOUS = 0x00000001, - - /** - * Scheduled notification - */ - TYPE_TIMER = 0x00000002, - } - /** * The type of the Do Not Disturb. * @@ -709,26 +808,31 @@ declare namespace notification { } /** - * Notification source type + * The remind type of the nofication. * * @since 8 * @systemapi Hide this for inner system use. */ - export enum SourceType { + export enum DeviceRemindType { + /** + * The device is not in use, no reminder + */ + IDLE_DONOT_REMIND = 0, + /** - * General notification + * The device is not in use, remind */ - TYPE_NORMAL = 0x00000000, + IDLE_REMIND = 1, /** - * Continuous notification + * The device is in use, no reminder */ - TYPE_CONTINUOUS = 0x00000001, + ACTIVE_DONOT_REMIND = 2, /** - * Scheduled notification + * The device is in use, reminder */ - TYPE_TIMER = 0x00000002, + ACTIVE_REMIND = 3, } } diff --git a/interfaces/kits/js/notification/notificationRequest.d.ts b/interfaces/kits/js/notification/notificationRequest.d.ts index f53673de216b902ddd1e73d496df2439ea8c8b25..9accc34cc170b4d355d705ab49b1f0fd1b02c123 100644 --- a/interfaces/kits/js/notification/notificationRequest.d.ts +++ b/interfaces/kits/js/notification/notificationRequest.d.ts @@ -162,6 +162,12 @@ export interface NotificationRequest { */ readonly creatorPid?: number; + /** + * @since 8 + * Read-only UserId of the notification creator. + */ + readonly creatorUserId?: number; + /** * Obtains the classification of this notification. * @@ -198,6 +204,21 @@ export interface NotificationRequest { */ template?: NotificationTemplate; + /** + * The options to distributed notification. + * + * @since 8 + */ + distributedOption?: DistributedOptions; + + /** + * The device ID of the notification source. + * + * @since 8 + * @systemapi Hide this for inner system use. + */ + readonly deviceId?: string; + /** * Obtains the set of identifiers for the notification. * @@ -205,3 +226,37 @@ export interface NotificationRequest { */ readonly notificationFlags?: NotificationFlags; } + +/** + * Describes distributed options. + * + * @name DistributedOptions + * @since 8 + * @sysCap SystemCapability.Notification.Notification + * @permission N/A + */ +export interface DistributedOptions { + /** + * Obtains whether is the distributed notification. + * + * @default true + */ + isDistributed?: boolean; + + /** + * Obtains the types of devices to which the notification can be synchronized. + */ + supportDisplayDevices?: Array; + + /** + * Obtains the devices on which notifications can be open. + */ + supportOperateDevices?: Array; + + /** + * Obtains the remind mode of the notification. enum DeviceRemindType. + + * @systemapi Hide this for inner system use. + */ + readonly remindType?: number; +} diff --git a/interfaces/kits/js/notification/notificationSubscriber.d.ts b/interfaces/kits/js/notification/notificationSubscriber.d.ts index 2ab26d469efe817a4d6e92445408bf001a2d1305..d14e59c088cd25a309190e869b1de94d54f498e1 100644 --- a/interfaces/kits/js/notification/notificationSubscriber.d.ts +++ b/interfaces/kits/js/notification/notificationSubscriber.d.ts @@ -41,6 +41,13 @@ export interface NotificationSubscriber { * @since 8 */ onDoNotDisturbDateChange?:(mode: notification.DoNotDisturbDate) => void; + + /** + * Callback when the notificaition permission is changed. + * + * @since 8 + */ + onEnabledNotificationChanged?:(callbackData: EnabledNotificationCallbackData) => void; } /** @@ -59,4 +66,17 @@ export interface SubscribeCallbackData { readonly reason?: number; readonly sound?: string; readonly vibrationValues?: Array; +} + +/** + * Describes the properties of the application that the permission to send notifications has changed. + * + * @name EnabledNotificationCallbackData + * @systemapi Hide this for inner system use. + * @since 8 + */ +export interface EnabledNotificationCallbackData { + readonly bundle: string; + readonly uid: number; + readonly enable: boolean; } \ No newline at end of file diff --git a/interfaces/kits/napi/ans/src/disturb_mode.cpp b/interfaces/kits/napi/ans/src/disturb_mode.cpp index 0bf8a47fb891dbf44868103c485cba7a3c79eafb..dd0291acdd8a703862fe146b2d4d5e70c233c9b0 100644 --- a/interfaces/kits/napi/ans/src/disturb_mode.cpp +++ b/interfaces/kits/napi/ans/src/disturb_mode.cpp @@ -308,13 +308,13 @@ napi_value GetDoNotDisturbDate(napi_env env, napi_callback_info info) { ANS_LOGI("enter"); - SetDoNotDisturbDateParams params {}; + GetDoNotDisturbDateParams params {}; if (ParseParameters(env, info, params) == nullptr) { return Common::NapiGetUndefined(env); } - AsyncCallbackInfoSetDoNotDisturb *asynccallbackinfo = - new (std::nothrow) AsyncCallbackInfoSetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params}; + AsyncCallbackInfoGetDoNotDisturb *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetDoNotDisturb {.env = env, .asyncWork = nullptr, .params = params}; if (!asynccallbackinfo) { return Common::JSParaError(env, params.callback); } diff --git a/interfaces/kits/napi/ans/src/init.cpp b/interfaces/kits/napi/ans/src/init.cpp index 9fae48ef7863ceddd8d9c0fa9d1fddafd9705f74..2a26b7ce35f7443ec3d14cf9f9d38427b8ecb1c4 100644 --- a/interfaces/kits/napi/ans/src/init.cpp +++ b/interfaces/kits/napi/ans/src/init.cpp @@ -63,7 +63,6 @@ napi_value NotificationInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("removeGroupByBundle", RemoveGroupByBundle), DECLARE_NAPI_FUNCTION("enableNotification", EnableNotification), DECLARE_NAPI_FUNCTION("isNotificationEnabled", IsNotificationEnabled), - DECLARE_NAPI_FUNCTION("isNotificationEnabledSelf", IsNotificationEnabledSelf), DECLARE_NAPI_FUNCTION("requestEnableNotification", RequestEnableNotification), DECLARE_NAPI_FUNCTION("displayBadge", DisplayBadge), DECLARE_NAPI_FUNCTION("isBadgeDisplayed", IsBadgeDisplayed), diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 539c4360e81c62617249bc333430ee46ed485a07..fb86fd4e03edb176fdb86f1bfb8ed486fe0115f5 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -135,6 +135,7 @@ public: void OnScreenOn(); void OnScreenOff(); #endif + void OnResourceRemove(int32_t userId); // Distributed KvStore void OnDistributedKvStoreDeathRecipient(); diff --git a/services/ans/include/bundle_manager_helper.h b/services/ans/include/bundle_manager_helper.h index 9a060facae6ec7dcb7d1bdab33e06a77af3423df..1ddaf79187f5f1425ad4ea59f700b659af2da745 100644 --- a/services/ans/include/bundle_manager_helper.h +++ b/services/ans/include/bundle_manager_helper.h @@ -33,7 +33,7 @@ class BundleManagerHelper : public DelayedSingleton { public: std::string GetBundleNameByUid(int uid); bool IsSystemApp(int uid); - int GetDefaultUidByBundleName(const std::string &bundle); + int GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId); bool GetBundleInfoByBundleName(const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED bool GetDistributedNotificationEnabled(const std::string &bundleName, const int userId); diff --git a/services/ans/include/interface_system_event.h b/services/ans/include/interface_system_event.h index 0c583550351610ef96b15fcf55a27b60b5a45867..a68618e0894a2f0499ba3ddca76451346acbdb1f 100644 --- a/services/ans/include/interface_system_event.h +++ b/services/ans/include/interface_system_event.h @@ -29,6 +29,7 @@ struct ISystemEvent { std::function onScreenOn; std::function onScreenOff; #endif + std::function onResourceRemove; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 5920b76f980f063cf369e63fc6c04b3097936006..63d6053779f2f7c6f528cc246e7ab9c024c3b85a 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -76,6 +76,7 @@ public: void OnDistributedKvStoreDeathRecipient(); void InitSettingFromDisturbDB(); + void RemoveSettings(int32_t userId); private: ErrCode CheckSlotForCreateSlot(const sptr &bundleOption, diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 0d943c24a47c75261334dd4c9a4293d041a88710..5382772b0e7170052df6a54b345f30d4b99f45c4 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -54,36 +54,39 @@ public: bool StoreDeathRecipient(); + bool RemoveNotificationEnable(const int32_t userId); + bool RemoveDoNotDisturbDate(const int32_t userId); + private: - void TryTwice(const std::function &func) const; - OHOS::DistributedKv::Status GetKvStore(); + void TryTwice(const std::function &func) const; + DistributedKv::Status GetKvStore(); void CloseKvStore(); bool CheckKvStore(); bool CheckBundle(const std::string &bundleName, const int &bundleUid); bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo); template - OHOS::DistributedKv::Status PutBundlePropertyToDisturbeDB( + DistributedKv::Status PutBundlePropertyToDisturbeDB( const std::string &bundleKey, const BundleType &type, const T &t); bool PutBundleToDisturbeDB( const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo); - void GetValueFromDisturbeDB(const std::string &key, std::function funcion); + void GetValueFromDisturbeDB(const std::string &key, std::function funcion); void GetValueFromDisturbeDB(const std::string &key, - std::function funcion); + std::function funcion); bool GetRemoveGroupKeysFromDisturbeDB( - const std::string &bundleKey, const std::string &groupId, std::vector &keys); + const std::string &bundleKey, const std::string &groupId, std::vector &keys); bool SlotToEntry(const std::string &bundleName, const int &bundleUid, const sptr &slot, - std::vector &entries); + std::vector &entries); bool GroupToEntry(const std::string &bundleName, const int &bundleUid, const sptr &group, - std::vector &entries); + std::vector &entries); void GenerateGroupEntry(const std::string &bundleKey, const sptr &group, - std::vector &entries) const; + std::vector &entries) const; void GenerateSlotEntry(const std::string &bundleKey, const sptr &slot, - std::vector &entries) const; + std::vector &entries) const; void GenerateEntry( - const std::string &key, const std::string &value, std::vector &entry) const; + const std::string &key, const std::string &value, std::vector &entry) const; std::string FindLastString(const std::string &findString, const std::string &inputString) const; std::string SubUniqueIdentifyFromString(const std::string &findString, const std::string &keyStr) const; @@ -99,19 +102,19 @@ private: std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const; void ParseBundleFromDistureDB( - NotificationPreferencesInfo &info, const std::vector &entries); + NotificationPreferencesInfo &info, const std::vector &entries); void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, - const OHOS::DistributedKv::Entry &entry); + const DistributedKv::Entry &entry); void ParseGroupFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, - const OHOS::DistributedKv::Entry &entry); + const DistributedKv::Entry &entry); void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, - const std::string &bundleKey, const OHOS::DistributedKv::Entry &entry); + const std::string &bundleKey, const DistributedKv::Entry &entry); void ParseDoNotDisturbType(NotificationPreferencesInfo &info); void ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info); void ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info); void ParseEnableAllNotification(NotificationPreferencesInfo &info); void ParseGroupDescription( - const std::string &bundleKey, sptr &group, const OHOS::DistributedKv::Entry &entry); + const std::string &bundleKey, sptr &group, const DistributedKv::Entry &entry); void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleShowBadge(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; @@ -124,7 +127,7 @@ private: NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseSlot( - const std::string &findString, sptr &slot, const OHOS::DistributedKv::Entry &entry); + const std::string &findString, sptr &slot, const DistributedKv::Entry &entry); void ParseSlotGroupId(sptr &slot, const std::string &value) const; void ParseSlotDescription(sptr &slot, const std::string &value) const; void ParseSlotLevel(sptr &slot, const std::string &value) const; diff --git a/services/ans/include/notification_preferences_info.h b/services/ans/include/notification_preferences_info.h index 732e1d736fc0bfe5e7e9fd217b72dfd1bca24631..b7af5c22c73b26e01f445e15a39628095c3a95fb 100644 --- a/services/ans/include/notification_preferences_info.h +++ b/services/ans/include/notification_preferences_info.h @@ -94,6 +94,8 @@ public: sptr &doNotDisturbDate) const; void SetEnabledAllNotification(const int32_t &userId, const bool &enable); bool GetEnabledAllNotification(const int32_t &userId, bool &enable) const; + void RemoveNotificationEnable(const int32_t userId); + void RemoveDoNotDisturbDate(const int32_t userId); private: std::map isEnabledAllNotification_; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index ab0d0fb28c5be0577d0133b145b2a02cd81c2761..40dbff15f914d141b0751f6f8fa55fb4b3bee47a 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -245,6 +245,7 @@ AdvancedNotificationService::AdvancedNotificationService() std::bind(&AdvancedNotificationService::OnScreenOn, this), std::bind(&AdvancedNotificationService::OnScreenOff, this), #endif + std::bind(&AdvancedNotificationService::OnResourceRemove, this, std::placeholders::_1), }; systemEventObserver_ = std::make_shared(iSystemEvent); @@ -302,7 +303,12 @@ sptr AdvancedNotificationService::GenerateValidBundleO if (bundleOption->GetUid() <= 0) { std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); if (bundleManager != nullptr) { - int uid = bundleManager->GetDefaultUidByBundleName(bundleOption->GetBundleName()); + int activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return validBundleOption; + } + int uid = bundleManager->GetDefaultUidByBundleName(bundleOption->GetBundleName(), activeUserId); if (uid > 0) { validBundleOption = new NotificationBundleOption(bundleOption->GetBundleName(), uid); } @@ -420,6 +426,11 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( ErrCode AdvancedNotificationService::Publish(const std::string &label, const sptr &request) { ANS_LOGD("%{public}s", __FUNCTION__); + + if (request->GetReceiverUserId() != SUBSCRIBE_USER_INIT && !IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + sptr bundleOption; ErrCode result = PrepareNotificationInfo(request, bundleOption); if (result != ERR_OK) { @@ -1575,7 +1586,7 @@ ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(targetBundle, allowed); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; - allowed = CheckApiCompatibility(bundleOption); + allowed = CheckApiCompatibility(targetBundle); } } })); @@ -1614,6 +1625,10 @@ ErrCode AdvancedNotificationService::PublishContinuousTaskNotification(const spt if (uid != SYSTEM_SERVICE_UID) { return ERR_ANS_NOT_SYSTEM_SERVICE; } + int userId = SUBSCRIBE_USER_INIT; + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId); + request->SetCreatorUserId(userId); + ANS_LOGD("%{public}s, uid=%{public}d userId=%{public}d", __FUNCTION__, uid, userId); if (request->GetCreatorBundleName().empty()) { request->SetCreatorBundleName(FOUNDATION_BUNDLE_NAME); @@ -2389,15 +2404,18 @@ ErrCode AdvancedNotificationService::SetDoNotDisturbDate(const sptr &request) { ANS_LOGD("%{public}s", __FUNCTION__); - - request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName)); + int activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return; + } + request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, activeUserId)); handler_->PostTask(std::bind([this, deviceId, bundleName, request]() { if (!CheckDistributedNotificationType(request)) { @@ -2807,8 +2829,12 @@ void AdvancedNotificationService::OnDistributedUpdate( const std::string &deviceId, const std::string &bundleName, sptr &request) { ANS_LOGD("%{public}s", __FUNCTION__); - - request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName)); + int activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return; + } + request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, activeUserId)); handler_->PostTask(std::bind([this, deviceId, bundleName, request]() { if (!CheckDistributedNotificationType(request)) { @@ -3089,7 +3115,9 @@ ErrCode AdvancedNotificationService::GetDoNotDisturbDate(const int32_t &userId, ErrCode AdvancedNotificationService::SetDoNotDisturbDateByUser(const int32_t &userId, const sptr &date) { + ANS_LOGD("%{public}s enter, userId = %{public}d", __FUNCTION__, userId); if (date == nullptr) { + ANS_LOGE("Invalid date param"); return ERR_ANS_INVALID_PARAM; } @@ -3097,7 +3125,6 @@ ErrCode AdvancedNotificationService::SetDoNotDisturbDateByUser(const int32_t &us int64_t beginDate = ResetSeconds(date->GetBeginDate()); int64_t endDate = ResetSeconds(date->GetEndDate()); - switch (date->GetDoNotDisturbType()) { case NotificationConstant::DoNotDisturbType::NONE: beginDate = 0; @@ -3114,7 +3141,7 @@ ErrCode AdvancedNotificationService::SetDoNotDisturbDateByUser(const int32_t &us default: break; } - + ANS_LOGD("Before set SetDoNotDisturbDate beginDate = %{public}lld, endDate = %{public}lld", beginDate, endDate); const sptr newConfig = new NotificationDoNotDisturbDate( date->GetDoNotDisturbType(), beginDate, @@ -3123,6 +3150,7 @@ ErrCode AdvancedNotificationService::SetDoNotDisturbDateByUser(const int32_t &us sptr bundleOption = GenerateBundleOption(); if (bundleOption == nullptr) { + ANS_LOGE("Generate invalid bundle option!"); return ERR_ANS_INVALID_BUNDLE; } @@ -3196,6 +3224,8 @@ bool AdvancedNotificationService::CheckApiCompatibility(const sptrGetUid(), callingUserId); if (bundleManager != nullptr) { if (!bundleManager->GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { + ANS_LOGW("Failed to GetBundleInfoByBundleName, bundlename = %{public}s", + bundleOption->GetBundleName().c_str()); return false; } } @@ -3207,5 +3237,14 @@ bool AdvancedNotificationService::CheckApiCompatibility(const sptrPostSyncTask(std::bind([&]() { + NotificationPreferences::GetInstance().RemoveSettings(userId); + })); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/bundle_manager_helper.cpp b/services/ans/src/bundle_manager_helper.cpp index 7afa5a38f68c2b2184681f66ba420df836aaa17a..9e965edcb73798f9fe07f06e0f98ee0b2983315e 100644 --- a/services/ans/src/bundle_manager_helper.cpp +++ b/services/ans/src/bundle_manager_helper.cpp @@ -111,7 +111,7 @@ void BundleManagerHelper::Disconnect() } } -int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle) +int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId) { int uid = -1; @@ -121,7 +121,7 @@ int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle) if (bundleMgr_ != nullptr) { AppExecFwk::BundleInfo bundleInfo; - if (bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo)) { + if (bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, userId)) { uid = bundleInfo.uid; } } diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 38729309f170d1e7aeb28418451f388fe6a1b515..79c2e9fd5d9697f725e4e904a95e0306b5e7d48f 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -27,6 +27,7 @@ namespace OHOS { namespace Notification { NotificationPreferences::NotificationPreferences() { + preferncesDB_ = std::make_unique(); InitSettingFromDisturbDB(); } @@ -566,6 +567,7 @@ ErrCode NotificationPreferences::GetDoNotDisturbDate(const int32_t &userId, ErrCode NotificationPreferences::SetDoNotDisturbDate(const int32_t &userId, const sptr date) { + ANS_LOGE("enter."); if (userId <= SUBSCRIBE_USER_INIT) { return ERR_ANS_INVALID_PARAM; } @@ -870,10 +872,23 @@ ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templat void NotificationPreferences::InitSettingFromDisturbDB() { - if (!preferncesDB_) { - preferncesDB_ = std::make_unique(); + ANS_LOGD("%{public}s", __FUNCTION__); + if (preferncesDB_ != nullptr) { + preferncesDB_->ParseFromDisturbeDB(preferencesInfo_); + } +} + +void NotificationPreferences::RemoveSettings(int32_t userId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + preferencesInfo_.RemoveNotificationEnable(userId); + preferencesInfo_.RemoveDoNotDisturbDate(userId); + + if (preferncesDB_ != nullptr) { + preferncesDB_->RemoveNotificationEnable(userId); + preferncesDB_->RemoveDoNotDisturbDate(userId); } - preferncesDB_->ParseFromDisturbeDB(preferencesInfo_); } } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index f445f5321c1b9c5c7de9373b74c5761aebdb3cd8..89bb2b22c252b678f6285ba562f46db8324da47e 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -136,25 +136,25 @@ NotificationPreferencesDatabase::~NotificationPreferencesDatabase() CloseKvStore(); } -void NotificationPreferencesDatabase::TryTwice(const std::function &func) const +void NotificationPreferencesDatabase::TryTwice(const std::function &func) const { - OHOS::DistributedKv::Status status = func(); - if (status != OHOS::DistributedKv::Status::SUCCESS) { + DistributedKv::Status status = func(); + if (status != DistributedKv::Status::SUCCESS) { status = func(); ANS_LOGW("Distribute database error and try to call again, result = %{public}d.", status); } } -OHOS::DistributedKv::Status NotificationPreferencesDatabase::GetKvStore() +DistributedKv::Status NotificationPreferencesDatabase::GetKvStore() { - OHOS::DistributedKv::Options options = { + DistributedKv::Options options = { .createIfMissing = true, .encrypt = false, .autoSync = false, - .kvStoreType = OHOS::DistributedKv::KvStoreType::SINGLE_VERSION, + .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION, }; auto status = dataManager_.GetSingleKvStore(options, appId_, storeId_, kvStorePtr_); - if (status != OHOS::DistributedKv::Status::SUCCESS) { + if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("Return error: %{public}d.", status); } else { ANS_LOGD("Get kvStore success."); @@ -191,7 +191,7 @@ bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB( } bool result = true; - std::vector entries; + std::vector entries; for (auto iter : slots) { result = SlotToEntry(bundleName, bundleUid, iter, entries); if (!result) { @@ -203,8 +203,8 @@ bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB( ANS_LOGE("KvStore is nullptr."); return false; } - OHOS::DistributedKv::Status status = kvStorePtr_->PutBatch(entries); - return (status == OHOS::DistributedKv::Status::SUCCESS); + DistributedKv::Status status = kvStorePtr_->PutBatch(entries); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::PutGroupsToDisturbeDB( @@ -221,7 +221,7 @@ bool NotificationPreferencesDatabase::PutGroupsToDisturbeDB( } bool result = true; - std::vector entries; + std::vector entries; for (auto iter : groups) { result = GroupToEntry(bundleName, bundleUid, iter, entries); if (!result) { @@ -233,8 +233,8 @@ bool NotificationPreferencesDatabase::PutGroupsToDisturbeDB( ANS_LOGE("KvStore is nullptr."); return false; } - OHOS::DistributedKv::Status status = kvStorePtr_->PutBatch(entries); - return (status == OHOS::DistributedKv::Status::SUCCESS); + DistributedKv::Status status = kvStorePtr_->PutBatch(entries); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( @@ -252,13 +252,13 @@ bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( std::string bundleKeyStr = KEY_BUNDLE_LABEL + GenerateBundleLablel(bundleInfo); bool result = false; - GetValueFromDisturbeDB(bundleKeyStr, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { + GetValueFromDisturbeDB(bundleKeyStr, [&](DistributedKv::Status &status, DistributedKv::Value &value) { switch (status) { - case OHOS::DistributedKv::Status::KEY_NOT_FOUND: { + case DistributedKv::Status::KEY_NOT_FOUND: { result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo); break; } - case OHOS::DistributedKv::Status::SUCCESS: { + case DistributedKv::Status::SUCCESS: { ANS_LOGE("Current bundle has exsited."); break; } @@ -282,9 +282,9 @@ bool NotificationPreferencesDatabase::PutShowBadge( } std::string bundleKey = GenerateBundleLablel(bundleInfo); - OHOS::DistributedKv::Status status = + DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable); - return (status == OHOS::DistributedKv::Status::SUCCESS); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::PutImportance( @@ -300,9 +300,9 @@ bool NotificationPreferencesDatabase::PutImportance( } std::string bundleKey = GenerateBundleLablel(bundleInfo); - OHOS::DistributedKv::Status status = + DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_IMPORTANCE_TYPE, importance); - return (status == OHOS::DistributedKv::Status::SUCCESS); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::PutTotalBadgeNums( @@ -317,9 +317,9 @@ bool NotificationPreferencesDatabase::PutTotalBadgeNums( return false; } std::string bundleKey = GenerateBundleLablel(bundleInfo); - OHOS::DistributedKv::Status status = + DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum); - return (status == OHOS::DistributedKv::Status::SUCCESS); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::PutPrivateNotificationsAllowed( @@ -334,10 +334,10 @@ bool NotificationPreferencesDatabase::PutPrivateNotificationsAllowed( return false; } std::string bundleKey = GenerateBundleLablel(bundleInfo); - OHOS::DistributedKv::Status status = + DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_PRIVATE_ALLOWED_TYPE, allow); - return (status == OHOS::DistributedKv::Status::SUCCESS); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::PutNotificationsEnabledForBundle( @@ -353,9 +353,9 @@ bool NotificationPreferencesDatabase::PutNotificationsEnabledForBundle( } std::string bundleKey = GenerateBundleLablel(bundleInfo); - OHOS::DistributedKv::Status status = + DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled); - return (status == OHOS::DistributedKv::Status::SUCCESS); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::PutNotificationsEnabled(const int32_t &userId, const bool &enabled) @@ -367,10 +367,10 @@ bool NotificationPreferencesDatabase::PutNotificationsEnabled(const int32_t &use std::string typeKey = std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId)); - OHOS::DistributedKv::Key enableKey(typeKey); - OHOS::DistributedKv::Value enableValue(std::to_string(enabled)); - OHOS::DistributedKv::Status status = kvStorePtr_->Put(enableKey, enableValue); - if (status != OHOS::DistributedKv::Status::SUCCESS) { + DistributedKv::Key enableKey(typeKey); + DistributedKv::Value enableValue(std::to_string(enabled)); + DistributedKv::Status status = kvStorePtr_->Put(enableKey, enableValue); + if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("Store enable notification failed. %{public}d", status); return false; } @@ -390,9 +390,9 @@ bool NotificationPreferencesDatabase::PutHasPoppedDialog( } std::string bundleKey = GenerateBundleLablel(bundleInfo); - OHOS::DistributedKv::Status status = + DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped); - return (status == OHOS::DistributedKv::Status::SUCCESS); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::PutDoNotDisturbDate( @@ -408,32 +408,32 @@ bool NotificationPreferencesDatabase::PutDoNotDisturbDate( return false; } - OHOS::DistributedKv::Entry type; + DistributedKv::Entry type; std::string typeKey = std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId)); - type.key = OHOS::DistributedKv::Key(typeKey); - type.value = OHOS::DistributedKv::Value(std::to_string((int)date->GetDoNotDisturbType())); + type.key = DistributedKv::Key(typeKey); + type.value = DistributedKv::Value(std::to_string((int)date->GetDoNotDisturbType())); - OHOS::DistributedKv::Entry beginDate; + DistributedKv::Entry beginDate; std::string beginDateKey = std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); - beginDate.key = OHOS::DistributedKv::Key(beginDateKey); - beginDate.value = OHOS::DistributedKv::Value(std::to_string(date->GetBeginDate())); + beginDate.key = DistributedKv::Key(beginDateKey); + beginDate.value = DistributedKv::Value(std::to_string(date->GetBeginDate())); - OHOS::DistributedKv::Entry endDate; + DistributedKv::Entry endDate; std::string endDateKey = std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); - endDate.key = OHOS::DistributedKv::Key(endDateKey); - endDate.value = OHOS::DistributedKv::Value(std::to_string(date->GetEndDate())); + endDate.key = DistributedKv::Key(endDateKey); + endDate.value = DistributedKv::Value(std::to_string(date->GetEndDate())); - std::vector entries = { + std::vector entries = { type, beginDate, endDate, }; - OHOS::DistributedKv::Status status = kvStorePtr_->PutBatch(entries); - if (status != OHOS::DistributedKv::Status::SUCCESS) { + DistributedKv::Status status = kvStorePtr_->PutBatch(entries); + if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("Store DoNotDisturbDate failed. %{public}d", status); return false; } @@ -442,42 +442,42 @@ bool NotificationPreferencesDatabase::PutDoNotDisturbDate( } void NotificationPreferencesDatabase::GetValueFromDisturbeDB( - const std::string &key, std::function funcion) + const std::string &key, std::function callback) { if (!CheckKvStore()) { ANS_LOGE("KvStore is nullptr."); return; } - OHOS::DistributedKv::Status status; - OHOS::DistributedKv::Value value; - OHOS::DistributedKv::Key getKey(key); + DistributedKv::Status status; + DistributedKv::Value value; + DistributedKv::Key getKey(key); status = kvStorePtr_->Get(getKey, value); - if (status != OHOS::DistributedKv::Status::SUCCESS) { - ANS_LOGE("Get value failed, use defalut value. error code is %{public}d", status); + if (status != DistributedKv::Status::SUCCESS) { + ANS_LOGE("Get value failed, use default value. error code is %{public}d", status); return; } if (value.Empty()) { - ANS_LOGE("Get value is empty, use defalut value. error code is %{public}d", value.Empty()); + ANS_LOGE("Get value is empty, use default value. error code is %{public}d", value.Empty()); return; } - funcion(value); + callback(value); } void NotificationPreferencesDatabase::GetValueFromDisturbeDB( - const std::string &key, std::function funcion) + const std::string &key, std::function callback) { if (!CheckKvStore()) { ANS_LOGE("KvStore is nullptr."); return; } - OHOS::DistributedKv::Status status; - OHOS::DistributedKv::Value value; - OHOS::DistributedKv::Key getKey(key); + DistributedKv::Status status; + DistributedKv::Value value; + DistributedKv::Key getKey(key); status = kvStorePtr_->Get(getKey, value); - funcion(status, value); + callback(status, value); } bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, const int &bundleUid) @@ -485,16 +485,16 @@ bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, std::string bundleKeyStr = KEY_BUNDLE_LABEL + bundleName + std::to_string(bundleUid); ANS_LOGD("CheckBundle bundleKeyStr %{public}s", bundleKeyStr.c_str()); bool result = true; - GetValueFromDisturbeDB(bundleKeyStr, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { + GetValueFromDisturbeDB(bundleKeyStr, [&](DistributedKv::Status &status, DistributedKv::Value &value) { switch (status) { - case OHOS::DistributedKv::Status::KEY_NOT_FOUND: { + case DistributedKv::Status::KEY_NOT_FOUND: { NotificationPreferencesInfo::BundleInfo bundleInfo; bundleInfo.SetBundleName(bundleName); bundleInfo.SetBundleUid(bundleUid); result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo); break; } - case OHOS::DistributedKv::Status::SUCCESS: { + case DistributedKv::Status::SUCCESS: { result = true; break; } @@ -509,7 +509,7 @@ bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB( const NotificationPreferencesInfo::BundleInfo &bundleInfo) { - std::vector entries; + std::vector entries; std::string bundleKey = bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid())); GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_NAME), bundleInfo.GetBundleName(), entries); GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM), @@ -533,8 +533,8 @@ bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB( ANS_LOGE("KvStore is nullptr."); return false; } - OHOS::DistributedKv::Status status = kvStorePtr_->PutBatch(entries); - if (status != OHOS::DistributedKv::Status::SUCCESS) { + DistributedKv::Status status = kvStorePtr_->PutBatch(entries); + if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("Store bundle failed. %{public}d", status); return false; } @@ -553,10 +553,10 @@ bool NotificationPreferencesDatabase::ParseFromDisturbeDB(NotificationPreference ANS_LOGE("KvStore is nullptr."); return false; } - OHOS::DistributedKv::Status status; - std::vector entries; + DistributedKv::Status status; + std::vector entries; status = kvStorePtr_->GetEntries(DistributedKv::Key(KEY_BUNDLE_LABEL), entries); - if (status != OHOS::DistributedKv::Status::SUCCESS) { + if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("Get Bundle Info failed."); return false; } @@ -571,8 +571,8 @@ bool NotificationPreferencesDatabase::RemoveAllDataFromDisturbeDB() ANS_LOGE("KvStore is nullptr."); return false; } - OHOS::DistributedKv::Status status = dataManager_.DeleteKvStore(appId_, storeId_); - return (status == OHOS::DistributedKv::Status::SUCCESS); + DistributedKv::Status status = dataManager_.DeleteKvStore(appId_, storeId_); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::RemoveBundleFromDisturbeDB(const std::string &bundleKey) @@ -583,25 +583,25 @@ bool NotificationPreferencesDatabase::RemoveBundleFromDisturbeDB(const std::stri return false; } - OHOS::DistributedKv::Status status; - std::vector entries; + DistributedKv::Status status; + std::vector entries; status = kvStorePtr_->GetEntries( DistributedKv::Key(KEY_ANS_BUNDLE + KEY_UNDER_LINE + bundleKey + KEY_UNDER_LINE), entries); - if (status != OHOS::DistributedKv::Status::SUCCESS) { + if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("Get Bundle Info failed."); return false; } - std::vector keys; + std::vector keys; for (auto iter : entries) { keys.push_back(iter.key); } - OHOS::DistributedKv::Key bundleDBKey(KEY_BUNDLE_LABEL + KEY_BUNDLE_NAME + KEY_UNDER_LINE + bundleKey); + DistributedKv::Key bundleDBKey(KEY_BUNDLE_LABEL + KEY_BUNDLE_NAME + KEY_UNDER_LINE + bundleKey); keys.push_back(bundleDBKey); status = kvStorePtr_->DeleteBatch(keys); - if (status != OHOS::DistributedKv::Status::SUCCESS) { + if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("delete bundle Info failed."); return false; } @@ -622,21 +622,21 @@ bool NotificationPreferencesDatabase::RemoveSlotFromDisturbeDB( return false; } - OHOS::DistributedKv::Status status; - std::vector slotentries; + DistributedKv::Status status; + std::vector slotentries; std::string slotType = std::to_string(type); status = kvStorePtr_->GetEntries(DistributedKv::Key(GenerateSlotKey(bundleKey, slotType) + KEY_UNDER_LINE), slotentries); if (status != DistributedKv::Status::SUCCESS) { return false; } - std::vector keys; + std::vector keys; for (auto iter : slotentries) { keys.push_back(iter.key); } status = kvStorePtr_->DeleteBatch(keys); - if (status != OHOS::DistributedKv::Status::SUCCESS) { + if (status != DistributedKv::Status::SUCCESS) { ANS_LOGE("delete bundle Info failed."); return false; } @@ -658,20 +658,20 @@ bool NotificationPreferencesDatabase::RemoveAllSlotsFromDisturbeDB(const std::st return false; } - OHOS::DistributedKv::Status status; - std::vector slotsEntries; + DistributedKv::Status status; + std::vector slotsEntries; status = kvStorePtr_->GetEntries(DistributedKv::Key(GenerateSlotKey(bundleKey) + KEY_UNDER_LINE), slotsEntries); if (status != DistributedKv::Status::SUCCESS) { return false; } - std::vector keys; + std::vector keys; for (auto iter : slotsEntries) { keys.push_back(iter.key); } status = kvStorePtr_->DeleteBatch(keys); ANS_LOGD("%{public}s remove all slots status %{public}d", __FUNCTION__, status); - return (status == OHOS::DistributedKv::Status::SUCCESS); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::RemoveGroupsFromDisturbeDB( @@ -692,7 +692,7 @@ bool NotificationPreferencesDatabase::RemoveGroupsFromDisturbeDB( return false; } - std::vector keys; + std::vector keys; bool result = true; for (auto iter : groupIds) { result = GetRemoveGroupKeysFromDisturbeDB(bundleKey, iter, keys); @@ -701,21 +701,21 @@ bool NotificationPreferencesDatabase::RemoveGroupsFromDisturbeDB( } } - OHOS::DistributedKv::Status status = kvStorePtr_->DeleteBatch(keys); + DistributedKv::Status status = kvStorePtr_->DeleteBatch(keys); ANS_LOGD("%{public}s remove groups status %{public}d", __FUNCTION__, status); - return (status == OHOS::DistributedKv::Status::SUCCESS); + return (status == DistributedKv::Status::SUCCESS); } bool NotificationPreferencesDatabase::GetRemoveGroupKeysFromDisturbeDB( - const std::string &bundleKey, const std::string &groupId, std::vector &keys) + const std::string &bundleKey, const std::string &groupId, std::vector &keys) { if (!CheckKvStore()) { ANS_LOGE("KvStore is nullptr."); return false; } - OHOS::DistributedKv::Status status; - std::vector groupentries; + DistributedKv::Status status; + std::vector groupentries; std::string slotKeyStr = GenerateGroupKey(bundleKey, groupId); status = kvStorePtr_->GetEntries(DistributedKv::Key(slotKeyStr + KEY_UNDER_LINE), groupentries); if (status != DistributedKv::Status::SUCCESS) { @@ -738,7 +738,7 @@ bool NotificationPreferencesDatabase::StoreDeathRecipient() } template -OHOS::DistributedKv::Status NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( +DistributedKv::Status NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( const std::string &bundleKey, const BundleType &type, const T &t) { std::string keyStr; @@ -764,13 +764,13 @@ OHOS::DistributedKv::Status NotificationPreferencesDatabase::PutBundlePropertyTo default: break; } - OHOS::DistributedKv::Key key(keyStr); - OHOS::DistributedKv::Value value(std::to_string(t)); + DistributedKv::Key key(keyStr); + DistributedKv::Value value(std::to_string(t)); if (!CheckKvStore()) { ANS_LOGE("KvStore is nullptr."); - return OHOS::DistributedKv::Status::ERROR; + return DistributedKv::Status::ERROR; } - OHOS::DistributedKv::Status status = kvStorePtr_->Put(key, value); + DistributedKv::Status status = kvStorePtr_->Put(key, value); return status; } @@ -783,9 +783,9 @@ bool NotificationPreferencesDatabase::PutBundleToDisturbeDB( } ANS_LOGD("Key not fund, so create a bundle, bundle key is %{public}s.", bundleKey.c_str()); - OHOS::DistributedKv::Key bundleDBKey(bundleKey); - OHOS::DistributedKv::Value bundleValue(GenerateBundleLablel(bundleInfo)); - if (kvStorePtr_->Put(bundleDBKey, bundleValue) != OHOS::DistributedKv::Status::SUCCESS) { + DistributedKv::Key bundleDBKey(bundleKey); + DistributedKv::Value bundleValue(GenerateBundleLablel(bundleInfo)); + if (kvStorePtr_->Put(bundleDBKey, bundleValue) != DistributedKv::Status::SUCCESS) { ANS_LOGE("Store bundle name to db is failed."); return false; } @@ -798,18 +798,18 @@ bool NotificationPreferencesDatabase::PutBundleToDisturbeDB( } void NotificationPreferencesDatabase::GenerateEntry( - const std::string &key, const std::string &value, std::vector &entries) const + const std::string &key, const std::string &value, std::vector &entries) const { - OHOS::DistributedKv::Entry entry; - OHOS::DistributedKv::Key dbKey(key); - OHOS::DistributedKv::Value dbValue(value); + DistributedKv::Entry entry; + DistributedKv::Key dbKey(key); + DistributedKv::Value dbValue(value); entry.key = dbKey; entry.value = dbValue; entries.push_back(entry); } bool NotificationPreferencesDatabase::SlotToEntry(const std::string &bundleName, const int &bundleUid, - const sptr &slot, std::vector &entries) + const sptr &slot, std::vector &entries) { if (slot == nullptr) { ANS_LOGE("Notification group is nullptr."); @@ -826,7 +826,7 @@ bool NotificationPreferencesDatabase::SlotToEntry(const std::string &bundleName, } void NotificationPreferencesDatabase::GenerateSlotEntry(const std::string &bundleKey, - const sptr &slot, std::vector &entries) const + const sptr &slot, std::vector &entries) const { std::string slotType = std::to_string(slot->GetType()); GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_TYPE), std::to_string(slot->GetType()), entries); @@ -857,7 +857,7 @@ void NotificationPreferencesDatabase::GenerateSlotEntry(const std::string &bundl } bool NotificationPreferencesDatabase::GroupToEntry(const std::string &bundleName, const int &bundleUid, - const sptr &group, std::vector &entries) + const sptr &group, std::vector &entries) { if (group == nullptr) { @@ -875,7 +875,7 @@ bool NotificationPreferencesDatabase::GroupToEntry(const std::string &bundleName } void NotificationPreferencesDatabase::GenerateGroupEntry(const std::string &bundleKey, - const sptr &group, std::vector &entries) const + const sptr &group, std::vector &entries) const { std::string groupLebal = group->GetId().append(KEY_UNDER_LINE); GenerateEntry(GenerateGroupKey(bundleKey, groupLebal + KEY_GROUP_ID), group->GetId(), entries); @@ -886,7 +886,7 @@ void NotificationPreferencesDatabase::GenerateGroupEntry(const std::string &bund } void NotificationPreferencesDatabase::ParseBundleFromDistureDB( - NotificationPreferencesInfo &info, const std::vector &entries) + NotificationPreferencesInfo &info, const std::vector &entries) { if (!CheckKvStore()) { ANS_LOGE("KvStore is nullptr."); @@ -895,7 +895,7 @@ void NotificationPreferencesDatabase::ParseBundleFromDistureDB( for (auto item : entries) { std::string bundleKey = item.value.ToString(); ANS_LOGD("Bundle name is %{public}s.", bundleKey.c_str()); - std::vector bundleEntries; + std::vector bundleEntries; kvStorePtr_->GetEntries(DistributedKv::Key(GenerateBundleKey(bundleKey)), bundleEntries); ANS_LOGD("Bundle key is %{public}s.", GenerateBundleKey(bundleKey).c_str()); NotificationPreferencesInfo::BundleInfo bunldeInfo; @@ -914,7 +914,7 @@ void NotificationPreferencesDatabase::ParseBundleFromDistureDB( } void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, - const std::string &bundleKey, const OHOS::DistributedKv::Entry &entry) + const std::string &bundleKey, const DistributedKv::Entry &entry) { std::string slotKey = entry.key.ToString(); std::string typeStr = SubUniqueIdentifyFromString(GenerateSlotKey(bundleKey) + KEY_UNDER_LINE, slotKey); @@ -929,7 +929,7 @@ void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPrefer } void NotificationPreferencesDatabase::ParseGroupFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, - const std::string &bundleKey, const OHOS::DistributedKv::Entry &entry) + const std::string &bundleKey, const DistributedKv::Entry &entry) { if (!CheckKvStore()) { ANS_LOGE("KvStore is nullptr."); @@ -942,7 +942,7 @@ void NotificationPreferencesDatabase::ParseGroupFromDisturbeDB(NotificationPrefe std::string groupName; std::string groupNameKey = GenerateGroupKey(bundleKey, groupId + KEY_UNDER_LINE + KEY_GROUP_NAME); GetValueFromDisturbeDB( - groupNameKey, [&groupName](OHOS::DistributedKv::Value &value) { groupName = value.ToString(); }); + groupNameKey, [&groupName](DistributedKv::Value &value) { groupName = value.ToString(); }); if (groupName.empty()) { ANS_LOGE("Group name does not exsited."); return; @@ -956,7 +956,7 @@ void NotificationPreferencesDatabase::ParseGroupFromDisturbeDB(NotificationPrefe void NotificationPreferencesDatabase::ParseBundlePropertyFromDisturbeDB( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, - const OHOS::DistributedKv::Entry &entry) + const DistributedKv::Entry &entry) { std::string typeStr = FindLastString(GenerateBundleKey(bundleKey), entry.key.ToString()); std::string valueStr = entry.value.ToString(); @@ -970,7 +970,7 @@ void NotificationPreferencesDatabase::ParseBundlePropertyFromDisturbeDB( } void NotificationPreferencesDatabase::ParseSlot( - const std::string &findString, sptr &slot, const OHOS::DistributedKv::Entry &entry) + const std::string &findString, sptr &slot, const DistributedKv::Entry &entry) { std::string typeStr = FindLastString(findString, entry.key.ToString()); std::string valueStr = entry.value.ToString(); @@ -987,7 +987,7 @@ void NotificationPreferencesDatabase::ParseSlot( if (!typeStr.compare(KEY_SLOT_VIBRATION_STYLE)) { GetValueFromDisturbeDB(findString + KEY_SLOT_ENABLE_VRBRATION, - [&](OHOS::DistributedKv::Value &value) { ParseSlotEnableVrbration(slot, value.ToString()); }); + [&](DistributedKv::Value &value) { ParseSlotEnableVrbration(slot, value.ToString()); }); } } @@ -1184,7 +1184,7 @@ void NotificationPreferencesDatabase::ParseEnableAllNotification(NotificationPre } void NotificationPreferencesDatabase::ParseGroupDescription( - const std::string &bundleKey, sptr &group, const OHOS::DistributedKv::Entry &entry) + const std::string &bundleKey, sptr &group, const DistributedKv::Entry &entry) { std::string findStr = GenerateGroupKey(bundleKey, group->GetId()) + KEY_UNDER_LINE; std::string typeStr = FindLastString(findStr, entry.key.ToString()); @@ -1348,13 +1348,13 @@ void NotificationPreferencesDatabase::GetDoNotDisturbType(NotificationPreference std::string key = std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( - key, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { + key, [&](DistributedKv::Status &status, DistributedKv::Value &value) { sptr disturbDate = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); info.GetDoNotDisturbDate(userId, disturbDate); - if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { + if (status == DistributedKv::Status::KEY_NOT_FOUND) { PutDoNotDisturbDate(userId, disturbDate); - } else if (status == OHOS::DistributedKv::Status::SUCCESS) { + } else if (status == DistributedKv::Status::SUCCESS) { if (!value.ToString().empty()) { if (disturbDate != nullptr) { disturbDate->SetDoNotDisturbType( @@ -1373,13 +1373,13 @@ void NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(NotificationPrefe std::string key = std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( - key, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { + key, [&](DistributedKv::Status &status, DistributedKv::Value &value) { sptr disturbDate = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); info.GetDoNotDisturbDate(userId, disturbDate); - if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { + if (status == DistributedKv::Status::KEY_NOT_FOUND) { PutDoNotDisturbDate(userId, disturbDate); - } else if (status == OHOS::DistributedKv::Status::SUCCESS) { + } else if (status == DistributedKv::Status::SUCCESS) { if (!value.ToString().empty()) { if (disturbDate != nullptr) { disturbDate->SetBeginDate(StringToInt64(value.ToString())); @@ -1397,13 +1397,13 @@ void NotificationPreferencesDatabase::GetDoNotDisturbEndDate(NotificationPrefere std::string key = std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( - key, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { + key, [&](DistributedKv::Status &status, DistributedKv::Value &value) { sptr disturbDate = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); info.GetDoNotDisturbDate(userId, disturbDate); - if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { + if (status == DistributedKv::Status::KEY_NOT_FOUND) { PutDoNotDisturbDate(userId, disturbDate); - } else if (status == OHOS::DistributedKv::Status::SUCCESS) { + } else if (status == DistributedKv::Status::SUCCESS) { if (!value.ToString().empty()) { if (disturbDate != nullptr) { disturbDate->SetEndDate(StringToInt64(value.ToString())); @@ -1421,15 +1421,15 @@ void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPrefe std::string key = std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( - key, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { - if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { + key, [&](DistributedKv::Status &status, DistributedKv::Value &value) { + if (status == DistributedKv::Status::KEY_NOT_FOUND) { bool enable = true; if (!info.GetEnabledAllNotification(userId, enable)) { info.SetEnabledAllNotification(userId, enable); ANS_LOGW("Enable setting not found, default true."); } PutNotificationsEnabled(userId, enable); - } else if (status == OHOS::DistributedKv::Status::SUCCESS) { + } else if (status == DistributedKv::Status::SUCCESS) { if (!value.ToString().empty()) { info.SetEnabledAllNotification(userId, static_cast(StringToInt(value.ToString()))); } @@ -1438,5 +1438,57 @@ void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPrefe } }); } + +bool NotificationPreferencesDatabase::RemoveNotificationEnable(const int32_t userId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + if (!CheckKvStore()) { + ANS_LOGE("KvStore is nullptr."); + return false; + } + + std::string key = + std::string(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId)); + DistributedKv::Key enableKey(key); + DistributedKv::Status status = kvStorePtr_->Delete(enableKey); + if (status != DistributedKv::Status::SUCCESS) { + ANS_LOGE("delete bundle Info failed."); + return false; + } + + ANS_LOGD("%{public}s remove notification enable, userId : %{public}d", __FUNCTION__, userId); + return true; +} + +bool NotificationPreferencesDatabase::RemoveDoNotDisturbDate(const int32_t userId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + if (!CheckKvStore()) { + ANS_LOGE("KvStore is nullptr."); + return false; + } + + std::string typeKey = + std::string(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId)); + std::string beginDateKey = + std::string(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); + std::string endDateKey = + std::string(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); + + std::vector keys = { + DistributedKv::Key(typeKey), + DistributedKv::Key(beginDateKey), + DistributedKv::Key(endDateKey) + }; + + DistributedKv::Status status = kvStorePtr_->DeleteBatch(keys); + if (status != DistributedKv::Status::SUCCESS) { + ANS_LOGE("delete DoNotDisturb date failed."); + return false; + } + + ANS_LOGD("%{public}s remove DoNotDisturb date, userId : %{public}d", __FUNCTION__, userId); + return true; +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/notification_preferences_info.cpp b/services/ans/src/notification_preferences_info.cpp index 55eabc6c099c80fe72c571bed32c3c083ea98bda..1afe46b42ffd44781c82a602940a94cc3bf6d02e 100644 --- a/services/ans/src/notification_preferences_info.cpp +++ b/services/ans/src/notification_preferences_info.cpp @@ -313,5 +313,15 @@ bool NotificationPreferencesInfo::GetEnabledAllNotification(const int32_t &userI } return false; } + +void NotificationPreferencesInfo::RemoveNotificationEnable(const int32_t userId) +{ + isEnabledAllNotification_.erase(userId); +} + +void NotificationPreferencesInfo::RemoveDoNotDisturbDate(const int32_t userId) +{ + doNotDisturbDate_.erase(userId); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_subscriber_manager.cpp b/services/ans/src/notification_subscriber_manager.cpp index 1cccad0594456b9e0a2fa08b7ca4e967541fcf6c..cb9342f9ee7715a5bd20f05cb1363a5c414facfc 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -63,11 +63,18 @@ ErrCode NotificationSubscriberManager::AddSubscriber( ANS_LOGE("Failed to create NotificationSubscribeInfo ptr."); return ERR_ANS_NO_MEMORY; } + } + if (subInfo->GetAppUserId() == SUBSCRIBE_USER_INIT) { int userId = SUBSCRIBE_USER_INIT; int callingUid = IPCSkeleton::GetCallingUid(); - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); - ANS_LOGD("AddSubscriber callingUid = <%{public}d> userId = <%{public}d>", callingUid, userId); + ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); + if (ret != ERR_OK) { + ANS_LOGD("Get userId failed, callingUid = <%{public}d>", callingUid); + return ERR_ANS_INVALID_PARAM; + } + + ANS_LOGD("Get userId successed, callingUid = <%{public}d> userId = <%{public}d>", callingUid, userId); subInfo->AddAppUserId(userId); } @@ -222,9 +229,6 @@ void NotificationSubscriberManager::AddRecordInfo( record->subscribedAll = false; } record->userId = subscribeInfo->GetAppUserId(); - if (record->userId == SUBSCRIBE_USER_INIT) { - record->userId = SUBSCRIBE_USER_ALL; - } } else { record->bundleList_.clear(); record->subscribedAll = true; @@ -300,15 +304,18 @@ void NotificationSubscriberManager::NotifyConsumedInner( { ANS_LOGD("%{public}s notification->GetUserId <%{public}d>", __FUNCTION__, notification->GetUserId()); int32_t recvUserId = notification->GetNotificationRequest().GetReceiverUserId(); + int32_t sendUserId = notification->GetUserId(); for (auto record : subscriberRecordList_) { - ANS_LOGD("%{public}s record->userId = <%{public}d>", __FUNCTION__, record->userId); auto BundleNames = notification->GetBundleName(); + ANS_LOGD("%{public}s record->userId = <%{public}d> BundleName = <%{public}s", + __FUNCTION__, record->userId, BundleNames.c_str()); auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), BundleNames); if (!record->subscribedAll == (iter != record->bundleList_.end()) && - (notification->GetUserId() == record->userId || - notification->GetUserId() == SUBSCRIBE_USER_ALL || - recvUserId == record->userId || - IsSystemUser(record->userId))) { + (record->userId == sendUserId || + record->userId == SUBSCRIBE_USER_ALL || + record->userId == recvUserId || + IsSystemUser(record->userId) || // Delete this, When the systemui subscribe carry the user ID. + IsSystemUser(sendUserId))) { record->subscriber->OnConsumed(notification, notificationMap); record->subscriber->OnConsumed(notification); } @@ -320,15 +327,17 @@ void NotificationSubscriberManager::NotifyCanceledInner( { ANS_LOGD("%{public}s notification->GetUserId <%{public}d>", __FUNCTION__, notification->GetUserId()); int32_t recvUserId = notification->GetNotificationRequest().GetReceiverUserId(); + int32_t sendUserId = notification->GetUserId(); for (auto record : subscriberRecordList_) { ANS_LOGD("%{public}s record->userId = <%{public}d>", __FUNCTION__, record->userId); auto BundleNames = notification->GetBundleName(); auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), BundleNames); if (!record->subscribedAll == (iter != record->bundleList_.end()) && - (notification->GetUserId() == record->userId || - notification->GetUserId() == SUBSCRIBE_USER_ALL || - recvUserId == record->userId || - IsSystemUser(record->userId))) { + (record->userId == sendUserId || + record->userId == SUBSCRIBE_USER_ALL || + record->userId == recvUserId || + IsSystemUser(record->userId) || // Delete this, When the systemui subscribe carry the user ID. + IsSystemUser(sendUserId))) { record->subscriber->OnCanceled(notification, notificationMap, deleteReason); record->subscriber->OnCanceled(notification); } diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index 64df9b4db8f602d32f3f16b947407f2f84b5cc24..3a1874b82063389e8afa6f732f75b08cc450cd06 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -15,6 +15,7 @@ #include "system_event_observer.h" +#include "advanced_notification_service.h" #include "bundle_constants.h" #include "common_event_manager.h" #include "common_event_support.h" @@ -31,6 +32,7 @@ SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callba matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF); #endif matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills); subscriber_ = std::make_shared( @@ -69,6 +71,9 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) #endif } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { NotificationPreferences::GetInstance().InitSettingFromDisturbDB(); + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { + int32_t userId = data.GetCode(); + callbacks_.onResourceRemove(userId); } } } // namespace Notification diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index fc1d0c27c842d028fcb732c200f1e954209c7d3a..f58b8f4e70b1f95b79deeea6b187cff60866e81f 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -1229,6 +1229,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_09600, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_09700, Function | SmallTest | Level1) { + EXPECT_EQ( + (int)advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), true), (int)ERR_OK); TestAddSlot(NotificationConstant::SlotType::OTHER); bool allowed = true; EXPECT_EQ((int)advancedNotificationService_->IsSpecialBundleAllowedNotify( @@ -1243,6 +1245,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_09700, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_09800, Function | SmallTest | Level1) { + EXPECT_EQ( + (int)advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), true), (int)ERR_OK); std::vector> notifications; bool allowed = true; EXPECT_EQ((int)advancedNotificationService_->IsSpecialBundleAllowedNotify( diff --git a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp index 7528a5c370598c4f3198c81625b6103ae9dd515e..6eba48ba6f8b9a28b9b77136e946362d3b88ca52 100644 --- a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp +++ b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp @@ -36,7 +36,7 @@ std::string BundleManagerHelper::GetBundleNameByUid(int uid) return (uid == NON_BUNDLE_NAME_UID) ? "" : "bundleName"; } -int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle) +int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId) { return NON_SYSTEM_APP_UID; } diff --git a/services/distributed/test/unittest/distributed_database_test.cpp b/services/distributed/test/unittest/distributed_database_test.cpp index f94a86995b037121c46699b1fb0f8f63033e66d5..0bd73d7fb106808c31d485b39a9ad91897ebf6ec 100644 --- a/services/distributed/test/unittest/distributed_database_test.cpp +++ b/services/distributed/test/unittest/distributed_database_test.cpp @@ -169,7 +169,7 @@ HWTEST_F(DistributedDatabaseTest, GetLocalDeviceId_00100, Function | SmallTest | /** * @tc.name : DistributedDatabase_GetLocalDeviceInfo_00100 * @tc.number : GetLocalDeviceInfo_00100 - * @tc.desc : Get local device infomation. + * @tc.desc : Get local device information. */ HWTEST_F(DistributedDatabaseTest, GetLocalDeviceInfo_00100, Function | SmallTest | Level1) { diff --git a/services/distributed/test/unittest/distributed_notification_manager_test.cpp b/services/distributed/test/unittest/distributed_notification_manager_test.cpp index c6cac5a4cf871d27f50b2b6dd7ace51a1b93ca8d..e4ab49cf0028b9dae973cc471fe5faf479f53c01 100644 --- a/services/distributed/test/unittest/distributed_notification_manager_test.cpp +++ b/services/distributed/test/unittest/distributed_notification_manager_test.cpp @@ -173,7 +173,7 @@ HWTEST_F(DistributedNotificationManagerTest, Distributed_Get_Current_Notificatio /** * @tc.name : Distributed_Get_Local_DeviceInfo_00100 * @tc.number : Distributed_Get_Local_DeviceInfo_00100 - * @tc.desc : Get local distributed device infomation. + * @tc.desc : Get local distributed device information. */ HWTEST_F(DistributedNotificationManagerTest, Distributed_Get_Local_DeviceInfo_00100, Function | SmallTest | Level1) { diff --git a/services/test/moduletest/ans_module_test.cpp b/services/test/moduletest/ans_module_test.cpp index 854b889753a1fba239fa56dab43e3ac2c7bc895d..9ca7b24f501ffbc9c9b10d0293c4555bf86854c3 100644 --- a/services/test/moduletest/ans_module_test.cpp +++ b/services/test/moduletest/ans_module_test.cpp @@ -126,7 +126,7 @@ void TestAddSlots() /** * @tc.number : AnsModuleTest_001 * @tc.name : ANS_Subscribe_Publish_0100 - * @tc.desc : Test the functions of subscribing, publishing, and canceling + * @tc.desc : Test the functions of subscribing all notifications, publishing, and canceling */ HWTEST_F(AnsModuleTest, AnsModuleTest_001, Function | SmallTest | Level1) { @@ -141,6 +141,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_001, Function | SmallTest | Level1) req1->SetLabel(label); sptr info = new NotificationSubscribeInfo(); info->AddAppName("bundleName"); + info->AddAppUserId(SUBSCRIBE_USER_ALL); EXPECT_EQ((int)g_advancedNotificationService->Subscribe(subscriber->GetImpl(), info), (int)ERR_OK); EXPECT_EQ((int)g_advancedNotificationService->Publish(label, req), (int)ERR_OK); EXPECT_EQ((int)g_advancedNotificationService->Publish(label, req1), (int)ERR_OK); @@ -186,7 +187,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_002, Function | SmallTest | Level1) /** * @tc.number : AnsModuleTest_003 * @tc.name : ANS_Module_Test_0300 - * @tc.desc : Test publish notifications when slot are not allowed publish. + * @tc.desc : Test publish notifications when slot not found, add it. */ HWTEST_F(AnsModuleTest, AnsModuleTest_003, Function | SmallTest | Level1) { @@ -213,7 +214,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_003, Function | SmallTest | Level1) g_advancedNotificationService->SetNotificationsEnabledForBundle("", false); g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(false, passed); + EXPECT_EQ(true, passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); } @@ -228,6 +229,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_005, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr sortingMap) { @@ -275,6 +277,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_006, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr, const std::shared_ptr) { passed = true; @@ -308,6 +311,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_007, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr sortingMap) { @@ -355,6 +359,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0013, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr, const std::shared_ptr) { passed = true; @@ -388,6 +393,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0014, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr, const std::shared_ptr) { passed = true; @@ -426,6 +432,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0015, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr, const std::shared_ptr) { passed = true; @@ -464,6 +471,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0017, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr, const std::shared_ptr) { passed = true; @@ -502,6 +510,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0019, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr, const std::shared_ptr) { passed = true; @@ -540,6 +549,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0021, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr sortingMap) { @@ -593,6 +603,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0023, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr, const std::shared_ptr) { passed = true; @@ -631,6 +642,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0031, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr sortingMap) { @@ -684,6 +696,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0033, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr) { @@ -728,6 +741,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0034, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr) { @@ -772,6 +786,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0035, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->canceledCb_ = [](const std::shared_ptr &request, const std::shared_ptr &sortingMap, @@ -812,6 +827,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0036, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->canceledCb_ = [](const std::shared_ptr &request, const std::shared_ptr &sortingMap, @@ -852,6 +868,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0039, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr sortingMap) { @@ -906,6 +923,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0040, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr) { passed = true; }; @@ -944,6 +962,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0041, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr) { passed = true; }; @@ -982,6 +1001,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0042, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr) { passed = true; }; @@ -1020,6 +1040,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0043, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr) { passed = true; }; @@ -1204,6 +1225,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0058, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); subscriber->consumedCb_ = [](const std::shared_ptr r, const std::shared_ptr) { if (r->GetNotificationRequest().GetBadgeNumber() == 1) { passed = true; @@ -1245,6 +1267,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0060, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); subscriber->consumedCb_ = [](const std::shared_ptr r, const std::shared_ptr) { passed = true; }; @@ -1292,6 +1315,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0061, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); subscriber->consumedCb_ = [](const std::shared_ptr r, const std::shared_ptr) { passed = true; }; @@ -1339,6 +1363,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0062, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr sortingMap) { @@ -1616,7 +1641,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0103, Function | SmallTest | Level1) // publish request g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(passed, false); + EXPECT_EQ(passed, true); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); } @@ -1659,6 +1684,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0106, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); subscriber->consumedCb_ = [](const std::shared_ptr notification, const std::shared_ptr sortingMap) { @@ -1691,6 +1717,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0107, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); // add slot @@ -1719,8 +1746,8 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0107, Function | SmallTest | Level1) g_advancedNotificationService->Publish(label, req1); // remove request - g_advancedNotificationService->Delete("_1_testLabel_0"); - g_advancedNotificationService->Delete("_1_testLabel_1"); + g_advancedNotificationService->Delete("_0_1_testLabel_0"); + g_advancedNotificationService->Delete("_0_1_testLabel_1"); int nums = -1; g_advancedNotificationService->GetActiveNotificationNums(nums); EXPECT_EQ(nums, 0); @@ -1738,6 +1765,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0108, Function | SmallTest | Level1) auto subscriber = new TestAnsSubscriber(); sptr subscriberInfo = new NotificationSubscribeInfo(); subscriberInfo->AddAppName("bundleName"); + subscriberInfo->AddAppUserId(SUBSCRIBE_USER_ALL); g_advancedNotificationService->Subscribe(subscriber->GetImpl(), subscriberInfo); // add slot @@ -1752,6 +1780,8 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0108, Function | SmallTest | Level1) sptr req1 = new NotificationRequest(1); req->SetLabel(label); req1->SetLabel(label); + req->SetNotificationId(0); + req1->SetNotificationId(1); // set content std::shared_ptr normalContent = std::make_shared(); @@ -1766,7 +1796,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0108, Function | SmallTest | Level1) g_advancedNotificationService->Publish(label, req1); // remove request - g_advancedNotificationService->DeleteAll(); + g_advancedNotificationService->DeleteAllByUser(0); int nums = -1; g_advancedNotificationService->GetActiveNotificationNums(nums); EXPECT_EQ(nums, 0); @@ -1826,9 +1856,10 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0112, Function | SmallTest | Level1) NotificationSorting sorting1; NotificationSorting sorting2; if (sortingKey.size() == 2) { - sortingMap->GetNotificationSorting("_1_testLabel_0", sorting1); - sortingMap->GetNotificationSorting("_1_testLabel_1", sorting2); + sortingMap->GetNotificationSorting("_0_1_testLabel_0", sorting1); + sortingMap->GetNotificationSorting("_0_1_testLabel_1", sorting2); } + if (sorting1.GetRanking() < sorting2.GetRanking() && notification->EnableLight() && notification->EnableSound() && notification->EnableVibrate()) { passed = true; @@ -1862,7 +1893,9 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0112, Function | SmallTest | Level1) req1->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); // publish request - EXPECT_FALSE(passed); + g_advancedNotificationService->Publish(label, req); + g_advancedNotificationService->Publish(label, req1); + EXPECT_TRUE(passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), nullptr); } @@ -2140,7 +2173,7 @@ HWTEST_F(AnsModuleTest, AnsModuleTest_0120, Function | SmallTest | Level1) g_advancedNotificationService->SetNotificationsEnabledForBundle("bundleName", false); g_advancedNotificationService->Publish(label, req); - EXPECT_EQ(false, passed); + EXPECT_EQ(true, passed); g_advancedNotificationService->Unsubscribe(subscriber->GetImpl(), subscriberInfo); }