• 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 
16 #ifndef OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H
18 
19 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h"
20 
21 #include "ability_connect_callback.h"
22 #include "ability_info.h"
23 #include "caller_callback.h"
24 #include "configuration.h"
25 #include "iability_callback.h"
26 #include "mission_info.h"
27 #include "native_engine/native_reference.h"
28 #include "native_engine/native_value.h"
29 #include "start_options.h"
30 #include "want.h"
31 
32 #ifdef SUPPORT_GRAPHICS
33 #include "pixel_map.h"
34 #endif
35 
36 namespace OHOS {
37 namespace Ace {
38 class UIContent;
39 }
40 
41 namespace AbilityRuntime {
42 using RuntimeTask = std::function<void(int, const AAFwk::Want&, bool)>;
43 using PermissionRequestTask = std::function<void(const std::vector<std::string>&, const std::vector<int>&)>;
44 using RequestDialogResultTask = std::function<void(int32_t resultCode, const AAFwk::Want&)>;
45 class LocalCallContainer;
46 constexpr int32_t DEFAULT_INVAL_VALUE = -1;
47 class AbilityContext : public Context {
48 public:
49     virtual ~AbilityContext() = default;
50 
51     /**
52      * @brief Starts a new ability.
53      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
54      * to start a specific ability. The system locates the target ability from installed abilities based on the value
55      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
56      *
57      * @param want Indicates the Want containing information about the target ability to start.
58      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
59      * template is started. You can define the request code to identify the results returned by abilities. The value
60      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
61      * template.
62      */
63     virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) = 0;
64 
65     /**
66      * @brief Starts a new ability.
67      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
68      * to start a specific ability. The system locates the target ability from installed abilities based on the value
69      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
70      *
71      * @param want Indicates the Want containing information about the target ability to start.
72      * @param accountId ability caller accountId.
73      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
74      * template is started. You can define the request code to identify the results returned by abilities. The value
75      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
76      * template.
77      */
78     virtual ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) = 0;
79 
80     /**
81      * @brief Starts a new ability.
82      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
83      * to start a specific ability. The system locates the target ability from installed abilities based on the value
84      * of the want parameter and the value of the start options and then starts it. You can specify the ability to
85      * start using the two parameters.
86      *
87      * @param want Indicates the Want containing application side information about the target ability to start.
88      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
89      * start.
90      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
91      * template is started. You can define the request code to identify the results returned by abilities. The value
92      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
93      * template.
94      */
95     virtual ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) = 0;
96 
97     /**
98      * @brief Starts a new ability using the original caller information.
99      * Start a new ability as if it was started by the ability that started current ability. This is for the confirm
100      * ability and selection ability, which passthrough their want to the target.
101      *
102      * @param want Indicates the Want containing information about the target ability to start.
103      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
104      * template is started. You can define the request code to identify the results returned by abilities. The value
105      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
106      * template.
107      */
108     virtual ErrCode StartAbilityAsCaller(const AAFwk::Want &want, int requestCode) = 0;
109 
110     /**
111      * @brief Starts a new ability using the original caller information.
112      * Start a new ability as if it was started by the ability that started current ability. This is for the confirm
113      * ability and selection ability, which passthrough their want to the target.
114      *
115      * @param want Indicates the Want containing information about the target ability to start.
116      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
117      * start.
118      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
119      * template is started. You can define the request code to identify the results returned by abilities. The value
120      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
121      * template.
122      */
123     virtual ErrCode StartAbilityAsCaller(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions,
124         int requestCode) = 0;
125 
126     /**
127      * @brief Starts a new ability.
128      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
129      * to start a specific ability. The system locates the target ability from installed abilities based on the value
130      * of the want parameter and the value of the start options and then starts it. You can specify the ability to
131      * start using the two parameters.
132      *
133      * @param want Indicates the Want containing application side information about the target ability to start.
134      * @param accountId caller userId.
135      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
136      * start.
137      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
138      * template is started. You can define the request code to identify the results returned by abilities. The value
139      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
140      * template.
141      */
142     virtual ErrCode StartAbilityWithAccount(
143         const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) = 0;
144 
145     virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, int requestCode, RuntimeTask &&task) = 0;
146 
147     virtual ErrCode StartAbilityForResultWithAccount(
148         const AAFwk::Want &Want, int accountId, int requestCode, RuntimeTask &&task) = 0;
149 
150     virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, const AAFwk::StartOptions &startOptions,
151         int requestCode, RuntimeTask &&task) = 0;
152 
153     virtual ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &Want, int accountId,
154         const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) = 0;
155 
156     virtual ErrCode StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId = -1) = 0;
157 
158     virtual ErrCode StopServiceExtensionAbility(const AAFwk::Want& want, int32_t accountId = -1) = 0;
159 
160     virtual ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) = 0;
161 
162     virtual ErrCode RestoreWindowStage(NativeEngine& engine, NativeValue* contentStorage) = 0;
163 
164     virtual void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) = 0;
165 
166     /**
167     * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
168     *
169     * @param want Indicates the want containing information about the ability to connect
170     * @param connectCallback Indicates the callback object when the target ability is connected.
171     * @return True means success and false means failure
172     */
173     virtual ErrCode ConnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) = 0;
174 
175     /**
176      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
177      * @param want Indicates the want containing information about the ability to connect
178      * @param accountId caller userId.
179      * @param connectCallback Indicates the callback object when the target ability is connected.
180      * @return True means success and false means failure
181      */
182     virtual ErrCode ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId,
183                                 const sptr<AbilityConnectCallback> &connectCallback) = 0;
184 
185     /**
186     * @brief Disconnects the current ability from an ability
187     *
188     * @param want Indicates the want containing information about the ability to disconnect
189     * @param connectCallback Indicates the callback object when the target ability is connected.
190     * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
191     */
192     virtual void DisconnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) = 0;
193 
194     /**
195      * @brief get ability info of the current ability
196      *
197      * @return the ability info of the current ability
198      */
199     virtual std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const = 0;
200 
201     /**
202      * @brief Minimize the current ability.
203      *
204      * @param fromUser mark the minimize operation source.
205      */
206     virtual void MinimizeAbility(bool fromUser = false) = 0;
207 
208     /**
209      * @brief Get OnBackPressedCallBack.
210      * @param needMoveToBackground true if ability will be moved to background; false if ability will be terminated.
211      *
212      * @return Returns ERR_OK on success, others on failure.
213      */
214     virtual ErrCode OnBackPressedCallBack(bool &needMoveToBackground) = 0;
215 
216     virtual ErrCode MoveAbilityToBackground() = 0;
217 
218     virtual ErrCode TerminateSelf() = 0;
219 
220     virtual ErrCode CloseAbility() = 0;
221 
222     /**
223      * @brief Get ContentStorage.
224      *
225      * @return Returns the ContentStorage.
226      */
227     virtual std::unique_ptr<NativeReference>& GetContentStorage() = 0;
228 
229     /**
230      * call function by callback object
231      *
232      * @param want Request info for ability.
233      * @param callback Indicates the callback object.
234      * @param accountId Indicates the account to start.
235      *
236      * @return Returns zero on success, others on failure.
237      */
238     virtual ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback,
239         int32_t accountId = DEFAULT_INVAL_VALUE) = 0;
240 
241     /**
242      * caller release by callback object
243      *
244      * @param callback Indicates the callback object.
245      *
246      * @return Returns zero on success, others on failure.
247      */
248     virtual ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) = 0;
249 
250     /**
251      * clear failed call connection by callback object
252      *
253      * @param callback Indicates the callback object.
254      *
255      * @return void.
256      */
257     virtual void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback) = 0;
258 
259     /**
260      * @brief Get LocalCallContainer.
261      *
262      * @return Returns the LocalCallContainer.
263      */
264     virtual std::shared_ptr<LocalCallContainer> GetLocalCallContainer() = 0;
265 
266     virtual void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) = 0;
267 
268     virtual void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) = 0;
269 
270     virtual void SetWeakSessionToken(const wptr<IRemoteObject>& sessionToken) = 0;
271 
272     /**
273      * @brief Requests dialogService from the system.
274      * This method is called for dialog request. This is an asynchronous method. When it is executed,
275      * the task will be called back.
276      *
277      * @param engine js native engine.
278      * @param want Indicates the dialog service to be requested.
279      * @param task The callback or promise fo js interface.
280      * @return Returns ERR_OK if success.
281      */
282     virtual ErrCode RequestDialogService(NativeEngine &engine, AAFwk::Want &want, RequestDialogResultTask &&task) = 0;
283 
284     /**
285      * @brief Report drawn completed.
286      *
287      * @return Returns ERR_OK on success, others on failure.
288      */
289     virtual ErrCode ReportDrawnCompleted() = 0;
290 
291     virtual ErrCode GetMissionId(int32_t &missionId) = 0;
292 
293     /**
294      * Set mission continue state of this ability.
295      *
296      * @param state the mission continuation state of this ability.
297      * @return Returns ERR_OK if success.
298      */
299     virtual ErrCode SetMissionContinueState(const AAFwk::ContinueState &state) = 0;
300 
301 #ifdef SUPPORT_GRAPHICS
302     /**
303      * @brief Set mission label of this ability.
304      *
305      * @param label the label of this ability.
306      * @return Returns ERR_OK if success.
307      */
308     virtual ErrCode SetMissionLabel(const std::string &label) = 0;
309 
310     /**
311      * @brief Set mission icon of this ability.
312      *
313      * @param icon the icon of this ability.
314      * @return Returns ERR_OK if success.
315      */
316     virtual ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) = 0;
317 
318     virtual int GetCurrentWindowMode() = 0;
319 
320     /**
321      * @brief Get window rectangle of this ability.
322      *
323      * @param the left position of window rectangle.
324      * @param the top position of window rectangle.
325      * @param the width position of window rectangle.
326      * @param the height position of window rectangle.
327      */
328     virtual void GetWindowRect(int32_t &left, int32_t &top, int32_t &width, int32_t &height) = 0;
329 
330     /**
331      * @brief Get ui content object.
332      *
333      * @return UIContent object of ACE.
334      */
335     virtual Ace::UIContent* GetUIContent() = 0;
336 #endif
337     virtual bool IsTerminating() = 0;
338     virtual void SetTerminating(bool state) = 0;
339     using SelfType = AbilityContext;
340     static const size_t CONTEXT_TYPE_ID;
341 
342 protected:
IsContext(size_t contextTypeId)343     bool IsContext(size_t contextTypeId) override
344     {
345         return contextTypeId == CONTEXT_TYPE_ID || Context::IsContext(contextTypeId);
346     }
347 };
348 } // namespace AbilityRuntime
349 } // namespace OHOS
350 #endif // OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_H
351