Merge "Index wi-fi preferences."
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 595a6e6..70b27bc 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1956,8 +1956,6 @@
     <string name="wifi_show_advanced">Advanced options</string>
     <!-- Message for talkback to say when focus on Advanced Options[CHAR LIMIT=NONE] -->
     <string name="wifi_advanced_toggle_description">Drop down list Advanced Options</string>
-    <!-- Extended message for talkback to say when Advanced Options is expanded. (e.g., Double-tap to collapse) [CHAR LIMIT=NONE] -->
-    <string name="wifi_advanced_toggle_description_expanded">collapse</string>
     <!-- Extended message for talkback to say when Advanced Options is collapsed. (e.g., Double-tap to expand) [CHAR LIMIT=NONE] -->
     <string name="wifi_advanced_toggle_description_collapsed">expand</string>
     <!-- Label for the SSID of the network -->
diff --git a/src/com/android/settings/AirplaneModeEnabler.java b/src/com/android/settings/AirplaneModeEnabler.java
index 64ad17d..ee3eaa4 100644
--- a/src/com/android/settings/AirplaneModeEnabler.java
+++ b/src/com/android/settings/AirplaneModeEnabler.java
@@ -104,8 +104,8 @@
             }
         }
 
-        for (ServiceStateListener ssListener : mServiceStateListeners) {
-            ssListener.start();
+        for (ServiceStateListener listener : mServiceStateListeners) {
+            listener.start();
         }
     }
 
@@ -113,8 +113,8 @@
         if (mServiceStateListeners == null) {
             return;
         }
-        for (ServiceStateListener ssListener : mServiceStateListeners) {
-            ssListener.stop();
+        for (ServiceStateListener listener : mServiceStateListeners) {
+            listener.stop();
         }
         mServiceStateListeners = null;
     }
@@ -210,9 +210,9 @@
             mEnabler = enabler;
         }
 
-        private int mSubId;
-        private TelephonyManager mTelephonyManager;
-        private AirplaneModeEnabler mEnabler;
+        private final int mSubId;
+        private final TelephonyManager mTelephonyManager;
+        private final AirplaneModeEnabler mEnabler;
 
         int getSubscriptionId() {
             return mSubId;
diff --git a/src/com/android/settings/datetime/timezone/TimeZoneSettings.java b/src/com/android/settings/datetime/timezone/TimeZoneSettings.java
index 8226a6c..60cd636 100644
--- a/src/com/android/settings/datetime/timezone/TimeZoneSettings.java
+++ b/src/com/android/settings/datetime/timezone/TimeZoneSettings.java
@@ -25,6 +25,7 @@
 import android.content.SharedPreferences;
 import android.icu.util.TimeZone;
 import android.os.Bundle;
+import android.provider.Settings;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuInflater;
@@ -123,9 +124,9 @@
         super.onCreate(icicle);
         // Hide all interactive preferences
         setPreferenceCategoryVisible((PreferenceCategory) findPreference(
-            PREF_KEY_REGION_CATEGORY), false);
+                PREF_KEY_REGION_CATEGORY), false);
         setPreferenceCategoryVisible((PreferenceCategory) findPreference(
-            PREF_KEY_FIXED_OFFSET_CATEGORY), false);
+                PREF_KEY_FIXED_OFFSET_CATEGORY), false);
 
         // Start loading TimeZoneData
         getLoaderManager().initLoader(0, null, new TimeZoneDataLoader.LoaderCreator(
@@ -237,7 +238,7 @@
         String tzId = data.getStringExtra(RegionZonePicker.EXTRA_RESULT_TIME_ZONE_ID);
         // Ignore the result if user didn't change the region or time zone.
         if (Objects.equals(regionId, use(RegionPreferenceController.class).getRegionId())
-            && Objects.equals(tzId, mSelectedTimeZoneId)) {
+                && Objects.equals(tzId, mSelectedTimeZoneId)) {
             return;
         }
 
@@ -327,9 +328,9 @@
     private void setSelectByRegion(boolean selectByRegion) {
         mSelectByRegion = selectByRegion;
         setPreferenceCategoryVisible((PreferenceCategory) findPreference(
-            PREF_KEY_REGION_CATEGORY), selectByRegion);
+                PREF_KEY_REGION_CATEGORY), selectByRegion);
         setPreferenceCategoryVisible((PreferenceCategory) findPreference(
-            PREF_KEY_FIXED_OFFSET_CATEGORY), !selectByRegion);
+                PREF_KEY_FIXED_OFFSET_CATEGORY), !selectByRegion);
         final String localeRegionId = getLocaleRegionId();
         final Set<String> allCountryIsoCodes = mTimeZoneData.getRegionIds();
 
