1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
.ui
.activity
;
21 import java
.net
.MalformedURLException
;
24 import com
.actionbarsherlock
.ActionBarSherlock
;
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
;
52 * This Activity is used to add an ownCloud account to the App
53 * @author Bartek Przybylski
56 public class AuthenticatorActivity
extends AccountAuthenticatorActivity
{
57 private Thread mAuthThread
;
58 private final Handler mHandler
= new Handler();
59 private boolean mUseSSLConnection
;
61 public static final String PARAM_USERNAME
= "param_Username";
62 public static final String PARAM_HOSTNAME
= "param_Hostname";
64 public AuthenticatorActivity() {
65 mUseSSLConnection
= true
;
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));
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();
101 public void onAuthenticationResult(boolean result
, String message
) {
103 TextView username_text
= (TextView
) findViewById(R
.id
.account_username
),
104 password_text
= (TextView
) findViewById(R
.id
.account_password
);
108 url
= new URL(message
);
109 } catch (MalformedURLException e
) {
110 // should never happen
111 Log
.e(getClass().getName(), "Malformed URL: " + message
);
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
);
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
);
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());
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
)
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
);
150 Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show();
155 public void onCancelClick(View view
) {
156 Log
.i(getClass().getName(), "Account creating canceled");
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
;
168 if (url_text
.getText().toString().trim().length() == 0) {
169 url_text
.setTextColor(Color
.RED
);
172 url_text
.setTextColor(android
.R
.color
.black
);
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
;
181 url_str
= "http://" + url_str
;
183 uri
= new URL(url_str
);
184 } catch (MalformedURLException e
) {
185 url_text
.setTextColor(Color
.RED
);
190 if (username_text
.getText().toString().contains(" ") ||
191 username_text
.getText().toString().trim().length() == 0) {
192 username_text
.setTextColor(Color
.RED
);
195 username_text
.setTextColor(android
.R
.color
.black
);
198 if (password_text
.getText().toString().trim().length() == 0) {
199 password_text
.setTextColor(Color
.RED
);
202 password_text
.setTextColor(android
.R
.color
.black
);
208 int new_port
= uri
.getPort();
209 if (new_port
== -1) {
210 if (mUseSSLConnection
)
217 uri
= new URL(uri
.getProtocol(), uri
.getHost(), new_port
, uri
.getPath());
218 } catch (MalformedURLException e
) {
219 e
.printStackTrace(); // should not happend
223 mAuthThread
= AuthUtils
.attemptAuth(uri
,
224 username_text
.getText().toString(),
225 password_text
.getText().toString(),
227 AuthenticatorActivity
.this);
230 public void sslBadgeClick(View view
, String val
) {
231 mUseSSLConnection
= ((TextView
)view
).getText().equals("SSL");
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
);
238 ((TextView
)view
).setInputType(InputType
.TYPE_CLASS_TEXT
| InputType
.TYPE_TEXT_VARIATION_PASSWORD
);