• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "ability_manager_adapter.h"
17 
18 #include "avsession_errors.h"
19 #include "avsession_log.h"
20 #include "ability_connect_helper.h"
21 
22 namespace OHOS::AVSession {
AbilityManagerAdapter(const std::string & bundleName,const std::string & abilityName)23 AbilityManagerAdapter::AbilityManagerAdapter(const std::string& bundleName, const std::string& abilityName)
24 {
25     SLOGI("construct bundleName=%{public}s abilityName=%{public}s", bundleName.c_str(), abilityName.c_str());
26     bundleName_ = bundleName;
27     abilityName_ = abilityName;
28 }
29 
~AbilityManagerAdapter()30 AbilityManagerAdapter::~AbilityManagerAdapter()
31 {}
32 
StartAbilityByCall(std::string & sessionId)33 int32_t AbilityManagerAdapter::StartAbilityByCall(std::string& sessionId)
34 {
35     if (status_ != Status::ABILITY_STATUS_INIT) {
36         SLOGE("Start Ability is running");
37         return ERR_START_ABILITY_IS_RUNNING;
38     }
39     status_ = Status::ABILITY_STATUS_RUNNING;
40     int32_t ret = AbilityConnectHelper::GetInstance().StartAbilityByCall(bundleName_, abilityName_);
41     if (ret != AVSESSION_SUCCESS) {
42         SLOGE("Start Ability failed: %{public}d", ret);
43         status_ = Status::ABILITY_STATUS_INIT;
44         return ret;
45     }
46 
47     WaitForTimeout(ABILITY_START_TIMEOUT_MS);
48     ret = ERR_START_ABILITY_TIMEOUT;
49     if (status_ == Status::ABILITY_STATUS_SUCCESS) {
50         ret = AVSESSION_SUCCESS;
51         sessionId = sessionId_;
52     }
53     status_ = Status::ABILITY_STATUS_INIT;
54     return ret;
55 }
56 
StartAbilityByCallDone(const std::string & sessionId)57 void AbilityManagerAdapter::StartAbilityByCallDone(const std::string& sessionId)
58 {
59     if (status_ != Status::ABILITY_STATUS_RUNNING) {
60         SLOGI("no need to notify");
61         return;
62     }
63     sessionId_ = sessionId;
64     syncCon_.notify_one();
65 }
66 
WaitForTimeout(uint32_t timeout)67 void AbilityManagerAdapter::WaitForTimeout(uint32_t timeout)
68 {
69     std::unique_lock<std::mutex> lock(syncMutex_);
70     auto waitStatus = syncCon_.wait_for(lock, std::chrono::milliseconds(timeout));
71     if (waitStatus == std::cv_status::timeout) {
72         SLOGE("StartAbilityByCall timeout");
73         status_ = Status::ABILITY_STATUS_FAILED;
74         return;
75     }
76     status_ = Status::ABILITY_STATUS_SUCCESS;
77 }
78 } // namespace OHOS::AVSession