Fix search highlight

- Fragments should not have advanced button when coming from search.

Change-Id: I10a192216b7ff702e73b791acbcc1eb1d71cb407
Fixes: 73348428
Test: robotests
diff --git a/src/com/android/settings/SettingsPreferenceFragment.java b/src/com/android/settings/SettingsPreferenceFragment.java
index 2fceb63..245a341 100644
--- a/src/com/android/settings/SettingsPreferenceFragment.java
+++ b/src/com/android/settings/SettingsPreferenceFragment.java
@@ -135,19 +135,7 @@
         if (icicle != null) {
             mPreferenceHighlighted = icicle.getBoolean(SAVE_HIGHLIGHTED_KEY);
         }
-        final Bundle arguments = getArguments();
-
-        // Check if we should keep the preferences expanded.
-        if (arguments != null) {
-            final String highlightKey =
-                    arguments.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY);
-            if (!TextUtils.isEmpty(highlightKey)) {
-                final PreferenceScreen screen = getPreferenceScreen();
-                if (screen != null) {
-                    screen.setInitialExpandedChildrenCount(Integer.MAX_VALUE);
-                }
-            }
-        }
+        HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(this /* host */);
     }
 
     @Override
@@ -264,6 +252,15 @@
         }
     }
 
+    /**
+     * Returns initial expanded child count.
+     * <p/>
+     * Only override this method if the initial expanded child must be determined at run time.
+     */
+    public int getInitialExpandedChildCount() {
+        return 0;
+    }
+
     protected void onDataSetChanged() {
         highlightPreferenceIfNeeded();
         updateEmptyView();
diff --git a/src/com/android/settings/deviceinfo/DeviceInfoSettings.java b/src/com/android/settings/deviceinfo/DeviceInfoSettings.java
index 9b99e8b..79f57be 100644
--- a/src/com/android/settings/deviceinfo/DeviceInfoSettings.java
+++ b/src/com/android/settings/deviceinfo/DeviceInfoSettings.java
@@ -20,14 +20,12 @@
 import android.app.Fragment;
 import android.content.Context;
 import android.content.Intent;
-import android.os.Bundle;
 import android.provider.SearchIndexableResource;
 import android.support.annotation.VisibleForTesting;
 import android.telephony.TelephonyManager;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
-import com.android.settings.SettingsActivity;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.dashboard.SummaryLoader;
 import com.android.settings.deviceinfo.firmwareversion.FirmwareVersionPreferenceController;
@@ -64,23 +62,12 @@
     }
 
     @Override
