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