• 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 "notification_dialog.h"
17 
18 #include "ability_manager_client.h"
19 #include "ans_inner_errors.h"
20 #include "ans_log_wrapper.h"
21 #include "bundle_manager_helper.h"
22 #include "in_process_call_wrapper.h"
23 #include "os_account_manager.h"
24 #include "system_dialog_connect_stb.h"
25 #include "extension_manager_client.h"
26 
27 namespace OHOS {
28 namespace Notification {
29 constexpr int32_t DEFAULT_VALUE = -1;
GetActiveUserId()30 int32_t NotificationDialog::GetActiveUserId()
31 {
32     std::vector<int32_t> activeUserId;
33     auto errCode = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId);
34     if (errCode != ERR_OK) {
35         ANS_LOGE("Query active accountIds failed with %{public}d.", errCode);
36         return AppExecFwk::Constants::ANY_USERID;
37     }
38 
39     if (activeUserId.empty()) {
40         ANS_LOGE("Active accountIds is empty.");
41         return AppExecFwk::Constants::ANY_USERID;
42     }
43 
44     return activeUserId.front();
45 }
46 
GetUidByBundleName(const std::string & bundleName)47 int32_t NotificationDialog::GetUidByBundleName(const std::string &bundleName)
48 {
49     auto userId = NotificationDialog::GetActiveUserId();
50     return IN_PROCESS_CALL(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, userId));
51 }
52 
StartEnableNotificationDialogAbility(const std::string & serviceBundleName,const std::string & serviceAbilityName,int32_t uid,std::string appBundleName,const sptr<IRemoteObject> & callerToken)53 ErrCode NotificationDialog::StartEnableNotificationDialogAbility(
54     const std::string &serviceBundleName,
55     const std::string &serviceAbilityName,
56     int32_t uid,
57     std::string appBundleName,
58     const sptr<IRemoteObject> &callerToken)
59 {
60     ANS_LOGD("%{public}s, Enter.", __func__);
61 
62     auto topBundleName = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName());
63     auto topUid = NotificationDialog::GetUidByBundleName(topBundleName);
64     if (topBundleName != appBundleName) {
65         ANS_LOGE("Current application isn't in foreground, top is %{public}s.", topBundleName.c_str());
66         return ERR_ANS_INVALID_BUNDLE;
67     }
68 
69     AAFwk::Want want;
70 
71     std::string bundleName = "com.ohos.sceneboard";
72     std::string abilityName = "com.ohos.sceneboard.systemdialog";
73     want.SetElementName(bundleName, abilityName);
74 
75     nlohmann::json root;
76     std::string uiExtensionType = "sysDialog/common";
77     root["bundleName"] = appBundleName;
78     root["bundleUid"] = uid;
79     root["ability.want.params.uiExtensionType"] = uiExtensionType;
80     std::string command  = root.dump();
81 
82     auto connection_ = sptr<SystemDialogConnectStb>(new (std::nothrow) SystemDialogConnectStb(command));
83     if (connection_ == nullptr) {
84         ANS_LOGD("new connection error.");
85         return ERR_NO_MEMORY;
86     }
87 
88     std::string identity = IPCSkeleton::ResetCallingIdentity();
89 
90     auto result = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want,
91     connection_, nullptr, DEFAULT_VALUE);
92     if (result != ERR_OK) {
93         ANS_LOGD("connect sceneboard systemdiaolog fail, result = %{public}d", result);
94         bundleName = "com.ohos.systemui";
95         abilityName = "com.ohos.systemui.dialog";
96         want.SetElementName(bundleName, abilityName);
97         result = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection_, nullptr,
98         DEFAULT_VALUE);
99     }
100 
101     IPCSkeleton::SetCallingIdentity(identity);
102 
103     ANS_LOGD("End, result = %{public}d", result);
104     return result;
105 }
106 }  // namespace Notification
107 }  // namespace OHOS
108