1 /*
2  * Copyright (c) 2022 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 "audio_manager_privacy_proxy.h"
17 
18 #include "accesstoken_log.h"
19 #include "audio_policy_ipc_interface_code.h"
20 
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_PRIVACY, "AudioManagerPrivacyProxy"};
26 static constexpr int32_t ERROR = -1;
27 }
28 
GetPersistentMicMuteState()29 bool AudioManagerPrivacyProxy::GetPersistentMicMuteState()
30 {
31     MessageParcel data;
32     MessageParcel reply;
33     MessageOption option;
34     if (!data.WriteInterfaceToken(GetDescriptor())) {
35         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed");
36         return false;
37     }
38     sptr<IRemoteObject> remote = Remote();
39     if (remote == nullptr) {
40         ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
41         return false;
42     }
43     int32_t error = remote->SendRequest(static_cast<uint32_t>(
44         AudioStandard::AudioPolicyInterfaceCode::GET_MICROPHONE_MUTE_PERSISTENT), data, reply, option);
45     if (error != ERR_NONE) {
46         ACCESSTOKEN_LOG_ERROR(LABEL, "GetPersistentMicMuteState failed, error: %{public}d", error);
47         return false;
48     }
49     bool isMute = reply.ReadBool();
50     ACCESSTOKEN_LOG_INFO(LABEL, "Mic mute state: %{public}d", isMute);
51     return isMute;
52 }
53 
SetMicrophoneMutePersistent(const bool isMute,const PolicyType type)54 int32_t AudioManagerPrivacyProxy::SetMicrophoneMutePersistent(const bool isMute, const PolicyType type)
55 {
56     MessageParcel data;
57     MessageParcel reply;
58     MessageOption option;
59     if (!data.WriteInterfaceToken(GetDescriptor())) {
60         ACCESSTOKEN_LOG_ERROR(LABEL, "WriteInterfaceToken failed");
61         return ERROR;
62     }
63     data.WriteBool(isMute);
64     data.WriteInt32(static_cast<int32_t>(type));
65     sptr<IRemoteObject> remote = Remote();
66     if (remote == nullptr) {
67         ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
68         return ERROR;
69     }
70     int32_t error = remote->SendRequest(static_cast<uint32_t>(
71         AudioStandard::AudioPolicyInterfaceCode::SET_MICROPHONE_MUTE_PERSISTENT), data, reply, option);
72     if (error != ERR_NONE) {
73         ACCESSTOKEN_LOG_ERROR(LABEL, "Set microphoneMute failed, error: %d", error);
74         return error;
75     }
76     int32_t ret = reply.ReadInt32();
77     ACCESSTOKEN_LOG_INFO(LABEL, "Set mute result: %{public}d", ret);
78     return ret;
79 }
80 } // namespace AccessToken
81 } // namespace Security
82 } // namespace OHOS
83