Merge "Improve UX of Wi-Fi QR code scanner enrollee flow"
diff --git a/src/com/android/settings/wifi/AddNetworkFragment.java b/src/com/android/settings/wifi/AddNetworkFragment.java
index 651b25f..81b6c85 100644
--- a/src/com/android/settings/wifi/AddNetworkFragment.java
+++ b/src/com/android/settings/wifi/AddNetworkFragment.java
@@ -19,12 +19,14 @@
 import android.app.Activity;
 import android.app.settings.SettingsEnums;
 import android.content.Intent;
+import android.net.wifi.WifiConfiguration;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.ImageButton;
+import android.widget.TextView;
 
 import androidx.annotation.VisibleForTesting;
 
@@ -40,7 +42,10 @@
     final static int SUBMIT_BUTTON_ID = android.R.id.button1;
     @VisibleForTesting
     final static int CANCEL_BUTTON_ID = android.R.id.button2;
-    final static int SCANNER_BUTTON_ID = R.id.ssid_scanner_button;
+    final static int SSID_SCANNER_BUTTON_ID = R.id.ssid_scanner_button;
+    final static int PASSWORD_SCANNER_BUTTON_ID = R.id.password_scanner_button;
+
+    private static final int REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER = 0;
 
     private WifiConfigController mUIController;
     private Button mSubmitBtn;
@@ -68,10 +73,12 @@
 
         mSubmitBtn = rootView.findViewById(SUBMIT_BUTTON_ID);
         mCancelBtn = rootView.findViewById(CANCEL_BUTTON_ID);
-        final ImageButton scannerButton = rootView.findViewById(SCANNER_BUTTON_ID);
+        final ImageButton ssidScannerButton = rootView.findViewById(SSID_SCANNER_BUTTON_ID);
+        final ImageButton passwordScannerButton = rootView.findViewById(PASSWORD_SCANNER_BUTTON_ID);
         mSubmitBtn.setOnClickListener(this);
         mCancelBtn.setOnClickListener(this);
-        scannerButton.setOnClickListener(this);
+        ssidScannerButton.setOnClickListener(this);
+        passwordScannerButton.setOnClickListener(this);
         mUIController = new WifiConfigController(this, rootView, null, getMode());
 
         return rootView;
@@ -85,6 +92,8 @@
 
     @Override
     public void onClick(View view) {
+        String ssid = null;
+
         switch (view.getId()) {
             case SUBMIT_BUTTON_ID:
                 handleSubmitAction();
@@ -92,15 +101,34 @@
             case CANCEL_BUTTON_ID:
                 handleCancelAction();
                 break;
-            case SCANNER_BUTTON_ID:
+            case SSID_SCANNER_BUTTON_ID:
+                final TextView ssidEditText = getView().findViewById(R.id.ssid);
+                ssid = ssidEditText.getText().toString();
+                // No break and flows to case PASSWORD_SCANNER_BUTTON_ID
+            case PASSWORD_SCANNER_BUTTON_ID:
                 // Launch QR code scanner to join a network.
-                getContext().startActivity(
-                        WifiDppUtils.getEnrolleeQrCodeScannerIntent(/* ssid */ null));
+                startActivityForResult(WifiDppUtils.getEnrolleeQrCodeScannerIntent(ssid),
+                        REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER);
                 break;
         }
     }
 
     @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+
+        if (requestCode == REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER) {
+            if (resultCode != Activity.RESULT_OK) {
+                return;
+            }
+
+            final WifiConfiguration config = data.getParcelableExtra(
+                    WifiDialogActivity.KEY_WIFI_CONFIGURATION);
+            successfullyFinish(config);
+        }
+    }
+
+    @Override
     public int getMode() {
         return WifiConfigUiBase.MODE_CONNECT;
     }
