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