1 /*
2 * Copyright (c) 2021 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/accounts/account_0/appdata/";
OnStart(const Want & want)22 void PageAbilityDemo::OnStart(const Want &want)
23 {
24 APP_LOGI("PageAbilityDemo::onStart");
25 Ability::OnStart(want);
26 const std::string appName = "com.third.hiworld.example1";
27 std::string path = BUNDLE_DATA_ROOT_PATH + appName + "/cache/";
28 APP_LOGI("PageAbilityDemo::CreateDir");
29 for (int i = 1; i < 7; i++) {
30 path += "dir" + std::to_string(i) + "/";
31 APP_LOGI("PageAbilityDemo::CreateDir %{public}s", path.c_str());
32 CreateDir(path);
33 }
34 const std::string testCacheFileName =
35 BUNDLE_DATA_ROOT_PATH + appName + "/cache/dir1/dir2/dir3/dir4/dir5/dir6/name.txt";
36 CreateFile(testCacheFileName);
37 }
38
OnNewWant(const Want & want)39 void PageAbilityDemo::OnNewWant(const Want &want)
40 {
41 APP_LOGI("PageAbilityDemo::OnNewWant");
42 Ability::OnNewWant(want);
43 }
44
OnForeground(const Want & want)45 void PageAbilityDemo::OnForeground(const Want &want)
46 {
47 APP_LOGI("PageAbilityDemo::OnForeground");
48 Ability::OnForeground(want);
49 }
50
OnStop()51 void PageAbilityDemo::OnStop()
52 {
53 APP_LOGI("PageAbilityDemo::onStop");
54 Ability::OnStop();
55 }
56
OnActive()57 void PageAbilityDemo::OnActive()
58 {
59 APP_LOGI("PageAbilityDemo::OnActive");
60 Ability::OnActive();
61 }
62
OnInactive()63 void PageAbilityDemo::OnInactive()
64 {
65 APP_LOGI("PageAbilityDemo::OnInactive");
66 Ability::OnInactive();
67 }
68
OnBackground()69 void PageAbilityDemo::OnBackground()
70 {
71 APP_LOGI("PageAbilityDemo::OnBackground");
72 Ability::OnBackground();
73 }
74
CreateFile(const std::string & path) const75 void PageAbilityDemo::CreateFile(const std::string &path) const
76 {
77
78 std::ofstream file(path);
79 file.close();
80
81 if (access(path.c_str(), F_OK) != 0) {
82 APP_LOGE("CreateFile-checkFile:%{public}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:%{public}s error", path.c_str());
91 }
92 }
93 }
94
95 REGISTER_AA(PageAbilityDemo)
96 } // namespace AppExecFwk
97 } // namespace OHOS