Merge "Implement Wi-Fi DPP UI."
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 7807f0e..caca444 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -3054,6 +3054,14 @@
             </intent-filter>
         </activity>
 
+        <activity
+            android:name=".wifi.dpp.WifiDppEnrolleeActivity">
+            <intent-filter>
+                <action android:name="android.settings.WIFI_DPP_ENROLLEE_QR_CODE_SCANNER"/>
+                <category android:name="android.intent.category.DEFAULT"/>
+            </intent-filter>
+        </activity>
+
         <activity android:name=".homepage.contextualcards.ContextualCardFeedbackDialog"
                   android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.Alert" />
         <!-- This is the longest AndroidManifest.xml ever. -->
diff --git a/res/layout/wifi_dpp_qrcode_scanner_fragment.xml b/res/layout/wifi_dpp_qrcode_scanner_fragment.xml
index ab38ac1..0beacff 100644
--- a/res/layout/wifi_dpp_qrcode_scanner_fragment.xml
+++ b/res/layout/wifi_dpp_qrcode_scanner_fragment.xml
@@ -44,8 +44,5 @@
         android:layout_height="wrap_content"
         android:layout_gravity="center"/>
 
-    <include layout="@layout/wifi_dpp_fragment_footer"
-        android:gravity="center|bottom"/>
-
 </LinearLayout>
 
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ae080cd..78c64c2 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2056,6 +2056,14 @@
     <string name="wifi_dpp_add_device_to_network">Add a device to this network</string>
     <!-- Hint for the user to center another device's QR code in the below camera window [CHAR LIMIT=120] -->
     <string name="wifi_dpp_center_qr_code">Center the device\u2019s QR code below to add device to \u201c<xliff:g id="ssid" example="OfficeWifi">%1$s</xliff:g>\u201d</string>
+    <!-- Title for the fragment to scan QR code [CHAR LIMIT=50]  -->
+    <string name="wifi_dpp_scan_qr_code">Scan QR code</string>
+    <!-- Hint for the user to center another device's QR code in the below camera window [CHAR LIMIT=NONE] -->
+    <string name="wifi_dpp_scan_qr_code_join_network">Join \u201c<xliff:g id="ssid" example="OfficeWifi">%1$s</xliff:g>\u201d by scanning a QR code</string>
+    <!-- Title for the fragment to share Wi-Fi [CHAR LIMIT=50]  -->
+    <string name="wifi_dpp_share_wifi">Share Wi\u2011Fi</string>
+    <!-- Hint for the user to use another device to scan QR code on screen to join Wi-Fi [CHAR LIMIT=NONE] -->
+    <string name="wifi_dpp_scan_qr_code_with_another_device">Scan this QR code with another device to join \u201c<xliff:g id="ssid" example="OfficeWifi">%1$s</xliff:g>\u201d</string>
     <!-- Label for the check box to share a network with other users on the same device -->
     <string name="wifi_shared">Share with other device users</string>
     <!-- Hint for unchanged fields -->
diff --git a/src/com/android/settings/wifi/dpp/WifiDppConfiguratorActivity.java b/src/com/android/settings/wifi/dpp/WifiDppConfiguratorActivity.java
index d81c19b..e4ae292 100644
--- a/src/com/android/settings/wifi/dpp/WifiDppConfiguratorActivity.java
+++ b/src/com/android/settings/wifi/dpp/WifiDppConfiguratorActivity.java
@@ -31,8 +31,26 @@
 import com.android.settings.core.InstrumentedActivity;
 import com.android.settings.R;
 
+/**
+ * To provision "other" device with specified Wi-Fi network.
+ *
+ * Uses different intents to specify different provisioning ways.
+ *
+ * For intent action {@code ACTION_CONFIGURATOR_QR_CODE_SCANNER} and
+ * {@code android.settings.WIFI_DPP_CONFIGURATOR_QR_CODE_GENERATOR}, specify the Wi-Fi network to be
+ * provisioned in:
+ *
+ * {@code WifiDppUtils.EXTRA_WIFI_SECURITY}
+ * {@code WifiDppUtils.EXTRA_WIFI_SSID}
+ * {@code WifiDppUtils.EXTRA_WIFI_PRE_SHARED_KEY}
+ * {@code WifiDppUtils.EXTRA_WIFI_HIDDEN_SSID}
+ *
+ * For intent action {@code ACTION_CONFIGURATOR_CHOOSE_SAVED_WIFI_NETWORK}, specify Wi-Fi (DPP)
+ * QR code in {@code WifiDppUtils.EXTRA_QR_CODE}
+ */
 public class WifiDppConfiguratorActivity extends InstrumentedActivity implements
