blob: 2981e65fc054e5aa5abbb2b8c1fede8e64b32456 [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 */
Bijan Amirzada41242f22014-03-21 12:12:18 -070016package com.android.browser;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080017
Bijan Amirzada41242f22014-03-21 12:12:18 -070018import com.android.browser.R;
Bjorn Bringert25738922010-10-12 10:56:20 +010019
20import android.app.AlertDialog;
21import android.content.Context;
22import android.content.DialogInterface;
John Recke8872c72011-02-28 17:52:53 -080023import android.view.KeyEvent;
Bjorn Bringert25738922010-10-12 10:56:20 +010024import android.view.LayoutInflater;
25import android.view.View;
26import android.view.WindowManager;
John Recke8872c72011-02-28 17:52:53 -080027import android.view.inputmethod.EditorInfo;
Bjorn Bringert25738922010-10-12 10:56:20 +010028import android.widget.TextView;
John Recke8872c72011-02-28 17:52:53 -080029import android.widget.TextView.OnEditorActionListener;
Bjorn Bringert25738922010-10-12 10:56:20 +010030
31/**
32 * HTTP authentication dialog.
33 */
34public class HttpAuthenticationDialog {
35
36 private final Context mContext;
37
38 private final String mHost;
39 private final String mRealm;
40
41 private AlertDialog mDialog;
42 private TextView mUsernameView;
43 private TextView mPasswordView;
44
45 private OkListener mOkListener;
46 private CancelListener mCancelListener;
47
48 /**
49 * Creates an HTTP authentication dialog.
50 */
51 public HttpAuthenticationDialog(Context context, String host, String realm) {
52 mContext = context;
53 mHost = host;
54 mRealm = realm;
55 createDialog();
56 }
57
58 private String getUsername() {
59 return mUsernameView.getText().toString();
60 }
61
62 private String getPassword() {
63 return mPasswordView.getText().toString();
64 }
65
66 /**
67 * Sets the listener that will be notified when the user submits the credentials.
68 */
69 public void setOkListener(OkListener okListener) {
70 mOkListener = okListener;
71 }
72
73 /**
74 * Sets the listener that will be notified when the user cancels the authentication
75 * dialog.
76 */
77 public void setCancelListener(CancelListener cancelListener) {
78 mCancelListener = cancelListener;
79 }
80
81 /**
82 * Shows the dialog.
83 */
84 public void show() {
85 mDialog.show();
86 mUsernameView.requestFocus();
87 }
88
89 /**
90 * Hides, recreates, and shows the dialog. This can be used to handle configuration changes.
91 */
92 public void reshow() {
93 String username = getUsername();
94 String password = getPassword();
95 int focusId = mDialog.getCurrentFocus().getId();
96 mDialog.dismiss();
97 createDialog();
98 mDialog.show();
99 if (username != null) {
100 mUsernameView.setText(username);
101 }
102 if (password != null) {
103 mPasswordView.setText(password);
104 }
105 if (focusId != 0) {
106 mDialog.findViewById(focusId).requestFocus();
107 } else {
108 mUsernameView.requestFocus();
109 }
110 }
111
112 private void createDialog() {
113 LayoutInflater factory = LayoutInflater.from(mContext);
114 View v = factory.inflate(R.layout.http_authentication, null);
115 mUsernameView = (TextView) v.findViewById(R.id.username_edit);
116 mPasswordView = (TextView) v.findViewById(R.id.password_edit);
John Recke8872c72011-02-28 17:52:53 -0800117 mPasswordView.setOnEditorActionListener(new OnEditorActionListener() {
118 @Override
119 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
120 if (actionId == EditorInfo.IME_ACTION_DONE) {
121 mDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
122 return true;
123 }
124 return false;
125 }
126 });
Bjorn Bringert25738922010-10-12 10:56:20 +0100127
128 String title = mContext.getText(R.string.sign_in_to).toString().replace(
129 "%s1", mHost).replace("%s2", mRealm);
130
131 mDialog = new AlertDialog.Builder(mContext)
132 .setTitle(title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200133 .setIconAttribute(android.R.attr.alertDialogIcon)
Bjorn Bringert25738922010-10-12 10:56:20 +0100134 .setView(v)
135 .setPositiveButton(R.string.action, new DialogInterface.OnClickListener() {
136 public void onClick(DialogInterface dialog, int whichButton) {
137 if (mOkListener != null) {
138 mOkListener.onOk(mHost, mRealm, getUsername(), getPassword());
139 }
140 }})
141 .setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
142 public void onClick(DialogInterface dialog, int whichButton) {
143 if (mCancelListener != null) mCancelListener.onCancel();
144 }})
145 .setOnCancelListener(new DialogInterface.OnCancelListener() {
146 public void onCancel(DialogInterface dialog) {
147 if (mCancelListener != null) mCancelListener.onCancel();
148 }})
149 .create();
150
151 // Make the IME appear when the dialog is displayed if applicable.
152 mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
153 }
154
155 /**
156 * Interface for listeners that are notified when the user submits the credentials.
157 */
158 public interface OkListener {
159 void onOk(String host, String realm, String username, String password);
160 }
161
162 /**
163 * Interface for listeners that are notified when the user cancels the dialog.
164 */
165 public interface CancelListener {
166 void onCancel();
167 }
168}