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 = GetActiveUserId();
47 return IN_PROCESS_CALL(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, userId));
48 }
49
StartEnableNotificationDialogAbility(int32_t uid,const sptr<IRemoteObject> & callerToken)50 ErrCode NotificationDialog::StartEnableNotificationDialogAbility(int32_t uid, const sptr<IRemoteObject> &callerToken)
51 {
52 ANS_LOGD("%{public}s, Enter.", __func__);
53 auto bundleName = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName());
54 auto topUid = GetUidByBundleName(bundleName);
55 if (topUid != uid) {
56 ANS_LOGE("Current application isn't in foreground, top is %{private}s.", bundleName.c_str());
57 return ERR_ANS_INVALID_BUNDLE;
58 }
59
60 AAFwk::Want want;
61 want.SetElementName("com.ohos.notificationdialog", "EnableNotificationDialog");
62 want.SetParam("from", bundleName);
63 if (callerToken != nullptr) {
64 want.SetParam("callerToken", callerToken);
65 }
66 auto result = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want));
67 ANS_LOGD("End, result = %{public}d", result);
68 return result;
69 }
70 } // namespace Notification
71 } // namespace OHOS