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