-        WifiNetworkConfig.Retriever {
+        WifiNetworkConfig.Retriever,
+        WifiDppQrCodeGeneratorFragment.OnQrCodeGeneratorFragmentAddButtonClickedListener {
     private static final String TAG = "WifiDppConfiguratorActivity";
 
     public static final String ACTION_CONFIGURATOR_QR_CODE_SCANNER =
@@ -43,7 +61,6 @@
             "android.settings.WIFI_DPP_CONFIGURATOR_CHOOSE_SAVED_WIFI_NETWORK";
 
     private FragmentManager mFragmentManager;
-    private FragmentTransaction mFragmentTransaction;
 
     /** The Wi-Fi network which will be configured */
     private WifiNetworkConfig mWifiNetworkConfig;
@@ -57,12 +74,20 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+
         setContentView(R.layout.wifi_dpp_activity);
-
         mFragmentManager = getSupportFragmentManager();
-        mFragmentTransaction = getSupportFragmentManager().beginTransaction();
 
-        Intent intent = getIntent();
+        handleIntent(getIntent());
+
+        ActionBar actionBar = getActionBar();
+        if (actionBar != null) {
+            actionBar.setElevation(0);
+            actionBar.setDisplayShowTitleEnabled(false);
+        }
+    }
+
+    private void handleIntent(Intent intent) {
         boolean cancelActivity = false;
         WifiNetworkConfig config;
         switch (intent.getAction()) {
@@ -72,7 +97,7 @@
                     cancelActivity = true;
                 } else {
                     mWifiNetworkConfig = config;
-                    addQrCodeScannerFragment(/* addToBackStack= */ false);
+                    showQrCodeScannerFragment(/* addToBackStack= */ false);
                 }
                 break;
             case ACTION_CONFIGURATOR_QR_CODE_GENERATOR:
@@ -81,11 +106,11 @@
                     cancelActivity = true;
                 } else {
                     mWifiNetworkConfig = config;
-                    addQrCodeGeneratorFragment();
+                    showQrCodeGeneratorFragment();
                 }
                 break;
             case ACTION_CONFIGURATOR_CHOOSE_SAVED_WIFI_NETWORK:
-                addChooseSavedWifiNetworkFragment(/* addToBackStack */ false);
+                showChooseSavedWifiNetworkFragment(/* addToBackStack */ false);
                 break;
             default:
                 cancelActivity = true;
@@ -98,45 +123,55 @@
         }
     }
 
