diff --git a/bundle.json b/bundle.json index deb31b5cdfe2743015bfd55ecc8dca3e8b3bd116..bf58b047d90ffa6935e5e87bd143106f43900657 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 cecb1c3999948f33c3ba61c7090f18ef745b96d1..ef6dc03da9a89322f2526eeec228c5e0069f3bce 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_settings_datashare.cpp", "native/src/usb_accessory_manager.cpp", "native/src/usb_descriptor_parser.cpp", "native/src/usb_device_manager.cpp", @@ -77,11 +78,14 @@ ohos_shared_library("usbservice") { "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "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", "hisysevent:libhisysevent", "hitrace:hitrace_meter", + "init:libbeget_proxy", "init:libbegetutil", "ipc:ipc_single", "memmgr:memmgrclient", diff --git a/services/native/include/usb_function_switch_window.h b/services/native/include/usb_function_switch_window.h index b28df80d36ad46241a25f7a0153bc912e528bdf6..285fad41836b4d2e3464c2ab5ac4b11d3cdfba28 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 18a3b48a20b70d8f942c86c3b4c6a60d4645d18f..ea2ca732bf424c45a2d272262c271ccfd4a6294e 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 ed9210a1b8d2832b7bf85ad20c6418672678448c..db5905c6d560a522f4989ce6afeefa63d27231f2 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 0000000000000000000000000000000000000000..ebf448dceab898dff891fe55631d911ca5b7c1cc --- /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 0000000000000000000000000000000000000000..864efee0b1f278ae3b70dd9554f3e24092afa354 --- /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