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
; 
  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
.graphics
.Color
; 
  33 import android
.os
.Bundle
; 
  34 import android
.os
.Handler
; 
  35 import android
.util
.Log
; 
  36 import android
.view
.View
; 
  37 import android
.view
.Window
; 
  38 import android
.widget
.TextView
; 
  39 import android
.widget
.Toast
; 
  40 import eu
.alefzero
.owncloud
.R
; 
  41 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
; 
  42 import eu
.alefzero
.owncloud
.authenticator
.AuthUtils
; 
  43 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
; 
  45 public class AuthenticatorActivity 
extends AccountAuthenticatorActivity 
{ 
  46     private Thread mAuthThread
; 
  47     private final Handler mHandler 
= new Handler(); 
  49     public static final String PARAM_USERNAME 
= "param_Username"; 
  50     public static final String PARAM_HOSTNAME 
= "param_Hostname"; 
  53     protected void onCreate(Bundle savedInstanceState
) { 
  54         super.onCreate(savedInstanceState
); 
  55         getWindow().requestFeature(Window
.FEATURE_NO_TITLE
); 
  56         setContentView(R
.layout
.account_setup
); 
  57         if (getIntent().hasExtra(PARAM_USERNAME
)) { 
  58             String username 
= getIntent().getStringExtra(PARAM_HOSTNAME
); 
  59             TextView host_text
, user_text
; 
  60             host_text 
= (TextView
) findViewById(R
.id
.host_URL
); 
  61             user_text 
= (TextView
) findViewById(R
.id
.account_username
); 
  62             host_text
.setText(host_text
.getText() + username
.substring(username
.lastIndexOf('@'))); 
  63             user_text
.setText(user_text
.getText() + username
.substring(0, username
.lastIndexOf('@') - 1)); 
  68     protected Dialog 
onCreateDialog(int id
) { 
  69         final ProgressDialog dialog 
= new ProgressDialog(this); 
  70         dialog
.setMessage("Trying to login"); 
  71         dialog
.setIndeterminate(true
); 
  72         dialog
.setCancelable(true
); 
  73         dialog
.setOnCancelListener(new DialogInterface
.OnCancelListener() { 
  74             public void onCancel(DialogInterface dialog
) { 
  75                 Log
.i(getClass().getName(), "Login canceled"); 
  76                 if (mAuthThread 
!= null
) { 
  77                     mAuthThread
.interrupt(); 
  85     public void onAuthenticationResult(boolean result
, String message
) { 
  87             TextView username_text 
= (TextView
) findViewById(R
.id
.account_username
), 
  88                     password_text 
= (TextView
) findViewById(R
.id
.account_password
); 
  92                 url 
= new URL(message
); 
  93             } catch (MalformedURLException e
) { 
  94                 // should never happend 
  95                 Log
.e(getClass().getName(), "Malformed URL: " + message
); 
  99             String username 
= username_text
.getText().toString().trim(); 
 100             Account account 
= new Account(username 
+ "@" + url
.getHost(), AccountAuthenticator
.ACCOUNT_TYPE
); 
 101             AccountManager accManager 
= AccountManager
.get(this); 
 102             accManager
.addAccountExplicitly(account
, password_text
.getText().toString(), null
); 
 104             final Intent intent 
= new Intent(); 
 105             intent
.putExtra(AccountManager
.KEY_ACCOUNT_TYPE
, AccountAuthenticator
.ACCOUNT_TYPE
); 
 106             intent
.putExtra(AccountManager
.KEY_ACCOUNT_NAME
, account
.name
); 
 107             intent
.putExtra(AccountManager
.KEY_AUTHTOKEN
, AccountAuthenticator
.ACCOUNT_TYPE
); 
 108             accManager
.setUserData(account
, AccountAuthenticator
.KEY_OC_URL
, url
.toString()); 
 110             // TODO prepare this URL during a central service 
 111             intent
.putExtra(AccountManager
.KEY_USERDATA
, username
); 
 112             accManager
.setUserData(account
, AccountAuthenticator
.KEY_CONTACT_URL
, 
 113                     url
.toString().replace(AuthUtils
.WEBDAV_PATH_2_0
, AuthUtils
.CARDDAV_PATH_2_0
) 
 116             setAccountAuthenticatorResult(intent
.getExtras()); 
 117             setResult(RESULT_OK
, intent
); 
 118             Bundle bundle 
= new Bundle(); 
 119             bundle
.putBoolean(ContentResolver
.SYNC_EXTRAS_MANUAL
, true
); 
 120             getContentResolver().startSync(ProviderTableMeta
.CONTENT_URI
, bundle
); 
 125             Toast
.makeText(this, message
, Toast
.LENGTH_LONG
).show(); 
 130     public void onCancelClick(View view
) { 
 131         Log
.i(getClass().getName(), "Account creating canceled"); 
 135     public void onOkClick(View view
) { 
 136         TextView url_text 
= (TextView
) findViewById(R
.id
.host_URL
); 
 137         TextView username_text 
= (TextView
) findViewById(R
.id
.account_username
); 
 138         TextView password_text 
= (TextView
) findViewById(R
.id
.account_password
); 
 139         Log
.i(getClass().getName(), "OK clicked"); 
 140         boolean hasErrors 
= false
; 
 143         if (url_text
.getText().toString().trim().length() == 0) { 
 144             url_text
.setTextColor(Color
.RED
); 
 147             url_text
.setTextColor(Color
.BLACK
); 
 150             String url_str 
= url_text
.getText().toString(); 
 151             if (!url_str
.startsWith("http://") && 
 152                     !url_str
.startsWith("https://")) { 
 153                 url_str 
= "http://" + url_str
; 
 155             uri 
= new URL(url_str
); 
 156         } catch (MalformedURLException e
) { 
 157             url_text
.setTextColor(Color
.RED
); 
 162         if (username_text
.getText().toString().contains(" ") || 
 163                 username_text
.getText().toString().trim().length() == 0) { 
 164             username_text
.setTextColor(Color
.RED
); 
 167             username_text
.setTextColor(Color
.BLACK
); 
 170         if (password_text
.getText().toString().trim().length() == 0) { 
 171             password_text
.setTextColor(Color
.RED
); 
 174             password_text
.setTextColor(Color
.BLACK
); 
 180         mAuthThread 
= AuthUtils
.attemptAuth(uri
, 
 181                 username_text
.getText().toString(), 
 182                 password_text
.getText().toString(), 
 184                 AuthenticatorActivity
.this);