Move AdvancedWifiSettings into ConfigureWifiSettings.

Bug:34719854
Test: make -j40 RunSettingsRoboTests
clicked on preferences and made sure functionality did not change.

Change-Id: Ia5f0c7f5ea0f88d1322008ab58ca79028cc1a2bb
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index a854eef..e754d6a 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -299,27 +299,6 @@
                 android:value="true" />
         </activity>
 
-        <activity android:name="Settings$AdvancedWifiSettingsActivity"
-                android:taskAffinity="com.android.settings"
-                android:label="@string/wifi_advanced_settings_label"
-                android:icon="@drawable/ic_settings_wireless"
-                android:configChanges="orientation|keyboardHidden|screenSize"
-                android:parentActivityName="Settings$WifiSettingsActivity">
-            <intent-filter android:priority="1">
-                <action android:name="android.settings.WIFI_IP_SETTINGS" />
-                <category android:name="android.intent.category.DEFAULT" />
-            </intent-filter>
-            <intent-filter>
-                <action android:name="android.intent.action.MAIN" />
-                <category android:name="android.intent.category.VOICE_LAUNCH" />
-                <category android:name="android.intent.category.DEFAULT" />
-            </intent-filter>
-            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
-                android:value="com.android.settings.wifi.AdvancedWifiSettings" />
-            <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
-                android:value="true" />
-        </activity>
-
         <activity android:name="Settings$SavedAccessPointsSettingsActivity"
                 android:taskAffinity="com.android.settings"
                 android:label="@string/wifi_saved_access_points_label"
diff --git a/res/xml/wifi_advanced_settings.xml b/res/xml/wifi_advanced_settings.xml
deleted file mode 100644
index 8d9a6cd..0000000
--- a/res/xml/wifi_advanced_settings.xml
+++ /dev/null
@@ -1,40 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2008 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.
--->
-
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-        android:title="@string/wifi_advanced_titlebar">
-
-    <Preference
-            android:key="install_credentials"
-            android:title="@string/wifi_install_credentials"
-            android:persistent="false" />
-
-    <Preference
-            android:key="wifi_direct"
-            android:title="@string/wifi_menu_p2p"
-            android:persistent="false" />
-
-    <Preference
-            android:key="wps_push_button"
-            android:title="@string/wifi_menu_wps_pbc"
-            android:persistent="false" />
-
-    <Preference
-            android:key="wps_pin_entry"
-            android:title="@string/wifi_menu_wps_pin"
-            android:persistent="false" />
-
-</PreferenceScreen>
diff --git a/res/xml/wifi_configure_settings.xml b/res/xml/wifi_configure_settings.xml
index 1e04749..66c9140 100644
--- a/res/xml/wifi_configure_settings.xml
+++ b/res/xml/wifi_configure_settings.xml
@@ -52,4 +52,29 @@
         android:key="current_ip_address"
         android:title="@string/wifi_advanced_ip_address_title"/>
 
+    <Preference
+            android:key="install_credentials"
+            android:title="@string/wifi_install_credentials">
+        <intent android:action="android.credentials.INSTALL_AS_USER"
+                android:targetPackage="com.android.certinstaller"
+                android:targetClass="com.android.certinstaller.CertInstallerMain">
+            <extra android:name="install_as_uid" android:value="1010"/>
+        </intent>
+    </Preference>
+
+    <Preference
+            android:key="wifi_direct"
+            android:title="@string/wifi_menu_p2p">
+        <intent android:targetPackage="com.android.settings"
+                android:targetClass="com.android.settings.Settings$WifiP2pSettingsActivity"/>
+    </Preference>
+
+    <Preference
+            android:key="wps_push_button"
+            android:title="@string/wifi_menu_wps_pbc" />
+
+    <Preference
+            android:key="wps_pin_entry"
+            android:title="@string/wifi_menu_wps_pin" />
+
 </PreferenceScreen>
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index 360d258..dfa291c 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -95,7 +95,6 @@
     public static class DeviceAdminSettingsActivity extends SettingsActivity { /* empty */ }
     public static class DataUsageSummaryActivity extends SettingsActivity { /* empty */ }
     public static class ConfigureWifiSettingsActivity extends SettingsActivity { /* empty */ }
