OSDN Git Service

Fix search highlight
authorFan Zhang <zhfan@google.com>
Tue, 20 Feb 2018 19:25:56 +0000 (11:25 -0800)
committerFan Zhang <zhfan@google.com>
Tue, 20 Feb 2018 21:19:26 +0000 (13:19 -0800)
- Fragments should not have advanced button when coming from search.

Change-Id: I10a192216b7ff702e73b791acbcc1eb1d71cb407
Fixes: 73348428
Test: robotests

src/com/android/settings/SettingsPreferenceFragment.java
src/com/android/settings/deviceinfo/DeviceInfoSettings.java
src/com/android/settings/location/LocationSettings.java
src/com/android/settings/widget/HighlightablePreferenceGroupAdapter.java
src/com/android/settings/wifi/ConfigureWifiSettings.java
tests/robotests/src/com/android/settings/widget/HighlightablePreferenceGroupAdapterTest.java

index 2fceb63..245a341 100644 (file)
@@ -135,19 +135,7 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
         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 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
         }
     }
 
+    /**
+     * 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();
index 9b99e8b..79f57be 100644 (file)
@@ -20,14 +20,12 @@ import android.app.Activity;
 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 @@ public class DeviceInfoSettings extends DashboardFragment implements Indexable {
     }
 
     @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
index 510c162..3e9c8af 100644 (file)
@@ -67,15 +67,13 @@ public class LocationSettings extends DashboardFragment {
     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;
index e1999ef..cad11b7 100644 (file)
 
 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.util.TypedValue;
 import android.view.View;
 
 import com.android.settings.R;
+import com.android.settings.SettingsPreferenceFragment;
 
 public class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter {
 
@@ -41,6 +46,39 @@ public class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter
     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);
index 1607b74..3fd2171 100644 (file)
@@ -23,7 +23,6 @@ import android.net.ConnectivityManager;
 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 @@ public class ConfigureWifiSettings extends DashboardFragment {
 
     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 @@ public class ConfigureWifiSettings extends DashboardFragment {
     }
 
     @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
index e2fb6c1..fd5800f 100644 (file)
@@ -27,12 +27,16 @@ import static org.mockito.Mockito.when;
 
 import android.content.Context;
 import android.graphics.drawable.ColorDrawable;
+import android.os.Bundle;
 import android.support.v7.preference.PreferenceCategory;
+import android.support.v7.preference.PreferenceScreen;
 import android.support.v7.preference.PreferenceViewHolder;
 import android.support.v7.widget.RecyclerView;
 import android.view.View;
 
 import com.android.settings.R;
+import com.android.settings.SettingsActivity;
+import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.TestConfig;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 
@@ -55,6 +59,9 @@ public class HighlightablePreferenceGroupAdapterTest {
     private View mRoot;
     @Mock
     private PreferenceCategory mPreferenceCatetory;
+    @Mock
+    private SettingsPreferenceFragment mFragment;
+
     private Context mContext;
     private HighlightablePreferenceGroupAdapter mAdapter;
     private PreferenceViewHolder mViewHolder;
@@ -96,6 +103,57 @@ public class HighlightablePreferenceGroupAdapterTest {
     }
 
     @Test
+    public void adjustInitialExpandedChildCount_invalidInput_shouldNotadjust() {
+        HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(null /* host */);
+        HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(mFragment);
+        final Bundle args = new Bundle();
+        when(mFragment.getArguments()).thenReturn(args);
+        HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(mFragment);
+        final PreferenceScreen screen = mock(PreferenceScreen.class);
+        when(mFragment.getArguments()).thenReturn(null);
+        when(mFragment.getPreferenceScreen()).thenReturn(screen);
+        HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(mFragment);
+        verifyZeroInteractions(screen);
+    }
+
+    @Test
+    public void adjustInitialExpandedChildCount_hasHightlightKey_shouldExpandAllChildren() {
+        final Bundle args = new Bundle();
+        when(mFragment.getArguments()).thenReturn(args);
+        args.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY, "testkey");
+        final PreferenceScreen screen = mock(PreferenceScreen.class);
+        when(mFragment.getPreferenceScreen()).thenReturn(screen);
+        HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(mFragment);
+
+        verify(screen).setInitialExpandedChildrenCount(Integer.MAX_VALUE);
+    }
+
+    @Test
+    public void adjustInitialExpandedChildCount_noKeyOrChildCountOverride_shouldDoNothing() {
+        final Bundle args = new Bundle();
+        when(mFragment.getArguments()).thenReturn(args);
+        when(mFragment.getInitialExpandedChildCount()).thenReturn(-1);
+        final PreferenceScreen screen = mock(PreferenceScreen.class);
+        when(mFragment.getPreferenceScreen()).thenReturn(screen);
+        HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(mFragment);
+
+        verify(mFragment).getInitialExpandedChildCount();
+        verifyZeroInteractions(screen);
+    }
+
+    @Test
+    public void adjustInitialExpandedChildCount_hasCountOverride_shouldDoNothing() {
+        when(mFragment.getInitialExpandedChildCount()).thenReturn(10);
+        final PreferenceScreen screen = mock(PreferenceScreen.class);
+        when(mFragment.getPreferenceScreen()).thenReturn(screen);
+        HighlightablePreferenceGroupAdapter.adjustInitialExpandedChildCount(mFragment);
+
+        verify(mFragment).getInitialExpandedChildCount();
+
+        verify(screen).setInitialExpandedChildrenCount(10);
+    }
+
+    @Test
     public void updateBackground_notHighlightedRow_shouldNotSetHighlightedTag() {
         ReflectionHelpers.setField(mAdapter, "mHighlightPosition", 10);