@@ -353,6 +354,7 @@
      * Find the a region associated with the specified time zone, based on the time zone data.
      * If there are multiple regions associated with the given time zone, the priority will be given
      * to the region the user last picked and the country in user's locale.
+     *
      * @return null if no region associated with the time zone
      */
     private String findRegionIdForTzId(String tzId) {
@@ -378,7 +380,7 @@
     }
 
     private void setPreferenceCategoryVisible(PreferenceCategory category,
-        boolean isVisible) {
+            boolean isVisible) {
         // Hiding category doesn't hide all the children preference. Set visibility of its children.
         // Do not care grandchildren as time_zone_pref.xml has only 2 levels.
         category.setVisible(isVisible);
@@ -392,5 +394,13 @@
     }
 
     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
-            new BaseSearchIndexProvider(R.xml.time_zone_prefs);
+            new BaseSearchIndexProvider(R.xml.time_zone_prefs) {
+                @Override
+                protected boolean isPageSearchEnabled(Context context) {
+                    // We can't enter this page if the auto time zone is enabled.
+                    final int autoTimeZone = Settings.Global.getInt(context.getContentResolver(),
+                            Settings.Global.AUTO_TIME_ZONE, 1);
+                    return autoTimeZone == 1 ? false : true;
+                }
+            };
 }
diff --git a/src/com/android/settings/deviceinfo/BrandedAccountPreferenceController.java b/src/com/android/settings/deviceinfo/BrandedAccountPreferenceController.java
index bdd76fc..3543832 100644
--- a/src/com/android/settings/deviceinfo/BrandedAccountPreferenceController.java
+++ b/src/com/android/settings/deviceinfo/BrandedAccountPreferenceController.java
@@ -32,13 +32,13 @@
 import com.android.settings.overlay.FeatureFactory;
 
 public class BrandedAccountPreferenceController extends BasePreferenceController {
-    private final Account[] mAccounts;
+    private final AccountFeatureProvider mAccountFeatureProvider;
+    private Account[] mAccounts;
 
     public BrandedAccountPreferenceController(Context context, String key) {
         super(context, key);
-        final AccountFeatureProvider accountFeatureProvider = FeatureFactory.getFactory(
-                mContext).getAccountFeatureProvider();
-        mAccounts = accountFeatureProvider.getAccounts(mContext);
+        mAccountFeatureProvider = FeatureFactory.getFactory(mContext).getAccountFeatureProvider();
+        mAccounts = mAccountFeatureProvider.getAccounts(mContext);
     }
 
     @Override
@@ -56,8 +56,6 @@
     @Override
     public void displayPreference(PreferenceScreen screen) {
         super.displayPreference(screen);
-        final AccountFeatureProvider accountFeatureProvider = FeatureFactory.getFactory(
-                mContext).getAccountFeatureProvider();
         final Preference accountPreference = screen.findPreference(getPreferenceKey());
         if (accountPreference != null && (mAccounts == null || mAccounts.length == 0)) {
             screen.removePreference(accountPreference);
@@ -72,7 +70,7 @@
             args.putParcelable(AccountDetailDashboardFragment.KEY_USER_HANDLE,
                     android.os.Process.myUserHandle());
             args.putString(AccountDetailDashboardFragment.KEY_ACCOUNT_TYPE,
-                    accountFeatureProvider.getAccountType());
+                    mAccountFeatureProvider.getAccountType());
 
             new SubSettingLauncher(mContext)
                     .setDestination(AccountDetailDashboardFragment.class.getName())
@@ -83,4 +81,13 @@
             return true;
         });
     }
