• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "pageAbilityDemo.h"
16 #include <fcntl.h>
17 #include <fstream>
18 #include <sys/stat.h>
19 namespace OHOS {
20 namespace AppExecFwk {
21 const std::string BUNDLE_DATA_ROOT_PATH = "/data/app/el2/100/base/";
22 const int32_t FOLDER_LAYERS = 7;
OnStart(const Want & want,sptr<AAFwk::SessionInfo> sessionInfo)23 void PageAbilityDemo::OnStart(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo)
24 {
25     APP_LOGI("PageAbilityDemo::onStart");
26     Ability::OnStart(want, sessionInfo);
27     const std::string appName = "com.third.hiworld.example1";
28     std::string path = BUNDLE_DATA_ROOT_PATH + appName + "/cache/";
29     APP_LOGI("PageAbilityDemo::CreateDir");
30     for (int i = 1; i < FOLDER_LAYERS; i++) {
31         path += "dir" + std::to_string(i) + "/";
32         APP_LOGI("PageAbilityDemo::CreateDir %{private}s", path.c_str());
33         CreateDir(path);
34     }
35     const std::string testCacheFileName =
36         BUNDLE_DATA_ROOT_PATH + appName + "/cache/dir1/dir2/dir3/dir4/dir5/dir6/name.txt";
37     CreateFile(testCacheFileName);
38 }
39 
OnNewWant(const Want & want)40 void PageAbilityDemo::OnNewWant(const Want &want)
41 {
42     APP_LOGI("PageAbilityDemo::OnNewWant");
43     Ability::OnNewWant(want);
44 }
45 
OnForeground(const Want & want)46 void PageAbilityDemo::OnForeground(const Want &want)
47 {
48     APP_LOGI("PageAbilityDemo::OnForeground");
49     Ability::OnForeground(want);
50 }
51 
OnStop()52 void PageAbilityDemo::OnStop()
53 {
54     APP_LOGI("PageAbilityDemo::onStop");
55     Ability::OnStop();
56 }
57 
OnActive()58 void PageAbilityDemo::OnActive()
59 {
60     APP_LOGI("PageAbilityDemo::OnActive");
61     Ability::OnActive();
62 }
63 
OnInactive()64 void PageAbilityDemo::OnInactive()
65 {
66     APP_LOGI("PageAbilityDemo::OnInactive");
67     Ability::OnInactive();
68 }
69 
OnBackground()70 void PageAbilityDemo::OnBackground()
71 {
72     APP_LOGI("PageAbilityDemo::OnBackground");
73     Ability::OnBackground();
74 }
75 
CreateFile(const std::string & path) const76 void PageAbilityDemo::CreateFile(const std::string &path) const
77 {
78     std::ofstream file(path);
79     file.close();
80 
81     if (access(path.c_str(), F_OK) != 0) {
82         APP_LOGE("CreateFile-checkFile:%{private}s not exist", path.c_str());
83     }
84 }
85 
CreateDir(const std::string & path) const86 void PageAbilityDemo::CreateDir(const std::string &path) const
87 {
88     if (access(path.c_str(), F_OK) != 0) {
89         if (mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
90             APP_LOGE("CreateDir:%{private}s error", path.c_str());
91         }
92     }
93 }
94 
95 REGISTER_AA(PageAbilityDemo)
96 }  // namespace AppExecFwk
97 }  // namespace OHOS