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
16 #include "location_log.h"
17 #include "cj_lambda.h"
18 #include "country_code_callback.h"
19
20 namespace OHOS {
21 namespace GeoLocationManager {
CountryCodeCallback()22 CountryCodeCallback::CountryCodeCallback()
23 {
24 }
25
CountryCodeCallback(int64_t callbackId)26 CountryCodeCallback::CountryCodeCallback(int64_t callbackId)
27 {
28 this->callbackId_ = callbackId;
29 auto cFunc = reinterpret_cast<void(*)(CJCountryCode countryCode)>(callbackId);
30 callback_ = [ lambda = CJLambda::Create(cFunc)](const std::shared_ptr<Location::CountryCode>& country) ->
31 void { lambda(CountryCodeToCJCountryCode(country)); };
32 }
33
~CountryCodeCallback()34 CountryCodeCallback::~CountryCodeCallback()
35 {
36 }
37
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 int CountryCodeCallback::OnRemoteRequest(
39 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
40 {
41 LBSLOGD(Location::COUNTRY_CODE_CALLBACK, "CountryCodeCallback::OnRemoteRequest!");
42 if (data.ReadInterfaceToken() != GetDescriptor()) {
43 LBSLOGE(Location::COUNTRY_CODE_CALLBACK, "invalid token.");
44 return -1;
45 }
46
47 switch (code) {
48 case Location::ICountryCodeCallback::COUNTRY_CODE_CHANGE_EVENT: {
49 auto countryCodePtr = Location::CountryCode::Unmarshalling(data);
50 OnCountryCodeChange(countryCodePtr);
51 break;
52 }
53 default: {
54 IPCObjectStub::OnRemoteRequest(code, data, reply, option);
55 break;
56 }
57 }
58 return 0;
59 }
60
OnCountryCodeChange(const std::shared_ptr<Location::CountryCode> & country)61 void CountryCodeCallback::OnCountryCodeChange(const std::shared_ptr<Location::CountryCode>& country)
62 {
63 LBSLOGD(Location::COUNTRY_CODE_CALLBACK, "CountryCodeCallback::OnCountryCodeChange");
64 std::unique_lock<std::mutex> guard(mutex_);
65 if (callback_ != nullptr) {
66 callback_(country);
67 }
68 }
69 }
70 }