Android Open Source - WarDroid Multi Select List Preference






From Project

Back to project page WarDroid.

License

The source code is released under:

MIT License

If you think the Android project WarDroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2013 Alex Yanchenko//from w w w. j ava 2 s .com
 *
 * 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 com.deathsnacks.wardroid.utils.preferences;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.widget.ListView;

import com.deathsnacks.wardroid.R;
import com.deathsnacks.wardroid.utils.PreferenceUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

// android:defaultValue="entryValue1|entryValue2"
public class MultiSelectListPreference extends ListPreference {

    public CharSequence[] getCheckedEntries() {
        CharSequence[] entries = getEntries();
        ArrayList<CharSequence> checkedEntries = new ArrayList<CharSequence>();
        for (int i = 0; i < entries.length; i++) {
            if (checkedEntryIndexes[i]) {
                checkedEntries.add(entries[i]);
            }
        }
        return checkedEntries.toArray(new String[checkedEntries.size()]);
    }

    // boring stuff

    private boolean[] checkedEntryIndexes;
    private boolean enableToggle;
    private Context context;

    public MultiSelectListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.context = context;
        enableToggle = true;
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String attr = attrs.getAttributeName(i);
            String val = attrs.getAttributeValue(i);
            if (attr.equalsIgnoreCase("toggleAll")) {
                if (val.equalsIgnoreCase("false"))
                    enableToggle = false;
            }
        }
    }

    public MultiSelectListPreference(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public void setEntries(CharSequence[] entries) {
        super.setEntries(entries);
        updateCheckedEntryIndexes();
    }

    @Override
    public void setValue(String value) {
        super.setValue(value);
        updateCheckedEntryIndexes();
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        updateCheckedEntryIndexes();
        List<CharSequence> entriesList = new ArrayList<CharSequence>();
        entriesList.addAll(Arrays.asList(getEntries()));
        CharSequence[] entries = entriesList.toArray(new CharSequence[entriesList.size()]);
        builder.setMultiChoiceItems(entries, checkedEntryIndexes,
                new OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which,
                                        boolean isChecked) {
                        if (enableToggle) {
                            if (which == 0) {
                                ListView list = ((AlertDialog) dialog).getListView();
                                for (int i = 1; i < list.getCount(); i++) {
                                    list.setItemChecked(i, isChecked);
                                    checkedEntryIndexes[i] = isChecked;
                                }
                                return;
                            }
                        }
                        checkedEntryIndexes[which] = isChecked;
                    }
                });
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            CharSequence[] entryVals = getEntryValues();
            ArrayList<CharSequence> checkedVals = new ArrayList<CharSequence>();
            for (int i = enableToggle ? 1 : 0; i < entryVals.length; i++) {
                if (checkedEntryIndexes[i]) {
                    checkedVals.add(entryVals[i]);
                }
            }
            String val = PreferenceUtils.toPersistedPreferenceValue(checkedVals
                    .toArray(new CharSequence[checkedVals.size()]));
            if (callChangeListener(val)) {
                setValue(val);
            }
        }
    }

    private void updateCheckedEntryIndexes() {
        CharSequence[] entryVals = getEntryValues();
        checkedEntryIndexes = new boolean[entryVals.length];
        String val = getValue();
        if (val != null) {
            HashSet<String> checkedEntryVals = new HashSet<String>(
                    Arrays.asList(PreferenceUtils.fromPersistedPreferenceValue(val)));
            for (int i = 0; i < entryVals.length; i++) {
                checkedEntryIndexes[i] = checkedEntryVals
                        .contains(entryVals[i]);
            }
        }
    }

    @Override
    public CharSequence[] getEntries() {
        List<CharSequence> entries = new ArrayList<CharSequence>();
        if (enableToggle)
            entries.add(context.getString(R.string.toggle_all));
        entries.addAll(Arrays.asList(super.getEntries()));
        return entries.toArray(new CharSequence[entries.size()]);
    }

    @Override
    public CharSequence[] getEntryValues() {
        List<CharSequence> entries = new ArrayList<CharSequence>();
        if (enableToggle)
            entries.add(context.getString(R.string.toggle_all));
        entries.addAll(Arrays.asList(super.getEntryValues()));
        return entries.toArray(new CharSequence[entries.size()]);
    }
}




Java Source Code List

