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 FOUNDATION_APPEXECFWK_OHOS_APPLICATION_CONTEXT_H 17 #define FOUNDATION_APPEXECFWK_OHOS_APPLICATION_CONTEXT_H 18 19 #include "context_container.h" 20 21 namespace OHOS { 22 namespace AppExecFwk { 23 class TaskDispatcherContext; 24 class ApplicationContext : public ContextContainer, public std::enable_shared_from_this<ApplicationContext> { 25 public: 26 ApplicationContext(); 27 virtual ~ApplicationContext(); 28 29 /** 30 * @brief Obtains information about the current ability. 31 * The returned information includes the class name, bundle name, and other information about the current ability. 32 * 33 * @return Returns the AbilityInfo object for the current ability. 34 */ 35 const std::shared_ptr<AbilityInfo> GetAbilityInfo() override; 36 37 /** 38 * @brief Starts a new ability. 39 * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method 40 * to start a specific ability. The system locates the target ability from installed abilities based on the value 41 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 42 * 43 * @param want Indicates the Want containing information about the target ability to start. 44 * 45 * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE 46 * template is started. You can define the request code to identify the results returned by abilities. The value 47 * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE 48 * template. 49 * 50 * @return errCode ERR_OK on success, others on failure. 51 */ 52 ErrCode StartAbility(const AAFwk::Want &want, int requestCode) override; 53 54 /** 55 * @brief Starts a new ability with special ability start setting. 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 is started. You can define the request 59 * code to identify the results returned by abilities. The value ranges from 0 to 65535. 60 * @param abilityStartSetting Indicates the special start setting used in starting ability. 61 * 62 * @return errCode ERR_OK on success, others on failure. 63 */ 64 ErrCode StartAbility(const Want &want, int requestCode, const AbilityStartSetting &abilityStartSetting) override; 65 66 /** 67 * @brief Destroys another ability you had previously started by calling Ability.startAbilityForResult 68 * (ohos.aafwk.content.Want, int, ohos.aafwk.ability.startsetting.AbilityStartSetting) with the same requestCode 69 * passed. 70 * 71 * @param requestCode Indicates the request code passed for starting the ability. 72 * 73 * @return errCode ERR_OK on success, others on failure. 74 */ 75 ErrCode TerminateAbility(int requestCode) override; 76 77 /** 78 * @brief Destroys the current ability. 79 * 80 * @return errCode ERR_OK on success, others on failure. 81 */ 82 ErrCode TerminateAbility() override; 83 84 /** 85 * @brief 86 * Destroys this Service ability if the number of times it has been started equals the number represented by the 87 * given {@code startId}. This method is the same as calling {@link #terminateAbility} to destroy this Service 88 * ability, except that this method helps you avoid destroying it if a client has requested a Service 89 * ability startup in {@link ohos.aafwk.ability.Ability#onCommand} but you are unaware of it. 90 * 91 * @param startId Indicates the number of startup times of this Service ability passed to 92 * {@link ohos.aafwk.ability.Ability#onCommand}. The {@code startId} is 93 * incremented by 1 every time this ability is started. For example, 94 * if this ability has been started for six times, the value of {@code startId} is {@code 6}. 95 * 96 * @return Returns {@code true} if the {@code startId} matches the number of startup times 97 * and this Service ability will be destroyed; returns {@code false} otherwise. 98 */ 99 bool TerminateAbilityResult(int startId) override; 100 101 /** 102 * @brief Obtains the bundle name of the ability that called the current ability. 103 * You can use the obtained bundle name to check whether the calling ability is allowed to receive the data you will 104 * send. If you did not use Ability.startAbilityForResult(ohos.aafwk.content.Want, int, 105 * ohos.aafwk.ability.startsetting.AbilityStartSetting) to start the calling ability, null is returned. 106 * 107 * @return Returns the bundle name of the calling ability; returns null if no calling ability is available. 108 */ 109 std::string GetCallingBundle() override; 110 111 /** 112 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 113 * 114 * @param want Indicates the want containing information about the ability to connect 115 * 116 * @param conn Indicates the callback object when the target ability is connected. 117 * 118 * @return True means success and false means failure 119 */ 120 bool ConnectAbility(const Want &want, const sptr<AAFwk::IAbilityConnection> &conn) override; 121 122 /** 123 * @brief Disconnects the current ability from an ability 124 * 125 * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection 126 * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities. 127 * 128 * @return errCode ERR_OK on success, others on failure. 129 */ 130 ErrCode DisconnectAbility(const sptr<AAFwk::IAbilityConnection> &conn) override; 131 132 /** 133 * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template. 134 * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE 135 * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE 136 * template. The current ability itself can be destroyed by calling the terminateAbility() method. 137 * 138 * @param want Indicates the Want containing information about the ability to destroy. 139 * 140 * @return Returns true if the ability is destroyed successfully; returns false otherwise. 141 */ 142 bool StopAbility(const AAFwk::Want &want) override; 143 144 /** 145 * @brief Starts multiple abilities. 146 * 147 * @param wants Indicates the Want containing information array about the target ability to start. 148 */ 149 void StartAbilities(const std::vector<AAFwk::Want> &wants) override; 150 151 /** 152 * @brief Checks whether this ability is the first ability in a mission. 153 * 154 * @return Returns true is first in Mission. 155 */ 156 bool IsFirstInMission() override; 157 158 /** 159 * @brief Obtains the unique ID of the mission containing this ability. 160 * 161 * @return Returns the unique mission ID. 162 */ 163 int GetMissionId() override; 164 165 /** 166 * @brief Creates a parallel task dispatcher with a specified priority. 167 * 168 * @param name Indicates the task dispatcher name. This parameter is used to locate problems. 169 * @param priority Indicates the priority of all tasks dispatched by the parallel task dispatcher. 170 * 171 * @return Returns a parallel task dispatcher. 172 */ 173 std::shared_ptr<TaskDispatcher> CreateParallelTaskDispatcher( 174 const std::string &name, const TaskPriority &priority) override; 175 176 /** 177 * @brief Creates a serial task dispatcher with a specified priority. 178 * 179 * @param name Indicates the task dispatcher name. This parameter is used to locate problems. 180 * @param priority Indicates the priority of all tasks dispatched by the created task dispatcher. 181 * 182 * @return Returns a serial task dispatcher. 183 */ 184 std::shared_ptr<TaskDispatcher> CreateSerialTaskDispatcher( 185 const std::string &name, const TaskPriority &priority) override; 186 187 /** 188 * @brief Obtains a global task dispatcher with a specified priority. 189 * 190 * @param priority Indicates the priority of all tasks dispatched by the global task dispatcher. 191 * 192 * @return Returns a global task dispatcher. 193 */ 194 std::shared_ptr<TaskDispatcher> GetGlobalTaskDispatcher(const TaskPriority &priority) override; 195 196 protected: 197 sptr<IRemoteObject> GetToken() override; 198 std::shared_ptr<TaskDispatcherContext> taskDispatcherContext_ = nullptr; 199 std::mutex mutex_; 200 }; 201 } // namespace AppExecFwk 202 } // namespace OHOS 203 #endif // FOUNDATION_APPEXECFWK_OHOS_APPLICATION_CONTEXT_H 204