Fix QR code scanner layout in "Pair devices over Wi-Fi" page.

The toolbar needs to be disabled to remove the empty space at the top
of the page. Also added the landscape layout xml.

Bug: 193602141
Test: atest SettingsUnitTests and manual

Change-Id: I30368835979b360a8f09e16f70adf57064784517
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 426aebf..2b4bcae 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2196,6 +2196,8 @@
         <activity android:name=".development.AppPicker"
                   android:label="@string/select_application" />
 
+        <activity android:name=".development.AdbQrCodeActivity" />
+
         <activity android:name=".development.DSULoader"
                   android:label="Select DSU Package"
                   android:theme="@android:style/Theme.DeviceDefault.Light.Dialog" />
diff --git a/res/layout-land/adb_qrcode_scanner_fragment.xml b/res/layout-land/adb_qrcode_scanner_fragment.xml
new file mode 100644
index 0000000..6451551
--- /dev/null
+++ b/res/layout-land/adb_qrcode_scanner_fragment.xml
@@ -0,0 +1,97 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     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.
+-->
+
+<com.google.android.setupdesign.GlifLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:icon="@drawable/ic_scan_32dp"
+    app:sudDescriptionText="@string/adb_wireless_qrcode_pairing_description">
+
+    <LinearLayout
+        style="@style/SudContentFrame"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:gravity="center_horizontal"
+        android:orientation="vertical">
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:gravity="center"
+            android:orientation="vertical"
+            android:id="@+id/camera_layout">
+
+            <FrameLayout
+                android:layout_width="@dimen/qrcode_preview_size"
+                android:layout_height="@dimen/qrcode_preview_size"
+                android:clipChildren="true">
+                <TextureView
+                    android:id="@+id/preview_view"
+                    android:layout_width="wrap_content"
+                    android:layout_height="match_parent"/>
+                <com.android.settings.wifi.qrcode.QrDecorateView
+                    android:id="@+id/decorate_view"
+                    android:layout_width="wrap_content"
+                    android:layout_height="match_parent"/>
+            </FrameLayout>
+
+            <TextView
+                android:id="@+id/error_message"
+                style="@style/TextAppearance.ErrorText"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="16dp"
+                android:layout_marginStart="?attr/sudMarginStart"
+                android:layout_marginEnd="?attr/sudMarginEnd"
+                android:textAlignment="center"
+                android:visibility="invisible"/>
+
+        </LinearLayout>
+
+        <!--
+             The spinner indicating that the device is waiting for pairing
+             after getting valid QR code
+        -->
+        <LinearLayout
+            android:id="@+id/verifying_layout"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:gravity="center"
+            android:orientation="vertical"
+            android:visibility="gone">
+
+            <ProgressBar
+                android:id="@+id/verifying_progress"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"/>
+
+            <TextView
+                android:id="@+id/verifying_textview"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                style="@style/adb_wireless_item_progress_text"
+                android:text="@string/adb_wireless_verifying_qrcode_text"
+                android:accessibilityLiveRegion="polite"/>
+
+        </LinearLayout>
+
+    </LinearLayout>
+
+</com.google.android.setupdesign.GlifLayout>
+
diff --git a/res/layout/adb_qrcode_scanner_fragment.xml b/res/layout/adb_qrcode_scanner_fragment.xml
index 5b83cb2..7823155 100644
--- a/res/layout/adb_qrcode_scanner_fragment.xml
+++ b/res/layout/adb_qrcode_scanner_fragment.xml
@@ -17,10 +17,11 @@
 
 <com.google.android.setupdesign.GlifLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:theme="@style/GlifV3Theme"
-    android:icon="@drawable/ic_scan_32dp">
+    android:icon="@drawable/ic_scan_32dp"
+    app:sudDescriptionText="@string/adb_wireless_qrcode_pairing_description">
 
     <LinearLayout
         style="@style/SudContentFrame"
@@ -32,52 +33,34 @@
         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
+            android:gravity="center"
             android:orientation="vertical"
-            android:gravity="center_horizontal"
             android:id="@+id/camera_layout">
 
-            <TextView
-                android:id="@android:id/summary"
-                style="@style/TextAppearance.SudGlifBody"
+            <FrameLayout
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
+                android:clipChildren="true">
+                <TextureView
+                    android:id="@+id/preview_view"
+                    android:layout_width="match_parent"
+                    android:layout_height="@dimen/qrcode_preview_size"/>
+                <com.android.settings.wifi.qrcode.QrDecorateView
+                    android:id="@+id/decorate_view"
+                    android:layout_width="match_parent"
+                    android:layout_height="@dimen/qrcode_preview_size"/>
+            </FrameLayout>
+
+            <TextView
+                android:id="@+id/error_message"
+                style="@style/TextAppearance.ErrorText"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="16dp"
                 android:layout_marginStart="?attr/sudMarginStart"
                 android:layout_marginEnd="?attr/sudMarginEnd"
                 android:textAlignment="center"