+
+    @Override
+    public void updateState(Preference preference) {
+        super.updateState(preference);
+        mAccounts = mAccountFeatureProvider.getAccounts(mContext);
+        if (mAccounts == null || mAccounts.length == 0) {
+            preference.setVisible(false);
+        }
+    }
 }
diff --git a/src/com/android/settings/network/ProxySubscriptionManager.java b/src/com/android/settings/network/ProxySubscriptionManager.java
index b10c727..0306b55 100644
--- a/src/com/android/settings/network/ProxySubscriptionManager.java
+++ b/src/com/android/settings/network/ProxySubscriptionManager.java
@@ -77,7 +77,7 @@
         mActiveSubscriptionsListeners =
                 new ArrayList<OnActiveSubscriptionChangedListener>();
 
-        mSubsciptionsMonitor = new ActiveSubsciptionsListener(looper, context) {
+        mSubscriptionMonitor = new ActiveSubsciptionsListener(looper, context) {
             public void onChanged() {
                 notifyAllListeners();
             }
@@ -85,16 +85,16 @@
         mAirplaneModeMonitor = new GlobalSettingsChangeListener(looper,
                 context, Settings.Global.AIRPLANE_MODE_ON) {
             public void onChanged(String field) {
-                mSubsciptionsMonitor.clearCache();
+                mSubscriptionMonitor.clearCache();
                 notifyAllListeners();
             }
         };
 
-        mSubsciptionsMonitor.start();
+        mSubscriptionMonitor.start();
     }
 
     private Lifecycle mLifecycle;
-    private ActiveSubsciptionsListener mSubsciptionsMonitor;
+    private ActiveSubsciptionsListener mSubscriptionMonitor;
     private GlobalSettingsChangeListener mAirplaneModeMonitor;
 
     private List<OnActiveSubscriptionChangedListener> mActiveSubscriptionsListeners;
@@ -130,17 +130,17 @@
 
     @OnLifecycleEvent(ON_START)
     void onStart() {
-        mSubsciptionsMonitor.start();
+        mSubscriptionMonitor.start();
     }
 
     @OnLifecycleEvent(ON_STOP)
     void onStop() {
-        mSubsciptionsMonitor.stop();
+        mSubscriptionMonitor.stop();
     }
 
     @OnLifecycleEvent(ON_DESTROY)
     void onDestroy() {
-        mSubsciptionsMonitor.close();
+        mSubscriptionMonitor.close();
         mAirplaneModeMonitor.close();
 
         if (mLifecycle != null) {
@@ -157,7 +157,7 @@
      * @return a SubscriptionManager
      */
     public SubscriptionManager get() {
-        return mSubsciptionsMonitor.getSubscriptionManager();
+        return mSubscriptionMonitor.getSubscriptionManager();
     }
 
     /**
@@ -166,7 +166,7 @@
      * @return max. number of active subscription info(s)
      */
     public int getActiveSubscriptionInfoCountMax() {
-        return mSubsciptionsMonitor.getActiveSubscriptionInfoCountMax();
+        return mSubscriptionMonitor.getActiveSubscriptionInfoCountMax();
     }
 
     /**
@@ -175,7 +175,7 @@
      * @return A list of active subscription info
      */
     public List<SubscriptionInfo> getActiveSubscriptionsInfo() {
-        return mSubsciptionsMonitor.getActiveSubscriptionsInfo();
+        return mSubscriptionMonitor.getActiveSubscriptionsInfo();
     }
 
     /**
@@ -185,7 +185,7 @@
      * @return A subscription info which is active list
      */
     public SubscriptionInfo getActiveSubscriptionInfo(int subId) {
-        return mSubsciptionsMonitor.getActiveSubscriptionInfo(subId);
+        return mSubscriptionMonitor.getActiveSubscriptionInfo(subId);
     }
 
     /**
@@ -194,7 +194,7 @@
      * @return A list of accessible subscription info
      */
     public List<SubscriptionInfo> getAccessibleSubscriptionsInfo() {
-        return mSubsciptionsMonitor.getAccessibleSubscriptionsInfo();
+        return mSubscriptionMonitor.getAccessibleSubscriptionsInfo();
     }
 
     /**
@@ -204,14 +204,14 @@
      * @return A subscription info which is accessible list
      */
     public SubscriptionInfo getAccessibleSubscriptionInfo(int subId) {
-        return mSubsciptionsMonitor.getAccessibleSubscriptionInfo(subId);
+        return mSubscriptionMonitor.getAccessibleSubscriptionInfo(subId);
     }
 
     /**
      * Clear data cached within proxy
      */
     public void clearCache() {
-        mSubsciptionsMonitor.clearCache();
+        mSubscriptionMonitor.clearCache();
     }
 
     /**
diff --git a/src/com/android/settings/wifi/WifiConfigController.java b/src/com/android/settings/wifi/WifiConfigController.java
index 05fcf5e..e228a14 100644
--- a/src/com/android/settings/wifi/WifiConfigController.java
+++ b/src/com/android/settings/wifi/WifiConfigController.java
@@ -347,9 +347,13 @@
                 showProxyFields();
                 final CheckBox advancedTogglebox =
                         (CheckBox) mView.findViewById(R.id.wifi_advanced_togglebox);
-                advancedTogglebox.setOnCheckedChangeListener(this);
-                advancedTogglebox.setChecked(showAdvancedFields);
-                setAdvancedOptionAccessibilityString(showAdvancedFields);
+                if (!showAdvancedFields) {
+                    // Need to show Advanced Option button.
+                    mView.findViewById(R.id.wifi_advanced_toggle).setVisibility(View.VISIBLE);
+                    advancedTogglebox.setOnCheckedChangeListener(this);
+                    advancedTogglebox.setChecked(showAdvancedFields);
+                    setAdvancedOptionAccessibilityString();
+                }
                 mView.findViewById(R.id.wifi_advanced_fields)
                         .setVisibility(showAdvancedFields ? View.VISIBLE : View.GONE);
             }
@@ -1553,17 +1557,10 @@
                 ((EditText) mPasswordView).setSelection(pos);
             }
         } else if (view.getId() == R.id.wifi_advanced_togglebox) {
-            final int toggleVisibility;
-            final int stringID;
-            if (isChecked) {
-                toggleVisibility = View.VISIBLE;
-                // Hide the SoftKeyboard temporary to let user can see most of the expanded items.
-                hideSoftKeyboard(mView.getWindowToken());
-            } else {
-                toggleVisibility = View.GONE;
-            }
-            mView.findViewById(R.id.wifi_advanced_fields).setVisibility(toggleVisibility);
-            setAdvancedOptionAccessibilityString(isChecked);
+            // Hide the SoftKeyboard temporary to let user can see most of the expanded items.
+            hideSoftKeyboard(mView.getWindowToken());
+            view.setVisibility(View.GONE);
+            mView.findViewById(R.id.wifi_advanced_fields).setVisibility(View.VISIBLE);
         }
     }
 
@@ -1673,7 +1670,7 @@
         ((CheckBox) mView.findViewById(R.id.wifi_advanced_togglebox))
                 .setOnCheckedChangeListener(this);
         // Set correct accessibility strings.
-        setAdvancedOptionAccessibilityString(false /* showAdvancedFields */);
+        setAdvancedOptionAccessibilityString();
     }
 
     /**
@@ -1765,7 +1762,7 @@
         inputMethodManager.hideSoftInputFromWindow(windowToken, 0 /* flags */);
     }
 
