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