Merge cherrypicks of [11358637, 11357965, 11355583] into sparse-6436923-L65000000546543974

Change-Id: I721a0700c7557a8ffdc9a565d907d27bd651a8ac
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;