@@ -158,9 +186,13 @@
 
     @VisibleForTesting
     void handleSubmitAction() {
+        successfullyFinish(mUIController.getConfig());
+    }
+
+    private void successfullyFinish(WifiConfiguration config) {
         final Intent intent = new Intent();
         final Activity activity = getActivity();
-        intent.putExtra(WIFI_CONFIG_KEY, mUIController.getConfig());
+        intent.putExtra(WIFI_CONFIG_KEY, config);
         activity.setResult(Activity.RESULT_OK, intent);
         activity.finish();
     }
diff --git a/src/com/android/settings/wifi/WifiDialog.java b/src/com/android/settings/wifi/WifiDialog.java
index 7d5f3b3..fbea824 100644
--- a/src/com/android/settings/wifi/WifiDialog.java
+++ b/src/com/android/settings/wifi/WifiDialog.java
@@ -18,15 +18,16 @@
 
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.ImageButton;
+import android.widget.TextView;
 
 import androidx.appcompat.app.AlertDialog;
 
 import com.android.settings.R;
-import com.android.settings.wifi.dpp.WifiDppUtils;
 import com.android.settingslib.RestrictedLockUtils;
 import com.android.settingslib.RestrictedLockUtilsInternal;
 import com.android.settingslib.wifi.AccessPoint;
@@ -40,6 +41,9 @@
 
         default void onSubmit(WifiDialog dialog) {
         }
+
+        default void onScan(WifiDialog dialog, String ssid) {
+        }
     }
 
     private static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
@@ -80,18 +84,6 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         mView = getLayoutInflater().inflate(R.layout.wifi_dialog, /* root */ null);
-        final ImageButton scannerButton = mView.findViewById(R.id.password_scanner_button);
-        if (scannerButton != null) {
-            scannerButton.setOnClickListener((View v) -> {
-                String ssid = null;
-                if (mAccessPoint != null) {
-                    ssid = mAccessPoint.getSsidStr();
-                }
-                // Launch QR code scanner to join a network.
-                getContext().startActivity(
-                        WifiDppUtils.getEnrolleeQrCodeScannerIntent(ssid));
-            });
-        }
         setView(mView);
         mController = new WifiConfigController(this, mView, mAccessPoint, mMode);
         super.onCreate(savedInstanceState);
@@ -109,6 +101,35 @@
         }
     }
 
+    @Override
+    protected void onStart() {
+        View.OnClickListener onClickScannerButtonListener = v -> {
+            if (mListener == null) {
+                return;
+            }
+
+            String ssid = null;
+            if (mAccessPoint == null) {
+                final TextView ssidEditText = findViewById(R.id.ssid);
+                ssid = ssidEditText.getText().toString();
+            } else {
+                ssid = mAccessPoint.getSsidStr();
+            }
+            mListener.onScan(/* WifiDialog */ this, ssid);
+        };
+
+        final ImageButton ssidScannerButton = findViewById(R.id.ssid_scanner_button);
+        ssidScannerButton.setOnClickListener(onClickScannerButtonListener);
+
+        final ImageButton passwordScannerButton = findViewById(R.id.password_scanner_button);
+        passwordScannerButton.setOnClickListener(onClickScannerButtonListener);
+
+        if (mHideSubmitButton) {
+            ssidScannerButton.setVisibility(View.GONE);
+            passwordScannerButton.setVisibility(View.GONE);
+        }
+    }
+
     public void onRestoreInstanceState(Bundle savedInstanceState) {
         super.onRestoreInstanceState(savedInstanceState);
         mController.updatePassword();
diff --git a/src/com/android/settings/wifi/WifiDialogActivity.java b/src/com/android/settings/wifi/WifiDialogActivity.java
index c32bcf3..35de66e 100644
--- a/src/com/android/settings/wifi/WifiDialogActivity.java
+++ b/src/com/android/settings/wifi/WifiDialogActivity.java
@@ -29,6 +29,7 @@
 import androidx.annotation.VisibleForTesting;
 
 import com.android.settings.SetupWizardUtils;
+import com.android.settings.wifi.dpp.WifiDppUtils;
 import com.android.settingslib.wifi.AccessPoint;
 
 import com.google.android.setupcompat.util.WizardManagerHelper;
@@ -49,10 +50,13 @@
     @VisibleForTesting
     static final String KEY_CONNECT_FOR_CALLER = "connect_for_caller";
 
-    private static final String KEY_WIFI_CONFIGURATION = "wifi_configuration";
+    public static final String KEY_WIFI_CONFIGURATION = "wifi_configuration";
+
     private static final int RESULT_CONNECTED = RESULT_FIRST_USER;
     private static final int RESULT_FORGET = RESULT_FIRST_USER + 1;
 
+    private static final int REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER = 0;
+
     private WifiDialog mDialog;
 
     @Override
@@ -162,4 +166,25 @@
         mDialog = null;
         finish();
     }
