1 /*
2  * Copyright (c) 2024 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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioDialogAbilityConnection"
17 #endif
18 
19 #include "audio_dialog_ability_connection.h"
20 #include "audio_policy_service.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
24 std::atomic<bool> AudioDialogAbilityConnection::isDialogDestroy_(true);
25 std::condition_variable AudioDialogAbilityConnection::dialogCondition_;
26 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)27 void AudioDialogAbilityConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
28     const sptr<IRemoteObject> &remoteObject, int resultCode)
29 {
30     AUDIO_INFO_LOG("element: %{public}s", element.GetURI().c_str());
31     std::unique_lock<std::mutex> lock(mutex_);
32     CHECK_AND_RETURN_LOG(remoteObject != nullptr, "remoteObject is nullptr");
33 
34     if (!isDialogDestroy_.load()) {
35         auto status = dialogCondition_.wait_for(lock, std::chrono::seconds(WAIT_DIALOG_CLOSE_TIME_S),
36             [] () { return isDialogDestroy_.load(); });
37         if (status) {
38             AUDIO_INFO_LOG("wait safe dialog close failed.");
39             return;
40         }
41     }
42     MessageParcel data;
43     MessageParcel reply;
44     MessageOption option;
45     data.WriteInt32(MESSAGE_PARCEL_KEY_SIZE);
46     data.WriteString16(u"bundleName");
47     data.WriteString16(u"com.huawei.hmos.mediacontroller");
48     data.WriteString16(u"abilityName");
49     data.WriteString16(u"SafeVolumeAbility");
50     data.WriteString16(u"parameters");
51     nlohmann::json param;
52     param["ability.want.params.uiExtensionType"] = "sys/commonUI";
53     param["sysDialogZOrder"] = SYS_DIALOG_TYPE_UPPER;
54     std::string paramStr = param.dump();
55     data.WriteString16(Str8ToStr16(paramStr));
56     int32_t errCode = remoteObject->SendRequest(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
57     CHECK_AND_RETURN_LOG(errCode == SUCCESS, "send ON_ABILITY_CONNECT_DONE failed");
58     isDialogDestroy_.store(false);
59 }
60 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)61 void AudioDialogAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
62 {
63     AUDIO_INFO_LOG("element: %{public}s, resultCode:%{public}d", element.GetURI().c_str(), resultCode);
64     {
65         std::lock_guard<std::mutex> lock(mutex_);
66         isDialogDestroy_.store(true);
67         dialogCondition_.notify_all();
68     }
69     AudioPolicyService::GetAudioPolicyService().SafeVolumeDialogDisapper();
70 }
71 } // namespace AudioStandard
72 } // namespace OHOS