Remove unset threadNetwork Scan (#31704)

* divide android commissioning parameter

* Fix countrycode copy issue

* Restyled by clang-format

* Update commissioningParameter

---------

Co-authored-by: Restyled.io <commits@restyled.io>
diff --git a/src/controller/CommissioningDelegate.h b/src/controller/CommissioningDelegate.h
index 175c246..2a78663 100644
--- a/src/controller/CommissioningDelegate.h
+++ b/src/controller/CommissioningDelegate.h
@@ -370,9 +370,12 @@
         mAttestationNonce.SetValue(attestationNonce);
         return *this;
     }
+
+    // If a WiFiCredentials is provided, then the WiFiNetworkScan will not be attempted
     CommissioningParameters & SetWiFiCredentials(WiFiCredentials wifiCreds)
     {
         mWiFiCreds.SetValue(wifiCreds);
+        mAttemptWiFiNetworkScan.SetValue(false);
         return *this;
     }
 
diff --git a/src/controller/java/AndroidDeviceControllerWrapper.cpp b/src/controller/java/AndroidDeviceControllerWrapper.cpp
index 74e1187..c965bb0 100644
--- a/src/controller/java/AndroidDeviceControllerWrapper.cpp
+++ b/src/controller/java/AndroidDeviceControllerWrapper.cpp
@@ -104,7 +104,7 @@
     jobject keypairDelegate, jbyteArray rootCertificate, jbyteArray intermediateCertificate, jbyteArray nodeOperationalCertificate,
     jbyteArray ipkEpochKey, uint16_t listenPort, uint16_t controllerVendorId, uint16_t failsafeTimerSeconds,
     bool attemptNetworkScanWiFi, bool attemptNetworkScanThread, bool skipCommissioningComplete,
-    bool skipAttestationCertificateValidation, CHIP_ERROR * errInfoOnFailure)
+    bool skipAttestationCertificateValidation, jstring countryCode, CHIP_ERROR * errInfoOnFailure)
 {
     if (errInfoOnFailure == nullptr)
     {
@@ -207,11 +207,30 @@
     wrapper->mGroupDataProvider.SetStorageDelegate(wrapperStorage);
     wrapper->mGroupDataProvider.SetSessionKeystore(initParams.sessionKeystore);
 
-    CommissioningParameters params = wrapper->mAutoCommissioner.GetCommissioningParameters();
+    CommissioningParameters params = wrapper->GetCommissioningParameters();
     params.SetFailsafeTimerSeconds(failsafeTimerSeconds);
     params.SetAttemptWiFiNetworkScan(attemptNetworkScanWiFi);
     params.SetAttemptThreadNetworkScan(attemptNetworkScanThread);
     params.SetSkipCommissioningComplete(skipCommissioningComplete);
+
+    if (countryCode != nullptr)
+    {
+        JniUtfString countryCodeJniString(env, countryCode);
+        if (countryCodeJniString.size() != kCountryCodeBufferLen)
+        {
+            *errInfoOnFailure = CHIP_ERROR_INVALID_ARGUMENT;
+            return nullptr;
+        }
+
+        MutableCharSpan copiedCode(wrapper->mCountryCode);
+        if (CopyCharSpanToMutableCharSpan(countryCodeJniString.charSpan(), copiedCode) != CHIP_NO_ERROR)
+        {
+            *errInfoOnFailure = CHIP_ERROR_INVALID_ARGUMENT;
+            return nullptr;
+        }
+        params.SetCountryCode(copiedCode);
+    }
+
     wrapper->UpdateCommissioningParameters(params);
 
     CHIP_ERROR err = wrapper->mGroupDataProvider.Init();
@@ -526,6 +545,7 @@
 {
     // this will wipe out any custom attestationNonce and csrNonce that was being used.
     // however, Android APIs don't allow these to be set to custom values today.
+    mCommissioningParameter = params;
     return mAutoCommissioner.SetCommissioningParameters(params);
 }
 
diff --git a/src/controller/java/AndroidDeviceControllerWrapper.h b/src/controller/java/AndroidDeviceControllerWrapper.h
index ac27937..5ccb2ed 100644
--- a/src/controller/java/AndroidDeviceControllerWrapper.h
+++ b/src/controller/java/AndroidDeviceControllerWrapper.h
@@ -50,6 +50,8 @@
 
 constexpr uint8_t kUserActiveModeTriggerInstructionBufferLen =
     128 + 1; // 128bytes is max UserActiveModeTriggerInstruction size and 1 byte is for escape sequence.
+
+constexpr uint8_t kCountryCodeBufferLen = 2;
 /**
  * This class contains all relevant information for the JNI view of CHIPDeviceController
  * to handle all controller-related processing.
@@ -123,10 +125,7 @@
 
     chip::Credentials::PartialDACVerifier * GetPartialDACVerifier() { return &mPartialDACVerifier; }
 
-    const chip::Controller::CommissioningParameters & GetCommissioningParameters() const
-    {
-        return mAutoCommissioner.GetCommissioningParameters();
-    }
+    const chip::Controller::CommissioningParameters & GetCommissioningParameters() const { return mCommissioningParameter; }
 
     static AndroidDeviceControllerWrapper * FromJNIHandle(jlong handle)
     {
@@ -171,20 +170,19 @@
      * @param[in] skipCommissioningComplete whether to skip the CASE commissioningComplete command during commissioning
      * @param[out] errInfoOnFailure a pointer to a CHIP_ERROR that will be populated if this method returns nullptr
      */
-    static AndroidDeviceControllerWrapper *
-    AllocateNew(JavaVM * vm, jobject deviceControllerObj, chip::NodeId nodeId, chip::FabricId fabricId,
-                const chip::CATValues & cats, chip::System::Layer * systemLayer,
-                chip::Inet::EndPointManager<chip::Inet::TCPEndPoint> * tcpEndPointManager,
-                chip::Inet::EndPointManager<chip::Inet::UDPEndPoint> * udpEndPointManager,
+    static AndroidDeviceControllerWrapper * AllocateNew(
+        JavaVM * vm, jobject deviceControllerObj, chip::NodeId nodeId, chip::FabricId fabricId, const chip::CATValues & cats,
+        chip::System::Layer * systemLayer, chip::Inet::EndPointManager<chip::Inet::TCPEndPoint> * tcpEndPointManager,
+        chip::Inet::EndPointManager<chip::Inet::UDPEndPoint> * udpEndPointManager,
 #ifdef JAVA_MATTER_CONTROLLER_TEST
-                ExampleOperationalCredentialsIssuerPtr opCredsIssuer,
+        ExampleOperationalCredentialsIssuerPtr opCredsIssuer,
 #else
-                AndroidOperationalCredentialsIssuerPtr opCredsIssuer,
+        AndroidOperationalCredentialsIssuerPtr opCredsIssuer,
 #endif
-                jobject keypairDelegate, jbyteArray rootCertificate, jbyteArray intermediateCertificate,
-                jbyteArray nodeOperationalCertificate, jbyteArray ipkEpochKey, uint16_t listenPort, uint16_t controllerVendorId,
-                uint16_t failsafeTimerSeconds, bool attemptNetworkScanWiFi, bool attemptNetworkScanThread,
-                bool skipCommissioningComplete, bool skipAttestationCertificateValidation, CHIP_ERROR * errInfoOnFailure);
+        jobject keypairDelegate, jbyteArray rootCertificate, jbyteArray intermediateCertificate,
+        jbyteArray nodeOperationalCertificate, jbyteArray ipkEpochKey, uint16_t listenPort, uint16_t controllerVendorId,
+        uint16_t failsafeTimerSeconds, bool attemptNetworkScanWiFi, bool attemptNetworkScanThread, bool skipCommissioningComplete,
+        bool skipAttestationCertificateValidation, jstring countryCode, CHIP_ERROR * errInfoOnFailure);
 
     void Shutdown();
 
@@ -246,6 +244,8 @@
     std::vector<uint8_t> mIcacCertificate;
     std::vector<uint8_t> mRcacCertificate;
 
+    char mCountryCode[kCountryCodeBufferLen];
+
     chip::Controller::AutoCommissioner mAutoCommissioner;
 
     chip::Credentials::PartialDACVerifier mPartialDACVerifier;
@@ -262,6 +262,8 @@
     chip::MutableCharSpan mUserActiveModeTriggerInstruction = chip::MutableCharSpan(mUserActiveModeTriggerInstructionBuffer);
     chip::BitMask<chip::app::Clusters::IcdManagement::UserActiveModeTriggerBitmap> mUserActiveModeTriggerHint;
 
+    chip::Controller::CommissioningParameters mCommissioningParameter;
+
     AndroidDeviceControllerWrapper(ChipDeviceControllerPtr controller,
 #ifdef JAVA_MATTER_CONTROLLER_TEST
                                    ExampleOperationalCredentialsIssuerPtr opCredsIssuer
diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp
index 99520d1..f1c7487 100644
--- a/src/controller/java/CHIPDeviceController-JNI.cpp
+++ b/src/controller/java/CHIPDeviceController-JNI.cpp
@@ -371,6 +371,10 @@
         jobject countryCodeOptional        = env->CallObjectMethod(controllerParams, getCountryCode);
         jobject regulatoryLocationOptional = env->CallObjectMethod(controllerParams, getRegulatoryLocation);
 
+        jobject countryCode;
+        err = chip::JniReferences::GetInstance().GetOptionalValue(countryCodeOptional, countryCode);
+        SuccessOrExit(err);
+
 #ifdef JAVA_MATTER_CONTROLLER_TEST
         std::unique_ptr<chip::Controller::ExampleOperationalCredentialsIssuer> opCredsIssuer(
             new chip::Controller::ExampleOperationalCredentialsIssuer());
@@ -383,7 +387,7 @@
             DeviceLayer::TCPEndPointManager(), DeviceLayer::UDPEndPointManager(), std::move(opCredsIssuer), keypairDelegate,
             rootCertificate, intermediateCertificate, operationalCertificate, ipk, listenPort, controllerVendorId,
             failsafeTimerSeconds, attemptNetworkScanWiFi, attemptNetworkScanThread, skipCommissioningComplete,
-            skipAttestationCertificateValidation, &err);
+            skipAttestationCertificateValidation, static_cast<jstring>(countryCode), &err);
         SuccessOrExit(err);
 
         if (caseFailsafeTimerSeconds > 0)
@@ -411,29 +415,6 @@
             }
         }
 
-        jobject countryCode;
-        err = chip::JniReferences::GetInstance().GetOptionalValue(countryCodeOptional, countryCode);
-        SuccessOrExit(err);
-
-        if (countryCode != nullptr)
-        {
-            jstring countryCodeStr = static_cast<jstring>(countryCode);
-            JniUtfString countryCodeJniString(env, countryCodeStr);
-
-            VerifyOrExit(countryCodeJniString.size() == 2, err = CHIP_ERROR_INVALID_ARGUMENT);
-
-            chip::Controller::CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters();
-            commissioningParams.SetCountryCode(countryCodeJniString.charSpan());
-
-            // The wrapper internally has reserved storage for the country code and will copy the value.
-            err = wrapper->UpdateCommissioningParameters(commissioningParams);
-            if (err != CHIP_NO_ERROR)
-            {
-                ChipLogError(Controller, "UpdateCommissioningParameters failed. Err = %" CHIP_ERROR_FORMAT, err.Format());
-                SuccessOrExit(err);
-            }
-        }
-
         jobject regulatoryLocation;
         err = chip::JniReferences::GetInstance().GetOptionalValue(regulatoryLocationOptional, regulatoryLocation);
         SuccessOrExit(err);
@@ -876,7 +857,7 @@
     chip::DeviceLayer::StackLock lock;
     AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);
 
-    CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters();
+    CommissioningParameters commissioningParams = wrapper->GetAutoCommissioner()->GetCommissioningParameters();
     CHIP_ERROR err                              = wrapper->ApplyNetworkCredentials(commissioningParams, networkCredentials);
     if (err != CHIP_NO_ERROR)
     {
@@ -884,10 +865,10 @@
         JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err);
         return;
     }
-    err = wrapper->UpdateCommissioningParameters(commissioningParams);
+    err = wrapper->GetAutoCommissioner()->SetCommissioningParameters(commissioningParams);
     if (err != CHIP_NO_ERROR)
     {
-        ChipLogError(Controller, "UpdateCommissioningParameters failed. Err = %" CHIP_ERROR_FORMAT, err.Format());
+        ChipLogError(Controller, "SetCommissioningParameters failed. Err = %" CHIP_ERROR_FORMAT, err.Format());
         JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err);
         return;
     }
@@ -911,7 +892,7 @@
     chip::DeviceLayer::StackLock lock;
     AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);
 
-    CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters();
+    CommissioningParameters commissioningParams = wrapper->GetAutoCommissioner()->GetCommissioningParameters();
     CHIP_ERROR err                              = wrapper->ApplyICDRegistrationInfo(commissioningParams, icdRegistrationInfo);
     if (err != CHIP_NO_ERROR)
     {
@@ -919,10 +900,10 @@
         JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err);
         return;
     }
-    err = wrapper->UpdateCommissioningParameters(commissioningParams);
+    err = wrapper->GetAutoCommissioner()->SetCommissioningParameters(commissioningParams);
     if (err != CHIP_NO_ERROR)
     {
-        ChipLogError(Controller, "UpdateCommissioningParameters failed. Err = %" CHIP_ERROR_FORMAT, err.Format());
+        ChipLogError(Controller, "SetCommissioningParameters failed. Err = %" CHIP_ERROR_FORMAT, err.Format());
         JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err);
         return;
     }