// Path: app/src/main/java/com/hippo/nimingban/preference/PreferenceUtils.java
// public final class PreferenceUtils {
//     private PreferenceUtils() {}
// 
//     private static final Method mRegisterOnActivityDestroyListener;
//     private static final Method mUnregisterOnActivityDestroyListener;
// 
//     static {
//         Method method;
//         Class<?> clazz = PreferenceManager.class;
// 
//         method = null;
//         try {
//             method = clazz.getDeclaredMethod("registerOnActivityDestroyListener",
//                     PreferenceManager.OnActivityDestroyListener.class);
//             if (null != method) {
//                 method.setAccessible(true);
//             }
//         } catch (NoSuchMethodException e) {
//             e.printStackTrace();
//         }
//         mRegisterOnActivityDestroyListener = method;
// 
//         method = null;
//         try {
//             method = clazz.getDeclaredMethod("unregisterOnActivityDestroyListener",
//                     PreferenceManager.OnActivityDestroyListener.class);
//             if (null != method) {
//                 method.setAccessible(true);
//             }
//         } catch (NoSuchMethodException e) {
//             e.printStackTrace();
//         }
//         mUnregisterOnActivityDestroyListener = method;
//     }
// 
//     @SuppressWarnings("TryWithIdenticalCatches")
//     public static void registerOnActivityDestroyListener(Preference preference,
//             PreferenceManager.OnActivityDestroyListener listener) {
//         if (null == mRegisterOnActivityDestroyListener || null == preference || null == listener) {
//             return;
//         }
//         PreferenceManager preferenceManager = preference.getPreferenceManager();
//         if (null == preferenceManager) {
//             return;
//         }
// 
//         try {
//             mRegisterOnActivityDestroyListener.invoke(preferenceManager, listener);
//         } catch (IllegalAccessException e) {
//             e.printStackTrace();
//         } catch (InvocationTargetException e) {
//             e.printStackTrace();
//         }
//     }
// 
//     @SuppressWarnings("TryWithIdenticalCatches")
//     public static void unregisterOnActivityDestroyListener(Preference preference,
//             PreferenceManager.OnActivityDestroyListener listener) {
//         if (null == mUnregisterOnActivityDestroyListener || null == preference || null == listener) {
//             return;
//         }
//         PreferenceManager preferenceManager = preference.getPreferenceManager();
//         if (null == preferenceManager) {
//             return;
//         }
// 
//         try {
//             mUnregisterOnActivityDestroyListener.invoke(preferenceManager, listener);
//         } catch (IllegalAccessException e) {
//             e.printStackTrace();
//         } catch (InvocationTargetException e) {
//             e.printStackTrace();
//         }
//     }
// }


import android.view.WindowManager;
import com.hippo.nimingban.preference.PreferenceUtils;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.util.AttributeSet;
import android.view.AbsSavedState;
import android.view.Window;
/*
 * Copyright 2019 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.preference;


/**
 * A base class for {@link Preference} objects that are
 * dialog-based. These preferences will, when clicked, open a dialog showing the
 * actual preference controls.
 */
