2019-02-26 20:38:27 +01:00
|
|
|
package info.nightscout.androidaps.utils;
|
2016-06-07 21:48:17 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
2016-08-02 18:39:14 +02:00
|
|
|
import android.media.MediaPlayer;
|
2016-06-07 21:48:17 +02:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2019-07-18 23:49:59 +02:00
|
|
|
import androidx.annotation.IdRes;
|
|
|
|
|
2017-08-16 21:37:33 +02:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
2019-02-28 23:16:50 +01:00
|
|
|
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
|
|
|
|
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
|
2017-08-16 21:37:33 +02:00
|
|
|
|
2016-06-07 21:48:17 +02:00
|
|
|
|
2016-08-02 18:39:14 +02:00
|
|
|
public class ToastUtils {
|
2019-07-18 23:49:59 +02:00
|
|
|
public static void showToastInUiThread(final Context ctx,
|
|
|
|
final int stringId) {
|
|
|
|
showToastInUiThread(ctx, MainApp.gs(stringId));
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:48:17 +02:00
|
|
|
public static void showToastInUiThread(final Context ctx,
|
|
|
|
final String string) {
|
|
|
|
|
|
|
|
Handler mainThread = new Handler(Looper.getMainLooper());
|
|
|
|
mainThread.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Toast.makeText(ctx, string, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-08-02 18:39:14 +02:00
|
|
|
|
|
|
|
public static void showToastInUiThread(final Context ctx,
|
|
|
|
final String string, int soundID) {
|
|
|
|
|
|
|
|
showToastInUiThread(ctx, string);
|
|
|
|
playSound(ctx, soundID);
|
2017-08-16 21:37:33 +02:00
|
|
|
new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Notification notification = new Notification(Notification.TOAST_ALARM, string, Notification.URGENT);
|
|
|
|
MainApp.bus().post(new EventNewNotification(notification));
|
|
|
|
}
|
|
|
|
}).start();
|
2016-08-02 18:39:14 +02:00
|
|
|
}
|
|
|
|
|
2016-08-08 10:08:30 +02:00
|
|
|
private static void playSound(final Context ctx, final int soundID) {
|
2016-08-02 18:39:14 +02:00
|
|
|
final MediaPlayer soundMP = MediaPlayer.create(ctx, soundID);
|
|
|
|
soundMP.start();
|
|
|
|
soundMP.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
|
|
|
@Override
|
|
|
|
public void onCompletion(MediaPlayer mp) {
|
|
|
|
mp.release();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-07 21:48:17 +02:00
|
|
|
}
|