[RESTRICT AUTOMERGE] Update PROVISIONING_USE_MOBILE_DATA behaviour.

The new behaviour is the following:
1. For QR and NFC provisioning:
 1.1. If DPC download info is not supplied, go to the landing screen
 1.2. Else if wifi SSID is specified, go to landing screen
 1.3. Else if wifi connection already exists, go to landing screen
 1.4. Else if PROVISIONING_USE_MOBILE_DATA is set to true, go to
      landing screen, ManagedProvisioning will handle mobile
      internet connection during the provisioning process
 1.5. Else if wifi SSID is NOT specified in the provisioning extras,
      show the wifi picker, pick wifi connection and go to landing
      screen
2. Maintain existing behaviour for non-QR and non-NFC provisioning

Test: manually performed provisioning with and without a
data-enabled SIM card, with and without a wifi connection, and
with and without any connection
Test: manual QA verified
Fixes: 153442588

Change-Id: I94b3a7a68ae7bef3477eb98488b7e57140cb518d
(cherry picked from commit 6f3efc7f9b2bbedffb74a42009a0432a01334851)
diff --git a/src/com/android/managedprovisioning/common/Utils.java b/src/com/android/managedprovisioning/common/Utils.java
index 6bbe2db..61dba92 100644
--- a/src/com/android/managedprovisioning/common/Utils.java
+++ b/src/com/android/managedprovisioning/common/Utils.java
@@ -532,6 +532,13 @@
                         /* defValue= */ PROVISIONING_TRIGGER_UNSPECIFIED);
     }
 
+    public boolean isQrProvisioning(Intent intent) {
+        return PROVISIONING_TRIGGER_QR_CODE ==
+                intent.getIntExtra(
+                        DevicePolicyManager.EXTRA_PROVISIONING_TRIGGER,
+                        /* defValue= */ PROVISIONING_TRIGGER_UNSPECIFIED);
+    }
+
     /**
      * Returns if the given intent for a organization owned provisioning.
      * Only QR, cloud enrollment and NFC are owned by organization.
diff --git a/src/com/android/managedprovisioning/model/ProvisioningParams.java b/src/com/android/managedprovisioning/model/ProvisioningParams.java
index 84421cb..e73805c 100644
--- a/src/com/android/managedprovisioning/model/ProvisioningParams.java
+++ b/src/com/android/managedprovisioning/model/ProvisioningParams.java
@@ -88,6 +88,7 @@
     public static final boolean DEFAULT_STARTED_BY_TRUSTED_SOURCE = false;
     public static final boolean DEFAULT_IS_NFC = false;
     public static final boolean DEFAULT_IS_CLOUD_ENROLLMENT = false;
+    public static final boolean DEFAULT_IS_QR_PROVISIONING = false;
     public static final boolean DEFAULT_LEAVE_ALL_SYSTEM_APPS_ENABLED = false;
     public static final boolean DEFAULT_EXTRA_PROVISIONING_SKIP_ENCRYPTION = false;
     public static final boolean DEFAULT_EXTRA_PROVISIONING_SKIP_USER_CONSENT = false;
@@ -122,6 +123,7 @@
     private static final String TAG_STARTED_BY_TRUSTED_SOURCE = "started-by-trusted-source";
     private static final String TAG_IS_NFC = "started-is-nfc";
     private static final String TAG_IS_CLOUD_ENROLLMENT = "is-cloud-enrollment";
+    private static final String TAG_IS_QR_PROVISIONING = "is-qr-provisioning";
     private static final String TAG_PROVISIONING_ACTION = "provisioning-action";
     private static final String TAG_IS_ORGANIZATION_OWNED_PROVISIONING =
             "is-organization-owned-provisioning";
@@ -230,6 +232,8 @@
 
     public final boolean isCloudEnrollment;
 
+    public final boolean isQrProvisioning;
+
     /** True if all system apps should be enabled after provisioning. */
     public final boolean leaveAllSystemAppsEnabled;
 
@@ -307,6 +311,7 @@
         startedByTrustedSource = builder.mStartedByTrustedSource;
         isNfc = builder.mIsNfc;
         isCloudEnrollment = builder.mIsCloudEnrollment;
+        isQrProvisioning = builder.mIsQrProvisioning;
         leaveAllSystemAppsEnabled = builder.mLeaveAllSystemAppsEnabled;
         skipEncryption = builder.mSkipEncryption;
         accountToMigrate = builder.mAccountToMigrate;
@@ -361,6 +366,7 @@
         bundle.putBoolean(TAG_STARTED_BY_TRUSTED_SOURCE, startedByTrustedSource);
         bundle.putBoolean(TAG_IS_NFC, isNfc);
         bundle.putBoolean(TAG_IS_CLOUD_ENROLLMENT, isCloudEnrollment);
+        bundle.putBoolean(TAG_IS_QR_PROVISIONING, isQrProvisioning);
         bundle.putBoolean(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED,
                 leaveAllSystemAppsEnabled);
         bundle.putBoolean(EXTRA_PROVISIONING_SKIP_ENCRYPTION, skipEncryption);
@@ -411,6 +417,7 @@
         builder.setStartedByTrustedSource(bundle.getBoolean(TAG_STARTED_BY_TRUSTED_SOURCE));
         builder.setIsNfc(bundle.getBoolean(TAG_IS_NFC));
         builder.setIsCloudEnrollment(bundle.getBoolean(TAG_IS_CLOUD_ENROLLMENT));
+        builder.setIsQrProvisioning(bundle.getBoolean(TAG_IS_QR_PROVISIONING));
         builder.setSkipEncryption(bundle.getBoolean(EXTRA_PROVISIONING_SKIP_ENCRYPTION));
         builder.setLeaveAllSystemAppsEnabled(bundle.getBoolean(
                 EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED));
@@ -530,6 +537,7 @@
         private boolean mStartedByTrustedSource = DEFAULT_STARTED_BY_TRUSTED_SOURCE;
         private boolean mIsNfc = DEFAULT_IS_NFC;
         private boolean mIsCloudEnrollment = DEFAULT_IS_CLOUD_ENROLLMENT;
+        private boolean mIsQrProvisioning = DEFAULT_IS_QR_PROVISIONING;
         private boolean mLeaveAllSystemAppsEnabled = DEFAULT_LEAVE_ALL_SYSTEM_APPS_ENABLED;
         private boolean mSkipEncryption = DEFAULT_EXTRA_PROVISIONING_SKIP_ENCRYPTION;
         private boolean mSkipUserConsent = DEFAULT_EXTRA_PROVISIONING_SKIP_USER_CONSENT;
@@ -641,6 +649,11 @@
             return this;
         }
 
