From c4afa069dfa9c92900d4ef649a167fe1c1a0b63b Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 13 Feb 2017 23:11:50 +0800 Subject: [PATCH] Add custom AlertDialog --- .../magisk/components/AlertDialogBuilder.java | 129 ++++++++++++++++++ .../com/topjohnwu/magisk/utils/Utils.java | 12 +- app/src/main/res/layout/alert_dialog.xml | 74 ++++++++++ 3 files changed, 210 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/components/AlertDialogBuilder.java create mode 100644 app/src/main/res/layout/alert_dialog.xml diff --git a/app/src/main/java/com/topjohnwu/magisk/components/AlertDialogBuilder.java b/app/src/main/java/com/topjohnwu/magisk/components/AlertDialogBuilder.java new file mode 100644 index 000000000..5c764f1fd --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/components/AlertDialogBuilder.java @@ -0,0 +1,129 @@ +package com.topjohnwu.magisk.components; + +import android.content.Context; +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; +import android.widget.Button; +import android.widget.TextView; + +import com.topjohnwu.magisk.R; + +import butterknife.BindView; +import butterknife.ButterKnife; + +public class AlertDialogBuilder extends AlertDialog.Builder { + + @BindView(R.id.negative) Button negative; + @BindView(R.id.positive) Button positive; + @BindView(R.id.neutral) Button neutral; + @BindView(R.id.message) TextView messageView; + + private DialogInterface.OnClickListener positiveListener; + private DialogInterface.OnClickListener negativeListener; + private DialogInterface.OnClickListener neutralListener; + + private AlertDialog dialog; + + public AlertDialogBuilder(@NonNull Context context) { + super(context); + setup(); + } + + public AlertDialogBuilder(@NonNull Context context, @StyleRes int themeResId) { + 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); + } + + @Override + public AlertDialog.Builder setMessage(@Nullable CharSequence message) { + messageView.setText(message); + 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) { + positive.setVisibility(View.VISIBLE); + positive.setText(text); + positiveListener = listener; + positive.setOnClickListener((v) -> { + if (positiveListener != null) + positiveListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); + 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) { + negative.setVisibility(View.VISIBLE); + negative.setText(text); + negativeListener = listener; + negative.setOnClickListener((v) -> { + if (negativeListener != null) + negativeListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); + 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) { + neutral.setVisibility(View.VISIBLE); + neutral.setText(text); + neutralListener = listener; + neutral.setOnClickListener((v) -> { + if (neutralListener != null) + neutralListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL); + 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; + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index ab43aef2c..f43040863 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -17,6 +17,7 @@ import android.widget.Toast; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.asyncs.LoadRepos; +import com.topjohnwu.magisk.components.AlertDialogBuilder; import com.topjohnwu.magisk.database.RepoDatabaseHelper; import com.topjohnwu.magisk.receivers.DownloadReceiver; @@ -117,11 +118,12 @@ public class Utils { } public static AlertDialog.Builder getAlertDialogBuilder(Context context) { - if (((MagiskManager) context.getApplicationContext()).isDarkTheme) { - return new AlertDialog.Builder(context, R.style.AlertDialog_dh); - } else { - return new AlertDialog.Builder(context); - } +// if (((MagiskManager) context.getApplicationContext()).isDarkTheme) { +// return new AlertDialog.Builder(context, R.style.AlertDialog_dh); +// } else { +// return new AlertDialog.Builder(context); +// } + return new AlertDialogBuilder(context); } public static boolean lowercaseContains(CharSequence string, CharSequence nonNullLowercaseSearch) { diff --git a/app/src/main/res/layout/alert_dialog.xml b/app/src/main/res/layout/alert_dialog.xml new file mode 100644 index 000000000..14eadeef8 --- /dev/null +++ b/app/src/main/res/layout/alert_dialog.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + +