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 "system_tone_options_napi.h"
17 #include "system_sound_log.h"
18
19 using namespace std;
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO_NAPI, "SystemToneOptionsNapi"};
23 }
24
25 namespace OHOS {
26 namespace Media {
27 thread_local napi_ref SystemToneOptionsNapi::sConstructor_ = nullptr;
28
29 bool SystemToneOptionsNapi::sMuteAudio_ = false;
30 bool SystemToneOptionsNapi::sMuteHaptics_ = false;
31
SystemToneOptionsNapi()32 SystemToneOptionsNapi::SystemToneOptionsNapi()
33 : env_(nullptr) {
34 }
35
36 SystemToneOptionsNapi::~SystemToneOptionsNapi() = default;
37
Destructor(napi_env env,void * nativeObject,void * finalize_hint)38 void SystemToneOptionsNapi::Destructor(napi_env env, void *nativeObject, void *finalize_hint)
39 {
40 if (nativeObject != nullptr) {
41 auto obj = static_cast<SystemToneOptionsNapi *>(nativeObject);
42 delete obj;
43 obj = nullptr;
44 }
45 }
46
Init(napi_env env,napi_value exports)47 napi_value SystemToneOptionsNapi::Init(napi_env env, napi_value exports)
48 {
49 napi_status status;
50 napi_value constructor;
51 napi_value result = nullptr;
52 napi_get_undefined(env, &result);
53
54 napi_property_descriptor system_tone_options_properties[] = {
55 DECLARE_NAPI_GETTER_SETTER("muteAudio", IsAudioMute, SetAudioMute),
56 DECLARE_NAPI_GETTER_SETTER("muteHaptics", IsHapticsMute, SetHapticsMute)
57 };
58
59 status = napi_define_class(env, SYSTEM_TONE_OPTIONS_NAPI_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Construct,
60 nullptr, sizeof(system_tone_options_properties) / sizeof(system_tone_options_properties[0]),
61 system_tone_options_properties, &constructor);
62 if (status != napi_ok) {
63 return result;
64 }
65
66 status = napi_create_reference(env, constructor, 1, &sConstructor_);
67 if (status == napi_ok) {
68 status = napi_set_named_property(env, exports, SYSTEM_TONE_OPTIONS_NAPI_CLASS_NAME.c_str(), constructor);
69 if (status == napi_ok) {
70 return exports;
71 }
72 }
73 MEDIA_LOGE("Failure in SystemToneOptionsNapi::Init()");
74
75 return result;
76 }
77
Construct(napi_env env,napi_callback_info info)78 napi_value SystemToneOptionsNapi::Construct(napi_env env, napi_callback_info info)
79 {
80 napi_status status;
81 napi_value jsThis = nullptr;
82 size_t argCount = 0;
83
84 status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
85 if (status == napi_ok) {
86 unique_ptr<SystemToneOptionsNapi> obj = make_unique<SystemToneOptionsNapi>();
87 if (obj != nullptr) {
88 obj->env_ = env;
89 obj->muteAudio_ = sMuteAudio_;
90 obj->muteHaptics_ = sMuteHaptics_;
91 status = napi_wrap(env, jsThis, static_cast<void*>(obj.get()),
92 SystemToneOptionsNapi::Destructor, nullptr, nullptr);
93 if (status == napi_ok) {
94 obj.release();
95 return jsThis;
96 }
97 }
98 }
99
100 MEDIA_LOGE("Failed in SystemToneOptionsNapi::Construct()!");
101 napi_get_undefined(env, &jsThis);
102
103 return jsThis;
104 }
105
CreateSystemToneOptionsWrapper(napi_env env,bool muteAudio,bool muteHaptics)106 napi_value SystemToneOptionsNapi::CreateSystemToneOptionsWrapper(napi_env env, bool muteAudio, bool muteHaptics)
107 {
108 napi_status status;
109 napi_value result = nullptr;
110 napi_value constructor;
111
112 status = napi_get_reference_value(env, sConstructor_, &constructor);
113 if (status == napi_ok) {
114 sMuteAudio_ = muteAudio;
115 sMuteHaptics_ = muteHaptics;
116 status = napi_new_instance(env, constructor, 0, nullptr, &result);
117 if (status == napi_ok) {
118 return result;
119 }
120 }
121 MEDIA_LOGE("Failed in CreateSystemToneOptionsWrapper, %{public}d", status);
122
123 napi_get_undefined(env, &result);
124
125 return result;
126 }
127
IsAudioMute(napi_env env,napi_callback_info info)128 napi_value SystemToneOptionsNapi::IsAudioMute(napi_env env, napi_callback_info info)
129 {
130 napi_status status;
131 SystemToneOptionsNapi *systemToneOptionsNapi = nullptr;
132 size_t argc = 0;
133 napi_value jsThis = nullptr;
134 napi_value jsResult = nullptr;
135 napi_get_undefined(env, &jsResult);
136
137 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
138 if (status != napi_ok || jsThis == nullptr) {
139 MEDIA_LOGE("IsAudioMute: failed for napi_get_cb_info");
140 return jsResult;
141 }
142
143 status = napi_unwrap(env, jsThis, (void **)&systemToneOptionsNapi);
144 if (status == napi_ok) {
145 status = napi_get_boolean(env, systemToneOptionsNapi->muteAudio_, &jsResult);
146 if (status == napi_ok) {
147 return jsResult;
148 }
149 }
150
151 return jsResult;
152 }
153
SetAudioMute(napi_env env,napi_callback_info info)154 napi_value SystemToneOptionsNapi::SetAudioMute(napi_env env, napi_callback_info info)
155 {
156 napi_status status;
157 SystemToneOptionsNapi *systemToneOptionsNapi = nullptr;
158 size_t argc = 1;
159 napi_value args[1] = { nullptr };
160 napi_value jsThis = nullptr;
161 bool muteAudio = false;
162 napi_value jsResult = nullptr;
163 napi_get_undefined(env, &jsResult);
164
165 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
166 if (status != napi_ok || jsThis == nullptr || argc < 1) {
167 MEDIA_LOGE("SetAudioMute: failed for napi_get_cb_info");
168 return jsResult;
169 }
170
171 status = napi_unwrap(env, jsThis, (void **)&systemToneOptionsNapi);
172 if (status == napi_ok) {
173 napi_valuetype valueType = napi_undefined;
174 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_boolean) {
175 MEDIA_LOGE("SetAudioMute: failed for wrong data type");
176 return jsResult;
177 }
178 }
179
180 status = napi_get_value_bool(env, args[0], &muteAudio);
181 if (status == napi_ok) {
182 systemToneOptionsNapi->muteAudio_ = muteAudio;
183 }
184
185 return jsResult;
186 }
187
IsHapticsMute(napi_env env,napi_callback_info info)188 napi_value SystemToneOptionsNapi::IsHapticsMute(napi_env env, napi_callback_info info)
189 {
190 napi_status status;
191 SystemToneOptionsNapi *systemToneOptionsNapi = nullptr;
192 size_t argc = 0;
193 napi_value jsThis = nullptr;
194 napi_value jsResult = nullptr;
195 napi_get_undefined(env, &jsResult);
196
197 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
198 if (status != napi_ok || jsThis == nullptr) {
199 MEDIA_LOGE("IsHapticsMute: failed for napi_get_cb_info");
200 return jsResult;
201 }
202
203 status = napi_unwrap(env, jsThis, (void **)&systemToneOptionsNapi);
204 if (status == napi_ok) {
205 status = napi_get_boolean(env, systemToneOptionsNapi->muteHaptics_, &jsResult);
206 if (status == napi_ok) {
207 return jsResult;
208 }
209 }
210
211 return jsResult;
212 }
213
SetHapticsMute(napi_env env,napi_callback_info info)214 napi_value SystemToneOptionsNapi::SetHapticsMute(napi_env env, napi_callback_info info)
215 {
216 napi_status status;
217 SystemToneOptionsNapi *ringtoneOptionsNapi = nullptr;
218 size_t argc = 1;
219 napi_value args[1] = { nullptr };
220 napi_value jsThis = nullptr;
221 bool muteHaptics = false;
222 napi_value jsResult = nullptr;
223 napi_get_undefined(env, &jsResult);
224
225 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
226 if (status != napi_ok || jsThis == nullptr || argc < 1) {
227 MEDIA_LOGE("SetHapticsMute: failed for napi_get_cb_info");
228 return jsResult;
229 }
230
231 status = napi_unwrap(env, jsThis, (void **)&ringtoneOptionsNapi);
232 if (status == napi_ok) {
233 napi_valuetype valueType = napi_undefined;
234 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_boolean) {
235 MEDIA_LOGE("SetHapticsMute: failed for wrong data type");
236 return jsResult;
237 }
238 }
239
240 status = napi_get_value_bool(env, args[0], &muteHaptics);
241 if (status == napi_ok) {
242 ringtoneOptionsNapi->muteHaptics_ = muteHaptics;
243 }
244
245 return jsResult;
246 }
247 } // namespace Media
248 } // namespace OHOS
249