-    public static class AdvancedWifiSettingsActivity extends SettingsActivity { /* empty */ }
     public static class SavedAccessPointsSettingsActivity extends SettingsActivity { /* empty */ }
     public static class TextToSpeechSettingsActivity extends SettingsActivity { /* empty */ }
     public static class AndroidBeamSettingsActivity extends SettingsActivity { /* empty */ }
diff --git a/src/com/android/settings/core/gateway/SettingsGateway.java b/src/com/android/settings/core/gateway/SettingsGateway.java
index 1dd83cb..2f0dff3 100644
--- a/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -122,7 +122,6 @@
 import com.android.settings.users.UserSettings;
 import com.android.settings.vpn2.VpnSettings;
 import com.android.settings.wfd.WifiDisplaySettings;
-import com.android.settings.wifi.AdvancedWifiSettings;
 import com.android.settings.wifi.ConfigureWifiSettings;
 import com.android.settings.wifi.SavedAccessPointsWifiSettings;
 import com.android.settings.wifi.WifiAPITest;
@@ -140,7 +139,6 @@
             WirelessSettings.class.getName(),
             WifiSettings.class.getName(),
             ConfigureWifiSettings.class.getName(),
-            AdvancedWifiSettings.class.getName(),
             SavedAccessPointsWifiSettings.class.getName(),
             BluetoothSettings.class.getName(),
             SimSettings.class.getName(),
diff --git a/src/com/android/settings/search/Ranking.java b/src/com/android/settings/search/Ranking.java
index 95cdd94..4f660bf 100644
--- a/src/com/android/settings/search/Ranking.java
+++ b/src/com/android/settings/search/Ranking.java
@@ -56,7 +56,6 @@
 import com.android.settings.sim.SimSettings;
 import com.android.settings.system.SystemDashboardFragment;
 import com.android.settings.users.UserSettings;
-import com.android.settings.wifi.AdvancedWifiSettings;
 import com.android.settings.wifi.ConfigureWifiSettings;
 import com.android.settings.wifi.SavedAccessPointsWifiSettings;
 import com.android.settings.wifi.WifiSettings;
@@ -106,7 +105,6 @@
         // Wi-Fi
         sRankMap.put(WifiSettings.class.getName(), RANK_WIFI);
         sRankMap.put(ConfigureWifiSettings.class.getName(), RANK_WIFI);
-        sRankMap.put(AdvancedWifiSettings.class.getName(), RANK_WIFI);
         sRankMap.put(SavedAccessPointsWifiSettings.class.getName(), RANK_WIFI);
 
         // BT
diff --git a/src/com/android/settings/search/SearchIndexableResources.java b/src/com/android/settings/search/SearchIndexableResources.java
index 4a73469..cbbc0b5 100644
--- a/src/com/android/settings/search/SearchIndexableResources.java
+++ b/src/com/android/settings/search/SearchIndexableResources.java
@@ -76,7 +76,6 @@
 import com.android.settings.tts.TtsEnginePreferenceFragment;
 import com.android.settings.tts.TtsSlidersFragment;
 import com.android.settings.users.UserSettings;
-import com.android.settings.wifi.AdvancedWifiSettings;
 import com.android.settings.wifi.ConfigureWifiSettings;
 import com.android.settings.wifi.SavedAccessPointsWifiSettings;
 import com.android.settings.wifi.WifiSettings;
