Refactoring: Added comments to every class, as well as a copyright
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / activity / AuthenticatorActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 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.graphics.Color;
33 import android.os.Bundle;
34 import android.os.Handler;
35 import android.util.Log;
36 import android.view.View;
37 import android.view.Window;
38 import android.widget.TextView;
39 import android.widget.Toast;
40 import eu.alefzero.owncloud.R;
41 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
42 import eu.alefzero.owncloud.authenticator.AuthUtils;
43 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
44
45 /**
46 * This Activity is used to add an ownCloud account to the App
47 * @author Bartek Przybylski
48 *
49 */
50 public class AuthenticatorActivity extends AccountAuthenticatorActivity {
51 private Thread mAuthThread;
52 private final Handler mHandler = new Handler();
53
54 public static final String PARAM_USERNAME = "param_Username";
55 public static final String PARAM_HOSTNAME = "param_Hostname";
56
57 @Override
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
61 setContentView(R.layout.account_setup);
62 if (getIntent().hasExtra(PARAM_USERNAME)) {
63 String username = getIntent().getStringExtra(PARAM_HOSTNAME);
64 TextView host_text, user_text;
65 host_text = (TextView) findViewById(R.id.host_URL);
66 user_text = (TextView) findViewById(R.id.account_username);
67 host_text.setText(host_text.getText() + username.substring(username.lastIndexOf('@')));
68 user_text.setText(user_text.getText() + username.substring(0, username.lastIndexOf('@') - 1));
69 }
70 }
71
72 @Override
73 protected Dialog onCreateDialog(int id) {
74 final ProgressDialog dialog = new ProgressDialog(this);
75 dialog.setMessage("Trying to login");
76 dialog.setIndeterminate(true);
77 dialog.setCancelable(true);
78 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
79 public void onCancel(DialogInterface dialog) {
80 Log.i(getClass().getName(), "Login canceled");
81 if (mAuthThread != null) {
82 mAuthThread.interrupt();
83 finish();
84 }
85 }
86 });
87 return dialog;
88 }
89
90 public void onAuthenticationResult(boolean result, String message) {
91 if (result) {
92 TextView username_text = (TextView) findViewById(R.id.account_username),
93 password_text = (TextView) findViewById(R.id.account_password);
94
95 URL url;
96 try {
97 url = new URL(message);
98 } catch (MalformedURLException e) {
99 // should never happend
100 Log.e(getClass().getName(), "Malformed URL: " + message);
101 return;
102 }
103
104 String username = username_text.getText().toString().trim();
105 Account account = new Account(username + "@" + url.getHost(), AccountAuthenticator.ACCOUNT_TYPE);
106 AccountManager accManager = AccountManager.get(this);
107 accManager.addAccountExplicitly(account, password_text.getText().toString(), null);
108
109 final Intent intent = new Intent();
110 intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, AccountAuthenticator.ACCOUNT_TYPE);
111 intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
112 intent.putExtra(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.ACCOUNT_TYPE);
113 accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL, url.toString());
114
115 // TODO prepare this URL during a central service
116 intent.putExtra(AccountManager.KEY_USERDATA, username);
117 accManager.setUserData(account, AccountAuthenticator.KEY_CONTACT_URL,
118 url.toString().replace(AuthUtils.WEBDAV_PATH_2_0, AuthUtils.CARDDAV_PATH_2_0)
119 );
120
121 setAccountAuthenticatorResult(intent.getExtras());
122 setResult(RESULT_OK, intent);
123 Bundle bundle = new Bundle();
124 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
125 getContentResolver().startSync(ProviderTableMeta.CONTENT_URI, bundle);
126
127 dismissDialog(0);
128 finish();
129 } else {
130 Toast.makeText(this, message, Toast.LENGTH_LONG).show();
131 dismissDialog(0);
132 }
133 }
134
135 public void onCancelClick(View view) {
136 Log.i(getClass().getName(), "Account creating canceled");
137 this.finish();
138 }
139
140 public void onOkClick(View view) {
141 TextView url_text = (TextView) findViewById(R.id.host_URL);
142 TextView username_text = (TextView) findViewById(R.id.account_username);
143 TextView password_text = (TextView) findViewById(R.id.account_password);
144 Log.i(getClass().getName(), "OK clicked");
145 boolean hasErrors = false;
146
147 URL uri = null;
148 if (url_text.getText().toString().trim().length() == 0) {
149 url_text.setTextColor(Color.RED);
150 hasErrors = true;
151 } else {
152 url_text.setTextColor(Color.BLACK);
153 }
154 try {
155 String url_str = url_text.getText().toString();
156 if (!url_str.startsWith("http://") &&
157 !url_str.startsWith("https://")) {
158 url_str = "http://" + url_str;
159 }
160 uri = new URL(url_str);
161 } catch (MalformedURLException e) {
162 url_text.setTextColor(Color.RED);
163 e.printStackTrace();
164 hasErrors = true;
165 }
166
167 if (username_text.getText().toString().contains(" ") ||
168 username_text.getText().toString().trim().length() == 0) {
169 username_text.setTextColor(Color.RED);
170 hasErrors = true;
171 } else {
172 username_text.setTextColor(Color.BLACK);
173 }
174
175 if (password_text.getText().toString().trim().length() == 0) {
176 password_text.setTextColor(Color.RED);
177 hasErrors = true;
178 } else {
179 password_text.setTextColor(Color.BLACK);
180 }
181 if (hasErrors) {
182 return;
183 }
184 showDialog(0);
185 mAuthThread = AuthUtils.attemptAuth(uri,
186 username_text.getText().toString(),
187 password_text.getText().toString(),
188 mHandler,
189 AuthenticatorActivity.this);
190 }
191 }