• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef COUNTRY_CODE_MANAGER_H
17 #define COUNTRY_CODE_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <singleton.h>
22 #include <string>
23 
24 #include "iremote_stub.h"
25 #include "common_event_subscriber.h"
26 
27 #include "country_code.h"
28 #include "i_locator_callback.h"
29 #include "i_country_code_callback.h"
30 #include "location.h"
31 
32 namespace OHOS {
33 namespace Location {
34 class CountryCodeManager {
35 public:
36     CountryCodeManager();
37     ~CountryCodeManager();
38     std::shared_ptr<CountryCode> GetIsoCountryCode();
39     void UnregisterCountryCodeCallback(const sptr<IRemoteObject>& callback);
40     void RegisterCountryCodeCallback(const sptr<IRemoteObject>& callback, pid_t uid);
41     void ReSubscribeEvent();
42     void ReUnsubscribeEvent();
43     bool IsCountryCodeRegistered();
44     static CountryCodeManager* GetInstance();
45 
46 private:
47     std::string GetCountryCodeByLocation(const std::unique_ptr<Location>& location);
48     bool UpdateCountryCodeByLocation(std::string countryCode, int type);
49     void UpdateCountryCode(std::string countryCode, int type);
50     std::string GetCountryCodeByLastLocation();
51     void NotifyAllListener();
52     bool SubscribeSimEvent();
53     bool SubscribeNetworkStatusEvent();
54     bool SubscribeLocaleConfigEvent();
55     bool UnsubscribeSimEvent();
56     bool UnsubscribeNetworkStatusEvent();
57 
58     class LocatorCallback : public IRemoteStub<ILocatorCallback> {
59     public:
60         void OnLocationReport(const std::unique_ptr<Location>& location);
61         void OnLocatingStatusChange(const int status);
62         void OnErrorReport(const int errorCode);
63     };
64 
65     class NetworkSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
66     public:
67         explicit NetworkSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &info);
68         ~NetworkSubscriber() override = default;
69     private:
70         void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &event) override;
71     };
72 
73     class SimSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
74     public:
75         explicit SimSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &info);
76         ~SimSubscriber() override = default;
77     private:
78         virtual void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &event) override;
79     };
80 
81     std::shared_ptr<CountryCode> lastCountryByLocation_;
82     std::shared_ptr<CountryCode> lastCountry_;
83     std::vector<sptr<ICountryCodeCallback>> countryCodeCallbacks_;
84     std::shared_ptr<SimSubscriber> simSubscriber_;
85     std::shared_ptr<NetworkSubscriber> networkSubscriber_;
86     std::mutex simSubscriberMutex_;
87     std::mutex networkSubscriberMutex_;
88     std::mutex countryCodeCallbackMutex_;
89 };
90 } // namespace Location
91 } // namespace OHOS
92 #endif // COUNTRY_CODE_MANAGER_H
93