@@ -103,7 +102,6 @@
     static {
         addIndex(WifiSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_wireless);
         addIndex(NetworkDashboardFragment.class, NO_DATA_RES_ID, R.drawable.ic_settings_wireless);
-        addIndex(AdvancedWifiSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_wireless);
         addIndex(ConfigureWifiSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_wireless);
         addIndex(SavedAccessPointsWifiSettings.class, NO_DATA_RES_ID,
                 R.drawable.ic_settings_wireless);
diff --git a/src/com/android/settings/wifi/AdvancedWifiSettings.java b/src/com/android/settings/wifi/AdvancedWifiSettings.java
deleted file mode 100644
index 07c7ce3..0000000
--- a/src/com/android/settings/wifi/AdvancedWifiSettings.java
+++ /dev/null
@@ -1,184 +0,0 @@
-/*
- * Copyright (C) 2011 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;
-
-import android.app.Dialog;
-import android.app.DialogFragment;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.net.wifi.WifiManager;
-import android.net.wifi.WpsInfo;
-import android.os.Bundle;
-import android.os.UserManager;
-import android.provider.SearchIndexableResource;
-import android.security.Credentials;
-import android.support.v7.preference.Preference;
-import android.support.v7.preference.Preference.OnPreferenceClickListener;
-import android.support.v7.preference.PreferenceScreen;
-
-import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
-import com.android.settings.R;
-import com.android.settings.RestrictedSettingsFragment;
-import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
-import com.android.settings.search.BaseSearchIndexProvider;
-import com.android.settings.search.Indexable;
-
-import java.util.Arrays;
-import java.util.List;
-
-public class AdvancedWifiSettings extends RestrictedSettingsFragment {
-    private static final String TAG = "AdvancedWifiSettings";
-
-    private static final String KEY_INSTALL_CREDENTIALS = "install_credentials";
-    private static final String KEY_WIFI_DIRECT = "wifi_direct";
-    private static final String KEY_WPS_PUSH = "wps_push_button";
-    private static final String KEY_WPS_PIN = "wps_pin_entry";
-
-    private boolean mUnavailable;
-
-    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            initPreferences();
-        }
-    };
-
-    public AdvancedWifiSettings() {
-        super(UserManager.DISALLOW_CONFIG_WIFI);
-    }
-
-    @Override
-    public int getMetricsCategory() {
-        return MetricsEvent.WIFI_ADVANCED;
-    }
-
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        if (isUiRestricted()) {
-            mUnavailable = true;
-            setPreferenceScreen(new PreferenceScreen(getPrefContext(), null));
-        } else {
-            addPreferencesFromResource(R.xml.wifi_advanced_settings);
-        }
-    }
-
-    @Override
-    public void onActivityCreated(Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
-        getEmptyTextView().setText(R.string.wifi_advanced_not_available);
-        if (mUnavailable) {
-            getPreferenceScreen().removeAll();
-        }
-    }
-
-    @Override
-    public void onStart() {
-        super.onStart();
-        if (!mUnavailable) {
-            getActivity().registerReceiver(mReceiver,
-                    new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
-            initPreferences();
-        }
-    }
-
-    @Override
-    public void onStop() {
-        super.onStop();
-        if (!mUnavailable) {
-            getActivity().unregisterReceiver(mReceiver);
-        }
-    }
-
-    private void initPreferences() {
-        final Context context = getActivity();
-        Intent intent = new Intent(Credentials.INSTALL_AS_USER_ACTION);
-        intent.setClassName("com.android.certinstaller",
-                "com.android.certinstaller.CertInstallerMain");
-        intent.putExtra(Credentials.EXTRA_INSTALL_AS_UID, android.os.Process.WIFI_UID);
-        Preference pref = findPreference(KEY_INSTALL_CREDENTIALS);
-        pref.setIntent(intent);
-
-        final WifiManager wifiManager =
-                (WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
-        Intent wifiDirectIntent = new Intent(context,
-                com.android.settings.Settings.WifiP2pSettingsActivity.class);
-        Preference wifiDirectPref = findPreference(KEY_WIFI_DIRECT);
-        wifiDirectPref.setIntent(wifiDirectIntent);
-        wifiDirectPref.setEnabled(wifiManager.isWifiEnabled());
-
-        // WpsDialog: Create the dialog like WifiSettings does.
-        Preference wpsPushPref = findPreference(KEY_WPS_PUSH);
-        wpsPushPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
-                public boolean onPreferenceClick(Preference arg0) {
-                    WpsFragment wpsFragment = new WpsFragment(WpsInfo.PBC);
-                    wpsFragment.show(getFragmentManager(), KEY_WPS_PUSH);
-                    return true;
-                }
-        });
-        wpsPushPref.setEnabled(wifiManager.isWifiEnabled());
-
-        // WpsDialog: Create the dialog like WifiSettings does.
-        Preference wpsPinPref = findPreference(KEY_WPS_PIN);
-        wpsPinPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
-                public boolean onPreferenceClick(Preference arg0) {
-                    WpsFragment wpsFragment = new WpsFragment(WpsInfo.DISPLAY);
-                    wpsFragment.show(getFragmentManager(), KEY_WPS_PIN);
-                    return true;
-                }
-        });
-        wpsPinPref.setEnabled(wifiManager.isWifiEnabled());
-    }
-
-    /* Wrapper class for the WPS dialog to properly handle life cycle events like rotation. */
-    public static class WpsFragment extends InstrumentedDialogFragment {
-        private static int mWpsSetup;
-
-        // Public default constructor is required for rotation.
-        public WpsFragment() {
-            super();
-        }
-
-        public WpsFragment(int wpsSetup) {
-            super();
-            mWpsSetup = wpsSetup;
-        }
-
-        @Override
-        public int getMetricsCategory() {
-            return MetricsEvent.DIALOG_WPS_SETUP;
-        }
-
-        @Override
-        public Dialog onCreateDialog(Bundle savedInstanceState) {
-            return new WpsDialog(getActivity(), mWpsSetup);
-        }
-    }
-
-    public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
-            new BaseSearchIndexProvider() {
-                @Override
-                public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
-                        boolean enabled) {
-                    final SearchIndexableResource sir = new SearchIndexableResource(context);
-                    sir.xmlResId = R.xml.wifi_advanced_settings;
-                    return Arrays.asList(sir);
-                }
-            };
-}
diff --git a/src/com/android/settings/wifi/ConfigureWifiSettings.java b/src/com/android/settings/wifi/ConfigureWifiSettings.java
index 682662a..5fd3eeb 100644
--- a/src/com/android/settings/wifi/ConfigureWifiSettings.java
+++ b/src/com/android/settings/wifi/ConfigureWifiSettings.java
@@ -15,6 +15,8 @@
  */
 package com.android.settings.wifi;
 
