[Android] Hack to enable MultiSelectListPreference

Geoffrey Métais git at videolan.org
Fri Apr 15 12:01:15 CEST 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Apr 15 12:00:19 2016 +0200| [1464d1a6ab882eec78ab3a749e8f06e52bbd7404] | committer: Geoffrey Métais

Hack to enable MultiSelectListPreference

> https://code.videolan.org/videolan/vlc-android/commit/1464d1a6ab882eec78ab3a749e8f06e52bbd7404
---

 vlc-android/res/xml/preferences_ui.xml             |  4 +-
 .../gui/preferences/BasePreferenceFragment.java    |  9 +++
 .../vlc/gui/preferences/PreferencesUi.java         |  1 +
 ...tiSelectListPreferenceDialogFragmentCompat.java | 88 ++++++++++++++++++++++
 .../hack/PreferenceFragmentCompatHack.java         | 63 ++++++++++++++++
 5 files changed, 163 insertions(+), 2 deletions(-)

diff --git a/vlc-android/res/xml/preferences_ui.xml b/vlc-android/res/xml/preferences_ui.xml
index 528ae7c..b9b73c7 100644
--- a/vlc-android/res/xml/preferences_ui.xml
+++ b/vlc-android/res/xml/preferences_ui.xml
@@ -33,13 +33,13 @@
             android:title="@string/audio_title_alignment" />
     </PreferenceCategory>
     <PreferenceCategory android:title="@string/interface_controls" >
-        <!--android.support.v14.preference.MultiSelectListPreference
+        <android.support.v14.preference.MultiSelectListPreference
             android:dialogTitle="@string/preferences_languages_download"
             android:entries="@array/language_entries"
             android:entryValues="@array/language_values"
             android:key="languages_download_list"
             android:summary="@string/list_languages_download_summary"
-            android:title="@string/list_languages_download_title" /-->
+            android:title="@string/list_languages_download_title" />
         <CheckBoxPreference
             android:defaultValue="false"
             android:key="save_brightness"
diff --git a/vlc-android/src/org/videolan/vlc/gui/preferences/BasePreferenceFragment.java b/vlc-android/src/org/videolan/vlc/gui/preferences/BasePreferenceFragment.java
index d17e733..50dc854 100644
--- a/vlc-android/src/org/videolan/vlc/gui/preferences/BasePreferenceFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/preferences/BasePreferenceFragment.java
@@ -26,9 +26,11 @@ package org.videolan.vlc.gui.preferences;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v7.app.AppCompatActivity;
+import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceFragmentCompat;
 
 import org.videolan.vlc.R;
