Skip to content

Commit

Permalink
Android plugins also support callback handles.
Browse files Browse the repository at this point in the history
  • Loading branch information
gga committed Nov 11, 2012
1 parent 731af09 commit e7852f6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class AlertPlugin implements RegisteredPlugin
{
private PluginRegistry registry;
private Context ctxt;
private String currentOkCallbackHandle;

public void setContext(PluginRegistry registry, Context ctxt)
{
Expand All @@ -24,14 +25,26 @@ public void setContext(PluginRegistry registry, Context ctxt)
public void execute(Intent action, RegisteredActivity frontmost)
{
AlertDialog.Builder builder = new AlertDialog.Builder(frontmost);
final boolean isConfirmDialog = action.getExtras().getString("method").equals("displayConfirm");
builder.setMessage(action.getExtras().getString("message"))
.setCancelable(false)
.setCancelable(isConfirmDialog)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
if (isConfirmDialog) {
AlertPlugin.this.registry.invokeCallback(currentOkCallbackHandle, 1);
}
}
});
if (isConfirmDialog) {
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
}
AlertDialog dialog = builder.create();
dialog.show();
}
Expand All @@ -40,6 +53,9 @@ public void onClick(DialogInterface dialogInterface, int i) {

public void call(String method, Map<String, Object> args)
{
ctxt.sendBroadcast(registry.pluginCommand("alert").putExtra("message", (String)args.get("message")));
currentOkCallbackHandle = (String)args.get("okHandler");
ctxt.sendBroadcast(registry.pluginCommand("alert")
.putExtra("method", method)
.putExtra("message", (String)args.get("message")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void onServiceConnected(ComponentName componentName, IBinder iBinder)
{
rhino = ((RhinoService.LocalBinder) iBinder).getService();
PageRegistry.setSharedRegistry(new PageRegistry(appName, appContext, application, rhino));
PluginRegistry.setSharedRegistry(new PluginRegistry(appName, appContext));
PluginRegistry.setSharedRegistry(new PluginRegistry(appName, appContext, rhino));
AjaxRequestManager.setSharedManager(new AjaxRequestManager(appContext, rhino));
initBridge();
startUp.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class PluginRegistry {
private static PluginRegistry sharedRegistry;

private Context appContext;
private RhinoService rhino;
private Map<String, RegisteredPlugin> registeredPlugins = new HashMap<String, RegisteredPlugin>();
private Map<String, PluginCommand> installedCmds = new HashMap<String, PluginCommand>();
private ObjectMapper jsonMapper = new ObjectMapper();
Expand All @@ -33,10 +34,11 @@ public static void setSharedRegistry(PluginRegistry shared)
sharedRegistry = shared;
}

public PluginRegistry(String packageName, Context appContext)
public PluginRegistry(String packageName, Context appContext, RhinoService rhino)
throws IOException, URISyntaxException, ClassNotFoundException, NameNotFoundException
{
this.appContext = appContext;
this.rhino = rhino;

// Find all the plugins to register in the app
addPlugins(packageName, appContext);
Expand Down Expand Up @@ -102,4 +104,9 @@ public void call(String plugin, String method, String argsJson)
Log.e(TAG, "Unable to deserialize JSON for plugin args.", e);
}
}
}

public void invokeCallback(String callbackHandle, Object data)
{
rhino.callJsFunction("calatrava.inbound.invokePluginCallback", new String[] {callbackHandle, data.toString()});
}
}

0 comments on commit e7852f6

Please sign in to comment.