• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "bundle_resource_register.h"
17 
18 #include "app_log_wrapper.h"
19 #include "bundle_resource_event_subscriber.h"
20 #ifdef ABILITY_RUNTIME_ENABLE
21 #include "app_mgr_client.h"
22 #include "bundle_resource_observer.h"
23 #endif
24 #include "common_event_manager.h"
25 #include "common_event_support.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
RegisterConfigurationObserver()29 void BundleResourceRegister::RegisterConfigurationObserver()
30 {
31 #ifdef ABILITY_RUNTIME_ENABLE
32     APP_LOGI("start");
33     sptr<IConfigurationObserver> observer(new (std::nothrow) BundleResourceObserver());
34     if (observer == nullptr) {
35         APP_LOGE("fail to create observer");
36         return;
37     }
38     auto appMgrClient = std::make_unique<AppMgrClient>();
39     if (appMgrClient == nullptr) {
40         APP_LOGE("fail to create appMgrClient");
41         return;
42     }
43     appMgrClient->RegisterConfigurationObserver(observer);
44     APP_LOGI("end");
45 #else
46     APP_LOGI("ability runtime not support");
47 #endif
48 }
49 
RegisterCommonEventSubscriber()50 void BundleResourceRegister::RegisterCommonEventSubscriber()
51 {
52     APP_LOGI("start");
53     EventFwk::MatchingSkills matchingSkills;
54     // for user changed
55     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
56     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
57     subscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
58 
59     auto subscriberPtr = std::make_shared<BundleResourceEventSubscriber>(subscribeInfo);
60     if (!EventFwk::CommonEventManager::SubscribeCommonEvent(subscriberPtr)) {
61         APP_LOGE("subscribe event %{public}s failed",
62             EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED.c_str());
63         return;
64     }
65     APP_LOGI("end");
66 }
67 } // AppExecFwk
68 } // OHOS
69