1 /*
2  * Copyright (c) 2022-2024 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 "dm_ability_manager.h"
17 
18 #include "ability_manager_client.h"
19 #include "auth_message_processor.h"
20 #include "dm_anonymous.h"
21 #include "dm_constants.h"
22 #include "dm_log.h"
23 #include "nlohmann/json.hpp"
24 #include "parameter.h"
25 #include "dm_single_instance.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace {
30 const std::string bundleName = "com.ohos.devicemanagerui";
31 const std::string abilityName = "com.ohos.devicemanagerui.ConfirmServiceExtAbility";
32 const std::string peerDeviceName = "deviceName";
33 const std::string appOperation = "appOperation";
34 const std::string customDescription = "customDescription";
35 const std::string peerdeviceType = "deviceType";
36 } // namespace
37 
StartAbility(const std::string & params)38 AbilityStatus DmAbilityManager::StartAbility(const std::string &params)
39 {
40     LOGI("Start Ability.");
41     std::string deviceName = "";
42     std::string appOperationStr = "";
43     std::string customDescriptionStr = "";
44     int32_t deviceType = -1;
45     nlohmann::json jsonObject = nlohmann::json::parse(params, nullptr, false);
46     if (!jsonObject.is_discarded()) {
47         if (IsString(jsonObject, TAG_REQUESTER)) {
48             deviceName = jsonObject[TAG_REQUESTER].get<std::string>();
49         }
50         if (IsString(jsonObject, TAG_APP_OPERATION)) {
51             appOperationStr = jsonObject[TAG_APP_OPERATION].get<std::string>();
52         }
53         if (IsString(jsonObject, TAG_CUSTOM_DESCRIPTION)) {
54             customDescriptionStr = jsonObject[TAG_CUSTOM_DESCRIPTION].get<std::string>();
55         }
56         if (IsInt32(jsonObject, TAG_LOCAL_DEVICE_TYPE)) {
57             deviceType = jsonObject[TAG_LOCAL_DEVICE_TYPE].get<std::int32_t>();
58         }
59     }
60 
61     char localDeviceId[DEVICE_UUID_LENGTH] = {0};
62     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
63     std::string deviceId = localDeviceId;
64     AAFwk::Want want;
65     AppExecFwk::ElementName element(deviceId, bundleName, abilityName);
66     want.SetElement(element);
67     want.SetParam(peerDeviceName, deviceName);
68     want.SetParam(customDescription, customDescriptionStr);
69     want.SetParam(peerdeviceType, deviceType);
70     if (!appOperationStr.empty()) {
71         want.SetParam(appOperation, appOperationStr);
72     }
73 
74     AAFwk::AbilityManagerClient::GetInstance()->Connect();
75     ErrCode result = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
76     if (result != 0) {
77         LOGE("Start Ability failed, error value = %{public}d", (int32_t)result);
78         return AbilityStatus::ABILITY_STATUS_FAILED;
79     }
80     return AbilityStatus::ABILITY_STATUS_SUCCESS;
81 }
82 } // namespace DistributedHardware
83 } // namespace OHOS
84