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,const bool easyAbroad)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 const bool easyAbroad)
50 {
51 ANS_LOGD("%{public}s, Enter.", __func__);
52
53 auto topBundleName = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName());
54 if (topBundleName != appBundleName) {
55 ANS_LOGW("App isn't in foreground, top %{public}s.", topBundleName.c_str());
56 if (!innerLake) {
57 return ERR_ANS_INVALID_BUNDLE;
58 } else {
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 return ERR_ANS_INVALID_BUNDLE;
64 }
65 }
66 }
67 ANS_LOGI("called");
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 root["easyAbroad"] = easyAbroad;
82 std::string command = root.dump();
83
84 auto connection_ = sptr<SystemDialogConnectStb>(new (std::nothrow) SystemDialogConnectStb(command));
85 if (connection_ == nullptr) {
86 ANS_LOGE("new connection error.");
87 return ERR_NO_MEMORY;
88 }
89
90 std::string identity = IPCSkeleton::ResetCallingIdentity();
91
92 auto result = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want,
93 connection_, nullptr, DEFAULT_VALUE);
94 if (result != ERR_OK) {
95 ANS_LOGW("connect fail, result = %{public}d", result);
96 bundleName = "com.ohos.systemui";
97 abilityName = "com.ohos.systemui.dialog";
98 want.SetElementName(bundleName, abilityName);
99 result = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection_, nullptr,
100 DEFAULT_VALUE);
101 }
102
103 IPCSkeleton::SetCallingIdentity(identity);
104
105 ANS_LOGD("End, result = %{public}d", result);
106 return result;
107 }
108 } // namespace Notification
109 } // namespace OHOS
110