Support for displaying Toasts from non-ui threads

This commit is contained in:
cpfeiffer 2015-07-11 21:49:51 +02:00
parent be45f7fe0c
commit b516ceda01
2 changed files with 92 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
@ -8,7 +9,10 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -30,6 +34,9 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.TimeChangeReceiver;
public class GB {
public static final int NOTIFICATION_ID = 1;
private static final Logger LOG = LoggerFactory.getLogger(GB.class);
public static final int INFO = 1;
public static final int WARN = 2;
public static final int ERROR = 3;
public static Notification createNotification(String text, Context context) {
Intent notificationIntent = new Intent(context, ControlCenter.class);
@ -160,4 +167,85 @@ public class GB {
return true;
}
/**
* Creates and display a Toast message using the application context.
* Additionally the toast is logged using the provided severity.
* Can be called from any thread.
* @param message the message to display.
* @param displayTime something like Toast.LENGTH_SHORT
* @param severity either INFO, WARNING, ERROR
*/
public static void toast(String message, int displayTime, int severity) {
toast(GBApplication.getContext(), message, displayTime, severity, null);
}
/**
* Creates and display a Toast message using the application context.
* Additionally the toast is logged using the provided severity.
* Can be called from any thread.
* @param message the message to display.
* @param displayTime something like Toast.LENGTH_SHORT
* @param severity either INFO, WARNING, ERROR
*/
public static void toast(String message, int displayTime, int severity, Throwable ex) {
toast(GBApplication.getContext(), message, displayTime, severity, ex);
}
/**
* Creates and display a Toast message using the application context
* Can be called from any thread.
* @param context the context to use
* @param message the message to display
* @param displayTime something like Toast.LENGTH_SHORT
* @param severity either INFO, WARNING, ERROR
*/
public static void toast(final Context context, final String message, final int displayTime, final int severity) {
toast(context, message, displayTime, severity, null);
}
/**
* Creates and display a Toast message using the application context
* Can be called from any thread.
* @param context the context to use
* @param message the message to display
* @param displayTime something like Toast.LENGTH_SHORT
* @param severity either INFO, WARNING, ERROR
* @param ex optional exception to be logged
*/
public static void toast(final Context context, final String message, final int displayTime, final int severity, final Throwable ex) {
Looper mainLooper = Looper.getMainLooper();
if (Thread.currentThread() == mainLooper.getThread()) {
log(message, severity, ex);
Toast.makeText(context, message, displayTime).show();
} else {
Runnable runnable = new Runnable() {
@Override
public void run() {
log(message, severity, ex);
Toast.makeText(context, message, displayTime).show();
}
};
if (context instanceof Activity) {
((Activity) context).runOnUiThread(runnable);
} else {
new Handler(mainLooper).post(runnable);
}
}
}
private static void log(String message, int severity, Throwable ex) {
switch (severity) {
case INFO:
LOG.info(message, ex);
break;
case WARN:
LOG.warn(message, ex);
break;
case ERROR:
LOG.error(message, ex);
break;
}
}
}

View File

@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import nodomain.freeyourgadget.gadgetbridge.GB;
import nodomain.freeyourgadget.gadgetbridge.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.database.schema.ActivityDBCreationScript;
@ -34,8 +35,7 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper {
ActivityDBCreationScript script = new ActivityDBCreationScript();
script.createSchema(db);
} catch (RuntimeException ex) {
LOG.error("Error creatomg database", ex);
Toast.makeText(GBApplication.getContext(), "Error creating database.", Toast.LENGTH_SHORT).show();
GB.toast("Error creating database.", Toast.LENGTH_SHORT, GB.ERROR, ex);
}
}
@ -51,8 +51,7 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper {
}
LOG.info("activity database is now at version " + newVersion);
} catch (RuntimeException ex) {
LOG.error("Error upgrading db version. ", ex);
Toast.makeText(GBApplication.getContext(), "Error upgrading database.", Toast.LENGTH_SHORT).show();
GB.toast("Error upgrading database.", Toast.LENGTH_SHORT, GB.ERROR, ex);
throw ex; // reject upgrade
}
}
@ -69,8 +68,7 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper {
}
LOG.info("activity database is now at version " + newVersion);
} catch (RuntimeException ex) {
LOG.error("Error downgrading db version. ", ex);
Toast.makeText(GBApplication.getContext(), "Error downgrading database.", Toast.LENGTH_SHORT).show();
GB.toast("Error downgrading database.", Toast.LENGTH_SHORT, GB.ERROR, ex);
throw ex; // reject downgrade
}
}