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         application_->CleanUselessTempData();
78         curState_ = APP_STATE_FOREGROUND;
79         return true;
80     }
81     TAG_LOGE(AAFwkTag::APPKIT, "curState is %{public}d", curState_);
82     return false;
83 }
84 
85 /**
86  * @brief Schedule the application to the APP_STATE_BACKGROUND state.
87  *
88  * @return Returns true if PerformBackground is scheduled successfully;
89  *         Returns false otherwise.
90  */
PerformBackground()91 bool ApplicationImpl::PerformBackground()
92 {
93     TAG_LOGD(AAFwkTag::APPKIT, "called");
94     if (curState_ == APP_STATE_FOREGROUND && application_ != nullptr) {
95         application_->OnBackground();
96         curState_ = APP_STATE_BACKGROUND;
97         return true;
98     }
99     TAG_LOGE(AAFwkTag::APPKIT, "curState is %{public}d", curState_);
100     return false;
101 }
102 
103 /**
104  * @brief Schedule the application to the APP_STATE_TERMINATED state.
105  *
106  * @param isLastProcess When it is the last application process, pass in true.
107  *
108  * @return Returns true if PerformTerminate is scheduled successfully;
109  *         Returns false otherwise.
110  */
PerformTerminate(bool isLastProcess)111 bool ApplicationImpl::PerformTerminate(bool isLastProcess)
112 {
113     TAG_LOGD(AAFwkTag::APPKIT, "called");
114     if (application_ == nullptr) {
115         TAG_LOGE(AAFwkTag::APPKIT, "Application instance is nullptr");
116         return false;
117     }
118 
119     application_->CleanAppTempData(isLastProcess);
120     if (curState_ == APP_STATE_BACKGROUND) {
121         application_->OnTerminate();
122         curState_ = APP_STATE_TERMINATED;
123         return true;
124     }
125     TAG_LOGE(AAFwkTag::APPKIT, "curState is %{public}d", curState_);
126     return false;
127 }
128 
129 /**
130  * @brief Schedule the application to the APP_STATE_TERMINATED state.
131  *
132  * @return Returns true if PerformTerminate is scheduled successfully;
133  *         Returns false otherwise.
134  */
PerformTerminateStrong()135 void ApplicationImpl::PerformTerminateStrong()
136 {
137     TAG_LOGD(AAFwkTag::APPKIT, "called");
138     if (application_ == nullptr) {
139         TAG_LOGE(AAFwkTag::APPKIT, "invalid application_.");
140         return;
141     }
142     application_->OnTerminate();
143 }
144 
145 /**
146  * @brief System determines to trim the memory.
147  *
148  * @param level Indicates the memory trim level, which shows the current memory usage status.
149  *
150  */
PerformMemoryLevel(int level)151 void ApplicationImpl::PerformMemoryLevel(int level)
152 {
153     TAG_LOGD(AAFwkTag::APPKIT, "called");
154     if (application_ != nullptr) {
155         application_->OnMemoryLevel(level);
156     }
157 }
158 
159 /**
160  * @brief System determines to send the new config to application.
161  *
162  * @param config Indicates the updated configuration information.
163  *
164  */
PerformConfigurationUpdated(const Configuration &config)165 void ApplicationImpl::PerformConfigurationUpdated(const Configuration &config)
166 {
167     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
168     TAG_LOGD(AAFwkTag::APPKIT, "called");
169     if (application_ != nullptr) {
170         application_->OnConfigurationUpdated(config, AbilityRuntime::SetLevel::System);
171     }
172 }
173 
174 /**
175  * @brief Set the target state to application.
176  *
177  * @param state The target state of application.
178  *
179  */
SetState(int state)180 int ApplicationImpl::SetState(int state)
181 {
182     curState_ = state;
183     return curState_;
184 }
185 
186 /**
187  * @brief Get the current state of application.
188  *
189  * @return Returns the current state of application.
190  *
191  */
GetState() const192 int ApplicationImpl::GetState() const
193 {
194     return curState_;
195 }
196 
197 /**
198  * @brief Set the RecordId to application.
199  *
200  * @param id recordId.
201  *
202  */
SetRecordId(int id)203 void ApplicationImpl::SetRecordId(int id)
204 {
205     recordId_ = id;
206 }
207 
208 /**
209  * @brief Get the recordId of application.
210  *
211  * @return Returns the recordId of application.
212  *
213  */
GetRecordId() const214 int ApplicationImpl::GetRecordId() const
215 {
216     return recordId_;
217 }
218 }  // namespace AppExecFwk
219 }  // namespace OHOS
220