Android Open Source - Dual-Battery-Widget Integer List Preference






From Project

Back to project page Dual-Battery-Widget.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions f...

If you think the Android project Dual-Battery-Widget 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 2011 Artiom Chilaru (http://flexlabs.org)
 *//from w  w  w.j  ava  2 s.c o  m
 * 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.flexlabs.widgets.dualbattery.ui;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import org.flexlabs.widgets.dualbattery.R;

public class IntegerListPreference extends DialogPreference {
    private CharSequence[] mEntries;
    private int[] mEntryValues;
    private int mValue;
    private String mSummary;
    private int mClickedDialogEntryIndex;

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

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IntListPreference, 0, 0);
        mEntries = a.getTextArray(R.styleable.IntListPreference_entries);

        int valuesResId = a.getResourceId(R.styleable.IntListPreference_entryValues, 0);
        if (valuesResId == 0) {
            throw new IllegalArgumentException("IntegerListPreference: error - valueList is not specified");
        }
        mEntryValues = a.getResources().getIntArray(valuesResId);

        mSummary = a.getString(R.styleable.IntListPreference_summary);
        a.recycle();
    }

    public IntegerListPreference(Context context) {
        this(context, null);
    }

    /**
     * Sets the human-readable entries to be shown in the list. This will be
     * shown in subsequent dialogs.
     * <p>
     * Each entry must have a corresponding index in
     * {@link #setEntryValues(int[])}.
     *
     * @param entries The entries.
     * @see #setEntryValues(int[])
     */
    public void setEntries(CharSequence[] entries) {
        mEntries = entries;
    }

    /**
     * @see #setEntries(CharSequence[])
     * @param entriesResId The entries array as a resource.
     */
    public void setEntries(int entriesResId) {
        setEntries(getContext().getResources().getTextArray(entriesResId));
    }

    /**
     * The list of entries to be shown in the list in subsequent dialogs.
     *
     * @return The list as an array.
     */
    public CharSequence[] getEntries() {
        return mEntries;
    }

    /**
     * The array to find the value to save for a preference when an entry from
     * entries is selected. If a user clicks on the second item in entries, the
     * second item in this array will be saved to the preference.
     *
     * @param entryValues The array to be used as values to save for the preference.
     */
    public void setEntryValues(int[] entryValues) {
        mEntryValues = entryValues;
    }

    /**
     * @see #setEntryValues(int[])
     * @param entryValuesResId The entry values array as a resource.
     */
    public void setEntryValues(int entryValuesResId) {
        setEntryValues(getContext().getResources().getIntArray(entryValuesResId));
    }

    /**
     * Returns the array of values to be saved for the preference.
     *
     * @return The array of values.
     */
    public int[] getEntryValues() {
        return mEntryValues;
    }

    /**
     * Sets the value of the key. This should be one of the entries in
     * {@link #getEntryValues()}.
     *
     * @param value The value to set for the key.
     */
    public void setValue(int value) {
        mValue = value;
        notifyChanged();
        persistInt(value);
    }

    /**
     * Returns the summary of this ListPreference. If the summary
     * has a {@linkplain java.lang.String#format String formatting}
     * marker in it (i.e. "%s" or "%1$s"), then the current entry
     * value will be substituted in its place.
     *
     * @return the summary with appropriate string substitution
     */
    @Override
    public CharSequence getSummary() {
        final CharSequence entry = getEntry();
        if (mSummary == null) {
            return super.getSummary();
        } else {
            return String.format(mSummary, entry);
        }
    }

    /**
     * Sets the summary for this Preference with a CharSequence.
     * If the summary has a
     * {@linkplain java.lang.String#format String formatting}
     * marker in it (i.e. "%s" or "%1$s"), then the current entry
     * value will be substituted in its place when it's retrieved.
     *
     * @param summary The summary for the preference.
     */
    @Override
    public void setSummary(CharSequence summary) {
        super.setSummary(summary);
        if (summary == null && mSummary != null) {
            mSummary = null;
        } else if (summary != null && !summary.equals(mSummary)) {
            mSummary = summary.toString();
        }
    }

    /**
     * Sets the value to the given index from the entry values.
     *
     * @param index The index of the value to set.
     */
    public void setValueIndex(int index) {
        if (mEntryValues != null) {
            setValue(mEntryValues[index]);
        }
    }

    /**
     * Returns the value of the key. This should be one of the entries in
     * {@link #getEntryValues()}.
     *
     * @return The value of the key.
     */
    public int getValue() {
        return mValue;
    }

    /**
     * Returns the entry corresponding to the current value.
     *
     * @return The entry corresponding to the current value, or null.
     */
    public CharSequence getEntry() {
        int index = getValueIndex();
        return index >= 0 && mEntries != null ? mEntries[index] : null;
    }

    /**
     * Returns the index of the given value (in the entry values array).
     *
     * @param value The value whose index should be returned.
     * @return The index of the value, or -1 if not found.
     */
    public int findIndexOfValue(int value) {
        if (mEntryValues != null) {
            for (int i = mEntryValues.length - 1; i >= 0; i--) {
                if (mEntryValues[i]==value) {
                    return i;
                }
            }
        }
        return -1;
    }

    private int getValueIndex() {
        return findIndexOfValue(mValue);
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        super.onPrepareDialogBuilder(builder);

        if (mEntries == null || mEntryValues == null) {
            throw new IllegalStateException("ListPreference requires an entries array and an entryValues array.");
        }

        mClickedDialogEntryIndex = getValueIndex();
        builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        mClickedDialogEntryIndex = which;

                        /*
                         * Clicking on an item simulates the positive button
                         * click, and dismisses the dialog.
                         */
                        IntegerListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        dialog.dismiss();
                    }
        });

        /*
         * The typical interaction for list-based dialogs is to have
         * click-on-an-item dismiss the dialog instead of the user having to
         * press 'Ok'.
         */
        builder.setPositiveButton(null, null);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if (positiveResult && mClickedDialogEntryIndex >= 0 && mEntryValues != null) {
            int value = mEntryValues[mClickedDialogEntryIndex];
            if (callChangeListener(value)) {
                setValue(value);
            }
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInt(index, 1);
    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        setValue(restoreValue ? getPersistedInt(mValue) : (Integer) defaultValue);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        final Parcelable superState = super.onSaveInstanceState();
        if (isPersistent()) {
            // No need to save instance state since it's persistent
            return superState;
        }

        final SavedState myState = new SavedState(superState);
        myState.value = getValue();
        return myState;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state == null || !state.getClass().equals(SavedState.class)) {
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            return;
        }

        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        setValue(myState.value);
    }

    private static class SavedState extends BaseSavedState {
        int value;

        public SavedState(Parcel source) {
            super(source);
            value = source.readInt();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(value);
        }

        public SavedState(Parcelable superState) {
            super(superState);
        }

        public static final Parcelable.Creator<SavedState> CREATOR =
                new Parcelable.Creator<SavedState>() {
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }

}