-    private void addQrCodeScannerFragment(boolean addToBackStack) {
-        WifiDppQrCodeScannerFragment fragment = new WifiDppQrCodeScannerFragment();
-        mFragmentTransaction.add(R.id.fragment_container, fragment);
-        if (addToBackStack) {
-            mFragmentTransaction.addToBackStack(/* name */ null);
+    private void showQrCodeScannerFragment(boolean addToBackStack) {
+        // Avoid to replace the same fragment during configuration change
+        if (mFragmentManager.findFragmentByTag(WifiDppUtils.TAG_FRAGMENT_QR_CODE_SCANNER) != null) {
+            return;
         }
-        mFragmentTransaction.commit();
+
+        WifiDppQrCodeScannerFragment fragment = new WifiDppQrCodeScannerFragment();
+        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
+
+        fragmentTransaction.replace(R.id.fragment_container, fragment,
+                WifiDppUtils.TAG_FRAGMENT_QR_CODE_SCANNER);
+        if (addToBackStack) {
+            fragmentTransaction.addToBackStack(/* name */ null);
+        }
+        fragmentTransaction.commit();
     }
 
-    private void addQrCodeGeneratorFragment() {
+    private void showQrCodeGeneratorFragment() {
+        // Avoid to replace the same fragment during configuration change
+        if (mFragmentManager.findFragmentByTag(
+                WifiDppUtils.TAG_FRAGMENT_QR_CODE_GENERATOR) != null) {
+            return;
+        }
+
         WifiDppQrCodeGeneratorFragment fragment = new WifiDppQrCodeGeneratorFragment();
-        mFragmentTransaction.add(R.id.fragment_container, fragment);
-        mFragmentTransaction.commit();
+        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
+
+        fragmentTransaction.replace(R.id.fragment_container, fragment,
+                WifiDppUtils.TAG_FRAGMENT_QR_CODE_GENERATOR);
+        fragmentTransaction.commit();
     }
 
-    private void addChooseSavedWifiNetworkFragment(boolean addToBackStack) {
-        ActionBar action = getActionBar();
-        if (action != null) {
-            action.hide();
+    private void showChooseSavedWifiNetworkFragment(boolean addToBackStack) {
+        // Avoid to replace the same fragment during configuration change
+        if (mFragmentManager.findFragmentByTag(
+                WifiDppUtils.TAG_FRAGMENT_CHOOSE_SAVED_WIFI_NETWORK) != null) {
+            return;
         }
 
         WifiDppChooseSavedWifiNetworkFragment fragment =
                 new WifiDppChooseSavedWifiNetworkFragment();
-        mFragmentTransaction.add(R.id.fragment_container, fragment);
+        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
+
+        fragmentTransaction.replace(R.id.fragment_container, fragment,
+                WifiDppUtils.TAG_FRAGMENT_CHOOSE_SAVED_WIFI_NETWORK);
         if (addToBackStack) {
-            mFragmentTransaction.addToBackStack(/* name */ null);
+            fragmentTransaction.addToBackStack(/* name */ null);
         }
-        mFragmentTransaction.commit();
-    }
-
-    @Override
-    protected void onStop() {
-        Fragment fragment = mFragmentManager.findFragmentById(R.id.fragment_container);
-        if (fragment != null) {
-            // Remove it to prevent stacking multiple fragments after screen rotated.
-            mFragmentManager.beginTransaction().remove(fragment).commit();
-        }
-
-        super.onStop();
+        fragmentTransaction.commit();
     }
 
     @Override
@@ -153,4 +188,22 @@
             return true;
         }
     }
+
+    @Override
+    public boolean onNavigateUp(){
+        Fragment fragment = mFragmentManager.findFragmentById(R.id.fragment_container);
+        if (fragment instanceof WifiDppQrCodeGeneratorFragment) {
+            setResult(Activity.RESULT_CANCELED);
+            finish();
+            return true;
+        } else if (fragment instanceof WifiDppQrCodeScannerFragment) {
+            mFragmentManager.popBackStackImmediate();
+        }
+
+        return false;
+    }
+
+    @Override public void onQrCodeGeneratorFragmentAddButtonClicked() {
+        showQrCodeScannerFragment(/* addToBackStack */ true);
+    }
 }
diff --git a/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivity.java b/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivity.java
new file mode 100644
index 0000000..7c58fd5
--- /dev/null
+++ b/src/com/android/settings/wifi/dpp/WifiDppEnrolleeActivity.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * 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.
+ */
+
+package com.android.settings.wifi.dpp;
+
+import android.app.ActionBar;
+import android.app.Activity;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+
+import androidx.fragment.app.Fragment;
+import androidx.fragment.app.FragmentManager;
+import androidx.fragment.app.FragmentTransaction;
+
+import com.android.internal.logging.nano.MetricsProto;
+
+import com.android.settings.core.InstrumentedActivity;
+import com.android.settings.R;
+
+/**
+ * To provision "this" device with specified Wi-Fi network.
+ *
+ * To use intent action {@code ACTION_ENROLLEE_QR_CODE_SCANNER}, specify the SSID string of the
+ * Wi-Fi network to be provisioned in {@code WifiDppUtils.EXTRA_QR_CODE}.
+ */
+public class WifiDppEnrolleeActivity extends InstrumentedActivity {
+    private static final String TAG = "WifiDppEnrolleeActivity";
+
+    public static final String ACTION_ENROLLEE_QR_CODE_SCANNER =
+            "android.settings.WIFI_DPP_ENROLLEE_QR_CODE_SCANNER";
+
+    private FragmentManager mFragmentManager;
+
+    @Override
+    public int getMetricsCategory() {
+        //TODO:Should we use a new metrics category for Wi-Fi DPP?
+        return MetricsProto.MetricsEvent.WIFI_NETWORK_DETAILS;
+    }
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        setContentView(R.layout.wifi_dpp_activity);
+        mFragmentManager = getSupportFragmentManager();
+
+        handleIntent(getIntent());
+
+        ActionBar actionBar = getActionBar();
+        if (actionBar != null) {
+            actionBar.setElevation(0);
+            actionBar.setDisplayShowTitleEnabled(false);
+        }
+    }
+
+    private void handleIntent(Intent intent) {
+        switch (intent.getAction()) {
+            case ACTION_ENROLLEE_QR_CODE_SCANNER:
+                String ssid = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SSID);
+                showQrCodeScannerFragment(/* addToBackStack */ false, ssid);
+                break;
+            default:
+                Log.e(TAG, "Launch with an invalid action");
+                setResult(Activity.RESULT_CANCELED);
+                finish();
+        }
+    }
+
+    private void showQrCodeScannerFragment(boolean addToBackStack, String ssid) {
+        // Avoid to replace the same fragment during configuration change
+        if (mFragmentManager.findFragmentByTag(WifiDppUtils.TAG_FRAGMENT_QR_CODE_SCANNER) != null) {
+            return;
+        }
+
+        WifiDppQrCodeScannerFragment fragment = new WifiDppQrCodeScannerFragment(ssid);
+        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
+
+        fragmentTransaction.replace(R.id.fragment_container, fragment,
+                WifiDppUtils.TAG_FRAGMENT_QR_CODE_SCANNER);
+        if (addToBackStack) {
+            fragmentTransaction.addToBackStack(/* name */ null);
+        }
+        fragmentTransaction.commit();
+    }
+
+    @Override
+    public boolean onNavigateUp(){
+        setResult(Activity.RESULT_CANCELED);
+        finish();
+        return true;
+    }
+}
diff --git a/src/com/android/settings/wifi/dpp/WifiDppQrCodeBaseFragment.java b/src/com/android/settings/wifi/dpp/WifiDppQrCodeBaseFragment.java
index c86fc98..cddd55c 100644
--- a/src/com/android/settings/wifi/dpp/WifiDppQrCodeBaseFragment.java
+++ b/src/com/android/settings/wifi/dpp/WifiDppQrCodeBaseFragment.java
@@ -34,6 +34,10 @@
 import com.android.settings.R;
 
 /**
+ * TODO: Should refine code to only initiate UI component in each child fragment.
+ */
+
+/**
  * There are below 4 fragments for Wi-Fi DPP UI flow, to reduce redundant code of UI components,
  * this parent fragment instantiates all UI components and provides setting APIs for them.
  *
@@ -58,11 +62,9 @@
     private ImageView mWifiApPictureView;   //optional, for WifiDppAddDeviceFragment
     private TextView mChooseDifferentNetwork;//optional, for WifiDppAddDeviceFragment
 
-    private Button mButtonLeft;             //optional, for WifiDppQrCodeScannerFragment,
-                                            //              WifiDppChooseSavedWifiNetworkFragment,
+    private Button mButtonLeft;             //optional, for WifiDppChooseSavedWifiNetworkFragment,
                                             //              WifiDppAddDeviceFragment
-    private Button mButtonRight;            //optional, for WifiDppQrCodeScannerFragment,
-                                            //              WifiDppChooseSavedWifiNetworkFragment,
+    private Button mButtonRight;            //optional, for WifiDppChooseSavedWifiNetworkFragment,
                                             //              WifiDppAddDeviceFragment
 
     abstract protected int getLayout();
@@ -122,8 +124,7 @@
     }
 
     /**
-     * optional, for WifiDppQrCodeScannerFragment,
-     *               WifiDppChooseSavedWifiNetworkFragment,
+     * optional, for WifiDppChooseSavedWifiNetworkFragment,
      *               WifiDppAddDeviceFragment
      */
     protected void setLeftButtonText(String text) {
@@ -133,8 +134,7 @@
     }
 
     /**
-     * optional, for WifiDppQrCodeScannerFragment,
-     *               WifiDppChooseSavedWifiNetworkFragment,
+     * optional, for WifiDppChooseSavedWifiNetworkFragment,
      *               WifiDppAddDeviceFragment
      */
     protected void setRightButtonText(String text) {
@@ -144,8 +144,7 @@
     }
 
     /**
-     * optional, for WifiDppQrCodeScannerFragment,
-     *               WifiDppChooseSavedWifiNetworkFragment,
+     * optional, for WifiDppChooseSavedWifiNetworkFragment,
      *               WifiDppAddDeviceFragment
      */
     protected void hideLeftButton() {
@@ -155,8 +154,7 @@
     }
 
     /**
-     * optional, for WifiDppQrCodeScannerFragment,
-     *               WifiDppChooseSavedWifiNetworkFragment,
+     * optional, for WifiDppChooseSavedWifiNetworkFragment,
      *               WifiDppAddDeviceFragment
      */
     protected void hideRightButton() {
@@ -166,8 +164,7 @@
     }
 
     /**
-     * optional, for WifiDppQrCodeScannerFragment,
-     *               WifiDppChooseSavedWifiNetworkFragment,
+     * optional, for WifiDppChooseSavedWifiNetworkFragment,
      *               WifiDppAddDeviceFragment
      */
     protected void setLeftButtonOnClickListener(View.OnClickListener listener) {
@@ -177,8 +174,7 @@
     }
 
     /**
-     * optional, for WifiDppQrCodeScannerFragment,
-     *               WifiDppChooseSavedWifiNetworkFragment,
+     * optional, for WifiDppChooseSavedWifiNetworkFragment,
      *               WifiDppAddDeviceFragment
      */
     protected void setRightButtonOnClickListener(View.OnClickListener listener) {
diff --git a/src/com/android/settings/wifi/dpp/WifiDppQrCodeGeneratorFragment.java b/src/com/android/settings/wifi/dpp/WifiDppQrCodeGeneratorFragment.java
index 9935bf2..b064253 100644
--- a/src/com/android/settings/wifi/dpp/WifiDppQrCodeGeneratorFragment.java
+++ b/src/com/android/settings/wifi/dpp/WifiDppQrCodeGeneratorFragment.java
@@ -16,7 +16,12 @@
 
 package com.android.settings.wifi.dpp;
 
+import android.app.ActionBar;
+import android.content.Context;
 import android.os.Bundle;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
 
 import com.android.settings.R;
 
@@ -30,8 +35,64 @@
         return R.layout.wifi_dpp_qrcode_generator_fragment;
     }
 
