No more 'secure icon' when secure connection is NOT available
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / AuthenticatorActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.ui.activity;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.net.URLEncoder;
24
25 import android.accounts.Account;
26 import android.accounts.AccountAuthenticatorActivity;
27 import android.accounts.AccountManager;
28 import android.app.Dialog;
29 import android.app.ProgressDialog;
30 import android.content.ContentResolver;
31 import android.content.DialogInterface;
32 import android.content.Intent;
33 import android.content.SharedPreferences;
34 import android.os.Bundle;
35 import android.os.Handler;
36 import android.preference.PreferenceManager;
37 import android.text.InputType;
38 import android.util.Log;
39 import android.view.View;
40 import android.view.View.OnClickListener;
41 import android.view.View.OnFocusChangeListener;
42 import android.view.Window;
43 import android.widget.ImageView;
44 import android.widget.TextView;
45 import eu.alefzero.owncloud.AccountUtils;
46 import eu.alefzero.owncloud.R;
47 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
48 import eu.alefzero.owncloud.authenticator.AuthenticationRunnable;
49 import eu.alefzero.owncloud.authenticator.ConnectionCheckerRunnable;
50 import eu.alefzero.owncloud.authenticator.OnAuthenticationResultListener;
51 import eu.alefzero.owncloud.authenticator.OnConnectCheckListener;
52 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
53 import eu.alefzero.owncloud.extensions.ExtensionsAvailableActivity;
54 import eu.alefzero.owncloud.utils.OwnCloudVersion;
55
56 /**
57 * This Activity is used to add an ownCloud account to the App
58 *
59 * @author Bartek Przybylski
60 *
61 */
62 public class AuthenticatorActivity extends AccountAuthenticatorActivity
63 implements OnAuthenticationResultListener, OnConnectCheckListener,
64 OnFocusChangeListener, OnClickListener {
65 private static final int DIALOG_LOGIN_PROGRESS = 0;
66
67 private static final String TAG = "AuthActivity";
68
69 private Thread mAuthThread;
70 private AuthenticationRunnable mAuthRunnable;
71 private ConnectionCheckerRunnable mConnChkRunnable;
72 private final Handler mHandler = new Handler();
73 private String mBaseUrl;
74
75 private static final String STATUS_TEXT = "STATUS_TEXT";
76 private static final String STATUS_ICON = "STATUS_ICON";
77 private static final String STATUS_CORRECT = "STATUS_CORRECT";
78 private static final String IS_SSL_CONN = "IS_SSL_CONN";
79 private int mStatusText, mStatusIcon;
80 private boolean mStatusCorrect, mIsSslConn;
81
82 public static final String PARAM_USERNAME = "param_Username";
83 public static final String PARAM_HOSTNAME = "param_Hostname";
84
85 @Override
86 protected void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
89 setContentView(R.layout.account_setup);
90 ImageView iv = (ImageView) findViewById(R.id.refreshButton);
91 ImageView iv2 = (ImageView) findViewById(R.id.viewPassword);
92 TextView tv = (TextView) findViewById(R.id.host_URL);
93 TextView tv2 = (TextView) findViewById(R.id.account_password);
94
95 if (savedInstanceState != null) {
96 mStatusIcon = savedInstanceState.getInt(STATUS_ICON);
97 mStatusText = savedInstanceState.getInt(STATUS_TEXT);
98 mStatusCorrect = savedInstanceState.getBoolean(STATUS_CORRECT);
99 mIsSslConn = savedInstanceState.getBoolean(IS_SSL_CONN);
100 setResultIconAndText(mStatusIcon, mStatusText);
101 findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);
102 if (!mStatusCorrect)
103 iv.setVisibility(View.VISIBLE);
104 else
105 iv.setVisibility(View.INVISIBLE);
106
107 } else {
108 mStatusText = mStatusIcon = 0;
109 mStatusCorrect = false;
110 mIsSslConn = false;
111 }
112 iv.setOnClickListener(this);
113 iv2.setOnClickListener(this);
114 tv.setOnFocusChangeListener(this);
115 tv2.setOnFocusChangeListener(this);
116 }
117
118 @Override
119 protected void onSaveInstanceState(Bundle outState) {
120 outState.putInt(STATUS_ICON, mStatusIcon);
121 outState.putInt(STATUS_TEXT, mStatusText);
122 outState.putBoolean(STATUS_CORRECT, mStatusCorrect);
123 super.onSaveInstanceState(outState);
124 }
125
126 @Override
127 protected Dialog onCreateDialog(int id) {
128 Dialog dialog = null;
129 switch (id) {
130 case DIALOG_LOGIN_PROGRESS: {
131 ProgressDialog working_dialog = new ProgressDialog(this);
132 working_dialog.setMessage(getResources().getString(
133 R.string.auth_trying_to_login));
134 working_dialog.setIndeterminate(true);
135 working_dialog.setCancelable(true);
136 working_dialog
137 .setOnCancelListener(new DialogInterface.OnCancelListener() {
138 @Override
139 public void onCancel(DialogInterface dialog) {
140 Log.i(TAG, "Login canceled");
141 if (mAuthThread != null) {
142 mAuthThread.interrupt();
143 finish();
144 }
145 }
146 });
147 dialog = working_dialog;
148 break;
149 }
150 default:
151 Log.e(TAG, "Incorrect dialog called with id = " + id);
152 }
153 return dialog;
154 }
155
156 public void onAuthenticationResult(boolean success, String message) {
157 if (success) {
158 TextView username_text = (TextView) findViewById(R.id.account_username), password_text = (TextView) findViewById(R.id.account_password);
159
160 URL url;
161 try {
162 url = new URL(message);
163 } catch (MalformedURLException e) {
164 // should never happen
165 Log.e(getClass().getName(), "Malformed URL: " + message);
166 return;
167 }
168
169 String username = username_text.getText().toString().trim();
170 String accountName = username + "@" + url.getHost();
171 if (url.getPort() >= 0) {
172 accountName += ":" + url.getPort();
173 }
174 Account account = new Account(accountName,
175 AccountAuthenticator.ACCOUNT_TYPE);
176 AccountManager accManager = AccountManager.get(this);
177 accManager.addAccountExplicitly(account, password_text.getText()
178 .toString(), null);
179
180 // Add this account as default in the preferences, if there is none
181 // already
182 Account defaultAccount = AccountUtils
183 .getCurrentOwnCloudAccount(this);
184 if (defaultAccount == null) {
185 SharedPreferences.Editor editor = PreferenceManager
186 .getDefaultSharedPreferences(this).edit();
187 editor.putString("select_oc_account", accountName);
188 editor.commit();
189 }
190
191 final Intent intent = new Intent();
192 intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE,
193 AccountAuthenticator.ACCOUNT_TYPE);
194 intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
195 intent.putExtra(AccountManager.KEY_AUTHTOKEN,
196 AccountAuthenticator.ACCOUNT_TYPE);
197 intent.putExtra(AccountManager.KEY_USERDATA, username);
198
199 accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL,
200 url.toString());
201 accManager.setUserData(account,
202 AccountAuthenticator.KEY_OC_VERSION, mConnChkRunnable
203 .getDiscoveredVersion().toString());
204 accManager.setUserData(account,
205 AccountAuthenticator.KEY_OC_BASE_URL, mBaseUrl);
206
207 setAccountAuthenticatorResult(intent.getExtras());
208 setResult(RESULT_OK, intent);
209 Bundle bundle = new Bundle();
210 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
211 //getContentResolver().startSync(ProviderTableMeta.CONTENT_URI,
212 // bundle);
213 ContentResolver.requestSync(account, "org.owncloud", bundle);
214
215 /*
216 * if
217 * (mConnChkRunnable.getDiscoveredVersion().compareTo(OwnCloudVersion
218 * .owncloud_v2) >= 0) { Intent i = new Intent(this,
219 * ExtensionsAvailableActivity.class); startActivity(i); }
220 */
221
222 finish();
223 } else {
224 dismissDialog(DIALOG_LOGIN_PROGRESS);
225 TextView tv = (TextView) findViewById(R.id.account_username);
226 tv.setError(message);
227 }
228 }
229 public void onCancelClick(View view) {
230 finish();
231 }
232
233 public void onOkClick(View view) {
234 String prefix = "";
235 String url = ((TextView) findViewById(R.id.host_URL)).getText()
236 .toString();
237 if (mIsSslConn) {
238 prefix = "https://";
239 } else {
240 prefix = "http://";
241 }
242 if (url.toLowerCase().startsWith("http://")
243 || url.toLowerCase().startsWith("https://")) {
244 prefix = "";
245 }
246 continueConnection(prefix);
247 }
248
249 private void continueConnection(String prefix) {
250 String url = ((TextView) findViewById(R.id.host_URL)).getText()
251 .toString();
252 String username = ((TextView) findViewById(R.id.account_username))
253 .getText().toString();
254 String password = ((TextView) findViewById(R.id.account_password))
255 .getText().toString();
256 if (url.endsWith("/"))
257 url = url.substring(0, url.length() - 1);
258
259 URL uri = null;
260 String webdav_path = AccountUtils.getWebdavPath(mConnChkRunnable
261 .getDiscoveredVersion());
262
263 try {
264 mBaseUrl = prefix + url;
265 String url_str = prefix + url + webdav_path;
266 uri = new URL(url_str);
267 } catch (MalformedURLException e) {
268 // should not happend
269 e.printStackTrace();
270 }
271
272 showDialog(DIALOG_LOGIN_PROGRESS);
273 mAuthRunnable = new AuthenticationRunnable(uri, username, password);
274 mAuthRunnable.setOnAuthenticationResultListener(this, mHandler);
275 mAuthThread = new Thread(mAuthRunnable);
276 mAuthThread.start();
277 }
278
279 @Override
280 public void onConnectionCheckResult(ResultType type) {
281 mStatusText = mStatusIcon = 0;
282 mStatusCorrect = false;
283 String t_url = ((TextView) findViewById(R.id.host_URL)).getText()
284 .toString().toLowerCase();
285
286 switch (type) {
287 case OK_SSL:
288 mIsSslConn = true;
289 mStatusIcon = android.R.drawable.ic_secure;
290 mStatusText = R.string.auth_secure_connection;
291 mStatusCorrect = true;
292 break;
293 case OK_NO_SSL:
294 mIsSslConn = false;
295 mStatusCorrect = true;
296 if (t_url.startsWith("http://") ) {
297 mStatusText = R.string.auth_connection_established;
298 mStatusIcon = R.drawable.ic_ok;
299 } else {
300 mStatusText = R.string.auth_nossl_plain_ok_title;
301 mStatusIcon = android.R.drawable.ic_partial_secure;
302 }
303 break;
304 case TIMEOUT:
305 case INORRECT_ADDRESS:
306 case SSL_INIT_ERROR:
307 case HOST_NOT_AVAILABLE:
308 mStatusIcon = R.drawable.common_error;
309 mStatusText = R.string.auth_unknow_host_title;
310 break;
311 case NO_NETWORK_CONNECTION:
312 mStatusIcon = R.drawable.no_network;
313 mStatusText = R.string.auth_no_net_conn_title;
314 break;
315 case INSTANCE_NOT_CONFIGURED:
316 mStatusIcon = R.drawable.common_error;
317 mStatusText = R.string.auth_not_configured_title;
318 break;
319 case UNKNOWN_ERROR:
320 mStatusIcon = R.drawable.common_error;
321 mStatusText = R.string.auth_unknow_error;
322 break;
323 case FILE_NOT_FOUND:
324 mStatusIcon = R.drawable.common_error;
325 mStatusText = R.string.auth_incorrect_path_title;
326 break;
327 default:
328 Log.e(TAG, "Incorrect connection checker result type: " + type);
329 }
330 setResultIconAndText(mStatusIcon, mStatusText);
331 if (!mStatusCorrect)
332 findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);
333 else
334 findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);
335 findViewById(R.id.buttonOK).setEnabled(mStatusCorrect);
336 }
337
338 @Override
339 public void onFocusChange(View view, boolean hasFocus) {
340 if (view.getId() == R.id.host_URL) {
341 if (!hasFocus) {
342 TextView tv = ((TextView) findViewById(R.id.host_URL));
343 String uri = tv.getText().toString();
344 if (uri.length() != 0) {
345 setResultIconAndText(R.drawable.progress_small,
346 R.string.auth_testing_connection);
347 findViewById(R.id.buttonOK).setEnabled(false); // avoid connect can be clicked if the test was previously passed
348 mConnChkRunnable = new ConnectionCheckerRunnable(uri, this);
349 mConnChkRunnable.setListener(this, mHandler);
350 mAuthThread = new Thread(mConnChkRunnable);
351 mAuthThread.start();
352 } else {
353 findViewById(R.id.refreshButton).setVisibility(
354 View.INVISIBLE);
355 setResultIconAndText(0, 0);
356 }
357 }
358 } else if (view.getId() == R.id.account_password) {
359 ImageView iv = (ImageView) findViewById(R.id.viewPassword);
360 if (hasFocus) {
361 iv.setVisibility(View.VISIBLE);
362 } else {
363 TextView v = (TextView) findViewById(R.id.account_password);
364 int input_type = InputType.TYPE_CLASS_TEXT
365 | InputType.TYPE_TEXT_VARIATION_PASSWORD;
366 v.setInputType(input_type);
367 iv.setVisibility(View.INVISIBLE);
368 }
369 }
370 }
371
372 private void setResultIconAndText(int drawable_id, int text_id) {
373 ImageView iv = (ImageView) findViewById(R.id.action_indicator);
374 TextView tv = (TextView) findViewById(R.id.status_text);
375
376 if (drawable_id == 0 && text_id == 0) {
377 iv.setVisibility(View.INVISIBLE);
378 tv.setVisibility(View.INVISIBLE);
379 } else {
380 iv.setImageResource(drawable_id);
381 tv.setText(text_id);
382 iv.setVisibility(View.VISIBLE);
383 tv.setVisibility(View.VISIBLE);
384 }
385 }
386
387 @Override
388 public void onClick(View v) {
389 if (v.getId() == R.id.refreshButton) {
390 onFocusChange(findViewById(R.id.host_URL), false);
391 } else if (v.getId() == R.id.viewPassword) {
392 TextView view = (TextView) findViewById(R.id.account_password);
393 int input_type = InputType.TYPE_CLASS_TEXT
394 | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
395 view.setInputType(input_type);
396 }
397 }
398 }