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