|
@@ -0,0 +1,168 @@
|
|
|
+package com.afollestad.materialdialogs.prefs;
|
|
|
+
|
|
|
+import android.annotation.TargetApi;
|
|
|
+import android.app.Dialog;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.DialogInterface;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.Parcel;
|
|
|
+import android.os.Parcelable;
|
|
|
+import android.preference.DialogPreference;
|
|
|
+import android.preference.ListPreference;
|
|
|
+import android.preference.PreferenceManager;
|
|
|
+import android.support.annotation.NonNull;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.util.AttributeSet;
|
|
|
+import android.view.View;
|
|
|
+
|
|
|
+import com.afollestad.materialdialogs.MaterialDialog;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Aidan Follestad (afollestad)
|
|
|
+ */
|
|
|
+public class MaterialDialogPreference extends DialogPreference {
|
|
|
+
|
|
|
+ private Context context;
|
|
|
+ private MaterialDialog mDialog;
|
|
|
+
|
|
|
+ @TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
|
+ public MaterialDialogPreference(Context context) {
|
|
|
+ super(context);
|
|
|
+ init(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ public MaterialDialogPreference(Context context, AttributeSet attrs) {
|
|
|
+ super(context, attrs);
|
|
|
+ init(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void init(Context context) {
|
|
|
+ this.context = context;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Dialog getDialog() {
|
|
|
+ return mDialog;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void showDialog(Bundle state) {
|
|
|
+ MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
|
|
|
+ .title(getDialogTitle())
|
|
|
+ .content(getDialogMessage())
|
|
|
+ .icon(getDialogIcon())
|
|
|
+ .positiveText(getPositiveButtonText())
|
|
|
+ .negativeText(getNegativeButtonText())
|
|
|
+ .autoDismiss(true); // immediately close the dialog after selection
|
|
|
+
|
|
|
+ final View contentView = onCreateDialogView();
|
|
|
+ if (contentView != null) {
|
|
|
+ onBindDialogView(contentView);
|
|
|
+ builder.customView(contentView, false);
|
|
|
+ } else {
|
|
|
+ builder.content(getDialogMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ PreferenceManager pm = getPreferenceManager();
|
|
|
+ Method method = pm.getClass().getDeclaredMethod(
|
|
|
+ "registerOnActivityDestroyListener",
|
|
|
+ PreferenceManager.OnActivityDestroyListener.class);
|
|
|
+ method.setAccessible(true);
|
|
|
+ method.invoke(pm, this);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+
|
|
|
+ mDialog = builder.build();
|
|
|
+ if (state != null)
|
|
|
+ mDialog.onRestoreInstanceState(state);
|
|
|
+ mDialog.show();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDismiss(DialogInterface dialog) {
|
|
|
+ super.onDismiss(dialog);
|
|
|
+ try {
|
|
|
+ PreferenceManager pm = getPreferenceManager();
|
|
|
+ Method method = pm.getClass().getDeclaredMethod(
|
|
|
+ "unregisterOnActivityDestroyListener",
|
|
|
+ PreferenceManager.OnActivityDestroyListener.class);
|
|
|
+ method.setAccessible(true);
|
|
|
+ method.invoke(pm, this);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @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(@NonNull 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 Creator<SavedState> CREATOR =
|
|
|
+ new Creator<SavedState>() {
|
|
|
+ public SavedState createFromParcel(Parcel in) {
|
|
|
+ return new SavedState(in);
|
|
|
+ }
|
|
|
+
|
|
|
+ public SavedState[] newArray(int size) {
|
|
|
+ return new SavedState[size];
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+}
|