+    // Container Activity must implement this interface
+    public interface OnQrCodeGeneratorFragmentAddButtonClickedListener {
+        public void onQrCodeGeneratorFragmentAddButtonClicked();
+    }
+    OnQrCodeGeneratorFragmentAddButtonClickedListener mListener;
+
     @Override
-    public void onActivityCreated (Bundle savedInstanceState) {
+    public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
+
+        WifiNetworkConfig wifiNetworkConfig = ((WifiNetworkConfig.Retriever) getActivity())
+                .getWifiNetworkConfig();
+        if (!WifiNetworkConfig.isValidConfig(wifiNetworkConfig)) {
+            throw new IllegalArgumentException("Invalid Wi-Fi network for configuring");
+        }
+        setTitle(getString(R.string.wifi_dpp_share_wifi));
+        setDescription(getString(R.string.wifi_dpp_scan_qr_code_with_another_device,
+                wifiNetworkConfig.getSsid()));
+
+        setHasOptionsMenu(true);
+        ActionBar actionBar = getActivity().getActionBar();
+        if (actionBar != null) {
+            actionBar.setDisplayHomeAsUpEnabled(true);
+            actionBar.show();
+        }
+    }
+
+    @Override
+    public void onAttach(Context context) {
+        super.onAttach(context);
+
+        mListener = (OnQrCodeGeneratorFragmentAddButtonClickedListener) context;
+    }
+
+    @Override
+    public void onDetach() {
+        mListener = null;
+
+        super.onDetach();
+    }
+
+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        MenuItem item = menu.add(0, Menu.FIRST, 0, R.string.next_label);
+        item.setIcon(R.drawable.ic_menu_add);
+        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
+
+        super.onCreateOptionsMenu(menu, inflater);
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem menuItem) {
+        switch (menuItem.getItemId()) {
+            case Menu.FIRST:
+                mListener.onQrCodeGeneratorFragmentAddButtonClicked();
+                return true;
+            default:
+                return super.onOptionsItemSelected(menuItem);
+        }
     }
 }
diff --git a/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java b/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java
index 70dade5..5689c56 100644
--- a/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java
+++ b/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java
@@ -17,12 +17,15 @@
 package com.android.settings.wifi.dpp;
 
 import android.annotation.Nullable;
+import android.app.ActionBar;
 import android.app.Activity;
+import android.content.Context;
 import android.content.Intent;
 import android.graphics.Rect;
 import android.os.Bundle;
-import android.text.TextUtils;
 import android.util.Size;
+import android.view.Menu;
+import android.view.MenuInflater;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;
 import android.view.View;
@@ -38,32 +41,60 @@
     private SurfaceView mSurfaceView;
     private QrDecorateView mDecorateView;
 
+    /** true if the fragment working for configurator, false enrollee*/
+    private final boolean mConfiguratorMode;
+
+    /** The SSID of the Wi-Fi network which the user specify to enroll */
+    private String mSsid;
+
     @Override
     protected int getLayout() {
         return R.layout.wifi_dpp_qrcode_scanner_fragment;
     }
 
+    /**
+     * Configurator container activity of the fragment should create instance with this constructor.
+     */
+    public WifiDppQrCodeScannerFragment() {
+        super();
+
+        mConfiguratorMode = true;
+    }
+
+    /**
+     * Enrollee container activity of the fragment should create instance with this constructor and
+     * specify the SSID string of the WI-Fi network to be provisioned.
+     */
+    public WifiDppQrCodeScannerFragment(String ssid) {
+        super();
+
+        mConfiguratorMode = false;
+        mSsid = ssid;
+    }
+
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
-        setTitle(getString(R.string.wifi_dpp_add_device_to_network));
+        if (mConfiguratorMode) {
+            setTitle(getString(R.string.wifi_dpp_add_device_to_network));
 
-        WifiNetworkConfig wifiNetworkConfig = ((WifiNetworkConfig.Retriever) getActivity())
+            WifiNetworkConfig wifiNetworkConfig = ((WifiNetworkConfig.Retriever) getActivity())
                 .getWifiNetworkConfig();
-        if (!WifiNetworkConfig.isValidConfig(wifiNetworkConfig)) {
-            throw new IllegalArgumentException("Invalid Wi-Fi network for configuring");
+            if (!WifiNetworkConfig.isValidConfig(wifiNetworkConfig)) {
+                throw new IllegalArgumentException("Invalid Wi-Fi network for configuring");
+            }
+            setDescription(getString(R.string.wifi_dpp_center_qr_code, wifiNetworkConfig.getSsid()));
+        } else {
+            setTitle(getString(R.string.wifi_dpp_scan_qr_code));
+            setDescription(getString(R.string.wifi_dpp_scan_qr_code_join_network, mSsid));
         }
-        setDescription(getString(R.string.wifi_dpp_center_qr_code, wifiNetworkConfig.getSsid()));
 
-        hideRightButton();
-
-        setLeftButtonText(getString(android.R.string.cancel));
-
-        setLeftButtonOnClickListener((view) -> {
-            getActivity().setResult(Activity.RESULT_CANCELED);
-            getActivity().finish();
-        });
+        ActionBar actionBar = getActivity().getActionBar();
+        if (actionBar != null) {
+            actionBar.setDisplayHomeAsUpEnabled(true);
+            actionBar.show();
+        }
     }
 
     @Override
