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 && application_ != nullptr) {
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)) && application_ != nullptr) {
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 && application_ != nullptr) {
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 && application_ != nullptr) {
106 application_->OnTerminate();
107 curState_ = APP_STATE_TERMINATED;
108 return true;
109 }
110 HILOG_ERROR("ApplicationImpl::performTerminate error! curState is %{public}d", curState_);
111 return false;
112 }
113
114 /**
115 * @brief Schedule the application to the APP_STATE_TERMINATED state.
116 *
117 * @return Returns true if PerformTerminate is scheduled successfully;
118 * Returns false otherwise.
119 */
PerformTerminateStrong()120 void ApplicationImpl::PerformTerminateStrong()
121 {
122 HILOG_DEBUG("ApplicationImpl::PerformTerminateStrong called");
123 if (application_ == nullptr) {
124 HILOG_ERROR("ApplicationImpl::PerformTerminateStrong: invalid application_.");
125 return;
126 }
127 application_->OnTerminate();
128 }
129
130 /**
131 * @brief System determines to trim the memory.
132 *
133 * @param level Indicates the memory trim level, which shows the current memory usage status.
134 *
135 */
PerformMemoryLevel(int level)136 void ApplicationImpl::PerformMemoryLevel(int level)
137 {
138 HILOG_DEBUG("ApplicationImpl::PerformMemoryLevel called");
139 if (application_ != nullptr) {
140 application_->OnMemoryLevel(level);
141 }
142 }
143
144 /**
145 * @brief System determines to send the new config to application.
146 *
147 * @param config Indicates the updated configuration information.
148 *
149 */
PerformConfigurationUpdated(const Configuration & config)150 void ApplicationImpl::PerformConfigurationUpdated(const Configuration &config)
151 {
152 HILOG_DEBUG("ApplicationImpl::PerformConfigurationUpdated called");
153 if (application_ != nullptr) {
154 application_->OnConfigurationUpdated(config);
155 }
156 }
157
158 /**
159 * @brief Set the target state to application.
160 *
161 * @param state The target state of application.
162 *
163 */
SetState(int state)164 int ApplicationImpl::SetState(int state)
165 {
166 curState_ = state;
167 return curState_;
168 }
169
170 /**
171 * @brief Get the current state of application.
172 *
173 * @return Returns the current state of application.
174 *
175 */
GetState() const176 int ApplicationImpl::GetState() const
177 {
178 return curState_;
179 }
180
181 /**
182 * @brief Set the RecordId to application.
183 *
184 * @param id recordId.
185 *
186 */
SetRecordId(int id)187 void ApplicationImpl::SetRecordId(int id)
188 {
189 recordId_ = id;
190 }
191
192 /**
193 * @brief Get the recordId of application.
194 *
195 * @return Returns the recordId of application.
196 *
197 */
GetRecordId() const198 int ApplicationImpl::GetRecordId() const
199 {
200 return recordId_;
201 }
202 } // namespace AppExecFwk
203 } // namespace OHOS
204