Java Source Code List

org.achartengine.ChartFactory.java
org.achartengine.GraphicalActivity.java
org.achartengine.GraphicalView.java
org.achartengine.ITouchHandler.java
org.achartengine.TouchHandlerOld.java
org.achartengine.TouchHandler.java
org.achartengine.chart.AbstractChart.java
org.achartengine.chart.BarChart.java
org.achartengine.chart.BubbleChart.java
org.achartengine.chart.ClickableArea.java
org.achartengine.chart.CombinedXYChart.java
org.achartengine.chart.CubicLineChart.java
org.achartengine.chart.DialChart.java
org.achartengine.chart.DoughnutChart.java
org.achartengine.chart.LineChart.java
org.achartengine.chart.PieChart.java
org.achartengine.chart.PieMapper.java
org.achartengine.chart.PieSegment.java
org.achartengine.chart.PointStyle.java
org.achartengine.chart.RangeBarChart.java
org.achartengine.chart.RangeStackedBarChart.java
org.achartengine.chart.RoundChart.java
org.achartengine.chart.ScatterChart.java
org.achartengine.chart.TimeChart.java
org.achartengine.chart.XYChart.java
org.achartengine.model.CategorySeries.java
org.achartengine.model.MultipleCategorySeries.java
org.achartengine.model.Point.java
org.achartengine.model.RangeCategorySeries.java
org.achartengine.model.SeriesSelection.java
org.achartengine.model.TimeSeries.java
org.achartengine.model.XYMultipleSeriesDataset.java
org.achartengine.model.XYSeries.java
org.achartengine.model.XYValueSeries.java
org.achartengine.renderer.BasicStroke.java
org.achartengine.renderer.DefaultRenderer.java
org.achartengine.renderer.DialRenderer.java
org.achartengine.renderer.SimpleSeriesRenderer.java
org.achartengine.renderer.XYMultipleSeriesRenderer.java
org.achartengine.renderer.XYSeriesRenderer.java
org.achartengine.tools.AbstractTool.java
org.achartengine.tools.FitZoom.java
org.achartengine.tools.PanListener.java
org.achartengine.tools.Pan.java
org.achartengine.tools.ZoomEvent.java
org.achartengine.tools.ZoomListener.java
org.achartengine.tools.Zoom.java
org.achartengine.util.IndexXYMap.java
org.achartengine.util.MathHelper.java
org.achartengine.util.XYEntry.java
org.flexlabs.widgets.dualbattery.BatteryApplication.java
org.flexlabs.widgets.dualbattery.BatteryLevel.java
org.flexlabs.widgets.dualbattery.BatteryWidget1x1.java
org.flexlabs.widgets.dualbattery.BatteryWidget2x2.java
org.flexlabs.widgets.dualbattery.BatteryWidget3x4.java
org.flexlabs.widgets.dualbattery.BatteryWidgetUpdater.java
org.flexlabs.widgets.dualbattery.BatteryWidget.java
org.flexlabs.widgets.dualbattery.BillingObserver.java
org.flexlabs.widgets.dualbattery.Constants.java
org.flexlabs.widgets.dualbattery.app.AboutFragment.java
org.flexlabs.widgets.dualbattery.app.BatteryHistoryActivity.java
org.flexlabs.widgets.dualbattery.app.DonateFragment.java
org.flexlabs.widgets.dualbattery.app.FeedbackFragment.java
org.flexlabs.widgets.dualbattery.app.SettingsActivity.java
org.flexlabs.widgets.dualbattery.app.SettingsContainer.java
org.flexlabs.widgets.dualbattery.app.SettingsFragment.java
org.flexlabs.widgets.dualbattery.service.BootUpReceiver.java
org.flexlabs.widgets.dualbattery.service.IntentReceiver.java
org.flexlabs.widgets.dualbattery.service.MonitorService.java
org.flexlabs.widgets.dualbattery.service.NotificationManager.java
org.flexlabs.widgets.dualbattery.storage.BatteryLevelAdapter.java
org.flexlabs.widgets.dualbattery.ui.IntegerListPreference.java
org.flexlabs.widgets.dualbattery.ui.PreferenceListFragment.java
org.flexlabs.widgets.dualbattery.ui.SeekBarPreference.java
org.flexlabs.widgets.dualbattery.widgetsettings.BatteryInfoFragment.java
org.flexlabs.widgets.dualbattery.widgetsettings.PropertiesFragment.java
org.flexlabs.widgets.dualbattery.widgetsettings.WidgetActivity.java
org.flexlabs.widgets.dualbattery.widgetsettings.WidgetSettingsContainer.java