• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "app_state_observer_manager.h"
17 #include "application_state_observer_stub.h"
18 #include "hilog_wrapper.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 const std::string THREAD_NAME = "AppStateObserverManager";
24 const int BUNDLE_NAME_LIST_MAX_SIZE = 128;
25 } // namespace
AppStateObserverManager()26 AppStateObserverManager::AppStateObserverManager()
27 {
28     HILOG_INFO("AppStateObserverManager instance is created");
29 }
30 
~AppStateObserverManager()31 AppStateObserverManager::~AppStateObserverManager()
32 {
33     HILOG_INFO("AppStateObserverManager instance is destroyed");
34 }
35 
Init()36 void AppStateObserverManager::Init()
37 {
38     if (!handler_) {
39         handler_ = std::make_shared<EventHandler>(EventRunner::Create(THREAD_NAME));
40     }
41 }
42 
RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer,const std::vector<std::string> & bundleNameList)43 int32_t AppStateObserverManager::RegisterApplicationStateObserver(
44     const sptr<IApplicationStateObserver> &observer, const std::vector<std::string> &bundleNameList)
45 {
46     HILOG_INFO("Register applicationStateObserver begin.");
47     if (bundleNameList.size() > BUNDLE_NAME_LIST_MAX_SIZE) {
48         HILOG_ERROR("the bundleNameList passed in is too long");
49         return ERR_INVALID_VALUE;
50     }
51     if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
52         HILOG_ERROR("%{public}s: Permission verification failed", __func__);
53         return ERR_PERMISSION_DENIED;
54     }
55     if (observer == nullptr) {
56         HILOG_ERROR("The param observer is nullptr.");
57         return ERR_INVALID_VALUE;
58     }
59     if (ObserverExist(observer)) {
60         HILOG_ERROR("Observer exist.");
61         return ERR_INVALID_VALUE;
62     }
63     std::lock_guard<std::recursive_mutex> lockRegister(observerLock_);
64     appStateObserverMap_.emplace(observer, bundleNameList);
65     HILOG_DEBUG("%{public}s appStateObserverMap_ size:%{public}zu", __func__, appStateObserverMap_.size());
66     AddObserverDeathRecipient(observer);
67     return ERR_OK;
68 }
69 
UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> & observer)70 int32_t AppStateObserverManager::UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer)
71 {
72     HILOG_INFO("%{public}s begin", __func__);
73     if (AAFwk::PermissionVerification::GetInstance()->VerifyAppStateObserverPermission() == ERR_PERMISSION_DENIED) {
74         HILOG_ERROR("%{public}s: Permission verification failed", __func__);
75         return ERR_PERMISSION_DENIED;
76     }
77     std::lock_guard<std::recursive_mutex> lockUnregister(observerLock_);
78     if (observer == nullptr) {
79         HILOG_ERROR("Observer nullptr");
80         return ERR_INVALID_VALUE;
81     }
82     std::map<sptr<IApplicationStateObserver>, std::vector<std::string>>::iterator it;
83     for (it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
84         if (it->first->AsObject() == observer->AsObject()) {
85             appStateObserverMap_.erase(it);
86             HILOG_INFO("%{public}s appStateObserverMap_ size:%{public}zu", __func__, appStateObserverMap_.size());
87             RemoveObserverDeathRecipient(observer);
88             return ERR_OK;
89         }
90     }
91     HILOG_ERROR("Observer not exist.");
92     return ERR_INVALID_VALUE;
93 }
94 
OnAppStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state,bool needNotifyApp)95 void AppStateObserverManager::OnAppStateChanged(
96     const std::shared_ptr<AppRunningRecord> &appRecord, const ApplicationState state, bool needNotifyApp)
97 {
98     if (handler_ == nullptr) {
99         HILOG_ERROR("handler is nullptr, OnAppStateChanged failed.");
100         return;
101     }
102 
103     auto task = [weak = weak_from_this(), appRecord, state, needNotifyApp]() {
104         auto self = weak.lock();
105         if (self == nullptr) {
106             HILOG_ERROR("self is nullptr, OnAppStateChanged failed.");
107             return;
108         }
109         HILOG_INFO("OnAppStateChanged come.");
110         self->HandleAppStateChanged(appRecord, state, needNotifyApp);
111     };
112     handler_->PostTask(task);
113 }
114 
OnProcessDied(const std::shared_ptr<AppRunningRecord> & appRecord)115 void AppStateObserverManager::OnProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord)
116 {
117     if (handler_ == nullptr) {
118         HILOG_ERROR("handler is nullptr, OnProcessDied failed.");
119         return;
120     }
121 
122     auto task = [weak = weak_from_this(), appRecord]() {
123         auto self = weak.lock();
124         if (self == nullptr) {
125             HILOG_ERROR("self is nullptr, OnProcessDied failed.");
126             return;
127         }
128         HILOG_INFO("OnProcessDied come.");
129     self->HandleOnAppProcessDied(appRecord);
130     };
131     handler_->PostTask(task);
132 }
133 
OnRenderProcessDied(const std::shared_ptr<RenderRecord> & renderRecord)134 void AppStateObserverManager::OnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord)
135 {
136     if (handler_ == nullptr) {
137         HILOG_ERROR("handler is nullptr, OnRenderProcessDied failed.");
138         return;
139     }
140 
141     auto task = [weak = weak_from_this(), renderRecord]() {
142         auto self = weak.lock();
143         if (self == nullptr) {
144             HILOG_ERROR("self is nullptr, OnRenderProcessDied failed.");
145             return;
146         }
147         HILOG_INFO("OnRenderProcessDied come.");
148         self->HandleOnRenderProcessDied(renderRecord);
149     };
150     handler_->PostTask(task);
151 }
152 
OnProcessStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord)153 void AppStateObserverManager::OnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord)
154 {
155     if (handler_ == nullptr) {
156         HILOG_ERROR("handler is nullptr, OnProcessStateChanged failed.");
157         return;
158     }
159 
160     auto task = [weak = weak_from_this(), appRecord]() {
161         auto self = weak.lock();
162         if (self == nullptr) {
163             HILOG_ERROR("self is nullptr, OnProcessStateChanged failed.");
164             return;
165         }
166         HILOG_INFO("OnProcessStateChanged come.");
167         self->HandleOnProcessStateChanged(appRecord);
168     };
169     handler_->PostTask(task);
170 }
171 
OnProcessCreated(const std::shared_ptr<AppRunningRecord> & appRecord)172 void AppStateObserverManager::OnProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord)
173 {
174     if (handler_ == nullptr) {
175         HILOG_ERROR("handler is nullptr, OnProcessCreated failed.");
176         return;
177     }
178 
179     auto task = [weak = weak_from_this(), appRecord]() {
180         auto self = weak.lock();
181         if (self == nullptr) {
182             HILOG_ERROR("self is nullptr, OnProcessCreated failed.");
183             return;
184         }
185         HILOG_INFO("OnProcessCreated come.");
186         self->HandleOnAppProcessCreated(appRecord);
187     };
188     handler_->PostTask(task);
189 }
190 
OnProcessReused(const std::shared_ptr<AppRunningRecord> & appRecord)191 void AppStateObserverManager::OnProcessReused(const std::shared_ptr<AppRunningRecord> &appRecord)
192 {
193     if (handler_ == nullptr) {
194         HILOG_ERROR("handler is nullptr, OnProcessReused failed.");
195         return;
196     }
197 
198     auto task = [weak = weak_from_this(), appRecord]() {
199         auto self = weak.lock();
200         if (self == nullptr) {
201             HILOG_ERROR("self is nullptr, OnProcessReused failed.");
202             return;
203         }
204         HILOG_INFO("OnProcessReused come.");
205         self->HandleOnProcessResued(appRecord);
206     };
207     handler_->PostTask(task);
208 }
209 
OnRenderProcessCreated(const std::shared_ptr<RenderRecord> & renderRecord)210 void AppStateObserverManager::OnRenderProcessCreated(const std::shared_ptr<RenderRecord> &renderRecord)
211 {
212     if (handler_ == nullptr) {
213         HILOG_ERROR("handler is nullptr, OnRenderProcessCreated failed.");
214         return;
215     }
216 
217     auto task = [weak = weak_from_this(), renderRecord]() {
218         auto self = weak.lock();
219         if (self == nullptr) {
220             HILOG_ERROR("self is nullptr, OnRenderProcessCreated failed.");
221             return;
222         }
223         HILOG_INFO("OnRenderProcessCreated come.");
224         self->HandleOnRenderProcessCreated(renderRecord);
225     };
226     handler_->PostTask(task);
227 }
228 
StateChangedNotifyObserver(const AbilityStateData abilityStateData,bool isAbility)229 void AppStateObserverManager::StateChangedNotifyObserver(const AbilityStateData abilityStateData, bool isAbility)
230 {
231     if (handler_ == nullptr) {
232         HILOG_ERROR("handler is nullptr, StateChangedNotifyObserver failed.");
233         return;
234     }
235 
236     auto task = [weak = weak_from_this(), abilityStateData, isAbility]() {
237         auto self = weak.lock();
238         if (self == nullptr) {
239             HILOG_ERROR("self is nullptr, StateChangedNotifyObserver failed.");
240             return;
241         }
242         HILOG_INFO("StateChangedNotifyObserver come.");
243         self->HandleStateChangedNotifyObserver(abilityStateData, isAbility);
244     };
245     handler_->PostTask(task);
246 }
247 
HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state,bool needNotifyApp)248 void AppStateObserverManager::HandleAppStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord,
249     const ApplicationState state, bool needNotifyApp)
250 {
251     if (appRecord == nullptr) {
252         return;
253     }
254 
255     if (state == ApplicationState::APP_STATE_FOREGROUND || state == ApplicationState::APP_STATE_BACKGROUND) {
256         AppStateData data = WrapAppStateData(appRecord, state);
257         HILOG_DEBUG("HandleAppStateChanged, name:%{public}s, uid:%{public}d, state:%{public}d, notify:%{public}d",
258             data.bundleName.c_str(), data.uid, data.state, needNotifyApp);
259         std::lock_guard<std::recursive_mutex> lockNotify(observerLock_);
260         for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
261             std::vector<std::string>::iterator iter = std::find(it->second.begin(),
262                 it->second.end(), data.bundleName);
263             bool valid = (it->second.empty() || iter != it->second.end()) && it->first != nullptr;
264             if (valid) {
265                 it->first->OnForegroundApplicationChanged(data);
266             }
267             if (valid && needNotifyApp) {
268                 it->first->OnAppStateChanged(data);
269             }
270         }
271     }
272 
273     if (state == ApplicationState::APP_STATE_CREATE || state == ApplicationState::APP_STATE_TERMINATED) {
274         AppStateData data = WrapAppStateData(appRecord, state);
275         HILOG_INFO("OnApplicationStateChanged, name:%{public}s, uid:%{public}d, state:%{public}d",
276             data.bundleName.c_str(), data.uid, data.state);
277         std::lock_guard<std::recursive_mutex> lockNotify(observerLock_);
278         for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
279             std::vector<std::string>::iterator iter = std::find(it->second.begin(),
280                 it->second.end(), data.bundleName);
281             if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
282                 it->first->OnApplicationStateChanged(data);
283             }
284         }
285     }
286 }
287 
HandleStateChangedNotifyObserver(const AbilityStateData abilityStateData,bool isAbility)288 void AppStateObserverManager::HandleStateChangedNotifyObserver(const AbilityStateData abilityStateData, bool isAbility)
289 {
290     std::lock_guard<std::recursive_mutex> lockNotify(observerLock_);
291     HILOG_DEBUG("Handle state change, module:%{public}s, bundle:%{public}s, ability:%{public}s, state:%{public}d,"
292         "pid:%{public}d ,uid:%{public}d, abilityType:%{public}d, isAbility:%{public}d",
293         abilityStateData.moduleName.c_str(), abilityStateData.bundleName.c_str(),
294         abilityStateData.abilityName.c_str(), abilityStateData.abilityState,
295         abilityStateData.pid, abilityStateData.uid, abilityStateData.abilityType, isAbility);
296     for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
297         std::vector<std::string>::iterator iter = std::find(it->second.begin(),
298             it->second.end(), abilityStateData.bundleName);
299         if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
300             if (isAbility) {
301                 it->first->OnAbilityStateChanged(abilityStateData);
302             } else {
303                 it->first->OnExtensionStateChanged(abilityStateData);
304             }
305         }
306     }
307 }
308 
HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> & appRecord)309 void AppStateObserverManager::HandleOnAppProcessCreated(const std::shared_ptr<AppRunningRecord> &appRecord)
310 {
311     if (!appRecord) {
312         HILOG_ERROR("app record is null");
313         return;
314     }
315     ProcessData data = WrapProcessData(appRecord);
316     HILOG_DEBUG("Process Create, bundle:%{public}s, pid:%{public}d, uid:%{public}d",
317         data.bundleName.c_str(), data.pid, data.uid);
318     HandleOnProcessCreated(data);
319 }
320 
HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> & appRecord)321 void AppStateObserverManager::HandleOnProcessResued(const std::shared_ptr<AppRunningRecord> &appRecord)
322 {
323     if (!appRecord) {
324         HILOG_ERROR("app record is null");
325         return;
326     }
327     ProcessData data = WrapProcessData(appRecord);
328     HILOG_DEBUG("Process Resued, bundle:%{public}s, pid:%{public}d, uid:%{public}d",
329         data.bundleName.c_str(), data.pid, data.uid);
330 
331     std::lock_guard<std::recursive_mutex> lockNotify(observerLock_);
332     for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
333         std::vector<std::string>::iterator iter = std::find(it->second.begin(),
334             it->second.end(), data.bundleName);
335         if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
336             it->first->OnProcessReused(data);
337         }
338     }
339 }
340 
HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> & renderRecord)341 void AppStateObserverManager::HandleOnRenderProcessCreated(const std::shared_ptr<RenderRecord> &renderRecord)
342 {
343     if (!renderRecord) {
344         HILOG_ERROR("render record is null");
345         return;
346     }
347     ProcessData data = WrapRenderProcessData(renderRecord);
348     HILOG_DEBUG("RenderProcess Create, bundle:%{public}s, pid:%{public}d, uid:%{public}d",
349         data.bundleName.c_str(), data.pid, data.uid);
350     HandleOnProcessCreated(data);
351 }
352 
HandleOnProcessCreated(const ProcessData & data)353 void AppStateObserverManager::HandleOnProcessCreated(const ProcessData &data)
354 {
355     std::lock_guard<std::recursive_mutex> lockNotify(observerLock_);
356     for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
357         std::vector<std::string>::iterator iter = std::find(it->second.begin(),
358             it->second.end(), data.bundleName);
359         if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
360             it->first->OnProcessCreated(data);
361         }
362     }
363 }
364 
HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> & appRecord)365 void AppStateObserverManager::HandleOnProcessStateChanged(const std::shared_ptr<AppRunningRecord> &appRecord)
366 {
367     if (!appRecord) {
368         HILOG_ERROR("app record is null");
369         return;
370     }
371     ProcessData data = WrapProcessData(appRecord);
372     HILOG_DEBUG("Process State Change, bundle:%{public}s pid:%{public}d uid:%{public}d isContinuousTask:%{public}d",
373         data.bundleName.c_str(), data.pid, data.uid, data.isContinuousTask);
374     std::lock_guard<std::recursive_mutex> lockNotify(observerLock_);
375     for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
376         std::vector<std::string>::iterator iter = std::find(it->second.begin(),
377             it->second.end(), data.bundleName);
378         if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
379             it->first->OnProcessStateChanged(data);
380         }
381     }
382 }
383 
HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> & appRecord)384 void AppStateObserverManager::HandleOnAppProcessDied(const std::shared_ptr<AppRunningRecord> &appRecord)
385 {
386     if (!appRecord) {
387         HILOG_ERROR("app record is null");
388         return;
389     }
390     ProcessData data = WrapProcessData(appRecord);
391     HILOG_DEBUG("Process died, bundle:%{public}s, pid:%{public}d, uid:%{public}d",
392         data.bundleName.c_str(), data.pid, data.uid);
393     HandleOnProcessDied(data);
394 }
395 
HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> & renderRecord)396 void AppStateObserverManager::HandleOnRenderProcessDied(const std::shared_ptr<RenderRecord> &renderRecord)
397 {
398     if (!renderRecord) {
399         HILOG_ERROR("render record is null");
400         return;
401     }
402     ProcessData data = WrapRenderProcessData(renderRecord);
403     HILOG_DEBUG("Render Process died, bundle:%{public}s, pid:%{public}d, uid:%{public}d",
404         data.bundleName.c_str(), data.pid, data.uid);
405     HandleOnProcessDied(data);
406 }
407 
HandleOnProcessDied(const ProcessData & data)408 void AppStateObserverManager::HandleOnProcessDied(const ProcessData &data)
409 {
410     std::lock_guard<std::recursive_mutex> lockNotify(observerLock_);
411     for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
412         std::vector<std::string>::iterator iter = std::find(it->second.begin(),
413             it->second.end(), data.bundleName);
414         if ((it->second.empty() || iter != it->second.end()) && it->first != nullptr) {
415             it->first->OnProcessDied(data);
416         }
417     }
418 }
419 
WrapProcessData(const std::shared_ptr<AppRunningRecord> & appRecord)420 ProcessData AppStateObserverManager::WrapProcessData(const std::shared_ptr<AppRunningRecord> &appRecord)
421 {
422     ProcessData processData;
423     processData.bundleName = appRecord->GetBundleName();
424     processData.pid = appRecord->GetPriorityObject()->GetPid();
425     processData.uid = appRecord->GetUid();
426     processData.state = static_cast<AppProcessState>(appRecord->GetState());
427     processData.isContinuousTask = appRecord->IsContinuousTask();
428     processData.isKeepAlive = appRecord->IsKeepAliveApp();
429     processData.isFocused = appRecord->GetFocusFlag();
430     processData.requestProcCode = appRecord->GetRequestProcCode();
431     processData.processChangeReason = static_cast<int32_t>(appRecord->GetProcessChangeReason());
432     return processData;
433 }
434 
WrapRenderProcessData(const std::shared_ptr<RenderRecord> & renderRecord)435 ProcessData AppStateObserverManager::WrapRenderProcessData(const std::shared_ptr<RenderRecord> &renderRecord)
436 {
437     ProcessData processData;
438     processData.bundleName = renderRecord->GetHostBundleName();
439     processData.pid = renderRecord->GetPid();
440     processData.uid = renderRecord->GetHostUid();
441     return processData;
442 }
443 
ObserverExist(const sptr<IApplicationStateObserver> & observer)444 bool AppStateObserverManager::ObserverExist(const sptr<IApplicationStateObserver> &observer)
445 {
446     if (observer == nullptr) {
447         HILOG_ERROR("The param observer is nullptr.");
448         return false;
449     }
450     std::lock_guard<std::recursive_mutex> lockRegister(observerLock_);
451     for (auto it = appStateObserverMap_.begin(); it != appStateObserverMap_.end(); ++it) {
452         if (it->first->AsObject() == observer->AsObject()) {
453             return true;
454         }
455     }
456     return false;
457 }
458 
AddObserverDeathRecipient(const sptr<IApplicationStateObserver> & observer)459 void AppStateObserverManager::AddObserverDeathRecipient(const sptr<IApplicationStateObserver> &observer)
460 {
461     HILOG_INFO("Add observer death recipient begin.");
462     if (observer == nullptr || observer->AsObject() == nullptr) {
463         HILOG_ERROR("The param observer is nullptr.");
464         return;
465     }
466     auto it = recipientMap_.find(observer->AsObject());
467     if (it != recipientMap_.end()) {
468         HILOG_ERROR("This death recipient has been added.");
469         return;
470     } else {
471         std::weak_ptr<AppStateObserverManager> thisWeakPtr(shared_from_this());
472         sptr<IRemoteObject::DeathRecipient> deathRecipient =
473             new ApplicationStateObserverRecipient([thisWeakPtr](const wptr<IRemoteObject> &remote) {
474                 auto appStateObserverManager = thisWeakPtr.lock();
475                 if (appStateObserverManager) {
476                     appStateObserverManager->OnObserverDied(remote);
477                 }
478             });
479         observer->AsObject()->AddDeathRecipient(deathRecipient);
480         recipientMap_.emplace(observer->AsObject(), deathRecipient);
481     }
482 }
483 
RemoveObserverDeathRecipient(const sptr<IApplicationStateObserver> & observer)484 void AppStateObserverManager::RemoveObserverDeathRecipient(const sptr<IApplicationStateObserver> &observer)
485 {
486     HILOG_INFO("Remove observer death recipient begin.");
487     if (observer == nullptr || observer->AsObject() == nullptr) {
488         HILOG_ERROR("The param observer is nullptr.");
489         return;
490     }
491     auto it = recipientMap_.find(observer->AsObject());
492     if (it != recipientMap_.end()) {
493         it->first->RemoveDeathRecipient(it->second);
494         recipientMap_.erase(it);
495         return;
496     }
497 }
498 
OnObserverDied(const wptr<IRemoteObject> & remote)499 void AppStateObserverManager::OnObserverDied(const wptr<IRemoteObject> &remote)
500 {
501     HILOG_INFO("%{public}s begin", __func__);
502     auto object = remote.promote();
503     if (object == nullptr) {
504         HILOG_ERROR("observer nullptr.");
505         return;
506     }
507     sptr<IApplicationStateObserver> observer = iface_cast<IApplicationStateObserver>(object);
508     UnregisterApplicationStateObserver(observer);
509 }
510 
WrapAppStateData(const std::shared_ptr<AppRunningRecord> & appRecord,const ApplicationState state)511 AppStateData AppStateObserverManager::WrapAppStateData(const std::shared_ptr<AppRunningRecord> &appRecord,
512     const ApplicationState state)
513 {
514     AppStateData appStateData;
515     appStateData.pid = appRecord->GetPriorityObject()->GetPid();
516     appStateData.bundleName = appRecord->GetBundleName();
517     appStateData.state = static_cast<int32_t>(state);
518     appStateData.uid = appRecord->GetUid();
519     appStateData.accessTokenId = static_cast<int32_t>(appRecord->GetApplicationInfo()->accessTokenId);
520     appStateData.isFocused = appRecord->GetFocusFlag();
521     return appStateData;
522 }
523 }  // namespace AppExecFwk
524 }  // namespace OHOS
525