• 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 
16 #include "application_impl.h"
17 
18 #include "hilog_wrapper.h"
19 #include "ohos_application.h"
20 #include "uri_permission_manager_client.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
ApplicationImpl()24 ApplicationImpl::ApplicationImpl() : curState_(APP_STATE_CREATE), recordId_(0)
25 {}
26 
27 /**
28  * @brief Set the application to the ApplicationImpl.
29  *
30  * @param application The application which the mainthread launched.
31  *
32  */
SetApplication(const std::shared_ptr<OHOSApplication> & application)33 void ApplicationImpl::SetApplication(const std::shared_ptr<OHOSApplication> &application)
34 {
35     if (application == nullptr) {
36         HILOG_ERROR("ApplicationImpl::SetApplication failed, application is nullptr");
37         return;
38     }
39     this->application_ = application;
40 }
41 
42 /**
43  * @brief Schedule the application to the APP_STATE_READY state.
44  *
45  * @return Returns true if performAppReady is scheduled successfully;
46  *         Returns false otherwise.
47  */
PerformAppReady()48 bool ApplicationImpl::PerformAppReady()
49 {
50     HILOG_DEBUG("ApplicationImpl::PerformAppReady called");
51     if (curState_ == APP_STATE_CREATE) {
52         application_->OnStart();
53         curState_ = APP_STATE_READY;
54         return true;
55     }
56     HILOG_ERROR("ApplicationImpl::PerformAppReady error! curState is %{public}d", curState_);
57     return false;
58 }
59 
60 /**
61  * @brief Schedule the application to the APP_STATE_FOREGROUND state.
62  *
63  * @return Returns true if PerformForeground is scheduled successfully;
64  *         Returns false otherwise.
65  */
PerformForeground()66 bool ApplicationImpl::PerformForeground()
67 {
68     HILOG_DEBUG("ApplicationImpl::performForeground called");
69     if ((curState_ == APP_STATE_READY) || (curState_ == APP_STATE_BACKGROUND)) {
70         application_->OnForeground();
71         curState_ = APP_STATE_FOREGROUND;
72         return true;
73     }
74     HILOG_ERROR("ApplicationImpl::performForeground error! curState is %{public}d", curState_);
75     return false;
76 }
77 
78 /**
79  * @brief Schedule the application to the APP_STATE_BACKGROUND state.
80  *
81  * @return Returns true if PerformBackground is scheduled successfully;
82  *         Returns false otherwise.
83  */
PerformBackground()84 bool ApplicationImpl::PerformBackground()
85 {
86     HILOG_DEBUG("ApplicationImpl::performBackground called");
87     if (curState_ == APP_STATE_FOREGROUND) {
88         application_->OnBackground();
89         curState_ = APP_STATE_BACKGROUND;
90         return true;
91     }
92     HILOG_ERROR("ApplicationImpl::performBackground error! curState is %{public}d", curState_);
93     return false;
94 }
95 
96 /**
97  * @brief Schedule the application to the APP_STATE_TERMINATED state.
98  *
99  * @return Returns true if PerformTerminate is scheduled successfully;
100  *         Returns false otherwise.
101  */
PerformTerminate()102 bool ApplicationImpl::PerformTerminate()
103 {
104     HILOG_DEBUG("ApplicationImpl::PerformTerminate called");
105     if (curState_ == APP_STATE_BACKGROUND) {
106         application_->OnTerminate();
107         curState_ = APP_STATE_TERMINATED;
108         RemoveUriPermission();
109         return true;
110     }
111     HILOG_ERROR("ApplicationImpl::performTerminate error! curState is %{public}d", curState_);
112     return false;
113 }
114 
115 /**
116  * @brief Schedule the application to the APP_STATE_TERMINATED state.
117  *
118  * @return Returns true if PerformTerminate is scheduled successfully;
119  *         Returns false otherwise.
120  */
PerformTerminateStrong()121 void ApplicationImpl::PerformTerminateStrong()
122 {
123     HILOG_DEBUG("ApplicationImpl::PerformTerminateStrong called");
124     application_->OnTerminate();
125     RemoveUriPermission();
126 }
127 
RemoveUriPermission()128 void ApplicationImpl::RemoveUriPermission()
129 {
130     auto appContext = application_->GetAppContext();
131     if (!appContext) {
132         HILOG_ERROR("ApplicationImpl::RemoveUriPermission: Get appliction context failed.");
133         return;
134     }
135     auto appInfo = appContext->GetApplicationInfo();
136     if (!appInfo) {
137         HILOG_ERROR("ApplicationImpl::RemoveUriPermission: Get appliction info failed.");
138         return;
139     }
140     auto uriPermMgrClient = AAFwk::UriPermissionManagerClient::GetInstance();
141     uriPermMgrClient->RemoveUriPermission(appInfo->accessTokenId);
142 }
143 
144 /**
145  * @brief System determines to trim the memory.
146  *
147  * @param level Indicates the memory trim level, which shows the current memory usage status.
148  *
149  */
PerformMemoryLevel(int level)150 void ApplicationImpl::PerformMemoryLevel(int level)
151 {
152     HILOG_DEBUG("ApplicationImpl::PerformMemoryLevel called");
153     application_->OnMemoryLevel(level);
154 }
155 
156 /**
157  * @brief System determines to send the new config to application.
158  *
159  * @param config Indicates the updated configuration information.
160  *
161  */
PerformConfigurationUpdated(const Configuration & config)162 void ApplicationImpl::PerformConfigurationUpdated(const Configuration &config)
163 {
164     HILOG_DEBUG("ApplicationImpl::PerformConfigurationUpdated called");
165     application_->OnConfigurationUpdated(config);
166 }
167 
168 /**
169  * @brief Set the target state to application.
170  *
171  * @param state The target state of application.
172  *
173  */
SetState(int state)174 int ApplicationImpl::SetState(int state)
175 {
176     curState_ = state;
177     return curState_;
178 }
179 
180 /**
181  * @brief Get the current state of application.
182  *
183  * @return Returns the current state of application.
184  *
185  */
GetState() const186 int ApplicationImpl::GetState() const
187 {
188     return curState_;
189 }
190 
191 /**
192  * @brief Set the RecordId to application.
193  *
194  * @param id recordId.
195  *
196  */
SetRecordId(int id)197 void ApplicationImpl::SetRecordId(int id)
198 {
199     recordId_ = id;
200 }
201 
202 /**
203  * @brief Get the recordId of application.
204  *
205  * @return Returns the recordId of application.
206  *
207  */
GetRecordId() const208 int ApplicationImpl::GetRecordId() const
209 {
210     return recordId_;
211 }
212 }  // namespace AppExecFwk
213 }  // namespace OHOS
214