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#include "app_identity.h"
32
33namespace OHOS {
34namespace Location {
35class CountryCodeManager {
36public:
37    CountryCodeManager();
38    ~CountryCodeManager();
39    std::shared_ptr<CountryCode> GetIsoCountryCode();
40    void UnregisterCountryCodeCallback(const sptr<IRemoteObject>& callback);
41    void RegisterCountryCodeCallback(const sptr<IRemoteObject>& callback, AppIdentity &identity);
42    void ReSubscribeEvent();
43    void ReUnsubscribeEvent();
44    bool IsCountryCodeRegistered();
45    static CountryCodeManager* GetInstance();
46
47private:
48    std::string GetCountryCodeByLocation(const std::unique_ptr<Location>& location);
49    bool UpdateCountryCodeByLocation(std::string countryCode, int type);
50    void UpdateCountryCode(std::string countryCode, int type);
51    std::string GetCountryCodeByLastLocation();
52    void NotifyAllListener();
53    bool SubscribeSimEvent();
54    bool SubscribeNetworkStatusEvent();
55    bool SubscribeLocaleConfigEvent();
56    bool UnsubscribeSimEvent();
57    bool UnsubscribeNetworkStatusEvent();
58
59    class LocatorCallback : public IRemoteStub<ILocatorCallback> {
60    public:
61        void OnLocationReport(const std::unique_ptr<Location>& location);
62        void OnLocatingStatusChange(const int status);
63        void OnErrorReport(const int errorCode);
64    };
65
66    class NetworkSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
67    public:
68        explicit NetworkSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &info);
69        ~NetworkSubscriber() override = default;
70    private:
71        void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &event) override;
72    };
73
74    class SimSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
75    public:
76        explicit SimSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &info);
77        ~SimSubscriber() override = default;
78    private:
79        virtual void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &event) override;
80    };
81
82    std::shared_ptr<CountryCode> lastCountryByLocation_;
83    std::shared_ptr<CountryCode> lastCountry_;
84    std::map<sptr<IRemoteObject>, AppIdentity> countryCodeCallbacksMap_;
85    std::shared_ptr<SimSubscriber> simSubscriber_;
86    std::shared_ptr<NetworkSubscriber> networkSubscriber_;
87    std::mutex simSubscriberMutex_;
88    std::mutex networkSubscriberMutex_;
89    std::mutex countryCodeCallbackMutex_;
90};
91} // namespace Location
92} // namespace OHOS
93#endif // COUNTRY_CODE_MANAGER_H
94