@@ -79,6 +110,21 @@
     }
 
     @Override
+    public void onDestroyView() {
+        SurfaceHolder surfaceHolder = mSurfaceView.getHolder();
+        surfaceHolder.removeCallback(this);
+
+        super.onDestroyView();
+    }
+
+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        menu.removeItem(Menu.FIRST);
+
+        super.onCreateOptionsMenu(menu, inflater);
+    }
+
+    @Override
     public void surfaceCreated(final SurfaceHolder holder) {
         initCamera(holder);
     }
diff --git a/src/com/android/settings/wifi/dpp/WifiDppUtils.java b/src/com/android/settings/wifi/dpp/WifiDppUtils.java
index 70ef3a8..8d40171 100644
--- a/src/com/android/settings/wifi/dpp/WifiDppUtils.java
+++ b/src/com/android/settings/wifi/dpp/WifiDppUtils.java
@@ -22,6 +22,27 @@
  * Here are the items shared by both WifiDppConfiguratorActivity & WifiDppEnrolleeActivity
  */
 public class WifiDppUtils {
+    /**
+     * The fragment tag specified to FragmentManager for container activities to manage fragments.
+     */
+    public static final String TAG_FRAGMENT_QR_CODE_SCANNER = "qr_code_scanner_fragment";
+
+    /**
+     * @see #TAG_FRAGMENT_QR_CODE_SCANNER
+     */
+    public static final String TAG_FRAGMENT_QR_CODE_GENERATOR = "qr_code_generator_fragment";
+
+    /**
+     * @see #TAG_FRAGMENT_QR_CODE_SCANNER
+     */
+    public static final String TAG_FRAGMENT_CHOOSE_SAVED_WIFI_NETWORK =
+            "choose_saved_wifi_network_fragment";
+
+    /**
+     * @see #TAG_FRAGMENT_QR_CODE_SCANNER
+     */
+    public static final String TAG_FRAGMENT_ADD_DEVICE = "add_device_fragment";
+
     /** The data is from {@code com.android.settingslib.wifi.AccessPoint.securityToString} */
     public static final String EXTRA_WIFI_SECURITY = "security";
 
diff --git a/tests/unit/src/com/android/settings/wifi/dpp/WifiDppConfiguratorActivityTest.java b/tests/unit/src/com/android/settings/wifi/dpp/WifiDppConfiguratorActivityTest.java
index 70e9587..2f95fa5 100644
--- a/tests/unit/src/com/android/settings/wifi/dpp/WifiDppConfiguratorActivityTest.java
+++ b/tests/unit/src/com/android/settings/wifi/dpp/WifiDppConfiguratorActivityTest.java
@@ -73,4 +73,12 @@
 
         assertThat(activity instanceof WifiNetworkConfig.Retriever).isEqualTo(true);
     }
