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 45 protected: 46 /** 47 * @brief Called when i18n service start. 48 */ 49 void OnStart(const SystemAbilityOnDemandReason& startReason) override; 50 51 /** 52 * @brief Called when i18n service stop. 53 */ 54 void OnStop() override; 55 56 /** 57 * @brief Called when i18n service unload. 58 */ 59 int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason) override; 60 61 private: 62 enum class ModifyTaskNumber { 63 DECREASE = -1, 64 QUERY = 0, 65 INCREASE = 1 66 }; 67 /** 68 * @brief Trigger unload i18n service after one request finished, but service will not unload 69 * until there no request in 10s. 70 */ 71 void DelayUnloadI18nServiceAbility(); 72 int32_t UpdateTaskNumber(ModifyTaskNumber action); 73 void StartTask(int32_t pid); 74 void FinishTask(int32_t pid); 75 int32_t GetCallingUserId(); 76 I18nErrorCode CheckPermission(); 77 // i18n service unload event handler. 78 std::shared_ptr<AppExecFwk::EventHandler> handler = nullptr; 79 std::unique_ptr<I18nServiceEvent> i18nServiceEvent = nullptr; 80 std::mutex taskNumberMutex; 81 int32_t taskNumber; 82 }; 83 } // namespace I18n 84 } // namespace Global 85 } // namespace OHOS 86 #endif // OHOS_GLOBAL_I18N_I18N_SERVICE_ABILITY_H