public abstract class DialogPreference extends Preference implements
        DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
        PreferenceManager.OnActivityDestroyListener {

    /** The dialog, if it is showing. */
    private AlertDialog mDialog;

    /** Which button was clicked. */
    private int mWhichButtonClicked;

    public DialogPreference(Context context) {
        super(context);
    }

    public DialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * Prepares the dialog builder to be shown when the preference is clicked.
     * Use this to set custom properties on the dialog.
     * <p>
     * Do not {@link AlertDialog.Builder#create()} or
     * {@link AlertDialog.Builder#show()}.
     */
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    }

    @Override
    protected void onClick() {
        if (mDialog != null && mDialog.isShowing()) return;

        showDialog(null);
    }

    /**
     * Shows the dialog associated with this Preference. This is normally initiated
     * automatically on clicking on the preference. Call this method if you need to
     * show the dialog on some other event.
     *
     * @param state Optional instance state to restore on the dialog
     */
    protected void showDialog(Bundle state) {
        mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE;

// Path: app/src/main/java/com/hippo/nimingban/preference/PreferenceUtils.java
// public final class PreferenceUtils {
//     private PreferenceUtils() {}
// 
//     private static final Method mRegisterOnActivityDestroyListener;
//     private static final Method mUnregisterOnActivityDestroyListener;
// 
//     static {
//         Method method;
//         Class<?> clazz = PreferenceManager.class;
// 
//         method = null;
//         try {
//             method = clazz.getDeclaredMethod("registerOnActivityDestroyListener",
//                     PreferenceManager.OnActivityDestroyListener.class);
//             if (null != method) {
//                 method.setAccessible(true);
//             }
//         } catch (NoSuchMethodException e) {
//             e.printStackTrace();
//         }
//         mRegisterOnActivityDestroyListener = method;
// 
//         method = null;
//         try {
//             method = clazz.getDeclaredMethod("unregisterOnActivityDestroyListener",
//                     PreferenceManager.OnActivityDestroyListener.class);
//             if (null != method) {
//                 method.setAccessible(true);
//             }
//         } catch (NoSuchMethodException e) {
//             e.printStackTrace();
//         }
//         mUnregisterOnActivityDestroyListener = method;
//     }
// 
//     @SuppressWarnings("TryWithIdenticalCatches")
//     public static void registerOnActivityDestroyListener(Preference preference,
//             PreferenceManager.OnActivityDestroyListener listener) {
//         if (null == mRegisterOnActivityDestroyListener || null == preference || null == listener) {
//             return;
//         }
//         PreferenceManager preferenceManager = preference.getPreferenceManager();
//         if (null == preferenceManager) {
//             return;
//         }
// 
//         try {
//             mRegisterOnActivityDestroyListener.invoke(preferenceManager, listener);
//         } catch (IllegalAccessException e) {
//             e.printStackTrace();
//         } catch (InvocationTargetException e) {
//             e.printStackTrace();
//         }
//     }
// 
//     @SuppressWarnings("TryWithIdenticalCatches")
//     public static void unregisterOnActivityDestroyListener(Preference preference,
//             PreferenceManager.OnActivityDestroyListener listener) {
//         if (null == mUnregisterOnActivityDestroyListener || null == preference || null == listener) {
//             return;
//         }
//         PreferenceManager preferenceManager = preference.getPreferenceManager();
//         if (null == preferenceManager) {
//             return;
//         }
// 
//         try {
//             mUnregisterOnActivityDestroyListener.invoke(preferenceManager, listener);
//         } catch (IllegalAccessException e) {
//             e.printStackTrace();
//         } catch (InvocationTargetException e) {
//             e.printStackTrace();
//         }
//     }
// }


// Path: app/src/main/java/com/hippo/preference/DialogPreference.java
import android.view.WindowManager;
import com.hippo.nimingban.preference.PreferenceUtils;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.util.AttributeSet;
import android.view.AbsSavedState;
import android.view.Window;

/*
 * Copyright 2019 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.preference;


/**
 * A base class for {@link Preference} objects that are
 * dialog-based. These preferences will, when clicked, open a dialog showing the
 * actual preference controls.
 */
public abstract class DialogPreference extends Preference implements
        DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
        PreferenceManager.OnActivityDestroyListener {

    /** The dialog, if it is showing. */
    private AlertDialog mDialog;

    /** Which button was clicked. */
    private int mWhichButtonClicked;

    public DialogPreference(Context context) {
        super(context);
    }

    public DialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * Prepares the dialog builder to be shown when the preference is clicked.
     * Use this to set custom properties on the dialog.
     * <p>
     * Do not {@link AlertDialog.Builder#create()} or
     * {@link AlertDialog.Builder#show()}.
     */
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    }

    @Override
    protected void onClick() {
        if (mDialog != null && mDialog.isShowing()) return;

        showDialog(null);
    }

    /**
     * Shows the dialog associated with this Preference. This is normally initiated
     * automatically on clicking on the preference. Call this method if you need to
     * show the dialog on some other event.
     *
     * @param state Optional instance state to restore on the dialog
     */
    protected void showDialog(Bundle state) {
        mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE;

        PreferenceUtils.registerOnActivityDestroyListener(this, this);