1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "ans_dialog_host_client.h" 17 18 #include "ans_log_wrapper.h" 19 20 namespace OHOS::Notification { CreateIfNullptr(sptr<AnsDialogHostClient> & result)21bool AnsDialogHostClient::CreateIfNullptr(sptr<AnsDialogHostClient>& result) 22 { 23 ANS_LOGD("enter"); 24 std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_); 25 if (instance_ != nullptr) { 26 result = instance_; 27 return false; 28 } 29 AnsDialogHostClient::instance_ = new (std::nothrow) AnsDialogHostClient(); 30 result = AnsDialogHostClient::instance_; 31 return result != nullptr; 32 } 33 GetInstance()34sptr<AnsDialogHostClient> AnsDialogHostClient::GetInstance() 35 { 36 std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_); 37 return AnsDialogHostClient::instance_; 38 } 39 Destroy()40void AnsDialogHostClient::Destroy() 41 { 42 std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_); 43 AnsDialogHostClient::instance_ = nullptr; 44 } 45 SetDialogCallbackInterface(std::unique_ptr<AnsDialogCallbackNativeInterface> dialogCallbackInterface)46bool AnsDialogHostClient::SetDialogCallbackInterface( 47 std::unique_ptr<AnsDialogCallbackNativeInterface> dialogCallbackInterface) 48 { 49 ANS_LOGD("enter"); 50 std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_); 51 if (dialogCallbackInterface == nullptr || AnsDialogHostClient::instance_ == nullptr) { 52 return false; 53 } 54 AnsDialogHostClient::instance_->dialogCallbackInterface_ = std::move(dialogCallbackInterface); 55 return true; 56 } 57 OnDialogStatusChanged(const DialogStatusData & statusData)58void AnsDialogHostClient::OnDialogStatusChanged(const DialogStatusData& statusData) 59 { 60 ANS_LOGD("enter"); 61 if (dialogCallbackInterface_ == nullptr) { 62 ANS_LOGE("AnsDialogCallbackNativeInterface is null."); 63 return; 64 } 65 if (hasBeenCalled.exchange(true)) { 66 ANS_LOGE("Has been called."); 67 return; 68 } 69 dialogCallbackInterface_->ProcessDialogStatusChanged(statusData); 70 } 71 } // namespace OHOS::Notification 72