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 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
;
51 * This Activity is used to add an ownCloud account to the App
53 * @author Bartek Przybylski
56 public class AuthenticatorActivity
extends AccountAuthenticatorActivity
implements OnAuthenticationResultListener
{
57 private static final int DIALOG_LOGIN_PROGRESS
= 0;
58 private static final String TAG
= "AuthActivity";
60 private Thread mAuthThread
;
61 private AuthenticationRunnable mAuthRunnable
;
62 private final Handler mHandler
= new Handler();
63 private boolean mUseSSLConnection
;
65 public static final String PARAM_USERNAME
= "param_Username";
66 public static final String PARAM_HOSTNAME
= "param_Hostname";
68 public AuthenticatorActivity() {
69 mUseSSLConnection
= true
;
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));
90 protected Dialog
onCreateDialog(int 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() {
101 public void onCancel(DialogInterface dialog
) {
102 Log
.i(getClass().getName(), "Login canceled");
103 if (mAuthThread
!= null
) {
104 mAuthThread
.interrupt();
112 Log
.e(TAG
, "Incorrect dialog called with id = " + id
);
117 public void onAuthenticationResult(boolean success
, String message
) {
119 TextView username_text
= (TextView
) findViewById(R
.id
.account_username
), password_text
= (TextView
) findViewById(R
.id
.account_password
);
123 url
= new URL(message
);
124 } catch (MalformedURLException e
) {
125 // should never happen
126 Log
.e(getClass().getName(), "Malformed URL: " + message
);
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()
138 // Add this account as default in the preferences, if there is none
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
);
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
,
157 // TODO prepare this URL using a central service
158 intent
.putExtra(AccountManager
.KEY_USERDATA
, username
);
159 accManager
.setUserData(
161 AccountAuthenticator
.KEY_CONTACT_URL
,
162 url
.toString().replace(AccountUtils
.WEBDAV_PATH_2_0
,
163 AccountUtils
.CARDDAV_PATH_2_0
));
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
);
174 Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show();
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
;
187 if (url_text
.getText().toString().trim().length() == 0) {
188 url_text
.setTextColor(Color
.RED
);
191 url_text
.setTextColor(android
.R
.color
.black
);
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
;
199 url_str
= "http://" + url_str
;
201 uri
= new URL(url_str
);
202 } catch (MalformedURLException e
) {
203 url_text
.setTextColor(Color
.RED
);
208 if (username_text
.getText().toString().contains(" ")
209 || username_text
.getText().toString().trim().length() == 0) {
210 username_text
.setTextColor(Color
.RED
);
213 username_text
.setTextColor(android
.R
.color
.black
);
216 if (password_text
.getText().toString().trim().length() == 0) {
217 password_text
.setTextColor(Color
.RED
);
220 password_text
.setTextColor(android
.R
.color
.black
);
226 int new_port
= uri
.getPort();
227 if (new_port
== -1) {
228 if (mUseSSLConnection
)
235 uri
= new URL(uri
.getProtocol(), uri
.getHost(), new_port
, uri
.getPath());
236 } catch (MalformedURLException e
) {
237 e
.printStackTrace(); // should not happend
240 showDialog(DIALOG_LOGIN_PROGRESS
);
241 mAuthRunnable
= new AuthenticationRunnable(
243 username_text
.getText().toString(),
244 password_text
.getText().toString());
245 mAuthRunnable
.setOnAuthenticationResultListener(this, mHandler
);
246 mAuthThread
= new Thread(mAuthRunnable
);
250 public void sslBadgeClick(View view
, String val
) {
251 mUseSSLConnection
= ((TextView
) view
).getText().equals("SSL");
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
;
260 ((TextView
) view
).setInputType(input_type
);