-    public void onCreate(Bundle icicle) {
-        super.onCreate(icicle);
-        final Bundle arguments = getArguments();
-        // Do not override initial expand children count if we come from
-        // search (EXTRA_FRAGMENT_ARG_KEY is set) - we need to display every if entry point
-        // is search.
-        if (arguments == null
-                || !arguments.containsKey(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY)) {
-
-            // Increase the number of children when the device contains more than 1 sim.
-            final TelephonyManager telephonyManager = (TelephonyManager) getContext()
-                    .getSystemService(Context.TELEPHONY_SERVICE);
-            final int numberOfChildren = Math.max(SIM_PREFERENCES_COUNT,
-                    SIM_PREFERENCES_COUNT * telephonyManager.getPhoneCount())
-                    + NON_SIM_PREFERENCES_COUNT;
-            getPreferenceScreen().setInitialExpandedChildrenCount(numberOfChildren);
-        }
+    public int getInitialExpandedChildCount() {
+        final TelephonyManager telephonyManager = (TelephonyManager) getContext()
+                .getSystemService(Context.TELEPHONY_SERVICE);
+        return Math.max(SIM_PREFERENCES_COUNT,
+                SIM_PREFERENCES_COUNT * telephonyManager.getPhoneCount())
+                + NON_SIM_PREFERENCES_COUNT;
     }
 
     @Override
diff --git a/src/com/android/settings/location/LocationSettings.java b/src/com/android/settings/location/LocationSettings.java
index 510c162..3e9c8af 100644
--- a/src/com/android/settings/location/LocationSettings.java
+++ b/src/com/android/settings/location/LocationSettings.java
@@ -67,15 +67,13 @@
     private LocationSwitchBarController mSwitchBarController;
 
     @Override
-    public void onCreate(Bundle icicle) {
-        super.onCreate(icicle);
+    public int getInitialExpandedChildCount() {
         final RecentLocationApps recentLocationApps = new RecentLocationApps(getActivity());
-        int locationRequestsApps = recentLocationApps.getAppList().size();
-        int locationRequestsPrefs = locationRequestsApps == 0 ? 1 : locationRequestsApps;
-        getPreferenceScreen().setInitialExpandedChildrenCount(locationRequestsPrefs + 2);
+        final int locationRequestsApps = recentLocationApps.getAppList().size();
+        final int locationRequestsPrefs = locationRequestsApps == 0 ? 1 : locationRequestsApps;
+        return locationRequestsPrefs + 2;
     }
 
-
     @Override
     public int getMetricsCategory() {
         return MetricsEvent.LOCATION;
diff --git a/src/com/android/settings/widget/HighlightablePreferenceGroupAdapter.java b/src/com/android/settings/widget/HighlightablePreferenceGroupAdapter.java
index e1999ef..cad11b7 100644
--- a/src/com/android/settings/widget/HighlightablePreferenceGroupAdapter.java
+++ b/src/com/android/settings/widget/HighlightablePreferenceGroupAdapter.java
@@ -16,10 +16,14 @@
 
 package com.android.settings.widget;
 
+import static com.android.settings.SettingsActivity.EXTRA_FRAGMENT_ARG_KEY;
+
 import android.content.Context;
+import android.os.Bundle;
 import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.PreferenceGroup;
 import android.support.v7.preference.PreferenceGroupAdapter;
+import android.support.v7.preference.PreferenceScreen;
 import android.support.v7.preference.PreferenceViewHolder;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
@@ -27,6 +31,7 @@
 import android.view.View;
 
 import com.android.settings.R;
+import com.android.settings.SettingsPreferenceFragment;
 
 public class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
 
@@ -41,6 +46,39 @@
     private boolean mHighlightRequested;
     private int mHighlightPosition = RecyclerView.NO_POSITION;
 
+
+    /**
+     * Tries to override initial expanded child count.
+     * <p/>
+     * Initial expanded child count will be ignored if:
+     * 1. fragment contains request to highlight a particular row.
+     * 2. count value is invalid.
+     */
+    public static void adjustInitialExpandedChildCount(SettingsPreferenceFragment host) {
+        if (host == null) {
+            return;
+        }
+        final PreferenceScreen screen = host.getPreferenceScreen();
+        if (screen == null) {
+            return;
+        }
+        final Bundle arguments = host.getArguments();
+        if (arguments != null) {
+            final String highlightKey = arguments.getString(EXTRA_FRAGMENT_ARG_KEY);
+            if (!TextUtils.isEmpty(highlightKey)) {
+                // Has highlight row - expand everything
+                screen.setInitialExpandedChildrenCount(Integer.MAX_VALUE);
+                return;
+            }
+        }
+
+        final int initialCount = host.getInitialExpandedChildCount();
+        if (initialCount <= 0) {
+            return;
+        }
+        screen.setInitialExpandedChildrenCount(initialCount);
+    }
+
     public HighlightablePreferenceGroupAdapter(PreferenceGroup preferenceGroup, String key,
             boolean highlightRequested) {
         super(preferenceGroup);
diff --git a/src/com/android/settings/wifi/ConfigureWifiSettings.java b/src/com/android/settings/wifi/ConfigureWifiSettings.java
index 1607b74..3fd2171 100644
--- a/src/com/android/settings/wifi/ConfigureWifiSettings.java
+++ b/src/com/android/settings/wifi/ConfigureWifiSettings.java
@@ -23,7 +23,6 @@
 import android.net.NetworkInfo;
 import android.net.NetworkScoreManager;
 import android.net.wifi.WifiManager;
-import android.os.Bundle;
 import android.provider.SearchIndexableResource;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -44,7 +43,6 @@
 
     private static final String TAG = "ConfigureWifiSettings";
 
-    public static final String KEY_WIFI_CONFIGURE = "wifi_configure_settings_screen";
     public static final String KEY_IP_ADDRESS = "current_ip_address";
 
     private WifiWakeupPreferenceController mWifiWakeupPreferenceController;
@@ -61,13 +59,12 @@
     }
 
     @Override
-    public void onCreate(Bundle icicle) {
-        super.onCreate(icicle);
+    public int getInitialExpandedChildCount() {
         int tileLimit = 2;
         if (mUseOpenWifiPreferenceController.isAvailable()) {
             tileLimit++;
         }
-        getPreferenceScreen().setInitialExpandedChildrenCount(tileLimit);
+        return tileLimit;
     }
 
     @Override