-    private void setAdvancedOptionAccessibilityString(boolean showAdvancedFields) {
+    private void setAdvancedOptionAccessibilityString() {
         final CheckBox advancedToggleBox = mView.findViewById(R.id.wifi_advanced_togglebox);
         advancedToggleBox.setAccessibilityDelegate(new AccessibilityDelegate() {
             @Override
@@ -1776,12 +1773,10 @@
                 info.setCheckable(false /* checkable */);
                 // To let TalkBack don't pronounce CheckBox.
                 info.setClassName(null /* className */);
-                final CharSequence accessibilityDoubleTapExtend = mContext.getString(
-                        showAdvancedFields ? R.string.wifi_advanced_toggle_description_expanded
-                                : R.string.wifi_advanced_toggle_description_collapsed);
                 // Customize TalkBack's pronunciation which been appended to "Double-tap to".
                 final AccessibilityAction customClick = new AccessibilityAction(
-                        AccessibilityNodeInfo.ACTION_CLICK, accessibilityDoubleTapExtend);
+                        AccessibilityNodeInfo.ACTION_CLICK,
+                        mContext.getString(R.string.wifi_advanced_toggle_description_collapsed));
                 info.addAction(customClick);
             }
         });
diff --git a/src/com/android/settings/wifi/WifiConfigController2.java b/src/com/android/settings/wifi/WifiConfigController2.java
index 0e9c4d0..3e8a42c 100644
--- a/src/com/android/settings/wifi/WifiConfigController2.java
+++ b/src/com/android/settings/wifi/WifiConfigController2.java
@@ -348,9 +348,13 @@
                 showProxyFields();
                 final CheckBox advancedTogglebox =
                         (CheckBox) mView.findViewById(R.id.wifi_advanced_togglebox);
