blob: a9ba332e374122313ca388628e7e4b61c28f4d3d [file] [log] [blame]
Bjorn Bringert25738922010-10-12 10:56:20 +01001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.browser;
17
18import android.app.AlertDialog;
19import android.content.Context;
20import android.content.DialogInterface;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.WindowManager;
24import android.widget.TextView;
25
26/**
27 * HTTP authentication dialog.
28 */
29public class HttpAuthenticationDialog {
30
31 private final Context mContext;
32
33 private final String mHost;
34 private final String mRealm;
35
36 private AlertDialog mDialog;
37 private TextView mUsernameView;
38 private TextView mPasswordView;
39
40 private OkListener mOkListener;
41 private CancelListener mCancelListener;
42
43 /**
44 * Creates an HTTP authentication dialog.
45 */
46 public HttpAuthenticationDialog(Context context, String host, String realm) {
47 mContext = context;
48 mHost = host;
49 mRealm = realm;
50 createDialog();
51 }
52
53 private String getUsername() {
54 return mUsernameView.getText().toString();
55 }
56
57 private String getPassword() {
58 return mPasswordView.getText().toString();
59 }
60
61 /**
62 * Sets the listener that will be notified when the user submits the credentials.
63 */
64 public void setOkListener(OkListener okListener) {
65 mOkListener = okListener;
66 }
67
68 /**
69 * Sets the listener that will be notified when the user cancels the authentication
70 * dialog.
71 */
72 public void setCancelListener(CancelListener cancelListener) {
73 mCancelListener = cancelListener;
74 }
75
76 /**
77 * Shows the dialog.
78 */
79 public void show() {
80 mDialog.show();
81 mUsernameView.requestFocus();
82 }
83
84 /**
85 * Hides, recreates, and shows the dialog. This can be used to handle configuration changes.
86 */
87 public void reshow() {
88 String username = getUsername();
89 String password = getPassword();
90 int focusId = mDialog.getCurrentFocus().getId();
91 mDialog.dismiss();
92 createDialog();
93 mDialog.show();
94 if (username != null) {
95 mUsernameView.setText(username);
96 }
97 if (password != null) {
98 mPasswordView.setText(password);
99 }
100 if (focusId != 0) {
101 mDialog.findViewById(focusId).requestFocus();
102 } else {
103 mUsernameView.requestFocus();
104 }
105 }
106
107 private void createDialog() {
108 LayoutInflater factory = LayoutInflater.from(mContext);
109 View v = factory.inflate(R.layout.http_authentication, null);
110 mUsernameView = (TextView) v.findViewById(R.id.username_edit);
111 mPasswordView = (TextView) v.findViewById(R.id.password_edit);
112
113 String title = mContext.getText(R.string.sign_in_to).toString().replace(
114 "%s1", mHost).replace("%s2", mRealm);
115
116 mDialog = new AlertDialog.Builder(mContext)
117 .setTitle(title)
118 .setIcon(android.R.drawable.ic_dialog_alert)
119 .setView(v)
120 .setPositiveButton(R.string.action, new DialogInterface.OnClickListener() {
121 public void onClick(DialogInterface dialog, int whichButton) {
122 if (mOkListener != null) {
123 mOkListener.onOk(mHost, mRealm, getUsername(), getPassword());
124 }
125 }})
126 .setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
127 public void onClick(DialogInterface dialog, int whichButton) {
128 if (mCancelListener != null) mCancelListener.onCancel();
129 }})
130 .setOnCancelListener(new DialogInterface.OnCancelListener() {
131 public void onCancel(DialogInterface dialog) {
132 if (mCancelListener != null) mCancelListener.onCancel();
133 }})
134 .create();
135
136 // Make the IME appear when the dialog is displayed if applicable.
137 mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
138 }
139
140 /**
141 * Interface for listeners that are notified when the user submits the credentials.
142 */
143 public interface OkListener {
144 void onOk(String host, String realm, String username, String password);
145 }
146
147 /**
148 * Interface for listeners that are notified when the user cancels the dialog.
149 */
150 public interface CancelListener {
151 void onCancel();
152 }
153}