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 #include <thread>
28 #include <chrono>
29
30 namespace OHOS {
31 namespace Notification {
32 constexpr int32_t DEFAULT_VALUE = -1;
33 const int32_t SLEEP_TIME = 200;
34
GetUidByBundleName(const std::string & bundleName)35 int32_t NotificationDialog::GetUidByBundleName(const std::string &bundleName)
36 {
37 int32_t userId = AppExecFwk::Constants::ANY_USERID;
38 OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId);
39 return IN_PROCESS_CALL(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, userId));
40 }
41
StartEnableNotificationDialogAbility(const std::string & serviceBundleName,const std::string & serviceAbilityName,int32_t uid,std::string appBundleName,const sptr<IRemoteObject> & callerToken,const bool innerLake)42 ErrCode NotificationDialog::StartEnableNotificationDialogAbility(
43 const std::string &serviceBundleName,
44 const std::string &serviceAbilityName,
45 int32_t uid,
46 std::string appBundleName,
47 const sptr<IRemoteObject> &callerToken,
48 const bool innerLake)
49 {
50 ANS_LOGD("%{public}s, Enter.", __func__);
51
52 auto topBundleName = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName());
53 if (topBundleName != appBundleName) {
54 ANS_LOGE("Current application isn't in foreground, top is %{public}s.", topBundleName.c_str());
55 if (!innerLake) {
56 return ERR_ANS_INVALID_BUNDLE;
57 } else {
58 ANS_LOGE("get top ability again");
59 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
60 topBundleName = IN_PROCESS_CALL(
61 AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName());
62 if (topBundleName != appBundleName) {
63 ANS_LOGE("get top ability again failed");
64 return ERR_ANS_INVALID_BUNDLE;
65 }
66 }
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 root["innerLake"] = innerLake;
81 std::string command = root.dump();
82
83 auto connection_ = sptr<SystemDialogConnectStb>(new (std::nothrow) SystemDialogConnectStb(command));
84 if (connection_ == nullptr) {
85 ANS_LOGD("new connection error.");
86 return ERR_NO_MEMORY;
87 }
88
89 std::string identity = IPCSkeleton::ResetCallingIdentity();
90
91 auto result = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want,
92 connection_, nullptr, DEFAULT_VALUE);
93 if (result != ERR_OK) {
94 ANS_LOGD("connect sceneboard systemdiaolog fail, result = %{public}d", result);
95 bundleName = "com.ohos.systemui";
96 abilityName = "com.ohos.systemui.dialog";
97 want.SetElementName(bundleName, abilityName);
98 result = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection_, nullptr,
99 DEFAULT_VALUE);
100 }
101
102 IPCSkeleton::SetCallingIdentity(identity);
103
104 ANS_LOGD("End, result = %{public}d", result);
105 return result;
106 }
107 } // namespace Notification
108 } // namespace OHOS
109