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