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