-                android:accessibilityLiveRegion="polite"/>
-
-            <LinearLayout
-                android:layout_width="match_parent"
-                android:layout_height="match_parent"
-                android:gravity="center"
-                android:orientation="vertical">
-
-                <FrameLayout
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    android:clipChildren="true">
-                    <TextureView
-                        android:id="@+id/preview_view"
-                        android:layout_width="match_parent"
-                        android:layout_height="@dimen/qrcode_preview_size"/>
-                    <com.android.settings.wifi.qrcode.QrDecorateView
-                        android:id="@+id/decorate_view"
-                        android:layout_width="match_parent"
-                        android:layout_height="@dimen/qrcode_preview_size"/>
-                </FrameLayout>
-
-                <TextView
-                    android:id="@+id/error_message"
-                    style="@style/TextAppearance.ErrorText"
-                    android:layout_width="wrap_content"
-                    android:layout_height="wrap_content"
-                    android:layout_marginTop="16dp"
-                    android:layout_marginStart="?attr/sudMarginStart"
-                    android:layout_marginEnd="?attr/sudMarginEnd"
-                    android:textAlignment="center"
-                    android:visibility="invisible"/>
-
-            </LinearLayout>
+                android:visibility="invisible"/>
 
         </LinearLayout>
 
@@ -109,4 +92,5 @@
         </LinearLayout>
 
     </LinearLayout>
+
 </com.google.android.setupdesign.GlifLayout>
diff --git a/src/com/android/settings/development/AdbQrCodeActivity.java b/src/com/android/settings/development/AdbQrCodeActivity.java
new file mode 100644
index 0000000..bab20f9
--- /dev/null
+++ b/src/com/android/settings/development/AdbQrCodeActivity.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2021 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.development;
+
+import android.app.settings.SettingsEnums;
+import android.content.Intent;
+import android.os.Bundle;
+
+import androidx.fragment.app.FragmentTransaction;
+
+import com.android.settings.R;
+import com.android.settings.wifi.dpp.WifiDppBaseActivity;
+
+/**
+ * To scan an ADB QR code to pair a device.
+ *
+ * To use intent action {@code ACTION_ADB_QR_CODE_SCANNER}.
+ */
+public class AdbQrCodeActivity extends WifiDppBaseActivity {
+    private static final String TAG = "AdbQrCodeActivity";
+
+    static final String TAG_FRAGMENT_ADB_QR_CODE_SCANNER = "adb_qr_code_scanner_fragment";
+
+    public static final String ACTION_ADB_QR_CODE_SCANNER =
+            "android.settings.ADB_QR_CODE_SCANNER";
+
+    @Override
+    public int getMetricsCategory() {
+        return SettingsEnums.SETTINGS_ADB_WIRELESS;
+    }
+
+    @Override
+    protected void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+        AdbQrcodeScannerFragment fragment =
+                (AdbQrcodeScannerFragment) mFragmentManager.findFragmentByTag(
+                        TAG_FRAGMENT_ADB_QR_CODE_SCANNER);
+
+        if (fragment == null) {
+            fragment = new AdbQrcodeScannerFragment();
+        } else {
+            if (fragment.isVisible()) {
+                return;
+            }
+
+            // When the fragment in back stack but not on top of the stack, we can simply pop
+            // stack because current fragment transactions are arranged in an order
+            mFragmentManager.popBackStackImmediate();
+            return;
+        }
+        final FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
+
+        fragmentTransaction.replace(R.id.fragment_container, fragment,
+                TAG_FRAGMENT_ADB_QR_CODE_SCANNER);
+        fragmentTransaction.commit();
+    }
+
+    @Override
+    protected void handleIntent(Intent intent) {
+    }
+}
diff --git a/src/com/android/settings/development/AdbQrCodePreferenceController.java b/src/com/android/settings/development/AdbQrCodePreferenceController.java
index 7c1ae12..9085d1b 100644
--- a/src/com/android/settings/development/AdbQrCodePreferenceController.java
+++ b/src/com/android/settings/development/AdbQrCodePreferenceController.java
@@ -16,8 +16,8 @@
 
 package com.android.settings.development;
 
-import android.app.settings.SettingsEnums;
 import android.content.Context;
+import android.content.Intent;
 import android.debug.IAdbManager;
 import android.os.RemoteException;
 import android.os.ServiceManager;
@@ -28,7 +28,6 @@
 import androidx.preference.Preference;
 
 import com.android.settings.core.BasePreferenceController;