-                advancedTogglebox.setOnCheckedChangeListener(this);
-                advancedTogglebox.setChecked(showAdvancedFields);
-                setAdvancedOptionAccessibilityString(showAdvancedFields);
+                if (!showAdvancedFields) {
+                    // Need to show Advanced Option button.
+                    mView.findViewById(R.id.wifi_advanced_toggle).setVisibility(View.VISIBLE);
+                    advancedTogglebox.setOnCheckedChangeListener(this);
+                    advancedTogglebox.setChecked(showAdvancedFields);
+                    setAdvancedOptionAccessibilityString();
+                }
                 mView.findViewById(R.id.wifi_advanced_fields)
                         .setVisibility(showAdvancedFields ? View.VISIBLE : View.GONE);
             }
@@ -1554,17 +1558,10 @@
                 ((EditText) mPasswordView).setSelection(pos);
             }
         } else if (view.getId() == R.id.wifi_advanced_togglebox) {
-            final int toggleVisibility;
-            final int stringID;
-            if (isChecked) {
-                toggleVisibility = View.VISIBLE;
-                // Hide the SoftKeyboard temporary to let user can see most of the expanded items.
-                hideSoftKeyboard(mView.getWindowToken());
-            } else {
-                toggleVisibility = View.GONE;
-            }
-            mView.findViewById(R.id.wifi_advanced_fields).setVisibility(toggleVisibility);
-            setAdvancedOptionAccessibilityString(isChecked);
+            // Hide the SoftKeyboard temporary to let user can see most of the expanded items.
+            hideSoftKeyboard(mView.getWindowToken());
+            view.setVisibility(View.GONE);
+            mView.findViewById(R.id.wifi_advanced_fields).setVisibility(View.VISIBLE);
         }
     }
 
@@ -1674,7 +1671,7 @@
         ((CheckBox) mView.findViewById(R.id.wifi_advanced_togglebox))
                 .setOnCheckedChangeListener(this);
         // Set correct accessibility strings.
-        setAdvancedOptionAccessibilityString(false /* showAdvancedFields */);
+        setAdvancedOptionAccessibilityString();
     }
 
     /**
@@ -1766,7 +1763,7 @@
         inputMethodManager.hideSoftInputFromWindow(windowToken, 0 /* flags */);
     }
 
