• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_ABILITY_RUNTIME_APPLICATION_CONTEXT_H
17 #define OHOS_ABILITY_RUNTIME_APPLICATION_CONTEXT_H
18 
19 #include <vector>
20 #include <shared_mutex>
21 
22 #include "ability_lifecycle_callback.h"
23 #include "application_state_change_callback.h"
24 #include "context.h"
25 #include "context_impl.h"
26 #include "environment_callback.h"
27 namespace OHOS {
28 namespace AAFwk {
29 class Want;
30 }
31 namespace AbilityRuntime {
32 using AppConfigUpdateCallback = std::function<void(const AppExecFwk::Configuration &config)>;
33 class ApplicationContext : public Context {
34 public:
35     ApplicationContext() = default;
36     ~ApplicationContext() = default;
37     void RegisterAbilityLifecycleCallback(const std::shared_ptr<AbilityLifecycleCallback> &abilityLifecycleCallback);
38     void UnregisterAbilityLifecycleCallback(const std::shared_ptr<AbilityLifecycleCallback> &abilityLifecycleCallback);
39     bool IsAbilityLifecycleCallbackEmpty();
40     void RegisterEnvironmentCallback(const std::shared_ptr<EnvironmentCallback> &environmentCallback);
41     void UnregisterEnvironmentCallback(const std::shared_ptr<EnvironmentCallback> &environmentCallback);
42     void RegisterApplicationStateChangeCallback(
43         const std::weak_ptr<ApplicationStateChangeCallback> &applicationStateChangeCallback);
44     void DispatchOnAbilityCreate(const std::shared_ptr<NativeReference> &ability);
45     void DispatchOnWindowStageCreate(const std::shared_ptr<NativeReference> &ability,
46         const std::shared_ptr<NativeReference> &windowStage);
47     void DispatchOnWindowStageDestroy(const std::shared_ptr<NativeReference> &ability,
48         const std::shared_ptr<NativeReference> &windowStage);
49     void DispatchWindowStageFocus(const std::shared_ptr<NativeReference> &ability,
50         const std::shared_ptr<NativeReference> &windowStage);
51     void DispatchWindowStageUnfocus(const std::shared_ptr<NativeReference> &ability,
52         const std::shared_ptr<NativeReference> &windowStage);
53     void DispatchOnAbilityDestroy(const std::shared_ptr<NativeReference> &ability);
54     void DispatchOnAbilityForeground(const std::shared_ptr<NativeReference> &ability);
55     void DispatchOnAbilityBackground(const std::shared_ptr<NativeReference> &ability);
56     void DispatchOnAbilityContinue(const std::shared_ptr<NativeReference> &ability);
57     void DispatchOnAbilityWillContinue(const std::shared_ptr<NativeReference> &ability);
58     void DispatchOnWindowStageWillRestore(const std::shared_ptr<NativeReference> &ability,
59         const std::shared_ptr<NativeReference> &windowStage);
60     void DispatchOnWindowStageRestore(const std::shared_ptr<NativeReference> &ability,
61         const std::shared_ptr<NativeReference> &windowStage);
62     void DispatchOnAbilityWillSaveState(const std::shared_ptr<NativeReference> &ability);
63     void DispatchOnAbilitySaveState(const std::shared_ptr<NativeReference> &ability);
64     void DispatchConfigurationUpdated(const AppExecFwk::Configuration &config);
65     void DispatchMemoryLevel(const int level);
66     void NotifyApplicationForeground();
67     void NotifyApplicationBackground();
68     void DispatchOnWillNewWant(const std::shared_ptr<NativeReference> &ability);
69     void DispatchOnNewWant(const std::shared_ptr<NativeReference> &ability);
70     void DispatchOnAbilityWillCreate(const std::shared_ptr<NativeReference> &ability);
71     void DispatchOnWindowStageWillCreate(const std::shared_ptr<NativeReference> &ability,
72         const std::shared_ptr<NativeReference> &windowStage);
73     void DispatchOnWindowStageWillDestroy(const std::shared_ptr<NativeReference> &ability,
74         const std::shared_ptr<NativeReference> &windowStage);
75     void DispatchOnAbilityWillDestroy(const std::shared_ptr<NativeReference> &ability);
76     void DispatchOnAbilityWillForeground(const std::shared_ptr<NativeReference> &ability);
77     void DispatchOnAbilityWillBackground(const std::shared_ptr<NativeReference> &ability);
78 
79     std::string GetBundleName() const override;
80     std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override;
81     std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override;
82     std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override;
83     std::shared_ptr<Global::Resource::ResourceManager> CreateModuleResourceManager(
84         const std::string &bundleName, const std::string &moduleName) override;
85     int32_t CreateSystemHspModuleResourceManager(const std::string &bundleName,
86         const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) override;
87 #ifdef SUPPORT_GRAPHICS
88     std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override;
89 #endif
90     std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override;
91     void SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info);
92     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
93     std::string GetBundleCodePath() const override;
94     std::string GetBundleCodeDir() override;
95     std::string GetCacheDir() override;
96     std::string GetTempDir() override;
97     std::string GetResourceDir() override;
98     void GetAllTempDir(std::vector<std::string> &tempPaths);
99     std::string GetFilesDir() override;
100     bool IsUpdatingConfigurations() override;
101     bool PrintDrawnCompleted() override;
102     std::string GetDatabaseDir() override;
103     std::string GetPreferencesDir() override;
104     int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override;
105     int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override;
106     std::string GetGroupDir(std::string groupId) override;
107     std::string GetDistributedFilesDir() override;
108     std::string GetCloudFileDir() override;
109     sptr<IRemoteObject> GetToken() override;
110     void SetToken(const sptr<IRemoteObject> &token) override;
111     void SwitchArea(int mode) override;
112     void SetColorMode(int32_t colorMode);
113     void SetLanguage(const std::string &language);
114     void SetFont(const std::string &font);
115     bool SetFontSizeScale(double fontSizeScale);
116     void SetMcc(const std::string &mcc);
117     void SetMnc(const std::string &mnc);
118     void ClearUpApplicationData();
119     int GetArea() override;
120     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override;
121     void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config);
122     void AppHasDarkRes(bool &darkRes);
123     std::string GetBaseDir() const override;
124     Global::Resource::DeviceType GetDeviceType() const override;
125     void KillProcessBySelf(const bool clearPageStack = true);
126     int32_t GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info);
127     int32_t RestartApp(const AAFwk::Want& want);
128 
129     void AttachContextImpl(const std::shared_ptr<ContextImpl> &contextImpl);
130 
131     static std::shared_ptr<ApplicationContext> GetInstance();
132 
133     // unused
134     std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override;
135 
136     bool GetApplicationInfoUpdateFlag() const;
137     void SetApplicationInfoUpdateFlag(bool flag);
138     void RegisterAppConfigUpdateObserver(AppConfigUpdateCallback appConfigChangeCallback);
139     void RegisterAppFontObserver(AppConfigUpdateCallback appFontCallback);
140 #ifdef SUPPORT_GRAPHICS
141     void RegisterGetDisplayConfig(GetDisplayConfigCallback getDisplayConfigCallback);
142 #endif
143 
144     std::string GetAppRunningUniqueId() const;
145     void SetAppRunningUniqueId(const std::string &appRunningUniqueId);
146     int32_t SetSupportedProcessCacheSelf(bool isSupport);
147     int32_t GetCurrentAppCloneIndex();
148     void SetCurrentAppCloneIndex(int32_t appIndex);
149     std::string GetCurrentInstanceKey();
150     void SetCurrentInstanceKey(const std::string& instanceKey);
151     int32_t GetAllRunningInstanceKeys(std::vector<std::string> &instanceKeys);
152     int32_t GetCurrentAppMode();
153     void SetCurrentAppMode(int32_t appIndex);
154 
155     using SelfType = ApplicationContext;
156     static const size_t CONTEXT_TYPE_ID;
157 
158 protected:
IsContext(size_t contextTypeId)159     bool IsContext(size_t contextTypeId) override
160     {
161         return contextTypeId == CONTEXT_TYPE_ID || Context::IsContext(contextTypeId);
162     }
163 
164 private:
165     std::shared_ptr<ContextImpl> contextImpl_;
166     static std::vector<std::shared_ptr<AbilityLifecycleCallback>> callbacks_;
167     static std::vector<std::shared_ptr<EnvironmentCallback>> envCallbacks_;
168     static std::vector<std::weak_ptr<ApplicationStateChangeCallback>> applicationStateCallback_;
169     std::recursive_mutex callbackLock_;
170     std::recursive_mutex envCallbacksLock_;
171     std::recursive_mutex applicationStateCallbackLock_;
172     bool applicationInfoUpdateFlag_ = false;
173     AppConfigUpdateCallback appConfigChangeCallback_ = nullptr;
174     AppConfigUpdateCallback appFontCallback_ = nullptr;
175     std::string appRunningUniqueId_;
176     int32_t appIndex_ = 0;
177     int32_t appMode_ = 0;
178     std::string instanceKey_;
179 };
180 }  // namespace AbilityRuntime
181 }  // namespace OHOS
182 #endif  // OHOS_ABILITY_RUNTIME_APPLICATION_CONTEXT_H
183