/**
   * Tell the client to display a prompt dialog to the user. If the client returns true, WebView
   * will assume that the client will handle the prompt dialog and call the appropriate
   * JsPromptResult method.
   *
   * <p>Since we are hacking prompts for our own purposes, we should not be using them for this
   * purpose, perhaps we should hack console.log to do this instead!
   *
   * @param view
   * @param url
   * @param message
   * @param defaultValue
   * @param result
   */
  @Override
  public boolean onJsPrompt(
      WebView view, String url, String message, String defaultValue, JsPromptResult result) {

    // Security check to make sure any requests are coming from the page initially
    // loaded in webview and not another loaded in an iframe.
    boolean reqOk = false;
    if (url.startsWith("file://")
        || url.indexOf(this.ctx.baseUrl) == 0
        || ctx.isUrlWhiteListed(url)) {
      reqOk = true;
    }

    // Calling PluginManager.exec() to call a native service using
    // prompt(this.stringify(args), "gap:"+this.stringify([service, action, callbackId, true]));
    if (reqOk
        && defaultValue != null
        && defaultValue.length() > 3
        && defaultValue.substring(0, 4).equals("gap:")) {
      JSONArray array;
      try {
        array = new JSONArray(defaultValue.substring(4));
        String service = array.getString(0);
        String action = array.getString(1);
        String callbackId = array.getString(2);
        boolean async = array.getBoolean(3);
        String r = ctx.pluginManager.exec(service, action, callbackId, message, async);
        result.confirm(r);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }

    // Polling for JavaScript messages
    else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) {
      String r = ctx.callbackServer.getJavascript();
      result.confirm(r);
    }

    // Calling into CallbackServer
    else if (reqOk && defaultValue != null && defaultValue.equals("gap_callbackServer:")) {
      String r = "";
      if (message.equals("usePolling")) {
        r = "" + ctx.callbackServer.usePolling();
      } else if (message.equals("restartServer")) {
        ctx.callbackServer.restartServer();
      } else if (message.equals("getPort")) {
        r = Integer.toString(ctx.callbackServer.getPort());
      } else if (message.equals("getToken")) {
        r = ctx.callbackServer.getToken();
      }
      result.confirm(r);
    }

    // Cordova JS has initialized, so show webview
    // (This solves white flash seen when rendering HTML)
    else if (reqOk && defaultValue != null && defaultValue.equals("gap_init:")) {
      ctx.appView.setVisibility(View.VISIBLE);
      ctx.spinnerStop();
      result.confirm("OK");
    }

    // Show dialog
    else {
      final JsPromptResult res = result;
      AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx);
      dlg.setMessage(message);
      final EditText input = new EditText(this.ctx);
      if (defaultValue != null) {
        input.setText(defaultValue);
      }
      dlg.setView(input);
      dlg.setCancelable(false);
      dlg.setPositiveButton(
          android.R.string.ok,
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              String usertext = input.getText().toString();
              res.confirm(usertext);
            }
          });
      dlg.setNegativeButton(
          android.R.string.cancel,
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              res.cancel();
            }
          });
      dlg.create();
      dlg.show();
    }
    return true;
  }