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 "advanced_notification_service.h"
20 #include "in_process_call_wrapper.h"
21 #include "ipc_skeleton.h"
22
23 namespace OHOS {
24 namespace Notification {
JudgeSelfCalled(const std::shared_ptr<AAFwk::AbilityRecord> & abilityRecord)25 bool NotificationDialog::JudgeSelfCalled(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord)
26 {
27 auto callingTokenId = IPCSkeleton::GetCallingTokenID();
28 auto tokenID = abilityRecord->GetApplicationInfo().accessTokenId;
29 if (callingTokenId != tokenID) {
30 ANS_LOGE("Is not self, not enabled");
31 return false;
32 }
33 return true;
34 }
35
StartEnableNotificationDialogAbility()36 ErrCode NotificationDialog::StartEnableNotificationDialogAbility()
37 {
38 ANS_LOGD("%{public}s, Enter.", __func__);
39 sptr<IRemoteObject> token;
40 int result = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility(token));
41 std::shared_ptr<AAFwk::AbilityRecord> ability = AAFwk::Token::GetAbilityRecordByToken(token);
42 if (result != ERR_OK) {
43 ANS_LOGD("%{public}s, GetTopAbility failed. result=%{public}d", __func__, result);
44 return result;
45 }
46 if (!JudgeSelfCalled(ability)) {
47 ANS_LOGD("%{public}s, if it is not selfcalled.", __func__);
48 return result;
49 }
50 AAFwk::Want want;
51 std::string bundleName = IN_PROCESS_CALL(
52 AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName());
53 want.SetElementName("com.ohos.notificationdialog", "EnableNotificationDialog");
54 want.SetParam("tokenId", token);
55 want.SetParam("from", bundleName);
56 result = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token, -1);
57 ANS_LOGD("%{public}s, End Calling StartNotificationDialog. result=%{public}d", __func__, result);
58 return result;
59 }
60 } // namespace Notification
61 } // namespace OHOS