Search in sources :

Example 1 with ClickableSwitchPreference

use of org.videolan.vlc.gui.view.ClickableSwitchPreference in project vlc-android by GeoffreyMetais.

the class PreferencesExtensions method createCheckboxes.

private void createCheckboxes() {
    PackageManager pm = getActivity().getApplicationContext().getPackageManager();
    for (int i = 0; i < mExtensions.size(); ++i) {
        ExtensionListing extension = mExtensions.get(i);
        ClickableSwitchPreference switchPreference = new ClickableSwitchPreference(preferenceScreen.getContext());
        switchPreference.setTitle(extension.title());
        switchPreference.setSummary(extension.description());
        final String key = ExtensionsManager.EXTENSION_PREFIX + "_" + extension.componentName().getPackageName();
        switchPreference.setKey(key);
        int iconRes = extension.menuIcon();
        Drawable extensionIcon = null;
        if (iconRes != 0) {
            try {
                Resources res = pm.getResourcesForApplication(extension.componentName().getPackageName());
                extensionIcon = res.getDrawable(extension.menuIcon());
            } catch (PackageManager.NameNotFoundException e) {
            }
        }
        if (extensionIcon != null)
            switchPreference.setIcon(extensionIcon);
        else
            try {
                switchPreference.setIcon(pm.getApplicationIcon(mExtensions.get(i).componentName().getPackageName()));
            } catch (PackageManager.NameNotFoundException e) {
                switchPreference.setIcon(R.drawable.icon);
            }
        final boolean checked = mSettings.getBoolean(key, false);
        switchPreference.setChecked(checked);
        preferenceScreen.addPreference(switchPreference);
        switchPreference.setOnSwitchClickListener(new View.OnClickListener() {

            @Override
            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();
            }
        });
        count++;
    }
    if (count == 0) {
        PreferenceCategory emptyCategory = new PreferenceCategory(preferenceScreen.getContext());
        emptyCategory.setTitle(R.string.extensions_empty);
        preferenceScreen.addPreference(emptyCategory);
    }
}
Also used : Drawable(android.graphics.drawable.Drawable) View(android.view.View) ExtensionListing(org.videolan.vlc.extensions.ExtensionListing) PackageManager(android.content.pm.PackageManager) PreferenceCategory(android.support.v7.preference.PreferenceCategory) Resources(android.content.res.Resources) Map(java.util.Map) ClickableSwitchPreference(org.videolan.vlc.gui.view.ClickableSwitchPreference)

Example 2 with ClickableSwitchPreference

use of org.videolan.vlc.gui.view.ClickableSwitchPreference in project vlc-android by videolan.

the class PreferencesExtensions method createCheckboxes.

private void createCheckboxes() {
    PackageManager pm = getActivity().getApplicationContext().getPackageManager();
    for (int i = 0; i < mExtensions.size(); ++i) {
        ExtensionListing extension = mExtensions.get(i);
        ClickableSwitchPreference switchPreference = new ClickableSwitchPreference(preferenceScreen.getContext());
        switchPreference.setTitle(extension.title());
        switchPreference.setSummary(extension.description());
        final String key = ExtensionsManager.EXTENSION_PREFIX + "_" + extension.componentName().getPackageName();
        switchPreference.setKey(key);
        int iconRes = extension.menuIcon();
        Drawable extensionIcon = null;
        if (iconRes != 0) {
            try {
                Resources res = pm.getResourcesForApplication(extension.componentName().getPackageName());
                extensionIcon = res.getDrawable(extension.menuIcon());
            } catch (PackageManager.NameNotFoundException e) {
            }
        }
        if (extensionIcon != null)
            switchPreference.setIcon(extensionIcon);
        else
            try {
                switchPreference.setIcon(pm.getApplicationIcon(mExtensions.get(i).componentName().getPackageName()));
            } catch (PackageManager.NameNotFoundException e) {
                switchPreference.setIcon(R.drawable.icon);
            }
        final boolean checked = mSettings.getBoolean(key, false);
        switchPreference.setChecked(checked);
        preferenceScreen.addPreference(switchPreference);
        switchPreference.setOnSwitchClickListener(new View.OnClickListener() {

            @Override
            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();
            }
        });
        count++;
    }
    if (count == 0) {
        PreferenceCategory emptyCategory = new PreferenceCategory(preferenceScreen.getContext());
        emptyCategory.setTitle(R.string.extensions_empty);
        preferenceScreen.addPreference(emptyCategory);
    }
}
Also used : Drawable(android.graphics.drawable.Drawable) View(android.view.View) ExtensionListing(org.videolan.vlc.extensions.ExtensionListing) PackageManager(android.content.pm.PackageManager) PreferenceCategory(android.support.v7.preference.PreferenceCategory) Resources(android.content.res.Resources) Map(java.util.Map) ClickableSwitchPreference(org.videolan.vlc.gui.view.ClickableSwitchPreference)

Aggregations

PackageManager (android.content.pm.PackageManager)2 Resources (android.content.res.Resources)2 Drawable (android.graphics.drawable.Drawable)2 PreferenceCategory (android.support.v7.preference.PreferenceCategory)2 View (android.view.View)2 Map (java.util.Map)2 ExtensionListing (org.videolan.vlc.extensions.ExtensionListing)2 ClickableSwitchPreference (org.videolan.vlc.gui.view.ClickableSwitchPreference)2