+
+    @Test
+    public void testActivity_shouldImplementsQrCodeGeneratorFragmentCallback() {
+        WifiDppConfiguratorActivity activity = mActivityRule.getActivity();
+
+        assertThat(activity instanceof WifiDppQrCodeGeneratorFragment
+                .OnQrCodeGeneratorFragmentAddButtonClickedListener).isEqualTo(true);
+    }
 }
diff --git a/tests/unit/src/com/android/settings/wifi/dpp/WifiDppQrCodeGeneratorFragmentTest.java b/tests/unit/src/com/android/settings/wifi/dpp/WifiDppQrCodeGeneratorFragmentTest.java
new file mode 100644
index 0000000..d3f667f
--- /dev/null
+++ b/tests/unit/src/com/android/settings/wifi/dpp/WifiDppQrCodeGeneratorFragmentTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * 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.
+ */
+
+package com.android.settings.wifi.dpp;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
+
+import com.android.settings.R;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+public class WifiDppQrCodeGeneratorFragmentTest {
+    @Rule
+    public final ActivityTestRule<WifiDppConfiguratorActivity> mActivityRule =
+            new ActivityTestRule<>(WifiDppConfiguratorActivity.class, true);
+
+    @Before
+    public void setUp() {
+        Intent intent =
+                new Intent(WifiDppConfiguratorActivity.ACTION_CONFIGURATOR_QR_CODE_GENERATOR);
+        intent.putExtra(WifiDppUtils.EXTRA_WIFI_SECURITY, "WEP");
+        intent.putExtra(WifiDppUtils.EXTRA_WIFI_SSID, "GoogleGuest");
+        mActivityRule.launchActivity(intent);
+    }
+
+    @Test
+    public void rotateScreen_shouldNotCrash() {
+        mActivityRule.getActivity().setRequestedOrientation(
+                ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
+        mActivityRule.getActivity().setRequestedOrientation(
+                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
+    }
+}
diff --git a/tests/unit/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragmentTest.java b/tests/unit/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragmentTest.java
index 98742ed..70cfc2d 100644
--- a/tests/unit/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragmentTest.java
+++ b/tests/unit/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragmentTest.java
@@ -16,12 +16,6 @@
 
 package com.android.settings.wifi.dpp;
 
-import static android.support.test.espresso.action.ViewActions.click;
-import static android.support.test.espresso.matcher.ViewMatchers.withText;
-import static android.support.test.espresso.Espresso.onView;
-
-import static com.google.common.truth.Truth.assertThat;
-
 import android.app.Activity;
 import android.content.Intent;
 import android.content.pm.ActivityInfo;