From 6cc5869751e93bce37c9aeebc03d0358d8c6976b Mon Sep 17 00:00:00 2001 From: liufeng Date: Tue, 7 Jan 2025 17:01:06 +0800 Subject: [PATCH 1/5] =?UTF-8?q?OOBE=E9=98=B6=E6=AE=B5=E5=88=A0=E9=99=A4USB?= =?UTF-8?q?=E5=BC=B9=E7=AA=97&=E9=87=8D=E5=90=AF=E4=B9=8B=E5=90=8E?= =?UTF-8?q?=E4=B8=8D=E5=BC=B9=E7=AA=97=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liufeng --- bundle.json | 1 + services/BUILD.gn | 4 + .../include/usb_function_switch_window.h | 2 + .../native/src/usb_function_switch_window.cpp | 89 ++++++++++++++- services/usb_service.cfg | 1 + utils/native/include/usb_settings_datashare.h | 38 +++++++ utils/native/src/usb_settings_datashare.cpp | 103 ++++++++++++++++++ 7 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 utils/native/include/usb_settings_datashare.h create mode 100644 utils/native/src/usb_settings_datashare.cpp diff --git a/bundle.json b/bundle.json index deb31b5c..bf58b047 100644 --- a/bundle.json +++ b/bundle.json @@ -35,6 +35,7 @@ "napi", "bundle_framework", "common_event_service", + "data_share", "drivers_interface_usb", "drivers_peripheral_usb", "init", diff --git a/services/BUILD.gn b/services/BUILD.gn index cecb1c39..5fb1015c 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -39,6 +39,7 @@ ohos_shared_library("usbservice") { install_enable = true sources = [ "${usb_manager_path}/services/zidl/src/usb_srv_stub.cpp", + "${utils_path}/native/src/usb_accessory_manager.cpp", "native/src/usb_accessory_manager.cpp", "native/src/usb_descriptor_parser.cpp", "native/src/usb_device_manager.cpp", @@ -77,12 +78,15 @@ ohos_shared_library("usbservice") { "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "data_share:data_share_common", + "data_share:data_share_consumer", "drivers_interface_usb:libusb_proxy_1.1", "drivers_peripheral_usb:libusb_interface_service_1.1", "hilog:libhilog", "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", + "init:libbeget_proxy", "ipc:ipc_single", "memmgr:memmgrclient", "napi:ace_napi", diff --git a/services/native/include/usb_function_switch_window.h b/services/native/include/usb_function_switch_window.h index b28df80d..285fad41 100644 --- a/services/native/include/usb_function_switch_window.h +++ b/services/native/include/usb_function_switch_window.h @@ -72,6 +72,8 @@ private: bool ShowFunctionSwitchWindow(); bool UnShowFunctionSwitchWindow(); bool CheckDialogInstallStatus(); + bool ShouldRejectShowWindow(); + static void BootCompletedEventCallback(const char *key, const char *value, void *context); static std::shared_ptr instance_; static int32_t GetSupportedFunctions(); sptr usbFuncAbilityConn = nullptr; diff --git a/services/native/src/usb_function_switch_window.cpp b/services/native/src/usb_function_switch_window.cpp index 18a3b48a..892600cb 100644 --- a/services/native/src/usb_function_switch_window.cpp +++ b/services/native/src/usb_function_switch_window.cpp @@ -28,7 +28,10 @@ #include "bundle_mgr_client.h" #include "common_event_manager.h" #include "common_event_support.h" +#include "usb_settings_datashare.h" +#include "os_account_manager.h" #include "usb_errors.h" +#include "uri.h" #define DEFAULT_PARAM_VALUE "charge,mtp,ptp" using namespace OHOS::AppExecFwk; @@ -112,6 +115,20 @@ bool UsbFunctionSwitchWindow::PopUpFunctionSwitchWindow() return false; } windowAction_ = UsbFunctionSwitchWindowAction::FUNCTION_SWITCH_WINDOW_ACTION_SHOW; + + isPromptEnabeld = OHOS::system::GetBoolParameter("bootevent.boot.completed", false); + if (!isPromptEnabeld) { + USB_HILOGE(MODULE_USB_SERVICE, "boot.completed is false!"); + int ret = WatchParameter("bootevent.boot.completed", BootCompletedEventCallback, this); + if (ret != 0) { + USB_HILOGI(MODULE_USB_SERVICE, "watchParameter is failed!"); + } + return false; + } + if (ShouldRejectShowWindow()) { + USB_HILOGE(MODULE_USB_SERVICE, "OOBE is not ready!"); + return false; + } return ShowFunctionSwitchWindow(); } @@ -293,12 +310,82 @@ bool UsbFunctionSwitchWindow::CheckDialogInstallStatus() } if (windowAction_ == UsbFunctionSwitchWindowAction::FUNCTION_SWITCH_WINDOW_ACTION_SHOW) { - ShowFunctionSwitchWindow(); + if (OHOS::system::GetBoolParameter("bootevent.boot.completed", false)) { + ShowFunctionSwitchWindow(); + } } return true; } USB_HILOGE(MODULE_USB_SERVICE, "dialog is not installed"); return false; } + +void UsbFunctionSwitchWindow::BootCompletedEventCallback(const char *key, const char *value, void *context) +{ + USB_HILOGI(MODULE_USB_SERVICE, "testParameterChange key: %{public}s, value: %{public}s!", key, value); + if (!OHOS::system::GetBoolParameter("bootevent.boot.completed", false)) { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: boot.completed is false!", __func__); + return; + } + if (context == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: context is null!", __func__); + return; + } + auto eventSwitchWindow = reinterpret_cast(context); + if (eventSwitchWindow == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "get eventSwitchWindow is null!"); + return; + } + + if (eventSwitchWindow->ShouldRejectShowWindow()) { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: OOBE is not ready!", __func__); + return; + } + bool ret = eventSwitchWindow->ShowFunctionSwitchWindow(); + if (!ret) { + USB_HILOGE(MODULE_USB_SERVICE, "watchParameter to ShowFunctionSwitchWindow is failed!"); + } +} + +bool UsbFunctionSwitchWindow::ShouldRejectShowWindow() +{ + auto datashareHelper = std::make_shared(); + std::string device_provisioned {"0"}; + OHOS::Uri uri( + "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=device_provisioned"); + bool resp = datashareHelper->Query(uri, "device_provisioned", device_provisioned); + if (resp && device_provisioned != "1") { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: device_provisioned is = 0", __func__); + return true; + } + + std::string user_setup_complete {"0"}; + std::vector activedOsAccountIds; + OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activedOsAccountIds); + if (activedOsAccountIds.empty()) { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: activedOsAccountIds is empty", __func__); + return true; + } + int userId = activedOsAccountIds[0]; + OHOS::Uri uri_setup( + "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_" + + std::to_string(userId) + "?Proxy=true&key=user_setup_complete"); + bool resp_userSetup = datashareHelper->Query(uri_setup, "user_setup_complete", user_setup_complete); + if (resp_userSetup && user_setup_complete != "1") { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: user_setup_complete is = 0", __func__); + return true; + } + + std::string is_ota_finished {"0"}; + OHOS::Uri uri_ota( + "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_" + + std::to_string(userId) + "?Proxy=true&key=is_ota_finished"); + bool resp_ota = datashareHelper->Query(uri_ota, "is_ota_finished", is_ota_finished); + if (!resp_ota && is_ota_finished == "0") { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: is_ota_finished is = 0", __func__); + return true; + } + return false; +} } // namespace USB } // namespace OHOS diff --git a/services/usb_service.cfg b/services/usb_service.cfg index ed9210a1..db5905c6 100644 --- a/services/usb_service.cfg +++ b/services/usb_service.cfg @@ -13,6 +13,7 @@ "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", "ohos.permission.GET_BUNDLE_RESOURCES", "ohos.permission.MANAGE_LOCAL_ACCOUNTS", + "ohos.permission.MANAGE_SECURE_SETTINGS", "ohos.permission.START_SYSTEM_DIALOG" ], "apl" : "system_basic", diff --git a/utils/native/include/usb_settings_datashare.h b/utils/native/include/usb_settings_datashare.h new file mode 100644 index 00000000..ebf448dc --- /dev/null +++ b/utils/native/include/usb_settings_datashare.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SETTINGS_DATASHARE_HELPER_H +#define SETTINGS_DATASHARE_HELPER_H + +#include "datashare_helper.h" +#include "datashare_predicates.h" +#include "system_ability_definition.h" +#include "singleton.h" +#include "uri.h" + +namespace OHOS { +namespace USB { +class UsbSettingDataShare { +public: + UsbSettingDataShare(); + ~UsbSettingDataShare(); + bool Query(Uri& uri, const std::string& key, std::string& values); +private: + std::shared_ptr CreateDataShareHelper(int systemAbilityId); + std::shared_ptr datashareHelper_ = nullptr; +}; +} // namespace USB +} // namespace OHOS +#endif //SETTINGS_DATASHARE_HELPER_H \ No newline at end of file diff --git a/utils/native/src/usb_settings_datashare.cpp b/utils/native/src/usb_settings_datashare.cpp new file mode 100644 index 00000000..864efee0 --- /dev/null +++ b/utils/native/src/usb_settings_datashare.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "usb_settings_datashare.h" + +#include "hilog_wrapper.h" +#include "datashare_helper.h" +#include "datashare_predicates.h" +#include "iservice_registry.h" +#include "uri.h" +#include "singleton.h" + +namespace OHOS { +namespace USB { +namespace { +constexpr const char *SETTINGS_DATASHARE_URI = + "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true"; +const std::string SETTINGS_DATASHARE_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; +constexpr const char *SETTINGS_DATA_COLUMN_KEYWORD = "KEYWORD"; +constexpr const char *SETTINGS_DATA_COLUMN_VALUE = "VALUE"; +} + +UsbSettingDataShare::UsbSettingDataShare() +{ + datashareHelper_ = CreateDataShareHelper(USB_SYSTEM_ABILITY_ID); +} + +UsbSettingDataShare::~UsbSettingDataShare() +{ + if (datashareHelper_ != nullptr) { + USB_HILOGI(MODULE_USB_SERVICE, "datashareHelper_ is here Release!"); + datashareHelper_->Release(); + } +} + +std::shared_ptr UsbSettingDataShare::CreateDataShareHelper(int systemAbilityId) +{ + USB_HILOGI(MODULE_USB_SERVICE, "CreateDataShareHelper start!"); + sptr saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saManager == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "GetSystemAbilityManager failed!"); + return nullptr; + } + sptr remote = saManager->GetSystemAbility(systemAbilityId); + if (remote == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "GetSystemAbility Service Failed!"); + return nullptr; + } + USB_HILOGI(MODULE_USB_SERVICE, "systemAbilityId = %{public}d", systemAbilityId); + return DataShare::DataShareHelper::Creator(remote, SETTINGS_DATASHARE_URI, SETTINGS_DATASHARE_EXT_URI); +} + +bool UsbSettingDataShare::Query(Uri& uri, const std::string& key, std::string& value) +{ + USB_HILOGI(MODULE_USB_SERVICE, "start Query key = %{public}s", key.c_str()); + if (datashareHelper_ == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "query error, datashareHelper_ is nullptr"); + return false; + } + + std::vector columns; + DataShare::DataSharePredicates predicates; + predicates.EqualTo(SETTINGS_DATA_COLUMN_KEYWORD, key); + auto result = datashareHelper_->Query(uri, predicates, columns); + if (result == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "query error, result is nullptr"); + return false; + } + + int rowCount = 0; + result->GetRowCount(rowCount); + if (rowCount == 0) { + USB_HILOGE(MODULE_USB_SERVICE, "query success, but rowCount is 0"); + return true; + } + + if (result->GoToFirstRow() != DataShare::E_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "query error, go to first row error"); + result->Close(); + return false; + } + + int columnIndex = 0; + result->GetColumnIndex(SETTINGS_DATA_COLUMN_VALUE, columnIndex); + result->GetString(columnIndex, value); + result->Close(); + USB_HILOGI(MODULE_USB_SERVICE, "SettingUtils: query success"); + return true; +} +} // namespace USB +} // namespace OHOS -- Gitee From 39b0d154988d54d1289149d070dc556d8f264e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=B8=B0?= Date: Tue, 7 Jan 2025 09:05:01 +0000 Subject: [PATCH 2/5] update services/native/src/usb_function_switch_window.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘丰 --- services/native/src/usb_function_switch_window.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/native/src/usb_function_switch_window.cpp b/services/native/src/usb_function_switch_window.cpp index 892600cb..ea2ca732 100644 --- a/services/native/src/usb_function_switch_window.cpp +++ b/services/native/src/usb_function_switch_window.cpp @@ -381,7 +381,7 @@ bool UsbFunctionSwitchWindow::ShouldRejectShowWindow() "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_" + std::to_string(userId) + "?Proxy=true&key=is_ota_finished"); bool resp_ota = datashareHelper->Query(uri_ota, "is_ota_finished", is_ota_finished); - if (!resp_ota && is_ota_finished == "0") { + if (resp_ota && is_ota_finished == "0") { USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: is_ota_finished is = 0", __func__); return true; } -- Gitee From f076ab4af9fb6fd3a6f7085f0538f3e0f3ac2fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=B8=B0?= Date: Tue, 7 Jan 2025 12:54:43 +0000 Subject: [PATCH 3/5] update services/BUILD.gn. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘丰 --- services/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index 5fb1015c..299b1aef 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -78,8 +78,8 @@ ohos_shared_library("usbservice") { "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", - "data_share:data_share_common", - "data_share:data_share_consumer", + "data_share:datashare_common", + "data_share:datashare_consumer", "drivers_interface_usb:libusb_proxy_1.1", "drivers_peripheral_usb:libusb_interface_service_1.1", "hilog:libhilog", -- Gitee From 573c2c259d3ec0dfd5d336158859ba1ed5063d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=B8=B0?= Date: Tue, 7 Jan 2025 13:05:03 +0000 Subject: [PATCH 4/5] update services/BUILD.gn. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘丰 --- services/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index 299b1aef..ca9a3f28 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -85,8 +85,8 @@ ohos_shared_library("usbservice") { "hilog:libhilog", "hisysevent:libhisysevent", "hitrace:hitrace_meter", - "init:libbegetutil", "init:libbeget_proxy", + "init:libbegetutil", "ipc:ipc_single", "memmgr:memmgrclient", "napi:ace_napi", -- Gitee From 14a55841d012eb8a24852f2b4643b69a3beb4865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=B8=B0?= Date: Tue, 7 Jan 2025 13:53:38 +0000 Subject: [PATCH 5/5] update services/BUILD.gn. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘丰 --- services/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index ca9a3f28..ef6dc03d 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -39,7 +39,7 @@ ohos_shared_library("usbservice") { install_enable = true sources = [ "${usb_manager_path}/services/zidl/src/usb_srv_stub.cpp", - "${utils_path}/native/src/usb_accessory_manager.cpp", + "${utils_path}/native/src/usb_settings_datashare.cpp", "native/src/usb_accessory_manager.cpp", "native/src/usb_descriptor_parser.cpp", "native/src/usb_device_manager.cpp", -- Gitee