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