• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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() {
23    super(ERROR_MSG_INVALID_PARAM);
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  }
35
36  onUpdateConfiguration(config) {
37    this.config = config;
38  }
39
40  startAbility(want, options, callback) {
41    return this.__context_impl__.startAbility(want, options, callback);
42  }
43
44  startAbilityAsCaller(want, options, callback) {
45    return this.__context_impl__.startAbilityAsCaller(want, options, callback);
46  }
47
48  startRecentAbility(want, options, callback) {
49    return this.__context_impl__.startRecentAbility(want, options, callback);
50  }
51
52  startAbilityWithAccount(want, accountId, options, callback) {
53    return this.__context_impl__.startAbilityWithAccount(want, accountId, options, callback);
54  }
55
56  startAbilityByCall(want) {
57    return new Promise(async (resolve, reject) => {
58      if (typeof want !== 'object' || want == null) {
59        console.log('AbilityContext::startAbilityByCall input param error');
60        reject(new ParamError());
61        return;
62      }
63
64      let callee = null;
65      try {
66        callee = await this.__context_impl__.startAbilityByCall(want);
67      } catch (error) {
68        console.log('AbilityContext::startAbilityByCall Obtain remoteObject failed');
69        reject(error);
70        return;
71      }
72
73      resolve(new Caller(callee));
74      console.log('AbilityContext::startAbilityByCall success');
75      return;
76    });
77  }
78
79  startAbilityByCallWithAccount(want, accountId) {
80    return new Promise(async (resolve, reject) => {
81      if (typeof want !== 'object' || want == null || typeof accountId !== 'number') {
82        console.log('AbilityContext::startAbilityByCall With accountId input param error');
83        reject(new ParamError());
84        return;
85      }
86
87      let callee = null;
88      try {
89        callee = await this.__context_impl__.startAbilityByCall(want, accountId);
90      } catch (error) {
91        console.log('AbilityContext::startAbilityByCall With accountId Obtain remoteObject failed');
92        reject(error);
93        return;
94      }
95
96      resolve(new Caller(callee));
97      console.log('AbilityContext::startAbilityByCall With accountId success');
98      return;
99    });
100  }
101
102  startAbilityForResult(want, options, callback) {
103    return this.__context_impl__.startAbilityForResult(want, options, callback);
104  }
105
106  startAbilityForResultWithAccount(want, accountId, options, callback) {
107    return this.__context_impl__.startAbilityForResultWithAccount(want, accountId, options, callback);
108  }
109
110  startServiceExtensionAbility(want, callback) {
111    return this.__context_impl__.startServiceExtensionAbility(want, callback);
112  }
113
114  startServiceExtensionAbilityWithAccount(want, accountId, callback) {
115    return this.__context_impl__.startServiceExtensionAbilityWithAccount(want, accountId, callback);
116  }
117
118  stopServiceExtensionAbility(want, callback) {
119    return this.__context_impl__.stopServiceExtensionAbility(want, callback);
120  }
121
122  stopServiceExtensionAbilityWithAccount(want, accountId, callback) {
123    return this.__context_impl__.stopServiceExtensionAbilityWithAccount(want, accountId, callback);
124  }
125
126  connectAbility(want, options) {
127    return this.__context_impl__.connectAbility(want, options);
128  }
129
130  connectServiceExtensionAbility(want, options) {
131    return this.__context_impl__.connectServiceExtensionAbility(want, options);
132  }
133
134  connectAbilityWithAccount(want, accountId, options) {
135    return this.__context_impl__.connectAbilityWithAccount(want, accountId, options);
136  }
137
138  connectServiceExtensionAbilityWithAccount(want, accountId, options) {
139    return this.__context_impl__.connectServiceExtensionAbilityWithAccount(want, accountId, options);
140  }
141
142  disconnectAbility(connection, callback) {
143    return this.__context_impl__.disconnectAbility(connection, callback);
144  }
145
146  disconnectServiceExtensionAbility(connection, callback) {
147    return this.__context_impl__.disconnectServiceExtensionAbility(connection, callback);
148  }
149
150  terminateSelf(callback) {
151    return this.__context_impl__.terminateSelf(callback);
152  }
153
154  isTerminating() {
155    return this.__context_impl__.isTerminating();
156  }
157
158  terminateSelfWithResult(abilityResult, callback) {
159    return this.__context_impl__.terminateSelfWithResult(abilityResult, callback);
160  }
161
162  restoreWindowStage(contentStorage) {
163    return this.__context_impl__.restoreWindowStage(contentStorage);
164  }
165
166  setMissionContinueState(state, callback) {
167    return this.__context_impl__.setMissionContinueState(state, callback);
168  }
169
170  setMissionLabel(label, callback) {
171    return this.__context_impl__.setMissionLabel(label, callback);
172  }
173
174  setMissionIcon(icon, callback) {
175    return this.__context_impl__.setMissionIcon(icon, callback);
176  }
177
178  requestDialogService(want, resultCallback) {
179    return this.__context_impl__.requestDialogService(want, resultCallback);
180  }
181
182  reportDrawnCompleted(callback) {
183    return this.__context_impl__.reportDrawnCompleted(callback);
184  }
185
186  startAbilityByType(type, wantParam, abilityStartCallback, callback) {
187    return this.__context_impl__.startAbilityByType(type, wantParam, abilityStartCallback, callback);
188  }
189
190  requestModalUIExtension(want, callback) {
191    return this.__context_impl__.requestModalUIExtension(want, callback);
192  }
193}
194
195export default AbilityContext;
196