• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 #ifndef OHOS_GLOBAL_I18N_I18N_SERVICE_ABILITY_H
17 #define OHOS_GLOBAL_I18N_I18N_SERVICE_ABILITY_H
18 
19 #include "event_handler.h"
20 #include "i18n_service_ability_stub.h"
21 #include "i18n_service_event.h"
22 #include "system_ability.h"
23 
24 namespace OHOS {
25 namespace Global {
26 namespace I18n {
27 class I18nServiceAbility : public SystemAbility, public I18nServiceAbilityStub {
28     DECLARE_SYSTEM_ABILITY(I18nServiceAbility);
29 public:
30     DISALLOW_COPY_AND_MOVE(I18nServiceAbility);
31     I18nServiceAbility(int32_t saId, bool runOnCreate);
32     ~I18nServiceAbility();
33 
34     // The following are the i18n service implement functions.
35     ErrCode SetSystemLanguage(const std::string& language, int32_t& code) override;
36     ErrCode SetSystemRegion(const std::string& region, int32_t& code) override;
37     ErrCode SetSystemLocale(const std::string& locale, int32_t& code) override;
38     ErrCode Set24HourClock(const std::string& flag, int32_t& code) override;
39     ErrCode SetUsingLocalDigit(bool flag, int32_t& code) override;
40     ErrCode AddPreferredLanguage(const std::string& language, int32_t index, int32_t& code) override;
41     ErrCode RemovePreferredLanguage(int32_t index, int32_t& code) override;
42     ErrCode SetTemperatureType(int32_t type, int32_t& code) override;
43     ErrCode SetFirstDayOfWeek(int32_t type, int32_t& code) override;
44     ErrCode GetSystemCollations(std::unordered_map<std::string, std::string>& systemCollations,
45         int32_t& code) override;
46     ErrCode GetUsingCollation(std::string& usingCollation, int32_t& code) override;
47     ErrCode SetSystemCollation(const std::string& identifier, int32_t& code) override;
48     ErrCode GetSystemNumberingSystems(std::unordered_map<std::string, std::string>& systemNumberingSystems,
49         int32_t& code) override;
50     ErrCode GetUsingNumberingSystem(std::string& usingNumberingSystem, int32_t& code) override;
51     ErrCode SetSystemNumberingSystem(const std::string& identifier, int32_t& code) override;
52     ErrCode GetSystemNumberPatterns(std::unordered_map<std::string, std::string>& systemNumberPatterns,
53         int32_t& code) override;
54     ErrCode GetUsingNumberPattern(std::string& usingNumberPattern, int32_t& code) override;
55     ErrCode SetSystemNumberPattern(const std::string& pattern, int32_t& code) override;
56     ErrCode GetSystemMeasurements(std::unordered_map<std::string, std::string>& systemMeasurements,
57         int32_t& code) override;
58     ErrCode GetUsingMeasurement(std::string& identifier, int32_t& code) override;
59     ErrCode SetSystemMeasurement(const std::string& identifier, int32_t& code) override;
60     ErrCode GetSystemNumericalDatePatterns(std::unordered_map<std::string, std::string>& numericalDatePatterns,
61         int32_t& code) override;
62     ErrCode GetUsingNumericalDatePattern(std::string& identifier, int32_t& code) override;
63     ErrCode SetSystemNumericalDatePattern(const std::string& identifier, int32_t& code) override;
64     ErrCode GetLanguageFromUserId(int32_t userId, std::string& language) override;
65 
66 protected:
67     /**
68      * @brief Called when i18n service start.
69      */
70     void OnStart(const SystemAbilityOnDemandReason& startReason) override;
71 
72     /**
73      * @brief Called when i18n service stop.
74      */
75     void OnStop() override;
76 
77     /**
78      * @brief Called when i18n service unload.
79      */
80     int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason) override;
81 
82 private:
83     enum class ModifyTaskNumber {
84         DECREASE = -1,
85         QUERY = 0,
86         INCREASE = 1
87     };
88     /**
89      * @brief Trigger unload i18n service after one request finished, but service will not unload
90      * until there no request in 10s.
91      */
92     void DelayUnloadI18nServiceAbility();
93     int32_t UpdateTaskNumber(ModifyTaskNumber action);
94     void StartTask(int32_t pid);
95     void FinishTask(int32_t pid);
96     int32_t GetCallingUserId();
97     I18nErrorCode CheckPermission();
98     I18nErrorCode CheckSystemPermission();
99     I18nErrorCode CheckUpdatePermission();
100     // i18n service unload event handler.
101     std::shared_ptr<AppExecFwk::EventHandler> handler = nullptr;
102     std::unique_ptr<I18nServiceEvent> i18nServiceEvent = nullptr;
103     std::mutex taskNumberMutex;
104     int32_t taskNumber;
105 };
106 } // namespace I18n
107 } // namespace Global
108 } // namespace OHOS
109 #endif // OHOS_GLOBAL_I18N_I18N_SERVICE_ABILITY_H