• 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 "app_manager.h"
17 
18 #include "ability_manager_client.h"
19 #include "component_manager.h"
20 #include "element_name.h"
21 #include "wukong_define.h"
22 #include "wukong_util.h"
23 
24 namespace OHOS {
25 namespace WuKong {
26 namespace {
27 const std::string HELP_MSG_NO_ABILITY_NAME_OPTION = "error: -a <ability-name> is expected";
28 const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: -b <bundle-name> is expected";
29 
30 const std::string STRING_START_ABILITY_OK = "start ability successfully.";
31 const std::string STRING_START_ABILITY_NG = "error: failed to start ability.";
32 }  // namespace
33 
AllowAbilityStart(const AAFwk::Want & want,const std::string & bundleName)34 bool AppManager::BlockAbilityController::AllowAbilityStart(const AAFwk::Want &want, const std::string &bundleName)
35 {
36     TRACK_LOG_STD();
37     std::vector<std::string> blocklist;
38     std::vector<std::string> tempAllowList;
39     bool orderFlag;
40     auto util = WuKongUtil::GetInstance();
41 
42     tempAllowList = util->GetTempAllowList();
43     // if bundleName in the tempAllow list to allow ability start.
44     auto it = find(tempAllowList.begin(), tempAllowList.end(), bundleName);
45     orderFlag = util->GetOrderFlag();
46     if (orderFlag && tempAllowList.size() != 0) {
47         if (it != tempAllowList.end()) {
48             DEBUG_LOG("bundle start allow");
49             return true;
50         } else {
51             return false;
52         }
53     }
54 
55     util->GetBlockList(blocklist);
56     DEBUG_LOG_STR("BundleName: %s", bundleName.c_str());
57 
58     // if bundleName in the block list to unallow ability start.
59     it = find(blocklist.begin(), blocklist.end(), bundleName);
60     if (it == blocklist.end()) {
61         DEBUG_LOG("bundle start prohibition");
62         return true;
63     }
64 
65     TRACK_LOG_END();
66     return false;
67 }
68 
69 // turn to background
AllowAbilityBackground(const std::string & bundleName)70 bool AppManager::BlockAbilityController::AllowAbilityBackground(const std::string &bundleName)
71 {
72     if (WuKongUtil::GetInstance()->GetOrderFlag()) {
73         return false;
74     } else {
75         return true;
76     }
77 }
78 
StartAbilityByBundleInfo(std::string abilityName,std::string bundleName)79 ErrCode AppManager::StartAbilityByBundleInfo(std::string abilityName, std::string bundleName)
80 {
81     TRACK_LOG_STD();
82     AAFwk::Want want;
83     int result;
84     std::string output;
85     if (abilityName.size() == 0 || bundleName.size() == 0) {
86         if (abilityName.size() == 0) {
87             output.append(HELP_MSG_NO_ABILITY_NAME_OPTION + "\n");
88         }
89         if (bundleName.size() == 0) {
90             output.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
91         }
92         result = OHOS::ERR_INVALID_VALUE;
93     } else {
94         AppExecFwk::ElementName element("", bundleName, abilityName);
95         want.SetElement(element);
96         ComponentManager::GetInstance()->BackToHome();
97         result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
98         if (result == OHOS::ERR_OK) {
99             output = STRING_START_ABILITY_OK;
100         } else {
101             output = STRING_START_ABILITY_NG;
102         }
103     }
104     DEBUG_LOG_STR("%s", output.c_str());
105     TRACK_LOG_STR("result %s", std::to_string(result).c_str());
106     return result;
107 }
108 
StartAbilityByUriAndType(const std::string uri,const std::string typeVal)109 ErrCode AppManager::StartAbilityByUriAndType(const std::string uri, const std::string typeVal)
110 {
111     TRACK_LOG_STD();
112     AAFwk::Want want;
113     int result;
114     want.SetUri(uri);
115     want.SetType(typeVal);
116     ComponentManager::GetInstance()->BackToHome();
117     result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
118     if (result == OHOS::ERR_OK) {
119         INFO_LOG("The specified URI page is opened successfully.");
120     } else {
121         ERROR_LOG("Failed to open the page with the specified URI.");
122     }
123     TRACK_LOG_STR("result %s", std::to_string(result).c_str());
124     return result;
125 }
126 
StartAbilityByAbilityAndUri(const std::string uri,const std::vector<std::string> & abilityName)127 ErrCode AppManager::StartAbilityByAbilityAndUri(const std::string uri, const std::vector<std::string> &abilityName)
128 {
129     TRACK_LOG_STD();
130     AAFwk::Want want;
131     int result;
132     std::vector<std::string> allowList;
133     WuKongUtil::GetInstance()->GetAllowList(allowList);
134     AppExecFwk::ElementName element("", allowList[0], abilityName[0]);
135     want.SetElement(element);
136     want.SetUri(uri);
137     ComponentManager::GetInstance()->BackToHome();
138     result = OHOS::AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
139     if (result == OHOS::ERR_OK) {
140         INFO_LOG("The specified URI page is opened successfully.");
141     } else {
142         ERROR_LOG("Failed to open the page with the specified URI.");
143     }
144     TRACK_LOG_STR("result %s", std::to_string(result).c_str());
145     return result;
146 }
147 
SetAbilityController()148 void AppManager::SetAbilityController()
149 {
150     if (abilityController_ == nullptr) {
151         abilityController_ = new (std::nothrow) BlockAbilityController();
152     }
153     OHOS::AAFwk::AbilityManagerClient::GetInstance()->SetAbilityController(abilityController_, true);
154 }
155 }  // namespace WuKong
156 }  // namespace OHOS
157