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 "usb_function_switch_window.h"
17
18 #include <semaphore.h>
19 #include <sys/types.h>
20 #include <thread>
21 #include <unistd.h>
22
23 #include "ability_manager_client.h"
24 #include "common_event_manager.h"
25 #include "common_event_support.h"
26 #include "usb_errors.h"
27
28 using namespace OHOS::AppExecFwk;
29 using namespace OHOS::EventFwk;
30
31 namespace OHOS {
32 namespace USB {
33
34 std::shared_ptr<UsbFunctionSwitchWindow> UsbFunctionSwitchWindow::instance_;
35
GetInstance()36 std::shared_ptr<UsbFunctionSwitchWindow> UsbFunctionSwitchWindow::GetInstance()
37 {
38 if (instance_ == nullptr) {
39 USB_HILOGI(MODULE_USB_SERVICE, "reset to new instance");
40 instance_.reset(new UsbFunctionSwitchWindow());
41 }
42 return instance_;
43 }
44
UsbFunctionSwitchWindow()45 UsbFunctionSwitchWindow::UsbFunctionSwitchWindow() {}
46
~UsbFunctionSwitchWindow()47 UsbFunctionSwitchWindow::~UsbFunctionSwitchWindow()
48 {
49 if (windowAction_ == UsbFunctionSwitchWindowAction::FUNCTION_SWITCH_WINDOW_ACTION_SHOW) {
50 (void)UnShowFunctionSwitchWindow();
51 }
52 }
53
Init()54 int32_t UsbFunctionSwitchWindow::Init()
55 {
56 USB_HILOGI(MODULE_USB_SERVICE, "init: windown action=%{public}d", windowAction_);
57 return UEC_OK;
58 }
59
PopUpFunctionSwitchWindow()60 bool UsbFunctionSwitchWindow::PopUpFunctionSwitchWindow()
61 {
62 USB_HILOGI(MODULE_USB_SERVICE, "pop up function switch window");
63 char paramValue[PARAM_BUF_LEN] = { 0 };
64 const char defaultValue[PARAM_BUF_LEN] = { 0 };
65 std::lock_guard<std::mutex> guard(opMutex_);
66 int32_t ret = GetParameter("persist.usb.setting.gadget_conn_prompt", defaultValue, paramValue, sizeof(paramValue));
67 if (ret < 0) {
68 USB_HILOGE(MODULE_USB_SERVICE, "GetParameter fail");
69 return false;
70 }
71 ret = strcmp(paramValue, "true");
72 if (ret != 0) {
73 USB_HILOGE(MODULE_USB_SERVICE, "not allow open");
74 return false;
75 }
76 if (windowAction_ == UsbFunctionSwitchWindowAction::FUNCTION_SWITCH_WINDOW_ACTION_FORBID) {
77 USB_HILOGI(MODULE_USB_SERVICE, "forbid: pop up function switch window");
78 return false;
79 }
80 windowAction_ = UsbFunctionSwitchWindowAction::FUNCTION_SWITCH_WINDOW_ACTION_SHOW;
81 int32_t defaultChoose = 0;
82 (void)GetDefaultChooseFunction(defaultChoose);
83 return ShowFunctionSwitchWindow(defaultChoose);
84 }
85
DismissFunctionSwitchWindow()86 bool UsbFunctionSwitchWindow::DismissFunctionSwitchWindow()
87 {
88 USB_HILOGI(MODULE_USB_SERVICE, "dismiss function switch window");
89 std::lock_guard<std::mutex> guard(opMutex_);
90 if (windowAction_ == UsbFunctionSwitchWindowAction::FUNCTION_SWITCH_WINDOW_ACTION_FORBID) {
91 USB_HILOGI(MODULE_USB_SERVICE, "forbid: dismiss function switch window");
92 return false;
93 }
94 windowAction_ = UsbFunctionSwitchWindowAction::FUNCTION_SWITCH_WINDOW_ACTION_DISMISS;
95 return UnShowFunctionSwitchWindow();
96 }
97
GetDefaultChooseFunction(int32_t & defaultChoose)98 bool UsbFunctionSwitchWindow::GetDefaultChooseFunction(int32_t &defaultChoose)
99 {
100 defaultChoose = UsbFunctionChoose::FUNCTION_CHOOSE_CHARGE_ONLY;
101 return true;
102 }
103
ShowFunctionSwitchWindow(int32_t defaultChoose)104 bool UsbFunctionSwitchWindow::ShowFunctionSwitchWindow(int32_t defaultChoose)
105 {
106 USB_HILOGI(MODULE_USB_SERVICE, "show function switch window right now");
107 auto abmc = AAFwk::AbilityManagerClient::GetInstance();
108 if (abmc == nullptr) {
109 USB_HILOGE(MODULE_USB_SERVICE, "GetInstance failed");
110 return false;
111 }
112 AAFwk::Want want;
113 want.SetElementName(functionSwitchBundleName_, functionSwitchExtAbility_);
114 want.SetParam("defaultChoose", defaultChoose);
115
116 auto ret = abmc->StartAbility(want);
117 if (ret != UEC_OK) {
118 USB_HILOGE(MODULE_SERVICE, "StartAbility failed %{public}d", ret);
119 return false;
120 }
121 USB_HILOGD(MODULE_SERVICE, "StartAbility success");
122 return true;
123 }
124
UnShowFunctionSwitchWindow()125 bool UsbFunctionSwitchWindow::UnShowFunctionSwitchWindow()
126 {
127 auto abmc = AAFwk::AbilityManagerClient::GetInstance();
128 if (abmc == nullptr) {
129 USB_HILOGE(MODULE_USB_SERVICE, "GetInstance failed");
130 return false;
131 }
132 USB_HILOGI(MODULE_USB_SERVICE, "unshow function switch window");
133 AAFwk::Want want;
134 want.SetElementName(functionSwitchBundleName_, functionSwitchExtAbility_);
135 auto ret = abmc->StopServiceAbility(want);
136 if (ret != UEC_OK) {
137 USB_HILOGE(MODULE_SERVICE, "StopServiceAbility failed %{public}d", ret);
138 if (abmc->KillProcess(functionSwitchBundleName_) != UEC_OK) {
139 USB_HILOGE(MODULE_USB_SERVICE, "KillProcess failed");
140 }
141 return false;
142 }
143 USB_HILOGD(MODULE_USB_SERVICE, "unshow function switch window success");
144 return true;
145 }
146
147 } // namespace USB
148 } // namespace OHOS
149