[Android] Separate click and switch in extension preferences

David Papazian git at videolan.org
Fri Sep 1 17:40:01 CEST 2017


vlc-android | branch: master | David Papazian <david.papazian at metz.supelec.fr> | Tue Aug 29 18:25:55 2017 +0200| [5b59c898b7b0928a527f968cf987aec6c0c906bf] | committer: Geoffrey Métais

Separate click and switch in extension preferences

Signed-off-by: Geoffrey Métais <geoffrey.metais at gmail.com>

> https://code.videolan.org/videolan/vlc-android/commit/5b59c898b7b0928a527f968cf987aec6c0c906bf
---

 .../preferences/PreferencesExtensionFragment.java  |  1 +
 .../vlc/gui/preferences/PreferencesExtensions.java | 26 ++++++++-----
 .../vlc/gui/view/ClickableSwitchPreference.java    | 44 ++++++++++++++++++++++
 3 files changed, 62 insertions(+), 9 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesExtensionFragment.java b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesExtensionFragment.java
index 0d44f8117..3c7f9e0b7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesExtensionFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesExtensionFragment.java
@@ -96,6 +96,7 @@ public class PreferencesExtensionFragment extends BasePreferenceFragment {
             String key = mExtensionKey + "_" + ExtensionsManager.ANDROID_AUTO_SUFFIX;
             checkbox.setKey(key);
             checkbox.setChecked(switchPreference.isChecked() && mSettings.getBoolean(key, false));
+            checkbox.setEnabled(switchPreference.isChecked());
             preferences.add(checkbox);
             preferenceScreen.addPreference(checkbox);
         }
diff --git a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesExtensions.java b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesExtensions.java
index ff9c229e2..054cd641f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesExtensions.java
+++ b/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesExtensions.java
@@ -10,14 +10,17 @@ import android.support.design.widget.AppBarLayout;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceCategory;
 import android.support.v7.preference.PreferenceScreen;
-import android.support.v7.preference.SwitchPreferenceCompat;
+import android.support.v7.widget.SwitchCompat;
+import android.view.View;
 
 import org.videolan.vlc.R;
 import org.videolan.vlc.extensions.ExtensionListing;
 import org.videolan.vlc.extensions.ExtensionsManager;
+import org.videolan.vlc.gui.view.ClickableSwitchPreference;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 public class PreferencesExtensions extends BasePreferenceFragment {
 
@@ -61,10 +64,11 @@ public class PreferencesExtensions extends BasePreferenceFragment {
         PackageManager pm = getActivity().getApplicationContext().getPackageManager();
         for (int i = 0 ; i < mExtensions.size() ; ++i) {
             ExtensionListing extension = mExtensions.get(i);
-            SwitchPreferenceCompat switchPreference = new SwitchPreferenceCompat(preferenceScreen.getContext());
+            ClickableSwitchPreference switchPreference = new ClickableSwitchPreference(preferenceScreen.getContext());
             switchPreference.setTitle(extension.title());
             switchPreference.setSummary(extension.description());
-            switchPreference.setKey(ExtensionsManager.EXTENSION_PREFIX + "_" + extension.componentName().getPackageName());
+            final String key = ExtensionsManager.EXTENSION_PREFIX + "_" + extension.componentName().getPackageName();
+            switchPreference.setKey(key);
             int iconRes = extension.menuIcon();
             Drawable extensionIcon = null;
             if (iconRes != 0) {
@@ -81,16 +85,20 @@ public class PreferencesExtensions extends BasePreferenceFragment {
                 } catch (PackageManager.NameNotFoundException e) {
                     switchPreference.setIcon(R.drawable.icon);
                 }
-            final boolean checked = mSettings.getBoolean(ExtensionsManager.EXTENSION_PREFIX + "_" + extension.componentName().getPackageName(), false);
+            final boolean checked = mSettings.getBoolean(key, false);
             switchPreference.setChecked(checked);
-            switchPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+            preferenceScreen.addPreference(switchPreference);
+            switchPreference.setOnSwitchClickListener(new View.OnClickListener() {
                 @Override
-                public boolean onPreferenceClick(Preference preference) {
-                    ((SwitchPreferenceCompat) preference).setChecked(true);
-                    return false;
+                public void onClick(View view) {
+                    if (((SwitchCompat)view).isChecked())
+                        mSettings.edit().putBoolean(key, true).apply();
+                    else
+                        for (Map.Entry<String, ?> entry : mSettings.getAll().entrySet())
+                            if (entry.getKey().startsWith(ExtensionsManager.EXTENSION_PREFIX + "_"))
+                                mSettings.edit().putBoolean(entry.getKey(), false).apply();
                 }
             });
-            preferenceScreen.addPreference(switchPreference);
             count++;
         }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/view/ClickableSwitchPreference.java b/vlc-android/src/org/videolan/vlc/gui/view/ClickableSwitchPreference.java
new file mode 100644
index 000000000..35ed6d16a
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/view/ClickableSwitchPreference.java
@@ -0,0 +1,44 @@
+package org.videolan.vlc.gui.view;
+
+import android.content.Context;
+import android.support.v7.preference.PreferenceViewHolder;
+import android.support.v7.preference.TwoStatePreference;
+import android.support.v7.widget.SwitchCompat;
+import android.view.View;
+import android.widget.CompoundButton;
+
+public class ClickableSwitchPreference extends TwoStatePreference {
+
+    private View switchView;
+    private View.OnClickListener switchClickListener;
+
+    public ClickableSwitchPreference(Context context) {
+        super(context, null, android.support.v7.preference.R.attr.switchPreferenceCompatStyle, 0);
+    }
+
+    @Override
+    public void onBindViewHolder(PreferenceViewHolder holder) {
+        super.onBindViewHolder(holder);
+        switchView = holder.findViewById(android.support.v7.preference.R.id.switchWidget);
+        switchView.setOnClickListener(switchClickListener);
+
+        //for some reason, it does not initialize itself;
+        ((SwitchCompat) switchView).setChecked(isChecked());
+
+        ((SwitchCompat)switchView).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+            @Override
+            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
+
+            }
+        });
+    }
+
+    public void setOnSwitchClickListener(View.OnClickListener listener) {
+        switchClickListener = listener;
+    }
+
+    @Override
+    protected void onClick() {
+        //Do not call super.onClick();
+    }
+}
\ No newline at end of file



More information about the Android mailing list