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 "print_extension_context.h"
17
18 #include "ability_connection.h"
19 #include "ability_manager_client.h"
20 #include "print_log.h"
21
22 namespace OHOS {
23 namespace AbilityRuntime {
24 const size_t PrintExtensionContext::CONTEXT_TYPE_ID(std::hash<const char *> {}("PrintExtensionContext"));
25 int PrintExtensionContext::ILLEGAL_REQUEST_CODE(-1);
26
StartAbility(const AAFwk::Want & want) const27 ErrCode PrintExtensionContext::StartAbility(const AAFwk::Want &want) const
28 {
29 PRINT_HILOGD("%{public}s begin.", __func__);
30 ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token_, ILLEGAL_REQUEST_CODE);
31 PRINT_HILOGD("%{public}s. End calling StartAbility. ret=%{public}d", __func__, err);
32 if (err != ERR_OK) {
33 PRINT_HILOGE("PrintExtensionContext::StartAbility is failed %{public}d", err);
34 }
35 return err;
36 }
37
StartAbility(const AAFwk::Want & want,const AAFwk::StartOptions & startOptions) const38 ErrCode PrintExtensionContext::StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const
39 {
40 PRINT_HILOGD("%{public}s begin.", __func__);
41 ErrCode err =
42 AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, startOptions, token_, ILLEGAL_REQUEST_CODE);
43 PRINT_HILOGD("%{public}s. End calling StartAbility. ret=%{public}d", __func__, err);
44 if (err != ERR_OK) {
45 PRINT_HILOGE("PrintExtensionContext::StartAbility is failed %{public}d", err);
46 }
47 return err;
48 }
49
ConnectAbility(const AAFwk::Want & want,const sptr<AbilityConnectCallback> & connectCallback) const50 bool PrintExtensionContext::ConnectAbility(
51 const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const
52 {
53 PRINT_HILOGD("%{public}s begin.", __func__);
54 ErrCode ret = ConnectionManager::GetInstance().ConnectAbility(token_, want, connectCallback);
55 PRINT_HILOGD("PrintExtensionContext::ConnectAbility ErrorCode = %{public}d", ret);
56 return ret == ERR_OK;
57 }
58
StartAbilityWithAccount(const AAFwk::Want & want,int accountId) const59 ErrCode PrintExtensionContext::StartAbilityWithAccount(const AAFwk::Want &want, int accountId) const
60 {
61 PRINT_HILOGD("%{public}s begin.", __func__);
62 PRINT_HILOGD("%{public}d accountId:", accountId);
63 ErrCode err =
64 AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token_, ILLEGAL_REQUEST_CODE, accountId);
65 PRINT_HILOGD("%{public}s. End calling StartAbilityWithAccount. ret=%{public}d", __func__, err);
66 if (err != ERR_OK) {
67 PRINT_HILOGE("PrintExtensionContext::StartAbilityWithAccount is failed %{public}d", err);
68 }
69 return err;
70 }
71
StartAbilityWithAccount(const AAFwk::Want & want,int accountId,const AAFwk::StartOptions & startOptions) const72 ErrCode PrintExtensionContext::StartAbilityWithAccount(
73 const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions) const
74 {
75 PRINT_HILOGD("%{public}s begin.", __func__);
76 ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(
77 want, startOptions, token_, ILLEGAL_REQUEST_CODE, accountId);
78 PRINT_HILOGD("%{public}s. End calling StartAbilityWithAccount. ret=%{public}d", __func__, err);
79 if (err != ERR_OK) {
80 PRINT_HILOGE("PrintExtensionContext::StartAbilityWithAccount is failed %{public}d", err);
81 }
82 return err;
83 }
84
ConnectAbilityWithAccount(const AAFwk::Want & want,int accountId,const sptr<AbilityConnectCallback> & connectCallback) const85 bool PrintExtensionContext::ConnectAbilityWithAccount(
86 const AAFwk::Want &want, int accountId, const sptr<AbilityConnectCallback> &connectCallback) const
87 {
88 PRINT_HILOGD("%{public}s begin.", __func__);
89 ErrCode ret = ConnectionManager::GetInstance().ConnectAbilityWithAccount(token_, want, accountId, connectCallback);
90 PRINT_HILOGD("PrintExtensionContext::ConnectAbilityWithAccount ErrorCode = %{public}d", ret);
91 return ret == ERR_OK;
92 }
93
DisconnectAbility(const AAFwk::Want & want,const sptr<AbilityConnectCallback> & connectCallback) const94 ErrCode PrintExtensionContext::DisconnectAbility(
95 const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const
96 {
97 PRINT_HILOGD("%{public}s begin.", __func__);
98 ErrCode ret = ConnectionManager::GetInstance().DisconnectAbility(token_, want.GetElement(), connectCallback);
99 if (ret != ERR_OK) {
100 PRINT_HILOGE("%{public}s end DisconnectAbility error, ret=%{public}d", __func__, ret);
101 }
102 PRINT_HILOGD("%{public}s end DisconnectAbility", __func__);
103 return ret;
104 }
105
TerminateAbility()106 ErrCode PrintExtensionContext::TerminateAbility()
107 {
108 PRINT_HILOGD("%{public}s begin.", __func__);
109 ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->TerminateAbility(token_, -1, nullptr);
110 if (err != ERR_OK) {
111 PRINT_HILOGE("PrintExtensionContext::TerminateAbility is failed %{public}d", err);
112 }
113 PRINT_HILOGD("%{public}s end.", __func__);
114 return err;
115 }
116
GetAbilityInfoType() const117 AppExecFwk::AbilityType PrintExtensionContext::GetAbilityInfoType() const
118 {
119 std::shared_ptr<AppExecFwk::AbilityInfo> info = GetAbilityInfo();
120 if (info == nullptr) {
121 PRINT_HILOGE("PrintExtensionContext::GetAbilityInfoType info == nullptr");
122 return AppExecFwk::AbilityType::UNKNOWN;
123 }
124
125 return info->type;
126 }
127 } // namespace AbilityRuntime
128 } // namespace OHOS