• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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,bool newInterface)21 bool AnsDialogHostClient::CreateIfNullptr(sptr<AnsDialogHostClient>& result, bool newInterface)
22 {
23     ANS_LOGD("called");
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     AnsDialogHostClient::newInterface_ = newInterface;
31     result = AnsDialogHostClient::instance_;
32     return result != nullptr;
33 }
34 
GetInstance()35 sptr<AnsDialogHostClient> AnsDialogHostClient::GetInstance()
36 {
37     std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_);
38     return AnsDialogHostClient::instance_;
39 }
40 
Destroy()41 void AnsDialogHostClient::Destroy()
42 {
43     std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_);
44     AnsDialogHostClient::instance_ = nullptr;
45 }
46 
SetDialogCallbackInterface(std::unique_ptr<AnsDialogCallbackNativeInterface> dialogCallbackInterface)47 bool AnsDialogHostClient::SetDialogCallbackInterface(
48     std::unique_ptr<AnsDialogCallbackNativeInterface> dialogCallbackInterface)
49 {
50     ANS_LOGD("called");
51     std::lock_guard<std::mutex> lock(AnsDialogHostClient::instanceMutex_);
52     if (dialogCallbackInterface == nullptr || AnsDialogHostClient::instance_ == nullptr) {
53         return false;
54     }
55     AnsDialogHostClient::instance_->dialogCallbackInterface_ = std::move(dialogCallbackInterface);
56     return true;
57 }
58 
OnDialogStatusChanged(const DialogStatusData & statusData)59 ErrCode AnsDialogHostClient::OnDialogStatusChanged(const DialogStatusData& statusData)
60 {
61     ANS_LOGD("called");
62     if (dialogCallbackInterface_ == nullptr) {
63         ANS_LOGE("AnsDialogCallbackNativeInterface is null.");
64         return ERR_OK;
65     }
66     if (hasBeenCalled.exchange(true) && !AnsDialogHostClient::newInterface_) {
67         ANS_LOGE("Has been called.");
68         return ERR_INVALID_DATA;
69     }
70     dialogCallbackInterface_->ProcessDialogStatusChanged(statusData);
71     return ERR_OK;
72 }
73 } // namespace OHOS::Notification
74