• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "networkshare_notification.h"
17 
18 #include "ability_manager_ipc_interface_code.h"
19 #include "extension_manager_client.h"
20 #include "iservice_registry.h"
21 #include "message_parcel.h"
22 #include "system_ability_definition.h"
23 #include "netmgr_ext_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 
GetInstance()28 NetworkShareNotification &NetworkShareNotification::GetInstance()
29 {
30     static NetworkShareNotification instance;
31     return instance;
32 }
33 
PublishNetworkShareNotification(NotificationId notificationId)34 void NetworkShareNotification::PublishNetworkShareNotification(NotificationId notificationId)
35 {
36     NETMGR_EXT_LOG_I("Publishing notification, id [%{public}d]", static_cast<int>(notificationId));
37     AAFwk::Want want;
38     want.SetElementName("com.ohos.locationdialog", "HotSpotServiceAbility");
39     want.SetParam("operateType", static_cast<int>(NotificationOpetationType::PUBLISH));
40     want.SetParam("notificationId", static_cast<int>(notificationId));
41     auto result = StartAbility(want);
42     isNtfPublished = true;
43     NETMGR_EXT_LOG_I("Publishing notification End, result = %{public}d", result);
44 }
45 
CancelNetworkShareNotification(NotificationId notificationId)46 void NetworkShareNotification::CancelNetworkShareNotification(NotificationId notificationId)
47 {
48     NETMGR_EXT_LOG_I("Cancel notification, id [%{public}d]", static_cast<int>(notificationId));
49     if (!isNtfPublished) {
50         return;
51     }
52     AAFwk::Want want;
53     want.SetElementName("com.ohos.locationdialog", "HotSpotServiceAbility");
54     want.SetParam("operateType", static_cast<int>(NotificationOpetationType::CANCEL));
55     want.SetParam("notificationId", static_cast<int>(notificationId));
56     auto result = StartAbility(want);
57     isNtfPublished = false;
58     NETMGR_EXT_LOG_I("Cancel notification End, result = %{public}d", result);
59 }
60 
StartAbility(OHOS::AAFwk::Want & want)61 int32_t NetworkShareNotification::StartAbility(OHOS::AAFwk::Want& want)
62 {
63     sptr<ISystemAbilityManager> systemAbilityManager =
64         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65     if (systemAbilityManager == nullptr) {
66         NETMGR_EXT_LOG_E("systemAbilityManager is nullptr");
67         return -1;
68     }
69     sptr<IRemoteObject> remote = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
70     if (remote == nullptr) {
71         NETMGR_EXT_LOG_E("remote is nullptr");
72         return -1;
73     }
74 
75     int error;
76     MessageParcel data;
77     MessageParcel reply;
78     MessageOption option;
79 
80     if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
81         return -1;
82     }
83     if (!data.WriteParcelable(&want)) {
84         NETMGR_EXT_LOG_E("want write failed.");
85         return -1;
86     }
87 
88     if (!data.WriteInt32(DEFAULT_INVAL_VALUE)) {
89         NETMGR_EXT_LOG_E("userId write failed.");
90         return -1;
91     }
92 
93     if (!data.WriteInt32(DEFAULT_INVAL_VALUE)) {
94         NETMGR_EXT_LOG_E("requestCode write failed.");
95         return -1;
96     }
97     uint32_t task =  static_cast<uint32_t>(AAFwk::AbilityManagerInterfaceCode::START_ABILITY);
98     error = remote->SendRequest(task, data, reply, option);
99     if (error != NO_ERROR) {
100         NETMGR_EXT_LOG_E("Send request error: %{public}d", error);
101         return error;
102     }
103     return reply.ReadInt32();
104 }
105 } // namespace NetManagerStandard
106 } // namespace OHOS