/* * Copyright (C) 2014 Hippo Seven * * 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.hippo.ehviewer.preference; import android.content.Context; import android.content.res.TypedArray; import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; import com.hippo.ehviewer.app.MaterialAlertDialog; import com.hippo.ehviewer.app.MaterialAlertDialog.Builder; public class ListPreference extends DialogPreference { private CharSequence[] mEntries; private CharSequence[] mEntryValues; private String mValue; private String mSummary; private int mClickedDialogEntryIndex; private boolean mValueSet; public ListPreference(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs, 0); } public ListPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs, defStyle); } private void init(Context context, AttributeSet attrs, int defStyle) { TypedArray a = context.obtainStyledAttributes(attrs, new int[]{ 16842930, 16843256 }, 0, 0); mEntries = a.getTextArray(0); mEntryValues = a.getTextArray(1); a.recycle(); a = context.obtainStyledAttributes(attrs, new int[]{ 16842754, 16842765, 16842766, 16842994, 16843233, 16843238, 16843240, 16843241, 16843242, 16843243, 16843244, 16843245, 16843246, 16843491 }, 0, 0); mSummary = a.getString(7); a.recycle(); } public void setEntries(CharSequence[] entries) { mEntries = entries; } public void setEntries(int entriesResId) { setEntries(getContext().getResources().getTextArray(entriesResId)); } public CharSequence[] getEntries() { return mEntries; } public void setEntryValues(CharSequence[] entryValues) { mEntryValues = entryValues; } public void setEntryValues(int entryValuesResId) { setEntryValues(getContext().getResources().getTextArray(entryValuesResId)); } public CharSequence[] getEntryValues() { return mEntryValues; } public void setValue(String value) { // Always persist/notify the first time. final boolean changed = !TextUtils.equals(mValue, value); if (changed || !mValueSet) { mValue = value; mValueSet = true; persistString(value); if (changed) { notifyChanged(); } } } @Override public CharSequence getSummary() { final CharSequence entry = getEntry(); if (entry == null) { return super.getSummary(); } else if (mSummary == null) { return entry; } else { return String.format(mSummary, entry); } } @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(); } } public void setValueIndex(int index) { if (mEntryValues != null) { setValue(mEntryValues[index].toString()); } } public String getValue() { return mValue; } public CharSequence getEntry() { int index = getValueIndex(); return index >= 0 && mEntries != null ? mEntries[index] : null; } public int findIndexOfValue(String value) { if (value != null && mEntryValues != null) { for (int i = mEntryValues.length - 1; i >= 0; i--) { if (mEntryValues[i].equals(value)) { return i; } } } return -1; } private int getValueIndex() { return findIndexOfValue(mValue); } @Override protected boolean inScrollView() { return false; } @Override protected void onBindDialogView(View view) { // Empty } @Override protected void onPrepareDialogBuilder(Builder builder) { mClickedDialogEntryIndex = getValueIndex(); builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, new MaterialAlertDialog.OnClickListener() { @Override public boolean onClick(MaterialAlertDialog dialog, int which) { mClickedDialogEntryIndex = which; ListPreference.this.onClick(dialog, MaterialAlertDialog.POSITIVE); return true; } }); } @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (positiveResult && mClickedDialogEntryIndex >= 0 && mEntryValues != null) { String value = mEntryValues[mClickedDialogEntryIndex].toString(); if (callChangeListener(value)) { setValue(value); } } } @Override protected Object onGetDefaultValue(TypedArray a, int index) { return a.getString(index); } @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { setValue(restoreValue ? getPersistedString(mValue) : (String) 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 { String value; public SavedState(Parcel source) { super(source); value = source.readString(); } @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeString(value); } public SavedState(Parcelable superState) { super(superState); } @SuppressWarnings("unused") public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { @Override public SavedState createFromParcel(Parcel in) { return new SavedState(in); } @Override public SavedState[] newArray(int size) { return new SavedState[size]; } }; } }