1/*
2 * Copyright (c) 2021-2023 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
16let Context = requireNapi('application.Context');
17let Caller = requireNapi('application.Caller');
18
19const ERROR_CODE_INVALID_PARAM = 401;
20const ERROR_MSG_INVALID_PARAM = 'Invalid input parameter.';
21class ParamError extends Error {
22  constructor(msg) {
23    super(msg);
24    this.code = ERROR_CODE_INVALID_PARAM;
25  }
26}
27
28class AbilityContext extends Context {
29  constructor(obj) {
30    super(obj);
31    this.abilityInfo = obj.abilityInfo;
32    this.currentHapModuleInfo = obj.currentHapModuleInfo;
33    this.config = obj.config;
34    this.windowStage = obj.windowStage;
35  }
36
37  onUpdateConfiguration(config) {
38    this.config = config;
39  }
40
41  startAbility(want, options, callback) {
42    return this.__context_impl__.startAbility(want, options, callback);
43  }
44
45  openLink(link, options, callback) {
46    return this.__context_impl__.openLink(link, options, callback);
47  }
48
49  startAbilityAsCaller(want, options, callback) {
50    return this.__context_impl__.startAbilityAsCaller(want, options, callback);
51  }
52
53  startRecentAbility(want, options, callback) {
54    return this.__context_impl__.startRecentAbility(want, options, callback);
55  }
56
57  startAbilityWithAccount(want, accountId, options, callback) {
58    return this.__context_impl__.startAbilityWithAccount(want, accountId, options, callback);
59  }
60
61  startAbilityByCall(want) {
62    return new Promise(async (resolve, reject) => {
63      if (typeof want !== 'object' || want == null) {
64        console.log('AbilityContext::startAbilityByCall input param error');
65        reject(new ParamError('Parse param want failed, want must be Want'));
66        return;
67      }
68
69      let callee = null;
70      try {
71        callee = await this.__context_impl__.startAbilityByCall(want);
72      } catch (error) {
73        console.log('AbilityContext::startAbilityByCall Obtain remoteObject failed');
74        reject(error);
75        return;
76      }
77
78      resolve(new Caller(callee));
79      console.log('AbilityContext::startAbilityByCall success');
80      return;
81    });
82  }
83
84  startAbilityByCallWithAccount(want, accountId) {
85    return new Promise(async (resolve, reject) => {
86      if (typeof want !== 'object' || want == null || typeof accountId !== 'number') {
87        console.log('AbilityContext::startAbilityByCall With accountId input param error');
88        reject(new ParamError(
89          'Parse param want or accountId failed, want must be Want and accountId must be number'));
90        return;
91      }
92
93      let callee = null;
94      try {
95        callee = await this.__context_impl__.startAbilityByCall(want, accountId);
96      } catch (error) {
97        console.log('AbilityContext::startAbilityByCall With accountId Obtain remoteObject failed');
98        reject(error);
99        return;
100      }
101
102      resolve(new Caller(callee));
103      console.log('AbilityContext::startAbilityByCall With accountId success');
104      return;
105    });
106  }
107
108  startAbilityForResult(want, options, callback) {
109    return this.__context_impl__.startAbilityForResult(want, options, callback);
110  }
111
112  startAbilityForResultWithAccount(want, accountId, options, callback) {
113    return this.__context_impl__.startAbilityForResultWithAccount(want, accountId, options, callback);
114  }
115
116  startUIServiceExtensionAbility(want, callback) {
117    return this.__context_impl__.startUIServiceExtensionAbility(want, callback);
118  }
119
120  connectUIServiceExtensionAbility(want, callback) {
121    return this.__context_impl__.connectUIServiceExtensionAbility(want, callback);
122  }
123
124  disconnectUIServiceExtensionAbility(proxy) {
125    return this.__context_impl__.disconnectUIServiceExtensionAbility(proxy);
126  }
127
128  startServiceExtensionAbility(want, callback) {
129    return this.__context_impl__.startServiceExtensionAbility(want, callback);
130  }
131
132  startServiceExtensionAbilityWithAccount(want, accountId, callback) {
133    return this.__context_impl__.startServiceExtensionAbilityWithAccount(want, accountId, callback);
134  }
135
136  stopServiceExtensionAbility(want, callback) {
137    return this.__context_impl__.stopServiceExtensionAbility(want, callback);
138  }
139
140  stopServiceExtensionAbilityWithAccount(want, accountId, callback) {
141    return this.__context_impl__.stopServiceExtensionAbilityWithAccount(want, accountId, callback);
142  }
143
144  connectServiceExtensionAbility(want, options) {
145    return this.__context_impl__.connectServiceExtensionAbility(want, options);
146  }
147
148  connectAbilityWithAccount(want, accountId, options) {
149    return this.__context_impl__.connectAbilityWithAccount(want, accountId, options);
150  }
151
152  connectServiceExtensionAbilityWithAccount(want, accountId, options) {
153    return this.__context_impl__.connectServiceExtensionAbilityWithAccount(want, accountId, options);
154  }
155
156  disconnectAbility(connection, callback) {
157    return this.__context_impl__.disconnectAbility(connection, callback);
158  }
159
160  disconnectServiceExtensionAbility(connection, callback) {
161    return this.__context_impl__.disconnectServiceExtensionAbility(connection, callback);
162  }
163
164  terminateSelf(callback) {
165    return this.__context_impl__.terminateSelf(callback);
166  }
167
168  isTerminating() {
169    return this.__context_impl__.isTerminating();
170  }
171
172  terminateSelfWithResult(abilityResult, callback) {
173    return this.__context_impl__.terminateSelfWithResult(abilityResult, callback);
174  }
175
176  backToCallerAbilityWithResult(abilityResult, requestCode) {
177    return this.__context_impl__.backToCallerAbilityWithResult(abilityResult, requestCode);
178  }
179
180  restoreWindowStage(contentStorage) {
181    return this.__context_impl__.restoreWindowStage(contentStorage);
182  }
183
184  setMissionContinueState(state, callback) {
185    return this.__context_impl__.setMissionContinueState(state, callback);
186  }
187
188  setMissionLabel(label, callback) {
189    return this.__context_impl__.setMissionLabel(label, callback);
190  }
191
192  setMissionIcon(icon, callback) {
193    return this.__context_impl__.setMissionIcon(icon, callback);
194  }
195
196  requestDialogService(want, resultCallback) {
197    return this.__context_impl__.requestDialogService(want, resultCallback);
198  }
199
200  reportDrawnCompleted(callback) {
201    return this.__context_impl__.reportDrawnCompleted(callback);
202  }
203
204  startAbilityByType(type, wantParam, abilityStartCallback, callback) {
205    return this.__context_impl__.startAbilityByType(type, wantParam, abilityStartCallback, callback);
206  }
207
208  requestModalUIExtension(want, callback) {
209    return this.__context_impl__.requestModalUIExtension(want, callback);
210  }
211
212  showAbility() {
213    return this.__context_impl__.showAbility();
214  }
215
216  hideAbility() {
217    return this.__context_impl__.hideAbility();
218  }
219
220  openAtomicService(appId, options, callback) {
221    return this.__context_impl__.openAtomicService(appId, options, callback);
222  }
223
224  moveAbilityToBackground(callback) {
225    return this.__context_impl__.moveAbilityToBackground(callback);
226  }
227
228  setRestoreEnabled(enabled) {
229    this.__context_impl__.setRestoreEnabled(enabled);
230  }
231}
232
233export default AbilityContext;
234