/**
   * Notify the host application to handle an authentication request. The default behavior is to
   * cancel the request.
   *
   * @param view The XWalkViewInternal that is initiating the callback.
   * @param handler The XWalkHttpAuthHandler that will handle the user's response.
   * @param host The host requiring authentication.
   * @param realm A description to help store user credentials for future visits.
   */
  @XWalkAPI
  public void onReceivedHttpAuthRequest(
      XWalkViewInternal view, XWalkHttpAuthHandlerInternal handler, String host, String realm) {
    if (view == null) return;

    final XWalkHttpAuthHandlerInternal haHandler = handler;
    Context context = view.getContext();
    LinearLayout layout = new LinearLayout(context);
    final EditText userNameEditText = new EditText(context);
    final EditText passwordEditText = new EditText(context);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setPaddingRelative(10, 0, 10, 20);
    userNameEditText.setHint(R.string.http_auth_user_name);
    passwordEditText.setHint(R.string.http_auth_password);
    passwordEditText.setInputType(
        InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    layout.addView(userNameEditText);
    layout.addView(passwordEditText);

    final Activity curActivity = view.getActivity();
    AlertDialog.Builder httpAuthDialog = new AlertDialog.Builder(curActivity);
    httpAuthDialog
        .setTitle(R.string.http_auth_title)
        .setView(layout)
        .setCancelable(false)
        .setPositiveButton(
            R.string.http_auth_log_in,
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int whichButton) {
                String userName = userNameEditText.getText().toString();
                String password = passwordEditText.getText().toString();
                haHandler.proceed(userName, password);
                dialog.dismiss();
              }
            })
        .setNegativeButton(android.R.string.cancel, null)
        .setOnCancelListener(
            new DialogInterface.OnCancelListener() {
              @Override
              public void onCancel(DialogInterface dialog) {
                haHandler.cancel();
              }
            })
        .create()
        .show();
  }
Exemplo n.º 2
0
  private void showHttpAuthDialog(
      final XWalkHttpAuthHandler handler, final String host, final String realm) {
    LinearLayout layout = new LinearLayout(mContext);
    final EditText userNameEditText = new EditText(mContext);
    final EditText passwordEditText = new EditText(mContext);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setPaddingRelative(10, 0, 10, 20);
    userNameEditText.setHint(R.string.http_auth_user_name);
    passwordEditText.setHint(R.string.http_auth_password);
    layout.addView(userNameEditText);
    layout.addView(passwordEditText);

    final Activity curActivity = mView.getActivity();
    AlertDialog.Builder httpAuthDialog = new AlertDialog.Builder(curActivity);
    httpAuthDialog
        .setTitle(R.string.http_auth_title)
        .setView(layout)
        .setCancelable(false)
        .setPositiveButton(
            R.string.http_auth_log_in,
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                String userName = userNameEditText.getText().toString();
                String password = passwordEditText.getText().toString();
                setHttpAuthUsernamePassword(host, realm, userName, password);
                handler.proceed(userName, password);
                dialog.dismiss();
              }
            })
        .setNegativeButton(
            android.R.string.cancel,
            new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                handler.cancel();
                dialog.dismiss();
              }
            })
        .create()
        .show();
  }