+import static android.content.Context.WIFI_SERVICE;
+
 import android.content.Context;
 import android.net.wifi.WifiManager;
 import android.provider.SearchIndexableResource;
@@ -25,13 +27,12 @@
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
+import com.android.settings.wifi.p2p.WifiP2pPreferenceController;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
-import static android.content.Context.WIFI_SERVICE;
-
 public class ConfigureWifiSettings extends DashboardFragment {
 
     private static final String TAG = "ConfigureWifiSettings";
@@ -63,6 +64,9 @@
         controllers.add(new NotifyOpenNetworksPreferenceController(context, getLifecycle()));
         controllers.add(new WifiWakeupPreferenceController(context, getLifecycle()));
         controllers.add(new WifiSleepPolicyPreferenceController(context));
+        controllers.add(new WifiP2pPreferenceController(context, getLifecycle(), mWifiManager));
+        controllers.add(new WpsPreferenceController(
+                context, getLifecycle(), mWifiManager, getFragmentManager()));
         return controllers;
     }
 
diff --git a/src/com/android/settings/wifi/WifiPickerActivity.java b/src/com/android/settings/wifi/WifiPickerActivity.java
index 3f7a5e7..5d5afd3 100644
--- a/src/com/android/settings/wifi/WifiPickerActivity.java
+++ b/src/com/android/settings/wifi/WifiPickerActivity.java
@@ -39,8 +39,9 @@
     protected boolean isValidFragment(String fragmentName) {
         if (WifiSettings.class.getName().equals(fragmentName)
                 || WifiP2pSettings.class.getName().equals(fragmentName)
-                || SavedAccessPointsWifiSettings.class.getName().equals(fragmentName)
-                || AdvancedWifiSettings.class.getName().equals(fragmentName)) return true;
+                || SavedAccessPointsWifiSettings.class.getName().equals(fragmentName)) {
+            return true;
+        }
         return false;
     }
 
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index fd31761..4357569 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -93,7 +93,6 @@
 
     /* package */ static final int MENU_ID_WPS_PBC = Menu.FIRST;
     private static final int MENU_ID_WPS_PIN = Menu.FIRST + 1;
-    private static final int MENU_ID_ADVANCED = Menu.FIRST + 4;
     private static final int MENU_ID_SCAN = Menu.FIRST + 5;
     private static final int MENU_ID_CONNECT = Menu.FIRST + 6;
     private static final int MENU_ID_FORGET = Menu.FIRST + 7;
@@ -113,6 +112,7 @@
     private static final String PREF_KEY_EMPTY_WIFI_LIST = "wifi_empty_list";
     private static final String PREF_KEY_ACCESS_POINTS = "access_points";
     private static final String PREF_KEY_ADDITIONAL_SETTINGS = "additional_settings";