-    private void setAdvancedOptionAccessibilityString(boolean showAdvancedFields) {
+    private void setAdvancedOptionAccessibilityString() {
         final CheckBox advancedToggleBox = mView.findViewById(R.id.wifi_advanced_togglebox);
         advancedToggleBox.setAccessibilityDelegate(new AccessibilityDelegate() {
             @Override
@@ -1777,12 +1774,10 @@
                 info.setCheckable(false /* checkable */);
                 // To let TalkBack don't pronounce CheckBox.
                 info.setClassName(null /* className */);
-                final CharSequence accessibilityDoubleTapExtend = mContext.getString(
-                        showAdvancedFields ? R.string.wifi_advanced_toggle_description_expanded
-                                : R.string.wifi_advanced_toggle_description_collapsed);
                 // Customize TalkBack's pronunciation which been appended to "Double-tap to".
                 final AccessibilityAction customClick = new AccessibilityAction(
-                        AccessibilityNodeInfo.ACTION_CLICK, accessibilityDoubleTapExtend);
+                        AccessibilityNodeInfo.ACTION_CLICK,
+                        mContext.getString(R.string.wifi_advanced_toggle_description_collapsed));
                 info.addAction(customClick);
             }
         });
diff --git a/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java b/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java
index f99b1d6..5f74d56 100644
--- a/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java
+++ b/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java
@@ -589,16 +589,24 @@
     private void refreshButtons() {
         boolean canForgetNetwork = mWifiEntry.canForget();
         boolean canSignIntoNetwork = canSignIntoNetwork();
-        boolean canConnectNetwork = mWifiEntry.canConnect();
+        boolean showConnectButton = mWifiEntry.canConnect()
+                || mWifiEntry.getConnectedState() == WifiEntry.CONNECTED_STATE_CONNECTING;
         boolean canShareNetwork = canShareNetwork();
 
         mButtonsPref.setButton1Visible(canForgetNetwork);
         mButtonsPref.setButton2Visible(canSignIntoNetwork);
-        mButtonsPref.setButton3Visible(canConnectNetwork);
+        mButtonsPref.setButton3Visible(showConnectButton);
+        if (showConnectButton) {
+            if (mWifiEntry.getConnectedState() == WifiEntry.CONNECTED_STATE_CONNECTING) {
+                mButtonsPref.setButton3Text(R.string.wifi_connecting).setButton3Enabled(false);
+            } else {
+                mButtonsPref.setButton3Text(R.string.wifi_connect).setButton3Enabled(true);
+            }
+        }
         mButtonsPref.setButton4Visible(canShareNetwork);
         mButtonsPref.setVisible(canForgetNetwork
                 || canSignIntoNetwork
-                || canConnectNetwork
+                || showConnectButton
                 || canShareNetwork);
     }
 
@@ -806,8 +814,6 @@
 
     @VisibleForTesting
     void connectNetwork() {
-        // TODO(b/143326832): What to do with WifiManager#isWifiEnabled() false case?
-        mButtonsPref.setButton3Text(R.string.wifi_connecting).setButton3Enabled(false);
         mWifiEntry.connect();
     }
 
@@ -847,8 +853,6 @@
             Toast.makeText(mContext,
                     mContext.getString(R.string.wifi_connected_to_message, mWifiEntry.getTitle()),
                     Toast.LENGTH_SHORT).show();
-            updateNetworkInfo();
-            refreshPage();
         } else if (mWifiEntry.getLevel() == WifiEntry.WIFI_LEVEL_UNREACHABLE) {
             Toast.makeText(mContext,
                     R.string.wifi_not_in_range_message,
diff --git a/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java b/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java
index e1202ae..bf50742 100644
--- a/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/WifiConfigControllerTest.java
@@ -549,4 +549,13 @@
         assertThat(advButton.getContentDescription()).isEqualTo(
                 mContext.getString(R.string.wifi_advanced_toggle_description));
     }
+
+    @Test
+    public void getVisibility_whenAdvancedOptionClicked_shouldBeGone() {
+        final CheckBox advButton = mView.findViewById(R.id.wifi_advanced_togglebox);
+
+        advButton.performClick();
+
+        assertThat(advButton.getVisibility()).isEqualTo(View.GONE);
+    }
 }