diff --git a/frameworks/js/napi/napi_wallpaper_ability.cpp b/frameworks/js/napi/napi_wallpaper_ability.cpp index 12cb909010b22f0e2c82bc093bdc160a343526cb..349b2bcf072e8086d3ca276a432b85939605ed7f 100644 --- a/frameworks/js/napi/napi_wallpaper_ability.cpp +++ b/frameworks/js/napi/napi_wallpaper_ability.cpp @@ -35,6 +35,9 @@ const int32_t ONE = 1; const int32_t TWO = 2; const int32_t EVENTTYPESIZE = 64; const int32_t BUFFERSIZE = 100; +constexpr const char *COLOR_CHANGE_EVENT = "colorChange"; +constexpr const char *WALLPAPER_CHANGE_EVENT = "wallpaperChange"; + struct WorkData { napi_env env_; @@ -601,7 +604,8 @@ void NapiWallpaperAbility::GetImageInner(std::shared_ptr context return napi_ok; }; auto output = [context](napi_env env, napi_value *result) -> napi_status { - napi_value pixelVal = PixelMapNapi::CreatePixelMap(env, std::move(context->pixelMap)); + napi_value pixelVal = (context->pixelMap != nullptr ? + PixelMapNapi::CreatePixelMap(env, std::move(context->pixelMap)) : nullptr); HILOG_DEBUG("output PixelMapNapi::CreatePixelMap != nullptr[%{public}d]", pixelVal != nullptr); *result = pixelVal; return napi_ok; @@ -613,9 +617,9 @@ void NapiWallpaperAbility::GetImageInner(std::shared_ptr context WallpaperMgrService::WallpaperManagerkits::GetInstance().GetPixelMap(context->wallpaperType, apiInfo, pixelMap); HILOG_DEBUG("exec wallpaperErrorCode[%{public}d]", wallpaperErrorCode); - if (wallpaperErrorCode == E_OK && pixelMap != nullptr) { + if (wallpaperErrorCode == E_OK) { context->status = napi_ok; - context->pixelMap = std::move(pixelMap); + context->pixelMap = (pixelMap != nullptr ? std::move(pixelMap) : nullptr); return; } if (apiInfo.needException) { @@ -641,20 +645,36 @@ napi_value NAPI_On(napi_env env, napi_callback_info info) !NapiWallpaperAbility::IsValidArgType(env, argv[0], napi_string) || !NapiWallpaperAbility::IsValidArgType(env, argv[1], napi_function)) { HILOG_DEBUG("input argc : %{public}zu", argc); + if (NapiWallpaperAbility::IsValidArgCount(argc, 1) && // 1: argument count + NapiWallpaperAbility::IsValidArgType(env, argv[0], napi_string) && + WallpaperJSUtil::Convert2String(env, argv[0]) == COLOR_CHANGE_EVENT) { + return nullptr; + } + JsErrorInfo jsErrorInfo = JsError::ConvertErrorCode(E_PARAMETERS_INVALID); + JsError::ThrowError(env, jsErrorInfo.code, jsErrorInfo.message); return nullptr; } + std::string type = WallpaperJSUtil::Convert2String(env, argv[0]); HILOG_DEBUG("type : %{public}s", type.c_str()); - + if (type != COLOR_CHANGE_EVENT && type != WALLPAPER_CHANGE_EVENT) { + HILOG_ERROR("do not support event type: %{public}s", type.c_str()); + JsErrorInfo jsErrorInfo = JsError::ConvertErrorCode(E_PARAMETERS_INVALID); + JsError::ThrowError(env, jsErrorInfo.code, jsErrorInfo.message); + return nullptr; + } std::shared_ptr listener = std::make_shared(env, argv[1]); - - bool status = WallpaperMgrService::WallpaperManagerkits::GetInstance().On(type, listener); - if (!status) { + ErrorCode errorCode = WallpaperMgrService::WallpaperManagerkits::GetInstance().On(type, listener); + if (errorCode != E_OK) { HILOG_ERROR("WallpaperMgrService::WallpaperManagerkits::GetInstance().On failed!"); + if (type == COLOR_CHANGE_EVENT) { + return nullptr; + } + JsErrorInfo jsErrorInfo = JsError::ConvertErrorCode(errorCode); + JsError::ThrowError(env, jsErrorInfo.code, jsErrorInfo.message); return nullptr; } - napi_value result = nullptr; napi_get_undefined(env, &result); return result; @@ -668,27 +688,38 @@ napi_value NAPI_Off(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - if (!NapiWallpaperAbility::IsValidArgCount(argc, ONE) || + if (!NapiWallpaperAbility::IsValidArgCount(argc, 1) || // 1: argument count !NapiWallpaperAbility::IsValidArgType(env, argv[0], napi_string)) { HILOG_DEBUG("input argc : %{public}zu", argc); + JsErrorInfo jsErrorInfo = JsError::ConvertErrorCode(E_PARAMETERS_INVALID); + JsError::ThrowError(env, jsErrorInfo.code, jsErrorInfo.message); return nullptr; } + std::string type = WallpaperJSUtil::Convert2String(env, argv[0]); HILOG_DEBUG("type : %{public}s", type.c_str()); - + if (type != COLOR_CHANGE_EVENT && type != WALLPAPER_CHANGE_EVENT) { + HILOG_ERROR("do not support event type: %{public}s", type.c_str()); + JsErrorInfo jsErrorInfo = JsError::ConvertErrorCode(E_PARAMETERS_INVALID); + JsError::ThrowError(env, jsErrorInfo.code, jsErrorInfo.message); + return nullptr; + } std::shared_ptr listener = nullptr; if (argc >= TWO) { if (NapiWallpaperAbility::IsValidArgType(env, argv[1], napi_function)) { listener = std::make_shared(env, argv[1]); } } - - bool status = WallpaperMgrService::WallpaperManagerkits::GetInstance().Off(type, listener); - if (!status) { + ErrorCode errorCode = WallpaperMgrService::WallpaperManagerkits::GetInstance().Off(type, listener); + if (errorCode != E_OK) { HILOG_ERROR("WallpaperMgrService::WallpaperManagerkits::GetInstance().Off failed!"); + if (type == COLOR_CHANGE_EVENT) { + return nullptr; + } + JsErrorInfo jsErrorInfo = JsError::ConvertErrorCode(errorCode); + JsError::ThrowError(env, jsErrorInfo.code, jsErrorInfo.message); return nullptr; } - napi_value result = nullptr; napi_get_undefined(env, &result); return result; diff --git a/frameworks/js/napi/test/unittest/src/wallpaper_js/Wallpaper.test.js b/frameworks/js/napi/test/unittest/src/wallpaper_js/Wallpaper.test.js index 04b7386095954c1c1918cc11e0d36a690e501a23..ca824b8008b5f41c4de9f3735040835718ae6292 100644 --- a/frameworks/js/napi/test/unittest/src/wallpaper_js/Wallpaper.test.js +++ b/frameworks/js/napi/test/unittest/src/wallpaper_js/Wallpaper.test.js @@ -1631,7 +1631,7 @@ describe('WallpaperJSTest', function () { done(); }) } catch (error) { - console.info(`onCallbackTest001 error : ${error}`); + console.info(`onCallbackTest001 error : ${error.message}`); expect(null).assertFail(); done(); } @@ -1639,6 +1639,120 @@ describe('WallpaperJSTest', function () { await wallpaper.restore(WALLPAPER_LOCKSCREEN); }) + /** + * @tc.name: onCallbackTest002 + * @tc.desc: Test on_wallpaperChange to registers a listener for wallpaper changes to + * receive notifications about the changes. + * @tc.type: FUNC test + * @tc.require: issueI5UHRG + */ + it('onCallbackTest002', 0, async function (done) { + await wallpaper.restore(WALLPAPER_SYSTEM); + try { + wallpaper.on('wallpaperChange', (wallpaperType, resourceType) => { + expect(wallpaperType != null).assertTrue(); + expect(resourceType != null).assertTrue(); + wallpaper.off('wallpaperChange'); + done(); + }) + } catch (error) { + console.info(`onCallbackTest002 error : ${error.message}`); + expect(null).assertFail(); + done(); + } + await wallpaper.setImage(URI, WALLPAPER_SYSTEM); + await wallpaper.restore(WALLPAPER_SYSTEM); + }) + + /** + * @tc.name: onCallbackTest003 + * @tc.desc: Test on_wallpaperChange to registers a listener for wallpaper changes to + * receive notifications about the changes. + * @tc.type: FUNC test + * @tc.require: issueI5UHRG + */ + it('onCallbackTest003', 0, async function (done) { + await wallpaper.restore(WALLPAPER_SYSTEM); + try { + wallpaper.on('wallpaperChange'); + expect(null).assertFail(); + done(); + } catch (error) { + console.info(`onCallbackTest003 error : ${error.message}`); + expect(error != null).assertTrue(); + done(); + } + await wallpaper.setImage(URI, WALLPAPER_SYSTEM); + await wallpaper.restore(WALLPAPER_SYSTEM); + }) + + /** + * @tc.name: onCallbackTest004 + * @tc.desc: Test to register not exist event + * @tc.type: FUNC test + * @tc.require: issueI5UHRG + */ + it('onCallbackTest004', 0, async function (done) { + await wallpaper.restore(WALLPAPER_SYSTEM); + try { + wallpaper.on('wallpaperChangeX', (wallpaperType, resourceType) => { + expect(null).assertFail(); + done(); + }) + } catch (error) { + console.info(`onCallbackTest004 error : ${error.message}`); + expect(error.code == PARAMETER_ERROR).assertTrue(); + done(); + } + await wallpaper.setImage(URI, WALLPAPER_SYSTEM); + await wallpaper.restore(WALLPAPER_SYSTEM); + }) + + /** + * @tc.name: onCallbackTest005 + * @tc.desc: Test on_wallpaperChange to registers a listener for wallpaper changes to + * receive notifications about the changes. + * @tc.type: FUNC test + * @tc.require: issueI5UHRG + */ + it('onCallbackTest005', 0, async function (done) { + await wallpaper.restore(WALLPAPER_SYSTEM); + try { + wallpaper.on('wallpaperChange', 'illegalArg', (wallpaperType, resourceType) => { + expect(null).assertFail(); + done(); + }) + } catch (error) { + console.info(`onCallbackTest005 error : ${error.message}`); + expect(error != null).assertTrue(); + done(); + } + await wallpaper.setImage(URI, WALLPAPER_SYSTEM); + await wallpaper.restore(WALLPAPER_SYSTEM); + }) + + /** + * @tc.name: onCallbackTest006 + * @tc.desc: Test on_wallpaperChange to registers a listener for wallpaper changes to + * receive notifications about the changes. + * @tc.type: FUNC test + * @tc.require: issueI5UHRG + */ + it('onCallbackTest006', 0, async function (done) { + await wallpaper.restore(WALLPAPER_SYSTEM); + try { + wallpaper.on('wallpaperChange', 'other') + expect(null).assertFail(); + done(); + } catch (error) { + console.info(`onCallbackTest006 error : ${error.message}`); + expect(error != null).assertTrue(); + done(); + } + await wallpaper.setImage(URI, WALLPAPER_SYSTEM); + await wallpaper.restore(WALLPAPER_SYSTEM); + }) + /** * @tc.name: offCallbackTest001 * @tc.desc: Test off_colorChange to log off a listener for wallpaper color changes to @@ -1667,13 +1781,120 @@ describe('WallpaperJSTest', function () { await wallpaper.restore(WALLPAPER_SYSTEM); }) + /** + * @tc.name: offCallbackTest002 + * @tc.desc: Test wallpaperChange to log off a listener for wallpaper changes to + * receive notifications about the changes. + * @tc.type: FUNC test + * @tc.require: issueI5UHRG + */ + it('offCallbackTest002', 0, async function (done) { + let callbackTimes = 0; + await wallpaper.setImage(URI, WALLPAPER_SYSTEM); + try { + wallpaper.on('wallpaperChange', async (wallpaperType, resourceType) => { + expect(wallpaperType != null).assertTrue(); + expect(resourceType != null).assertTrue(); + callbackTimes = callbackTimes + 1; + wallpaper.off('wallpaperChange', async (wallpaperType, resourceType) => {}) + await wallpaper.setImage(URI, WALLPAPER_SYSTEM); + await wallpaper.restore(WALLPAPER_SYSTEM); + expect(callbackTimes === 1).assertTrue(); + done(); + }) + } catch (error) { + console.info(`offCallbackTest002 error : ${error.message}`); + expect(null).assertFail(); + done(); + } + await wallpaper.restore(WALLPAPER_SYSTEM); + }) + + /** + * @tc.name: offCallbackTest003 + * @tc.desc: Test wallpaperChange to log off a listener for wallpaper changes to + * receive notifications about the changes. + * @tc.type: FUNC test + * @tc.require: issueI5UHRG + */ + it('offCallbackTest003', 0, async function (done) { + try { + wallpaper.off('wallpaperChange', async (wallpaperType, resourceType) => { + }, 'other'); + expect(true).assertTrue(); + done(); + } catch (error) { + console.info(`offCallbackTest003 error : ${error.message}`); + expect(null).assertFail(); + done(); + } + }) + + /** + * @tc.name: offCallbackTest004 + * @tc.desc: Test wallpaperChange to log off a listener for wallpaper changes to + * receive notifications about the changes. + * @tc.type: FUNC test + * @tc.require: issueI5UHRG + */ + it('offCallbackTest004', 0, async function (done) { + try { + wallpaper.off('wallpaperChange'); + expect(true).assertTrue(); + done(); + } catch (error) { + console.info(`offCallbackTest004 error : ${error.message}`); + expect(null).assertFail(); + done(); + } + }) + + /** + * @tc.name: offCallbackTest005 + * @tc.desc: Test wallpaperChange to log off a listener for wallpaper changes to + * receive notifications about the changes. + * @tc.type: FUNC test + * @tc.require: issueI5UHRG + */ + it('offCallbackTest005', 0, async function (done) { + try { + wallpaper.off('wallpaperChange', 'other'); + expect(true).assertTrue(); + done(); + } catch (error) { + console.info(`offCallbackTest005 error : ${error.message}`); + expect(null).assertFail(); + done(); + } + }) + + /** + * @tc.name: offCallbackTest006 + * @tc.desc: Test not exist event wallpaperChangeX to off + * @tc.type: FUNC test + * @tc.require: issueI5UHRG + */ + it('offCallbackTest006', 0, async function (done) { + await wallpaper.setImage(URI, WALLPAPER_SYSTEM); + try { + wallpaper.off('wallpaperChangeX'); + expect(null).assertFail(); + done(); + } catch (error) { + console.info(`offCallbackTest006 error : ${error.message}`); + expect(error.code == PARAMETER_ERROR).assertTrue(); + done(); + } + await wallpaper.restore(WALLPAPER_SYSTEM); + }) + /** * @tc.name: setVideoTest001 * @tc.desc: Test setVideo to set live wallpaper. * @tc.type: FUNC test * @tc.require: issueI6R07J */ - it('setVideoTest001', 0, async function (done) { + it('setVideoTest001', 0, async function (done) { try { wallpaper.setVideo(URI_30FPS_3S_MP4, WALLPAPER_SYSTEM, (error) => { if (error != undefined) { diff --git a/frameworks/native/include/i_wallpaper_service.h b/frameworks/native/include/i_wallpaper_service.h index 5b3328f8487fd66621d12da57719cfa7fecf273d..2e386b07abff8298f0d85b1cefc6f50fe30b35f2 100644 --- a/frameworks/native/include/i_wallpaper_service.h +++ b/frameworks/native/include/i_wallpaper_service.h @@ -128,17 +128,17 @@ public: * Registers a listener for wallpaper event changes to receive notifications about the changes. * @param type event type * @param listener event observer. - * @return true or false + * @return error code */ - virtual bool On(const std::string &type, sptr listener) = 0; + virtual ErrorCode On(const std::string &type, sptr listener) = 0; /** * Unregisters a listener for wallpaper event changes. * @param type event type * @param listener event observer. - * @return true or false + * @return error code */ - virtual bool Off(const std::string &type, sptr listener) = 0; + virtual ErrorCode Off(const std::string &type, sptr listener) = 0; virtual bool RegisterWallpaperCallback(const sptr callback) = 0; diff --git a/frameworks/native/include/wallpaper_manager.h b/frameworks/native/include/wallpaper_manager.h index dc23d7f8a2ca36c87af656984c786304ad6ada40..65a245fff981c71da0fc3e83de5b7306c5249423 100644 --- a/frameworks/native/include/wallpaper_manager.h +++ b/frameworks/native/include/wallpaper_manager.h @@ -116,20 +116,20 @@ public: ErrorCode ResetWallpaper(std::int32_t wallpaperType, const ApiInfo &apiInfo) final; /** - * Registers a listener for wallpaper color changes to receive notifications about the changes. - * @param type The incoming colorChange table open receiver pick a color change wallpaper wallpaper color changes - * @param callback Provides dominant colors of the wallpaper. - * @return true or false + * Registers a listener for wallpaper event to receive notifications about the changes. + * @param type event type + * @param listener event listener + * @return error code */ - bool On(const std::string &type, std::shared_ptr listener) final; + ErrorCode On(const std::string &type, std::shared_ptr listener) final; /** - * Registers a listener for wallpaper color changes to receive notifications about the changes. - * @param type Incoming 'colorChange' table delete receiver to pick up a color change wallpaper wallpaper color - * changes - * @param callback Provides dominant colors of the wallpaper. + * Unregisters a listener for wallpaper event to receive notifications about the changes. + * @param type event type + * @param listener event listener + * @return error code */ - bool Off(const std::string &type, std::shared_ptr listener) final; + ErrorCode Off(const std::string &type, std::shared_ptr listener) final; /** * Sets live wallpaper of the specified type based on the uri path of the MP4 file. diff --git a/frameworks/native/include/wallpaper_manager_kits.h b/frameworks/native/include/wallpaper_manager_kits.h index f39d16280ec029825d8e01494deafc164a2576b1..79efb0447a453e893dea764da0f09ccc83bd4269 100644 --- a/frameworks/native/include/wallpaper_manager_kits.h +++ b/frameworks/native/include/wallpaper_manager_kits.h @@ -119,20 +119,20 @@ public: virtual ErrorCode ResetWallpaper(std::int32_t wallpaperType, const ApiInfo &apiInfo) = 0; /** - * Registers a listener for wallpaper color changes to receive notifications about the changes. - * @param type The incoming colorChange table open receiver pick a color change wallpaper wallpaper color changes - * @param callback Provides dominant colors of the wallpaper. - * @return true or false + * Registers a listener for wallpaper event to receive notifications about the changes. + * @param type event type + * @param listener event listener + * @return error code */ - virtual bool On(const std::string &type, std::shared_ptr listener) = 0; + virtual ErrorCode On(const std::string &type, std::shared_ptr listener) = 0; /** - * Registers a listener for wallpaper color changes to receive notifications about the changes. - * @param type Incoming 'colorChange' table delete receiver to pick up a color change wallpaper wallpaper color - * changes - * @param callback Provides dominant colors of the wallpaper. + * Unregisters a listener for wallpaper event to receive notifications about the changes. + * @param type event type + * @param listener event listener + * @return error code */ - virtual bool Off(const std::string &type, std::shared_ptr listener) = 0; + virtual ErrorCode Off(const std::string &type, std::shared_ptr listener) = 0; /** * Sets live wallpaper of the specified type based on the uri path of the MP4 file. diff --git a/frameworks/native/include/wallpaper_service_proxy.h b/frameworks/native/include/wallpaper_service_proxy.h index 6e11da0b710ed61f2597d5e80911839796809ed5..32a2458cef927e10daaab6a0476d477e6599cf14 100644 --- a/frameworks/native/include/wallpaper_service_proxy.h +++ b/frameworks/native/include/wallpaper_service_proxy.h @@ -42,8 +42,8 @@ public: bool IsChangePermitted() override; bool IsOperationAllowed() override; ErrorCode ResetWallpaper(int wallpaperType) override; - bool On(const std::string &type, sptr listener) override; - bool Off(const std::string &type, sptr listener) override; + ErrorCode On(const std::string &type, sptr listener) override; + ErrorCode Off(const std::string &type, sptr listener) override; bool RegisterWallpaperCallback(const sptr callback) override; ErrorCode SetVideo(int32_t fd, int32_t wallpaperType, int32_t length) override; ErrorCode SendEvent(const std::string &eventType) override; diff --git a/frameworks/native/src/wallpaper_manager.cpp b/frameworks/native/src/wallpaper_manager.cpp index b18428a87f401540e675cabbd2ea282851bd8c0e..4e8316dd2ce4048d699a516561fe427b20bb3405 100644 --- a/frameworks/native/src/wallpaper_manager.cpp +++ b/frameworks/native/src/wallpaper_manager.cpp @@ -331,6 +331,11 @@ ErrorCode WallpaperManager::GetPixelMap(int32_t wallpaperType, const ApiInfo &ap if (wallpaperErrorCode != E_OK) { return wallpaperErrorCode; } + // current wallpaper is live video, not image + if (fdInfo.size == 0 && fdInfo.fd == -1) { // 0: empty file size; -1: invalid file description + pixelMap = nullptr; + return E_OK; + } uint32_t errorCode = 0; OHOS::Media::SourceOptions opts; opts.formatHint = "image/jpeg"; @@ -426,54 +431,47 @@ ErrorCode WallpaperManager::ResetWallpaper(std::int32_t wallpaperType, const Api return wallpaperErrorCode; } -bool WallpaperManager::On(const std::string &type, std::shared_ptr listener) +ErrorCode WallpaperManager::On(const std::string &type, std::shared_ptr listener) { HILOG_DEBUG("WallpaperManager::On in"); auto wpServerProxy = GetService(); if (wpServerProxy == nullptr) { HILOG_ERROR("Get proxy failed"); - return false; + return E_SA_DIED; } if (listener == nullptr) { HILOG_ERROR("listener is nullptr."); - return false; + return E_DEAL_FAILED; } std::lock_guard lck(listenerMapMutex_); sptr ipcListener = new (std::nothrow) WallpaperEventListenerClient(listener); if (ipcListener == nullptr) { HILOG_ERROR("new WallpaperEventListenerClient failed"); - return false; + return E_NO_MEMORY; } HILOG_DEBUG("WallpaperManager::On out"); return wpServerProxy->On(type, ipcListener); } -bool WallpaperManager::Off(const std::string &type, std::shared_ptr listener) +ErrorCode WallpaperManager::Off(const std::string &type, std::shared_ptr listener) { HILOG_DEBUG("WallpaperManager::Off in"); auto wpServerProxy = GetService(); if (wpServerProxy == nullptr) { HILOG_ERROR("Get proxy failed"); - return false; + return E_SA_DIED; } std::lock_guard lck(listenerMapMutex_); - bool status = false; + sptr ipcListener = nullptr; if (listener != nullptr) { sptr ipcListener = new (std::nothrow) WallpaperEventListenerClient(listener); if (ipcListener == nullptr) { HILOG_ERROR("new WallpaperEventListenerClient failed"); - return false; + return E_NO_MEMORY; } - status = wpServerProxy->Off(type, ipcListener); - } else { - status = wpServerProxy->Off(type, nullptr); - } - if (!status) { - HILOG_ERROR("off failed"); - return false; } HILOG_DEBUG("WallpaperManager::Off out"); - return true; + return wpServerProxy->Off(type, ipcListener); } JScallback WallpaperManager::GetCallback() diff --git a/frameworks/native/src/wallpaper_service_proxy.cpp b/frameworks/native/src/wallpaper_service_proxy.cpp index 944006702be26c102ec896b922bf9e7c65acfc25..1a9315c7cb1f688203923836515df2a483dc1b5c 100644 --- a/frameworks/native/src/wallpaper_service_proxy.cpp +++ b/frameworks/native/src/wallpaper_service_proxy.cpp @@ -402,7 +402,7 @@ ErrorCode WallpaperServiceProxy::SendEvent(const std::string &eventType) return ConvertIntToErrorCode(reply.ReadInt32()); } -bool WallpaperServiceProxy::On(const std::string &type, sptr listener) +ErrorCode WallpaperServiceProxy::On(const std::string &type, sptr listener) { HILOG_DEBUG("WallpaperServiceProxy::On in"); MessageParcel data; @@ -410,34 +410,32 @@ bool WallpaperServiceProxy::On(const std::string &type, sptrAsObject())) { HILOG_ERROR("write subscribe type or parcel failed."); - return false; + return E_WRITE_PARCEL_ERROR; } int32_t result = Remote()->SendRequest(ON, data, reply, option); if (result != ERR_NONE) { HILOG_ERROR(" WallpaperServiceProxy::On fail, result = %{public}d ", result); - return false; + return E_DEAL_FAILED; } - int32_t status = reply.ReadInt32(); - bool ret = status == static_cast(E_OK); HILOG_DEBUG("WallpaperServiceProxy::On out"); - return ret; + return ConvertIntToErrorCode(reply.ReadInt32()); } -bool WallpaperServiceProxy::Off(const std::string &type, sptr listener) +ErrorCode WallpaperServiceProxy::Off(const std::string &type, sptr listener) { HILOG_DEBUG("WallpaperServiceProxy::Off in"); MessageParcel data; @@ -445,29 +443,27 @@ bool WallpaperServiceProxy::Off(const std::string &type, sptrAsObject())) { HILOG_ERROR("write subscribe type or parcel failed."); - return false; + return E_WRITE_PARCEL_ERROR; } } int32_t result = Remote()->SendRequest(OFF, data, reply, option); if (result != ERR_NONE) { HILOG_ERROR(" WallpaperServiceProxy::Off fail, result = %{public}d ", result); - return false; + return E_DEAL_FAILED; } - int32_t status = reply.ReadInt32(); - bool ret = status == static_cast(E_OK); HILOG_DEBUG("WallpaperServiceProxy::Off out"); - return ret; + return ConvertIntToErrorCode(reply.ReadInt32()); } bool WallpaperServiceProxy::RegisterWallpaperCallback(const sptr callback) diff --git a/services/include/wallpaper_service.h b/services/include/wallpaper_service.h index 3c312d8ff22e97c148c3040c23b22c32fff56f08..569e0c1c684ddc4561e186456efe47137baf40e4 100644 --- a/services/include/wallpaper_service.h +++ b/services/include/wallpaper_service.h @@ -58,6 +58,7 @@ class WallpaperService : public SystemAbility, public WallpaperServiceStub { DECLARE_SYSTEM_ABILITY(WallpaperService); enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; enum class FileType : uint8_t { WALLPAPER_FILE, CROP_FILE }; + using WallpaperListenerMap = std::map>; public: DISALLOW_COPY_AND_MOVE(WallpaperService); @@ -76,8 +77,8 @@ public: bool IsChangePermitted() override; bool IsOperationAllowed() override; ErrorCode ResetWallpaper(int32_t wallpaperType) override; - bool On(const std::string &type, sptr listener) override; - bool Off(const std::string &type, sptr listener) override; + ErrorCode On(const std::string &type, sptr listener) override; + ErrorCode Off(const std::string &type, sptr listener) override; bool RegisterWallpaperCallback(const sptr callback) override; int32_t Dump(int32_t fd, const std::vector &args) override; ErrorCode SetOffset(int32_t xOffset, int32_t yOffset) override; @@ -150,6 +151,7 @@ private: void OnColorsChange(WallpaperType wallpaperType, const ColorManager::Color &color); ErrorCode CheckValid(int32_t wallpaperType, int32_t length, WallpaperResourceType resourceType); bool WallpaperChanged(WallpaperType wallpaperType, WallpaperResourceType resType); + void NotifyColorChange(const std::vector &colors, const WallpaperType &wallpaperType); void StoreResType(); void LoadResType(); @@ -185,7 +187,7 @@ private: std::mutex mtx_; uint64_t lockWallpaperColor_; uint64_t systemWallpaperColor_; - std::map> colorChangeListenerMap_; + std::map wallpaperEventMap_; std::mutex listenerMapMutex_; int32_t pictureWidth_ = 0; int32_t pictureHeight_ = 0; diff --git a/services/src/wallpaper_service.cpp b/services/src/wallpaper_service.cpp index 1e540563a0ce34456b283cc8d1a0f00b46f8cbb1..bb5d5da99416c823550534bc28807d1cf24ade38 100644 --- a/services/src/wallpaper_service.cpp +++ b/services/src/wallpaper_service.cpp @@ -80,6 +80,7 @@ constexpr const char *SHOW_LOCK_SCREEN = "SHOW_LOCKSCREEN"; constexpr const char *SYSTEM_RES_TYPE = "SystemResType"; constexpr const char *LOCKSCREEN_RES_TYPE = "LockScreenResType"; constexpr const char *WALLPAPER_CHANGE = "wallpaperChange"; +constexpr const char *COLOR_CHANGE = "colorChange"; constexpr int64_t INIT_INTERVAL = 10000L; constexpr int64_t DELAY_TIME = 1000L; @@ -238,7 +239,7 @@ void WallpaperService::InitData() wallpaperCropPath_ = WALLPAPER_USERID_PATH + WALLPAPER_CROP_PICTURE; systemWallpaperColor_ = 0; lockWallpaperColor_ = 0; - colorChangeListenerMap_.clear(); + wallpaperEventMap_.clear(); InitUserDir(userId); LoadSettingsLocked(userId, true); InitResources(userId, WALLPAPER_SYSTEM); @@ -831,6 +832,10 @@ bool WallpaperService::SendWallpaperState() ErrorCode WallpaperService::SetVideo(int32_t fd, int32_t wallpaperType, int32_t length) { + if (!IsSystemApp()) { + HILOG_ERROR("current app is not SystemApp"); + return E_NOT_SYSTEM_APP; + } StartAsyncTrace(HITRACE_TAG_MISC, "SetVideo", static_cast(TraceTaskId::SET_VIDEO)); ErrorCode wallpaperErrorCode = SetWallpaper(fd, wallpaperType, length, VIDEO); FinishAsyncTrace(HITRACE_TAG_MISC, "SetVideo", static_cast(TraceTaskId::SET_VIDEO)); @@ -869,6 +874,15 @@ ErrorCode WallpaperService::GetPixelMap(int32_t wallpaperType, IWallpaperService auto type = static_cast(wallpaperType); int32_t userId = QueryActiveUserId(); HILOG_INFO("QueryCurrentOsAccount userId: %{public}d", userId); + // current user's wallpaper is live video, not image + WallpaperResourceType resType = + (type == WALLPAPER_SYSTEM ? resTypeMap_[SYSTEM_RES_TYPE] : resTypeMap_[LOCKSCREEN_RES_TYPE]); + if (resType != PICTURE) { + HILOG_ERROR("Current user's wallpaper is live video, not image"); + fdInfo.size = 0; // 0: empty file size + fdInfo.fd = -1; // -1: invalid file description + return E_OK; + } ErrorCode ret = GetImageSize(userId, type, fdInfo.size); if (ret != E_OK) { HILOG_ERROR("GetImageSize failed"); @@ -1030,31 +1044,42 @@ ErrorCode WallpaperService::SetDefaultDataForWallpaper(int32_t userId, Wallpaper return E_OK; } -bool WallpaperService::On(const std::string &type, sptr listener) +ErrorCode WallpaperService::On(const std::string &type, sptr listener) { HILOG_DEBUG("WallpaperService::On in"); if (listener == nullptr) { HILOG_ERROR("WallpaperService::On listener is null"); - return false; + return E_DEAL_FAILED; + } + if (type == WALLPAPER_CHANGE && !IsSystemApp()) { + HILOG_ERROR("current app is not SystemApp"); + return E_NOT_SYSTEM_APP; } std::lock_guard autoLock(listenerMapMutex_); - colorChangeListenerMap_[type] = listener; + wallpaperEventMap_[type].insert_or_assign(IPCSkeleton::GetCallingTokenID(), listener); HILOG_DEBUG("WallpaperService::On out"); - return true; + return E_OK; } -bool WallpaperService::Off(const std::string &type, sptr listener) +ErrorCode WallpaperService::Off(const std::string &type, sptr listener) { HILOG_DEBUG("WallpaperService::Off in"); (void)listener; + if (type == WALLPAPER_CHANGE && !IsSystemApp()) { + HILOG_ERROR("current app is not SystemApp"); + return E_NOT_SYSTEM_APP; + } std::lock_guard autoLock(listenerMapMutex_); - auto iter = colorChangeListenerMap_.find(type); - if (iter != colorChangeListenerMap_.end()) { - iter->second = nullptr; - colorChangeListenerMap_.erase(iter); + auto iter = wallpaperEventMap_.find(type); + if (iter != wallpaperEventMap_.end()) { + auto it = iter->second.find(IPCSkeleton::GetCallingTokenID()); + if (it != iter->second.end()) { + it->second = nullptr; + iter->second.erase(it); + } } HILOG_DEBUG("WallpaperService::Off out"); - return true; + return E_OK; } bool WallpaperService::RegisterWallpaperCallback(const sptr callback) @@ -1424,17 +1449,11 @@ void WallpaperService::OnColorsChange(WallpaperType wallpaperType, const ColorMa if (wallpaperType == WALLPAPER_SYSTEM && !CompareColor(systemWallpaperColor_, color)) { systemWallpaperColor_ = color.PackValue(); colors.emplace_back(systemWallpaperColor_); - std::lock_guard autoLock(listenerMapMutex_); - for (const auto &listener : colorChangeListenerMap_) { - listener.second->OnColorsChange(colors, WALLPAPER_SYSTEM); - } + NotifyColorChange(colors, WALLPAPER_SYSTEM); } else if (wallpaperType == WALLPAPER_LOCKSCREEN && !CompareColor(lockWallpaperColor_, color)) { lockWallpaperColor_ = color.PackValue(); colors.emplace_back(lockWallpaperColor_); - std::lock_guard autoLock(listenerMapMutex_); - for (const auto &listener : colorChangeListenerMap_) { - listener.second->OnColorsChange(colors, WALLPAPER_LOCKSCREEN); - } + NotifyColorChange(colors, WALLPAPER_LOCKSCREEN); } } @@ -1459,15 +1478,33 @@ ErrorCode WallpaperService::CheckValid(int32_t wallpaperType, int32_t length, Wa bool WallpaperService::WallpaperChanged(WallpaperType wallpaperType, WallpaperResourceType resType) { std::lock_guard autoLock(listenerMapMutex_); - auto it = colorChangeListenerMap_.find(WALLPAPER_CHANGE); - if (it != colorChangeListenerMap_.end() && it->second != nullptr) { - it->second->OnWallpaperChange(wallpaperType, resType); + auto it = wallpaperEventMap_.find(WALLPAPER_CHANGE); + if (it != wallpaperEventMap_.end()) { + for (auto iter = it->second.begin(); iter != it->second.end(); iter++) { + if (iter->second == nullptr) { + continue; + } + iter->second->OnWallpaperChange(wallpaperType, resType); + } return true; } - return false; } +void WallpaperService::NotifyColorChange(const std::vector &colors, const WallpaperType &wallpaperType) +{ + std::lock_guard autoLock(listenerMapMutex_); + auto it = wallpaperEventMap_.find(COLOR_CHANGE); + if (it != wallpaperEventMap_.end()) { + for (auto iter = it->second.begin(); iter != it->second.end(); iter++) { + if (iter->second == nullptr) { + continue; + } + iter->second->OnColorsChange(colors, wallpaperType); + } + } +} + void WallpaperService::StoreResType() { nlohmann::json root; diff --git a/services/src/wallpaper_service_stub.cpp b/services/src/wallpaper_service_stub.cpp index 9368255fef25c3f029e7568978707fe699d4d177..c6c7590104aec8ed7ae7586f5e1f4f26bd3d009d 100644 --- a/services/src/wallpaper_service_stub.cpp +++ b/services/src/wallpaper_service_stub.cpp @@ -383,9 +383,9 @@ int32_t WallpaperServiceStub::OnWallpaperOn(MessageParcel &data, MessageParcel & HILOG_ERROR("OnWallpaperOn nullptr after ipc"); return IPC_STUB_INVALID_DATA_ERR; } - sptr WallpaperListenerProxy = iface_cast(remote); - bool status = On(type, std::move(WallpaperListenerProxy)); - int32_t ret = status ? static_cast(E_OK) : static_cast(E_DEAL_FAILED); + sptr wallpaperListenerProxy = iface_cast(remote); + ErrorCode errorCode = On(type, std::move(wallpaperListenerProxy)); + int32_t ret = static_cast(errorCode); if (!reply.WriteInt32(ret)) { HILOG_ERROR("WriteInt32 failed"); return IPC_STUB_WRITE_PARCEL_ERR; @@ -403,14 +403,14 @@ int32_t WallpaperServiceStub::OnWallpaperOff(MessageParcel &data, MessageParcel return IPC_STUB_INVALID_DATA_ERR; } sptr remote = data.ReadRemoteObject(); - bool status = false; + ErrorCode errorCode = E_UNKNOWN; if (remote == nullptr) { - status = Off(type, nullptr); + errorCode = Off(type, nullptr); } else { - sptr WallpaperListenerProxy = iface_cast(remote); - status = Off(type, std::move(WallpaperListenerProxy)); + sptr wallpaperListenerProxy = iface_cast(remote); + errorCode = Off(type, std::move(wallpaperListenerProxy)); } - int32_t ret = status ? static_cast(E_OK) : static_cast(E_DEAL_FAILED); + int32_t ret = static_cast(errorCode); if (!reply.WriteInt32(ret)) { HILOG_ERROR("WriteInt32 failed"); return IPC_STUB_WRITE_PARCEL_ERR; diff --git a/test/unittest/wallpaper_test.cpp b/test/unittest/wallpaper_test.cpp index 87fd27dd7691a3fc7bc763b54914b6e2f2ec29ad..5c60f390e9867e4a223bc309e13eb8dafeb60bef 100644 --- a/test/unittest/wallpaper_test.cpp +++ b/test/unittest/wallpaper_test.cpp @@ -141,8 +141,8 @@ public: WallpaperEventListenerTestImpl &operator=(WallpaperEventListenerTestImpl &&) = delete; // callback function will be called when the db data is changed. - void OnColorsChange(const std::vector &color, int wallpaperType); - + void OnColorsChange(const std::vector &color, int wallpaperType) override; + void OnWallpaperChange(WallpaperType wallpaperType, WallpaperResourceType resourceType) override; // reset the callCount_ to zero. void ResetToZero(); @@ -161,6 +161,11 @@ void WallpaperEventListenerTestImpl::OnColorsChange(const std::vector wallpaperType_ = wallpaperType; } +void WallpaperEventListenerTestImpl::OnWallpaperChange(WallpaperType wallpaperType, WallpaperResourceType resourceType) +{ + HILOG_INFO("wallpaperType: %{public}d, resourceType: %{public}d", static_cast(wallpaperType), static_cast(resourceType)); +} + WallpaperEventListenerTestImpl::WallpaperEventListenerTestImpl() { callCount_ = 0; @@ -380,10 +385,27 @@ HWTEST_F(WallpaperTest, On001, TestSize.Level1) { HILOG_INFO("On001 begin"); auto listener = std::make_shared(); - WallpaperManagerkits::GetInstance().On("colorChange", listener); - + auto status = WallpaperManagerkits::GetInstance().On("colorChange", listener); + EXPECT_EQ(status, E_OK) << "subscribe wallpaper color change failed."; auto offSubStatus = WallpaperManagerkits::GetInstance().Off("colorChange", listener); - EXPECT_EQ(offSubStatus, true) << "unsubscribe wallpaper color change."; + EXPECT_EQ(offSubStatus, E_OK) << "unsubscribe wallpaper color change failed."; +} + +/** +* @tc.name: On002 +* @tc.desc: set wallpaper and get callback. +* @tc.type: FUNC +* @tc.require: +* @tc.author: +*/ +HWTEST_F(WallpaperTest, On002, TestSize.Level1) +{ + HILOG_INFO("On002 begin"); + auto listener = std::make_shared(); + auto status = WallpaperManagerkits::GetInstance().On("wallpaperChange", listener); + EXPECT_EQ(status, E_NOT_SYSTEM_APP) << "subscribe wallpaper change."; + auto offSubStatus = WallpaperManagerkits::GetInstance().Off("wallpaperChange", listener); + EXPECT_EQ(offSubStatus, E_NOT_SYSTEM_APP) << "unsubscribe wallpaper change."; } /********************* On & Off *********************/ @@ -1029,7 +1051,7 @@ HWTEST_F(WallpaperTest, SetVideo003, TestSize.Level0) { HILOG_INFO("SetVideo003 begin"); ErrorCode ret = WallpaperManagerkits::GetInstance().SetVideo(URI_30FPS_3S_MP4, SYSTYEM); - EXPECT_EQ(ret, E_OK); + EXPECT_EQ(ret, E_NOT_SYSTEM_APP); } /** @@ -1045,6 +1067,59 @@ HWTEST_F(WallpaperTest, SetVideo004, TestSize.Level0) EXPECT_EQ(ret, E_PARAMETERS_INVALID); } +/** + * @tc.name: SetVideo005 + * @tc.desc: SetVideo input error fileType + * @tc.type: FUNC + * @tc.require: issueI6R07J + */ +HWTEST_F(WallpaperTest, SetVideo005, TestSize.Level0) +{ + HILOG_INFO("SetVideo005 begin"); + ErrorCode ret = WallpaperManagerkits::GetInstance().SetVideo(URI_30FPS_3S_MOV, LOCKSCREEN); + EXPECT_EQ(ret, E_PARAMETERS_INVALID); +} + +/** + * @tc.name: SetVideo006 + * @tc.desc: SetVideo input error uri + * @tc.type: FUNC + * @tc.require: issueI6R07J + */ +HWTEST_F(WallpaperTest, SetVideo006, TestSize.Level0) +{ + HILOG_INFO("SetVideo006 begin"); + std::string errUri = "errorPath/zm_30fps_4s.mp4"; + ErrorCode ret = WallpaperManagerkits::GetInstance().SetVideo(errUri, LOCKSCREEN); + EXPECT_EQ(ret, E_FILE_ERROR); +} + +/** + * @tc.name: SetVideo007 + * @tc.desc: SetVideo input correct parameter + * @tc.type: FUNC + * @tc.require: issueI6R07J + */ +HWTEST_F(WallpaperTest, SetVideo007, TestSize.Level0) +{ + HILOG_INFO("SetVideo007 begin"); + ErrorCode ret = WallpaperManagerkits::GetInstance().SetVideo(URI_30FPS_3S_MP4, LOCKSCREEN); + EXPECT_EQ(ret, E_NOT_SYSTEM_APP); +} + +/** + * @tc.name: SetVideo008 + * @tc.desc: SetVideo input error duration + * @tc.type: FUNC + * @tc.require: issueI6R07J + */ +HWTEST_F(WallpaperTest, SetVideo008, TestSize.Level0) +{ + HILOG_INFO("SetVideo008 begin"); + ErrorCode ret = WallpaperManagerkits::GetInstance().SetVideo(URI_15FPS_7S_MP4, LOCKSCREEN); + EXPECT_EQ(ret, E_PARAMETERS_INVALID); +} + /********************* SetVideo *********************/ /********************* SendEvent *********************/