+    private static final String PREF_KEY_CONFIGURE_WIFI_SETTINGS = "configure_settings";
     private static final String PREF_KEY_SAVED_NETWORKS = "saved_networks";
 
     protected WifiManager mWifiManager;
@@ -155,6 +155,7 @@
     private PreferenceCategory mAccessPointsPreferenceCategory;
     private PreferenceCategory mAdditionalSettingsPreferenceCategory;
     private Preference mAddPreference;
+    private Preference mConfigureWifiSettingsPreference;
     private Preference mSavedNetworksPreference;
     private LinkablePreference mStatusMessagePreference;
 
@@ -186,6 +187,7 @@
                 (PreferenceCategory) findPreference(PREF_KEY_ACCESS_POINTS);
         mAdditionalSettingsPreferenceCategory =
                 (PreferenceCategory) findPreference(PREF_KEY_ADDITIONAL_SETTINGS);
+        mConfigureWifiSettingsPreference = findPreference(PREF_KEY_CONFIGURE_WIFI_SETTINGS);
         mSavedNetworksPreference = findPreference(PREF_KEY_SAVED_NETWORKS);
 
         Context prefContext = getPrefContext();
@@ -359,9 +361,6 @@
      * @param menu
      */
     void addOptionsMenuItems(Menu menu) {
-        menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced)
-                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
-
         final boolean wifiIsEnabled = mWifiTracker.isWifiEnabled();
         mScanMenuItem = menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.menu_stats_refresh)
                 .setIcon(com.android.internal.R.drawable.ic_menu_refresh);
@@ -425,17 +424,6 @@
                 mMetricsFeatureProvider.action(getActivity(), MetricsEvent.ACTION_WIFI_FORCE_SCAN);
                 mWifiTracker.forceScan();
                 return true;
-            case MENU_ID_ADVANCED:
-                if (getActivity() instanceof SettingsActivity) {
-                    ((SettingsActivity) getActivity()).startPreferencePanel(
-                            AdvancedWifiSettings.class.getCanonicalName(), null,
-                            R.string.wifi_advanced_titlebar, null, this, 0);
-                } else {
-                    startFragment(this, AdvancedWifiSettings.class.getCanonicalName(),
-                            R.string.wifi_advanced_titlebar, -1 /* Do not request a results */,
-                            null);
-                }
-                return true;
         }
         return super.onOptionsItemSelected(item);
     }
@@ -689,11 +677,11 @@
                     mAccessPointsPreferenceCategory.addPreference(pref);
                     mAddPreference.setOrder(index++);
                     mAccessPointsPreferenceCategory.addPreference(mAddPreference);
-                    setSavedNetworkPreferenceVisibility();
+                    setConfigureWifiSettingsVisibility();
                 } else {
                     mAddPreference.setOrder(index++);
                     mAccessPointsPreferenceCategory.addPreference(mAddPreference);
-                    setSavedNetworkPreferenceVisibility();
+                    setConfigureWifiSettingsVisibility();
                     setProgressBarVisible(false);
                 }
                 if (mScanMenuItem != null) {
@@ -713,7 +701,7 @@
 
             case WifiManager.WIFI_STATE_DISABLED:
                 setOffMessage();
-                setSavedNetworkPreferenceVisibility();
+                setConfigureWifiSettingsVisibility();
                 setProgressBarVisible(false);
                 if (mScanMenuItem != null) {
                     mScanMenuItem.setEnabled(false);
@@ -722,7 +710,12 @@
         }
     }
 
