1 /*
2  * Copyright (c) 2021-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_CONTEXT_DEAL_H
17 #define OHOS_ABILITY_RUNTIME_CONTEXT_DEAL_H
18 
19 #include "fa_context.h"
20 #include "lifecycle_state_info.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 class BundleMgrHelper;
25 class ContextDeal : public std::enable_shared_from_this<ContextDeal> {
26 public:
27     ContextDeal() = default;
28     explicit ContextDeal(bool isCreateBySystemApp);
29     virtual ~ContextDeal() = default;
30 
31     /**
32      * @brief Obtains information about the current application. The returned application information includes basic
33      * information such as the application name and application permissions.
34      *
35      * @return Returns the ApplicationInfo for the current application.
36      */
37     std::shared_ptr<ApplicationInfo> GetApplicationInfo() const;
38 
39     /**
40      * @brief Set ApplicationInfo
41      *
42      * @param info ApplicationInfo instance.
43      */
44     void SetApplicationInfo(const std::shared_ptr<ApplicationInfo> &info);
45 
46     /**
47      * @brief Obtains the Context object of the application.
48      *
49      * @return Returns the Context object of the application.
50      */
51     std::shared_ptr<Context> GetApplicationContext() const;
52 
53     /**
54      * @brief Set ApplicationContext
55      *
56      * @param context ApplicationContext instance.
57      */
58     void SetApplicationContext(const std::shared_ptr<Context> &context);
59 
60     /**
61      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
62      *  source code, and configuration files of a module.
63      *
64      * @return Returns the path of the package file.
65      */
66     std::string GetBundleCodePath();
67 
68     /**
69      * @brief SetBundleCodePath
70      *
71      * @param Returns string path
72      */
73     void SetBundleCodePath(std::string &path);
74 
75     /**
76      * @brief Obtains information about the current ability.
77      * The returned information includes the class name, bundle name, and other information about the current ability.
78      *
79      * @return Returns the AbilityInfo object for the current ability.
80      */
81     const std::shared_ptr<AbilityInfo> GetAbilityInfo();
82 
83     /**
84      * @brief Set AbilityInfo
85      *
86      * @param info AbilityInfo instance.
87      */
88     void SetAbilityInfo(const std::shared_ptr<AbilityInfo> &info);
89 
90     /**
91      * @brief Obtains the Context object of the ability.
92      *
93      * @return Returns the Context object of the ability.
94      */
95     std::shared_ptr<Context> GetContext();
96 
97     /**
98      * @brief Set Ability context
99      *
100      * @param context Ability object
101      */
102     void SetContext(const std::shared_ptr<Context> &context);
103 
104     /**
105      * @brief Obtains a BundleMgrHelper instance.
106      * You can use this instance to obtain information about the application bundle.
107      *
108      * @return Returns a BundleMgrHelper instance.
109      */
110     std::shared_ptr<BundleMgrHelper> GetBundleManager() const;
111 
112     /**
113      * @brief Obtains a resource manager.
114      *
115      * @return Returns a ResourceManager object.
116      */
117     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const;
118 
119     /**
120      * @brief Obtains the local database path.
121      * If the local database path does not exist, the system creates one and returns the created path.
122      *
123      * @return Returns the local database file.
124      */
125     std::string GetDatabaseDir();
126 
127     /**
128      * @brief Obtains the absolute path where all private data files of this application are stored.
129      *
130      * @return Returns the absolute path storing all private data files of this application.
131      */
132     std::string GetDataDir();
133 
134     /**
135      * @brief Obtains the directory for storing custom data files of the application.
136      * You can use the returned File object to create and access files in this directory. The files
137      * can be accessible only by the current application.
138      *
139      * @param name Indicates the name of the directory to retrieve. This directory is created as part
140      * of your application data.
141      * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE.
142      *
143      * @return Returns a File object for the requested directory.
144      */
145     std::string GetDir(const std::string &name, int mode);
146 
147     /**
148      * @brief Obtains the directory for storing files for the application on the device's internal storage.
149      *
150      * @return Returns the application file directory.
151      */
152     std::string GetFilesDir();
153 
154     /**
155      * @brief Obtains the bundle name of the current ability.
156      *
157      * @return Returns the bundle name of the current ability.
158      */
159     std::string GetBundleName() const;
160 
161     /**
162      * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability.
163      *
164      * @return Returns the path of the HAP containing this ability.
165      */
166     std::string GetBundleResourcePath();
167 
168     /**
169      * @brief Obtains an ability manager.
170      * The ability manager provides information about running processes and memory usage of an application.
171      *
172      * @return Returns an IAbilityManager instance.
173      */
174     sptr<AAFwk::IAbilityManager> GetAbilityManager();
175 
176     /**
177      * @brief Obtains the type of this application.
178      *
179      * @return Returns system if this application is a system application;
180      * returns normal if it is released in OHOS AppGallery;
181      * returns other if it is released by a third-party vendor;
182      * returns an empty string if the query fails.
183      */
184     std::string GetAppType();
185 
186     /**
187      * @brief Sets the pattern of this Context based on the specified pattern ID.
188      *
189      * @param patternId Indicates the resource ID of the pattern to set.
190      */
191     void SetPattern(int patternId);
192 
193     /**
194      * @brief Obtains the HapModuleInfo object of the application.
195      *
196      * @return Returns the HapModuleInfo object of the application.
197      */
198     std::shared_ptr<HapModuleInfo> GetHapModuleInfo();
199 
200     /**
201      * @brief Obtains the name of the current process.
202      *
203      * @return Returns the current process name.
204      */
205     std::string GetProcessName();
206 
207     /**
208      * @brief init the ResourceManager for ContextDeal.
209      *
210      * @param the ResourceManager has been inited.
211      *
212      */
213     void initResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager);
214 
215     /**
216      * @brief Get the string of this Context based on the specified resource ID.
217      *
218      * @param resId Indicates the resource ID of the string to get.
219      *
220      * @return Returns the string of this Context.
221      */
222     std::string GetString(int resId);
223 
224     /**
225      * @brief Get the string array of this Context based on the specified resource ID.
226      *
227      * @param resId Indicates the resource ID of the string array to get.
228      *
229      * @return Returns the string array of this Context.
230      */
231     std::vector<std::string> GetStringArray(int resId);
232 
233     /**
234      * @brief Get the integer array of this Context based on the specified resource ID.
235      *
236      * @param resId Indicates the resource ID of the integer array to get.
237      *
238      * @return Returns the integer array of this Context.
239      */
240     std::vector<int> GetIntArray(int resId);
241 
242     /**
243      * @brief Obtains the theme of this Context.
244      *
245      * @return theme Returns the theme of this Context.
246      */
247     std::map<std::string, std::string> GetTheme();
248 
249     /**
250      * @brief Sets the theme of this Context based on the specified theme ID.
251      *
252      * @param themeId Indicates the resource ID of the theme to set.
253      */
254     void SetTheme(int themeId);
255 
256     /**
257      * @brief Obtains the pattern of this Context.
258      *
259      * @return getPattern in interface Context
260      */
261     std::map<std::string, std::string> GetPattern();
262 
263     /**
264      * @brief Get the color of this Context based on the specified resource ID.
265      *
266      * @param resId Indicates the resource ID of the color to get.
267      *
268      * @return Returns the color value of this Context.
269      */
270     int GetColor(int resId);
271 
272     /**
273      * @brief Obtains the theme id of this Context.
274      *
275      * @return int Returns the theme id of this Context.
276      */
277     int GetThemeId();
278 
279     /**
280      * @brief Obtains the current display orientation of this ability.
281      *
282      * @return Returns the current display orientation.
283      */
284     int GetDisplayOrientation();
285 
286     /**
287      * @brief Obtains the path storing the preference file of the application.
288      *        If the preference file path does not exist, the system creates one and returns the created path.
289      *
290      * @return Returns the preference file path .
291      */
292     std::string GetPreferencesDir();
293 
294     /**
295      * @brief Set color mode
296      *
297      * @param the value of color mode.
298      */
299     void SetColorMode(int mode);
300 
301     /**
302      * @brief Obtains color mode.
303      *
304      * @return Returns the color mode value.
305      */
306     int GetColorMode();
307 
308     /**
309      * @brief Obtains the application base directory on the device's internal storage.
310      *
311      * @return Returns the application base directory.
312      */
313     std::string GetBaseDir() const;
314 public:
315     static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP;
316     static const std::string CONTEXT_DEAL_FILE_SEPARATOR;
317     static const std::string CONTEXT_DEAL_Files;
318     static const std::string CONTEXT_DISTRIBUTED;
319     static const std::string CONTEXT_DATA_STORAGE;
320     static const std::string CONTEXT_DEAL_DATA_APP;
321     static const std::string CONTEXT_DEAL_BASE;
322     static const std::string CONTEXT_DEAL_DATABASE;
323     static const std::string CONTEXT_DEAL_PREFERENCES;
324     static const std::string CONTEXT_DEAL_DATA;
325     int flags_ = 0x00000000;
326 
327 protected:
328     bool HapModuleInfoRequestInit();
329 
330 private:
331     bool IsCreateBySystemApp() const;
332     int GetCurrentAccountId() const;
333     void CreateDirIfNotExist(const std::string& dirPath) const;
334 
335     std::shared_ptr<ApplicationInfo> applicationInfo_ = nullptr;
336     std::shared_ptr<AbilityInfo> abilityInfo_ = nullptr;
337     std::shared_ptr<Context> appContext_ = nullptr;
338     std::shared_ptr<Context> abilityContext_ = nullptr;
339     std::shared_ptr<Global::Resource::ResourceManager> resourceManager_ = nullptr;
340     std::string path_ = "";
341     std::map<std::string, std::string> pattern_;
342     std::map<std::string, std::string> theme_;
343     AAFwk::LifeCycleStateInfo lifeCycleStateInfo_;
344     std::shared_ptr<HapModuleInfo> hapModuleInfoLocal_ = nullptr;
345     bool isCreateBySystemApp_ = false;
346     std::string currArea_ = "el2";
347 };
348 }  // namespace AppExecFwk
349 }  // namespace OHOS
350 #endif  // OHOS_ABILITY_RUNTIME_CONTEXT_DEAL_H
351