afzkl.development.colorpickerview.dialog.ColorPickerDialog.java
afzkl.development.colorpickerview.drawable.AlphaPatternDrawable.java
afzkl.development.colorpickerview.preference.ColorPickerPreference.java
afzkl.development.colorpickerview.view.ColorPanelView.java
afzkl.development.colorpickerview.view.ColorPickerView.java
android.support.v4.preference.PreferenceFragment.java
android.support.v4.preference.PreferenceManagerCompat.java
com.deathsnacks.wardroid.Constants.java
com.deathsnacks.wardroid.activities.CustomFilterActivity.java
com.deathsnacks.wardroid.activities.MainActivity.java
com.deathsnacks.wardroid.activities.SettingsActivity.java
com.deathsnacks.wardroid.adapters.AlertsListViewAdapter.java
com.deathsnacks.wardroid.adapters.BadlandsListViewAdapter.java
com.deathsnacks.wardroid.adapters.InvasionListViewAdapter.java
com.deathsnacks.wardroid.adapters.NewsListViewAdapter.java
com.deathsnacks.wardroid.adapters.SeparatedListAdapter.java
com.deathsnacks.wardroid.fragments.AlertsFragment.java
com.deathsnacks.wardroid.fragments.BadlandDialogFragment.java
com.deathsnacks.wardroid.fragments.BadlandsFragment.java
com.deathsnacks.wardroid.fragments.InvasionFragment.java
com.deathsnacks.wardroid.fragments.NewsFragment.java
com.deathsnacks.wardroid.fragments.SettingsFragment.java
com.deathsnacks.wardroid.gson.alert.Activation.java
com.deathsnacks.wardroid.gson.alert.Alert.java
com.deathsnacks.wardroid.gson.alert.CountedItem.java
com.deathsnacks.wardroid.gson.alert.Expiry.java
com.deathsnacks.wardroid.gson.alert.MissionInfo.java
com.deathsnacks.wardroid.gson.alert.MissionReward.java
com.deathsnacks.wardroid.gson.alert._id.java
com.deathsnacks.wardroid.gson.badlands.AttId.java
com.deathsnacks.wardroid.gson.badlands.AttackerInfo.java
com.deathsnacks.wardroid.gson.badlands.BadlandNode.java
com.deathsnacks.wardroid.gson.badlands.ConflictExpiration.java
com.deathsnacks.wardroid.gson.badlands.CountedItem.java
com.deathsnacks.wardroid.gson.badlands.DefId.java
com.deathsnacks.wardroid.gson.badlands.DefenderInfo.java
com.deathsnacks.wardroid.gson.badlands.DeploymentActivationTime.java
com.deathsnacks.wardroid.gson.badlands.End.java
com.deathsnacks.wardroid.gson.badlands.History.java
com.deathsnacks.wardroid.gson.badlands.Id.java
com.deathsnacks.wardroid.gson.badlands.LastHealTime.java
com.deathsnacks.wardroid.gson.badlands.MissionInfo.java
com.deathsnacks.wardroid.gson.badlands.MissionRewardExtra.java
com.deathsnacks.wardroid.gson.badlands.MissionReward.java
com.deathsnacks.wardroid.gson.badlands.Start.java
com.deathsnacks.wardroid.gson.badlands.TaxChangeAllowedTime.java
com.deathsnacks.wardroid.gson.badlands.WinId.java
com.deathsnacks.wardroid.gson.badlands._id.java
com.deathsnacks.wardroid.services.GcmBroadcastReceiver.java
com.deathsnacks.wardroid.services.NotificationsUpdateReceiver.java
com.deathsnacks.wardroid.services.PollingAlarmReceiver.java
com.deathsnacks.wardroid.services.UpdateReceiver.java
com.deathsnacks.wardroid.utils.GlobalApplication.java
com.deathsnacks.wardroid.utils.Http.java
com.deathsnacks.wardroid.utils.Names.java
com.deathsnacks.wardroid.utils.Platform.java
com.deathsnacks.wardroid.utils.PreferenceActivityCallbacks.java
com.deathsnacks.wardroid.utils.PreferenceUtils.java
com.deathsnacks.wardroid.utils.UnifiedPreferenceUtils.java
com.deathsnacks.wardroid.utils.Utils.java
com.deathsnacks.wardroid.utils.gcmclasses.Alert.java
com.deathsnacks.wardroid.utils.gcmclasses.Invasion.java
com.deathsnacks.wardroid.utils.httpclasses.Alert.java
com.deathsnacks.wardroid.utils.httpclasses.InvasionMini.java
com.deathsnacks.wardroid.utils.httpclasses.Invasion.java
com.deathsnacks.wardroid.utils.httpclasses.News.java
com.deathsnacks.wardroid.utils.httpclasses.Sale.java
com.deathsnacks.wardroid.utils.preferences.IntEditTextPreference.java
com.deathsnacks.wardroid.utils.preferences.MultiSelectListPreference.java