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
25 namespace OHOS {
26 namespace Notification {
GetActiveUserId()27 int32_t NotificationDialog::GetActiveUserId()
28 {
29 std::vector<int32_t> activeUserId;
30 auto errCode = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId);
31 if (errCode != ERR_OK) {
32 ANS_LOGE("Query active accountIds failed with %{public}d.", errCode);
33 return AppExecFwk::Constants::ANY_USERID;
34 }
35
36 if (activeUserId.empty()) {
37 ANS_LOGE("Active accountIds is empty.");
38 return AppExecFwk::Constants::ANY_USERID;
39 }
40
41 return activeUserId.front();
42 }
43
GetUidByBundleName(const std::string & bundleName)44 int32_t NotificationDialog::GetUidByBundleName(const std::string &bundleName)
45 {
46 auto userId = NotificationDialog::GetActiveUserId();
47 return IN_PROCESS_CALL(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, userId));
48 }
49
StartEnableNotificationDialogAbility(const std::string & serviceBundleName,const std::string & serviceAbilityName,int32_t uid,const sptr<IRemoteObject> & callerToken)50 ErrCode NotificationDialog::StartEnableNotificationDialogAbility(
51 const std::string &serviceBundleName,
52 const std::string &serviceAbilityName,
53 int32_t uid,
54 const sptr<IRemoteObject> &callerToken)
55 {
56 ANS_LOGD("%{public}s, Enter.", __func__);
57 auto appBundleName = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName());
58 auto topUid = NotificationDialog::GetUidByBundleName(appBundleName);
59 if (topUid != uid) {
60 ANS_LOGE("Current application isn't in foreground, top is %{private}s.", appBundleName.c_str());
61 return ERR_ANS_INVALID_BUNDLE;
62 }
63
64 AAFwk::Want want;
65 want.SetElementName(serviceBundleName, serviceAbilityName);
66 want.SetParam("from", appBundleName);
67 if (callerToken != nullptr) {
68 want.SetParam("callerToken", callerToken);
69 }
70 auto result = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want));
71 ANS_LOGD("End, result = %{public}d", result);
72 return result;
73 }
74 } // namespace Notification
75 } // namespace OHOS
76