自定义dialog

QGDialog dialog = new QGDialog.Builder(context).setMessage(
        msg).create();
dialog.show(time);
public class QGDialog {
    private QGDialog.Builder mBuilder;
    private boolean isShow;
    private OnDismissListener internalDismissListener;
    private static final String TAG = "PateoDialog";

    private QGDialog(QGDialog.Builder mBuilder) {
        this.mBuilder = null;
        this.isShow = false;
        this.internalDismissListener = new OnDismissListener() {
            public void onDismiss(DialogInterface dialog) {
                QGDialog.this.isShow = false;
                if(QGDialog.this.mBuilder.dismissListener != null) {
                    QGDialog.this.mBuilder.dismissListener.onDismiss(dialog);
                }

            }
        };
        this.mBuilder = mBuilder;
    }

    public void show() {
        if(!this.isShow) {
            this.isShow = true;
            Message msg = QGDialogConnection.mMyHandler.obtainMessage();
            msg.obj = this.getConnection();
            msg.what = 0;
            msg.sendToTarget();
        } else {
            Log.e("PateoDialog", "You have use show() before! if you want show again, you can use update(RemoteViews view)");
        }
    }

    public void show(Long milliseconds) {
        if(milliseconds.longValue() <= 0L) {
            this.show();
        } else if(this.isShow) {
            Log.e("PateoDialog", "You have use show(), and not dissmis()");
        } else {
            this.isShow = true;
            Message msg = QGDialogConnection.mMyHandler.obtainMessage();
            msg.obj = this.getConnection();
            Bundle data = new Bundle();
            msg.setData(data);
            msg.what = 0;
            msg.sendToTarget();
            this.dismiss(milliseconds.longValue());
        }
    }

    private void dismiss(long timeout) {
        if(this.isShow) {
            Message msg = QGDialogConnection.mMyHandler.obtainMessage();
            msg.obj = this.getConnection();
            msg.what = 1;
            QGDialogConnection.mMyHandler.sendMessageDelayed(msg, timeout);
            QGDialogConnection.mMyHandler.postDelayed(new Runnable() {
                public void run() {
                    QGDialog.this.isShow = false;
                    QGDialog.this.mBuilder.connection = null;
                }
            }, timeout);
        }
    }

    public void dismiss() {
        if(this.isShow) {
            this.isShow = false;
        }

        Message msg = QGDialogConnection.mMyHandler.obtainMessage();
        msg.obj = this.getConnection();
        msg.what = 1;
        msg.sendToTarget();
        this.mBuilder.connection = null;
    }

    public void update(RemoteViews view) {
        Log.i("feifei", "update RemoteViews!!!");
        if(!this.isShow) {
            Log.i("feifei", "Please use show() first!");
        } else {
            this.mBuilder.mContentView = view;
            this.isShow = false;
            this.show();
        }
    }

    public void update(QGRemoteViews view) {
        Log.i("feifei", "update RemoteViews!!!");
        if(!this.isShow) {
            Log.i("feifei", "Please use show() first!");
        } else {
            this.mBuilder.mPateoContentView = view;
            this.isShow = false;
            this.show();
        }
    }

    public void update(String message) {
        if(this.mBuilder.mContentView != null) {
            Log.i("feifei", "Please use \'update(RemoteViews view)\' for customer view!");
        } else {
            this.mBuilder.mMessage = message;
            this.isShow = false;
            this.show();
        }
    }

    public void update(String message, long timeout) {
        if(this.mBuilder.mContentView != null) {
            Log.i("feifei", "Please use \'update(RemoteViews view)\' for customer view!");
        } else {
            this.mBuilder.mMessage = message;
            this.isShow = false;
            this.show(Long.valueOf(timeout));
        }
    }

    public void setLayoutParamsType(int LayoutParamsType) {
        this.mBuilder.mLayoutParamsType = LayoutParamsType;
    }

    public void setToTopLayer() {
        this.mBuilder.mLayoutParamsType = 2010;
    }

    private QGDialogConnection getConnection() {
        if(this.mBuilder.connection == null) {
            ClientProxy client = new ClientProxy(this.mBuilder.mCallback);
            QGDialogConnection connection = new QGDialogConnection(this.mBuilder, client);
            connection.dismissListener = this.internalDismissListener;
            client.mConnection = connection;
            this.mBuilder.connection = connection;
            connection.connect();
        }

        return this.mBuilder.connection;
    }

    public int getId() {
        return this.mBuilder.id;
    }

    public boolean isShowing() {
        return this.isShow;
    }

    public static class Builder {
        int id = -1;
        int type = 0;
        int mLayoutParamsType = 2002;
        QGDialogConnection connection = null;
        Context mContext = null;
        CharSequence mMessage;
        CharSequence mLeftButtonTxt;
        CharSequence mRightButtonTxt;
        int mLevel = -1;
        CallBack mCallback;
        RemoteViews mContentView = null;
        QGRemoteViews mPateoContentView = null;
        RemoteViews mOnClickContentView = null;
        OnDismissListener dismissListener = null;

        public Builder(Context context) {
            this.mContext = context;
        }

        public Context getContext() {
            return this.mContext;
        }

        public QGDialog.Builder setTitle(int titleId) {
            return this;
        }

        public QGDialog.Builder setMessage(CharSequence message) {
            this.mMessage = message;
            return this;
        }

        public QGDialog.Builder setLeftButtonText(int resId) {
            this.mLeftButtonTxt = this.mContext.getText(resId);
            return this;
        }

        public QGDialog.Builder setLeftButtonText(String msg) {
            this.mLeftButtonTxt = msg;
            return this;
        }

        public QGDialog.Builder setRightButtonText(int resId) {
            this.mRightButtonTxt = this.mContext.getText(resId);
            return this;
        }

        public QGDialog.Builder setRightButtonText(String msg) {
            this.mRightButtonTxt = msg;
            return this;
        }

        public QGDialog.Builder setMessageText(int resId) {
            this.mMessage = this.mContext.getText(resId);
            return this;
        }

        public QGDialog.Builder setMessageText(String msg) {
            this.mMessage = msg;
            return this;
        }

        public QGDialog.Builder setLevel(NotificationLevel messageLevel) {
            if(messageLevel == null) {
                return this;
            } else {
                this.mLevel = messageLevel.ordinal();
                return this;
            }
        }

        public QGDialog.Builder setCallBack(CallBack callback) {
            this.mCallback = callback;
            return this;
        }

        public QGDialog.Builder setOnDismissListener(OnDismissListener listener) {
            if(this.dismissListener != null) {
                throw new IllegalStateException("OnDismissListener is already exsit");
            } else {
                if(listener != null) {
                    this.dismissListener = listener;
                } else {
                    this.dismissListener = null;
                }

                return this;
            }
        }

        public QGDialog create() {
            if(this.mContentView == null && this.mPateoContentView == null) {
                this.type = DialogConstants.defaultLayout;
            } else {
                this.type = DialogConstants.customLayout;
            }

            this.id = QGDialogConnection.getUniqueId();
            if(this.mLevel > -1) {
                this.mLayoutParamsType = 2010;
            }

            return new QGDialog(this, null);
        }

        public QGDialog.Builder setCustomerView(RemoteViews rv) {
            this.mContentView = rv;
            return this;
        }

        public QGDialog.Builder setCustomerView(QGRemoteViews prv) {
            this.mPateoContentView = prv;
            return this;
        }
    }
}

 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值