• 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 "update_notify.h"
17 
18 #include "iservice_registry.h"
19 
20 #include "update_helper.h"
21 #include "update_log.h"
22 
23 using namespace OHOS;
24 using namespace OHOS::AAFwk;
25 using namespace OHOS::AppExecFwk;
26 using namespace std;
27 
28 namespace OHOS {
29 namespace UpdateEngine {
30 static sptr<AAFwk::IAbilityManager> g_abilityMgr;
31 
InitAbilityManagerService()32 bool UpdateNotify::InitAbilityManagerService()
33 {
34     if (g_abilityMgr != nullptr) {
35         return true;
36     }
37     sptr<ISystemAbilityManager> systemAbilityManager =
38         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39     if (systemAbilityManager == nullptr) {
40         ENGINE_LOGE("Failed to get system ability mgr.");
41         return false;
42     }
43     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
44     if (remoteObject == nullptr) {
45         ENGINE_LOGE("Failed to get ability manager service.");
46         return false;
47     }
48     g_abilityMgr = iface_cast<IAbilityManager>(remoteObject);
49     if ((g_abilityMgr == nullptr) || (g_abilityMgr->AsObject() == nullptr)) {
50         ENGINE_LOGE("Failed to get system ability manager services ability");
51         return false;
52     }
53     return true;
54 }
55 
StartAbility(const Want & want)56 ErrCode UpdateNotify::StartAbility(const Want &want)
57 {
58     if (!InitAbilityManagerService()) {
59         ENGINE_LOGE("failed to get ability manager service");
60         return OHOS::ERR_INVALID_VALUE;
61     }
62     ErrCode result = g_abilityMgr->StartAbility(want);
63     ENGINE_LOGI("StartAbility result %{public}d", result);
64     return result;
65 }
66 
StopServiceAbility(const Want & want)67 ErrCode UpdateNotify::StopServiceAbility(const Want &want)
68 {
69     if (!InitAbilityManagerService()) {
70         ENGINE_LOGE("failed to get ability manager service");
71         return OHOS::ERR_INVALID_VALUE;
72     }
73     ErrCode result = g_abilityMgr->StopServiceAbility(want);
74     ENGINE_LOGI("StopServiceAbility result %{public}d", result);
75     return result;
76 }
77 
ConnectAbility(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<IRemoteObject> & callerToken)78 ErrCode UpdateNotify::ConnectAbility(const Want &want, const sptr<IAbilityConnection> &connect,
79     const sptr<IRemoteObject> &callerToken)
80 {
81     if (!InitAbilityManagerService()) {
82         ENGINE_LOGE("failed to get ability manager service");
83         return OHOS::ERR_INVALID_VALUE;
84     }
85     ErrCode result = g_abilityMgr->ConnectAbility(want, connect, callerToken);
86     ENGINE_LOGI("ConnectAbility result %{public}d", result);
87     return result;
88 }
89 
DisconnectAbility(const sptr<IAbilityConnection> & connect)90 ErrCode UpdateNotify::DisconnectAbility(const sptr<IAbilityConnection> &connect)
91 {
92     if (!InitAbilityManagerService()) {
93         ENGINE_LOGE("failed to get ability manager service");
94         return OHOS::ERR_INVALID_VALUE;
95     }
96     ErrCode result = g_abilityMgr->DisconnectAbility(connect);
97     ENGINE_LOGI("DisconnectAbility result %{public}d", result);
98     return result;
99 }
100 
MakeWant(const std::string & deviceId,const std::string & abilityName,const std::string & bundleName,const std::string & subscribeInfo,const std::string & params)101 Want UpdateNotify::MakeWant(const std::string &deviceId, const std::string &abilityName, const std::string &bundleName,
102     const std::string &subscribeInfo, const std::string &params)
103 {
104     ElementName element(deviceId, bundleName, abilityName);
105     Want want;
106     want.SetElement(element);
107     want.SetParam("EventInfo", params);
108     want.SetParam("SubscribeInfo", subscribeInfo);
109     return want;
110 }
111 
NotifyToAppService(const std::string & eventInfo,const std::string & subscribeInfo)112 void UpdateNotify::NotifyToAppService(const std::string &eventInfo, const std::string &subscribeInfo)
113 {
114     if (eventInfo.empty()) {
115         ENGINE_LOGE("NotifyToAppService eventInfo error.");
116         return;
117     }
118     string bundleName = OUC_PACKAGE_NAME;
119     string abilityName = OUC_SERVICE_EXT_ABILITY_NAME;
120     Want want = MakeWant("", abilityName, bundleName, subscribeInfo, eventInfo);
121     ErrCode result = StartAbility(want);
122     if (result != OHOS::ERR_OK) {
123         ENGINE_LOGE("NotifyToAppService fail.");
124     }
125 }
126 
OnAbilityConnectDone(const ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)127 void NotifyConnection::OnAbilityConnectDone(const ElementName &element,
128     const sptr<IRemoteObject> &remoteObject, int32_t resultCode)
129 {
130     ENGINE_LOGI("OnAbilityConnectDone successfully. result %{public}d", resultCode);
131 }
132 
OnAbilityDisconnectDone(const ElementName & element,int resultCode)133 void NotifyConnection::OnAbilityDisconnectDone(const ElementName &element, int resultCode)
134 {
135     ENGINE_LOGI("OnAbilityDisconnectDone successfully. result %{public}d", resultCode);
136 }
137 } // namespace UpdateEngine
138 } // namespace OHOS