OSDN Git Service

Fix search highlight
[android-x86/packages-apps-Settings.git] / src / com / android / settings / widget / HighlightablePreferenceGroupAdapter.java
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);