• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "json_object.h"
24 #include "parameter.h"
25 #include "dm_single_instance.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace {
30 constexpr const char* BUNDLE_NAME = "com.ohos.devicemanagerui";
31 constexpr const char* ABILITY_NAME = "com.ohos.devicemanagerui.ConfirmServiceExtAbility";
32 constexpr const char* PEER_DEVICE_NAME = "deviceName";
33 constexpr const char* PEER_DEVICE_TYPE = "deviceType";
34 } // namespace
35 
StartAbility(const std::string & params)36 AbilityStatus DmAbilityManager::StartAbility(const std::string &params)
37 {
38     LOGI("Start Ability.");
39     std::string deviceName = "";
40     std::string appOperationStr = "";
41     std::string customDescriptionStr = "";
42     int32_t deviceType = -1;
43     JsonObject jsonObject(params);
44     if (!jsonObject.IsDiscarded()) {
45         if (IsString(jsonObject, TAG_REQUESTER)) {
46             deviceName = jsonObject[TAG_REQUESTER].Get<std::string>();
47         }
48         if (IsString(jsonObject, TAG_APP_OPERATION)) {
49             appOperationStr = jsonObject[TAG_APP_OPERATION].Get<std::string>();
50         }
51         if (IsString(jsonObject, TAG_CUSTOM_DESCRIPTION)) {
52             customDescriptionStr = jsonObject[TAG_CUSTOM_DESCRIPTION].Get<std::string>();
53         }
54         if (IsInt32(jsonObject, TAG_LOCAL_DEVICE_TYPE)) {
55             deviceType = jsonObject[TAG_LOCAL_DEVICE_TYPE].Get<std::int32_t>();
56         }
57     }
58 
59     char localDeviceId[DEVICE_UUID_LENGTH] = {0};
60     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
61     std::string deviceId = localDeviceId;
62     AAFwk::Want want;
63     AppExecFwk::ElementName element(deviceId, BUNDLE_NAME, ABILITY_NAME);
64     want.SetElement(element);
65     want.SetParam(PEER_DEVICE_NAME, deviceName);
66     want.SetParam(CUSTOM_DESCRIPTION, customDescriptionStr);
67     want.SetParam(PEER_DEVICE_TYPE, deviceType);
68     if (!appOperationStr.empty()) {
69         want.SetParam(APP_OPERATION, appOperationStr);
70     }
71 
72     AAFwk::AbilityManagerClient::GetInstance()->Connect();
73     ErrCode result = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
74     if (result != 0) {
75         LOGE("Start Ability failed, error value = %{public}d", (int32_t)result);
76         return AbilityStatus::ABILITY_STATUS_FAILED;
77     }
78     return AbilityStatus::ABILITY_STATUS_SUCCESS;
79 }
80 } // namespace DistributedHardware
81 } // namespace OHOS
82