Alert and notification dialogs should be run on UI thread.

This commit is contained in:
Bryce Curtis 2010-10-25 14:35:02 -05:00
parent 5647e54399
commit 153d42f693

View File

@ -18,7 +18,6 @@ import android.content.DialogInterface;
import android.media.Ringtone; import android.media.Ringtone;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
import android.os.Looper;
import android.os.Vibrator; import android.os.Vibrator;
/** /**
@ -56,14 +55,12 @@ public class Notification extends Plugin {
this.vibrate(args.getLong(0)); this.vibrate(args.getLong(0));
} }
else if (action.equals("alert")) { else if (action.equals("alert")) {
Looper.prepare();
this.alert(args.getString(0),args.getString(1),args.getString(2), callbackId); this.alert(args.getString(0),args.getString(1),args.getString(2), callbackId);
Looper.loop(); return new PluginResult(PluginResult.Status.RESULT_TO_BE_SENT);
} }
else if (action.equals("confirm")) { else if (action.equals("confirm")) {
Looper.prepare();
this.confirm(args.getString(0),args.getString(1),args.getString(2), callbackId); this.confirm(args.getString(0),args.getString(1),args.getString(2), callbackId);
Looper.loop(); return new PluginResult(PluginResult.Status.RESULT_TO_BE_SENT);
} }
else if (action.equals("activityStart")) { else if (action.equals("activityStart")) {
this.activityStart(args.getString(0),args.getString(1)); this.activityStart(args.getString(0),args.getString(1));
@ -94,10 +91,10 @@ public class Notification extends Plugin {
*/ */
public boolean isSynch(String action) { public boolean isSynch(String action) {
if (action.equals("alert")) { if (action.equals("alert")) {
return false; return true;
} }
else if (action.equals("confirm")) { else if (action.equals("confirm")) {
return false; return true;
} }
else if (action.equals("activityStart")) { else if (action.equals("activityStart")) {
return true; return true;
@ -169,11 +166,15 @@ public class Notification extends Plugin {
* @param buttonLabel The label of the button * @param buttonLabel The label of the button
* @param callbackId The callback id * @param callbackId The callback id
*/ */
public synchronized void alert(String message,String title,String buttonLabel, String callbackId) { public synchronized void alert(final String message, final String title, final String buttonLabel, final String callbackId) {
final Notification notification = this;
final String fCallbackId = callbackId;
AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx); final DroidGap ctx = this.ctx;
final Notification notification = this;
Runnable runnable = new Runnable() {
public void run() {
AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
dlg.setMessage(message); dlg.setMessage(message);
dlg.setTitle(title); dlg.setTitle(title);
dlg.setCancelable(false); dlg.setCancelable(false);
@ -181,11 +182,14 @@ public class Notification extends Plugin {
new AlertDialog.OnClickListener() { new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); dialog.dismiss();
notification.success(new PluginResult(PluginResult.Status.OK, 0), fCallbackId); notification.success(new PluginResult(PluginResult.Status.OK, 0), callbackId);
} }
}); });
dlg.create(); dlg.create();
dlg.show(); dlg.show();
};
};
this.ctx.runOnUiThread(runnable);
} }
/** /**
@ -198,13 +202,14 @@ public class Notification extends Plugin {
* @param buttonLabels A comma separated list of button labels (Up to 3 buttons) * @param buttonLabels A comma separated list of button labels (Up to 3 buttons)
* @param callbackId The callback id * @param callbackId The callback id
*/ */
public synchronized void confirm(final String message, final String title, String buttonLabels, String callbackId) { public synchronized void confirm(final String message, final String title, String buttonLabels, final String callbackId) {
final DroidGap ctx = this.ctx; final DroidGap ctx = this.ctx;
final Notification notification = this; final Notification notification = this;
final String[] fButtons = buttonLabels.split(","); final String[] fButtons = buttonLabels.split(",");
final String fCallbackId = callbackId;
Runnable runnable = new Runnable() {
public void run() {
AlertDialog.Builder dlg = new AlertDialog.Builder(ctx); AlertDialog.Builder dlg = new AlertDialog.Builder(ctx);
dlg.setMessage(message); dlg.setMessage(message);
dlg.setTitle(title); dlg.setTitle(title);
@ -216,7 +221,7 @@ public class Notification extends Plugin {
new AlertDialog.OnClickListener() { new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); dialog.dismiss();
notification.success(new PluginResult(PluginResult.Status.OK, 1), fCallbackId); notification.success(new PluginResult(PluginResult.Status.OK, 1), callbackId);
} }
}); });
} }
@ -227,7 +232,7 @@ public class Notification extends Plugin {
new AlertDialog.OnClickListener() { new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); dialog.dismiss();
notification.success(new PluginResult(PluginResult.Status.OK, 2), fCallbackId); notification.success(new PluginResult(PluginResult.Status.OK, 2), callbackId);
} }
}); });
} }
@ -238,7 +243,7 @@ public class Notification extends Plugin {
new AlertDialog.OnClickListener() { new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); dialog.dismiss();
notification.success(new PluginResult(PluginResult.Status.OK, 3), fCallbackId); notification.success(new PluginResult(PluginResult.Status.OK, 3), callbackId);
} }
} }
); );
@ -246,6 +251,9 @@ public class Notification extends Plugin {
dlg.create(); dlg.create();
dlg.show(); dlg.show();
};
};
this.ctx.runOnUiThread(runnable);
} }
/** /**