+
+    @Override
+    public void onScan(WifiDialog dialog, String ssid) {
+        // Launch QR code scanner to join a network.
+        startActivityForResult(WifiDppUtils.getEnrolleeQrCodeScannerIntent(ssid),
+                REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER);
+    }
+
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+
+        if (requestCode == REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER) {
+            if (resultCode != RESULT_OK) {
+                return;
+            }
+
+            setResult(RESULT_CONNECTED, data);
+            finish();
+        }
+    }
 }
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index 4ca9f86..27c495e 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -117,6 +117,8 @@
     private static final String PREF_KEY_SAVED_NETWORKS = "saved_networks";
     private static final String PREF_KEY_STATUS_MESSAGE = "wifi_status_message";
 
+    private static final int REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER = 0;
+
     private static boolean isVerboseLoggingEnabled() {
         return WifiTracker.sVerboseLogging || Log.isLoggable(TAG, Log.VERBOSE);
     }
@@ -427,10 +429,17 @@
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
 
-        // Only handle request comes from AddNetworkFragment
         if (requestCode == ADD_NETWORK_REQUEST) {
             handleAddNetworkRequest(resultCode, data);
             return;
+        } else if (requestCode == REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER) {
+            if (resultCode == Activity.RESULT_OK) {
+                if (mDialog != null) {
+                    mDialog.dismiss();
+                }
+                mWifiTracker.resumeScanning();
+            }
+            return;
         }
 
         final boolean formerlyRestricted = mIsRestricted;
@@ -1065,6 +1074,13 @@
         }
     }
 
