Magisk/app/src/main/java/com/topjohnwu/magisk/components/AlertDialogBuilder.java

162 lines
5.1 KiB
Java
Raw Normal View History

2017-02-13 23:11:50 +08:00
package com.topjohnwu.magisk.components;
2017-09-05 17:43:13 +08:00
import android.app.Activity;
2017-02-13 23:11:50 +08:00
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.annotation.StyleRes;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
2017-02-16 05:45:31 +08:00
import android.view.ViewGroup;
import android.view.ViewStub;
2017-02-13 23:11:50 +08:00
import android.widget.Button;
2017-02-16 05:45:31 +08:00
import android.widget.LinearLayout;
2017-02-13 23:11:50 +08:00
import android.widget.TextView;
import com.topjohnwu.magisk.R;
import butterknife.BindView;
import butterknife.ButterKnife;
public class AlertDialogBuilder extends AlertDialog.Builder {
2017-02-16 05:45:31 +08:00
@BindView(R.id.button_panel) LinearLayout buttons;
@BindView(R.id.message_panel) LinearLayout messagePanel;
2017-02-13 23:11:50 +08:00
@BindView(R.id.negative) Button negative;
@BindView(R.id.positive) Button positive;
@BindView(R.id.neutral) Button neutral;
@BindView(R.id.message) TextView messageView;
2017-02-16 05:45:31 +08:00
@BindView(R.id.custom_view) ViewStub custom;
2017-02-13 23:11:50 +08:00
private DialogInterface.OnClickListener positiveListener;
private DialogInterface.OnClickListener negativeListener;
private DialogInterface.OnClickListener neutralListener;
private AlertDialog dialog;
2017-09-05 17:43:13 +08:00
public AlertDialogBuilder(@NonNull Activity context) {
2017-02-13 23:11:50 +08:00
super(context);
setup();
}
2017-09-05 17:43:13 +08:00
public AlertDialogBuilder(@NonNull Activity context, @StyleRes int themeResId) {
2017-02-13 23:11:50 +08:00
super(context, themeResId);
setup();
}
private void setup() {
View v = LayoutInflater.from(getContext()).inflate(R.layout.alert_dialog, null);
ButterKnife.bind(this, v);
super.setView(v);
negative.setVisibility(View.GONE);
positive.setVisibility(View.GONE);
neutral.setVisibility(View.GONE);
2017-02-16 05:45:31 +08:00
buttons.setVisibility(View.GONE);
messagePanel.setVisibility(View.GONE);
}
@Override
public AlertDialog.Builder setView(int layoutResId) {
custom.setLayoutResource(layoutResId);
custom.inflate();
return this;
}
@Override
public AlertDialog.Builder setView(View view) {
ViewGroup parent = (ViewGroup) custom.getParent();
int idx = parent.indexOfChild(custom);
parent.removeView(custom);
parent.addView(view, idx);
return this;
2017-02-13 23:11:50 +08:00
}
@Override
public AlertDialog.Builder setMessage(@Nullable CharSequence message) {
messageView.setText(message);
2017-02-16 05:45:31 +08:00
messagePanel.setVisibility(View.VISIBLE);
2017-02-13 23:11:50 +08:00
return this;
}
@Override
public AlertDialog.Builder setMessage(@StringRes int messageId) {
return setMessage(getContext().getString(messageId));
}
@Override
public AlertDialog.Builder setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) {
2017-02-16 05:45:31 +08:00
buttons.setVisibility(View.VISIBLE);
2017-02-13 23:11:50 +08:00
positive.setVisibility(View.VISIBLE);
positive.setText(text);
positiveListener = listener;
positive.setOnClickListener((v) -> {
if (positiveListener != null) {
2017-02-13 23:11:50 +08:00
positiveListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
}
2017-02-13 23:11:50 +08:00
dialog.dismiss();
});
return this;
}
@Override
public AlertDialog.Builder setPositiveButton(@StringRes int textId, DialogInterface.OnClickListener listener) {
return setPositiveButton(getContext().getString(textId), listener);
}
@Override
public AlertDialog.Builder setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) {
2017-02-16 05:45:31 +08:00
buttons.setVisibility(View.VISIBLE);
2017-02-13 23:11:50 +08:00
negative.setVisibility(View.VISIBLE);
negative.setText(text);
negativeListener = listener;
negative.setOnClickListener((v) -> {
if (negativeListener != null) {
2017-02-13 23:11:50 +08:00
negativeListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
}
2017-02-13 23:11:50 +08:00
dialog.dismiss();
});
return this;
}
@Override
public AlertDialog.Builder setNegativeButton(@StringRes int textId, DialogInterface.OnClickListener listener) {
return setNegativeButton(getContext().getString(textId), listener);
}
@Override
public AlertDialog.Builder setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener) {
2017-02-16 05:45:31 +08:00
buttons.setVisibility(View.VISIBLE);
2017-02-13 23:11:50 +08:00
neutral.setVisibility(View.VISIBLE);
neutral.setText(text);
neutralListener = listener;
neutral.setOnClickListener((v) -> {
if (neutralListener != null) {
2017-02-13 23:11:50 +08:00
neutralListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
}
2017-02-13 23:11:50 +08:00
dialog.dismiss();
});
return this;
}
@Override
public AlertDialog.Builder setNeutralButton(@StringRes int textId, DialogInterface.OnClickListener listener) {
return setNeutralButton(getContext().getString(textId), listener);
}
@Override
public AlertDialog create() {
dialog = super.create();
return dialog;
}
@Override
public AlertDialog show() {
create();
dialog.show();
return dialog;
}
}