+        public Builder setIsQrProvisioning(boolean qrProvisioning) {
+            mIsQrProvisioning = qrProvisioning;
+            return this;
+        }
+
         public Builder setLeaveAllSystemAppsEnabled(boolean leaveAllSystemAppsEnabled) {
             mLeaveAllSystemAppsEnabled = leaveAllSystemAppsEnabled;
             return this;
diff --git a/src/com/android/managedprovisioning/parser/ExtrasProvisioningDataParser.java b/src/com/android/managedprovisioning/parser/ExtrasProvisioningDataParser.java
index ecbef53..9b5c2f3 100644
--- a/src/com/android/managedprovisioning/parser/ExtrasProvisioningDataParser.java
+++ b/src/com/android/managedprovisioning/parser/ExtrasProvisioningDataParser.java
@@ -632,6 +632,7 @@
                     .setSupportUrl(supportUrl)
                     .setDeviceAdminIconFilePath(deviceAdminIconFilePath)
                     .setIsCloudEnrollment(mUtils.isCloudEnrollment(intent))
+                    .setIsQrProvisioning(mUtils.isQrProvisioning(intent))
                     .setIsOrganizationOwnedProvisioning(
                             mUtils.isOrganizationOwnedProvisioning(intent));
         } catch (ClassCastException e) {
diff --git a/src/com/android/managedprovisioning/preprovisioning/PreProvisioningController.java b/src/com/android/managedprovisioning/preprovisioning/PreProvisioningController.java
index 62d4414..8584504 100644
--- a/src/com/android/managedprovisioning/preprovisioning/PreProvisioningController.java
+++ b/src/com/android/managedprovisioning/preprovisioning/PreProvisioningController.java
@@ -35,6 +35,9 @@
 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_IMEI;
 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SERIAL_NUMBER;
 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_EDUCATION_SCREENS;
+import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_TRIGGER;
+import static android.app.admin.DevicePolicyManager.PROVISIONING_TRIGGER_QR_CODE;
+import static android.app.admin.DevicePolicyManager.PROVISIONING_TRIGGER_UNSPECIFIED;
 import static android.nfc.NfcAdapter.ACTION_NDEF_DISCOVERED;
 
 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_PREPROVISIONING_ACTIVITY_TIME_MS;
@@ -288,7 +291,7 @@
         if (isDeviceOwnerProvisioning()) {
             // TODO: make a general test based on deviceAdminDownloadInfo field
             // PO doesn't ever initialize that field, so OK as a general case
-            if (shouldShowWifiPicker()) {
+            if (shouldShowWifiPicker(intent)) {
                 // Have the user pick a wifi network if necessary.
                 // It is not possible to ask the user to pick a wifi network if
                 // the screen is locked.
@@ -324,7 +327,20 @@
         }
     }
 
-    private boolean shouldShowWifiPicker() {
+    private boolean isNfcProvisioning(Intent intent) {
+        return ACTION_NDEF_DISCOVERED.equals(intent.getAction());
+    }
+
+    private boolean isQrCodeProvisioning(Intent intent) {
+        if (!ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE.equals(intent.getAction())) {
+            return false;
+        }
+        final int provisioningTrigger = intent.getIntExtra(EXTRA_PROVISIONING_TRIGGER,
+                PROVISIONING_TRIGGER_UNSPECIFIED);
+        return provisioningTrigger == PROVISIONING_TRIGGER_QR_CODE;
+    }
+
+    private boolean shouldShowWifiPicker(Intent intent) {
         if (mParams.wifiInfo != null) {
             return false;
         }
@@ -334,6 +350,9 @@
         if (mUtils.isConnectedToWifi(mContext)) {
             return false;
         }
+        if (mParams.useMobileData && (isQrCodeProvisioning(intent) || isNfcProvisioning(intent))) {
+            return false;
+        }
         if (mParams.useMobileData) {
             return !mUtils.isMobileNetworkConnectedToInternet(mContext);
         }
diff --git a/src/com/android/managedprovisioning/task/ConnectMobileNetworkTask.java b/src/com/android/managedprovisioning/task/ConnectMobileNetworkTask.java
index 32c527c..ae7f61d 100644
--- a/src/com/android/managedprovisioning/task/ConnectMobileNetworkTask.java
+++ b/src/com/android/managedprovisioning/task/ConnectMobileNetworkTask.java
@@ -32,7 +32,7 @@
  */
 public class ConnectMobileNetworkTask extends AbstractProvisioningTask
         implements NetworkMonitor.NetworkConnectedCallback {
-    private static final int RECONNECT_TIMEOUT_MS = 60000;
+    private static final int RECONNECT_TIMEOUT_MS = 600000;
 
     private final NetworkMonitor mNetworkMonitor;
 
@@ -61,7 +61,14 @@
         Settings.Global.putInt(mContext.getContentResolver(),
                 Settings.Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED, 1);
 
-        if (mUtils.isConnectedToNetwork(mContext)) {
+        if (isLegacyConnected()) {
+            success();
+            return;
+        }
+
+        if ((mProvisioningParams.isNfc
+                || mProvisioningParams.isQrProvisioning)
+                && mUtils.isMobileNetworkConnectedToInternet(mContext)) {
             success();
             return;
         }
@@ -84,14 +91,22 @@
     @Override
     public void onNetworkConnected() {
         ProvisionLogger.logd("onNetworkConnected");
-        if (mUtils.isConnectedToNetwork(mContext)) {
-            ProvisionLogger.logd("Connected to the mobile network");
+        if (isLegacyConnected()
+                || ((mProvisioningParams.isNfc || mProvisioningParams.isQrProvisioning)
+                        && mUtils.isMobileNetworkConnectedToInternet(mContext))) {
+            ProvisionLogger.logd("Connected to mobile data");
             finishTask(true);
             // Remove time out callback.
             mHandler.removeCallbacks(mTimeoutRunnable);
         }
     }
 
+    private boolean isLegacyConnected() {
+        return !mProvisioningParams.isNfc
+                && !mProvisioningParams.isQrProvisioning
+                && mUtils.isConnectedToNetwork(mContext);
+    }
+
     private synchronized void finishTask(boolean isSuccess) {
         if (mTaskDone) {
             return;