+    @Override
+    public void onScan(WifiDialog dialog, String ssid) {
+        // Launch QR code scanner to join a network.
+        startActivityForResult(WifiDppUtils.getEnrolleeQrCodeScannerIntent(ssid),
+                REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER);
+    }
+
     /* package */ void submit(WifiConfigController configController) {
 
         final WifiConfiguration config = configController.getConfig();
diff --git a/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java b/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java
index e664a05..a438ecb 100644
--- a/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java
+++ b/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java
@@ -31,7 +31,6 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
-import android.provider.Settings;
 import android.text.TextUtils;
 import android.util.Log;
 import android.util.Size;
@@ -48,6 +47,7 @@
 import androidx.lifecycle.ViewModelProviders;
 
 import com.android.settings.R;
+import com.android.settings.wifi.WifiDialogActivity;
 import com.android.settings.wifi.qrcode.QrCamera;
 import com.android.settings.wifi.qrcode.QrDecorateView;
 
@@ -77,6 +77,7 @@
     // Key for Bundle usage
     private static final String KEY_IS_CONFIGURATOR_MODE = "key_is_configurator_mode";
     private static final String KEY_LATEST_ERROR_CODE = "key_latest_error_code";
+    private static final String KEY_WIFI_CONFIGURATION = "key_wifi_configuration";
 
     private ProgressBar mProgressBar;
     private QrCamera mCamera;
@@ -93,6 +94,9 @@
     /** QR code data scanned by camera */
     private WifiQrCode mWifiQrCode;
 
+    /** The WifiConfiguration connecting for enrollee usage */
+    private WifiConfiguration mWifiConfiguration;
+
     private int mLatestStatusCode = WifiDppUtils.EASY_CONNECT_EVENT_FAILURE_NONE;
 
     @Override
@@ -102,6 +106,7 @@
         if (savedInstanceState != null) {
             mIsConfiguratorMode = savedInstanceState.getBoolean(KEY_IS_CONFIGURATOR_MODE);
             mLatestStatusCode = savedInstanceState.getInt(KEY_LATEST_ERROR_CODE);
+            mWifiConfiguration = savedInstanceState.getParcelable(KEY_WIFI_CONFIGURATION);
         }
 
         final WifiDppInitiatorViewModel model =
@@ -417,6 +422,7 @@
                     mErrorMessage.setVisibility(View.INVISIBLE);
 
                     final WifiNetworkConfig wifiNetworkConfig = (WifiNetworkConfig)msg.obj;
+                    mWifiConfiguration = wifiNetworkConfig.getWifiConfigurationOrNull();
                     wifiNetworkConfig.connect(getContext(),
                             /* listener */ WifiDppQrCodeScannerFragment.this);
                     break;
@@ -431,6 +437,7 @@
     public void onSaveInstanceState(Bundle outState) {
         outState.putBoolean(KEY_IS_CONFIGURATOR_MODE, mIsConfiguratorMode);
         outState.putInt(KEY_LATEST_ERROR_CODE, mLatestStatusCode);
+        outState.putParcelable(KEY_WIFI_CONFIGURATION, mWifiConfiguration);
 
         super.onSaveInstanceState(outState);
     }
@@ -446,6 +453,7 @@
             for (WifiConfiguration wifiConfig : wifiConfigs) {
                 if (wifiConfig.networkId == newNetworkId) {
                     mLatestStatusCode = WifiDppUtils.EASY_CONNECT_EVENT_SUCCESS;
+                    mWifiConfiguration = wifiConfig;
                     wifiManager.connect(wifiConfig, WifiDppQrCodeScannerFragment.this);
                     return;
                 }
@@ -539,9 +547,11 @@
 
     @Override
     public void onSuccess() {
-        startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
+        final Intent resultIntent = new Intent();
+        resultIntent.putExtra(WifiDialogActivity.KEY_WIFI_CONFIGURATION, mWifiConfiguration);
+
         final Activity hostActivity = getActivity();
-        hostActivity.setResult(Activity.RESULT_OK);
+        hostActivity.setResult(Activity.RESULT_OK, resultIntent);
         hostActivity.finish();
     }
 
diff --git a/src/com/android/settings/wifi/dpp/WifiDppUtils.java b/src/com/android/settings/wifi/dpp/WifiDppUtils.java
index 24cd1d7..6c991cf 100644
--- a/src/com/android/settings/wifi/dpp/WifiDppUtils.java
+++ b/src/com/android/settings/wifi/dpp/WifiDppUtils.java
@@ -95,6 +95,11 @@
     /**
      * Returns an intent to launch QR code scanner for Wi-Fi DPP enrollee.
      *
+     * After enrollee success, the callee activity will return connecting WifiConfiguration by
+     * putExtra {@code WifiDialogActivity.KEY_WIFI_CONFIGURATION} for
+     * {@code Activity#setResult(int resultCode, Intent data)}. The calling object should check
+     * if it's available before using it.
+     *
      * @param ssid The data corresponding to {@code WifiConfiguration} SSID
      * @return Intent for launching QR code scanner
      */
diff --git a/src/com/android/settings/wifi/dpp/WifiNetworkConfig.java b/src/com/android/settings/wifi/dpp/WifiNetworkConfig.java
index 979e602..6135cba 100644
--- a/src/com/android/settings/wifi/dpp/WifiNetworkConfig.java
+++ b/src/com/android/settings/wifi/dpp/WifiNetworkConfig.java
@@ -228,7 +228,7 @@
     /**
      * This is a simplified method from {@code WifiConfigController.getConfig()}
      */
-    private WifiConfiguration getWifiConfigurationOrNull() {
+    WifiConfiguration getWifiConfigurationOrNull() {
         if (!isValidConfig(this)) {
             return null;
         }