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