• 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 : public DelayedSingleton<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 
45 private:
46     std::string GetCountryCodeByLocation(const std::unique_ptr<Location>& location);
47     bool UpdateCountryCodeByLocation(std::string countryCode, int type);
48     void UpdateCountryCode(std::string countryCode, int type);
49     std::string GetCountryCodeByLastLocation();
50     void NotifyAllListener();
51     bool SubscribeSimEvent();
52     bool SubscribeNetworkStatusEvent();
53     bool SubscribeLocaleConfigEvent();
54     bool UnsubscribeSimEvent();
55     bool UnsubscribeNetworkStatusEvent();
56 
57     class LocatorCallback : public IRemoteStub<ILocatorCallback> {
58     public:
59         void OnLocationReport(const std::unique_ptr<Location>& location);
60         void OnLocatingStatusChange(const int status);
61         void OnErrorReport(const int errorCode);
62     };
63 
64     class NetworkSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
65     public:
66         explicit NetworkSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &info);
67         ~NetworkSubscriber() override = default;
68     private:
69         void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &event) override;
70     };
71 
72     class SimSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
73     public:
74         explicit SimSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &info);
75         ~SimSubscriber() override = default;
76     private:
77         virtual void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &event) override;
78     };
79 
80     std::shared_ptr<CountryCode> lastCountryByLocation_;
81     std::shared_ptr<CountryCode> lastCountry_;
82     std::unique_ptr<std::map<pid_t, sptr<ICountryCodeCallback>>> countryCodeCallback_;
83     std::shared_ptr<SimSubscriber> simSubscriber_;
84     std::shared_ptr<NetworkSubscriber> networkSubscriber_;
85     std::mutex simSubscriberMutex_;
86     std::mutex networkSubscriberMutex_;
87     std::mutex countryCodeCallbackMutex_;
88 };
89 } // namespace Location
90 } // namespace OHOS
91 #endif // COUNTRY_CODE_MANAGER_H
92