-    private void setSavedNetworkPreferenceVisibility() {
+    private void setConfigureWifiSettingsVisibility() {
+        if (isUiRestricted()) {
+            mAdditionalSettingsPreferenceCategory.removeAll();
+            return;
+        }
+        mAdditionalSettingsPreferenceCategory.addPreference(mConfigureWifiSettingsPreference);
         if (mWifiTracker.doSavedNetworksExist()) {
             mAdditionalSettingsPreferenceCategory.addPreference(mSavedNetworksPreference);
         } else {
diff --git a/src/com/android/settings/wifi/WpsPreferenceController.java b/src/com/android/settings/wifi/WpsPreferenceController.java
new file mode 100644
index 0000000..736d231
--- /dev/null
+++ b/src/com/android/settings/wifi/WpsPreferenceController.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2017 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;
+
+import android.app.Dialog;
+import android.app.FragmentManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.wifi.WifiManager;
+import android.net.wifi.WpsInfo;
+import android.os.Bundle;
+import android.support.annotation.VisibleForTesting;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.core.PreferenceController;
+import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
+import com.android.settings.core.lifecycle.Lifecycle;
+import com.android.settings.core.lifecycle.LifecycleObserver;
+import com.android.settings.core.lifecycle.events.OnPause;
+import com.android.settings.core.lifecycle.events.OnResume;
+
+/**
+ * {@link PreferenceController} that shows Dialog for WPS progress. Disabled when Wi-Fi is off.
+ */
+public class WpsPreferenceController extends PreferenceController implements
+        LifecycleObserver, OnPause, OnResume {
+
+    private static final String KEY_WPS_PUSH = "wps_push_button";
+    private static final String KEY_WPS_PIN = "wps_pin_entry";
+
+    private final WifiManager mWifiManager;
+    private final FragmentManager mFragmentManager;
+    @VisibleForTesting
+    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            togglePreferences();
+        }
+    };
+    private final IntentFilter mFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
+
+    private Preference mWpsPushPref;
+    private Preference mWpsPinPref;
+
+    public WpsPreferenceController(
+            Context context,
+            Lifecycle lifecycle,
+            WifiManager wifiManager,
+            FragmentManager fragmentManager) {
+        super(context);
+        mWifiManager = wifiManager;
+        mFragmentManager = fragmentManager;
+        lifecycle.addObserver(this);
+    }
+
+    @Override
+    public boolean isAvailable() {
+        // Always show preference.
+        return true;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        // Returns null because this controller contains more than 1 preference.
+        return null;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mWpsPushPref = screen.findPreference(KEY_WPS_PUSH);
+        mWpsPinPref = screen.findPreference(KEY_WPS_PIN);
+        if (mWpsPushPref == null || mWpsPinPref == null) {
+            return;
+        }
+        // WpsDialog: Create the dialog like WifiSettings does.
+        mWpsPushPref.setOnPreferenceClickListener((arg) -> {
+                    WpsFragment wpsFragment = new WpsFragment(WpsInfo.PBC);
+                    wpsFragment.show(mFragmentManager, KEY_WPS_PUSH);
+                    return true;
+                }
+        );
+
+        // WpsDialog: Create the dialog like WifiSettings does.
+        mWpsPinPref.setOnPreferenceClickListener((arg) -> {
+            WpsFragment wpsFragment = new WpsFragment(WpsInfo.DISPLAY);
+            wpsFragment.show(mFragmentManager, KEY_WPS_PIN);
+            return true;
+        });
+        togglePreferences();
+    }
+
+    @Override
+    public void onResume() {
+        mContext.registerReceiver(mReceiver, mFilter);
+    }
+
+    @Override
+    public void onPause() {
+        mContext.unregisterReceiver(mReceiver);
+    }
+
+    private void togglePreferences() {
+        if (mWpsPushPref != null && mWpsPinPref != null) {
+            boolean enabled = mWifiManager.isWifiEnabled();
+            mWpsPushPref.setEnabled(enabled);
+            mWpsPinPref.setEnabled(enabled);
+        }
+    }
+
+    /**
+     * Fragment for Dialog to show WPS progress.
+     */
+    public static class WpsFragment extends InstrumentedDialogFragment {
+        private static int mWpsSetup;
+
+        // Public default constructor is required for rotation.
+        public WpsFragment() {
+            super();
+        }
+
+        public WpsFragment(int wpsSetup) {
+            super();
+            mWpsSetup = wpsSetup;
+        }
+
+        @Override
+        public int getMetricsCategory() {
+            return MetricsEvent.DIALOG_WPS_SETUP;
+        }
+
+        @Override
+        public Dialog onCreateDialog(Bundle savedInstanceState) {
+            return new WpsDialog(getActivity(), mWpsSetup);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/wifi/p2p/WifiP2pPreferenceController.java b/src/com/android/settings/wifi/p2p/WifiP2pPreferenceController.java
new file mode 100644
index 0000000..df871ab
--- /dev/null
+++ b/src/com/android/settings/wifi/p2p/WifiP2pPreferenceController.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2017 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.p2p;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.wifi.WifiManager;
+import android.support.annotation.VisibleForTesting;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.core.PreferenceController;
+import com.android.settings.core.lifecycle.Lifecycle;
+import com.android.settings.core.lifecycle.LifecycleObserver;
+import com.android.settings.core.lifecycle.events.OnPause;
+import com.android.settings.core.lifecycle.events.OnResume;
+
+/**
+ * {@link PreferenceController} to toggle Wifi Direct preference on Wi-Fi state.
+ */
+public class WifiP2pPreferenceController extends PreferenceController implements
+        LifecycleObserver, OnPause, OnResume {
+
+    private static final String KEY_WIFI_DIRECT = "wifi_direct";
+
+    private final WifiManager mWifiManager;
+    @VisibleForTesting
+    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            togglePreferences();
+        }
+    };
+    private final IntentFilter mFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
+
+    private Preference mWifiDirectPref;
+
+    public WifiP2pPreferenceController(
+            Context context, Lifecycle lifecycle, WifiManager wifiManager) {
+        super(context);
+        mWifiManager = wifiManager;
+        lifecycle.addObserver(this);
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mWifiDirectPref = screen.findPreference(KEY_WIFI_DIRECT);
+        togglePreferences();
+    }
+
+    @Override
+    public void onResume() {
+        mContext.registerReceiver(mReceiver, mFilter);
+    }
+
+    @Override
+    public void onPause() {
+        mContext.unregisterReceiver(mReceiver);
+    }
+
+    @Override
+    public boolean isAvailable() {
+        // Always show preference.
+        return true;
+    }
+    @Override
+    public String getPreferenceKey() {
+        return KEY_WIFI_DIRECT;
+    }
+
+    private void togglePreferences() {
+        if (mWifiDirectPref != null) {
+            mWifiDirectPref.setEnabled(mWifiManager.isWifiEnabled());
+        }
+    }
+}
diff --git a/tests/robotests/assets/grandfather_not_implementing_indexable b/tests/robotests/assets/grandfather_not_implementing_indexable
index 33484d1..3e4a9a3 100644
--- a/tests/robotests/assets/grandfather_not_implementing_indexable
+++ b/tests/robotests/assets/grandfather_not_implementing_indexable
@@ -81,7 +81,6 @@
 com.android.settings.accessibility.ToggleFontSizePreferenceFragment
 com.android.settings.accessibility.ToggleGlobalGesturePreferenceFragment
 com.android.settings.wifi.ConfigureWifiSettings
-com.android.settings.wifi.AdvancedWifiSettings
 com.android.settings.applications.PremiumSmsAccess
 com.android.settings.applications.UsageAccessDetails
 com.android.settings.applications.AppStorageSettings
diff --git a/tests/robotests/src/com/android/settings/wifi/WpsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/WpsPreferenceControllerTest.java
new file mode 100644
index 0000000..ac0861e
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/wifi/WpsPreferenceControllerTest.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2017 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;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.FragmentManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.wifi.WifiManager;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settings.core.lifecycle.Lifecycle;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class WpsPreferenceControllerTest {
+
+    @Mock
+    private Context mContext;
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private WifiManager mWifiManager;
+    @Mock
+    private FragmentManager mFragmentManager;
+    @Mock
+    private PreferenceScreen mScreen;
+    @Mock
+    private Preference mWpsPushPref;
+    @Mock
+    private Preference mWpsPinPref;
+
+    private Lifecycle mLifecycle;
+    private WpsPreferenceController mController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mLifecycle = new Lifecycle();
+        when(mContext.getSystemService(WifiManager.class))
+                .thenReturn(mWifiManager);
+        when(mScreen.findPreference(anyString()))
+                .thenReturn(mWpsPushPref)
+                .thenReturn(mWpsPinPref);
+        mController = new WpsPreferenceController(
+                mContext, mLifecycle, mWifiManager, mFragmentManager);
+    }
+
+    @Test
+    public void testIsAvailable_shouldAlwaysReturnTrue() {
+        assertThat(mController.isAvailable()).isTrue();
+    }
+
+    @Test
+    public void testOnResume_shouldRegisterListener() {
+        mLifecycle.onResume();
+        verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
+    }
+    @Test
+    public void testOnPause_shouldUnregisterListener() {
+        mLifecycle.onPause();
+        verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
+    }
+
+    @Test
+    public void testWifiStateChange_shouldToggleEnabledState() {
+        when(mWifiManager.isWifiEnabled()).thenReturn(true);
+
+        //Sets the preferences.
+        mController.displayPreference(mScreen);
+        verify(mWpsPushPref).setEnabled(true);
+        verify(mWpsPinPref).setEnabled(true);
+
+        Intent dummyIntent = new Intent();
+        mController.mReceiver.onReceive(mContext, dummyIntent);
+        verify(mWpsPushPref, times(2)).setEnabled(true);
+        verify(mWpsPinPref, times(2)).setEnabled(true);
+
+        when(mWifiManager.isWifiEnabled()).thenReturn(false);
+        mController.mReceiver.onReceive(mContext, dummyIntent);
+        verify(mWpsPushPref).setEnabled(false);
+        verify(mWpsPinPref).setEnabled(false);
+    }
+
+    @Test
+    public void testDisplayPreference_shouldSetPreferenceClickListenerAndToggleEnabledState() {
+        when(mWifiManager.isWifiEnabled()).thenReturn(true);
+        mController.displayPreference(mScreen);
+        verify(mWpsPushPref).setOnPreferenceClickListener(any());
+        verify(mWpsPinPref).setOnPreferenceClickListener(any());
+        verify(mWpsPushPref).setEnabled(true);
+        verify(mWpsPinPref).setEnabled(true);
+    }
+
+    @Test
+    public void testDisplayPreference_shouldDisablePreferenceWhenWifiDisabled() {
+        when(mWifiManager.isWifiEnabled()).thenReturn(false);
+        mController.displayPreference(mScreen);
+        verify(mWpsPushPref).setEnabled(false);
+        verify(mWpsPinPref).setEnabled(false);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2PPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2PPreferenceControllerTest.java
new file mode 100644
index 0000000..06caadf
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2PPreferenceControllerTest.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2017 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.p2p;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.wifi.WifiManager;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settings.core.lifecycle.Lifecycle;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class WifiP2PPreferenceControllerTest {
+
+    @Mock
+    private Context mContext;
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private WifiManager mWifiManager;
+    @Mock
+    private PreferenceScreen mScreen;
+    @Mock
+    private Preference mWifiDirectPreference;
+
+    private Lifecycle mLifecycle;
+    private WifiP2pPreferenceController mController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mLifecycle = new Lifecycle();
+        when(mContext.getSystemService(WifiManager.class))
+                .thenReturn(mWifiManager);
+        when(mScreen.findPreference(anyString()))
+                .thenReturn(mWifiDirectPreference);
+        mController = new WifiP2pPreferenceController(mContext, mLifecycle, mWifiManager);
+    }
+
+    @Test
+    public void testIsAvailable_shouldAlwaysReturnTrue() {
+        assertThat(mController.isAvailable()).isTrue();
+    }
+
+    @Test
+    public void testOnResume_shouldRegisterListener() {
+        mLifecycle.onResume();
+        verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
+    }
+
+    @Test
+    public void testOnPause_shouldUnregisterListener() {
+        mLifecycle.onPause();
+        verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
+    }
+
+    @Test
+    public void testWifiStateChange_shouldToggleEnabledState() {
+        when(mWifiManager.isWifiEnabled()).thenReturn(true);
+
+        //Sets the preferences.
+        mController.displayPreference(mScreen);
+        verify(mWifiDirectPreference).setEnabled(true);
+
+        Intent dummyIntent = new Intent();
+        mController.mReceiver.onReceive(mContext, dummyIntent);
+        verify(mWifiDirectPreference, times(2)).setEnabled(true);
+
+        when(mWifiManager.isWifiEnabled()).thenReturn(false);
+        mController.mReceiver.onReceive(mContext, dummyIntent);
+        verify(mWifiDirectPreference).setEnabled(false);
+    }
+
+    @Test
+    public void testDisplayPreference_shouldToggleEnabledState() {
+        when(mWifiManager.isWifiEnabled()).thenReturn(true);
+        mController.displayPreference(mScreen);
+        verify(mWifiDirectPreference).setEnabled(true);
+
+        when(mWifiManager.isWifiEnabled()).thenReturn(false);
+        mController.displayPreference(mScreen);
+        verify(mWifiDirectPreference).setEnabled(false);
+    }
+}