-import com.android.settings.core.SubSettingLauncher;
 
 /**
  * Controller for the "Pair device with QR code" preference in the Wireless debugging
@@ -68,12 +67,9 @@
             return false;
         }
 
-        new SubSettingLauncher(preference.getContext())
-                .setDestination(AdbQrcodeScannerFragment.class.getName())
-                .setSourceMetricsCategory(SettingsEnums.SETTINGS_ADB_WIRELESS)
-                .setResultListener(mParentFragment,
-                    WirelessDebuggingFragment.PAIRING_DEVICE_REQUEST)
-                .launch();
+        final Intent intent = new Intent(mContext, AdbQrCodeActivity.class);
+        mParentFragment.startActivityForResult(intent,
+                WirelessDebuggingFragment.PAIRING_DEVICE_REQUEST);
         return true;
     }
 }
diff --git a/src/com/android/settings/development/AdbQrcodeScannerFragment.java b/src/com/android/settings/development/AdbQrcodeScannerFragment.java
index 7a30443..1035fbe 100644
--- a/src/com/android/settings/development/AdbQrcodeScannerFragment.java
+++ b/src/com/android/settings/development/AdbQrcodeScannerFragment.java
@@ -44,12 +44,15 @@
 import androidx.annotation.StringRes;
 
 import com.android.settings.R;
+import com.android.settings.SetupWizardUtils;
 import com.android.settings.wifi.dpp.AdbQrCode;
 import com.android.settings.wifi.dpp.WifiDppQrCodeBaseFragment;
 import com.android.settings.wifi.dpp.WifiNetworkConfig;
 import com.android.settings.wifi.qrcode.QrCamera;
 import com.android.settings.wifi.qrcode.QrDecorateView;
 
+import com.google.android.setupdesign.util.ThemeHelper;
+
 /**
  * Fragment shown when clicking on the "Pair by QR code" preference in
  * the Wireless Debugging fragment.
@@ -144,6 +147,9 @@
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
+        Context context = getContext();
+        context.setTheme(SetupWizardUtils.getTheme(context, getActivity().getIntent()));
+        ThemeHelper.trySetDynamicColor(getContext());
         super.onCreate(savedInstanceState);
 
         mIntentFilter = new IntentFilter(AdbManager.WIRELESS_DEBUG_PAIRING_RESULT_ACTION);
@@ -158,6 +164,7 @@
     @Override
     public void onViewCreated(View view, Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
+        mSummary = view.findViewById(R.id.sud_layout_subtitle);
 
         mTextureView = (TextureView) view.findViewById(R.id.preview_view);
         mTextureView.setSurfaceTextureListener(this);
@@ -165,8 +172,6 @@
         mDecorateView = view.findViewById(R.id.decorate_view);
         setProgressBarShown(false);
 
-        setHeaderIconImageResource(R.drawable.ic_scan_24dp);
-
         mQrCameraView = view.findViewById(R.id.camera_layout);
         mVerifyingView = view.findViewById(R.id.verifying_layout);
         mVerifyingTextView = view.findViewById(R.id.verifying_textview);
@@ -181,12 +186,18 @@
     public void onResume() {
         super.onResume();
 
+        restartCamera();
+
         mAdbManager = IAdbManager.Stub.asInterface(ServiceManager.getService(Context.ADB_SERVICE));
         getActivity().registerReceiver(mReceiver, mIntentFilter);
     }
 
     @Override
     public void onPause() {
+        if (mCamera != null) {
+            mCamera.stop();
+        }
+
         super.onPause();
 
         getActivity().unregisterReceiver(mReceiver);
@@ -195,8 +206,6 @@
         } catch (RemoteException e) {
             Log.e(TAG, "Unable to cancel pairing");
         }
-        getActivity().setResult(Activity.RESULT_CANCELED);
-        getActivity().finish();
     }
 
     @Override
@@ -213,7 +222,6 @@
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
-        getActivity().getActionBar().hide();
         // setTitle for TalkBack
         getActivity().setTitle(R.string.wifi_dpp_scan_qr_code);
     }
diff --git a/src/com/android/settings/wifi/dpp/WifiDppBaseActivity.java b/src/com/android/settings/wifi/dpp/WifiDppBaseActivity.java
index 499f66c..f55960b 100644
--- a/src/com/android/settings/wifi/dpp/WifiDppBaseActivity.java
+++ b/src/com/android/settings/wifi/dpp/WifiDppBaseActivity.java
@@ -26,7 +26,7 @@
 import com.android.settings.SetupWizardUtils;
 import com.android.settings.core.InstrumentedActivity;
 
-abstract class WifiDppBaseActivity extends InstrumentedActivity {
+public abstract class WifiDppBaseActivity extends InstrumentedActivity {
     protected FragmentManager mFragmentManager;
 
     protected abstract void handleIntent(Intent intent);