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 const std::string testCacheDir1 = BUNDLE_DATA_ROOT_PATH + appName + "/cache/testDir1";
28 const std::string testCacheDir2 = BUNDLE_DATA_ROOT_PATH + appName + "/cache/testDir2";
29 const std::string testCacheFileNamE1 = testCacheDir1 + "/name1.txt";
30 const std::string testCacheFileNamE2 = testCacheDir2 + "/name2.txt";
31 CreateDir(testCacheDir1);
32 CreateDir(testCacheDir2);
33 CreateFile(testCacheFileNamE1);
34 CreateFile(testCacheFileNamE2);
35 }
36
OnNewWant(const Want & want)37 void PageAbilityDemo::OnNewWant(const Want &want)
38 {
39 APP_LOGI("PageAbilityDemo::OnNewWant");
40 Ability::OnNewWant(want);
41 }
42
OnForeground(const Want & want)43 void PageAbilityDemo::OnForeground(const Want &want)
44 {
45 APP_LOGI("PageAbilityDemo::OnForeground");
46 Ability::OnForeground(want);
47 }
48
OnStop()49 void PageAbilityDemo::OnStop()
50 {
51 APP_LOGI("PageAbilityDemo::onStop");
52 Ability::OnStop();
53 }
54
OnActive()55 void PageAbilityDemo::OnActive()
56 {
57 APP_LOGI("PageAbilityDemo::OnActive");
58 Ability::OnActive();
59 }
60
OnInactive()61 void PageAbilityDemo::OnInactive()
62 {
63 APP_LOGI("PageAbilityDemo::OnInactive");
64 Ability::OnInactive();
65 }
66
OnBackground()67 void PageAbilityDemo::OnBackground()
68 {
69 APP_LOGI("PageAbilityDemo::OnBackground");
70 Ability::OnBackground();
71 }
72
CreateFile(const std::string & path) const73 void PageAbilityDemo::CreateFile(const std::string &path) const
74 {
75
76 std::ofstream file(path);
77 file.close();
78
79 if (access(path.c_str(), F_OK) != 0) {
80 APP_LOGE("CreateFile-checkFile:%{public}s not exist", path.c_str());
81 }
82 }
83
CreateDir(const std::string & path) const84 void PageAbilityDemo::CreateDir(const std::string &path) const
85 {
86 if (access(path.c_str(), F_OK) != 0) {
87 if (mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
88 APP_LOGE("CreateDir:%{public}s error", path.c_str());
89 }
90 }
91 }
92
93 REGISTER_AA(PageAbilityDemo)
94 } // namespace AppExecFwk
95 } // namespace OHOS