Last active
August 29, 2015 14:22
-
-
Save yukuku/99faed8f8bf48bb33294 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Dialog; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.os.Parcel; | |
import android.os.Parcelable; | |
import android.preference.DialogPreference; | |
import android.preference.PreferenceManager; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import com.afollestad.materialdialogs.MaterialDialog; | |
import com.afollestad.materialdialogs.MaterialDialog.Builder; | |
import java.lang.reflect.Method; | |
/** | |
* @author Aidan Follestad (afollestad) | |
*/ | |
public class MaterialDialogPreference extends DialogPreference { | |
private MaterialDialog mDialog; | |
public MaterialDialogPreference(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public MaterialDialogPreference(Context context) { | |
this(context, null); | |
} | |
@Override | |
public Dialog getDialog() { | |
return mDialog; | |
} | |
@Override | |
protected void showDialog(Bundle state) { | |
Builder mBuilder = new MaterialDialog.Builder(getContext()) | |
.title(getDialogTitle()) | |
.icon(getDialogIcon()) | |
.positiveText(getPositiveButtonText()) | |
.negativeText(getNegativeButtonText()) | |
.callback(callback) | |
.dismissListener(this); | |
View contentView = onCreateDialogView(); | |
if (contentView != null) { | |
onBindDialogView(contentView); | |
mBuilder.customView(contentView, false); | |
} | |
PreferenceManager pm = getPreferenceManager(); | |
try { | |
Method method = pm.getClass().getDeclaredMethod( | |
"registerOnActivityDestroyListener", | |
PreferenceManager.OnActivityDestroyListener.class); | |
method.setAccessible(true); | |
method.invoke(pm, this); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
mDialog = mBuilder.build(); | |
if (state != null) | |
mDialog.onRestoreInstanceState(state); | |
requestInputMethod(mDialog); | |
mDialog.show(); | |
} | |
/** | |
* Callback listener for the MaterialDialog. Positive button checks with | |
* OnPreferenceChangeListener before committing user entered text | |
*/ | |
private final MaterialDialog.ButtonCallback callback = new MaterialDialog.ButtonCallback() { | |
@Override | |
public void onPositive(MaterialDialog dialog) { | |
onDialogClosed(true); | |
} | |
@Override | |
public void onNegative(final MaterialDialog dialog) { | |
onDialogClosed(false); | |
} | |
}; | |
/** | |
* Copied from DialogPreference.java | |
*/ | |
private void requestInputMethod(Dialog dialog) { | |
Window window = dialog.getWindow(); | |
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); | |
} | |
@Override | |
public void onActivityDestroy() { | |
super.onActivityDestroy(); | |
if (mDialog != null && mDialog.isShowing()) | |
mDialog.dismiss(); | |
} | |
@Override | |
protected Parcelable onSaveInstanceState() { | |
final Parcelable superState = super.onSaveInstanceState(); | |
Dialog dialog = getDialog(); | |
if (dialog == null || !dialog.isShowing()) { | |
return superState; | |
} | |
final SavedState myState = new SavedState(superState); | |
myState.isDialogShowing = true; | |
myState.dialogBundle = dialog.onSaveInstanceState(); | |
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()); | |
if (myState.isDialogShowing) { | |
showDialog(myState.dialogBundle); | |
} | |
} | |
// From DialogPreference | |
private static class SavedState extends BaseSavedState { | |
boolean isDialogShowing; | |
Bundle dialogBundle; | |
public SavedState(Parcel source) { | |
super(source); | |
isDialogShowing = source.readInt() == 1; | |
dialogBundle = source.readBundle(); | |
} | |
@Override | |
public void writeToParcel(Parcel dest, int flags) { | |
super.writeToParcel(dest, flags); | |
dest.writeInt(isDialogShowing ? 1 : 0); | |
dest.writeBundle(dialogBundle); | |
} | |
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]; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment