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

Java tutorial

Introduction

Here is the source code for me.panpf.tool4a.app.ProgressDialogFragment.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.app.ProgressDialog;
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;

import java.util.Locale;

public class ProgressDialogFragment extends DialogFragment {
    public static String MESSAGE_DEFAULT_CHINA = "?...";
    public static String MESSAGE_DEFAULT_OTHER = "Please wait a moment...";
    public static String FRAGMENT_TAG_PROGRESS_DIALOG = "FRAGMENT_TAG_PROGRESS_DIALOG";

    private Builder builder;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        ProgressDialog progressDialog = new ProgressDialog(getActivity());
        applyParams(progressDialog);
        return progressDialog;
    }

    @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 progressDialog ?
     */
    private void applyParams(ProgressDialog progressDialog) {
        if (builder == null)
            throw new IllegalArgumentException("builder null ?setBuilder()Builder");

        progressDialog.setTitle(builder.title);
        progressDialog.setMessage(builder.message != null ? builder.message
                : (Locale.CHINA.equals(Locale.getDefault()) ? MESSAGE_DEFAULT_CHINA : MESSAGE_DEFAULT_OTHER));
        if (builder.confirmButtonName != null) {
            progressDialog.setButton(AlertDialog.BUTTON_POSITIVE, builder.confirmButtonName,
                    builder.confirmButtonClickListener);
        }
        if (builder.cancelButtonName != null) {
            progressDialog.setButton(AlertDialog.BUTTON_NEGATIVE, builder.cancelButtonName,
                    builder.cancelButtonClickListener);
        }
        if (builder.neutralButtonName != null) {
            progressDialog.setButton(AlertDialog.BUTTON_NEUTRAL, builder.neutralButtonName,
                    builder.neutralButtonClickListener);
        }
        progressDialog.setOnKeyListener(builder.onKeyListener);
        progressDialog.setOnShowListener(builder.onShowListener);
        setCancelable(builder.cancelable);
    }

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

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

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

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

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

    public static class Builder {
        private String title;
        private String message;
        private String confirmButtonName;
        private String cancelButtonName;
        private String neutralButtonName;
        private boolean cancelable = false;
        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) {
            ProgressDialogFragment.show(fragmentManager, this);
        }
    }
}