summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBjorn Bringert <bringert@android.com>2010-10-12 10:56:20 +0100
committerBjorn Bringert <bringert@android.com>2010-10-15 09:10:25 +0100
commit2573892d6dbcda5519fcc8bd2876a27c4c9502b1 (patch)
tree676735129c04020c8257ec0328a6a870419a6eba /src
parentd0c8fb38172cd23fab968c644ba93ce2328a37b5 (diff)
downloadpackages_apps_browser-2573892d6dbcda5519fcc8bd2876a27c4c9502b1.zip
packages_apps_browser-2573892d6dbcda5519fcc8bd2876a27c4c9502b1.tar.gz
packages_apps_browser-2573892d6dbcda5519fcc8bd2876a27c4c9502b1.tar.bz2
Rewrite HTTP auth dialog code
The old code reached into the AlertDialog view using hidden view ID constants to pull out the title. I've moved the dialog to its own class and made it only use public APIs. Change-Id: I84f576ece09ca802091175e1f928ae0be4a35d07
Diffstat (limited to 'src')
-rw-r--r--src/com/android/browser/BrowserActivity.java100
-rw-r--r--src/com/android/browser/HttpAuthenticationDialog.java153
-rw-r--r--src/com/android/browser/Tab.java3
3 files changed, 173 insertions, 83 deletions
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index df2d0ea..9683b31 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -1005,18 +1005,7 @@ public class BrowserActivity extends Activity
mSSLCertificateOnErrorError);
}
if (mHttpAuthenticationDialog != null) {
- String title = ((TextView) mHttpAuthenticationDialog
- .findViewById(com.android.internal.R.id.alertTitle)).getText()
- .toString();
- String name = ((TextView) mHttpAuthenticationDialog
- .findViewById(R.id.username_edit)).getText().toString();
- String password = ((TextView) mHttpAuthenticationDialog
- .findViewById(R.id.password_edit)).getText().toString();
- int focusId = mHttpAuthenticationDialog.getCurrentFocus()
- .getId();
- mHttpAuthenticationDialog.dismiss();
- showHttpAuthentication(mHttpAuthHandler, null, null, title,
- name, password, focusId);
+ mHttpAuthenticationDialog.reshow();
}
}
@@ -3630,73 +3619,23 @@ public class BrowserActivity extends Activity
/**
* Displays an http-authentication dialog.
*/
- void showHttpAuthentication(final HttpAuthHandler handler,
- final String host, final String realm, final String title,
- final String name, final String password, int focusId) {
- LayoutInflater factory = LayoutInflater.from(this);
- final View v = factory
- .inflate(R.layout.http_authentication, null);
- if (name != null) {
- ((EditText) v.findViewById(R.id.username_edit)).setText(name);
- }
- if (password != null) {
- ((EditText) v.findViewById(R.id.password_edit)).setText(password);
- }
-
- String titleText = title;
- if (titleText == null) {
- titleText = getText(R.string.sign_in_to).toString().replace(
- "%s1", host).replace("%s2", realm);
- }
-
- mHttpAuthHandler = handler;
- AlertDialog dialog = new AlertDialog.Builder(this)
- .setTitle(titleText)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setView(v)
- .setPositiveButton(R.string.action,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int whichButton) {
- String nm = ((EditText) v
- .findViewById(R.id.username_edit))
- .getText().toString();
- String pw = ((EditText) v
- .findViewById(R.id.password_edit))
- .getText().toString();
- BrowserActivity.this.setHttpAuthUsernamePassword
- (host, realm, nm, pw);
- handler.proceed(nm, pw);
- mHttpAuthenticationDialog = null;
- mHttpAuthHandler = null;
- }})
- .setNegativeButton(R.string.cancel,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int whichButton) {
- handler.cancel();
- BrowserActivity.this.resetTitleAndRevertLockIcon();
- mHttpAuthenticationDialog = null;
- mHttpAuthHandler = null;
- }})
- .setOnCancelListener(new DialogInterface.OnCancelListener() {
- public void onCancel(DialogInterface dialog) {
- handler.cancel();
- BrowserActivity.this.resetTitleAndRevertLockIcon();
- mHttpAuthenticationDialog = null;
- mHttpAuthHandler = null;
- }})
- .create();
- // Make the IME appear when the dialog is displayed if applicable.
- dialog.getWindow().setSoftInputMode(
- WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
- dialog.show();
- if (focusId != 0) {
- dialog.findViewById(focusId).requestFocus();
- } else {
- v.findViewById(R.id.username_edit).requestFocus();
- }
- mHttpAuthenticationDialog = dialog;
+ void showHttpAuthentication(final HttpAuthHandler handler, String host, String realm) {
+ mHttpAuthenticationDialog = new HttpAuthenticationDialog(this, host, realm);
+ mHttpAuthenticationDialog.setOkListener(new HttpAuthenticationDialog.OkListener() {
+ public void onOk(String host, String realm, String username, String password) {
+ BrowserActivity.this.setHttpAuthUsernamePassword(host, realm, username, password);
+ handler.proceed(username, password);
+ mHttpAuthenticationDialog = null;
+ }
+ });
+ mHttpAuthenticationDialog.setCancelListener(new HttpAuthenticationDialog.CancelListener() {
+ public void onCancel() {
+ handler.cancel();
+ BrowserActivity.this.resetTitleAndRevertLockIcon();
+ mHttpAuthenticationDialog = null;
+ }
+ });
+ mHttpAuthenticationDialog.show();
}
public int getProgress() {
@@ -4252,8 +4191,7 @@ public class BrowserActivity extends Activity
// as HttpAuthentication has different style for landscape / portrait, we
// have to re-open it when configuration changed
- private AlertDialog mHttpAuthenticationDialog;
- private HttpAuthHandler mHttpAuthHandler;
+ private HttpAuthenticationDialog mHttpAuthenticationDialog;
/*package*/ static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
new FrameLayout.LayoutParams(
diff --git a/src/com/android/browser/HttpAuthenticationDialog.java b/src/com/android/browser/HttpAuthenticationDialog.java
new file mode 100644
index 0000000..a9ba332
--- /dev/null
+++ b/src/com/android/browser/HttpAuthenticationDialog.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.browser;
+
+import android.app.AlertDialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.WindowManager;
+import android.widget.TextView;
+
+/**
+ * HTTP authentication dialog.
+ */
+public class HttpAuthenticationDialog {
+
+ private final Context mContext;
+
+ private final String mHost;
+ private final String mRealm;
+
+ private AlertDialog mDialog;
+ private TextView mUsernameView;
+ private TextView mPasswordView;
+
+ private OkListener mOkListener;
+ private CancelListener mCancelListener;
+
+ /**
+ * Creates an HTTP authentication dialog.
+ */
+ public HttpAuthenticationDialog(Context context, String host, String realm) {
+ mContext = context;
+ mHost = host;
+ mRealm = realm;
+ createDialog();
+ }
+
+ private String getUsername() {
+ return mUsernameView.getText().toString();
+ }
+
+ private String getPassword() {
+ return mPasswordView.getText().toString();
+ }
+
+ /**
+ * Sets the listener that will be notified when the user submits the credentials.
+ */
+ public void setOkListener(OkListener okListener) {
+ mOkListener = okListener;
+ }
+
+ /**
+ * Sets the listener that will be notified when the user cancels the authentication
+ * dialog.
+ */
+ public void setCancelListener(CancelListener cancelListener) {
+ mCancelListener = cancelListener;
+ }
+
+ /**
+ * Shows the dialog.
+ */
+ public void show() {
+ mDialog.show();
+ mUsernameView.requestFocus();
+ }
+
+ /**
+ * Hides, recreates, and shows the dialog. This can be used to handle configuration changes.
+ */
+ public void reshow() {
+ String username = getUsername();
+ String password = getPassword();
+ int focusId = mDialog.getCurrentFocus().getId();
+ mDialog.dismiss();
+ createDialog();
+ mDialog.show();
+ if (username != null) {
+ mUsernameView.setText(username);
+ }
+ if (password != null) {
+ mPasswordView.setText(password);
+ }
+ if (focusId != 0) {
+ mDialog.findViewById(focusId).requestFocus();
+ } else {
+ mUsernameView.requestFocus();
+ }
+ }
+
+ private void createDialog() {
+ LayoutInflater factory = LayoutInflater.from(mContext);
+ View v = factory.inflate(R.layout.http_authentication, null);
+ mUsernameView = (TextView) v.findViewById(R.id.username_edit);
+ mPasswordView = (TextView) v.findViewById(R.id.password_edit);
+
+ String title = mContext.getText(R.string.sign_in_to).toString().replace(
+ "%s1", mHost).replace("%s2", mRealm);
+
+ mDialog = new AlertDialog.Builder(mContext)
+ .setTitle(title)
+ .setIcon(android.R.drawable.ic_dialog_alert)
+ .setView(v)
+ .setPositiveButton(R.string.action, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int whichButton) {
+ if (mOkListener != null) {
+ mOkListener.onOk(mHost, mRealm, getUsername(), getPassword());
+ }
+ }})
+ .setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int whichButton) {
+ if (mCancelListener != null) mCancelListener.onCancel();
+ }})
+ .setOnCancelListener(new DialogInterface.OnCancelListener() {
+ public void onCancel(DialogInterface dialog) {
+ if (mCancelListener != null) mCancelListener.onCancel();
+ }})
+ .create();
+
+ // Make the IME appear when the dialog is displayed if applicable.
+ mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+ }
+
+ /**
+ * Interface for listeners that are notified when the user submits the credentials.
+ */
+ public interface OkListener {
+ void onOk(String host, String realm, String username, String password);
+ }
+
+ /**
+ * Interface for listeners that are notified when the user cancels the dialog.
+ */
+ public interface CancelListener {
+ void onCancel();
+ }
+}
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index cb70e01..36566c1 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -818,8 +818,7 @@ class Tab {
handler.proceed(username, password);
} else {
if (mInForeground) {
- mActivity.showHttpAuthentication(handler, host, realm,
- null, null, null, 0);
+ mActivity.showHttpAuthentication(handler, host, realm);
} else {
handler.cancel();
}