me.panpf.tool4a.app.MessageDialogFragment.java Source code

Java tutorial

Introduction

Here is the source code for me.panpf.tool4a.app.MessageDialogFragment.java

Source

/*
 * Copyright (C) 2017 Peng fei Pan <sky@panpf.me>
 * 
 * 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 me.panpf.tool4a.app;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class MessageDialogFragment extends DialogFragment {
    public static String FRAGMENT_TAG_MESSAGE_DIALOG = "FRAGMENT_TAG_MESSAGE_DIALOG";

    private Builder builder;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog messageDialog = new AlertDialog.Builder(getActivity()).create();
        applyParams(messageDialog);
        return messageDialog;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (builder.onDismissListener != null) {
            builder.onDismissListener.onDismiss(dialog);
        }
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        if (builder.onCancelListener != null) {
            builder.onCancelListener.onCancel(dialog);
        }
    }

    /**
     * Builder
     *
     * @param builder Builder
     */
    private void setBuilder(Builder builder) {
        this.builder = builder;
    }

    /**
     * ?
     *
     * @param messageDialog ?
     */
    private void applyParams(AlertDialog messageDialog) {
        if (builder == null)
            throw new IllegalArgumentException("builder null ?setBuilder()Builder");
        if (builder.message == null)
            throw new IllegalArgumentException("Builder.setMessage()?");

        messageDialog.setTitle(builder.title);
        messageDialog.setMessage(builder.message);
        if (builder.confirmButtonName != null) {
            messageDialog.setButton(AlertDialog.BUTTON_POSITIVE, builder.confirmButtonName,
                    builder.confirmButtonClickListener);
        }
        if (builder.cancelButtonName != null) {
            messageDialog.setButton(AlertDialog.BUTTON_NEGATIVE, builder.cancelButtonName,
                    builder.cancelButtonClickListener);
        }
        if (builder.neutralButtonName != null) {
            messageDialog.setButton(AlertDialog.BUTTON_NEUTRAL, builder.neutralButtonName,
                    builder.neutralButtonClickListener);
        }
        messageDialog.setOnKeyListener(builder.onKeyListener);
        messageDialog.setOnShowListener(builder.onShowListener);
        setCancelable(builder.cancelable);
    }

    /**
     * 
     */
    public void update() {
        Dialog dialog = getDialog();
        if (dialog == null || !(dialog instanceof AlertDialog))
            return;
        applyParams((AlertDialog) dialog);
    }

    /**
     * ??
     *
     * @param fragmentManager Fragment?
     * @param builder         
     */
    public static void show(FragmentManager fragmentManager, Builder builder) {
        if (fragmentManager == null)
            return;

        Fragment fragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_MESSAGE_DIALOG);
        boolean old = fragment != null && fragment instanceof MessageDialogFragment;
        MessageDialogFragment messageDialogFragment = old ? (MessageDialogFragment) fragment
                : new MessageDialogFragment();
        messageDialogFragment.setBuilder(builder);
        if (old) {
            messageDialogFragment.update();
        } else {
            messageDialogFragment.show(fragmentManager, FRAGMENT_TAG_MESSAGE_DIALOG);
        }
    }

    /**
     * ??
     *
     * @param fragmentManager Fragment?
     */
    public static void close(FragmentManager fragmentManager) {
        if (fragmentManager == null)
            return;

        Fragment fragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_MESSAGE_DIALOG);
        if (fragment != null && fragment instanceof MessageDialogFragment) {
            ((MessageDialogFragment) fragment).dismiss();
        }
    }

    public static class Builder {
        private String title;
        private String message;
        private String confirmButtonName;
        private String cancelButtonName;
        private String neutralButtonName;
        private boolean cancelable = true;
        private DialogInterface.OnClickListener confirmButtonClickListener;
        private DialogInterface.OnClickListener cancelButtonClickListener;
        private DialogInterface.OnClickListener neutralButtonClickListener;
        private DialogInterface.OnShowListener onShowListener;
        private DialogInterface.OnDismissListener onDismissListener;
        private DialogInterface.OnCancelListener onCancelListener;
        private DialogInterface.OnKeyListener onKeyListener;

        public Builder(String message) {
            this.message = message;
        }

        /**
         * 
         *
         * @param title 
         * @return Builder
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * ?
         *
         * @param message ?
         * @return Builder
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * ???
         *
         * @param cancelable ???
         * @return Builder
         */
        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        /**
         * 
         *
         * @param name ??
         * @return Builder
         */
        public Builder setConfirmButton(String name) {
            this.confirmButtonName = name;
            return this;
        }

        /**
         * 
         *
         * @param name          ??
         * @param clickListener ?
         * @return Builder
         */
        public Builder setConfirmButton(String name, DialogInterface.OnClickListener clickListener) {
            this.confirmButtonName = name;
            this.confirmButtonClickListener = clickListener;
            return this;
        }

        /**
         * ?
         *
         * @param name ??
         * @return Builder
         */
        public Builder setCancelButton(String name) {
            this.cancelButtonName = name;
            return this;
        }

        /**
         * ?
         *
         * @param name          ??
         * @param clickListener ?
         * @return Builder
         */
        public Builder setCancelButton(String name, DialogInterface.OnClickListener clickListener) {
            this.cancelButtonName = name;
            this.cancelButtonClickListener = clickListener;
            return this;
        }

        /**
         * 
         *
         * @param name ??
         * @return Builder
         */
        public Builder setNeutralButton(String name) {
            this.neutralButtonName = name;
            return this;
        }

        /**
         * 
         *
         * @param name          ??
         * @param clickListener ?
         * @return Builder
         */
        public Builder setNeutralButton(String name, DialogInterface.OnClickListener clickListener) {
            this.neutralButtonName = name;
            this.neutralButtonClickListener = clickListener;
            return this;
        }

        /**
         * ?
         *
         * @param onShowListener ?
         */
        public Builder setOnShowListener(DialogInterface.OnShowListener onShowListener) {
            this.onShowListener = onShowListener;
            return this;
        }

        /**
         * ?
         *
         * @param onDismissListener ?
         */
        public Builder setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
            this.onDismissListener = onDismissListener;
            return this;
        }

        /**
         * ??
         *
         * @param onCancelListener ??
         */
        public Builder setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
            this.onCancelListener = onCancelListener;
            return this;
        }

        /**
         * ?
         *
         * @param onKeyListener ?
         */
        public Builder setOnKeyListener(DialogInterface.OnKeyListener onKeyListener) {
            this.onKeyListener = onKeyListener;
            return this;
        }

        /**
         * 
         *
         * @param fragmentManager Fragment?
         */
        public void show(FragmentManager fragmentManager) {
            MessageDialogFragment.show(fragmentManager, this);
        }
    }
}