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