• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 ABILITY_RUNTIME_ABILITY_CONTEXT_H
17 #define ABILITY_RUNTIME_ABILITY_CONTEXT_H
18 
19 #include "foundation/aafwk/standard/frameworks/kits/ability/native/include/iability_callback.h"
20 #include "foundation/aafwk/standard/frameworks/kits/appkit/native/ability_runtime/context/context.h"
21 #include "foundation/aafwk/standard/interfaces/innerkits/app_manager/include/appmgr/configuration.h"
22 
23 #include "ability_connect_callback.h"
24 #include "ability_info.h"
25 #include "native_engine/native_reference.h"
26 #include "native_engine/native_value.h"
27 #include "start_options.h"
28 #include "want.h"
29 #include "caller_callback.h"
30 
31 #ifdef SUPPORT_GRAPHICS
32 #include "pixel_map.h"
33 #endif
34 
35 namespace OHOS {
36 namespace AbilityRuntime {
37 using RuntimeTask = std::function<void(int, const AAFwk::Want&)>;
38 using PermissionRequestTask = std::function<void(const std::vector<std::string>&, const std::vector<int>&)>;
39 class LocalCallContainer;
40 class AbilityContext : public Context {
41 public:
42     virtual ~AbilityContext() = default;
43 
44     /**
45      * @brief Starts a new ability.
46      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
47      * to start a specific ability. The system locates the target ability from installed abilities based on the value
48      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
49      *
50      * @param want Indicates the Want containing information about the target ability to start.
51      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
52      * template is started. You can define the request code to identify the results returned by abilities. The value
53      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
54      * template.
55      */
56     virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) = 0;
57 
58     /**
59      * @brief Starts a new ability.
60      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
61      * to start a specific ability. The system locates the target ability from installed abilities based on the value
62      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
63      *
64      * @param want Indicates the Want containing information about the target ability to start.
65      * @param accountId ability caller accountId.
66      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
67      * template is started. You can define the request code to identify the results returned by abilities. The value
68      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
69      * template.
70      */
71     virtual ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) = 0;
72 
73     /**
74      * @brief Starts a new ability.
75      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
76      * to start a specific ability. The system locates the target ability from installed abilities based on the value
77      * of the want parameter and the value of the start options and then starts it. You can specify the ability to
78      * start using the two parameters.
79      *
80      * @param want Indicates the Want containing application side information about the target ability to start.
81      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
82      * start.
83      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
84      * template is started. You can define the request code to identify the results returned by abilities. The value
85      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
86      * template.
87      */
88     virtual ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) = 0;
89 
90     /**
91      * @brief Starts a new ability.
92      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
93      * to start a specific ability. The system locates the target ability from installed abilities based on the value
94      * of the want parameter and the value of the start options and then starts it. You can specify the ability to
95      * start using the two parameters.
96      *
97      * @param want Indicates the Want containing application side information about the target ability to start.
98      * @param accountId caller userId.
99      * @param startOptions Indicates the StartOptions containing service side information about the target ability to
100      * start.
101      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
102      * template is started. You can define the request code to identify the results returned by abilities. The value
103      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
104      * template.
105      */
106     virtual ErrCode StartAbilityWithAccount(
107         const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) = 0;
108 
109     virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, int requestCode, RuntimeTask &&task) = 0;
110 
111     virtual ErrCode StartAbilityForResultWithAccount(
112         const AAFwk::Want &Want, int accountId, int requestCode, RuntimeTask &&task) = 0;
113 
114     virtual ErrCode StartAbilityForResult(const AAFwk::Want &Want, const AAFwk::StartOptions &startOptions,
115         int requestCode, RuntimeTask &&task) = 0;
116 
117     virtual ErrCode StartAbilityForResultWithAccount(
118         const AAFwk::Want &Want, int accountId, const AAFwk::StartOptions &startOptions,
119         int requestCode, RuntimeTask &&task) = 0;
120 
121     virtual ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) = 0;
122 
123     virtual ErrCode RestoreWindowStage(NativeEngine& engine, NativeValue* contentStorage) = 0;
124 
125     virtual void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) = 0;
126 
127     /**
128     * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
129     *
130     * @param want Indicates the want containing information about the ability to connect
131     * @param connectCallback Indicates the callback object when the target ability is connected.
132     * @return True means success and false means failure
133     */
134     virtual bool ConnectAbility(const AAFwk::Want &want,
135         const sptr<AbilityConnectCallback> &connectCallback) = 0;
136 
137     /**
138      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
139      * @param accountId caller userId.
140      * @param want Indicates the want containing information about the ability to connect
141      * @param conn Indicates the callback object when the target ability is connected.
142      * @return True means success and false means failure
143      */
144     virtual bool ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId,
145                                 const sptr<AbilityConnectCallback> &connectCallback) = 0;
146 
147     /**
148     * @brief Disconnects the current ability from an ability
149     *
150     * @param want Indicates the want containing information about the ability to disconnect
151     * @param connectCallback Indicates the callback object when the target ability is connected.
152     * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
153     */
154     virtual void DisconnectAbility(const AAFwk::Want &want,
155         const sptr<AbilityConnectCallback> &connectCallback) = 0;
156 
157     /**
158      * @brief get ability info of the current ability
159      *
160      * @return the ability info of the current ability
161      */
162     virtual std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const = 0;
163 
164     /**
165      * @brief Minimize the current ability.
166      *
167      * @param fromUser mark the minimize operation source.
168      */
169     virtual void MinimizeAbility(bool fromUser = false) = 0;
170 
171     virtual ErrCode TerminateSelf() = 0;
172 
173     virtual ErrCode CloseAbility() = 0;
174 
175     /**
176      * @brief Requests certain permissions from the system.
177      * This method is called for permission request. This is an asynchronous method. When it is executed,
178      * the OnRequestPermissionsFromUserResult(int, vector<string>, vector<int>) method will be called back.
179      *
180      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
181      * @param requestCode Indicates the request code to be passed to the OnRequestPermissionsFromUserResult(int,
182      * vector<string>, vector<int>) callback method. This code cannot be a negative number.
183      * @param task The callback or promise fo js interface.
184      */
185     virtual void RequestPermissionsFromUser(const std::vector<std::string> &permissions,
186         int requestCode, PermissionRequestTask &&task) = 0;
187 
188     /**
189      * @brief Called back after permissions are requested by using
190      * RequestPermissionsFromUser(vector<string>, int, PermissionRequestTask).
191      *
192      * @param requestCode Indicates the request code passed to this method from
193      * RequestPermissionsFromUser(vector<string>, int, PermissionRequestTask).
194      * @param permissions Indicates the list of permissions requested by using
195      * RequestPermissionsFromUser(vector<string>, int, PermissionRequestTask).
196      * @param grantResults Indicates the granting results of the corresponding permissions requested using
197      * RequestPermissionsFromUser(vector<string>, int, PermissionRequestTask). The value 0 indicates that a
198      * permission is granted, and the value -1 indicates not.
199      */
200     virtual void OnRequestPermissionsFromUserResult(
201         int requestCode, const std::vector<std::string> &permissions, const std::vector<int> &grantResults) = 0;
202 
203     /**
204      * @brief Get ContentStorage.
205      *
206      * @return Returns the ContentStorage.
207      */
208     virtual std::unique_ptr<NativeReference>& GetContentStorage() = 0;
209 
210     /**
211      * call function by callback object
212      *
213      * @param want Request info for ability.
214      * @param callback Indicates the callback object.
215      *
216      * @return Returns zero on success, others on failure.
217      */
218     virtual ErrCode StartAbility(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback) = 0;
219 
220     /**
221      * caller release by callback object
222      *
223      * @param callback Indicates the callback object.
224      *
225      * @return Returns zero on success, others on failure.
226      */
227     virtual ErrCode ReleaseAbility(const std::shared_ptr<CallerCallBack> &callback) = 0;
228 
229     /**
230      * @brief Set mission label of this ability.
231      *
232      * @param label the label of this ability.
233      * @return Returns ERR_OK if success.
234      */
235     virtual ErrCode SetMissionLabel(const std::string &label) = 0;
236 
237 #ifdef SUPPORT_GRAPHICS
238     /**
239      * @brief Set mission icon of this ability.
240      *
241      * @param icon the icon of this ability.
242      * @return Returns ERR_OK if success.
243      */
244     virtual ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) = 0;
245 #endif
246 
247     /**
248      * @brief Get LocalCallContainer.
249      *
250      * @return Returns the LocalCallContainer.
251      */
252     virtual sptr<LocalCallContainer> GetLocalCallContainer() = 0;
253 
254     virtual void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) = 0;
255 
256     virtual void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) = 0;
257 #ifdef SUPPORT_GRAPHICS
258     virtual int GetCurrentWindowMode() = 0;
259 #endif
260     using SelfType = AbilityContext;
261     static const size_t CONTEXT_TYPE_ID;
262 
263 protected:
IsContext(size_t contextTypeId)264     bool IsContext(size_t contextTypeId) override
265     {
266         return contextTypeId == CONTEXT_TYPE_ID || Context::IsContext(contextTypeId);
267     }
268 };
269 }  // namespace AbilityRuntime
270 }  // namespace OHOS
271 #endif  // ABILITY_RUNTIME_ABILITY_CONTEXT_H