1 // Copyright (c) 2013 Intel Corporation. All rights reserved
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core.client;
7 import android.app.Activity;
8 import android.app.AlertDialog;
9 import android.content.Context;
10 import android.content.DialogInterface;
11 import android.net.http.SslError;
12 import android.net.Uri;
13 import android.os.Message;
14 import android.view.View;
15 import android.view.ViewGroup;
16 import android.view.WindowManager;
17 import android.widget.EditText;
18 import android.widget.FrameLayout;
19 import android.widget.LinearLayout;
20 import android.widget.TextView;
22 import org.xwalk.core.HttpAuthDatabase;
23 import org.xwalk.core.XWalkHttpAuthHandler;
24 import org.xwalk.core.R;
25 import org.xwalk.core.SslErrorHandler;
26 import org.xwalk.core.XWalkClient;
27 import org.xwalk.core.XWalkView;
29 public class XWalkDefaultClient extends XWalkClient {
31 // Strings for displaying Dialog.
32 private static String mAlertTitle;
33 private static String mSslAlertTitle;
34 private static String mOKButton;
35 private static String mCancelButton;
37 private Context mContext;
38 private AlertDialog mDialog;
39 private XWalkView mView;
40 private HttpAuthDatabase mDatabase;
41 private static final String HTTP_AUTH_DATABASE_FILE = "http_auth.db";
43 public XWalkDefaultClient(Context context, XWalkView view) {
44 mDatabase = new HttpAuthDatabase(context.getApplicationContext(), HTTP_AUTH_DATABASE_FILE);
50 * Retrieve the HTTP authentication username and password for a given
53 * @param host The host for which the credentials apply.
54 * @param realm The realm for which the credentials apply.
55 * @return String[] if found, String[0] is username, which can be null and
56 * String[1] is password. Return null if it can't find anything.
58 public String[] getHttpAuthUsernamePassword(String host, String realm) {
59 return mDatabase.getHttpAuthUsernamePassword(host, realm);
63 public void onReceivedError(XWalkView view, int errorCode,
64 String description, String failingUrl) {
65 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
66 dialogBuilder.setTitle(android.R.string.dialog_alert_title)
67 .setMessage(description)
69 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
71 public void onClick(DialogInterface dialog, int which) {
75 mDialog = dialogBuilder.create();
80 public void onReceivedSslError(XWalkView view, SslErrorHandler handler,
82 final SslErrorHandler sslHandler = handler;
83 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
84 dialogBuilder.setTitle(R.string.ssl_alert_title)
85 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
87 public void onClick(DialogInterface dialog, int which) {
92 .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
94 public void onClick(DialogInterface dialog, int which) {
99 mDialog = dialogBuilder.create();
104 * Set the HTTP authentication credentials for a given host and realm.
106 * @param host The host for the credentials.
107 * @param realm The realm for the credentials.
108 * @param username The username for the password. If it is null, it means
109 * password can't be saved.
110 * @param password The password
112 public void setHttpAuthUsernamePassword(String host, String realm,
113 String username, String password) {
114 mDatabase.setHttpAuthUsernamePassword(host, realm, username, password);
117 private void showHttpAuthDialog( final XWalkHttpAuthHandler handler,
118 final String host, final String realm) {
119 LinearLayout layout = new LinearLayout(mContext);
120 final EditText userNameEditText = new EditText(mContext);
121 final EditText passwordEditText = new EditText(mContext);
122 layout.setOrientation(LinearLayout.VERTICAL);
123 layout.setPaddingRelative(10, 0, 10, 20);
124 userNameEditText.setHint(R.string.http_auth_user_name);
125 passwordEditText.setHint(R.string.http_auth_password);
126 layout.addView(userNameEditText);
127 layout.addView(passwordEditText);
129 final Activity curActivity = mView.getActivity();
130 AlertDialog.Builder httpAuthDialog = new AlertDialog.Builder(curActivity);
131 httpAuthDialog.setTitle(R.string.http_auth_title)
133 .setCancelable(false)
134 .setPositiveButton(R.string.http_auth_log_in, new DialogInterface.OnClickListener() {
135 public void onClick(DialogInterface dialog, int whichButton) {
136 String userName = userNameEditText.getText().toString();
137 String password = passwordEditText.getText().toString();
138 setHttpAuthUsernamePassword(host, realm, userName, password);
139 handler.proceed(userName, password);
143 .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
144 public void onClick(DialogInterface dialog, int whichButton) {
153 public void onReceivedHttpAuthRequest(XWalkView view,
154 XWalkHttpAuthHandler handler, String host, String realm) {
156 showHttpAuthDialog(handler, host, realm);
161 public void onCloseWindow(XWalkView view) {
162 if (view != null && view.getActivity() != null) {
163 view.getActivity().finish();