2017-03-31 09:21:40 +02:00
|
|
|
package info.nightscout.utils;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2018-07-30 22:56:00 +02:00
|
|
|
import android.content.Context;
|
2017-03-31 09:21:40 +02:00
|
|
|
import android.content.DialogInterface;
|
2018-07-30 22:56:00 +02:00
|
|
|
import android.os.Handler;
|
2017-09-06 20:00:36 +02:00
|
|
|
import android.os.SystemClock;
|
2017-03-31 09:21:40 +02:00
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
|
import android.support.v7.view.ContextThemeWrapper;
|
2017-06-26 12:44:03 +02:00
|
|
|
import android.text.Spanned;
|
2017-03-31 09:21:40 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import info.nightscout.androidaps.MainApp;
|
|
|
|
import info.nightscout.androidaps.R;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mike on 31.03.2017.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class OKDialog {
|
|
|
|
private static Logger log = LoggerFactory.getLogger(OKDialog.class);
|
|
|
|
|
2018-07-30 22:56:00 +02:00
|
|
|
public static void show(final Context context, String title, String message, final Runnable runnable) {
|
2017-03-31 09:21:40 +02:00
|
|
|
try {
|
2018-07-30 22:56:00 +02:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AppTheme));
|
2017-03-31 09:21:40 +02:00
|
|
|
builder.setTitle(title);
|
|
|
|
builder.setMessage(message);
|
2018-05-02 13:46:38 +02:00
|
|
|
builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() {
|
2017-03-31 09:21:40 +02:00
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
dialog.dismiss();
|
|
|
|
if (runnable != null) {
|
2017-09-06 20:00:36 +02:00
|
|
|
SystemClock.sleep(100);
|
2018-07-30 22:56:00 +02:00
|
|
|
runOnUiThread(runnable);
|
2017-03-31 09:21:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
builder.create().show();
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.debug("show_dialog exception: " + e);
|
|
|
|
}
|
|
|
|
}
|
2017-06-26 12:44:03 +02:00
|
|
|
|
2018-07-30 22:56:00 +02:00
|
|
|
public static boolean runOnUiThread(Runnable theRunnable) {
|
|
|
|
final Handler mainHandler = new Handler(MainApp.instance().getApplicationContext().getMainLooper());
|
|
|
|
return mainHandler.post(theRunnable);
|
|
|
|
}
|
|
|
|
|
2017-06-26 12:44:03 +02:00
|
|
|
public static void show(final Activity activity, String title, Spanned message, final Runnable runnable) {
|
|
|
|
try {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme));
|
|
|
|
builder.setTitle(title);
|
|
|
|
builder.setMessage(message);
|
2018-05-02 13:46:38 +02:00
|
|
|
builder.setPositiveButton(MainApp.gs(R.string.ok), new DialogInterface.OnClickListener() {
|
2017-06-26 12:44:03 +02:00
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
dialog.dismiss();
|
|
|
|
if (runnable != null) {
|
2017-09-06 20:00:36 +02:00
|
|
|
SystemClock.sleep(100);
|
2017-06-26 12:44:03 +02:00
|
|
|
activity.runOnUiThread(runnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
builder.create().show();
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.debug("show_dialog exception: " + e);
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 09:25:57 +02:00
|
|
|
|
|
|
|
public static void showConfirmation(final Activity activity, String message, final Runnable runnable) {
|
|
|
|
AlertDialog alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme))
|
|
|
|
.setMessage(message)
|
|
|
|
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
|
|
|
|
dialog.dismiss();
|
|
|
|
if (runnable != null) {
|
|
|
|
SystemClock.sleep(100);
|
|
|
|
activity.runOnUiThread(runnable);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
2017-03-31 09:21:40 +02:00
|
|
|
}
|