1 /*
2 * Copyright (c) 2023 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 "common.h"
17 #include "connection.h"
18 #include "extension_manager_proxy.h"
19 #include <semaphore.h>
20
21 namespace OHOS {
22 namespace HDC {
23 namespace AUTH {
24
25 using namespace std;
26 using namespace AAFwk;
27
GetSettingBundleName(string & bundle)28 bool HdcdConnection::GetSettingBundleName(string &bundle)
29 {
30 FILE *fp;
31 int bytesRead;
32 const int bufSize = 256;
33 char buf[bufSize] = { 0 };
34
35 fp = popen("bm dump -a | grep \"com.*.settings$\"", "r");
36 if (!fp) {
37 AUTH_LOGE("open pipe failed");
38 return false;
39 }
40 bytesRead = fread(buf, sizeof(char), bufSize, fp);
41
42 if (bytesRead > 0) {
43 string prefix = "com.";
44 string suffix = ".settings";
45 bundle.assign(buf, bytesRead);
46 bundle.erase(0, bundle.find(prefix));
47 bundle.erase(bundle.rfind(suffix) + suffix.length());
48 AUTH_LOGI("get setting bundle name success");
49 } else {
50 AUTH_LOGE("get setting bundle name failed");
51 }
52 pclose(fp);
53
54 return bytesRead > 0;
55 }
56
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)57 void HdcdConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
58 const sptr<IRemoteObject> &remoteObject, int32_t resultCode)
59 {
60 MessageParcel data;
61 MessageParcel reply;
62 MessageOption option;
63 const int paramNum = 3;
64 string bundleName;
65 string abilityName = "USBDebugDialog";
66 string parameters = "{\"ability.want.params.uiExtensionType\":\"sysDialog/common\",\"sysDialogZOrder\":2}";
67
68 AUTH_LOGE("connect success");
69
70 if (!GetSettingBundleName(bundleName)) {
71 AUTH_LOGE("Connect done get setting bundle error");
72 return;
73 }
74
75 data.WriteInt32(paramNum);
76 data.WriteString16(Str8ToStr16("bundleName"));
77 data.WriteString16(Str8ToStr16(bundleName));
78 data.WriteString16(Str8ToStr16("abilityName"));
79 data.WriteString16(Str8ToStr16(abilityName));
80
81 data.WriteString16(Str8ToStr16("parameters"));
82 data.WriteString16(Str8ToStr16(parameters));
83
84 if (!data.WriteParcelable(&element)) {
85 AUTH_LOGE("Connect done element error");
86 return;
87 }
88 if (!data.WriteRemoteObject(remoteObject)) {
89 AUTH_LOGE("Connect done remote object error");
90 return;
91 }
92 if (!data.WriteInt32(resultCode)) {
93 AUTH_LOGE("Connect done result code error");
94 return;
95 }
96
97 int32_t ret = remoteObject->SendRequest(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
98 showDialogResult = (ret == 0);
99 sem_post(&sem);
100 AUTH_LOGE("SendRequest(bundle(%s)ability(%s)parameters(%s)) ret(%d)",
101 bundleName.c_str(), abilityName.c_str(), parameters.c_str(), ret);
102 }
103
GetShowDialogResult(void)104 bool HdcdConnection::GetShowDialogResult(void)
105 {
106 AUTH_LOGE("wait for dialog ability");
107 sem_wait(&sem);
108 sem_destroy(&sem);
109 AUTH_LOGE("wait dialog ability over");
110 return showDialogResult;
111 }
112
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)113 void HdcdConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element,
114 int32_t resultCode)
115 {
116 AUTH_LOGE("disconnect success");
117 }
118
119 } // namespace AUTH
120 } // namespace HDC
121 } // namespace OHOS
122
123