new stuff for account authenticator
[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.content.SharedPreferences;
33 import android.graphics.Color;
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.Window;
41 import android.widget.TextView;
42 import android.widget.Toast;
43 import eu.alefzero.owncloud.AccountUtils;
44 import eu.alefzero.owncloud.R;
45 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
46 import eu.alefzero.owncloud.authenticator.AuthenticationRunnable;
47 import eu.alefzero.owncloud.authenticator.OnAuthenticationResultListener;
48 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
49
50 /**
51 * This Activity is used to add an ownCloud account to the App
52 *
53 * @author Bartek Przybylski
54 *
55 */
56 public class AuthenticatorActivity extends AccountAuthenticatorActivity implements OnAuthenticationResultListener {
57 private static final int DIALOG_LOGIN_PROGRESS = 0;
58 private static final String TAG = "AuthActivity";
59
60 private Thread mAuthThread;
61 private AuthenticationRunnable mAuthRunnable;
62 private final Handler mHandler = new Handler();
63 private boolean mUseSSLConnection;
64
65 public static final String PARAM_USERNAME = "param_Username";
66 public static final String PARAM_HOSTNAME = "param_Hostname";
67
68 public AuthenticatorActivity() {
69 mUseSSLConnection = true;
70 }
71
72 @Override
73 protected void onCreate(Bundle savedInstanceState) {
74 super.onCreate(savedInstanceState);
75 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
76 setContentView(R.layout.account_setup);
77 if (getIntent().hasExtra(PARAM_USERNAME)) {
78 String username = getIntent().getStringExtra(PARAM_HOSTNAME);
79 TextView host_text, user_text;
80 host_text = (TextView) findViewById(R.id.host_URL);
81 user_text = (TextView) findViewById(R.id.account_username);
82 host_text.setText(host_text.getText()
83 + username.substring(username.lastIndexOf('@')));
84 user_text.setText(user_text.getText()
85 + username.substring(0, username.lastIndexOf('@') - 1));
86 }
87 }
88
89 @Override
90 protected Dialog onCreateDialog(int id) {
91 Dialog dialog = null;
92 switch (id) {
93 case DIALOG_LOGIN_PROGRESS : {
94 ProgressDialog working_dialog = new ProgressDialog(this);
95 dialog = working_dialog;
96 working_dialog.setMessage(getResources().getString(R.string.auth_trying_to_login));
97 working_dialog.setIndeterminate(true);
98 working_dialog.setCancelable(true);
99 working_dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
100 @Override
101 public void onCancel(DialogInterface dialog) {
102 Log.i(getClass().getName(), "Login canceled");
103 if (mAuthThread != null) {
104 mAuthThread.interrupt();
105 finish();
106 }
107 }
108 });
109 break;
110 }
111 default :
112 Log.e(TAG, "Incorrect dialog called with id = " + id);
113 }
114 return dialog;
115 }
116
117 public void onAuthenticationResult(boolean success, String message) {
118 if (success) {
119 TextView username_text = (TextView) findViewById(R.id.account_username), password_text = (TextView) findViewById(R.id.account_password);
120
121 URL url;
122 try {
123 url = new URL(message);
124 } catch (MalformedURLException e) {
125 // should never happen
126 Log.e(getClass().getName(), "Malformed URL: " + message);
127 return;
128 }
129
130 String username = username_text.getText().toString().trim();
131 String accountName = username + "@" + url.getHost();
132 Account account = new Account(accountName,
133 AccountAuthenticator.ACCOUNT_TYPE);
134 AccountManager accManager = AccountManager.get(this);
135 accManager.addAccountExplicitly(account, password_text.getText()
136 .toString(), null);
137
138 // Add this account as default in the preferences, if there is none
139 // already
140 Account defaultAccount = AccountUtils.getCurrentOwnCloudAccount(this);
141 if (defaultAccount == null) {
142 SharedPreferences.Editor editor = PreferenceManager
143 .getDefaultSharedPreferences(this).edit();
144 editor.putString("select_oc_account", accountName);
145 editor.commit();
146 }
147
148 final Intent intent = new Intent();
149 intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE,
150 AccountAuthenticator.ACCOUNT_TYPE);
151 intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
152 intent.putExtra(AccountManager.KEY_AUTHTOKEN,
153 AccountAuthenticator.ACCOUNT_TYPE);
154 accManager.setUserData(account, AccountAuthenticator.KEY_OC_URL,
155 url.toString());
156
157 // TODO prepare this URL using a central service
158 intent.putExtra(AccountManager.KEY_USERDATA, username);
159 accManager.setUserData(
160 account,
161 AccountAuthenticator.KEY_CONTACT_URL,
162 url.toString().replace(AccountUtils.WEBDAV_PATH_2_0,
163 AccountUtils.CARDDAV_PATH_2_0));
164
165 setAccountAuthenticatorResult(intent.getExtras());
166 setResult(RESULT_OK, intent);
167 Bundle bundle = new Bundle();
168 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
169 getContentResolver().startSync(ProviderTableMeta.CONTENT_URI, bundle);
170
171 dismissDialog(0);
172 finish();
173 } else {
174 Toast.makeText(this, message, Toast.LENGTH_LONG).show();
175 dismissDialog(0);
176 }
177 }
178
179 public void onOkClick(View view) {
180 TextView url_text = (TextView) findViewById(R.id.host_URL);
181 TextView username_text = (TextView) findViewById(R.id.account_username);
182 TextView password_text = (TextView) findViewById(R.id.account_password);
183 Log.i(getClass().getName(), "OK clicked");
184 boolean hasErrors = false;
185
186 URL uri = null;
187 if (url_text.getText().toString().trim().length() == 0) {
188 url_text.setTextColor(Color.RED);
189 hasErrors = true;
190 } else {
191 url_text.setTextColor(android.R.color.black);
192 }
193 try {
194 String url_str = url_text.getText().toString();
195 if (!url_str.startsWith("http://") && !url_str.startsWith("https://")) {
196 if (mUseSSLConnection)
197 url_str = "https://" + url_str;
198 else
199 url_str = "http://" + url_str;
200 }
201 uri = new URL(url_str);
202 } catch (MalformedURLException e) {
203 url_text.setTextColor(Color.RED);
204 e.printStackTrace();
205 hasErrors = true;
206 }
207
208 if (username_text.getText().toString().contains(" ")
209 || username_text.getText().toString().trim().length() == 0) {
210 username_text.setTextColor(Color.RED);
211 hasErrors = true;
212 } else {
213 username_text.setTextColor(android.R.color.black);
214 }
215
216 if (password_text.getText().toString().trim().length() == 0) {
217 password_text.setTextColor(Color.RED);
218 hasErrors = true;
219 } else {
220 password_text.setTextColor(android.R.color.black);
221 }
222 if (hasErrors) {
223 return;
224 }
225
226 int new_port = uri.getPort();
227 if (new_port == -1) {
228 if (mUseSSLConnection)
229 new_port = 443;
230 else
231 new_port = 80;
232 }
233
234 try {
235 uri = new URL(uri.getProtocol(), uri.getHost(), new_port, uri.getPath());
236 } catch (MalformedURLException e) {
237 e.printStackTrace(); // should not happend
238 }
239
240 showDialog(DIALOG_LOGIN_PROGRESS);
241 mAuthRunnable = new AuthenticationRunnable(
242 uri,
243 username_text.getText().toString(),
244 password_text.getText().toString());
245 mAuthRunnable.setOnAuthenticationResultListener(this, mHandler);
246 mAuthThread = new Thread(mAuthRunnable);
247 mAuthThread.start();
248 }
249
250 public void sslBadgeClick(View view, String val) {
251 mUseSSLConnection = ((TextView) view).getText().equals("SSL");
252 }
253
254 public void passwordBadgeClick(View view, String val) {
255 int input_type = InputType.TYPE_CLASS_TEXT;
256 input_type |= val.equals("Hide")
257 ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
258 : InputType.TYPE_TEXT_VARIATION_PASSWORD;
259
260 ((TextView) view).setInputType(input_type);
261 }
262 }