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)21 bool AnsDialogHostClient::CreateIfNullptr(sptr<AnsDialogHostClient>& result)
22 {
23     ANS_LOGD("enter");
24     std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_);
25     if (instance_ != nullptr) {
26         return false;
27     }
28     AnsDialogHostClient::instance_ = new (std::nothrow) AnsDialogHostClient();
29     result = AnsDialogHostClient::instance_;
30     return result != nullptr;
31 }
32 
GetInstance()33 sptr<AnsDialogHostClient> AnsDialogHostClient::GetInstance()
34 {
35     std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_);
36     return AnsDialogHostClient::instance_;
37 }
38 
Destroy()39 void AnsDialogHostClient::Destroy()
40 {
41     std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_);
42     AnsDialogHostClient::instance_ = nullptr;
43 }
44 
SetDialogCallbackInterface(std::unique_ptr<AnsDialogCallbackNativeInterface> dialogCallbackInterface)45 bool AnsDialogHostClient::SetDialogCallbackInterface(
46     std::unique_ptr<AnsDialogCallbackNativeInterface> dialogCallbackInterface)
47 {
48     ANS_LOGD("enter");
49     std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_);
50     if (dialogCallbackInterface == nullptr || AnsDialogHostClient::instance_ == nullptr) {
51         return false;
52     }
53     AnsDialogHostClient::instance_->dialogCallbackInterface_ = std::move(dialogCallbackInterface);
54     return true;
55 }
56 
OnDialogStatusChanged(const DialogStatusData & statusData)57 void AnsDialogHostClient::OnDialogStatusChanged(const DialogStatusData& statusData)
58 {
59     ANS_LOGD("enter");
60     if (dialogCallbackInterface_ == nullptr) {
61         ANS_LOGE("AnsDialogCallbackNativeInterface is null.");
62         return;
63     }
64     if (hasBeenCalled.exchange(true)) {
65         ANS_LOGE("Has been called.");
66         return;
67     }
68     dialogCallbackInterface_->ProcessDialogStatusChanged(statusData);
69     AnsDialogHostClient::Destroy();
70 }
71 } // namespace OHOS::Notification
72