+import org.videolan.vlc.gui.preferences.hack.PreferenceFragmentCompatHack;
 
 public abstract class BasePreferenceFragment extends PreferenceFragmentCompat {
 
@@ -54,4 +56,11 @@ public abstract class BasePreferenceFragment extends PreferenceFragmentCompat {
                 .addToBackStack("main")
                 .commit();
     }
+
+    @Override
+    public void onDisplayPreferenceDialog(Preference preference) {
+        if (!PreferenceFragmentCompatHack.onDisplayPreferenceDialog(this, preference)) {
+            super.onDisplayPreferenceDialog(preference);
+        }
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java
index cea13d8..d42cc7c 100644
--- a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java
+++ b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesUi.java
@@ -50,6 +50,7 @@ public class PreferencesUi extends BasePreferenceFragment {
         super.onCreate(savedInstanceState);
 
         findPreference("tv_ui").setVisible(AndroidUtil.isJellyBeanMR1OrLater());
+        findPreference("languages_download_list").setVisible(AndroidUtil.isHoneycombOrLater());
     }
 
     @Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/preferences/hack/MultiSelectListPreferenceDialogFragmentCompat.java b/vlc-android/src/org/videolan/vlc/gui/preferences/hack/MultiSelectListPreferenceDialogFragmentCompat.java
new file mode 100644
index 0000000..d212db9
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/preferences/hack/MultiSelectListPreferenceDialogFragmentCompat.java
@@ -0,0 +1,88 @@
+package org.videolan.vlc.gui.preferences.hack;
+
+import android.annotation.TargetApi;
+import android.content.DialogInterface;
+import android.os.Build;
+import android.os.Bundle;
+import android.support.v14.preference.MultiSelectListPreference;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.preference.DialogPreference;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceDialogFragmentCompat;
+
+import java.util.HashSet;
+import java.util.Set;
+
+ at TargetApi(Build.VERSION_CODES.HONEYCOMB)
+public class MultiSelectListPreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat
+        implements DialogPreference.TargetFragment {
+    private final Set<String> mNewValues = new HashSet<>();
+    private boolean mPreferenceChanged;
+
+    public MultiSelectListPreferenceDialogFragmentCompat() {
+    }
+
+    public static MultiSelectListPreferenceDialogFragmentCompat newInstance(String key) {
+        MultiSelectListPreferenceDialogFragmentCompat fragment = new MultiSelectListPreferenceDialogFragmentCompat();
+        Bundle b = new Bundle(1);
+        b.putString("key", key);
+        fragment.setArguments(b);
+        return fragment;
+    }
+
+    private MultiSelectListPreference getListPreference() {
+        return (MultiSelectListPreference)this.getPreference();
+    }
+
+   protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
+        super.onPrepareDialogBuilder(builder);
+        final MultiSelectListPreference preference = getListPreference();
+        if (preference.getEntries() != null && preference.getEntryValues() != null) {
+            boolean[] checkedItems = getSelectedItems();
+            builder.setMultiChoiceItems(preference.getEntries(), checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
+                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
+                    mPreferenceChanged = true;
+                    if (isChecked) {
+                        mNewValues.add(preference.getEntryValues()[which].toString());
+                    } else {
+                        mNewValues.remove(preference.getEntryValues()[which].toString());
+                    }
+                }
+            });
+            this.mNewValues.clear();
+            this.mNewValues.addAll(preference.getValues());
+        } else {
+            throw new IllegalStateException("MultiSelectListPreference requires an entries array and an entryValues array.");
+        }
+    }
+
+    public void onDialogClosed(boolean positiveResult) {
+        MultiSelectListPreference preference = getListPreference();
+        if (positiveResult && mPreferenceChanged) {
+            Set<String> values = mNewValues;
+            if (preference.callChangeListener(values)) {
+                preference.setValues(values);
+            }
+        }
+        this.mPreferenceChanged = false;
+    }
+
+    private boolean[] getSelectedItems() {
+        MultiSelectListPreference preference = getListPreference();
+        CharSequence[] entries = preference.getEntryValues();
+        Set<String> values = preference.getValues();
+        boolean[] result = new boolean[entries.length];
+
+        for (int i = 0; i < entries.length; i++) {
+            result[i] = values.contains(entries[i].toString());
+        }
+
+        return result;
+    }
+
+    @Override
+    public Preference findPreference(CharSequence charSequence) {
+        return getPreference();
+    }
+}
+
diff --git a/vlc-android/src/org/videolan/vlc/gui/preferences/hack/PreferenceFragmentCompatHack.java b/vlc-android/src/org/videolan/vlc/gui/preferences/hack/PreferenceFragmentCompatHack.java
new file mode 100644
index 0000000..920135f
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/preferences/hack/PreferenceFragmentCompatHack.java
@@ -0,0 +1,63 @@
+/*
+ * This source is part of the
+ *      _____  ___   ____
+ *  __ / / _ \/ _ | / __/___  _______ _
+ * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/
+ * \___/_/|_/_/ |_/_/ (_)___/_/  \_, /
+ *                              /___/
+ * repository.
+ *
+ * Copyright (C) 2015 Carmen Alvarez (c at rmen.ca)
+ *
+ * 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 org.videolan.vlc.gui.preferences.hack;
+
+import android.os.Build;
+import android.support.v14.preference.MultiSelectListPreference;
+import android.support.v4.app.DialogFragment;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceFragmentCompat;
+
+public class PreferenceFragmentCompatHack {
+    private static final String FRAGMENT_TAG_DIALOG = "android.support.v7.preference.PreferenceFragment.DIALOG";
+
+    private PreferenceFragmentCompatHack() {
+        // prevent instantiation
+    }
+
+    /**
+     * Displays preference dialogs which aren't supported by default in PreferenceFragmentCompat.
+     *
+     * @return true if we managed a preference which isn't supported by default, false otherwise.
+     */
+    public static boolean onDisplayPreferenceDialog(PreferenceFragmentCompat preferenceFragmentCompat, Preference preference) {
+        DialogFragment dialogFragment = (DialogFragment) preferenceFragmentCompat.getFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG);
+        if (dialogFragment != null) return false;
+
+        // Hack to allow a MultiSelectListPreference
+        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH
+                && preference instanceof MultiSelectListPreference) {
+            dialogFragment = MultiSelectListPreferenceDialogFragmentCompat.newInstance(preference.getKey());
+        }
+
+        // We've created our own fragment:
+        if (dialogFragment != null) {
+            dialogFragment.setTargetFragment(preferenceFragmentCompat, 0);
+            dialogFragment.show(preferenceFragmentCompat.getFragmentManager(), FRAGMENT_TAG_DIALOG);
+            return true;
+        }
+
+        return false;
+    }
+}



More information about the Android mailing list