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